blob: 049d3c8702fae02c3ced66517b4187754cad8272 [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(
Dan Albert1d4a1ed2016-05-25 22:36:09 -070054 allocator_type::propagate_on_container_move_assignment::value &&
55 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056 vector& operator=(initializer_list<value_type> il);
57 template <class InputIterator>
58 void assign(InputIterator first, InputIterator last);
59 void assign(size_type n, const value_type& u);
60 void assign(initializer_list<value_type> il);
61
Howard Hinnantd1d27a42011-06-03 19:40:40 +000062 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063
Howard Hinnantd1d27a42011-06-03 19:40:40 +000064 iterator begin() noexcept;
65 const_iterator begin() const noexcept;
66 iterator end() noexcept;
67 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000068
Howard Hinnantd1d27a42011-06-03 19:40:40 +000069 reverse_iterator rbegin() noexcept;
70 const_reverse_iterator rbegin() const noexcept;
71 reverse_iterator rend() noexcept;
72 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000073
Howard Hinnantd1d27a42011-06-03 19:40:40 +000074 const_iterator cbegin() const noexcept;
75 const_iterator cend() const noexcept;
76 const_reverse_iterator crbegin() const noexcept;
77 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000078
Howard Hinnantd1d27a42011-06-03 19:40:40 +000079 size_type size() const noexcept;
80 size_type max_size() const noexcept;
81 size_type capacity() const noexcept;
82 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +000084 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000085
86 reference operator[](size_type n);
87 const_reference operator[](size_type n) const;
88 reference at(size_type n);
89 const_reference at(size_type n) const;
90
91 reference front();
92 const_reference front() const;
93 reference back();
94 const_reference back() const;
95
Howard Hinnantd1d27a42011-06-03 19:40:40 +000096 value_type* data() noexcept;
97 const value_type* data() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098
99 void push_back(const value_type& x);
100 void push_back(value_type&& x);
101 template <class... Args>
102 void emplace_back(Args&&... args);
103 void pop_back();
104
105 template <class... Args> iterator emplace(const_iterator position, Args&&... args);
106 iterator insert(const_iterator position, const value_type& x);
107 iterator insert(const_iterator position, value_type&& x);
108 iterator insert(const_iterator position, size_type n, const value_type& x);
109 template <class InputIterator>
110 iterator insert(const_iterator position, InputIterator first, InputIterator last);
111 iterator insert(const_iterator position, initializer_list<value_type> il);
112
113 iterator erase(const_iterator position);
114 iterator erase(const_iterator first, const_iterator last);
115
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000116 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000117
118 void resize(size_type sz);
119 void resize(size_type sz, const value_type& c);
120
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000121 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +0000122 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
123 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000124
125 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000126};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000127
Howard Hinnant324bb032010-08-22 00:02:43 +0000128template <class Allocator = allocator<T> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129class vector<bool, Allocator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000130{
131public:
132 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000133 typedef Allocator allocator_type;
134 typedef implementation-defined iterator;
135 typedef implementation-defined const_iterator;
136 typedef typename allocator_type::size_type size_type;
137 typedef typename allocator_type::difference_type difference_type;
138 typedef iterator pointer;
139 typedef const_iterator const_pointer;
140 typedef std::reverse_iterator<iterator> reverse_iterator;
141 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
142
143 class reference
144 {
145 public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000146 reference(const reference&) noexcept;
147 operator bool() const noexcept;
148 reference& operator=(const bool x) noexcept;
149 reference& operator=(const reference& x) noexcept;
150 iterator operator&() const noexcept;
151 void flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152 };
153
154 class const_reference
155 {
156 public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000157 const_reference(const reference&) noexcept;
158 operator bool() const noexcept;
159 const_iterator operator&() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160 };
161
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000162 vector()
163 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant9cbee432011-09-02 20:42:31 +0000164 explicit vector(const allocator_type&);
Marshall Clowa49a2c92013-09-14 00:47:59 +0000165 explicit vector(size_type n, const allocator_type& a = allocator_type()); // C++14
166 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000167 template <class InputIterator>
168 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
169 vector(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000170 vector(vector&& x)
171 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000172 vector(initializer_list<value_type> il);
173 vector(initializer_list<value_type> il, const allocator_type& a);
174 ~vector();
175 vector& operator=(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000176 vector& operator=(vector&& x)
177 noexcept(
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700178 allocator_type::propagate_on_container_move_assignment::value &&
179 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000180 vector& operator=(initializer_list<value_type> il);
181 template <class InputIterator>
182 void assign(InputIterator first, InputIterator last);
183 void assign(size_type n, const value_type& u);
184 void assign(initializer_list<value_type> il);
185
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000186 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000188 iterator begin() noexcept;
189 const_iterator begin() const noexcept;
190 iterator end() noexcept;
191 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000192
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000193 reverse_iterator rbegin() noexcept;
194 const_reverse_iterator rbegin() const noexcept;
195 reverse_iterator rend() noexcept;
196 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000197
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000198 const_iterator cbegin() const noexcept;
199 const_iterator cend() const noexcept;
200 const_reverse_iterator crbegin() const noexcept;
201 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000203 size_type size() const noexcept;
204 size_type max_size() const noexcept;
205 size_type capacity() const noexcept;
206 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000207 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000208 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000209
210 reference operator[](size_type n);
211 const_reference operator[](size_type n) const;
212 reference at(size_type n);
213 const_reference at(size_type n) const;
214
215 reference front();
216 const_reference front() const;
217 reference back();
218 const_reference back() const;
219
220 void push_back(const value_type& x);
Marshall Clow198a2a52013-08-13 23:54:12 +0000221 template <class... Args> void emplace_back(Args&&... args); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000222 void pop_back();
223
Marshall Clow198a2a52013-08-13 23:54:12 +0000224 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000225 iterator insert(const_iterator position, const value_type& x);
226 iterator insert(const_iterator position, size_type n, const value_type& x);
227 template <class InputIterator>
228 iterator insert(const_iterator position, InputIterator first, InputIterator last);
229 iterator insert(const_iterator position, initializer_list<value_type> il);
230
231 iterator erase(const_iterator position);
232 iterator erase(const_iterator first, const_iterator last);
233
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000234 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235
236 void resize(size_type sz);
237 void resize(size_type sz, value_type x);
238
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000239 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +0000240 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
241 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000242 void flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243
244 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000245};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000246
247template <class Allocator> struct hash<std::vector<bool, Allocator>>;
248
249template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
250template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
251template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
252template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
253template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
254template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
255
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000256template <class T, class Allocator>
257void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
258 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000259
260} // std
261
262*/
263
264#include <__config>
265#include <__bit_reference>
266#include <type_traits>
267#include <climits>
268#include <limits>
269#include <initializer_list>
270#include <memory>
271#include <stdexcept>
272#include <algorithm>
273#include <cstring>
274#include <__split_buffer>
275#include <__functional_base>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000276
Howard Hinnant66c6f972011-11-29 16:45:27 +0000277#include <__undef_min_max>
278
Eric Fiselierb9536102014-08-10 23:53:08 +0000279#include <__debug>
Howard Hinnant8b00e6c2013-08-02 00:26:35 +0000280
Howard Hinnant08e17472011-10-17 20:05:10 +0000281#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000282#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000283#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000284
285_LIBCPP_BEGIN_NAMESPACE_STD
286
287template <bool>
288class __vector_base_common
289{
290protected:
291 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
292 void __throw_length_error() const;
293 void __throw_out_of_range() const;
294};
295
296template <bool __b>
297void
298__vector_base_common<__b>::__throw_length_error() const
299{
300#ifndef _LIBCPP_NO_EXCEPTIONS
301 throw length_error("vector");
302#else
303 assert(!"vector length_error");
304#endif
305}
306
307template <bool __b>
308void
309__vector_base_common<__b>::__throw_out_of_range() const
310{
311#ifndef _LIBCPP_NO_EXCEPTIONS
312 throw out_of_range("vector");
313#else
314 assert(!"vector out_of_range");
315#endif
316}
317
Howard Hinnante9df0a52013-08-01 18:17:34 +0000318#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000319#pragma warning( push )
320#pragma warning( disable: 4231 )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000321#endif // _LIBCPP_MSVC
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000322_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS __vector_base_common<true>)
Howard Hinnante9df0a52013-08-01 18:17:34 +0000323#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000324#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000325#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000326
327template <class _Tp, class _Allocator>
328class __vector_base
329 : protected __vector_base_common<true>
330{
331protected:
Howard Hinnant324bb032010-08-22 00:02:43 +0000332 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000333 typedef _Allocator allocator_type;
334 typedef allocator_traits<allocator_type> __alloc_traits;
335 typedef value_type& reference;
336 typedef const value_type& const_reference;
337 typedef typename __alloc_traits::size_type size_type;
338 typedef typename __alloc_traits::difference_type difference_type;
339 typedef typename __alloc_traits::pointer pointer;
340 typedef typename __alloc_traits::const_pointer const_pointer;
341 typedef pointer iterator;
342 typedef const_pointer const_iterator;
343
344 pointer __begin_;
345 pointer __end_;
346 __compressed_pair<pointer, allocator_type> __end_cap_;
347
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000348 _LIBCPP_INLINE_VISIBILITY
349 allocator_type& __alloc() _NOEXCEPT
350 {return __end_cap_.second();}
351 _LIBCPP_INLINE_VISIBILITY
352 const allocator_type& __alloc() const _NOEXCEPT
353 {return __end_cap_.second();}
354 _LIBCPP_INLINE_VISIBILITY
355 pointer& __end_cap() _NOEXCEPT
356 {return __end_cap_.first();}
357 _LIBCPP_INLINE_VISIBILITY
358 const pointer& __end_cap() const _NOEXCEPT
359 {return __end_cap_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000360
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000361 _LIBCPP_INLINE_VISIBILITY
362 __vector_base()
363 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000364 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000365 ~__vector_base();
366
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000367 _LIBCPP_INLINE_VISIBILITY
368 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
369 _LIBCPP_INLINE_VISIBILITY
370 size_type capacity() const _NOEXCEPT
371 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000372
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000373 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000374 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377 void __copy_assign_alloc(const __vector_base& __c)
378 {__copy_assign_alloc(__c, integral_constant<bool,
379 __alloc_traits::propagate_on_container_copy_assignment::value>());}
380
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000382 void __move_assign_alloc(__vector_base& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000383 _NOEXCEPT_(
384 !__alloc_traits::propagate_on_container_move_assignment::value ||
385 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000386 {__move_assign_alloc(__c, integral_constant<bool,
387 __alloc_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000388private:
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000390 void __copy_assign_alloc(const __vector_base& __c, true_type)
391 {
392 if (__alloc() != __c.__alloc())
393 {
394 clear();
395 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
396 __begin_ = __end_ = __end_cap() = nullptr;
397 }
398 __alloc() = __c.__alloc();
399 }
400
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000402 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000403 {}
404
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000406 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000407 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000408 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000409 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000410 }
411
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000412 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000413 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000414 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000415 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000416};
417
418template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000419inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000420void
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000421__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000422{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000423 while (__new_last != __end_)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000424 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000425}
426
427template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000428inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000429__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000430 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000431 : __begin_(nullptr),
432 __end_(nullptr),
433 __end_cap_(nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000434{
435}
436
437template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000438inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000439__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000440 : __begin_(nullptr),
441 __end_(nullptr),
442 __end_cap_(nullptr, __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000443{
444}
445
446template <class _Tp, class _Allocator>
447__vector_base<_Tp, _Allocator>::~__vector_base()
448{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000449 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000450 {
451 clear();
452 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
453 }
454}
455
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700456template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000457class _LIBCPP_TYPE_VIS_ONLY vector
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000458 : private __vector_base<_Tp, _Allocator>
459{
460private:
461 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000462 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant324bb032010-08-22 00:02:43 +0000463public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000464 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +0000465 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000466 typedef _Allocator allocator_type;
467 typedef typename __base::__alloc_traits __alloc_traits;
468 typedef typename __base::reference reference;
469 typedef typename __base::const_reference const_reference;
470 typedef typename __base::size_type size_type;
471 typedef typename __base::difference_type difference_type;
472 typedef typename __base::pointer pointer;
473 typedef typename __base::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000474 typedef __wrap_iter<pointer> iterator;
475 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000476 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
477 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000478
Howard Hinnant02d5e182013-03-26 19:04:56 +0000479 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
480 "Allocator::value_type must be same type as value_type");
481
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000482 _LIBCPP_INLINE_VISIBILITY
Marshall Clowc912c0c2015-06-04 02:05:41 +0000483 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant7a563db2011-09-14 18:33:51 +0000484 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000485#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000486 __get_db()->__insert_c(this);
487#endif
488 }
489 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow127db912015-06-04 00:10:20 +0000490#if _LIBCPP_STD_VER <= 14
491 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
492#else
493 _NOEXCEPT
494#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +0000495 : __base(__a)
496 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000497#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000498 __get_db()->__insert_c(this);
499#endif
500 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000501 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59 +0000502#if _LIBCPP_STD_VER > 11
503 explicit vector(size_type __n, const allocator_type& __a);
504#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000505 vector(size_type __n, const_reference __x);
506 vector(size_type __n, const_reference __x, const allocator_type& __a);
507 template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +0000508 vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000509 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000510 !__is_forward_iterator<_InputIterator>::value &&
511 is_constructible<
512 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +0000513 typename iterator_traits<_InputIterator>::reference>::value,
514 _InputIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000515 template <class _InputIterator>
516 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
517 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000518 !__is_forward_iterator<_InputIterator>::value &&
519 is_constructible<
520 value_type,
521 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000522 template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +0000523 vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000524 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
525 is_constructible<
526 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +0000527 typename iterator_traits<_ForwardIterator>::reference>::value,
528 _ForwardIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000529 template <class _ForwardIterator>
530 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000531 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
532 is_constructible<
533 value_type,
534 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000535#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000537 vector(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000539 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000540#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantabe26282011-09-16 17:29:17 +0000541#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7a563db2011-09-14 18:33:51 +0000543 ~vector()
544 {
545 __get_db()->__erase_c(this);
546 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000547#endif
548
549 vector(const vector& __x);
550 vector(const vector& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000552 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000553#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000555 vector(vector&& __x)
Marshall Clow119ed472015-07-14 14:46:32 +0000556#if _LIBCPP_STD_VER > 14
557 _NOEXCEPT;
558#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000559 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow119ed472015-07-14 14:46:32 +0000560#endif
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000562 vector(vector&& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000564 vector& operator=(vector&& __x)
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700565 _NOEXCEPT_(
566 __alloc_traits::propagate_on_container_move_assignment::value &&
567 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000568#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000569#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000571 vector& operator=(initializer_list<value_type> __il)
572 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000573#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000574
575 template <class _InputIterator>
576 typename enable_if
577 <
578 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000579 !__is_forward_iterator<_InputIterator>::value &&
580 is_constructible<
581 value_type,
582 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000583 void
584 >::type
585 assign(_InputIterator __first, _InputIterator __last);
586 template <class _ForwardIterator>
587 typename enable_if
588 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000589 __is_forward_iterator<_ForwardIterator>::value &&
590 is_constructible<
591 value_type,
592 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000593 void
594 >::type
595 assign(_ForwardIterator __first, _ForwardIterator __last);
596
597 void assign(size_type __n, const_reference __u);
Howard Hinnante3e32912011-08-12 21:56:02 +0000598#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000600 void assign(initializer_list<value_type> __il)
601 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000602#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000603
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000604 _LIBCPP_INLINE_VISIBILITY
605 allocator_type get_allocator() const _NOEXCEPT
606 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000607
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000608 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
609 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
610 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
611 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000612
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000613 _LIBCPP_INLINE_VISIBILITY
614 reverse_iterator rbegin() _NOEXCEPT
615 {return reverse_iterator(end());}
616 _LIBCPP_INLINE_VISIBILITY
617 const_reverse_iterator rbegin() const _NOEXCEPT
618 {return const_reverse_iterator(end());}
619 _LIBCPP_INLINE_VISIBILITY
620 reverse_iterator rend() _NOEXCEPT
621 {return reverse_iterator(begin());}
622 _LIBCPP_INLINE_VISIBILITY
623 const_reverse_iterator rend() const _NOEXCEPT
624 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000625
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000626 _LIBCPP_INLINE_VISIBILITY
627 const_iterator cbegin() const _NOEXCEPT
628 {return begin();}
629 _LIBCPP_INLINE_VISIBILITY
630 const_iterator cend() const _NOEXCEPT
631 {return end();}
632 _LIBCPP_INLINE_VISIBILITY
633 const_reverse_iterator crbegin() const _NOEXCEPT
634 {return rbegin();}
635 _LIBCPP_INLINE_VISIBILITY
636 const_reverse_iterator crend() const _NOEXCEPT
637 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000638
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000639 _LIBCPP_INLINE_VISIBILITY
640 size_type size() const _NOEXCEPT
641 {return static_cast<size_type>(this->__end_ - this->__begin_);}
642 _LIBCPP_INLINE_VISIBILITY
643 size_type capacity() const _NOEXCEPT
644 {return __base::capacity();}
645 _LIBCPP_INLINE_VISIBILITY
646 bool empty() const _NOEXCEPT
647 {return this->__begin_ == this->__end_;}
648 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000649 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000650 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000651
652 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
653 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
654 reference at(size_type __n);
655 const_reference at(size_type __n) const;
656
Howard Hinnant7a563db2011-09-14 18:33:51 +0000657 _LIBCPP_INLINE_VISIBILITY reference front()
658 {
659 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
660 return *this->__begin_;
661 }
662 _LIBCPP_INLINE_VISIBILITY const_reference front() const
663 {
664 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
665 return *this->__begin_;
666 }
667 _LIBCPP_INLINE_VISIBILITY reference back()
668 {
669 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
670 return *(this->__end_ - 1);
671 }
672 _LIBCPP_INLINE_VISIBILITY const_reference back() const
673 {
674 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
675 return *(this->__end_ - 1);
676 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000677
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000678 _LIBCPP_INLINE_VISIBILITY
679 value_type* data() _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000680 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000681 _LIBCPP_INLINE_VISIBILITY
682 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000683 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000684
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000685 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000686#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000687 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000688#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000689 template <class... _Args>
690 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000691#endif // _LIBCPP_HAS_NO_VARIADICS
692#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000693 void pop_back();
694
695 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000696#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000697 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000698#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000699 template <class... _Args>
700 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000701#endif // _LIBCPP_HAS_NO_VARIADICS
702#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000703 iterator insert(const_iterator __position, size_type __n, const_reference __x);
704 template <class _InputIterator>
705 typename enable_if
706 <
707 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000708 !__is_forward_iterator<_InputIterator>::value &&
709 is_constructible<
710 value_type,
711 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000712 iterator
713 >::type
714 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
715 template <class _ForwardIterator>
716 typename enable_if
717 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000718 __is_forward_iterator<_ForwardIterator>::value &&
719 is_constructible<
720 value_type,
721 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000722 iterator
723 >::type
724 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +0000725#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000727 iterator insert(const_iterator __position, initializer_list<value_type> __il)
728 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000729#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000730
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000731 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000732 iterator erase(const_iterator __first, const_iterator __last);
733
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000734 _LIBCPP_INLINE_VISIBILITY
735 void clear() _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000736 {
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000737 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000738 __base::clear();
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000739 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000740 __invalidate_all_iterators();
741 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000742
743 void resize(size_type __sz);
744 void resize(size_type __sz, const_reference __x);
745
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000746 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +0000747#if _LIBCPP_STD_VER >= 14
748 _NOEXCEPT;
749#else
750 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
751 __is_nothrow_swappable<allocator_type>::value);
752#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000753
754 bool __invariants() const;
755
Howard Hinnantabe26282011-09-16 17:29:17 +0000756#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000757
758 bool __dereferenceable(const const_iterator* __i) const;
759 bool __decrementable(const const_iterator* __i) const;
760 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
761 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
762
Howard Hinnantabe26282011-09-16 17:29:17 +0000763#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000764
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000766 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000767 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000768 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000769 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31 +0000770 void __construct_at_end(size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000771 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000772 template <class _ForwardIterator>
773 typename enable_if
774 <
775 __is_forward_iterator<_ForwardIterator>::value,
776 void
777 >::type
Eric Fiselier088ed9f2015-03-31 16:54:19 +0000778 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000779 void __append(size_type __n);
780 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000782 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000784 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000785 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
786 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
787 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000788 void __move_assign(vector& __c, true_type)
789 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700790 void __move_assign(vector& __c, false_type);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000792 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000793 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000794#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000795 __c_node* __c = __get_db()->__find_c_and_lock(this);
796 for (__i_node** __p = __c->end_; __p != __c->beg_; )
797 {
798 --__p;
799 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
800 if (__i->base() > __new_last)
801 {
802 (*__p)->__c_ = nullptr;
803 if (--__c->end_ != __p)
804 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
805 }
806 }
807 __get_db()->unlock();
808#endif
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000809 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000810 __base::__destruct_at_end(__new_last);
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000811 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000812 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000813 template <class _Up>
814 void
815#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
816 __push_back_slow_path(_Up&& __x);
817#else
818 __push_back_slow_path(_Up& __x);
819#endif
Howard Hinnant0438ea22012-02-26 15:30:12 +0000820#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
821 template <class... _Args>
822 void
823 __emplace_back_slow_path(_Args&&... __args);
824#endif
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000825 // The following functions are no-ops outside of AddressSanitizer mode.
826 // We call annotatations only for the default Allocator because other allocators
827 // may not meet the AddressSanitizer alignment constraints.
828 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
829 void __annotate_contiguous_container
Kostya Serebryany497f9122014-09-02 23:43:38 +0000830 (const void *__beg, const void *__end, const void *__old_mid, const void *__new_mid) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000831 {
832#ifndef _LIBCPP_HAS_NO_ASAN
833 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
834 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
835#endif
836 }
837
Kostya Serebryany497f9122014-09-02 23:43:38 +0000838 void __annotate_new(size_type __current_size) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000839 {
840 __annotate_contiguous_container(data(), data() + capacity(),
841 data() + capacity(), data() + __current_size);
842 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000843 void __annotate_delete() const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000844 {
845 __annotate_contiguous_container(data(), data() + capacity(),
846 data() + size(), data() + capacity());
847 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000848 void __annotate_increase(size_type __n) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000849 {
850 __annotate_contiguous_container(data(), data() + capacity(),
851 data() + size(), data() + size() + __n);
852 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000853 void __annotate_shrink(size_type __old_size) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000854 {
855 __annotate_contiguous_container(data(), data() + capacity(),
856 data() + __old_size, data() + size());
857 }
Marshall Clow26f472d2014-09-03 21:37:43 +0000858#ifndef _LIBCPP_HAS_NO_ASAN
Kostya Serebryany497f9122014-09-02 23:43:38 +0000859 // The annotation for size increase should happen before the actual increase,
860 // but if an exception is thrown after that the annotation has to be undone.
861 struct __RAII_IncreaseAnnotator {
862 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000863 : __commit(false), __v(__v), __old_size(__v.size() + __n) {
Kostya Serebryany497f9122014-09-02 23:43:38 +0000864 __v.__annotate_increase(__n);
865 }
866 void __done() { __commit = true; }
867 ~__RAII_IncreaseAnnotator() {
868 if (__commit) return;
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000869 __v.__annotate_shrink(__old_size);
Kostya Serebryany497f9122014-09-02 23:43:38 +0000870 }
871 bool __commit;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000872 const vector &__v;
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000873 size_type __old_size;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000874 };
Marshall Clow26f472d2014-09-03 21:37:43 +0000875#else
876 struct __RAII_IncreaseAnnotator {
877 inline __RAII_IncreaseAnnotator(const vector &, size_type __n = 1) {}
878 inline void __done() {}
879 };
880#endif
881
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000882};
883
884template <class _Tp, class _Allocator>
885void
886vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
887{
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000888 __annotate_delete();
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000889 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000890 _VSTD::swap(this->__begin_, __v.__begin_);
891 _VSTD::swap(this->__end_, __v.__end_);
892 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000893 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000894 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000895 __invalidate_all_iterators();
896}
897
898template <class _Tp, class _Allocator>
899typename vector<_Tp, _Allocator>::pointer
900vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
901{
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000902 __annotate_delete();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000903 pointer __r = __v.__begin_;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000904 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
905 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000906 _VSTD::swap(this->__begin_, __v.__begin_);
907 _VSTD::swap(this->__end_, __v.__end_);
908 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000910 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000911 __invalidate_all_iterators();
912 return __r;
913}
914
915// Allocate space for __n objects
916// throws length_error if __n > max_size()
917// throws (probably bad_alloc) if memory run out
918// Precondition: __begin_ == __end_ == __end_cap() == 0
919// Precondition: __n > 0
920// Postcondition: capacity() == __n
921// Postcondition: size() == 0
922template <class _Tp, class _Allocator>
923void
924vector<_Tp, _Allocator>::allocate(size_type __n)
925{
926 if (__n > max_size())
927 this->__throw_length_error();
928 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
929 this->__end_cap() = this->__begin_ + __n;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000930 __annotate_new(0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000931}
932
933template <class _Tp, class _Allocator>
934void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000935vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000936{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000937 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000938 {
939 clear();
940 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000941 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000942 }
943}
944
945template <class _Tp, class _Allocator>
946typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000947vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000948{
Sean Hunt110b8bf2011-07-29 23:31:58 +0000949 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 +0000950}
951
952// Precondition: __new_size > capacity()
953template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000954inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000955typename vector<_Tp, _Allocator>::size_type
956vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
957{
958 const size_type __ms = max_size();
959 if (__new_size > __ms)
960 this->__throw_length_error();
961 const size_type __cap = capacity();
962 if (__cap >= __ms / 2)
963 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58 +0000964 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000965}
966
967// Default constructs __n objects starting at __end_
968// throws if construction throws
969// Precondition: __n > 0
970// Precondition: size() + __n <= capacity()
971// Postcondition: size() == size() + __n
972template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000973void
974vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
975{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000976 allocator_type& __a = this->__alloc();
977 do
978 {
Kostya Serebryany497f9122014-09-02 23:43:38 +0000979 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000980 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000981 ++this->__end_;
982 --__n;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000983 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000984 } while (__n > 0);
985}
986
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000987// Copy constructs __n objects starting at __end_ from __x
988// throws if construction throws
989// Precondition: __n > 0
990// Precondition: size() + __n <= capacity()
991// Postcondition: size() == old size() + __n
992// Postcondition: [i] == __x for all i in [size() - __n, __n)
993template <class _Tp, class _Allocator>
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700994inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000995void
996vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
997{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000998 allocator_type& __a = this->__alloc();
999 do
1000 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001001 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001002 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001003 ++this->__end_;
1004 --__n;
Kostya Serebryany497f9122014-09-02 23:43:38 +00001005 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001006 } while (__n > 0);
1007}
1008
1009template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001010template <class _ForwardIterator>
1011typename enable_if
1012<
1013 __is_forward_iterator<_ForwardIterator>::value,
1014 void
1015>::type
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001016vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001017{
1018 allocator_type& __a = this->__alloc();
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001019 __RAII_IncreaseAnnotator __annotator(*this, __n);
1020 __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
1021 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001022}
1023
1024// Default constructs __n objects starting at __end_
1025// throws if construction throws
1026// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001027// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001028template <class _Tp, class _Allocator>
1029void
1030vector<_Tp, _Allocator>::__append(size_type __n)
1031{
1032 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1033 this->__construct_at_end(__n);
1034 else
1035 {
1036 allocator_type& __a = this->__alloc();
1037 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1038 __v.__construct_at_end(__n);
1039 __swap_out_circular_buffer(__v);
1040 }
1041}
1042
1043// Default constructs __n objects starting at __end_
1044// throws if construction throws
1045// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001046// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001047template <class _Tp, class _Allocator>
1048void
1049vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1050{
1051 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1052 this->__construct_at_end(__n, __x);
1053 else
1054 {
1055 allocator_type& __a = this->__alloc();
1056 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1057 __v.__construct_at_end(__n, __x);
1058 __swap_out_circular_buffer(__v);
1059 }
1060}
1061
1062template <class _Tp, class _Allocator>
1063vector<_Tp, _Allocator>::vector(size_type __n)
1064{
Howard Hinnant0442b122011-09-16 18:41:29 +00001065#if _LIBCPP_DEBUG_LEVEL >= 2
1066 __get_db()->__insert_c(this);
1067#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001068 if (__n > 0)
1069 {
1070 allocate(__n);
1071 __construct_at_end(__n);
1072 }
1073}
1074
Marshall Clowa49a2c92013-09-14 00:47:59 +00001075#if _LIBCPP_STD_VER > 11
1076template <class _Tp, class _Allocator>
1077vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1078 : __base(__a)
1079{
1080#if _LIBCPP_DEBUG_LEVEL >= 2
1081 __get_db()->__insert_c(this);
1082#endif
1083 if (__n > 0)
1084 {
1085 allocate(__n);
1086 __construct_at_end(__n);
1087 }
1088}
1089#endif
1090
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001091template <class _Tp, class _Allocator>
1092vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1093{
Howard Hinnant0442b122011-09-16 18:41:29 +00001094#if _LIBCPP_DEBUG_LEVEL >= 2
1095 __get_db()->__insert_c(this);
1096#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001097 if (__n > 0)
1098 {
1099 allocate(__n);
1100 __construct_at_end(__n, __x);
1101 }
1102}
1103
1104template <class _Tp, class _Allocator>
1105vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1106 : __base(__a)
1107{
Howard Hinnant0442b122011-09-16 18:41:29 +00001108#if _LIBCPP_DEBUG_LEVEL >= 2
1109 __get_db()->__insert_c(this);
1110#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001111 if (__n > 0)
1112 {
1113 allocate(__n);
1114 __construct_at_end(__n, __x);
1115 }
1116}
1117
1118template <class _Tp, class _Allocator>
1119template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +00001120vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001121 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001122 !__is_forward_iterator<_InputIterator>::value &&
1123 is_constructible<
1124 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +00001125 typename iterator_traits<_InputIterator>::reference>::value,
1126 _InputIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001127{
Howard Hinnantabe26282011-09-16 17:29:17 +00001128#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001129 __get_db()->__insert_c(this);
1130#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001131 for (; __first != __last; ++__first)
1132 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001133}
1134
1135template <class _Tp, class _Allocator>
1136template <class _InputIterator>
1137vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1138 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001139 !__is_forward_iterator<_InputIterator>::value &&
1140 is_constructible<
1141 value_type,
1142 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001143 : __base(__a)
1144{
Howard Hinnantabe26282011-09-16 17:29:17 +00001145#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001146 __get_db()->__insert_c(this);
1147#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001148 for (; __first != __last; ++__first)
1149 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001150}
1151
1152template <class _Tp, class _Allocator>
1153template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +00001154vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001155 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1156 is_constructible<
1157 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +00001158 typename iterator_traits<_ForwardIterator>::reference>::value,
1159 _ForwardIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001160{
Howard Hinnant0442b122011-09-16 18:41:29 +00001161#if _LIBCPP_DEBUG_LEVEL >= 2
1162 __get_db()->__insert_c(this);
1163#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001164 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001165 if (__n > 0)
1166 {
1167 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001168 __construct_at_end(__first, __last, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001169 }
1170}
1171
1172template <class _Tp, class _Allocator>
1173template <class _ForwardIterator>
1174vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001175 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1176 is_constructible<
1177 value_type,
1178 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001179 : __base(__a)
1180{
Howard Hinnant0442b122011-09-16 18:41:29 +00001181#if _LIBCPP_DEBUG_LEVEL >= 2
1182 __get_db()->__insert_c(this);
1183#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001184 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001185 if (__n > 0)
1186 {
1187 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001188 __construct_at_end(__first, __last, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001189 }
1190}
1191
1192template <class _Tp, class _Allocator>
1193vector<_Tp, _Allocator>::vector(const vector& __x)
1194 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1195{
Howard Hinnant0442b122011-09-16 18:41:29 +00001196#if _LIBCPP_DEBUG_LEVEL >= 2
1197 __get_db()->__insert_c(this);
1198#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001199 size_type __n = __x.size();
1200 if (__n > 0)
1201 {
1202 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001203 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001204 }
1205}
1206
1207template <class _Tp, class _Allocator>
1208vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1209 : __base(__a)
1210{
Howard Hinnant0442b122011-09-16 18:41:29 +00001211#if _LIBCPP_DEBUG_LEVEL >= 2
1212 __get_db()->__insert_c(this);
1213#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001214 size_type __n = __x.size();
1215 if (__n > 0)
1216 {
1217 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001218 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001219 }
1220}
1221
Howard Hinnant73d21a42010-09-04 23:28:19 +00001222#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001223
1224template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001225inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001226vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clow119ed472015-07-14 14:46:32 +00001227#if _LIBCPP_STD_VER > 14
1228 _NOEXCEPT
1229#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001230 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow119ed472015-07-14 14:46:32 +00001231#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001232 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233{
Howard Hinnantabe26282011-09-16 17:29:17 +00001234#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001235 __get_db()->__insert_c(this);
Howard Hinnante6125bd2011-09-19 16:34:29 +00001236 __get_db()->swap(this, &__x);
Howard Hinnant7a563db2011-09-14 18:33:51 +00001237#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001238 this->__begin_ = __x.__begin_;
1239 this->__end_ = __x.__end_;
1240 this->__end_cap() = __x.__end_cap();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001241 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001242}
1243
1244template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001245inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001246vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1247 : __base(__a)
1248{
Howard Hinnant0442b122011-09-16 18:41:29 +00001249#if _LIBCPP_DEBUG_LEVEL >= 2
1250 __get_db()->__insert_c(this);
1251#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001252 if (__a == __x.__alloc())
1253 {
1254 this->__begin_ = __x.__begin_;
1255 this->__end_ = __x.__end_;
1256 this->__end_cap() = __x.__end_cap();
1257 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001258#if _LIBCPP_DEBUG_LEVEL >= 2
1259 __get_db()->swap(this, &__x);
1260#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001261 }
1262 else
1263 {
Howard Hinnant99968442011-11-29 18:15:50 +00001264 typedef move_iterator<iterator> _Ip;
1265 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001266 }
1267}
1268
Howard Hinnante3e32912011-08-12 21:56:02 +00001269#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1270
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001271template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001272inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001273vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1274{
Howard Hinnant0442b122011-09-16 18:41:29 +00001275#if _LIBCPP_DEBUG_LEVEL >= 2
1276 __get_db()->__insert_c(this);
1277#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001278 if (__il.size() > 0)
1279 {
1280 allocate(__il.size());
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001281 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001282 }
1283}
1284
1285template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001286inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001287vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1288 : __base(__a)
1289{
Howard Hinnant0442b122011-09-16 18:41:29 +00001290#if _LIBCPP_DEBUG_LEVEL >= 2
1291 __get_db()->__insert_c(this);
1292#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001293 if (__il.size() > 0)
1294 {
1295 allocate(__il.size());
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001296 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001297 }
1298}
1299
Howard Hinnante3e32912011-08-12 21:56:02 +00001300#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1301
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001302template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001303inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001304vector<_Tp, _Allocator>&
1305vector<_Tp, _Allocator>::operator=(vector&& __x)
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001306 _NOEXCEPT_(
1307 __alloc_traits::propagate_on_container_move_assignment::value &&
1308 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001309{
1310 __move_assign(__x, integral_constant<bool,
1311 __alloc_traits::propagate_on_container_move_assignment::value>());
1312 return *this;
1313}
1314
1315template <class _Tp, class _Allocator>
1316void
1317vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1318{
1319 if (__base::__alloc() != __c.__alloc())
1320 {
Howard Hinnant99968442011-11-29 18:15:50 +00001321 typedef move_iterator<iterator> _Ip;
1322 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001323 }
1324 else
1325 __move_assign(__c, true_type());
1326}
1327
1328template <class _Tp, class _Allocator>
1329void
1330vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001331 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001332{
1333 deallocate();
Marshall Clow3c2eac62014-07-21 15:11:13 +00001334 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001335 this->__begin_ = __c.__begin_;
1336 this->__end_ = __c.__end_;
1337 this->__end_cap() = __c.__end_cap();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001338 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001339#if _LIBCPP_DEBUG_LEVEL >= 2
1340 __get_db()->swap(this, &__c);
1341#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001342}
1343
Howard Hinnant73d21a42010-09-04 23:28:19 +00001344#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001345
1346template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001347inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001348vector<_Tp, _Allocator>&
1349vector<_Tp, _Allocator>::operator=(const vector& __x)
1350{
1351 if (this != &__x)
1352 {
1353 __base::__copy_assign_alloc(__x);
1354 assign(__x.__begin_, __x.__end_);
1355 }
1356 return *this;
1357}
1358
1359template <class _Tp, class _Allocator>
1360template <class _InputIterator>
1361typename enable_if
1362<
1363 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001364 !__is_forward_iterator<_InputIterator>::value &&
1365 is_constructible<
1366 _Tp,
1367 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001368 void
1369>::type
1370vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1371{
1372 clear();
1373 for (; __first != __last; ++__first)
1374 push_back(*__first);
1375}
1376
1377template <class _Tp, class _Allocator>
1378template <class _ForwardIterator>
1379typename enable_if
1380<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001381 __is_forward_iterator<_ForwardIterator>::value &&
1382 is_constructible<
1383 _Tp,
1384 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001385 void
1386>::type
1387vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1388{
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001389 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1390 if (__new_size <= capacity())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001391 {
1392 _ForwardIterator __mid = __last;
1393 bool __growing = false;
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001394 if (__new_size > size())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001395 {
1396 __growing = true;
1397 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001398 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001399 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001400 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001401 if (__growing)
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001402 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001403 else
1404 this->__destruct_at_end(__m);
1405 }
1406 else
1407 {
1408 deallocate();
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001409 allocate(__recommend(__new_size));
1410 __construct_at_end(__first, __last, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001411 }
1412}
1413
1414template <class _Tp, class _Allocator>
1415void
1416vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1417{
1418 if (__n <= capacity())
1419 {
1420 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001421 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001422 if (__n > __s)
1423 __construct_at_end(__n - __s, __u);
1424 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001425 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001426 }
1427 else
1428 {
1429 deallocate();
1430 allocate(__recommend(static_cast<size_type>(__n)));
1431 __construct_at_end(__n, __u);
1432 }
1433}
1434
Howard Hinnant324bb032010-08-22 00:02:43 +00001435template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001436inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001437typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001438vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001439{
Howard Hinnantabe26282011-09-16 17:29:17 +00001440#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441 return iterator(this, __p);
1442#else
1443 return iterator(__p);
1444#endif
1445}
1446
Howard Hinnant324bb032010-08-22 00:02:43 +00001447template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001448inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001449typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001450vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001451{
Howard Hinnantabe26282011-09-16 17:29:17 +00001452#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001453 return const_iterator(this, __p);
1454#else
1455 return const_iterator(__p);
1456#endif
1457}
1458
Howard Hinnant324bb032010-08-22 00:02:43 +00001459template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001460inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001461typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001462vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463{
1464 return __make_iter(this->__begin_);
1465}
1466
Howard Hinnant324bb032010-08-22 00:02:43 +00001467template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001468inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001470vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001471{
1472 return __make_iter(this->__begin_);
1473}
1474
Howard Hinnant324bb032010-08-22 00:02:43 +00001475template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001476inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001477typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001478vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001479{
1480 return __make_iter(this->__end_);
1481}
1482
Howard Hinnant324bb032010-08-22 00:02:43 +00001483template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001484inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001485typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001486vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001487{
1488 return __make_iter(this->__end_);
1489}
1490
Howard Hinnant324bb032010-08-22 00:02:43 +00001491template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001492inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001493typename vector<_Tp, _Allocator>::reference
1494vector<_Tp, _Allocator>::operator[](size_type __n)
1495{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001496 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001497 return this->__begin_[__n];
1498}
1499
Howard Hinnant324bb032010-08-22 00:02:43 +00001500template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001501inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001502typename vector<_Tp, _Allocator>::const_reference
1503vector<_Tp, _Allocator>::operator[](size_type __n) const
1504{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001505 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506 return this->__begin_[__n];
1507}
1508
Howard Hinnant324bb032010-08-22 00:02:43 +00001509template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001510typename vector<_Tp, _Allocator>::reference
1511vector<_Tp, _Allocator>::at(size_type __n)
1512{
1513 if (__n >= size())
1514 this->__throw_out_of_range();
1515 return this->__begin_[__n];
1516}
1517
Howard Hinnant324bb032010-08-22 00:02:43 +00001518template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001519typename vector<_Tp, _Allocator>::const_reference
1520vector<_Tp, _Allocator>::at(size_type __n) const
1521{
1522 if (__n >= size())
1523 this->__throw_out_of_range();
1524 return this->__begin_[__n];
1525}
1526
1527template <class _Tp, class _Allocator>
1528void
1529vector<_Tp, _Allocator>::reserve(size_type __n)
1530{
1531 if (__n > capacity())
1532 {
1533 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001534 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001535 __swap_out_circular_buffer(__v);
1536 }
1537}
1538
1539template <class _Tp, class _Allocator>
1540void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001541vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001542{
1543 if (capacity() > size())
1544 {
1545#ifndef _LIBCPP_NO_EXCEPTIONS
1546 try
1547 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001548#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001549 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001550 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001551 __swap_out_circular_buffer(__v);
1552#ifndef _LIBCPP_NO_EXCEPTIONS
1553 }
1554 catch (...)
1555 {
1556 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001557#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001558 }
1559}
1560
1561template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001562template <class _Up>
1563void
1564#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1565vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1566#else
1567vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1568#endif
1569{
1570 allocator_type& __a = this->__alloc();
1571 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1572 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnantf619e232013-01-11 20:36:59 +00001573 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1574 __v.__end_++;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001575 __swap_out_circular_buffer(__v);
1576}
1577
1578template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001579inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001580void
1581vector<_Tp, _Allocator>::push_back(const_reference __x)
1582{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001583 if (this->__end_ != this->__end_cap())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001584 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001585 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001586 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001587 _VSTD::__to_raw_pointer(this->__end_), __x);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001588 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001589 ++this->__end_;
1590 }
1591 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001592 __push_back_slow_path(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001593}
1594
Howard Hinnant73d21a42010-09-04 23:28:19 +00001595#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001596
1597template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001598inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001599void
1600vector<_Tp, _Allocator>::push_back(value_type&& __x)
1601{
1602 if (this->__end_ < this->__end_cap())
1603 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001604 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001605 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001606 _VSTD::__to_raw_pointer(this->__end_),
1607 _VSTD::move(__x));
Kostya Serebryany497f9122014-09-02 23:43:38 +00001608 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001609 ++this->__end_;
1610 }
1611 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001612 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001613}
1614
Howard Hinnant73d21a42010-09-04 23:28:19 +00001615#ifndef _LIBCPP_HAS_NO_VARIADICS
1616
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001617template <class _Tp, class _Allocator>
1618template <class... _Args>
1619void
Howard Hinnant0438ea22012-02-26 15:30:12 +00001620vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1621{
1622 allocator_type& __a = this->__alloc();
1623 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1624// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantf619e232013-01-11 20:36:59 +00001625 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1626 __v.__end_++;
Howard Hinnant0438ea22012-02-26 15:30:12 +00001627 __swap_out_circular_buffer(__v);
1628}
1629
1630template <class _Tp, class _Allocator>
1631template <class... _Args>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001632inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0438ea22012-02-26 15:30:12 +00001633void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001634vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1635{
1636 if (this->__end_ < this->__end_cap())
1637 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001638 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001639 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001640 _VSTD::__to_raw_pointer(this->__end_),
1641 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001642 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001643 ++this->__end_;
1644 }
1645 else
Howard Hinnant0438ea22012-02-26 15:30:12 +00001646 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001647}
1648
Howard Hinnant73d21a42010-09-04 23:28:19 +00001649#endif // _LIBCPP_HAS_NO_VARIADICS
1650#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001651
1652template <class _Tp, class _Allocator>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001653inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001654void
1655vector<_Tp, _Allocator>::pop_back()
1656{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001657 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001658 this->__destruct_at_end(this->__end_ - 1);
1659}
1660
1661template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001662inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001663typename vector<_Tp, _Allocator>::iterator
1664vector<_Tp, _Allocator>::erase(const_iterator __position)
1665{
Howard Hinnantabe26282011-09-16 17:29:17 +00001666#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001667 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1668 "vector::erase(iterator) called with an iterator not"
1669 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001670#endif
Howard Hinnant782da332013-03-25 22:12:26 +00001671 _LIBCPP_ASSERT(__position != end(),
1672 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001673 difference_type __ps = __position - cbegin();
1674 pointer __p = this->__begin_ + __ps;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001675 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001676 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001677 return __r;
1678}
1679
1680template <class _Tp, class _Allocator>
1681typename vector<_Tp, _Allocator>::iterator
1682vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1683{
Howard Hinnantabe26282011-09-16 17:29:17 +00001684#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001685 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1686 "vector::erase(iterator, iterator) called with an iterator not"
1687 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001688#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +00001689 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001690 pointer __p = this->__begin_ + (__first - begin());
1691 iterator __r = __make_iter(__p);
Howard Hinnantb4e67cf2013-04-18 15:02:57 +00001692 if (__first != __last)
1693 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001694 return __r;
1695}
1696
1697template <class _Tp, class _Allocator>
1698void
1699vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1700{
1701 pointer __old_last = this->__end_;
1702 difference_type __n = __old_last - __to;
1703 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1704 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001705 _VSTD::__to_raw_pointer(this->__end_),
1706 _VSTD::move(*__i));
1707 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001708}
1709
1710template <class _Tp, class _Allocator>
1711typename vector<_Tp, _Allocator>::iterator
1712vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1713{
Howard Hinnantabe26282011-09-16 17:29:17 +00001714#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001715 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1716 "vector::insert(iterator, x) called with an iterator not"
1717 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001718#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001719 pointer __p = this->__begin_ + (__position - begin());
1720 if (this->__end_ < this->__end_cap())
1721 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001722 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001723 if (__p == this->__end_)
1724 {
1725 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001726 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001727 ++this->__end_;
1728 }
1729 else
1730 {
1731 __move_range(__p, this->__end_, __p + 1);
1732 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1733 if (__p <= __xr && __xr < this->__end_)
1734 ++__xr;
1735 *__p = *__xr;
1736 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001737 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001738 }
1739 else
1740 {
1741 allocator_type& __a = this->__alloc();
1742 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1743 __v.push_back(__x);
1744 __p = __swap_out_circular_buffer(__v, __p);
1745 }
1746 return __make_iter(__p);
1747}
1748
Howard Hinnant73d21a42010-09-04 23:28:19 +00001749#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001750
1751template <class _Tp, class _Allocator>
1752typename vector<_Tp, _Allocator>::iterator
1753vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1754{
Howard Hinnantabe26282011-09-16 17:29:17 +00001755#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001756 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1757 "vector::insert(iterator, x) called with an iterator not"
1758 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001759#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001760 pointer __p = this->__begin_ + (__position - begin());
1761 if (this->__end_ < this->__end_cap())
1762 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001763 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001764 if (__p == this->__end_)
1765 {
1766 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001767 _VSTD::__to_raw_pointer(this->__end_),
1768 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001769 ++this->__end_;
1770 }
1771 else
1772 {
1773 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001774 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001775 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001776 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001777 }
1778 else
1779 {
1780 allocator_type& __a = this->__alloc();
1781 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001782 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001783 __p = __swap_out_circular_buffer(__v, __p);
1784 }
1785 return __make_iter(__p);
1786}
1787
Howard Hinnant73d21a42010-09-04 23:28:19 +00001788#ifndef _LIBCPP_HAS_NO_VARIADICS
1789
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001790template <class _Tp, class _Allocator>
1791template <class... _Args>
1792typename vector<_Tp, _Allocator>::iterator
1793vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1794{
Howard Hinnantabe26282011-09-16 17:29:17 +00001795#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001796 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1797 "vector::emplace(iterator, x) called with an iterator not"
1798 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001799#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001800 pointer __p = this->__begin_ + (__position - begin());
1801 if (this->__end_ < this->__end_cap())
1802 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001803 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001804 if (__p == this->__end_)
1805 {
1806 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001807 _VSTD::__to_raw_pointer(this->__end_),
1808 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001809 ++this->__end_;
1810 }
1811 else
1812 {
Howard Hinnanta58402a2012-07-08 23:23:04 +00001813 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001814 __move_range(__p, this->__end_, __p + 1);
Howard Hinnanta58402a2012-07-08 23:23:04 +00001815 *__p = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001816 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001817 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001818 }
1819 else
1820 {
1821 allocator_type& __a = this->__alloc();
1822 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001823 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001824 __p = __swap_out_circular_buffer(__v, __p);
1825 }
1826 return __make_iter(__p);
1827}
1828
Howard Hinnant73d21a42010-09-04 23:28:19 +00001829#endif // _LIBCPP_HAS_NO_VARIADICS
1830#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001831
1832template <class _Tp, class _Allocator>
1833typename vector<_Tp, _Allocator>::iterator
1834vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1835{
Howard Hinnantabe26282011-09-16 17:29:17 +00001836#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001837 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1838 "vector::insert(iterator, n, x) called with an iterator not"
1839 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001840#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001841 pointer __p = this->__begin_ + (__position - begin());
1842 if (__n > 0)
1843 {
1844 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1845 {
1846 size_type __old_n = __n;
1847 pointer __old_last = this->__end_;
1848 if (__n > static_cast<size_type>(this->__end_ - __p))
1849 {
1850 size_type __cx = __n - (this->__end_ - __p);
1851 __construct_at_end(__cx, __x);
1852 __n -= __cx;
1853 }
1854 if (__n > 0)
1855 {
Eric Fiselierd7590952014-11-14 18:28:36 +00001856 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001857 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001858 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001859 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1860 if (__p <= __xr && __xr < this->__end_)
1861 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001862 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001863 }
1864 }
1865 else
1866 {
1867 allocator_type& __a = this->__alloc();
1868 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1869 __v.__construct_at_end(__n, __x);
1870 __p = __swap_out_circular_buffer(__v, __p);
1871 }
1872 }
1873 return __make_iter(__p);
1874}
1875
1876template <class _Tp, class _Allocator>
1877template <class _InputIterator>
1878typename enable_if
1879<
1880 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001881 !__is_forward_iterator<_InputIterator>::value &&
1882 is_constructible<
1883 _Tp,
1884 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001885 typename vector<_Tp, _Allocator>::iterator
1886>::type
1887vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1888{
Howard Hinnantabe26282011-09-16 17:29:17 +00001889#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001890 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1891 "vector::insert(iterator, range) called with an iterator not"
1892 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001893#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001894 difference_type __off = __position - begin();
1895 pointer __p = this->__begin_ + __off;
1896 allocator_type& __a = this->__alloc();
1897 pointer __old_last = this->__end_;
1898 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1899 {
Eric Fiselier7d439a42015-07-18 18:22:12 +00001900 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001901 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001902 *__first);
1903 ++this->__end_;
Eric Fiselier7d439a42015-07-18 18:22:12 +00001904 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001905 }
1906 __split_buffer<value_type, allocator_type&> __v(__a);
1907 if (__first != __last)
1908 {
1909#ifndef _LIBCPP_NO_EXCEPTIONS
1910 try
1911 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001912#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001913 __v.__construct_at_end(__first, __last);
1914 difference_type __old_size = __old_last - this->__begin_;
1915 difference_type __old_p = __p - this->__begin_;
1916 reserve(__recommend(size() + __v.size()));
1917 __p = this->__begin_ + __old_p;
1918 __old_last = this->__begin_ + __old_size;
1919#ifndef _LIBCPP_NO_EXCEPTIONS
1920 }
1921 catch (...)
1922 {
1923 erase(__make_iter(__old_last), end());
1924 throw;
1925 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001926#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001927 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001928 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant0442b122011-09-16 18:41:29 +00001929 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1930 make_move_iterator(__v.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001931 return begin() + __off;
1932}
1933
1934template <class _Tp, class _Allocator>
1935template <class _ForwardIterator>
1936typename enable_if
1937<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001938 __is_forward_iterator<_ForwardIterator>::value &&
1939 is_constructible<
1940 _Tp,
1941 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001942 typename vector<_Tp, _Allocator>::iterator
1943>::type
1944vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1945{
Howard Hinnantabe26282011-09-16 17:29:17 +00001946#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001947 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1948 "vector::insert(iterator, range) called with an iterator not"
1949 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001950#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001951 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00001952 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953 if (__n > 0)
1954 {
1955 if (__n <= this->__end_cap() - this->__end_)
1956 {
1957 size_type __old_n = __n;
1958 pointer __old_last = this->__end_;
1959 _ForwardIterator __m = __last;
1960 difference_type __dx = this->__end_ - __p;
1961 if (__n > __dx)
1962 {
1963 __m = __first;
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001964 difference_type __diff = this->__end_ - __p;
1965 _VSTD::advance(__m, __diff);
1966 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001967 __n = __dx;
1968 }
1969 if (__n > 0)
1970 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001971 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001972 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001973 __annotator.__done();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001974 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001975 }
1976 }
1977 else
1978 {
1979 allocator_type& __a = this->__alloc();
1980 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1981 __v.__construct_at_end(__first, __last);
1982 __p = __swap_out_circular_buffer(__v, __p);
1983 }
1984 }
1985 return __make_iter(__p);
1986}
1987
1988template <class _Tp, class _Allocator>
1989void
1990vector<_Tp, _Allocator>::resize(size_type __sz)
1991{
1992 size_type __cs = size();
1993 if (__cs < __sz)
1994 this->__append(__sz - __cs);
1995 else if (__cs > __sz)
1996 this->__destruct_at_end(this->__begin_ + __sz);
1997}
1998
1999template <class _Tp, class _Allocator>
2000void
2001vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2002{
2003 size_type __cs = size();
2004 if (__cs < __sz)
2005 this->__append(__sz - __cs, __x);
2006 else if (__cs > __sz)
2007 this->__destruct_at_end(this->__begin_ + __sz);
2008}
2009
2010template <class _Tp, class _Allocator>
2011void
2012vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow7d914d12015-07-13 20:04:56 +00002013#if _LIBCPP_STD_VER >= 14
2014 _NOEXCEPT
2015#else
2016 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2017 __is_nothrow_swappable<allocator_type>::value)
2018#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002019{
Howard Hinnant7a563db2011-09-14 18:33:51 +00002020 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2021 this->__alloc() == __x.__alloc(),
2022 "vector::swap: Either propagate_on_container_swap must be true"
2023 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +00002024 _VSTD::swap(this->__begin_, __x.__begin_);
2025 _VSTD::swap(this->__end_, __x.__end_);
2026 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Marshall Clow7d914d12015-07-13 20:04:56 +00002027 __swap_allocator(this->__alloc(), __x.__alloc(),
2028 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantabe26282011-09-16 17:29:17 +00002029#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002030 __get_db()->swap(this, &__x);
Howard Hinnantabe26282011-09-16 17:29:17 +00002031#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002032}
2033
Howard Hinnant324bb032010-08-22 00:02:43 +00002034template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035bool
2036vector<_Tp, _Allocator>::__invariants() const
2037{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002038 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002039 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002040 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002041 return false;
2042 }
2043 else
2044 {
2045 if (this->__begin_ > this->__end_)
2046 return false;
2047 if (this->__begin_ == this->__end_cap())
2048 return false;
2049 if (this->__end_ > this->__end_cap())
2050 return false;
2051 }
2052 return true;
2053}
2054
Howard Hinnantabe26282011-09-16 17:29:17 +00002055#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002056
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002057template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:51 +00002058bool
2059vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2060{
2061 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2062}
2063
2064template <class _Tp, class _Allocator>
2065bool
2066vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2067{
2068 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2069}
2070
2071template <class _Tp, class _Allocator>
2072bool
2073vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2074{
2075 const_pointer __p = __i->base() + __n;
2076 return this->__begin_ <= __p && __p <= this->__end_;
2077}
2078
2079template <class _Tp, class _Allocator>
2080bool
2081vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2082{
2083 const_pointer __p = __i->base() + __n;
2084 return this->__begin_ <= __p && __p < this->__end_;
2085}
2086
Howard Hinnantabe26282011-09-16 17:29:17 +00002087#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002088
2089template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002090inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002091void
2092vector<_Tp, _Allocator>::__invalidate_all_iterators()
2093{
Howard Hinnantabe26282011-09-16 17:29:17 +00002094#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002095 __get_db()->__invalidate_all(this);
Howard Hinnantabe26282011-09-16 17:29:17 +00002096#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002097}
2098
2099// vector<bool>
2100
2101template <class _Allocator> class vector<bool, _Allocator>;
2102
2103template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2104
2105template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002106struct __has_storage_type<vector<bool, _Allocator> >
2107{
2108 static const bool value = true;
2109};
2110
2111template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002112class _LIBCPP_TYPE_VIS_ONLY vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002113 : private __vector_base_common<true>
2114{
2115public:
2116 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00002117 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002118 typedef _Allocator allocator_type;
2119 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002120 typedef typename __alloc_traits::size_type size_type;
2121 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantf867f632012-05-07 16:50:38 +00002122 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002123 typedef __bit_iterator<vector, false> pointer;
2124 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002125 typedef pointer iterator;
2126 typedef const_pointer const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002127 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2128 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002129
2130private:
Marshall Clow66302c62015-04-07 05:21:38 +00002131 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002132 typedef allocator_traits<__storage_allocator> __storage_traits;
2133 typedef typename __storage_traits::pointer __storage_pointer;
2134 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2135
2136 __storage_pointer __begin_;
2137 size_type __size_;
2138 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002139public:
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002140 typedef __bit_reference<vector> reference;
2141 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002142private:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002143 _LIBCPP_INLINE_VISIBILITY
2144 size_type& __cap() _NOEXCEPT
2145 {return __cap_alloc_.first();}
2146 _LIBCPP_INLINE_VISIBILITY
2147 const size_type& __cap() const _NOEXCEPT
2148 {return __cap_alloc_.first();}
2149 _LIBCPP_INLINE_VISIBILITY
2150 __storage_allocator& __alloc() _NOEXCEPT
2151 {return __cap_alloc_.second();}
2152 _LIBCPP_INLINE_VISIBILITY
2153 const __storage_allocator& __alloc() const _NOEXCEPT
2154 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002155
2156 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2157
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002158 _LIBCPP_INLINE_VISIBILITY
2159 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002160 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002161 _LIBCPP_INLINE_VISIBILITY
2162 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002163 {return (__n - 1) / __bits_per_word + 1;}
2164
2165public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002166 _LIBCPP_INLINE_VISIBILITY
Marshall Clowc912c0c2015-06-04 02:05:41 +00002167 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow127db912015-06-04 00:10:20 +00002168
2169 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2170#if _LIBCPP_STD_VER <= 14
2171 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2172#else
2173 _NOEXCEPT;
2174#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002175 ~vector();
2176 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59 +00002177#if _LIBCPP_STD_VER > 11
2178 explicit vector(size_type __n, const allocator_type& __a);
2179#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002180 vector(size_type __n, const value_type& __v);
2181 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2182 template <class _InputIterator>
2183 vector(_InputIterator __first, _InputIterator __last,
2184 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2185 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2186 template <class _InputIterator>
2187 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2188 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2189 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2190 template <class _ForwardIterator>
2191 vector(_ForwardIterator __first, _ForwardIterator __last,
2192 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2193 template <class _ForwardIterator>
2194 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2195 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2196
2197 vector(const vector& __v);
2198 vector(const vector& __v, const allocator_type& __a);
2199 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00002200#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002201 vector(initializer_list<value_type> __il);
2202 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00002203#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002204
Howard Hinnant73d21a42010-09-04 23:28:19 +00002205#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002206 _LIBCPP_INLINE_VISIBILITY
2207 vector(vector&& __v)
Marshall Clow119ed472015-07-14 14:46:32 +00002208#if _LIBCPP_STD_VER > 14
2209 _NOEXCEPT;
2210#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002211 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow119ed472015-07-14 14:46:32 +00002212#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002213 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002214 _LIBCPP_INLINE_VISIBILITY
2215 vector& operator=(vector&& __v)
Dan Albert1d4a1ed2016-05-25 22:36:09 -07002216 _NOEXCEPT_(
2217 __alloc_traits::propagate_on_container_move_assignment::value &&
2218 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002219#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +00002220#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002222 vector& operator=(initializer_list<value_type> __il)
2223 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00002224#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002225
2226 template <class _InputIterator>
2227 typename enable_if
2228 <
2229 __is_input_iterator<_InputIterator>::value &&
2230 !__is_forward_iterator<_InputIterator>::value,
2231 void
2232 >::type
2233 assign(_InputIterator __first, _InputIterator __last);
2234 template <class _ForwardIterator>
2235 typename enable_if
2236 <
2237 __is_forward_iterator<_ForwardIterator>::value,
2238 void
2239 >::type
2240 assign(_ForwardIterator __first, _ForwardIterator __last);
2241
2242 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +00002243#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002244 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002245 void assign(initializer_list<value_type> __il)
2246 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002247#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002248
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002249 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002250 {return allocator_type(this->__alloc());}
2251
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002252 size_type max_size() const _NOEXCEPT;
2253 _LIBCPP_INLINE_VISIBILITY
2254 size_type capacity() const _NOEXCEPT
2255 {return __internal_cap_to_external(__cap());}
2256 _LIBCPP_INLINE_VISIBILITY
2257 size_type size() const _NOEXCEPT
2258 {return __size_;}
2259 _LIBCPP_INLINE_VISIBILITY
2260 bool empty() const _NOEXCEPT
2261 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002262 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002263 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002264
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002265 _LIBCPP_INLINE_VISIBILITY
2266 iterator begin() _NOEXCEPT
2267 {return __make_iter(0);}
2268 _LIBCPP_INLINE_VISIBILITY
2269 const_iterator begin() const _NOEXCEPT
2270 {return __make_iter(0);}
2271 _LIBCPP_INLINE_VISIBILITY
2272 iterator end() _NOEXCEPT
2273 {return __make_iter(__size_);}
2274 _LIBCPP_INLINE_VISIBILITY
2275 const_iterator end() const _NOEXCEPT
2276 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002277
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002278 _LIBCPP_INLINE_VISIBILITY
2279 reverse_iterator rbegin() _NOEXCEPT
2280 {return reverse_iterator(end());}
2281 _LIBCPP_INLINE_VISIBILITY
2282 const_reverse_iterator rbegin() const _NOEXCEPT
2283 {return const_reverse_iterator(end());}
2284 _LIBCPP_INLINE_VISIBILITY
2285 reverse_iterator rend() _NOEXCEPT
2286 {return reverse_iterator(begin());}
2287 _LIBCPP_INLINE_VISIBILITY
2288 const_reverse_iterator rend() const _NOEXCEPT
2289 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002290
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002291 _LIBCPP_INLINE_VISIBILITY
2292 const_iterator cbegin() const _NOEXCEPT
2293 {return __make_iter(0);}
2294 _LIBCPP_INLINE_VISIBILITY
2295 const_iterator cend() const _NOEXCEPT
2296 {return __make_iter(__size_);}
2297 _LIBCPP_INLINE_VISIBILITY
2298 const_reverse_iterator crbegin() const _NOEXCEPT
2299 {return rbegin();}
2300 _LIBCPP_INLINE_VISIBILITY
2301 const_reverse_iterator crend() const _NOEXCEPT
2302 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002303
2304 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2305 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2306 reference at(size_type __n);
2307 const_reference at(size_type __n) const;
2308
2309 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2310 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2311 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2312 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2313
2314 void push_back(const value_type& __x);
Marshall Clow198a2a52013-08-13 23:54:12 +00002315#if _LIBCPP_STD_VER > 11
2316 template <class... _Args>
2317 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2318 { push_back ( value_type ( _VSTD::forward<_Args>(__args)... )); }
2319#endif
2320
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002321 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2322
Marshall Clow198a2a52013-08-13 23:54:12 +00002323#if _LIBCPP_STD_VER > 11
2324 template <class... _Args>
2325 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2326 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2327#endif
2328
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002329 iterator insert(const_iterator __position, const value_type& __x);
2330 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2331 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2332 template <class _InputIterator>
2333 typename enable_if
2334 <
2335 __is_input_iterator <_InputIterator>::value &&
2336 !__is_forward_iterator<_InputIterator>::value,
2337 iterator
2338 >::type
2339 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2340 template <class _ForwardIterator>
2341 typename enable_if
2342 <
2343 __is_forward_iterator<_ForwardIterator>::value,
2344 iterator
2345 >::type
2346 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00002347#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002348 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002349 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2350 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002351#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002352
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002353 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002354 iterator erase(const_iterator __first, const_iterator __last);
2355
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002356 _LIBCPP_INLINE_VISIBILITY
2357 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002358
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002359 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +00002360#if _LIBCPP_STD_VER >= 14
2361 _NOEXCEPT;
2362#else
2363 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2364 __is_nothrow_swappable<allocator_type>::value);
2365#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002366
2367 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002368 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002369
2370 bool __invariants() const;
2371
2372private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002373 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002374 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002375 void deallocate() _NOEXCEPT;
2376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00002377 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow0d1965d2014-07-28 15:02:42 +00002378 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002379 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2380 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002381 template <class _ForwardIterator>
2382 typename enable_if
2383 <
2384 __is_forward_iterator<_ForwardIterator>::value,
2385 void
2386 >::type
2387 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2388 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002389 _LIBCPP_INLINE_VISIBILITY
2390 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002391 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002392 _LIBCPP_INLINE_VISIBILITY
2393 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002394 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002395 _LIBCPP_INLINE_VISIBILITY
2396 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002397 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002398 _LIBCPP_INLINE_VISIBILITY
2399 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002400 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002401 _LIBCPP_INLINE_VISIBILITY
2402 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002403 {return begin() + (__p - cbegin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002404
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& __v)
2407 {__copy_assign_alloc(__v, integral_constant<bool,
2408 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002410 void __copy_assign_alloc(const vector& __c, true_type)
2411 {
2412 if (__alloc() != __c.__alloc())
2413 deallocate();
2414 __alloc() = __c.__alloc();
2415 }
2416
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002418 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002419 {}
2420
2421 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002422 void __move_assign(vector& __c, true_type)
2423 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002425 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002426 _NOEXCEPT_(
2427 !__storage_traits::propagate_on_container_move_assignment::value ||
2428 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002429 {__move_assign_alloc(__c, integral_constant<bool,
2430 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002431 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00002432 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002433 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002434 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002435 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002436 }
2437
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002438 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002439 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002440 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002441 {}
2442
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002443 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002444
2445 friend class __bit_reference<vector>;
2446 friend class __bit_const_reference<vector>;
2447 friend class __bit_iterator<vector, false>;
2448 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:18 +00002449 friend struct __bit_array<vector>;
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002450 friend struct _LIBCPP_TYPE_VIS_ONLY hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002451};
2452
2453template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002454inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002455void
2456vector<bool, _Allocator>::__invalidate_all_iterators()
2457{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002458}
2459
2460// Allocate space for __n objects
2461// throws length_error if __n > max_size()
2462// throws (probably bad_alloc) if memory run out
2463// Precondition: __begin_ == __end_ == __cap() == 0
2464// Precondition: __n > 0
2465// Postcondition: capacity() == __n
2466// Postcondition: size() == 0
2467template <class _Allocator>
2468void
2469vector<bool, _Allocator>::allocate(size_type __n)
2470{
2471 if (__n > max_size())
2472 this->__throw_length_error();
2473 __n = __external_cap_to_internal(__n);
2474 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2475 this->__size_ = 0;
2476 this->__cap() = __n;
2477}
2478
2479template <class _Allocator>
2480void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002481vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002482{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002483 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002484 {
2485 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2486 __invalidate_all_iterators();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002487 this->__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002488 this->__size_ = this->__cap() = 0;
2489 }
2490}
2491
2492template <class _Allocator>
2493typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002494vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002495{
2496 size_type __amax = __storage_traits::max_size(__alloc());
2497 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2498 if (__nmax / __bits_per_word <= __amax)
2499 return __nmax;
2500 return __internal_cap_to_external(__amax);
2501}
2502
2503// Precondition: __new_size > capacity()
2504template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002505inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002506typename vector<bool, _Allocator>::size_type
2507vector<bool, _Allocator>::__recommend(size_type __new_size) const
2508{
2509 const size_type __ms = max_size();
2510 if (__new_size > __ms)
2511 this->__throw_length_error();
2512 const size_type __cap = capacity();
2513 if (__cap >= __ms / 2)
2514 return __ms;
Howard Hinnant7f764502013-08-14 18:00:20 +00002515 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002516}
2517
2518// Default constructs __n objects starting at __end_
2519// Precondition: __n > 0
2520// Precondition: size() + __n <= capacity()
2521// Postcondition: size() == size() + __n
2522template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002523inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002524void
2525vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2526{
2527 size_type __old_size = this->__size_;
2528 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002529 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002530}
2531
2532template <class _Allocator>
2533template <class _ForwardIterator>
2534typename enable_if
2535<
2536 __is_forward_iterator<_ForwardIterator>::value,
2537 void
2538>::type
2539vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2540{
2541 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002542 this->__size_ += _VSTD::distance(__first, __last);
2543 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002544}
2545
2546template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002547inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002548vector<bool, _Allocator>::vector()
Marshall Clowc912c0c2015-06-04 02:05:41 +00002549 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002550 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002551 __size_(0),
2552 __cap_alloc_(0)
2553{
2554}
2555
2556template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002557inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002558vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow127db912015-06-04 00:10:20 +00002559#if _LIBCPP_STD_VER <= 14
2560 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2561#else
2562 _NOEXCEPT
2563#endif
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002564 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002565 __size_(0),
2566 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2567{
2568}
2569
2570template <class _Allocator>
2571vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002572 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002573 __size_(0),
2574 __cap_alloc_(0)
2575{
2576 if (__n > 0)
2577 {
2578 allocate(__n);
2579 __construct_at_end(__n, false);
2580 }
2581}
2582
Marshall Clowa49a2c92013-09-14 00:47:59 +00002583#if _LIBCPP_STD_VER > 11
2584template <class _Allocator>
2585vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2586 : __begin_(nullptr),
2587 __size_(0),
2588 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2589{
2590 if (__n > 0)
2591 {
2592 allocate(__n);
2593 __construct_at_end(__n, false);
2594 }
2595}
2596#endif
2597
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002598template <class _Allocator>
2599vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002600 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002601 __size_(0),
2602 __cap_alloc_(0)
2603{
2604 if (__n > 0)
2605 {
2606 allocate(__n);
2607 __construct_at_end(__n, __x);
2608 }
2609}
2610
2611template <class _Allocator>
2612vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002613 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002614 __size_(0),
2615 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2616{
2617 if (__n > 0)
2618 {
2619 allocate(__n);
2620 __construct_at_end(__n, __x);
2621 }
2622}
2623
2624template <class _Allocator>
2625template <class _InputIterator>
2626vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2627 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2628 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002629 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002630 __size_(0),
2631 __cap_alloc_(0)
2632{
2633#ifndef _LIBCPP_NO_EXCEPTIONS
2634 try
2635 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002636#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002637 for (; __first != __last; ++__first)
2638 push_back(*__first);
2639#ifndef _LIBCPP_NO_EXCEPTIONS
2640 }
2641 catch (...)
2642 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002643 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002644 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2645 __invalidate_all_iterators();
2646 throw;
2647 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002648#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002649}
2650
2651template <class _Allocator>
2652template <class _InputIterator>
2653vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2654 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2655 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002656 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002657 __size_(0),
2658 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2659{
2660#ifndef _LIBCPP_NO_EXCEPTIONS
2661 try
2662 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002663#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002664 for (; __first != __last; ++__first)
2665 push_back(*__first);
2666#ifndef _LIBCPP_NO_EXCEPTIONS
2667 }
2668 catch (...)
2669 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002670 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002671 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2672 __invalidate_all_iterators();
2673 throw;
2674 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002675#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002676}
2677
2678template <class _Allocator>
2679template <class _ForwardIterator>
2680vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2681 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002682 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002683 __size_(0),
2684 __cap_alloc_(0)
2685{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002686 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002687 if (__n > 0)
2688 {
2689 allocate(__n);
2690 __construct_at_end(__first, __last);
2691 }
2692}
2693
2694template <class _Allocator>
2695template <class _ForwardIterator>
2696vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2697 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002698 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002699 __size_(0),
2700 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2701{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002702 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002703 if (__n > 0)
2704 {
2705 allocate(__n);
2706 __construct_at_end(__first, __last);
2707 }
2708}
2709
Howard Hinnante3e32912011-08-12 21:56:02 +00002710#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2711
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002712template <class _Allocator>
2713vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002714 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002715 __size_(0),
2716 __cap_alloc_(0)
2717{
2718 size_type __n = static_cast<size_type>(__il.size());
2719 if (__n > 0)
2720 {
2721 allocate(__n);
2722 __construct_at_end(__il.begin(), __il.end());
2723 }
2724}
2725
2726template <class _Allocator>
2727vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002728 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002729 __size_(0),
2730 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2731{
2732 size_type __n = static_cast<size_type>(__il.size());
2733 if (__n > 0)
2734 {
2735 allocate(__n);
2736 __construct_at_end(__il.begin(), __il.end());
2737 }
2738}
2739
Howard Hinnante3e32912011-08-12 21:56:02 +00002740#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2741
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002742template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002743vector<bool, _Allocator>::~vector()
2744{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002745 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002746 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002747 __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002748}
2749
2750template <class _Allocator>
2751vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002752 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002753 __size_(0),
2754 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2755{
2756 if (__v.size() > 0)
2757 {
2758 allocate(__v.size());
2759 __construct_at_end(__v.begin(), __v.end());
2760 }
2761}
2762
2763template <class _Allocator>
2764vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002765 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002766 __size_(0),
2767 __cap_alloc_(0, __a)
2768{
2769 if (__v.size() > 0)
2770 {
2771 allocate(__v.size());
2772 __construct_at_end(__v.begin(), __v.end());
2773 }
2774}
2775
2776template <class _Allocator>
2777vector<bool, _Allocator>&
2778vector<bool, _Allocator>::operator=(const vector& __v)
2779{
2780 if (this != &__v)
2781 {
2782 __copy_assign_alloc(__v);
2783 if (__v.__size_)
2784 {
2785 if (__v.__size_ > capacity())
2786 {
2787 deallocate();
2788 allocate(__v.__size_);
2789 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002790 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002791 }
2792 __size_ = __v.__size_;
2793 }
2794 return *this;
2795}
2796
Howard Hinnant73d21a42010-09-04 23:28:19 +00002797#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2798
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002799template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002800inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002801vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clow119ed472015-07-14 14:46:32 +00002802#if _LIBCPP_STD_VER > 14
2803 _NOEXCEPT
2804#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002805 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow119ed472015-07-14 14:46:32 +00002806#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002807 : __begin_(__v.__begin_),
2808 __size_(__v.__size_),
2809 __cap_alloc_(__v.__cap_alloc_)
2810{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002811 __v.__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002812 __v.__size_ = 0;
2813 __v.__cap() = 0;
2814}
2815
2816template <class _Allocator>
2817vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002818 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002819 __size_(0),
2820 __cap_alloc_(0, __a)
2821{
2822 if (__a == allocator_type(__v.__alloc()))
2823 {
2824 this->__begin_ = __v.__begin_;
2825 this->__size_ = __v.__size_;
2826 this->__cap() = __v.__cap();
2827 __v.__begin_ = nullptr;
2828 __v.__cap() = __v.__size_ = 0;
2829 }
2830 else if (__v.size() > 0)
2831 {
2832 allocate(__v.size());
2833 __construct_at_end(__v.begin(), __v.end());
2834 }
2835}
2836
2837template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002838inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002839vector<bool, _Allocator>&
2840vector<bool, _Allocator>::operator=(vector&& __v)
Dan Albert1d4a1ed2016-05-25 22:36:09 -07002841 _NOEXCEPT_(
2842 __alloc_traits::propagate_on_container_move_assignment::value &&
2843 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002844{
2845 __move_assign(__v, integral_constant<bool,
2846 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002847 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002848}
2849
2850template <class _Allocator>
2851void
2852vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2853{
2854 if (__alloc() != __c.__alloc())
2855 assign(__c.begin(), __c.end());
2856 else
2857 __move_assign(__c, true_type());
2858}
2859
2860template <class _Allocator>
2861void
2862vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002863 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002864{
2865 deallocate();
Marshall Clowdb5e54d2014-07-21 15:15:15 +00002866 __move_assign_alloc(__c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002867 this->__begin_ = __c.__begin_;
2868 this->__size_ = __c.__size_;
2869 this->__cap() = __c.__cap();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002870 __c.__begin_ = nullptr;
2871 __c.__cap() = __c.__size_ = 0;
2872}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002873
2874#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002875
2876template <class _Allocator>
2877void
2878vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2879{
2880 __size_ = 0;
2881 if (__n > 0)
2882 {
2883 size_type __c = capacity();
2884 if (__n <= __c)
2885 __size_ = __n;
2886 else
2887 {
2888 vector __v(__alloc());
2889 __v.reserve(__recommend(__n));
2890 __v.__size_ = __n;
2891 swap(__v);
2892 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002893 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002894 }
2895}
2896
2897template <class _Allocator>
2898template <class _InputIterator>
2899typename enable_if
2900<
2901 __is_input_iterator<_InputIterator>::value &&
2902 !__is_forward_iterator<_InputIterator>::value,
2903 void
2904>::type
2905vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2906{
2907 clear();
2908 for (; __first != __last; ++__first)
2909 push_back(*__first);
2910}
2911
2912template <class _Allocator>
2913template <class _ForwardIterator>
2914typename enable_if
2915<
2916 __is_forward_iterator<_ForwardIterator>::value,
2917 void
2918>::type
2919vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2920{
2921 clear();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002922 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002923 if (__n)
2924 {
2925 if (__n > capacity())
2926 {
2927 deallocate();
2928 allocate(__n);
2929 }
2930 __construct_at_end(__first, __last);
2931 }
2932}
2933
2934template <class _Allocator>
2935void
2936vector<bool, _Allocator>::reserve(size_type __n)
2937{
2938 if (__n > capacity())
2939 {
2940 vector __v(this->__alloc());
2941 __v.allocate(__n);
2942 __v.__construct_at_end(this->begin(), this->end());
2943 swap(__v);
2944 __invalidate_all_iterators();
2945 }
2946}
2947
2948template <class _Allocator>
2949void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002950vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002951{
2952 if (__external_cap_to_internal(size()) > __cap())
2953 {
2954#ifndef _LIBCPP_NO_EXCEPTIONS
2955 try
2956 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002957#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002958 vector(*this, allocator_type(__alloc())).swap(*this);
2959#ifndef _LIBCPP_NO_EXCEPTIONS
2960 }
2961 catch (...)
2962 {
2963 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002964#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002965 }
2966}
2967
2968template <class _Allocator>
2969typename vector<bool, _Allocator>::reference
2970vector<bool, _Allocator>::at(size_type __n)
2971{
2972 if (__n >= size())
2973 this->__throw_out_of_range();
2974 return (*this)[__n];
2975}
2976
2977template <class _Allocator>
2978typename vector<bool, _Allocator>::const_reference
2979vector<bool, _Allocator>::at(size_type __n) const
2980{
2981 if (__n >= size())
2982 this->__throw_out_of_range();
2983 return (*this)[__n];
2984}
2985
2986template <class _Allocator>
2987void
2988vector<bool, _Allocator>::push_back(const value_type& __x)
2989{
2990 if (this->__size_ == this->capacity())
2991 reserve(__recommend(this->__size_ + 1));
2992 ++this->__size_;
2993 back() = __x;
2994}
2995
2996template <class _Allocator>
2997typename vector<bool, _Allocator>::iterator
2998vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2999{
3000 iterator __r;
3001 if (size() < capacity())
3002 {
3003 const_iterator __old_end = end();
3004 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003005 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003006 __r = __const_iterator_cast(__position);
3007 }
3008 else
3009 {
3010 vector __v(__alloc());
3011 __v.reserve(__recommend(__size_ + 1));
3012 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003013 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3014 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003015 swap(__v);
3016 }
3017 *__r = __x;
3018 return __r;
3019}
3020
3021template <class _Allocator>
3022typename vector<bool, _Allocator>::iterator
3023vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3024{
3025 iterator __r;
3026 size_type __c = capacity();
3027 if (__n <= __c && size() <= __c - __n)
3028 {
3029 const_iterator __old_end = end();
3030 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003031 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003032 __r = __const_iterator_cast(__position);
3033 }
3034 else
3035 {
3036 vector __v(__alloc());
3037 __v.reserve(__recommend(__size_ + __n));
3038 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003039 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3040 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003041 swap(__v);
3042 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003043 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003044 return __r;
3045}
3046
3047template <class _Allocator>
3048template <class _InputIterator>
3049typename enable_if
3050<
3051 __is_input_iterator <_InputIterator>::value &&
3052 !__is_forward_iterator<_InputIterator>::value,
3053 typename vector<bool, _Allocator>::iterator
3054>::type
3055vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3056{
3057 difference_type __off = __position - begin();
3058 iterator __p = __const_iterator_cast(__position);
3059 iterator __old_end = end();
3060 for (; size() != capacity() && __first != __last; ++__first)
3061 {
3062 ++this->__size_;
3063 back() = *__first;
3064 }
3065 vector __v(__alloc());
3066 if (__first != __last)
3067 {
3068#ifndef _LIBCPP_NO_EXCEPTIONS
3069 try
3070 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003071#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003072 __v.assign(__first, __last);
3073 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3074 difference_type __old_p = __p - begin();
3075 reserve(__recommend(size() + __v.size()));
3076 __p = begin() + __old_p;
3077 __old_end = begin() + __old_size;
3078#ifndef _LIBCPP_NO_EXCEPTIONS
3079 }
3080 catch (...)
3081 {
3082 erase(__old_end, end());
3083 throw;
3084 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003085#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003086 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003087 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003088 insert(__p, __v.begin(), __v.end());
3089 return begin() + __off;
3090}
3091
3092template <class _Allocator>
3093template <class _ForwardIterator>
3094typename enable_if
3095<
3096 __is_forward_iterator<_ForwardIterator>::value,
3097 typename vector<bool, _Allocator>::iterator
3098>::type
3099vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3100{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003101 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003102 iterator __r;
3103 size_type __c = capacity();
3104 if (__n <= __c && size() <= __c - __n)
3105 {
3106 const_iterator __old_end = end();
3107 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003108 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003109 __r = __const_iterator_cast(__position);
3110 }
3111 else
3112 {
3113 vector __v(__alloc());
3114 __v.reserve(__recommend(__size_ + __n));
3115 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003116 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3117 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003118 swap(__v);
3119 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003120 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003121 return __r;
3122}
3123
3124template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003125inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003126typename vector<bool, _Allocator>::iterator
3127vector<bool, _Allocator>::erase(const_iterator __position)
3128{
3129 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003130 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003131 --__size_;
3132 return __r;
3133}
3134
3135template <class _Allocator>
3136typename vector<bool, _Allocator>::iterator
3137vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3138{
3139 iterator __r = __const_iterator_cast(__first);
3140 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003141 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003142 __size_ -= __d;
3143 return __r;
3144}
3145
3146template <class _Allocator>
3147void
3148vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow7d914d12015-07-13 20:04:56 +00003149#if _LIBCPP_STD_VER >= 14
3150 _NOEXCEPT
3151#else
3152 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3153 __is_nothrow_swappable<allocator_type>::value)
3154#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003155{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003156 _VSTD::swap(this->__begin_, __x.__begin_);
3157 _VSTD::swap(this->__size_, __x.__size_);
3158 _VSTD::swap(this->__cap(), __x.__cap());
Marshall Clow7d914d12015-07-13 20:04:56 +00003159 __swap_allocator(this->__alloc(), __x.__alloc(),
3160 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003161}
3162
Howard Hinnant324bb032010-08-22 00:02:43 +00003163template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003164void
3165vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3166{
3167 size_type __cs = size();
3168 if (__cs < __sz)
3169 {
3170 iterator __r;
3171 size_type __c = capacity();
3172 size_type __n = __sz - __cs;
3173 if (__n <= __c && __cs <= __c - __n)
3174 {
3175 __r = end();
3176 __size_ += __n;
3177 }
3178 else
3179 {
3180 vector __v(__alloc());
3181 __v.reserve(__recommend(__size_ + __n));
3182 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003183 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003184 swap(__v);
3185 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003186 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003187 }
3188 else
3189 __size_ = __sz;
3190}
3191
Howard Hinnant324bb032010-08-22 00:02:43 +00003192template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003193void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003194vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003195{
3196 // do middle whole words
3197 size_type __n = __size_;
3198 __storage_pointer __p = __begin_;
3199 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3200 *__p = ~*__p;
3201 // do last partial word
3202 if (__n > 0)
3203 {
3204 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3205 __storage_type __b = *__p & __m;
3206 *__p &= ~__m;
3207 *__p |= ~__b & __m;
3208 }
3209}
3210
Howard Hinnant324bb032010-08-22 00:02:43 +00003211template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003212bool
3213vector<bool, _Allocator>::__invariants() const
3214{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00003215 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003216 {
3217 if (this->__size_ != 0 || this->__cap() != 0)
3218 return false;
3219 }
3220 else
3221 {
3222 if (this->__cap() == 0)
3223 return false;
3224 if (this->__size_ > this->capacity())
3225 return false;
3226 }
3227 return true;
3228}
3229
Howard Hinnant324bb032010-08-22 00:02:43 +00003230template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003231size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003232vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003233{
3234 size_t __h = 0;
3235 // do middle whole words
3236 size_type __n = __size_;
3237 __storage_pointer __p = __begin_;
3238 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3239 __h ^= *__p;
3240 // do last partial word
3241 if (__n > 0)
3242 {
3243 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3244 __h ^= *__p & __m;
3245 }
3246 return __h;
3247}
3248
3249template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003250struct _LIBCPP_TYPE_VIS_ONLY hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003251 : public unary_function<vector<bool, _Allocator>, size_t>
3252{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00003253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003254 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003255 {return __vec.__hash_code();}
3256};
3257
3258template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003259inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003260bool
3261operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3262{
3263 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003264 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003265}
3266
3267template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003268inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003269bool
3270operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3271{
3272 return !(__x == __y);
3273}
3274
3275template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003276inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003277bool
3278operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3279{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003280 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003281}
3282
3283template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003284inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003285bool
3286operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3287{
3288 return __y < __x;
3289}
3290
3291template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003292inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003293bool
3294operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3295{
3296 return !(__x < __y);
3297}
3298
3299template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003300inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003301bool
3302operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3303{
3304 return !(__y < __x);
3305}
3306
3307template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003308inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003309void
3310swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003311 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003312{
3313 __x.swap(__y);
3314}
3315
3316_LIBCPP_END_NAMESPACE_STD
3317
3318#endif // _LIBCPP_VECTOR