blob: edcb7dfabe5a52953926a54a79d684840df75b15 [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>
Eric Fiselier3816ef92016-07-21 03:20:17 +0000102 reference emplace_back(Args&&... args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000103 void pop_back();
104
105 template <class... Args> iterator emplace(const_iterator position, Args&&... args);
106 iterator insert(const_iterator position, const value_type& x);
107 iterator insert(const_iterator position, value_type&& x);
108 iterator insert(const_iterator position, size_type n, const value_type& x);
109 template <class InputIterator>
110 iterator insert(const_iterator position, InputIterator first, InputIterator last);
111 iterator insert(const_iterator position, initializer_list<value_type> il);
112
113 iterator erase(const_iterator position);
114 iterator erase(const_iterator first, const_iterator last);
115
Howard 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);
Eric Fiselier3816ef92016-07-21 03:20:17 +0000221 template <class... Args> reference 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() {}
Marshall Clow14c09a22016-08-25 15:09:01 +0000293 _LIBCPP_NORETURN void __throw_length_error() const;
294 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000295};
296
297template <bool __b>
298void
299__vector_base_common<__b>::__throw_length_error() const
300{
Marshall Clow14c09a22016-08-25 15:09:01 +0000301 _VSTD::__throw_length_error("vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000302}
303
304template <bool __b>
305void
306__vector_base_common<__b>::__throw_out_of_range() const
307{
Marshall Clow14c09a22016-08-25 15:09:01 +0000308 _VSTD::__throw_out_of_range("vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000309}
310
Howard Hinnante9df0a52013-08-01 18:17:34 +0000311#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000312#pragma warning( push )
313#pragma warning( disable: 4231 )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000314#endif // _LIBCPP_MSVC
Eric Fiselier833d6442016-09-15 22:27:07 +0000315_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __vector_base_common<true>)
Howard Hinnante9df0a52013-08-01 18:17:34 +0000316#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000317#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000318#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000319
320template <class _Tp, class _Allocator>
321class __vector_base
322 : protected __vector_base_common<true>
323{
324protected:
Howard Hinnant324bb032010-08-22 00:02:43 +0000325 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000326 typedef _Allocator allocator_type;
327 typedef allocator_traits<allocator_type> __alloc_traits;
328 typedef value_type& reference;
329 typedef const value_type& const_reference;
330 typedef typename __alloc_traits::size_type size_type;
331 typedef typename __alloc_traits::difference_type difference_type;
332 typedef typename __alloc_traits::pointer pointer;
333 typedef typename __alloc_traits::const_pointer const_pointer;
334 typedef pointer iterator;
335 typedef const_pointer const_iterator;
336
337 pointer __begin_;
338 pointer __end_;
339 __compressed_pair<pointer, allocator_type> __end_cap_;
340
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000341 _LIBCPP_INLINE_VISIBILITY
342 allocator_type& __alloc() _NOEXCEPT
343 {return __end_cap_.second();}
344 _LIBCPP_INLINE_VISIBILITY
345 const allocator_type& __alloc() const _NOEXCEPT
346 {return __end_cap_.second();}
347 _LIBCPP_INLINE_VISIBILITY
348 pointer& __end_cap() _NOEXCEPT
349 {return __end_cap_.first();}
350 _LIBCPP_INLINE_VISIBILITY
351 const pointer& __end_cap() const _NOEXCEPT
352 {return __end_cap_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000353
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000354 _LIBCPP_INLINE_VISIBILITY
355 __vector_base()
356 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000357 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000358 ~__vector_base();
359
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000360 _LIBCPP_INLINE_VISIBILITY
361 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
362 _LIBCPP_INLINE_VISIBILITY
363 size_type capacity() const _NOEXCEPT
364 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000365
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000367 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000368
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000370 void __copy_assign_alloc(const __vector_base& __c)
371 {__copy_assign_alloc(__c, integral_constant<bool,
372 __alloc_traits::propagate_on_container_copy_assignment::value>());}
373
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375 void __move_assign_alloc(__vector_base& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000376 _NOEXCEPT_(
377 !__alloc_traits::propagate_on_container_move_assignment::value ||
378 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000379 {__move_assign_alloc(__c, integral_constant<bool,
380 __alloc_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381private:
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000383 void __copy_assign_alloc(const __vector_base& __c, true_type)
384 {
385 if (__alloc() != __c.__alloc())
386 {
387 clear();
388 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
389 __begin_ = __end_ = __end_cap() = nullptr;
390 }
391 __alloc() = __c.__alloc();
392 }
393
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000395 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000396 {}
397
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000399 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000400 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000401 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000402 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000403 }
404
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000406 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000407 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000408 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409};
410
411template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000412inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000413void
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000414__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000415{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000416 while (__new_last != __end_)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000417 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000418}
419
420template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000421inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000422__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000423 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000424 : __begin_(nullptr),
425 __end_(nullptr),
426 __end_cap_(nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000427{
428}
429
430template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000431inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000432__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000433 : __begin_(nullptr),
434 __end_(nullptr),
435 __end_cap_(nullptr, __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000436{
437}
438
439template <class _Tp, class _Allocator>
440__vector_base<_Tp, _Allocator>::~__vector_base()
441{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000442 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000443 {
444 clear();
445 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
446 }
447}
448
Eric Fiselierb3792282016-02-20 00:19:45 +0000449template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000450class _LIBCPP_TYPE_VIS_ONLY vector
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000451 : private __vector_base<_Tp, _Allocator>
452{
453private:
454 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000455 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant324bb032010-08-22 00:02:43 +0000456public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000457 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +0000458 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000459 typedef _Allocator allocator_type;
460 typedef typename __base::__alloc_traits __alloc_traits;
461 typedef typename __base::reference reference;
462 typedef typename __base::const_reference const_reference;
463 typedef typename __base::size_type size_type;
464 typedef typename __base::difference_type difference_type;
465 typedef typename __base::pointer pointer;
466 typedef typename __base::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000467 typedef __wrap_iter<pointer> iterator;
468 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000469 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
470 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000471
Howard Hinnant02d5e182013-03-26 19:04:56 +0000472 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
473 "Allocator::value_type must be same type as value_type");
474
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000475 _LIBCPP_INLINE_VISIBILITY
Marshall Clowc912c0c2015-06-04 02:05:41 +0000476 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant7a563db2011-09-14 18:33:51 +0000477 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000478#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000479 __get_db()->__insert_c(this);
480#endif
481 }
482 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow127db912015-06-04 00:10:20 +0000483#if _LIBCPP_STD_VER <= 14
484 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
485#else
486 _NOEXCEPT
487#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +0000488 : __base(__a)
489 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000490#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000491 __get_db()->__insert_c(this);
492#endif
493 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000494 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59 +0000495#if _LIBCPP_STD_VER > 11
496 explicit vector(size_type __n, const allocator_type& __a);
497#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000498 vector(size_type __n, const_reference __x);
499 vector(size_type __n, const_reference __x, const allocator_type& __a);
500 template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +0000501 vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000502 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000503 !__is_forward_iterator<_InputIterator>::value &&
504 is_constructible<
505 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +0000506 typename iterator_traits<_InputIterator>::reference>::value,
507 _InputIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000508 template <class _InputIterator>
509 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
510 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000511 !__is_forward_iterator<_InputIterator>::value &&
512 is_constructible<
513 value_type,
514 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000515 template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +0000516 vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000517 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
518 is_constructible<
519 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +0000520 typename iterator_traits<_ForwardIterator>::reference>::value,
521 _ForwardIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000522 template <class _ForwardIterator>
523 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000524 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
525 is_constructible<
526 value_type,
527 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000528#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000530 vector(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000532 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000533#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantabe26282011-09-16 17:29:17 +0000534#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7a563db2011-09-14 18:33:51 +0000536 ~vector()
537 {
538 __get_db()->__erase_c(this);
539 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000540#endif
541
542 vector(const vector& __x);
543 vector(const vector& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000545 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000546#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000548 vector(vector&& __x)
Marshall Clow119ed472015-07-14 14:46:32 +0000549#if _LIBCPP_STD_VER > 14
550 _NOEXCEPT;
551#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000552 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow119ed472015-07-14 14:46:32 +0000553#endif
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000555 vector(vector&& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000557 vector& operator=(vector&& __x)
Marshall Clowaf961ed2015-08-18 18:57:00 +0000558 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnant73d21a42010-09-04 23:28:19 +0000559#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000560#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000562 vector& operator=(initializer_list<value_type> __il)
563 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000564#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000565
566 template <class _InputIterator>
567 typename enable_if
568 <
569 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000570 !__is_forward_iterator<_InputIterator>::value &&
571 is_constructible<
572 value_type,
573 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000574 void
575 >::type
576 assign(_InputIterator __first, _InputIterator __last);
577 template <class _ForwardIterator>
578 typename enable_if
579 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000580 __is_forward_iterator<_ForwardIterator>::value &&
581 is_constructible<
582 value_type,
583 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000584 void
585 >::type
586 assign(_ForwardIterator __first, _ForwardIterator __last);
587
588 void assign(size_type __n, const_reference __u);
Howard Hinnante3e32912011-08-12 21:56:02 +0000589#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000591 void assign(initializer_list<value_type> __il)
592 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000593#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000594
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000595 _LIBCPP_INLINE_VISIBILITY
596 allocator_type get_allocator() const _NOEXCEPT
597 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000598
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000599 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
600 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
601 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
602 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000603
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000604 _LIBCPP_INLINE_VISIBILITY
605 reverse_iterator rbegin() _NOEXCEPT
606 {return reverse_iterator(end());}
607 _LIBCPP_INLINE_VISIBILITY
608 const_reverse_iterator rbegin() const _NOEXCEPT
609 {return const_reverse_iterator(end());}
610 _LIBCPP_INLINE_VISIBILITY
611 reverse_iterator rend() _NOEXCEPT
612 {return reverse_iterator(begin());}
613 _LIBCPP_INLINE_VISIBILITY
614 const_reverse_iterator rend() const _NOEXCEPT
615 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000616
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000617 _LIBCPP_INLINE_VISIBILITY
618 const_iterator cbegin() const _NOEXCEPT
619 {return begin();}
620 _LIBCPP_INLINE_VISIBILITY
621 const_iterator cend() const _NOEXCEPT
622 {return end();}
623 _LIBCPP_INLINE_VISIBILITY
624 const_reverse_iterator crbegin() const _NOEXCEPT
625 {return rbegin();}
626 _LIBCPP_INLINE_VISIBILITY
627 const_reverse_iterator crend() const _NOEXCEPT
628 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000629
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000630 _LIBCPP_INLINE_VISIBILITY
631 size_type size() const _NOEXCEPT
632 {return static_cast<size_type>(this->__end_ - this->__begin_);}
633 _LIBCPP_INLINE_VISIBILITY
634 size_type capacity() const _NOEXCEPT
635 {return __base::capacity();}
636 _LIBCPP_INLINE_VISIBILITY
637 bool empty() const _NOEXCEPT
638 {return this->__begin_ == this->__end_;}
639 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000640 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000641 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000642
643 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
644 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
645 reference at(size_type __n);
646 const_reference at(size_type __n) const;
647
Howard Hinnant7a563db2011-09-14 18:33:51 +0000648 _LIBCPP_INLINE_VISIBILITY reference front()
649 {
650 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
651 return *this->__begin_;
652 }
653 _LIBCPP_INLINE_VISIBILITY const_reference front() const
654 {
655 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
656 return *this->__begin_;
657 }
658 _LIBCPP_INLINE_VISIBILITY reference back()
659 {
660 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
661 return *(this->__end_ - 1);
662 }
663 _LIBCPP_INLINE_VISIBILITY const_reference back() const
664 {
665 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
666 return *(this->__end_ - 1);
667 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000668
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000669 _LIBCPP_INLINE_VISIBILITY
670 value_type* data() _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000671 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000672 _LIBCPP_INLINE_VISIBILITY
673 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000674 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000675
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000676 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000677#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000678 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000679#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000680 template <class... _Args>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000681 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier3816ef92016-07-21 03:20:17 +0000682 reference emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000683#endif // _LIBCPP_HAS_NO_VARIADICS
684#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000686 void pop_back();
687
688 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000689#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000690 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000691#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000692 template <class... _Args>
693 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000694#endif // _LIBCPP_HAS_NO_VARIADICS
695#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000696 iterator insert(const_iterator __position, size_type __n, const_reference __x);
697 template <class _InputIterator>
698 typename enable_if
699 <
700 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000701 !__is_forward_iterator<_InputIterator>::value &&
702 is_constructible<
703 value_type,
704 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000705 iterator
706 >::type
707 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
708 template <class _ForwardIterator>
709 typename enable_if
710 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000711 __is_forward_iterator<_ForwardIterator>::value &&
712 is_constructible<
713 value_type,
714 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000715 iterator
716 >::type
717 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +0000718#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000720 iterator insert(const_iterator __position, initializer_list<value_type> __il)
721 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000722#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000724 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000725 iterator erase(const_iterator __first, const_iterator __last);
726
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000727 _LIBCPP_INLINE_VISIBILITY
728 void clear() _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000729 {
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000730 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000731 __base::clear();
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000732 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000733 __invalidate_all_iterators();
734 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000735
736 void resize(size_type __sz);
737 void resize(size_type __sz, const_reference __x);
738
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000739 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +0000740#if _LIBCPP_STD_VER >= 14
741 _NOEXCEPT;
742#else
743 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
744 __is_nothrow_swappable<allocator_type>::value);
745#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000746
747 bool __invariants() const;
748
Howard Hinnantabe26282011-09-16 17:29:17 +0000749#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000750
751 bool __dereferenceable(const const_iterator* __i) const;
752 bool __decrementable(const const_iterator* __i) const;
753 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
754 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
755
Howard Hinnantabe26282011-09-16 17:29:17 +0000756#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000757
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000758private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000759 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000760 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000761 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000762 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31 +0000763 void __construct_at_end(size_type __n);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000764 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000766 template <class _ForwardIterator>
767 typename enable_if
768 <
769 __is_forward_iterator<_ForwardIterator>::value,
770 void
771 >::type
Eric Fiselier088ed9f2015-03-31 16:54:19 +0000772 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000773 void __append(size_type __n);
774 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000776 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000778 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000779 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
780 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
781 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000782 void __move_assign(vector& __c, true_type)
783 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clowaf961ed2015-08-18 18:57:00 +0000784 void __move_assign(vector& __c, false_type)
785 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000787 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000788 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000789#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000790 __c_node* __c = __get_db()->__find_c_and_lock(this);
791 for (__i_node** __p = __c->end_; __p != __c->beg_; )
792 {
793 --__p;
794 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
795 if (__i->base() > __new_last)
796 {
797 (*__p)->__c_ = nullptr;
798 if (--__c->end_ != __p)
799 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
800 }
801 }
802 __get_db()->unlock();
803#endif
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000804 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000805 __base::__destruct_at_end(__new_last);
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000806 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000807 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000808 template <class _Up>
809 void
810#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
811 __push_back_slow_path(_Up&& __x);
812#else
813 __push_back_slow_path(_Up& __x);
814#endif
Howard Hinnant0438ea22012-02-26 15:30:12 +0000815#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
816 template <class... _Args>
817 void
818 __emplace_back_slow_path(_Args&&... __args);
819#endif
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000820 // The following functions are no-ops outside of AddressSanitizer mode.
821 // We call annotatations only for the default Allocator because other allocators
822 // may not meet the AddressSanitizer alignment constraints.
823 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
824 void __annotate_contiguous_container
Kostya Serebryany497f9122014-09-02 23:43:38 +0000825 (const void *__beg, const void *__end, const void *__old_mid, const void *__new_mid) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000826 {
827#ifndef _LIBCPP_HAS_NO_ASAN
828 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
829 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
830#endif
831 }
832
Kostya Serebryany497f9122014-09-02 23:43:38 +0000833 void __annotate_new(size_type __current_size) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000834 {
835 __annotate_contiguous_container(data(), data() + capacity(),
836 data() + capacity(), data() + __current_size);
837 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000838 void __annotate_delete() const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000839 {
840 __annotate_contiguous_container(data(), data() + capacity(),
841 data() + size(), data() + capacity());
842 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000843 void __annotate_increase(size_type __n) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000844 {
845 __annotate_contiguous_container(data(), data() + capacity(),
846 data() + size(), data() + size() + __n);
847 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000848 void __annotate_shrink(size_type __old_size) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000849 {
850 __annotate_contiguous_container(data(), data() + capacity(),
851 data() + __old_size, data() + size());
852 }
Marshall Clow26f472d2014-09-03 21:37:43 +0000853#ifndef _LIBCPP_HAS_NO_ASAN
Kostya Serebryany497f9122014-09-02 23:43:38 +0000854 // The annotation for size increase should happen before the actual increase,
855 // but if an exception is thrown after that the annotation has to be undone.
856 struct __RAII_IncreaseAnnotator {
857 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000858 : __commit(false), __v(__v), __old_size(__v.size() + __n) {
Kostya Serebryany497f9122014-09-02 23:43:38 +0000859 __v.__annotate_increase(__n);
860 }
861 void __done() { __commit = true; }
862 ~__RAII_IncreaseAnnotator() {
863 if (__commit) return;
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000864 __v.__annotate_shrink(__old_size);
Kostya Serebryany497f9122014-09-02 23:43:38 +0000865 }
866 bool __commit;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000867 const vector &__v;
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000868 size_type __old_size;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000869 };
Marshall Clow26f472d2014-09-03 21:37:43 +0000870#else
871 struct __RAII_IncreaseAnnotator {
872 inline __RAII_IncreaseAnnotator(const vector &, size_type __n = 1) {}
873 inline void __done() {}
874 };
875#endif
876
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000877};
878
879template <class _Tp, class _Allocator>
880void
881vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
882{
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000883 __annotate_delete();
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000884 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000885 _VSTD::swap(this->__begin_, __v.__begin_);
886 _VSTD::swap(this->__end_, __v.__end_);
887 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000888 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000889 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000890 __invalidate_all_iterators();
891}
892
893template <class _Tp, class _Allocator>
894typename vector<_Tp, _Allocator>::pointer
895vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
896{
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000897 __annotate_delete();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000898 pointer __r = __v.__begin_;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000899 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
900 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000901 _VSTD::swap(this->__begin_, __v.__begin_);
902 _VSTD::swap(this->__end_, __v.__end_);
903 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000904 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000905 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000906 __invalidate_all_iterators();
907 return __r;
908}
909
910// Allocate space for __n objects
911// throws length_error if __n > max_size()
912// throws (probably bad_alloc) if memory run out
913// Precondition: __begin_ == __end_ == __end_cap() == 0
914// Precondition: __n > 0
915// Postcondition: capacity() == __n
916// Postcondition: size() == 0
917template <class _Tp, class _Allocator>
918void
919vector<_Tp, _Allocator>::allocate(size_type __n)
920{
921 if (__n > max_size())
922 this->__throw_length_error();
923 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
924 this->__end_cap() = this->__begin_ + __n;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000925 __annotate_new(0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000926}
927
928template <class _Tp, class _Allocator>
929void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000930vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000931{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000932 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000933 {
934 clear();
935 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000936 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000937 }
938}
939
940template <class _Tp, class _Allocator>
941typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000942vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000943{
Sean Hunt110b8bf2011-07-29 23:31:58 +0000944 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 +0000945}
946
947// Precondition: __new_size > capacity()
948template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000949inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000950typename vector<_Tp, _Allocator>::size_type
951vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
952{
953 const size_type __ms = max_size();
954 if (__new_size > __ms)
955 this->__throw_length_error();
956 const size_type __cap = capacity();
957 if (__cap >= __ms / 2)
958 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58 +0000959 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000960}
961
962// Default constructs __n objects starting at __end_
963// throws if construction throws
964// Precondition: __n > 0
965// Precondition: size() + __n <= capacity()
966// Postcondition: size() == size() + __n
967template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000968void
969vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
970{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000971 allocator_type& __a = this->__alloc();
972 do
973 {
Kostya Serebryany497f9122014-09-02 23:43:38 +0000974 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000975 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000976 ++this->__end_;
977 --__n;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000978 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000979 } while (__n > 0);
980}
981
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000982// Copy constructs __n objects starting at __end_ from __x
983// throws if construction throws
984// Precondition: __n > 0
985// Precondition: size() + __n <= capacity()
986// Postcondition: size() == old size() + __n
987// Postcondition: [i] == __x for all i in [size() - __n, __n)
988template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000989inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990void
991vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
992{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000993 allocator_type& __a = this->__alloc();
994 do
995 {
Kostya Serebryany497f9122014-09-02 23:43:38 +0000996 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000997 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000998 ++this->__end_;
999 --__n;
Kostya Serebryany497f9122014-09-02 23:43:38 +00001000 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001001 } while (__n > 0);
1002}
1003
1004template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001005template <class _ForwardIterator>
1006typename enable_if
1007<
1008 __is_forward_iterator<_ForwardIterator>::value,
1009 void
1010>::type
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001011vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001012{
1013 allocator_type& __a = this->__alloc();
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001014 __RAII_IncreaseAnnotator __annotator(*this, __n);
1015 __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
1016 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001017}
1018
1019// Default constructs __n objects starting at __end_
1020// throws if construction throws
1021// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001022// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001023template <class _Tp, class _Allocator>
1024void
1025vector<_Tp, _Allocator>::__append(size_type __n)
1026{
1027 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1028 this->__construct_at_end(__n);
1029 else
1030 {
1031 allocator_type& __a = this->__alloc();
1032 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1033 __v.__construct_at_end(__n);
1034 __swap_out_circular_buffer(__v);
1035 }
1036}
1037
1038// Default constructs __n objects starting at __end_
1039// throws if construction throws
1040// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001041// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001042template <class _Tp, class _Allocator>
1043void
1044vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1045{
1046 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1047 this->__construct_at_end(__n, __x);
1048 else
1049 {
1050 allocator_type& __a = this->__alloc();
1051 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1052 __v.__construct_at_end(__n, __x);
1053 __swap_out_circular_buffer(__v);
1054 }
1055}
1056
1057template <class _Tp, class _Allocator>
1058vector<_Tp, _Allocator>::vector(size_type __n)
1059{
Howard Hinnant0442b122011-09-16 18:41:29 +00001060#if _LIBCPP_DEBUG_LEVEL >= 2
1061 __get_db()->__insert_c(this);
1062#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001063 if (__n > 0)
1064 {
1065 allocate(__n);
1066 __construct_at_end(__n);
1067 }
1068}
1069
Marshall Clowa49a2c92013-09-14 00:47:59 +00001070#if _LIBCPP_STD_VER > 11
1071template <class _Tp, class _Allocator>
1072vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1073 : __base(__a)
1074{
1075#if _LIBCPP_DEBUG_LEVEL >= 2
1076 __get_db()->__insert_c(this);
1077#endif
1078 if (__n > 0)
1079 {
1080 allocate(__n);
1081 __construct_at_end(__n);
1082 }
1083}
1084#endif
1085
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001086template <class _Tp, class _Allocator>
1087vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1088{
Howard Hinnant0442b122011-09-16 18:41:29 +00001089#if _LIBCPP_DEBUG_LEVEL >= 2
1090 __get_db()->__insert_c(this);
1091#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001092 if (__n > 0)
1093 {
1094 allocate(__n);
1095 __construct_at_end(__n, __x);
1096 }
1097}
1098
1099template <class _Tp, class _Allocator>
1100vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1101 : __base(__a)
1102{
Howard Hinnant0442b122011-09-16 18:41:29 +00001103#if _LIBCPP_DEBUG_LEVEL >= 2
1104 __get_db()->__insert_c(this);
1105#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001106 if (__n > 0)
1107 {
1108 allocate(__n);
1109 __construct_at_end(__n, __x);
1110 }
1111}
1112
1113template <class _Tp, class _Allocator>
1114template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +00001115vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001116 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001117 !__is_forward_iterator<_InputIterator>::value &&
1118 is_constructible<
1119 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +00001120 typename iterator_traits<_InputIterator>::reference>::value,
1121 _InputIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001122{
Howard Hinnantabe26282011-09-16 17:29:17 +00001123#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001124 __get_db()->__insert_c(this);
1125#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001126 for (; __first != __last; ++__first)
1127 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001128}
1129
1130template <class _Tp, class _Allocator>
1131template <class _InputIterator>
1132vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1133 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001134 !__is_forward_iterator<_InputIterator>::value &&
1135 is_constructible<
1136 value_type,
1137 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001138 : __base(__a)
1139{
Howard Hinnantabe26282011-09-16 17:29:17 +00001140#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001141 __get_db()->__insert_c(this);
1142#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001143 for (; __first != __last; ++__first)
1144 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001145}
1146
1147template <class _Tp, class _Allocator>
1148template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +00001149vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001150 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1151 is_constructible<
1152 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +00001153 typename iterator_traits<_ForwardIterator>::reference>::value,
1154 _ForwardIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001155{
Howard Hinnant0442b122011-09-16 18:41:29 +00001156#if _LIBCPP_DEBUG_LEVEL >= 2
1157 __get_db()->__insert_c(this);
1158#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001159 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001160 if (__n > 0)
1161 {
1162 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001163 __construct_at_end(__first, __last, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001164 }
1165}
1166
1167template <class _Tp, class _Allocator>
1168template <class _ForwardIterator>
1169vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001170 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1171 is_constructible<
1172 value_type,
1173 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001174 : __base(__a)
1175{
Howard Hinnant0442b122011-09-16 18:41:29 +00001176#if _LIBCPP_DEBUG_LEVEL >= 2
1177 __get_db()->__insert_c(this);
1178#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001179 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001180 if (__n > 0)
1181 {
1182 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001183 __construct_at_end(__first, __last, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001184 }
1185}
1186
1187template <class _Tp, class _Allocator>
1188vector<_Tp, _Allocator>::vector(const vector& __x)
1189 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1190{
Howard Hinnant0442b122011-09-16 18:41:29 +00001191#if _LIBCPP_DEBUG_LEVEL >= 2
1192 __get_db()->__insert_c(this);
1193#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001194 size_type __n = __x.size();
1195 if (__n > 0)
1196 {
1197 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001198 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001199 }
1200}
1201
1202template <class _Tp, class _Allocator>
1203vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1204 : __base(__a)
1205{
Howard Hinnant0442b122011-09-16 18:41:29 +00001206#if _LIBCPP_DEBUG_LEVEL >= 2
1207 __get_db()->__insert_c(this);
1208#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001209 size_type __n = __x.size();
1210 if (__n > 0)
1211 {
1212 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001213 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001214 }
1215}
1216
Howard Hinnant73d21a42010-09-04 23:28:19 +00001217#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001218
1219template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001220inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001221vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clow119ed472015-07-14 14:46:32 +00001222#if _LIBCPP_STD_VER > 14
1223 _NOEXCEPT
1224#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001225 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow119ed472015-07-14 14:46:32 +00001226#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001227 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001228{
Howard Hinnantabe26282011-09-16 17:29:17 +00001229#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001230 __get_db()->__insert_c(this);
Howard Hinnante6125bd2011-09-19 16:34:29 +00001231 __get_db()->swap(this, &__x);
Howard Hinnant7a563db2011-09-14 18:33:51 +00001232#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001233 this->__begin_ = __x.__begin_;
1234 this->__end_ = __x.__end_;
1235 this->__end_cap() = __x.__end_cap();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001236 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001237}
1238
1239template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001240inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001241vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1242 : __base(__a)
1243{
Howard Hinnant0442b122011-09-16 18:41:29 +00001244#if _LIBCPP_DEBUG_LEVEL >= 2
1245 __get_db()->__insert_c(this);
1246#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001247 if (__a == __x.__alloc())
1248 {
1249 this->__begin_ = __x.__begin_;
1250 this->__end_ = __x.__end_;
1251 this->__end_cap() = __x.__end_cap();
1252 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001253#if _LIBCPP_DEBUG_LEVEL >= 2
1254 __get_db()->swap(this, &__x);
1255#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001256 }
1257 else
1258 {
Howard Hinnant99968442011-11-29 18:15:50 +00001259 typedef move_iterator<iterator> _Ip;
1260 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001261 }
1262}
1263
Howard Hinnante3e32912011-08-12 21:56:02 +00001264#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1265
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001266template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001267inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001268vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1269{
Howard Hinnant0442b122011-09-16 18:41:29 +00001270#if _LIBCPP_DEBUG_LEVEL >= 2
1271 __get_db()->__insert_c(this);
1272#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001273 if (__il.size() > 0)
1274 {
1275 allocate(__il.size());
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001276 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001277 }
1278}
1279
1280template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001281inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001282vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1283 : __base(__a)
1284{
Howard Hinnant0442b122011-09-16 18:41:29 +00001285#if _LIBCPP_DEBUG_LEVEL >= 2
1286 __get_db()->__insert_c(this);
1287#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001288 if (__il.size() > 0)
1289 {
1290 allocate(__il.size());
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001291 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001292 }
1293}
1294
Howard Hinnante3e32912011-08-12 21:56:02 +00001295#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1296
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001297template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001298inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001299vector<_Tp, _Allocator>&
1300vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001301 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001302{
1303 __move_assign(__x, integral_constant<bool,
1304 __alloc_traits::propagate_on_container_move_assignment::value>());
1305 return *this;
1306}
1307
1308template <class _Tp, class _Allocator>
1309void
1310vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001311 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001312{
1313 if (__base::__alloc() != __c.__alloc())
1314 {
Howard Hinnant99968442011-11-29 18:15:50 +00001315 typedef move_iterator<iterator> _Ip;
1316 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001317 }
1318 else
1319 __move_assign(__c, true_type());
1320}
1321
1322template <class _Tp, class _Allocator>
1323void
1324vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001325 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326{
1327 deallocate();
Marshall Clow3c2eac62014-07-21 15:11:13 +00001328 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001329 this->__begin_ = __c.__begin_;
1330 this->__end_ = __c.__end_;
1331 this->__end_cap() = __c.__end_cap();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001332 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001333#if _LIBCPP_DEBUG_LEVEL >= 2
1334 __get_db()->swap(this, &__c);
1335#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001336}
1337
Howard Hinnant73d21a42010-09-04 23:28:19 +00001338#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001339
1340template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001341inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001342vector<_Tp, _Allocator>&
1343vector<_Tp, _Allocator>::operator=(const vector& __x)
1344{
1345 if (this != &__x)
1346 {
1347 __base::__copy_assign_alloc(__x);
1348 assign(__x.__begin_, __x.__end_);
1349 }
1350 return *this;
1351}
1352
1353template <class _Tp, class _Allocator>
1354template <class _InputIterator>
1355typename enable_if
1356<
1357 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001358 !__is_forward_iterator<_InputIterator>::value &&
1359 is_constructible<
1360 _Tp,
1361 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001362 void
1363>::type
1364vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1365{
1366 clear();
1367 for (; __first != __last; ++__first)
1368 push_back(*__first);
1369}
1370
1371template <class _Tp, class _Allocator>
1372template <class _ForwardIterator>
1373typename enable_if
1374<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001375 __is_forward_iterator<_ForwardIterator>::value &&
1376 is_constructible<
1377 _Tp,
1378 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001379 void
1380>::type
1381vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1382{
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001383 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1384 if (__new_size <= capacity())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001385 {
1386 _ForwardIterator __mid = __last;
1387 bool __growing = false;
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001388 if (__new_size > size())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001389 {
1390 __growing = true;
1391 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001392 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001393 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001394 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001395 if (__growing)
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001396 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001397 else
1398 this->__destruct_at_end(__m);
1399 }
1400 else
1401 {
1402 deallocate();
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001403 allocate(__recommend(__new_size));
1404 __construct_at_end(__first, __last, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001405 }
1406}
1407
1408template <class _Tp, class _Allocator>
1409void
1410vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1411{
1412 if (__n <= capacity())
1413 {
1414 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001415 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001416 if (__n > __s)
1417 __construct_at_end(__n - __s, __u);
1418 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001419 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001420 }
1421 else
1422 {
1423 deallocate();
1424 allocate(__recommend(static_cast<size_type>(__n)));
1425 __construct_at_end(__n, __u);
1426 }
1427}
1428
Howard Hinnant324bb032010-08-22 00:02:43 +00001429template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001430inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001431typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001432vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001433{
Howard Hinnantabe26282011-09-16 17:29:17 +00001434#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001435 return iterator(this, __p);
1436#else
1437 return iterator(__p);
1438#endif
1439}
1440
Howard Hinnant324bb032010-08-22 00:02:43 +00001441template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001442inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001443typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001444vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001445{
Howard Hinnantabe26282011-09-16 17:29:17 +00001446#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001447 return const_iterator(this, __p);
1448#else
1449 return const_iterator(__p);
1450#endif
1451}
1452
Howard Hinnant324bb032010-08-22 00:02:43 +00001453template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001454inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001455typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001456vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001457{
1458 return __make_iter(this->__begin_);
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>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001464vector<_Tp, _Allocator>::begin() const _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>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001472vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001473{
1474 return __make_iter(this->__end_);
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>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001480vector<_Tp, _Allocator>::end() const _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>::reference
1488vector<_Tp, _Allocator>::operator[](size_type __n)
1489{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001490 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491 return this->__begin_[__n];
1492}
1493
Howard Hinnant324bb032010-08-22 00:02:43 +00001494template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001495inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001496typename vector<_Tp, _Allocator>::const_reference
1497vector<_Tp, _Allocator>::operator[](size_type __n) const
1498{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001499 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001500 return this->__begin_[__n];
1501}
1502
Howard Hinnant324bb032010-08-22 00:02:43 +00001503template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504typename vector<_Tp, _Allocator>::reference
1505vector<_Tp, _Allocator>::at(size_type __n)
1506{
1507 if (__n >= size())
1508 this->__throw_out_of_range();
1509 return this->__begin_[__n];
1510}
1511
Howard Hinnant324bb032010-08-22 00:02:43 +00001512template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001513typename vector<_Tp, _Allocator>::const_reference
1514vector<_Tp, _Allocator>::at(size_type __n) const
1515{
1516 if (__n >= size())
1517 this->__throw_out_of_range();
1518 return this->__begin_[__n];
1519}
1520
1521template <class _Tp, class _Allocator>
1522void
1523vector<_Tp, _Allocator>::reserve(size_type __n)
1524{
1525 if (__n > capacity())
1526 {
1527 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001528 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001529 __swap_out_circular_buffer(__v);
1530 }
1531}
1532
1533template <class _Tp, class _Allocator>
1534void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001535vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001536{
1537 if (capacity() > size())
1538 {
1539#ifndef _LIBCPP_NO_EXCEPTIONS
1540 try
1541 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001542#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001543 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001544 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001545 __swap_out_circular_buffer(__v);
1546#ifndef _LIBCPP_NO_EXCEPTIONS
1547 }
1548 catch (...)
1549 {
1550 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001551#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001552 }
1553}
1554
1555template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001556template <class _Up>
1557void
1558#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1559vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1560#else
1561vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1562#endif
1563{
1564 allocator_type& __a = this->__alloc();
1565 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1566 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnantf619e232013-01-11 20:36:59 +00001567 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1568 __v.__end_++;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001569 __swap_out_circular_buffer(__v);
1570}
1571
1572template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001573inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001574void
1575vector<_Tp, _Allocator>::push_back(const_reference __x)
1576{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001577 if (this->__end_ != this->__end_cap())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001578 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001579 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001580 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001581 _VSTD::__to_raw_pointer(this->__end_), __x);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001582 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001583 ++this->__end_;
1584 }
1585 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001586 __push_back_slow_path(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001587}
1588
Howard Hinnant73d21a42010-09-04 23:28:19 +00001589#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590
1591template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001592inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001593void
1594vector<_Tp, _Allocator>::push_back(value_type&& __x)
1595{
1596 if (this->__end_ < this->__end_cap())
1597 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001598 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001599 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001600 _VSTD::__to_raw_pointer(this->__end_),
1601 _VSTD::move(__x));
Kostya Serebryany497f9122014-09-02 23:43:38 +00001602 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001603 ++this->__end_;
1604 }
1605 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001606 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001607}
1608
Howard Hinnant73d21a42010-09-04 23:28:19 +00001609#ifndef _LIBCPP_HAS_NO_VARIADICS
1610
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611template <class _Tp, class _Allocator>
1612template <class... _Args>
1613void
Howard Hinnant0438ea22012-02-26 15:30:12 +00001614vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1615{
1616 allocator_type& __a = this->__alloc();
1617 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1618// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantf619e232013-01-11 20:36:59 +00001619 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1620 __v.__end_++;
Howard Hinnant0438ea22012-02-26 15:30:12 +00001621 __swap_out_circular_buffer(__v);
1622}
1623
1624template <class _Tp, class _Allocator>
1625template <class... _Args>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001626inline
Eric Fiselier3816ef92016-07-21 03:20:17 +00001627typename vector<_Tp, _Allocator>::reference
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001628vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1629{
1630 if (this->__end_ < this->__end_cap())
1631 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001632 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001633 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001634 _VSTD::__to_raw_pointer(this->__end_),
1635 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001636 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001637 ++this->__end_;
1638 }
1639 else
Howard Hinnant0438ea22012-02-26 15:30:12 +00001640 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Eric Fiselier3816ef92016-07-21 03:20:17 +00001641 return this->back();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001642}
1643
Howard Hinnant73d21a42010-09-04 23:28:19 +00001644#endif // _LIBCPP_HAS_NO_VARIADICS
1645#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001646
1647template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001648inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001649void
1650vector<_Tp, _Allocator>::pop_back()
1651{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001652 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001653 this->__destruct_at_end(this->__end_ - 1);
1654}
1655
1656template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001657inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001658typename vector<_Tp, _Allocator>::iterator
1659vector<_Tp, _Allocator>::erase(const_iterator __position)
1660{
Howard Hinnantabe26282011-09-16 17:29:17 +00001661#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001662 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1663 "vector::erase(iterator) called with an iterator not"
1664 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001665#endif
Howard Hinnant782da332013-03-25 22:12:26 +00001666 _LIBCPP_ASSERT(__position != end(),
1667 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001668 difference_type __ps = __position - cbegin();
1669 pointer __p = this->__begin_ + __ps;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001670 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001671 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001672 return __r;
1673}
1674
1675template <class _Tp, class _Allocator>
1676typename vector<_Tp, _Allocator>::iterator
1677vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1678{
Howard Hinnantabe26282011-09-16 17:29:17 +00001679#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001680 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1681 "vector::erase(iterator, iterator) called with an iterator not"
1682 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001683#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +00001684 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001685 pointer __p = this->__begin_ + (__first - begin());
1686 iterator __r = __make_iter(__p);
Howard Hinnantb4e67cf2013-04-18 15:02:57 +00001687 if (__first != __last)
1688 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001689 return __r;
1690}
1691
1692template <class _Tp, class _Allocator>
1693void
1694vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1695{
1696 pointer __old_last = this->__end_;
1697 difference_type __n = __old_last - __to;
1698 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1699 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001700 _VSTD::__to_raw_pointer(this->__end_),
1701 _VSTD::move(*__i));
1702 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001703}
1704
1705template <class _Tp, class _Allocator>
1706typename vector<_Tp, _Allocator>::iterator
1707vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1708{
Howard Hinnantabe26282011-09-16 17:29:17 +00001709#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001710 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1711 "vector::insert(iterator, x) called with an iterator not"
1712 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001713#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001714 pointer __p = this->__begin_ + (__position - begin());
1715 if (this->__end_ < this->__end_cap())
1716 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001717 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001718 if (__p == this->__end_)
1719 {
1720 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001721 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001722 ++this->__end_;
1723 }
1724 else
1725 {
1726 __move_range(__p, this->__end_, __p + 1);
1727 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1728 if (__p <= __xr && __xr < this->__end_)
1729 ++__xr;
1730 *__p = *__xr;
1731 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001732 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001733 }
1734 else
1735 {
1736 allocator_type& __a = this->__alloc();
1737 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1738 __v.push_back(__x);
1739 __p = __swap_out_circular_buffer(__v, __p);
1740 }
1741 return __make_iter(__p);
1742}
1743
Howard Hinnant73d21a42010-09-04 23:28:19 +00001744#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001745
1746template <class _Tp, class _Allocator>
1747typename vector<_Tp, _Allocator>::iterator
1748vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1749{
Howard Hinnantabe26282011-09-16 17:29:17 +00001750#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001751 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1752 "vector::insert(iterator, x) called with an iterator not"
1753 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001754#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001755 pointer __p = this->__begin_ + (__position - begin());
1756 if (this->__end_ < this->__end_cap())
1757 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001758 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001759 if (__p == this->__end_)
1760 {
1761 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001762 _VSTD::__to_raw_pointer(this->__end_),
1763 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001764 ++this->__end_;
1765 }
1766 else
1767 {
1768 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001769 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001770 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001771 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001772 }
1773 else
1774 {
1775 allocator_type& __a = this->__alloc();
1776 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001777 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001778 __p = __swap_out_circular_buffer(__v, __p);
1779 }
1780 return __make_iter(__p);
1781}
1782
Howard Hinnant73d21a42010-09-04 23:28:19 +00001783#ifndef _LIBCPP_HAS_NO_VARIADICS
1784
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001785template <class _Tp, class _Allocator>
1786template <class... _Args>
1787typename vector<_Tp, _Allocator>::iterator
1788vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1789{
Howard Hinnantabe26282011-09-16 17:29:17 +00001790#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001791 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1792 "vector::emplace(iterator, x) called with an iterator not"
1793 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001794#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001795 pointer __p = this->__begin_ + (__position - begin());
1796 if (this->__end_ < this->__end_cap())
1797 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001798 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001799 if (__p == this->__end_)
1800 {
1801 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001802 _VSTD::__to_raw_pointer(this->__end_),
1803 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001804 ++this->__end_;
1805 }
1806 else
1807 {
Marshall Clow51d7e8e2016-07-11 21:38:08 +00001808 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001809 __move_range(__p, this->__end_, __p + 1);
Marshall Clow51d7e8e2016-07-11 21:38:08 +00001810 *__p = _VSTD::move(__tmp.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001811 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001812 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001813 }
1814 else
1815 {
1816 allocator_type& __a = this->__alloc();
1817 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001818 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001819 __p = __swap_out_circular_buffer(__v, __p);
1820 }
1821 return __make_iter(__p);
1822}
1823
Howard Hinnant73d21a42010-09-04 23:28:19 +00001824#endif // _LIBCPP_HAS_NO_VARIADICS
1825#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001826
1827template <class _Tp, class _Allocator>
1828typename vector<_Tp, _Allocator>::iterator
1829vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1830{
Howard Hinnantabe26282011-09-16 17:29:17 +00001831#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001832 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1833 "vector::insert(iterator, n, x) called with an iterator not"
1834 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001835#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001836 pointer __p = this->__begin_ + (__position - begin());
1837 if (__n > 0)
1838 {
1839 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1840 {
1841 size_type __old_n = __n;
1842 pointer __old_last = this->__end_;
1843 if (__n > static_cast<size_type>(this->__end_ - __p))
1844 {
1845 size_type __cx = __n - (this->__end_ - __p);
1846 __construct_at_end(__cx, __x);
1847 __n -= __cx;
1848 }
1849 if (__n > 0)
1850 {
Eric Fiselierd7590952014-11-14 18:28:36 +00001851 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001852 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001853 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001854 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1855 if (__p <= __xr && __xr < this->__end_)
1856 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001857 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001858 }
1859 }
1860 else
1861 {
1862 allocator_type& __a = this->__alloc();
1863 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1864 __v.__construct_at_end(__n, __x);
1865 __p = __swap_out_circular_buffer(__v, __p);
1866 }
1867 }
1868 return __make_iter(__p);
1869}
1870
1871template <class _Tp, class _Allocator>
1872template <class _InputIterator>
1873typename enable_if
1874<
1875 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001876 !__is_forward_iterator<_InputIterator>::value &&
1877 is_constructible<
1878 _Tp,
1879 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001880 typename vector<_Tp, _Allocator>::iterator
1881>::type
1882vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1883{
Howard Hinnantabe26282011-09-16 17:29:17 +00001884#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001885 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1886 "vector::insert(iterator, range) called with an iterator not"
1887 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001888#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001889 difference_type __off = __position - begin();
1890 pointer __p = this->__begin_ + __off;
1891 allocator_type& __a = this->__alloc();
1892 pointer __old_last = this->__end_;
1893 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1894 {
Eric Fiselier7d439a42015-07-18 18:22:12 +00001895 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001896 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001897 *__first);
1898 ++this->__end_;
Eric Fiselier7d439a42015-07-18 18:22:12 +00001899 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001900 }
1901 __split_buffer<value_type, allocator_type&> __v(__a);
1902 if (__first != __last)
1903 {
1904#ifndef _LIBCPP_NO_EXCEPTIONS
1905 try
1906 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001907#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001908 __v.__construct_at_end(__first, __last);
1909 difference_type __old_size = __old_last - this->__begin_;
1910 difference_type __old_p = __p - this->__begin_;
1911 reserve(__recommend(size() + __v.size()));
1912 __p = this->__begin_ + __old_p;
1913 __old_last = this->__begin_ + __old_size;
1914#ifndef _LIBCPP_NO_EXCEPTIONS
1915 }
1916 catch (...)
1917 {
1918 erase(__make_iter(__old_last), end());
1919 throw;
1920 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001921#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001922 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001923 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant0442b122011-09-16 18:41:29 +00001924 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1925 make_move_iterator(__v.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001926 return begin() + __off;
1927}
1928
1929template <class _Tp, class _Allocator>
1930template <class _ForwardIterator>
1931typename enable_if
1932<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001933 __is_forward_iterator<_ForwardIterator>::value &&
1934 is_constructible<
1935 _Tp,
1936 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001937 typename vector<_Tp, _Allocator>::iterator
1938>::type
1939vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1940{
Howard Hinnantabe26282011-09-16 17:29:17 +00001941#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001942 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1943 "vector::insert(iterator, range) called with an iterator not"
1944 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001945#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001946 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00001947 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001948 if (__n > 0)
1949 {
1950 if (__n <= this->__end_cap() - this->__end_)
1951 {
1952 size_type __old_n = __n;
1953 pointer __old_last = this->__end_;
1954 _ForwardIterator __m = __last;
1955 difference_type __dx = this->__end_ - __p;
1956 if (__n > __dx)
1957 {
1958 __m = __first;
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001959 difference_type __diff = this->__end_ - __p;
1960 _VSTD::advance(__m, __diff);
1961 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001962 __n = __dx;
1963 }
1964 if (__n > 0)
1965 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001966 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001967 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001968 __annotator.__done();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001969 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001970 }
1971 }
1972 else
1973 {
1974 allocator_type& __a = this->__alloc();
1975 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1976 __v.__construct_at_end(__first, __last);
1977 __p = __swap_out_circular_buffer(__v, __p);
1978 }
1979 }
1980 return __make_iter(__p);
1981}
1982
1983template <class _Tp, class _Allocator>
1984void
1985vector<_Tp, _Allocator>::resize(size_type __sz)
1986{
1987 size_type __cs = size();
1988 if (__cs < __sz)
1989 this->__append(__sz - __cs);
1990 else if (__cs > __sz)
1991 this->__destruct_at_end(this->__begin_ + __sz);
1992}
1993
1994template <class _Tp, class _Allocator>
1995void
1996vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
1997{
1998 size_type __cs = size();
1999 if (__cs < __sz)
2000 this->__append(__sz - __cs, __x);
2001 else if (__cs > __sz)
2002 this->__destruct_at_end(this->__begin_ + __sz);
2003}
2004
2005template <class _Tp, class _Allocator>
2006void
2007vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow7d914d12015-07-13 20:04:56 +00002008#if _LIBCPP_STD_VER >= 14
2009 _NOEXCEPT
2010#else
2011 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2012 __is_nothrow_swappable<allocator_type>::value)
2013#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002014{
Howard Hinnant7a563db2011-09-14 18:33:51 +00002015 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2016 this->__alloc() == __x.__alloc(),
2017 "vector::swap: Either propagate_on_container_swap must be true"
2018 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +00002019 _VSTD::swap(this->__begin_, __x.__begin_);
2020 _VSTD::swap(this->__end_, __x.__end_);
2021 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Marshall Clow7d914d12015-07-13 20:04:56 +00002022 __swap_allocator(this->__alloc(), __x.__alloc(),
2023 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantabe26282011-09-16 17:29:17 +00002024#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002025 __get_db()->swap(this, &__x);
Howard Hinnantabe26282011-09-16 17:29:17 +00002026#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002027}
2028
Howard Hinnant324bb032010-08-22 00:02:43 +00002029template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002030bool
2031vector<_Tp, _Allocator>::__invariants() const
2032{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002033 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002034 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002035 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002036 return false;
2037 }
2038 else
2039 {
2040 if (this->__begin_ > this->__end_)
2041 return false;
2042 if (this->__begin_ == this->__end_cap())
2043 return false;
2044 if (this->__end_ > this->__end_cap())
2045 return false;
2046 }
2047 return true;
2048}
2049
Howard Hinnantabe26282011-09-16 17:29:17 +00002050#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002051
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002052template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:51 +00002053bool
2054vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2055{
2056 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2057}
2058
2059template <class _Tp, class _Allocator>
2060bool
2061vector<_Tp, _Allocator>::__decrementable(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>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2069{
2070 const_pointer __p = __i->base() + __n;
2071 return this->__begin_ <= __p && __p <= this->__end_;
2072}
2073
2074template <class _Tp, class _Allocator>
2075bool
2076vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2077{
2078 const_pointer __p = __i->base() + __n;
2079 return this->__begin_ <= __p && __p < this->__end_;
2080}
2081
Howard Hinnantabe26282011-09-16 17:29:17 +00002082#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002083
2084template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002085inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002086void
2087vector<_Tp, _Allocator>::__invalidate_all_iterators()
2088{
Howard Hinnantabe26282011-09-16 17:29:17 +00002089#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002090 __get_db()->__invalidate_all(this);
Howard Hinnantabe26282011-09-16 17:29:17 +00002091#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002092}
2093
2094// vector<bool>
2095
2096template <class _Allocator> class vector<bool, _Allocator>;
2097
2098template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2099
2100template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002101struct __has_storage_type<vector<bool, _Allocator> >
2102{
2103 static const bool value = true;
2104};
2105
2106template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002107class _LIBCPP_TYPE_VIS_ONLY vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002108 : private __vector_base_common<true>
2109{
2110public:
2111 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00002112 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002113 typedef _Allocator allocator_type;
2114 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002115 typedef typename __alloc_traits::size_type size_type;
2116 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantf867f632012-05-07 16:50:38 +00002117 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002118 typedef __bit_iterator<vector, false> pointer;
2119 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002120 typedef pointer iterator;
2121 typedef const_pointer const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002122 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2123 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002124
2125private:
Marshall Clow66302c62015-04-07 05:21:38 +00002126 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002127 typedef allocator_traits<__storage_allocator> __storage_traits;
2128 typedef typename __storage_traits::pointer __storage_pointer;
2129 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2130
2131 __storage_pointer __begin_;
2132 size_type __size_;
2133 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002134public:
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002135 typedef __bit_reference<vector> reference;
2136 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002137private:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002138 _LIBCPP_INLINE_VISIBILITY
2139 size_type& __cap() _NOEXCEPT
2140 {return __cap_alloc_.first();}
2141 _LIBCPP_INLINE_VISIBILITY
2142 const size_type& __cap() const _NOEXCEPT
2143 {return __cap_alloc_.first();}
2144 _LIBCPP_INLINE_VISIBILITY
2145 __storage_allocator& __alloc() _NOEXCEPT
2146 {return __cap_alloc_.second();}
2147 _LIBCPP_INLINE_VISIBILITY
2148 const __storage_allocator& __alloc() const _NOEXCEPT
2149 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002150
2151 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2152
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002153 _LIBCPP_INLINE_VISIBILITY
2154 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002155 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002156 _LIBCPP_INLINE_VISIBILITY
2157 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002158 {return (__n - 1) / __bits_per_word + 1;}
2159
2160public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002161 _LIBCPP_INLINE_VISIBILITY
Marshall Clowc912c0c2015-06-04 02:05:41 +00002162 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow127db912015-06-04 00:10:20 +00002163
2164 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2165#if _LIBCPP_STD_VER <= 14
2166 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2167#else
2168 _NOEXCEPT;
2169#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002170 ~vector();
2171 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59 +00002172#if _LIBCPP_STD_VER > 11
2173 explicit vector(size_type __n, const allocator_type& __a);
2174#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002175 vector(size_type __n, const value_type& __v);
2176 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2177 template <class _InputIterator>
2178 vector(_InputIterator __first, _InputIterator __last,
2179 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2180 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2181 template <class _InputIterator>
2182 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2183 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2184 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2185 template <class _ForwardIterator>
2186 vector(_ForwardIterator __first, _ForwardIterator __last,
2187 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2188 template <class _ForwardIterator>
2189 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2190 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2191
2192 vector(const vector& __v);
2193 vector(const vector& __v, const allocator_type& __a);
2194 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00002195#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002196 vector(initializer_list<value_type> __il);
2197 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00002198#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002199
Howard Hinnant73d21a42010-09-04 23:28:19 +00002200#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002201 _LIBCPP_INLINE_VISIBILITY
2202 vector(vector&& __v)
Marshall Clow119ed472015-07-14 14:46:32 +00002203#if _LIBCPP_STD_VER > 14
2204 _NOEXCEPT;
2205#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002206 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow119ed472015-07-14 14:46:32 +00002207#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002208 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002209 _LIBCPP_INLINE_VISIBILITY
2210 vector& operator=(vector&& __v)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002211 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnant73d21a42010-09-04 23:28:19 +00002212#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +00002213#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002215 vector& operator=(initializer_list<value_type> __il)
2216 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00002217#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002218
2219 template <class _InputIterator>
2220 typename enable_if
2221 <
2222 __is_input_iterator<_InputIterator>::value &&
2223 !__is_forward_iterator<_InputIterator>::value,
2224 void
2225 >::type
2226 assign(_InputIterator __first, _InputIterator __last);
2227 template <class _ForwardIterator>
2228 typename enable_if
2229 <
2230 __is_forward_iterator<_ForwardIterator>::value,
2231 void
2232 >::type
2233 assign(_ForwardIterator __first, _ForwardIterator __last);
2234
2235 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +00002236#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002238 void assign(initializer_list<value_type> __il)
2239 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002240#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002241
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002242 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002243 {return allocator_type(this->__alloc());}
2244
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002245 size_type max_size() const _NOEXCEPT;
2246 _LIBCPP_INLINE_VISIBILITY
2247 size_type capacity() const _NOEXCEPT
2248 {return __internal_cap_to_external(__cap());}
2249 _LIBCPP_INLINE_VISIBILITY
2250 size_type size() const _NOEXCEPT
2251 {return __size_;}
2252 _LIBCPP_INLINE_VISIBILITY
2253 bool empty() const _NOEXCEPT
2254 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002255 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002256 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002257
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002258 _LIBCPP_INLINE_VISIBILITY
2259 iterator begin() _NOEXCEPT
2260 {return __make_iter(0);}
2261 _LIBCPP_INLINE_VISIBILITY
2262 const_iterator begin() const _NOEXCEPT
2263 {return __make_iter(0);}
2264 _LIBCPP_INLINE_VISIBILITY
2265 iterator end() _NOEXCEPT
2266 {return __make_iter(__size_);}
2267 _LIBCPP_INLINE_VISIBILITY
2268 const_iterator end() const _NOEXCEPT
2269 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002270
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002271 _LIBCPP_INLINE_VISIBILITY
2272 reverse_iterator rbegin() _NOEXCEPT
2273 {return reverse_iterator(end());}
2274 _LIBCPP_INLINE_VISIBILITY
2275 const_reverse_iterator rbegin() const _NOEXCEPT
2276 {return const_reverse_iterator(end());}
2277 _LIBCPP_INLINE_VISIBILITY
2278 reverse_iterator rend() _NOEXCEPT
2279 {return reverse_iterator(begin());}
2280 _LIBCPP_INLINE_VISIBILITY
2281 const_reverse_iterator rend() const _NOEXCEPT
2282 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002283
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002284 _LIBCPP_INLINE_VISIBILITY
2285 const_iterator cbegin() const _NOEXCEPT
2286 {return __make_iter(0);}
2287 _LIBCPP_INLINE_VISIBILITY
2288 const_iterator cend() const _NOEXCEPT
2289 {return __make_iter(__size_);}
2290 _LIBCPP_INLINE_VISIBILITY
2291 const_reverse_iterator crbegin() const _NOEXCEPT
2292 {return rbegin();}
2293 _LIBCPP_INLINE_VISIBILITY
2294 const_reverse_iterator crend() const _NOEXCEPT
2295 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002296
2297 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2298 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2299 reference at(size_type __n);
2300 const_reference at(size_type __n) const;
2301
2302 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2303 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2304 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2305 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2306
2307 void push_back(const value_type& __x);
Marshall Clow198a2a52013-08-13 23:54:12 +00002308#if _LIBCPP_STD_VER > 11
2309 template <class... _Args>
Eric Fiselier3816ef92016-07-21 03:20:17 +00002310 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args) {
2311 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
2312 return this->back();
2313 }
Marshall Clow198a2a52013-08-13 23:54:12 +00002314#endif
2315
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002316 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2317
Marshall Clow198a2a52013-08-13 23:54:12 +00002318#if _LIBCPP_STD_VER > 11
2319 template <class... _Args>
2320 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2321 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2322#endif
2323
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002324 iterator insert(const_iterator __position, const value_type& __x);
2325 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2326 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2327 template <class _InputIterator>
2328 typename enable_if
2329 <
2330 __is_input_iterator <_InputIterator>::value &&
2331 !__is_forward_iterator<_InputIterator>::value,
2332 iterator
2333 >::type
2334 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2335 template <class _ForwardIterator>
2336 typename enable_if
2337 <
2338 __is_forward_iterator<_ForwardIterator>::value,
2339 iterator
2340 >::type
2341 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00002342#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002344 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2345 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002346#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002347
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002348 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002349 iterator erase(const_iterator __first, const_iterator __last);
2350
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002351 _LIBCPP_INLINE_VISIBILITY
2352 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002353
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002354 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +00002355#if _LIBCPP_STD_VER >= 14
2356 _NOEXCEPT;
2357#else
2358 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2359 __is_nothrow_swappable<allocator_type>::value);
2360#endif
Marshall Clow005c60b2016-04-07 14:20:31 +00002361 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002362
2363 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002364 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002365
2366 bool __invariants() const;
2367
2368private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002369 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002370 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002371 void deallocate() _NOEXCEPT;
2372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00002373 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow0d1965d2014-07-28 15:02:42 +00002374 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002375 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2376 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002377 template <class _ForwardIterator>
2378 typename enable_if
2379 <
2380 __is_forward_iterator<_ForwardIterator>::value,
2381 void
2382 >::type
2383 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2384 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002385 _LIBCPP_INLINE_VISIBILITY
2386 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002387 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002388 _LIBCPP_INLINE_VISIBILITY
2389 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002390 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002391 _LIBCPP_INLINE_VISIBILITY
2392 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002393 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002394 _LIBCPP_INLINE_VISIBILITY
2395 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002396 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002397 _LIBCPP_INLINE_VISIBILITY
2398 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002399 {return begin() + (__p - cbegin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002400
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002402 void __copy_assign_alloc(const vector& __v)
2403 {__copy_assign_alloc(__v, integral_constant<bool,
2404 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002406 void __copy_assign_alloc(const vector& __c, true_type)
2407 {
2408 if (__alloc() != __c.__alloc())
2409 deallocate();
2410 __alloc() = __c.__alloc();
2411 }
2412
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002414 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002415 {}
2416
2417 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002418 void __move_assign(vector& __c, true_type)
2419 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002421 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002422 _NOEXCEPT_(
2423 !__storage_traits::propagate_on_container_move_assignment::value ||
2424 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002425 {__move_assign_alloc(__c, integral_constant<bool,
2426 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00002428 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002429 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002430 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002431 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002432 }
2433
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002435 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002436 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002437 {}
2438
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002439 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002440
2441 friend class __bit_reference<vector>;
2442 friend class __bit_const_reference<vector>;
2443 friend class __bit_iterator<vector, false>;
2444 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:18 +00002445 friend struct __bit_array<vector>;
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002446 friend struct _LIBCPP_TYPE_VIS_ONLY hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002447};
2448
2449template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002450inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002451void
2452vector<bool, _Allocator>::__invalidate_all_iterators()
2453{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002454}
2455
2456// Allocate space for __n objects
2457// throws length_error if __n > max_size()
2458// throws (probably bad_alloc) if memory run out
2459// Precondition: __begin_ == __end_ == __cap() == 0
2460// Precondition: __n > 0
2461// Postcondition: capacity() == __n
2462// Postcondition: size() == 0
2463template <class _Allocator>
2464void
2465vector<bool, _Allocator>::allocate(size_type __n)
2466{
2467 if (__n > max_size())
2468 this->__throw_length_error();
2469 __n = __external_cap_to_internal(__n);
2470 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2471 this->__size_ = 0;
2472 this->__cap() = __n;
2473}
2474
2475template <class _Allocator>
2476void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002477vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002478{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002479 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002480 {
2481 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2482 __invalidate_all_iterators();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002483 this->__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002484 this->__size_ = this->__cap() = 0;
2485 }
2486}
2487
2488template <class _Allocator>
2489typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002490vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002491{
2492 size_type __amax = __storage_traits::max_size(__alloc());
2493 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2494 if (__nmax / __bits_per_word <= __amax)
2495 return __nmax;
2496 return __internal_cap_to_external(__amax);
2497}
2498
2499// Precondition: __new_size > capacity()
2500template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002501inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002502typename vector<bool, _Allocator>::size_type
2503vector<bool, _Allocator>::__recommend(size_type __new_size) const
2504{
2505 const size_type __ms = max_size();
2506 if (__new_size > __ms)
2507 this->__throw_length_error();
2508 const size_type __cap = capacity();
2509 if (__cap >= __ms / 2)
2510 return __ms;
Howard Hinnant7f764502013-08-14 18:00:20 +00002511 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002512}
2513
2514// Default constructs __n objects starting at __end_
2515// Precondition: __n > 0
2516// Precondition: size() + __n <= capacity()
2517// Postcondition: size() == size() + __n
2518template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002519inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002520void
2521vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2522{
2523 size_type __old_size = this->__size_;
2524 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002525 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002526}
2527
2528template <class _Allocator>
2529template <class _ForwardIterator>
2530typename enable_if
2531<
2532 __is_forward_iterator<_ForwardIterator>::value,
2533 void
2534>::type
2535vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2536{
2537 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002538 this->__size_ += _VSTD::distance(__first, __last);
2539 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002540}
2541
2542template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002543inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002544vector<bool, _Allocator>::vector()
Marshall Clowc912c0c2015-06-04 02:05:41 +00002545 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002546 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002547 __size_(0),
2548 __cap_alloc_(0)
2549{
2550}
2551
2552template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002553inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002554vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow127db912015-06-04 00:10:20 +00002555#if _LIBCPP_STD_VER <= 14
2556 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2557#else
2558 _NOEXCEPT
2559#endif
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002560 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002561 __size_(0),
2562 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2563{
2564}
2565
2566template <class _Allocator>
2567vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002568 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002569 __size_(0),
2570 __cap_alloc_(0)
2571{
2572 if (__n > 0)
2573 {
2574 allocate(__n);
2575 __construct_at_end(__n, false);
2576 }
2577}
2578
Marshall Clowa49a2c92013-09-14 00:47:59 +00002579#if _LIBCPP_STD_VER > 11
2580template <class _Allocator>
2581vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2582 : __begin_(nullptr),
2583 __size_(0),
2584 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2585{
2586 if (__n > 0)
2587 {
2588 allocate(__n);
2589 __construct_at_end(__n, false);
2590 }
2591}
2592#endif
2593
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002594template <class _Allocator>
2595vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002596 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002597 __size_(0),
2598 __cap_alloc_(0)
2599{
2600 if (__n > 0)
2601 {
2602 allocate(__n);
2603 __construct_at_end(__n, __x);
2604 }
2605}
2606
2607template <class _Allocator>
2608vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002609 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002610 __size_(0),
2611 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2612{
2613 if (__n > 0)
2614 {
2615 allocate(__n);
2616 __construct_at_end(__n, __x);
2617 }
2618}
2619
2620template <class _Allocator>
2621template <class _InputIterator>
2622vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2623 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2624 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002625 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002626 __size_(0),
2627 __cap_alloc_(0)
2628{
2629#ifndef _LIBCPP_NO_EXCEPTIONS
2630 try
2631 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002632#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002633 for (; __first != __last; ++__first)
2634 push_back(*__first);
2635#ifndef _LIBCPP_NO_EXCEPTIONS
2636 }
2637 catch (...)
2638 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002639 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002640 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2641 __invalidate_all_iterators();
2642 throw;
2643 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002644#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002645}
2646
2647template <class _Allocator>
2648template <class _InputIterator>
2649vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2650 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2651 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002652 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002653 __size_(0),
2654 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2655{
2656#ifndef _LIBCPP_NO_EXCEPTIONS
2657 try
2658 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002659#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002660 for (; __first != __last; ++__first)
2661 push_back(*__first);
2662#ifndef _LIBCPP_NO_EXCEPTIONS
2663 }
2664 catch (...)
2665 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002666 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002667 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2668 __invalidate_all_iterators();
2669 throw;
2670 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002671#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002672}
2673
2674template <class _Allocator>
2675template <class _ForwardIterator>
2676vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2677 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002678 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002679 __size_(0),
2680 __cap_alloc_(0)
2681{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002682 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002683 if (__n > 0)
2684 {
2685 allocate(__n);
2686 __construct_at_end(__first, __last);
2687 }
2688}
2689
2690template <class _Allocator>
2691template <class _ForwardIterator>
2692vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2693 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002694 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002695 __size_(0),
2696 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2697{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002698 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002699 if (__n > 0)
2700 {
2701 allocate(__n);
2702 __construct_at_end(__first, __last);
2703 }
2704}
2705
Howard Hinnante3e32912011-08-12 21:56:02 +00002706#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2707
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002708template <class _Allocator>
2709vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002710 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002711 __size_(0),
2712 __cap_alloc_(0)
2713{
2714 size_type __n = static_cast<size_type>(__il.size());
2715 if (__n > 0)
2716 {
2717 allocate(__n);
2718 __construct_at_end(__il.begin(), __il.end());
2719 }
2720}
2721
2722template <class _Allocator>
2723vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002724 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002725 __size_(0),
2726 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2727{
2728 size_type __n = static_cast<size_type>(__il.size());
2729 if (__n > 0)
2730 {
2731 allocate(__n);
2732 __construct_at_end(__il.begin(), __il.end());
2733 }
2734}
2735
Howard Hinnante3e32912011-08-12 21:56:02 +00002736#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2737
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002739vector<bool, _Allocator>::~vector()
2740{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002741 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002742 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002743 __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002744}
2745
2746template <class _Allocator>
2747vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002748 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002749 __size_(0),
2750 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2751{
2752 if (__v.size() > 0)
2753 {
2754 allocate(__v.size());
2755 __construct_at_end(__v.begin(), __v.end());
2756 }
2757}
2758
2759template <class _Allocator>
2760vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002761 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002762 __size_(0),
2763 __cap_alloc_(0, __a)
2764{
2765 if (__v.size() > 0)
2766 {
2767 allocate(__v.size());
2768 __construct_at_end(__v.begin(), __v.end());
2769 }
2770}
2771
2772template <class _Allocator>
2773vector<bool, _Allocator>&
2774vector<bool, _Allocator>::operator=(const vector& __v)
2775{
2776 if (this != &__v)
2777 {
2778 __copy_assign_alloc(__v);
2779 if (__v.__size_)
2780 {
2781 if (__v.__size_ > capacity())
2782 {
2783 deallocate();
2784 allocate(__v.__size_);
2785 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002786 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002787 }
2788 __size_ = __v.__size_;
2789 }
2790 return *this;
2791}
2792
Howard Hinnant73d21a42010-09-04 23:28:19 +00002793#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2794
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002795template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002796inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002797vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clow119ed472015-07-14 14:46:32 +00002798#if _LIBCPP_STD_VER > 14
2799 _NOEXCEPT
2800#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002801 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow119ed472015-07-14 14:46:32 +00002802#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002803 : __begin_(__v.__begin_),
2804 __size_(__v.__size_),
2805 __cap_alloc_(__v.__cap_alloc_)
2806{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002807 __v.__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002808 __v.__size_ = 0;
2809 __v.__cap() = 0;
2810}
2811
2812template <class _Allocator>
2813vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002814 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002815 __size_(0),
2816 __cap_alloc_(0, __a)
2817{
2818 if (__a == allocator_type(__v.__alloc()))
2819 {
2820 this->__begin_ = __v.__begin_;
2821 this->__size_ = __v.__size_;
2822 this->__cap() = __v.__cap();
2823 __v.__begin_ = nullptr;
2824 __v.__cap() = __v.__size_ = 0;
2825 }
2826 else if (__v.size() > 0)
2827 {
2828 allocate(__v.size());
2829 __construct_at_end(__v.begin(), __v.end());
2830 }
2831}
2832
2833template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002834inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002835vector<bool, _Allocator>&
2836vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002837 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002838{
2839 __move_assign(__v, integral_constant<bool,
2840 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002841 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002842}
2843
2844template <class _Allocator>
2845void
2846vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2847{
2848 if (__alloc() != __c.__alloc())
2849 assign(__c.begin(), __c.end());
2850 else
2851 __move_assign(__c, true_type());
2852}
2853
2854template <class _Allocator>
2855void
2856vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002857 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002858{
2859 deallocate();
Marshall Clowdb5e54d2014-07-21 15:15:15 +00002860 __move_assign_alloc(__c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002861 this->__begin_ = __c.__begin_;
2862 this->__size_ = __c.__size_;
2863 this->__cap() = __c.__cap();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002864 __c.__begin_ = nullptr;
2865 __c.__cap() = __c.__size_ = 0;
2866}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002867
2868#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002869
2870template <class _Allocator>
2871void
2872vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2873{
2874 __size_ = 0;
2875 if (__n > 0)
2876 {
2877 size_type __c = capacity();
2878 if (__n <= __c)
2879 __size_ = __n;
2880 else
2881 {
2882 vector __v(__alloc());
2883 __v.reserve(__recommend(__n));
2884 __v.__size_ = __n;
2885 swap(__v);
2886 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002887 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002888 }
2889}
2890
2891template <class _Allocator>
2892template <class _InputIterator>
2893typename enable_if
2894<
2895 __is_input_iterator<_InputIterator>::value &&
2896 !__is_forward_iterator<_InputIterator>::value,
2897 void
2898>::type
2899vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2900{
2901 clear();
2902 for (; __first != __last; ++__first)
2903 push_back(*__first);
2904}
2905
2906template <class _Allocator>
2907template <class _ForwardIterator>
2908typename enable_if
2909<
2910 __is_forward_iterator<_ForwardIterator>::value,
2911 void
2912>::type
2913vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2914{
2915 clear();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002916 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002917 if (__n)
2918 {
2919 if (__n > capacity())
2920 {
2921 deallocate();
2922 allocate(__n);
2923 }
2924 __construct_at_end(__first, __last);
2925 }
2926}
2927
2928template <class _Allocator>
2929void
2930vector<bool, _Allocator>::reserve(size_type __n)
2931{
2932 if (__n > capacity())
2933 {
2934 vector __v(this->__alloc());
2935 __v.allocate(__n);
2936 __v.__construct_at_end(this->begin(), this->end());
2937 swap(__v);
2938 __invalidate_all_iterators();
2939 }
2940}
2941
2942template <class _Allocator>
2943void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002944vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002945{
2946 if (__external_cap_to_internal(size()) > __cap())
2947 {
2948#ifndef _LIBCPP_NO_EXCEPTIONS
2949 try
2950 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002951#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002952 vector(*this, allocator_type(__alloc())).swap(*this);
2953#ifndef _LIBCPP_NO_EXCEPTIONS
2954 }
2955 catch (...)
2956 {
2957 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002958#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002959 }
2960}
2961
2962template <class _Allocator>
2963typename vector<bool, _Allocator>::reference
2964vector<bool, _Allocator>::at(size_type __n)
2965{
2966 if (__n >= size())
2967 this->__throw_out_of_range();
2968 return (*this)[__n];
2969}
2970
2971template <class _Allocator>
2972typename vector<bool, _Allocator>::const_reference
2973vector<bool, _Allocator>::at(size_type __n) const
2974{
2975 if (__n >= size())
2976 this->__throw_out_of_range();
2977 return (*this)[__n];
2978}
2979
2980template <class _Allocator>
2981void
2982vector<bool, _Allocator>::push_back(const value_type& __x)
2983{
2984 if (this->__size_ == this->capacity())
2985 reserve(__recommend(this->__size_ + 1));
2986 ++this->__size_;
2987 back() = __x;
2988}
2989
2990template <class _Allocator>
2991typename vector<bool, _Allocator>::iterator
2992vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2993{
2994 iterator __r;
2995 if (size() < capacity())
2996 {
2997 const_iterator __old_end = end();
2998 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002999 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003000 __r = __const_iterator_cast(__position);
3001 }
3002 else
3003 {
3004 vector __v(__alloc());
3005 __v.reserve(__recommend(__size_ + 1));
3006 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003007 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3008 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003009 swap(__v);
3010 }
3011 *__r = __x;
3012 return __r;
3013}
3014
3015template <class _Allocator>
3016typename vector<bool, _Allocator>::iterator
3017vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3018{
3019 iterator __r;
3020 size_type __c = capacity();
3021 if (__n <= __c && size() <= __c - __n)
3022 {
3023 const_iterator __old_end = end();
3024 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003025 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003026 __r = __const_iterator_cast(__position);
3027 }
3028 else
3029 {
3030 vector __v(__alloc());
3031 __v.reserve(__recommend(__size_ + __n));
3032 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003033 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3034 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003035 swap(__v);
3036 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003037 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003038 return __r;
3039}
3040
3041template <class _Allocator>
3042template <class _InputIterator>
3043typename enable_if
3044<
3045 __is_input_iterator <_InputIterator>::value &&
3046 !__is_forward_iterator<_InputIterator>::value,
3047 typename vector<bool, _Allocator>::iterator
3048>::type
3049vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3050{
3051 difference_type __off = __position - begin();
3052 iterator __p = __const_iterator_cast(__position);
3053 iterator __old_end = end();
3054 for (; size() != capacity() && __first != __last; ++__first)
3055 {
3056 ++this->__size_;
3057 back() = *__first;
3058 }
3059 vector __v(__alloc());
3060 if (__first != __last)
3061 {
3062#ifndef _LIBCPP_NO_EXCEPTIONS
3063 try
3064 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003065#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003066 __v.assign(__first, __last);
3067 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3068 difference_type __old_p = __p - begin();
3069 reserve(__recommend(size() + __v.size()));
3070 __p = begin() + __old_p;
3071 __old_end = begin() + __old_size;
3072#ifndef _LIBCPP_NO_EXCEPTIONS
3073 }
3074 catch (...)
3075 {
3076 erase(__old_end, end());
3077 throw;
3078 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003079#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003080 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003081 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003082 insert(__p, __v.begin(), __v.end());
3083 return begin() + __off;
3084}
3085
3086template <class _Allocator>
3087template <class _ForwardIterator>
3088typename enable_if
3089<
3090 __is_forward_iterator<_ForwardIterator>::value,
3091 typename vector<bool, _Allocator>::iterator
3092>::type
3093vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3094{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003095 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003096 iterator __r;
3097 size_type __c = capacity();
3098 if (__n <= __c && size() <= __c - __n)
3099 {
3100 const_iterator __old_end = end();
3101 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003102 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003103 __r = __const_iterator_cast(__position);
3104 }
3105 else
3106 {
3107 vector __v(__alloc());
3108 __v.reserve(__recommend(__size_ + __n));
3109 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003110 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3111 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003112 swap(__v);
3113 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003114 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003115 return __r;
3116}
3117
3118template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003119inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003120typename vector<bool, _Allocator>::iterator
3121vector<bool, _Allocator>::erase(const_iterator __position)
3122{
3123 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003124 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003125 --__size_;
3126 return __r;
3127}
3128
3129template <class _Allocator>
3130typename vector<bool, _Allocator>::iterator
3131vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3132{
3133 iterator __r = __const_iterator_cast(__first);
3134 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003135 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003136 __size_ -= __d;
3137 return __r;
3138}
3139
3140template <class _Allocator>
3141void
3142vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow7d914d12015-07-13 20:04:56 +00003143#if _LIBCPP_STD_VER >= 14
3144 _NOEXCEPT
3145#else
3146 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3147 __is_nothrow_swappable<allocator_type>::value)
3148#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003149{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003150 _VSTD::swap(this->__begin_, __x.__begin_);
3151 _VSTD::swap(this->__size_, __x.__size_);
3152 _VSTD::swap(this->__cap(), __x.__cap());
Marshall Clow7d914d12015-07-13 20:04:56 +00003153 __swap_allocator(this->__alloc(), __x.__alloc(),
3154 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003155}
3156
Howard Hinnant324bb032010-08-22 00:02:43 +00003157template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003158void
3159vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3160{
3161 size_type __cs = size();
3162 if (__cs < __sz)
3163 {
3164 iterator __r;
3165 size_type __c = capacity();
3166 size_type __n = __sz - __cs;
3167 if (__n <= __c && __cs <= __c - __n)
3168 {
3169 __r = end();
3170 __size_ += __n;
3171 }
3172 else
3173 {
3174 vector __v(__alloc());
3175 __v.reserve(__recommend(__size_ + __n));
3176 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003177 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003178 swap(__v);
3179 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003180 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003181 }
3182 else
3183 __size_ = __sz;
3184}
3185
Howard Hinnant324bb032010-08-22 00:02:43 +00003186template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003187void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003188vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003189{
3190 // do middle whole words
3191 size_type __n = __size_;
3192 __storage_pointer __p = __begin_;
3193 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3194 *__p = ~*__p;
3195 // do last partial word
3196 if (__n > 0)
3197 {
3198 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3199 __storage_type __b = *__p & __m;
3200 *__p &= ~__m;
3201 *__p |= ~__b & __m;
3202 }
3203}
3204
Howard Hinnant324bb032010-08-22 00:02:43 +00003205template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003206bool
3207vector<bool, _Allocator>::__invariants() const
3208{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00003209 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003210 {
3211 if (this->__size_ != 0 || this->__cap() != 0)
3212 return false;
3213 }
3214 else
3215 {
3216 if (this->__cap() == 0)
3217 return false;
3218 if (this->__size_ > this->capacity())
3219 return false;
3220 }
3221 return true;
3222}
3223
Howard Hinnant324bb032010-08-22 00:02:43 +00003224template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003225size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003226vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003227{
3228 size_t __h = 0;
3229 // do middle whole words
3230 size_type __n = __size_;
3231 __storage_pointer __p = __begin_;
3232 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3233 __h ^= *__p;
3234 // do last partial word
3235 if (__n > 0)
3236 {
3237 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3238 __h ^= *__p & __m;
3239 }
3240 return __h;
3241}
3242
3243template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003244struct _LIBCPP_TYPE_VIS_ONLY hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003245 : public unary_function<vector<bool, _Allocator>, size_t>
3246{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00003247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003248 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003249 {return __vec.__hash_code();}
3250};
3251
3252template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003253inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003254bool
3255operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3256{
3257 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003258 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003259}
3260
3261template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003262inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003263bool
3264operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3265{
3266 return !(__x == __y);
3267}
3268
3269template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003270inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003271bool
3272operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3273{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003274 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003275}
3276
3277template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003278inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003279bool
3280operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3281{
3282 return __y < __x;
3283}
3284
3285template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003286inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003287bool
3288operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3289{
3290 return !(__x < __y);
3291}
3292
3293template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003294inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003295bool
3296operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3297{
3298 return !(__y < __x);
3299}
3300
3301template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003302inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003303void
3304swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003305 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003306{
3307 __x.swap(__y);
3308}
3309
3310_LIBCPP_END_NAMESPACE_STD
3311
3312#endif // _LIBCPP_VECTOR