blob: 1cafae16efef4fea2771fbbd425fa74b04b05665 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------------ vector --------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_VECTOR
12#define _LIBCPP_VECTOR
13
14/*
15 vector synopsis
16
17namespace std
18{
19
Howard Hinnant324bb032010-08-22 00:02:43 +000020template <class T, class Allocator = allocator<T> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021class vector
Howard Hinnant324bb032010-08-22 00:02:43 +000022{
23public:
24 typedef T value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000025 typedef Allocator allocator_type;
26 typedef typename allocator_type::reference reference;
27 typedef typename allocator_type::const_reference const_reference;
28 typedef implementation-defined iterator;
29 typedef implementation-defined const_iterator;
30 typedef typename allocator_type::size_type size_type;
31 typedef typename allocator_type::difference_type difference_type;
32 typedef typename allocator_type::pointer pointer;
33 typedef typename allocator_type::const_pointer const_pointer;
34 typedef std::reverse_iterator<iterator> reverse_iterator;
35 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
36
Howard Hinnantd1d27a42011-06-03 19:40:40 +000037 vector()
38 noexcept(is_nothrow_default_constructible<allocator_type>::value);
39 explicit vector(const allocator_type&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000040 explicit vector(size_type n);
Marshall Clowa49a2c92013-09-14 00:47:59 +000041 explicit vector(size_type n, const allocator_type&); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
43 template <class InputIterator>
44 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
45 vector(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +000046 vector(vector&& x)
47 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000048 vector(initializer_list<value_type> il);
49 vector(initializer_list<value_type> il, const allocator_type& a);
50 ~vector();
51 vector& operator=(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +000052 vector& operator=(vector&& x)
53 noexcept(
Marshall Clowaf961ed2015-08-18 18:57:00 +000054 allocator_type::propagate_on_container_move_assignment::value ||
55 allocator_type::is_always_equal::value); // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056 vector& operator=(initializer_list<value_type> il);
57 template <class InputIterator>
58 void assign(InputIterator first, InputIterator last);
59 void assign(size_type n, const value_type& u);
60 void assign(initializer_list<value_type> il);
61
Howard Hinnantd1d27a42011-06-03 19:40:40 +000062 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063
Howard Hinnantd1d27a42011-06-03 19:40:40 +000064 iterator begin() noexcept;
65 const_iterator begin() const noexcept;
66 iterator end() noexcept;
67 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000068
Howard Hinnantd1d27a42011-06-03 19:40:40 +000069 reverse_iterator rbegin() noexcept;
70 const_reverse_iterator rbegin() const noexcept;
71 reverse_iterator rend() noexcept;
72 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000073
Howard Hinnantd1d27a42011-06-03 19:40:40 +000074 const_iterator cbegin() const noexcept;
75 const_iterator cend() const noexcept;
76 const_reverse_iterator crbegin() const noexcept;
77 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000078
Howard Hinnantd1d27a42011-06-03 19:40:40 +000079 size_type size() const noexcept;
80 size_type max_size() const noexcept;
81 size_type capacity() const noexcept;
82 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +000084 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000085
86 reference operator[](size_type n);
87 const_reference operator[](size_type n) const;
88 reference at(size_type n);
89 const_reference at(size_type n) const;
90
91 reference front();
92 const_reference front() const;
93 reference back();
94 const_reference back() const;
95
Howard Hinnantd1d27a42011-06-03 19:40:40 +000096 value_type* data() noexcept;
97 const value_type* data() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098
99 void push_back(const value_type& x);
100 void push_back(value_type&& x);
101 template <class... Args>
102 void emplace_back(Args&&... args);
103 void pop_back();
104
105 template <class... Args> iterator emplace(const_iterator position, Args&&... args);
106 iterator insert(const_iterator position, const value_type& x);
107 iterator insert(const_iterator position, value_type&& x);
108 iterator insert(const_iterator position, size_type n, const value_type& x);
109 template <class InputIterator>
110 iterator insert(const_iterator position, InputIterator first, InputIterator last);
111 iterator insert(const_iterator position, initializer_list<value_type> il);
112
113 iterator erase(const_iterator position);
114 iterator erase(const_iterator first, const_iterator last);
115
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000116 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000117
118 void resize(size_type sz);
119 void resize(size_type sz, const value_type& c);
120
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000121 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +0000122 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
123 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000124
125 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000126};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000127
Howard Hinnant324bb032010-08-22 00:02:43 +0000128template <class Allocator = allocator<T> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129class vector<bool, Allocator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000130{
131public:
132 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000133 typedef Allocator allocator_type;
134 typedef implementation-defined iterator;
135 typedef implementation-defined const_iterator;
136 typedef typename allocator_type::size_type size_type;
137 typedef typename allocator_type::difference_type difference_type;
138 typedef iterator pointer;
139 typedef const_iterator const_pointer;
140 typedef std::reverse_iterator<iterator> reverse_iterator;
141 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
142
143 class reference
144 {
145 public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000146 reference(const reference&) noexcept;
147 operator bool() const noexcept;
148 reference& operator=(const bool x) noexcept;
149 reference& operator=(const reference& x) noexcept;
150 iterator operator&() const noexcept;
151 void flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152 };
153
154 class const_reference
155 {
156 public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000157 const_reference(const reference&) noexcept;
158 operator bool() const noexcept;
159 const_iterator operator&() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160 };
161
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000162 vector()
163 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant9cbee432011-09-02 20:42:31 +0000164 explicit vector(const allocator_type&);
Marshall Clowa49a2c92013-09-14 00:47:59 +0000165 explicit vector(size_type n, const allocator_type& a = allocator_type()); // C++14
166 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000167 template <class InputIterator>
168 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
169 vector(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000170 vector(vector&& x)
171 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000172 vector(initializer_list<value_type> il);
173 vector(initializer_list<value_type> il, const allocator_type& a);
174 ~vector();
175 vector& operator=(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000176 vector& operator=(vector&& x)
177 noexcept(
Marshall Clowaf961ed2015-08-18 18:57:00 +0000178 allocator_type::propagate_on_container_move_assignment::value ||
179 allocator_type::is_always_equal::value); // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000180 vector& operator=(initializer_list<value_type> il);
181 template <class InputIterator>
182 void assign(InputIterator first, InputIterator last);
183 void assign(size_type n, const value_type& u);
184 void assign(initializer_list<value_type> il);
185
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000186 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000188 iterator begin() noexcept;
189 const_iterator begin() const noexcept;
190 iterator end() noexcept;
191 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000192
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000193 reverse_iterator rbegin() noexcept;
194 const_reverse_iterator rbegin() const noexcept;
195 reverse_iterator rend() noexcept;
196 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000197
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000198 const_iterator cbegin() const noexcept;
199 const_iterator cend() const noexcept;
200 const_reverse_iterator crbegin() const noexcept;
201 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000203 size_type size() const noexcept;
204 size_type max_size() const noexcept;
205 size_type capacity() const noexcept;
206 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000207 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000208 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000209
210 reference operator[](size_type n);
211 const_reference operator[](size_type n) const;
212 reference at(size_type n);
213 const_reference at(size_type n) const;
214
215 reference front();
216 const_reference front() const;
217 reference back();
218 const_reference back() const;
219
220 void push_back(const value_type& x);
Marshall Clow198a2a52013-08-13 23:54:12 +0000221 template <class... Args> void emplace_back(Args&&... args); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000222 void pop_back();
223
Marshall Clow198a2a52013-08-13 23:54:12 +0000224 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000225 iterator insert(const_iterator position, const value_type& x);
226 iterator insert(const_iterator position, size_type n, const value_type& x);
227 template <class InputIterator>
228 iterator insert(const_iterator position, InputIterator first, InputIterator last);
229 iterator insert(const_iterator position, initializer_list<value_type> il);
230
231 iterator erase(const_iterator position);
232 iterator erase(const_iterator first, const_iterator last);
233
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000234 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235
236 void resize(size_type sz);
237 void resize(size_type sz, value_type x);
238
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000239 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +0000240 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
241 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000242 void flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243
244 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000245};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000246
247template <class Allocator> struct hash<std::vector<bool, Allocator>>;
248
249template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
250template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
251template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
252template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
253template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
254template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
255
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000256template <class T, class Allocator>
257void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
258 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000259
260} // std
261
262*/
263
264#include <__config>
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
456template <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)
Marshall Clowaf961ed2015-08-18 18:57:00 +0000565 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnant73d21a42010-09-04 23:28:19 +0000566#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000567#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000569 vector& operator=(initializer_list<value_type> __il)
570 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000571#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572
573 template <class _InputIterator>
574 typename enable_if
575 <
576 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000577 !__is_forward_iterator<_InputIterator>::value &&
578 is_constructible<
579 value_type,
580 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000581 void
582 >::type
583 assign(_InputIterator __first, _InputIterator __last);
584 template <class _ForwardIterator>
585 typename enable_if
586 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000587 __is_forward_iterator<_ForwardIterator>::value &&
588 is_constructible<
589 value_type,
590 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000591 void
592 >::type
593 assign(_ForwardIterator __first, _ForwardIterator __last);
594
595 void assign(size_type __n, const_reference __u);
Howard Hinnante3e32912011-08-12 21:56:02 +0000596#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000597 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000598 void assign(initializer_list<value_type> __il)
599 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000600#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000601
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000602 _LIBCPP_INLINE_VISIBILITY
603 allocator_type get_allocator() const _NOEXCEPT
604 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000605
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000606 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
607 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
608 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
609 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000610
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000611 _LIBCPP_INLINE_VISIBILITY
612 reverse_iterator rbegin() _NOEXCEPT
613 {return reverse_iterator(end());}
614 _LIBCPP_INLINE_VISIBILITY
615 const_reverse_iterator rbegin() const _NOEXCEPT
616 {return const_reverse_iterator(end());}
617 _LIBCPP_INLINE_VISIBILITY
618 reverse_iterator rend() _NOEXCEPT
619 {return reverse_iterator(begin());}
620 _LIBCPP_INLINE_VISIBILITY
621 const_reverse_iterator rend() const _NOEXCEPT
622 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000623
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000624 _LIBCPP_INLINE_VISIBILITY
625 const_iterator cbegin() const _NOEXCEPT
626 {return begin();}
627 _LIBCPP_INLINE_VISIBILITY
628 const_iterator cend() const _NOEXCEPT
629 {return end();}
630 _LIBCPP_INLINE_VISIBILITY
631 const_reverse_iterator crbegin() const _NOEXCEPT
632 {return rbegin();}
633 _LIBCPP_INLINE_VISIBILITY
634 const_reverse_iterator crend() const _NOEXCEPT
635 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000636
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000637 _LIBCPP_INLINE_VISIBILITY
638 size_type size() const _NOEXCEPT
639 {return static_cast<size_type>(this->__end_ - this->__begin_);}
640 _LIBCPP_INLINE_VISIBILITY
641 size_type capacity() const _NOEXCEPT
642 {return __base::capacity();}
643 _LIBCPP_INLINE_VISIBILITY
644 bool empty() const _NOEXCEPT
645 {return this->__begin_ == this->__end_;}
646 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000647 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000648 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000649
650 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
651 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
652 reference at(size_type __n);
653 const_reference at(size_type __n) const;
654
Howard Hinnant7a563db2011-09-14 18:33:51 +0000655 _LIBCPP_INLINE_VISIBILITY reference front()
656 {
657 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
658 return *this->__begin_;
659 }
660 _LIBCPP_INLINE_VISIBILITY const_reference front() const
661 {
662 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
663 return *this->__begin_;
664 }
665 _LIBCPP_INLINE_VISIBILITY reference back()
666 {
667 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
668 return *(this->__end_ - 1);
669 }
670 _LIBCPP_INLINE_VISIBILITY const_reference back() const
671 {
672 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
673 return *(this->__end_ - 1);
674 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000675
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000676 _LIBCPP_INLINE_VISIBILITY
677 value_type* data() _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000678 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000679 _LIBCPP_INLINE_VISIBILITY
680 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000681 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000682
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000683 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000684#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000685 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000686#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000687 template <class... _Args>
688 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000689#endif // _LIBCPP_HAS_NO_VARIADICS
690#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000691 void pop_back();
692
693 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000694#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000695 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000696#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000697 template <class... _Args>
698 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000699#endif // _LIBCPP_HAS_NO_VARIADICS
700#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000701 iterator insert(const_iterator __position, size_type __n, const_reference __x);
702 template <class _InputIterator>
703 typename enable_if
704 <
705 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000706 !__is_forward_iterator<_InputIterator>::value &&
707 is_constructible<
708 value_type,
709 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000710 iterator
711 >::type
712 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
713 template <class _ForwardIterator>
714 typename enable_if
715 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000716 __is_forward_iterator<_ForwardIterator>::value &&
717 is_constructible<
718 value_type,
719 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000720 iterator
721 >::type
722 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +0000723#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000724 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000725 iterator insert(const_iterator __position, initializer_list<value_type> __il)
726 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000727#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000728
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000729 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000730 iterator erase(const_iterator __first, const_iterator __last);
731
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000732 _LIBCPP_INLINE_VISIBILITY
733 void clear() _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000734 {
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000735 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000736 __base::clear();
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000737 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000738 __invalidate_all_iterators();
739 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000740
741 void resize(size_type __sz);
742 void resize(size_type __sz, const_reference __x);
743
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000744 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +0000745#if _LIBCPP_STD_VER >= 14
746 _NOEXCEPT;
747#else
748 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
749 __is_nothrow_swappable<allocator_type>::value);
750#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000751
752 bool __invariants() const;
753
Howard Hinnantabe26282011-09-16 17:29:17 +0000754#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000755
756 bool __dereferenceable(const const_iterator* __i) const;
757 bool __decrementable(const const_iterator* __i) const;
758 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
759 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
760
Howard Hinnantabe26282011-09-16 17:29:17 +0000761#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000762
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000763private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000764 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000766 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000767 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31 +0000768 void __construct_at_end(size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000769 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000770 template <class _ForwardIterator>
771 typename enable_if
772 <
773 __is_forward_iterator<_ForwardIterator>::value,
774 void
775 >::type
Eric Fiselier088ed9f2015-03-31 16:54:19 +0000776 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000777 void __append(size_type __n);
778 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000780 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000782 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000783 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
784 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
785 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000786 void __move_assign(vector& __c, true_type)
787 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clowaf961ed2015-08-18 18:57:00 +0000788 void __move_assign(vector& __c, false_type)
789 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000791 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000792 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000793#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000794 __c_node* __c = __get_db()->__find_c_and_lock(this);
795 for (__i_node** __p = __c->end_; __p != __c->beg_; )
796 {
797 --__p;
798 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
799 if (__i->base() > __new_last)
800 {
801 (*__p)->__c_ = nullptr;
802 if (--__c->end_ != __p)
803 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
804 }
805 }
806 __get_db()->unlock();
807#endif
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000808 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000809 __base::__destruct_at_end(__new_last);
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000810 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000811 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000812 template <class _Up>
813 void
814#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
815 __push_back_slow_path(_Up&& __x);
816#else
817 __push_back_slow_path(_Up& __x);
818#endif
Howard Hinnant0438ea22012-02-26 15:30:12 +0000819#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
820 template <class... _Args>
821 void
822 __emplace_back_slow_path(_Args&&... __args);
823#endif
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000824 // The following functions are no-ops outside of AddressSanitizer mode.
825 // We call annotatations only for the default Allocator because other allocators
826 // may not meet the AddressSanitizer alignment constraints.
827 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
828 void __annotate_contiguous_container
Kostya Serebryany497f9122014-09-02 23:43:38 +0000829 (const void *__beg, const void *__end, const void *__old_mid, const void *__new_mid) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000830 {
831#ifndef _LIBCPP_HAS_NO_ASAN
832 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
833 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
834#endif
835 }
836
Kostya Serebryany497f9122014-09-02 23:43:38 +0000837 void __annotate_new(size_type __current_size) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000838 {
839 __annotate_contiguous_container(data(), data() + capacity(),
840 data() + capacity(), data() + __current_size);
841 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000842 void __annotate_delete() const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000843 {
844 __annotate_contiguous_container(data(), data() + capacity(),
845 data() + size(), data() + capacity());
846 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000847 void __annotate_increase(size_type __n) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000848 {
849 __annotate_contiguous_container(data(), data() + capacity(),
850 data() + size(), data() + size() + __n);
851 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000852 void __annotate_shrink(size_type __old_size) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000853 {
854 __annotate_contiguous_container(data(), data() + capacity(),
855 data() + __old_size, data() + size());
856 }
Marshall Clow26f472d2014-09-03 21:37:43 +0000857#ifndef _LIBCPP_HAS_NO_ASAN
Kostya Serebryany497f9122014-09-02 23:43:38 +0000858 // The annotation for size increase should happen before the actual increase,
859 // but if an exception is thrown after that the annotation has to be undone.
860 struct __RAII_IncreaseAnnotator {
861 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000862 : __commit(false), __v(__v), __old_size(__v.size() + __n) {
Kostya Serebryany497f9122014-09-02 23:43:38 +0000863 __v.__annotate_increase(__n);
864 }
865 void __done() { __commit = true; }
866 ~__RAII_IncreaseAnnotator() {
867 if (__commit) return;
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000868 __v.__annotate_shrink(__old_size);
Kostya Serebryany497f9122014-09-02 23:43:38 +0000869 }
870 bool __commit;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000871 const vector &__v;
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000872 size_type __old_size;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000873 };
Marshall Clow26f472d2014-09-03 21:37:43 +0000874#else
875 struct __RAII_IncreaseAnnotator {
876 inline __RAII_IncreaseAnnotator(const vector &, size_type __n = 1) {}
877 inline void __done() {}
878 };
879#endif
880
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000881};
882
883template <class _Tp, class _Allocator>
884void
885vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
886{
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000887 __annotate_delete();
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000888 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000889 _VSTD::swap(this->__begin_, __v.__begin_);
890 _VSTD::swap(this->__end_, __v.__end_);
891 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000892 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000893 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000894 __invalidate_all_iterators();
895}
896
897template <class _Tp, class _Allocator>
898typename vector<_Tp, _Allocator>::pointer
899vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
900{
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000901 __annotate_delete();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000902 pointer __r = __v.__begin_;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000903 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
904 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000905 _VSTD::swap(this->__begin_, __v.__begin_);
906 _VSTD::swap(this->__end_, __v.__end_);
907 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000908 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000909 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000910 __invalidate_all_iterators();
911 return __r;
912}
913
914// Allocate space for __n objects
915// throws length_error if __n > max_size()
916// throws (probably bad_alloc) if memory run out
917// Precondition: __begin_ == __end_ == __end_cap() == 0
918// Precondition: __n > 0
919// Postcondition: capacity() == __n
920// Postcondition: size() == 0
921template <class _Tp, class _Allocator>
922void
923vector<_Tp, _Allocator>::allocate(size_type __n)
924{
925 if (__n > max_size())
926 this->__throw_length_error();
927 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
928 this->__end_cap() = this->__begin_ + __n;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000929 __annotate_new(0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000930}
931
932template <class _Tp, class _Allocator>
933void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000934vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000935{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000936 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000937 {
938 clear();
939 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000940 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000941 }
942}
943
944template <class _Tp, class _Allocator>
945typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000946vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000947{
Sean Hunt110b8bf2011-07-29 23:31:58 +0000948 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 +0000949}
950
951// Precondition: __new_size > capacity()
952template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000953inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000954typename vector<_Tp, _Allocator>::size_type
955vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
956{
957 const size_type __ms = max_size();
958 if (__new_size > __ms)
959 this->__throw_length_error();
960 const size_type __cap = capacity();
961 if (__cap >= __ms / 2)
962 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58 +0000963 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000964}
965
966// Default constructs __n objects starting at __end_
967// throws if construction throws
968// Precondition: __n > 0
969// Precondition: size() + __n <= capacity()
970// Postcondition: size() == size() + __n
971template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000972void
973vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
974{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000975 allocator_type& __a = this->__alloc();
976 do
977 {
Kostya Serebryany497f9122014-09-02 23:43:38 +0000978 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000979 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000980 ++this->__end_;
981 --__n;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000982 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000983 } while (__n > 0);
984}
985
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000986// Copy constructs __n objects starting at __end_ from __x
987// throws if construction throws
988// Precondition: __n > 0
989// Precondition: size() + __n <= capacity()
990// Postcondition: size() == old size() + __n
991// Postcondition: [i] == __x for all i in [size() - __n, __n)
992template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000993inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000994void
995vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
996{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000997 allocator_type& __a = this->__alloc();
998 do
999 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001000 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001001 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001002 ++this->__end_;
1003 --__n;
Kostya Serebryany497f9122014-09-02 23:43:38 +00001004 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001005 } while (__n > 0);
1006}
1007
1008template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001009template <class _ForwardIterator>
1010typename enable_if
1011<
1012 __is_forward_iterator<_ForwardIterator>::value,
1013 void
1014>::type
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001015vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001016{
1017 allocator_type& __a = this->__alloc();
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001018 __RAII_IncreaseAnnotator __annotator(*this, __n);
1019 __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
1020 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001021}
1022
1023// Default constructs __n objects starting at __end_
1024// throws if construction throws
1025// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001026// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001027template <class _Tp, class _Allocator>
1028void
1029vector<_Tp, _Allocator>::__append(size_type __n)
1030{
1031 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1032 this->__construct_at_end(__n);
1033 else
1034 {
1035 allocator_type& __a = this->__alloc();
1036 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1037 __v.__construct_at_end(__n);
1038 __swap_out_circular_buffer(__v);
1039 }
1040}
1041
1042// Default constructs __n objects starting at __end_
1043// throws if construction throws
1044// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001045// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001046template <class _Tp, class _Allocator>
1047void
1048vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1049{
1050 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1051 this->__construct_at_end(__n, __x);
1052 else
1053 {
1054 allocator_type& __a = this->__alloc();
1055 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1056 __v.__construct_at_end(__n, __x);
1057 __swap_out_circular_buffer(__v);
1058 }
1059}
1060
1061template <class _Tp, class _Allocator>
1062vector<_Tp, _Allocator>::vector(size_type __n)
1063{
Howard Hinnant0442b122011-09-16 18:41:29 +00001064#if _LIBCPP_DEBUG_LEVEL >= 2
1065 __get_db()->__insert_c(this);
1066#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067 if (__n > 0)
1068 {
1069 allocate(__n);
1070 __construct_at_end(__n);
1071 }
1072}
1073
Marshall Clowa49a2c92013-09-14 00:47:59 +00001074#if _LIBCPP_STD_VER > 11
1075template <class _Tp, class _Allocator>
1076vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1077 : __base(__a)
1078{
1079#if _LIBCPP_DEBUG_LEVEL >= 2
1080 __get_db()->__insert_c(this);
1081#endif
1082 if (__n > 0)
1083 {
1084 allocate(__n);
1085 __construct_at_end(__n);
1086 }
1087}
1088#endif
1089
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001090template <class _Tp, class _Allocator>
1091vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1092{
Howard Hinnant0442b122011-09-16 18:41:29 +00001093#if _LIBCPP_DEBUG_LEVEL >= 2
1094 __get_db()->__insert_c(this);
1095#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001096 if (__n > 0)
1097 {
1098 allocate(__n);
1099 __construct_at_end(__n, __x);
1100 }
1101}
1102
1103template <class _Tp, class _Allocator>
1104vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1105 : __base(__a)
1106{
Howard Hinnant0442b122011-09-16 18:41:29 +00001107#if _LIBCPP_DEBUG_LEVEL >= 2
1108 __get_db()->__insert_c(this);
1109#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001110 if (__n > 0)
1111 {
1112 allocate(__n);
1113 __construct_at_end(__n, __x);
1114 }
1115}
1116
1117template <class _Tp, class _Allocator>
1118template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +00001119vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001120 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001121 !__is_forward_iterator<_InputIterator>::value &&
1122 is_constructible<
1123 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +00001124 typename iterator_traits<_InputIterator>::reference>::value,
1125 _InputIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001126{
Howard Hinnantabe26282011-09-16 17:29:17 +00001127#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001128 __get_db()->__insert_c(this);
1129#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001130 for (; __first != __last; ++__first)
1131 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001132}
1133
1134template <class _Tp, class _Allocator>
1135template <class _InputIterator>
1136vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1137 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001138 !__is_forward_iterator<_InputIterator>::value &&
1139 is_constructible<
1140 value_type,
1141 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001142 : __base(__a)
1143{
Howard Hinnantabe26282011-09-16 17:29:17 +00001144#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001145 __get_db()->__insert_c(this);
1146#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001147 for (; __first != __last; ++__first)
1148 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001149}
1150
1151template <class _Tp, class _Allocator>
1152template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +00001153vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001154 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1155 is_constructible<
1156 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +00001157 typename iterator_traits<_ForwardIterator>::reference>::value,
1158 _ForwardIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001159{
Howard Hinnant0442b122011-09-16 18:41:29 +00001160#if _LIBCPP_DEBUG_LEVEL >= 2
1161 __get_db()->__insert_c(this);
1162#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001163 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001164 if (__n > 0)
1165 {
1166 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001167 __construct_at_end(__first, __last, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001168 }
1169}
1170
1171template <class _Tp, class _Allocator>
1172template <class _ForwardIterator>
1173vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001174 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1175 is_constructible<
1176 value_type,
1177 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001178 : __base(__a)
1179{
Howard Hinnant0442b122011-09-16 18:41:29 +00001180#if _LIBCPP_DEBUG_LEVEL >= 2
1181 __get_db()->__insert_c(this);
1182#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001183 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001184 if (__n > 0)
1185 {
1186 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001187 __construct_at_end(__first, __last, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001188 }
1189}
1190
1191template <class _Tp, class _Allocator>
1192vector<_Tp, _Allocator>::vector(const vector& __x)
1193 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1194{
Howard Hinnant0442b122011-09-16 18:41:29 +00001195#if _LIBCPP_DEBUG_LEVEL >= 2
1196 __get_db()->__insert_c(this);
1197#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001198 size_type __n = __x.size();
1199 if (__n > 0)
1200 {
1201 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001202 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001203 }
1204}
1205
1206template <class _Tp, class _Allocator>
1207vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1208 : __base(__a)
1209{
Howard Hinnant0442b122011-09-16 18:41:29 +00001210#if _LIBCPP_DEBUG_LEVEL >= 2
1211 __get_db()->__insert_c(this);
1212#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001213 size_type __n = __x.size();
1214 if (__n > 0)
1215 {
1216 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001217 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001218 }
1219}
1220
Howard Hinnant73d21a42010-09-04 23:28:19 +00001221#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001222
1223template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001224inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001225vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clow119ed472015-07-14 14:46:32 +00001226#if _LIBCPP_STD_VER > 14
1227 _NOEXCEPT
1228#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001229 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow119ed472015-07-14 14:46:32 +00001230#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001231 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001232{
Howard Hinnantabe26282011-09-16 17:29:17 +00001233#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001234 __get_db()->__insert_c(this);
Howard Hinnante6125bd2011-09-19 16:34:29 +00001235 __get_db()->swap(this, &__x);
Howard Hinnant7a563db2011-09-14 18:33:51 +00001236#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001237 this->__begin_ = __x.__begin_;
1238 this->__end_ = __x.__end_;
1239 this->__end_cap() = __x.__end_cap();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001240 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001241}
1242
1243template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001244inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1246 : __base(__a)
1247{
Howard Hinnant0442b122011-09-16 18:41:29 +00001248#if _LIBCPP_DEBUG_LEVEL >= 2
1249 __get_db()->__insert_c(this);
1250#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001251 if (__a == __x.__alloc())
1252 {
1253 this->__begin_ = __x.__begin_;
1254 this->__end_ = __x.__end_;
1255 this->__end_cap() = __x.__end_cap();
1256 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001257#if _LIBCPP_DEBUG_LEVEL >= 2
1258 __get_db()->swap(this, &__x);
1259#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001260 }
1261 else
1262 {
Howard Hinnant99968442011-11-29 18:15:50 +00001263 typedef move_iterator<iterator> _Ip;
1264 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001265 }
1266}
1267
Howard Hinnante3e32912011-08-12 21:56:02 +00001268#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1269
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001270template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001271inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001272vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1273{
Howard Hinnant0442b122011-09-16 18:41:29 +00001274#if _LIBCPP_DEBUG_LEVEL >= 2
1275 __get_db()->__insert_c(this);
1276#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001277 if (__il.size() > 0)
1278 {
1279 allocate(__il.size());
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001280 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001281 }
1282}
1283
1284template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001285inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001286vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1287 : __base(__a)
1288{
Howard Hinnant0442b122011-09-16 18:41:29 +00001289#if _LIBCPP_DEBUG_LEVEL >= 2
1290 __get_db()->__insert_c(this);
1291#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001292 if (__il.size() > 0)
1293 {
1294 allocate(__il.size());
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001295 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001296 }
1297}
1298
Howard Hinnante3e32912011-08-12 21:56:02 +00001299#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1300
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001301template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001302inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001303vector<_Tp, _Allocator>&
1304vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001305 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001306{
1307 __move_assign(__x, integral_constant<bool,
1308 __alloc_traits::propagate_on_container_move_assignment::value>());
1309 return *this;
1310}
1311
1312template <class _Tp, class _Allocator>
1313void
1314vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001315 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001316{
1317 if (__base::__alloc() != __c.__alloc())
1318 {
Howard Hinnant99968442011-11-29 18:15:50 +00001319 typedef move_iterator<iterator> _Ip;
1320 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001321 }
1322 else
1323 __move_assign(__c, true_type());
1324}
1325
1326template <class _Tp, class _Allocator>
1327void
1328vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001329 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001330{
1331 deallocate();
Marshall Clow3c2eac62014-07-21 15:11:13 +00001332 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001333 this->__begin_ = __c.__begin_;
1334 this->__end_ = __c.__end_;
1335 this->__end_cap() = __c.__end_cap();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001336 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001337#if _LIBCPP_DEBUG_LEVEL >= 2
1338 __get_db()->swap(this, &__c);
1339#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001340}
1341
Howard Hinnant73d21a42010-09-04 23:28:19 +00001342#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001343
1344template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001345inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001346vector<_Tp, _Allocator>&
1347vector<_Tp, _Allocator>::operator=(const vector& __x)
1348{
1349 if (this != &__x)
1350 {
1351 __base::__copy_assign_alloc(__x);
1352 assign(__x.__begin_, __x.__end_);
1353 }
1354 return *this;
1355}
1356
1357template <class _Tp, class _Allocator>
1358template <class _InputIterator>
1359typename enable_if
1360<
1361 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001362 !__is_forward_iterator<_InputIterator>::value &&
1363 is_constructible<
1364 _Tp,
1365 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001366 void
1367>::type
1368vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1369{
1370 clear();
1371 for (; __first != __last; ++__first)
1372 push_back(*__first);
1373}
1374
1375template <class _Tp, class _Allocator>
1376template <class _ForwardIterator>
1377typename enable_if
1378<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001379 __is_forward_iterator<_ForwardIterator>::value &&
1380 is_constructible<
1381 _Tp,
1382 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001383 void
1384>::type
1385vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1386{
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001387 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1388 if (__new_size <= capacity())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001389 {
1390 _ForwardIterator __mid = __last;
1391 bool __growing = false;
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001392 if (__new_size > size())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001393 {
1394 __growing = true;
1395 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001396 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001397 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001398 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001399 if (__growing)
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001400 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001401 else
1402 this->__destruct_at_end(__m);
1403 }
1404 else
1405 {
1406 deallocate();
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001407 allocate(__recommend(__new_size));
1408 __construct_at_end(__first, __last, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001409 }
1410}
1411
1412template <class _Tp, class _Allocator>
1413void
1414vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1415{
1416 if (__n <= capacity())
1417 {
1418 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001419 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001420 if (__n > __s)
1421 __construct_at_end(__n - __s, __u);
1422 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001423 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001424 }
1425 else
1426 {
1427 deallocate();
1428 allocate(__recommend(static_cast<size_type>(__n)));
1429 __construct_at_end(__n, __u);
1430 }
1431}
1432
Howard Hinnant324bb032010-08-22 00:02:43 +00001433template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001434inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001435typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001436vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001437{
Howard Hinnantabe26282011-09-16 17:29:17 +00001438#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001439 return iterator(this, __p);
1440#else
1441 return iterator(__p);
1442#endif
1443}
1444
Howard Hinnant324bb032010-08-22 00:02:43 +00001445template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001446inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001447typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001448vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001449{
Howard Hinnantabe26282011-09-16 17:29:17 +00001450#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001451 return const_iterator(this, __p);
1452#else
1453 return const_iterator(__p);
1454#endif
1455}
1456
Howard Hinnant324bb032010-08-22 00:02:43 +00001457template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001458inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001459typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001460vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001461{
1462 return __make_iter(this->__begin_);
1463}
1464
Howard Hinnant324bb032010-08-22 00:02:43 +00001465template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001466inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001467typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001468vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469{
1470 return __make_iter(this->__begin_);
1471}
1472
Howard Hinnant324bb032010-08-22 00:02:43 +00001473template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001474inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001475typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001476vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001477{
1478 return __make_iter(this->__end_);
1479}
1480
Howard Hinnant324bb032010-08-22 00:02:43 +00001481template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001482inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001483typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001484vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001485{
1486 return __make_iter(this->__end_);
1487}
1488
Howard Hinnant324bb032010-08-22 00:02:43 +00001489template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001490inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491typename vector<_Tp, _Allocator>::reference
1492vector<_Tp, _Allocator>::operator[](size_type __n)
1493{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001494 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001495 return this->__begin_[__n];
1496}
1497
Howard Hinnant324bb032010-08-22 00:02:43 +00001498template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001499inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001500typename vector<_Tp, _Allocator>::const_reference
1501vector<_Tp, _Allocator>::operator[](size_type __n) const
1502{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001503 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504 return this->__begin_[__n];
1505}
1506
Howard Hinnant324bb032010-08-22 00:02:43 +00001507template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001508typename vector<_Tp, _Allocator>::reference
1509vector<_Tp, _Allocator>::at(size_type __n)
1510{
1511 if (__n >= size())
1512 this->__throw_out_of_range();
1513 return this->__begin_[__n];
1514}
1515
Howard Hinnant324bb032010-08-22 00:02:43 +00001516template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001517typename vector<_Tp, _Allocator>::const_reference
1518vector<_Tp, _Allocator>::at(size_type __n) const
1519{
1520 if (__n >= size())
1521 this->__throw_out_of_range();
1522 return this->__begin_[__n];
1523}
1524
1525template <class _Tp, class _Allocator>
1526void
1527vector<_Tp, _Allocator>::reserve(size_type __n)
1528{
1529 if (__n > capacity())
1530 {
1531 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001532 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001533 __swap_out_circular_buffer(__v);
1534 }
1535}
1536
1537template <class _Tp, class _Allocator>
1538void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001539vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001540{
1541 if (capacity() > size())
1542 {
1543#ifndef _LIBCPP_NO_EXCEPTIONS
1544 try
1545 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001546#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001547 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001548 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001549 __swap_out_circular_buffer(__v);
1550#ifndef _LIBCPP_NO_EXCEPTIONS
1551 }
1552 catch (...)
1553 {
1554 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001555#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001556 }
1557}
1558
1559template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001560template <class _Up>
1561void
1562#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1563vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1564#else
1565vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1566#endif
1567{
1568 allocator_type& __a = this->__alloc();
1569 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1570 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnantf619e232013-01-11 20:36:59 +00001571 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1572 __v.__end_++;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001573 __swap_out_circular_buffer(__v);
1574}
1575
1576template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001577inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001578void
1579vector<_Tp, _Allocator>::push_back(const_reference __x)
1580{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001581 if (this->__end_ != this->__end_cap())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001582 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001583 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001584 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001585 _VSTD::__to_raw_pointer(this->__end_), __x);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001586 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001587 ++this->__end_;
1588 }
1589 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001590 __push_back_slow_path(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001591}
1592
Howard Hinnant73d21a42010-09-04 23:28:19 +00001593#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001594
1595template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001596inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597void
1598vector<_Tp, _Allocator>::push_back(value_type&& __x)
1599{
1600 if (this->__end_ < this->__end_cap())
1601 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001602 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001603 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001604 _VSTD::__to_raw_pointer(this->__end_),
1605 _VSTD::move(__x));
Kostya Serebryany497f9122014-09-02 23:43:38 +00001606 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001607 ++this->__end_;
1608 }
1609 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001610 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611}
1612
Howard Hinnant73d21a42010-09-04 23:28:19 +00001613#ifndef _LIBCPP_HAS_NO_VARIADICS
1614
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001615template <class _Tp, class _Allocator>
1616template <class... _Args>
1617void
Howard Hinnant0438ea22012-02-26 15:30:12 +00001618vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1619{
1620 allocator_type& __a = this->__alloc();
1621 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1622// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantf619e232013-01-11 20:36:59 +00001623 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1624 __v.__end_++;
Howard Hinnant0438ea22012-02-26 15:30:12 +00001625 __swap_out_circular_buffer(__v);
1626}
1627
1628template <class _Tp, class _Allocator>
1629template <class... _Args>
Howard Hinnant1e564242013-10-04 22:09:00 +00001630inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0438ea22012-02-26 15:30:12 +00001631void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001632vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1633{
1634 if (this->__end_ < this->__end_cap())
1635 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001636 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001637 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001638 _VSTD::__to_raw_pointer(this->__end_),
1639 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001640 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001641 ++this->__end_;
1642 }
1643 else
Howard Hinnant0438ea22012-02-26 15:30:12 +00001644 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001645}
1646
Howard Hinnant73d21a42010-09-04 23:28:19 +00001647#endif // _LIBCPP_HAS_NO_VARIADICS
1648#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001649
1650template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001651inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001652void
1653vector<_Tp, _Allocator>::pop_back()
1654{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001655 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001656 this->__destruct_at_end(this->__end_ - 1);
1657}
1658
1659template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001660inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001661typename vector<_Tp, _Allocator>::iterator
1662vector<_Tp, _Allocator>::erase(const_iterator __position)
1663{
Howard Hinnantabe26282011-09-16 17:29:17 +00001664#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001665 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1666 "vector::erase(iterator) called with an iterator not"
1667 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001668#endif
Howard Hinnant782da332013-03-25 22:12:26 +00001669 _LIBCPP_ASSERT(__position != end(),
1670 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001671 difference_type __ps = __position - cbegin();
1672 pointer __p = this->__begin_ + __ps;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001673 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001674 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001675 return __r;
1676}
1677
1678template <class _Tp, class _Allocator>
1679typename vector<_Tp, _Allocator>::iterator
1680vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1681{
Howard Hinnantabe26282011-09-16 17:29:17 +00001682#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001683 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1684 "vector::erase(iterator, iterator) called with an iterator not"
1685 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001686#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +00001687 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001688 pointer __p = this->__begin_ + (__first - begin());
1689 iterator __r = __make_iter(__p);
Howard Hinnantb4e67cf2013-04-18 15:02:57 +00001690 if (__first != __last)
1691 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001692 return __r;
1693}
1694
1695template <class _Tp, class _Allocator>
1696void
1697vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1698{
1699 pointer __old_last = this->__end_;
1700 difference_type __n = __old_last - __to;
1701 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1702 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001703 _VSTD::__to_raw_pointer(this->__end_),
1704 _VSTD::move(*__i));
1705 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001706}
1707
1708template <class _Tp, class _Allocator>
1709typename vector<_Tp, _Allocator>::iterator
1710vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1711{
Howard Hinnantabe26282011-09-16 17:29:17 +00001712#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001713 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1714 "vector::insert(iterator, x) called with an iterator not"
1715 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001716#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001717 pointer __p = this->__begin_ + (__position - begin());
1718 if (this->__end_ < this->__end_cap())
1719 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001720 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001721 if (__p == this->__end_)
1722 {
1723 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001724 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001725 ++this->__end_;
1726 }
1727 else
1728 {
1729 __move_range(__p, this->__end_, __p + 1);
1730 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1731 if (__p <= __xr && __xr < this->__end_)
1732 ++__xr;
1733 *__p = *__xr;
1734 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001735 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001736 }
1737 else
1738 {
1739 allocator_type& __a = this->__alloc();
1740 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1741 __v.push_back(__x);
1742 __p = __swap_out_circular_buffer(__v, __p);
1743 }
1744 return __make_iter(__p);
1745}
1746
Howard Hinnant73d21a42010-09-04 23:28:19 +00001747#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001748
1749template <class _Tp, class _Allocator>
1750typename vector<_Tp, _Allocator>::iterator
1751vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1752{
Howard Hinnantabe26282011-09-16 17:29:17 +00001753#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001754 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1755 "vector::insert(iterator, x) called with an iterator not"
1756 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001757#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001758 pointer __p = this->__begin_ + (__position - begin());
1759 if (this->__end_ < this->__end_cap())
1760 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001761 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001762 if (__p == this->__end_)
1763 {
1764 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001765 _VSTD::__to_raw_pointer(this->__end_),
1766 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001767 ++this->__end_;
1768 }
1769 else
1770 {
1771 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001772 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001773 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001774 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001775 }
1776 else
1777 {
1778 allocator_type& __a = this->__alloc();
1779 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001780 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001781 __p = __swap_out_circular_buffer(__v, __p);
1782 }
1783 return __make_iter(__p);
1784}
1785
Howard Hinnant73d21a42010-09-04 23:28:19 +00001786#ifndef _LIBCPP_HAS_NO_VARIADICS
1787
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001788template <class _Tp, class _Allocator>
1789template <class... _Args>
1790typename vector<_Tp, _Allocator>::iterator
1791vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1792{
Howard Hinnantabe26282011-09-16 17:29:17 +00001793#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001794 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1795 "vector::emplace(iterator, x) called with an iterator not"
1796 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001797#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001798 pointer __p = this->__begin_ + (__position - begin());
1799 if (this->__end_ < this->__end_cap())
1800 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001801 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001802 if (__p == this->__end_)
1803 {
1804 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001805 _VSTD::__to_raw_pointer(this->__end_),
1806 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001807 ++this->__end_;
1808 }
1809 else
1810 {
Howard Hinnanta58402a2012-07-08 23:23:04 +00001811 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001812 __move_range(__p, this->__end_, __p + 1);
Howard Hinnanta58402a2012-07-08 23:23:04 +00001813 *__p = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001814 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001815 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001816 }
1817 else
1818 {
1819 allocator_type& __a = this->__alloc();
1820 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001821 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001822 __p = __swap_out_circular_buffer(__v, __p);
1823 }
1824 return __make_iter(__p);
1825}
1826
Howard Hinnant73d21a42010-09-04 23:28:19 +00001827#endif // _LIBCPP_HAS_NO_VARIADICS
1828#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829
1830template <class _Tp, class _Allocator>
1831typename vector<_Tp, _Allocator>::iterator
1832vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1833{
Howard Hinnantabe26282011-09-16 17:29:17 +00001834#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001835 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1836 "vector::insert(iterator, n, x) called with an iterator not"
1837 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001838#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001839 pointer __p = this->__begin_ + (__position - begin());
1840 if (__n > 0)
1841 {
1842 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1843 {
1844 size_type __old_n = __n;
1845 pointer __old_last = this->__end_;
1846 if (__n > static_cast<size_type>(this->__end_ - __p))
1847 {
1848 size_type __cx = __n - (this->__end_ - __p);
1849 __construct_at_end(__cx, __x);
1850 __n -= __cx;
1851 }
1852 if (__n > 0)
1853 {
Eric Fiselierd7590952014-11-14 18:28:36 +00001854 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001855 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001856 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001857 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1858 if (__p <= __xr && __xr < this->__end_)
1859 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001860 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001861 }
1862 }
1863 else
1864 {
1865 allocator_type& __a = this->__alloc();
1866 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1867 __v.__construct_at_end(__n, __x);
1868 __p = __swap_out_circular_buffer(__v, __p);
1869 }
1870 }
1871 return __make_iter(__p);
1872}
1873
1874template <class _Tp, class _Allocator>
1875template <class _InputIterator>
1876typename enable_if
1877<
1878 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001879 !__is_forward_iterator<_InputIterator>::value &&
1880 is_constructible<
1881 _Tp,
1882 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001883 typename vector<_Tp, _Allocator>::iterator
1884>::type
1885vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1886{
Howard Hinnantabe26282011-09-16 17:29:17 +00001887#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001888 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1889 "vector::insert(iterator, range) called with an iterator not"
1890 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001891#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001892 difference_type __off = __position - begin();
1893 pointer __p = this->__begin_ + __off;
1894 allocator_type& __a = this->__alloc();
1895 pointer __old_last = this->__end_;
1896 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1897 {
Eric Fiselier7d439a42015-07-18 18:22:12 +00001898 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001899 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001900 *__first);
1901 ++this->__end_;
Eric Fiselier7d439a42015-07-18 18:22:12 +00001902 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903 }
1904 __split_buffer<value_type, allocator_type&> __v(__a);
1905 if (__first != __last)
1906 {
1907#ifndef _LIBCPP_NO_EXCEPTIONS
1908 try
1909 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001910#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001911 __v.__construct_at_end(__first, __last);
1912 difference_type __old_size = __old_last - this->__begin_;
1913 difference_type __old_p = __p - this->__begin_;
1914 reserve(__recommend(size() + __v.size()));
1915 __p = this->__begin_ + __old_p;
1916 __old_last = this->__begin_ + __old_size;
1917#ifndef _LIBCPP_NO_EXCEPTIONS
1918 }
1919 catch (...)
1920 {
1921 erase(__make_iter(__old_last), end());
1922 throw;
1923 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001924#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001925 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001926 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant0442b122011-09-16 18:41:29 +00001927 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1928 make_move_iterator(__v.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001929 return begin() + __off;
1930}
1931
1932template <class _Tp, class _Allocator>
1933template <class _ForwardIterator>
1934typename enable_if
1935<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001936 __is_forward_iterator<_ForwardIterator>::value &&
1937 is_constructible<
1938 _Tp,
1939 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001940 typename vector<_Tp, _Allocator>::iterator
1941>::type
1942vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1943{
Howard Hinnantabe26282011-09-16 17:29:17 +00001944#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001945 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1946 "vector::insert(iterator, range) called with an iterator not"
1947 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001948#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001949 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00001950 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001951 if (__n > 0)
1952 {
1953 if (__n <= this->__end_cap() - this->__end_)
1954 {
1955 size_type __old_n = __n;
1956 pointer __old_last = this->__end_;
1957 _ForwardIterator __m = __last;
1958 difference_type __dx = this->__end_ - __p;
1959 if (__n > __dx)
1960 {
1961 __m = __first;
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001962 difference_type __diff = this->__end_ - __p;
1963 _VSTD::advance(__m, __diff);
1964 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001965 __n = __dx;
1966 }
1967 if (__n > 0)
1968 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001969 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001970 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001971 __annotator.__done();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001972 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001973 }
1974 }
1975 else
1976 {
1977 allocator_type& __a = this->__alloc();
1978 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1979 __v.__construct_at_end(__first, __last);
1980 __p = __swap_out_circular_buffer(__v, __p);
1981 }
1982 }
1983 return __make_iter(__p);
1984}
1985
1986template <class _Tp, class _Allocator>
1987void
1988vector<_Tp, _Allocator>::resize(size_type __sz)
1989{
1990 size_type __cs = size();
1991 if (__cs < __sz)
1992 this->__append(__sz - __cs);
1993 else if (__cs > __sz)
1994 this->__destruct_at_end(this->__begin_ + __sz);
1995}
1996
1997template <class _Tp, class _Allocator>
1998void
1999vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2000{
2001 size_type __cs = size();
2002 if (__cs < __sz)
2003 this->__append(__sz - __cs, __x);
2004 else if (__cs > __sz)
2005 this->__destruct_at_end(this->__begin_ + __sz);
2006}
2007
2008template <class _Tp, class _Allocator>
2009void
2010vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow7d914d12015-07-13 20:04:56 +00002011#if _LIBCPP_STD_VER >= 14
2012 _NOEXCEPT
2013#else
2014 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2015 __is_nothrow_swappable<allocator_type>::value)
2016#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002017{
Howard Hinnant7a563db2011-09-14 18:33:51 +00002018 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2019 this->__alloc() == __x.__alloc(),
2020 "vector::swap: Either propagate_on_container_swap must be true"
2021 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +00002022 _VSTD::swap(this->__begin_, __x.__begin_);
2023 _VSTD::swap(this->__end_, __x.__end_);
2024 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Marshall Clow7d914d12015-07-13 20:04:56 +00002025 __swap_allocator(this->__alloc(), __x.__alloc(),
2026 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantabe26282011-09-16 17:29:17 +00002027#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002028 __get_db()->swap(this, &__x);
Howard Hinnantabe26282011-09-16 17:29:17 +00002029#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002030}
2031
Howard Hinnant324bb032010-08-22 00:02:43 +00002032template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002033bool
2034vector<_Tp, _Allocator>::__invariants() const
2035{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002036 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002037 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002038 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002039 return false;
2040 }
2041 else
2042 {
2043 if (this->__begin_ > this->__end_)
2044 return false;
2045 if (this->__begin_ == this->__end_cap())
2046 return false;
2047 if (this->__end_ > this->__end_cap())
2048 return false;
2049 }
2050 return true;
2051}
2052
Howard Hinnantabe26282011-09-16 17:29:17 +00002053#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002054
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002055template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:51 +00002056bool
2057vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2058{
2059 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2060}
2061
2062template <class _Tp, class _Allocator>
2063bool
2064vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2065{
2066 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2067}
2068
2069template <class _Tp, class _Allocator>
2070bool
2071vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2072{
2073 const_pointer __p = __i->base() + __n;
2074 return this->__begin_ <= __p && __p <= this->__end_;
2075}
2076
2077template <class _Tp, class _Allocator>
2078bool
2079vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2080{
2081 const_pointer __p = __i->base() + __n;
2082 return this->__begin_ <= __p && __p < this->__end_;
2083}
2084
Howard Hinnantabe26282011-09-16 17:29:17 +00002085#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002086
2087template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002088inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002089void
2090vector<_Tp, _Allocator>::__invalidate_all_iterators()
2091{
Howard Hinnantabe26282011-09-16 17:29:17 +00002092#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002093 __get_db()->__invalidate_all(this);
Howard Hinnantabe26282011-09-16 17:29:17 +00002094#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002095}
2096
2097// vector<bool>
2098
2099template <class _Allocator> class vector<bool, _Allocator>;
2100
2101template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2102
2103template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002104struct __has_storage_type<vector<bool, _Allocator> >
2105{
2106 static const bool value = true;
2107};
2108
2109template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002110class _LIBCPP_TYPE_VIS_ONLY vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002111 : private __vector_base_common<true>
2112{
2113public:
2114 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00002115 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002116 typedef _Allocator allocator_type;
2117 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002118 typedef typename __alloc_traits::size_type size_type;
2119 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantf867f632012-05-07 16:50:38 +00002120 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002121 typedef __bit_iterator<vector, false> pointer;
2122 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002123 typedef pointer iterator;
2124 typedef const_pointer const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002125 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2126 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002127
2128private:
Marshall Clow66302c62015-04-07 05:21:38 +00002129 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130 typedef allocator_traits<__storage_allocator> __storage_traits;
2131 typedef typename __storage_traits::pointer __storage_pointer;
2132 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2133
2134 __storage_pointer __begin_;
2135 size_type __size_;
2136 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002137public:
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002138 typedef __bit_reference<vector> reference;
2139 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002140private:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002141 _LIBCPP_INLINE_VISIBILITY
2142 size_type& __cap() _NOEXCEPT
2143 {return __cap_alloc_.first();}
2144 _LIBCPP_INLINE_VISIBILITY
2145 const size_type& __cap() const _NOEXCEPT
2146 {return __cap_alloc_.first();}
2147 _LIBCPP_INLINE_VISIBILITY
2148 __storage_allocator& __alloc() _NOEXCEPT
2149 {return __cap_alloc_.second();}
2150 _LIBCPP_INLINE_VISIBILITY
2151 const __storage_allocator& __alloc() const _NOEXCEPT
2152 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002153
2154 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2155
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002156 _LIBCPP_INLINE_VISIBILITY
2157 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002158 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002159 _LIBCPP_INLINE_VISIBILITY
2160 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002161 {return (__n - 1) / __bits_per_word + 1;}
2162
2163public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002164 _LIBCPP_INLINE_VISIBILITY
Marshall Clowc912c0c2015-06-04 02:05:41 +00002165 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow127db912015-06-04 00:10:20 +00002166
2167 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2168#if _LIBCPP_STD_VER <= 14
2169 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2170#else
2171 _NOEXCEPT;
2172#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002173 ~vector();
2174 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59 +00002175#if _LIBCPP_STD_VER > 11
2176 explicit vector(size_type __n, const allocator_type& __a);
2177#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002178 vector(size_type __n, const value_type& __v);
2179 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2180 template <class _InputIterator>
2181 vector(_InputIterator __first, _InputIterator __last,
2182 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2183 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2184 template <class _InputIterator>
2185 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2186 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2187 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2188 template <class _ForwardIterator>
2189 vector(_ForwardIterator __first, _ForwardIterator __last,
2190 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2191 template <class _ForwardIterator>
2192 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2193 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2194
2195 vector(const vector& __v);
2196 vector(const vector& __v, const allocator_type& __a);
2197 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00002198#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002199 vector(initializer_list<value_type> __il);
2200 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00002201#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002202
Howard Hinnant73d21a42010-09-04 23:28:19 +00002203#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002204 _LIBCPP_INLINE_VISIBILITY
2205 vector(vector&& __v)
Marshall Clow119ed472015-07-14 14:46:32 +00002206#if _LIBCPP_STD_VER > 14
2207 _NOEXCEPT;
2208#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002209 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow119ed472015-07-14 14:46:32 +00002210#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002211 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002212 _LIBCPP_INLINE_VISIBILITY
2213 vector& operator=(vector&& __v)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002214 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnant73d21a42010-09-04 23:28:19 +00002215#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +00002216#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002217 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002218 vector& operator=(initializer_list<value_type> __il)
2219 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00002220#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002221
2222 template <class _InputIterator>
2223 typename enable_if
2224 <
2225 __is_input_iterator<_InputIterator>::value &&
2226 !__is_forward_iterator<_InputIterator>::value,
2227 void
2228 >::type
2229 assign(_InputIterator __first, _InputIterator __last);
2230 template <class _ForwardIterator>
2231 typename enable_if
2232 <
2233 __is_forward_iterator<_ForwardIterator>::value,
2234 void
2235 >::type
2236 assign(_ForwardIterator __first, _ForwardIterator __last);
2237
2238 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +00002239#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002241 void assign(initializer_list<value_type> __il)
2242 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002243#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002244
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002245 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002246 {return allocator_type(this->__alloc());}
2247
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002248 size_type max_size() const _NOEXCEPT;
2249 _LIBCPP_INLINE_VISIBILITY
2250 size_type capacity() const _NOEXCEPT
2251 {return __internal_cap_to_external(__cap());}
2252 _LIBCPP_INLINE_VISIBILITY
2253 size_type size() const _NOEXCEPT
2254 {return __size_;}
2255 _LIBCPP_INLINE_VISIBILITY
2256 bool empty() const _NOEXCEPT
2257 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002258 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002259 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002260
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002261 _LIBCPP_INLINE_VISIBILITY
2262 iterator begin() _NOEXCEPT
2263 {return __make_iter(0);}
2264 _LIBCPP_INLINE_VISIBILITY
2265 const_iterator begin() const _NOEXCEPT
2266 {return __make_iter(0);}
2267 _LIBCPP_INLINE_VISIBILITY
2268 iterator end() _NOEXCEPT
2269 {return __make_iter(__size_);}
2270 _LIBCPP_INLINE_VISIBILITY
2271 const_iterator end() const _NOEXCEPT
2272 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002273
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002274 _LIBCPP_INLINE_VISIBILITY
2275 reverse_iterator rbegin() _NOEXCEPT
2276 {return reverse_iterator(end());}
2277 _LIBCPP_INLINE_VISIBILITY
2278 const_reverse_iterator rbegin() const _NOEXCEPT
2279 {return const_reverse_iterator(end());}
2280 _LIBCPP_INLINE_VISIBILITY
2281 reverse_iterator rend() _NOEXCEPT
2282 {return reverse_iterator(begin());}
2283 _LIBCPP_INLINE_VISIBILITY
2284 const_reverse_iterator rend() const _NOEXCEPT
2285 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002286
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002287 _LIBCPP_INLINE_VISIBILITY
2288 const_iterator cbegin() const _NOEXCEPT
2289 {return __make_iter(0);}
2290 _LIBCPP_INLINE_VISIBILITY
2291 const_iterator cend() const _NOEXCEPT
2292 {return __make_iter(__size_);}
2293 _LIBCPP_INLINE_VISIBILITY
2294 const_reverse_iterator crbegin() const _NOEXCEPT
2295 {return rbegin();}
2296 _LIBCPP_INLINE_VISIBILITY
2297 const_reverse_iterator crend() const _NOEXCEPT
2298 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002299
2300 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2301 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2302 reference at(size_type __n);
2303 const_reference at(size_type __n) const;
2304
2305 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2306 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2307 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2308 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2309
2310 void push_back(const value_type& __x);
Marshall Clow198a2a52013-08-13 23:54:12 +00002311#if _LIBCPP_STD_VER > 11
2312 template <class... _Args>
2313 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2314 { push_back ( value_type ( _VSTD::forward<_Args>(__args)... )); }
2315#endif
2316
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002317 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2318
Marshall Clow198a2a52013-08-13 23:54:12 +00002319#if _LIBCPP_STD_VER > 11
2320 template <class... _Args>
2321 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2322 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2323#endif
2324
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002325 iterator insert(const_iterator __position, const value_type& __x);
2326 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2327 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2328 template <class _InputIterator>
2329 typename enable_if
2330 <
2331 __is_input_iterator <_InputIterator>::value &&
2332 !__is_forward_iterator<_InputIterator>::value,
2333 iterator
2334 >::type
2335 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2336 template <class _ForwardIterator>
2337 typename enable_if
2338 <
2339 __is_forward_iterator<_ForwardIterator>::value,
2340 iterator
2341 >::type
2342 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00002343#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002344 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002345 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2346 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002347#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002348
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002349 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002350 iterator erase(const_iterator __first, const_iterator __last);
2351
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002352 _LIBCPP_INLINE_VISIBILITY
2353 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002354
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002355 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +00002356#if _LIBCPP_STD_VER >= 14
2357 _NOEXCEPT;
2358#else
2359 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2360 __is_nothrow_swappable<allocator_type>::value);
2361#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002362
2363 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002364 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002365
2366 bool __invariants() const;
2367
2368private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002369 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002370 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002371 void deallocate() _NOEXCEPT;
2372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00002373 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow0d1965d2014-07-28 15:02:42 +00002374 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002375 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2376 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002377 template <class _ForwardIterator>
2378 typename enable_if
2379 <
2380 __is_forward_iterator<_ForwardIterator>::value,
2381 void
2382 >::type
2383 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2384 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002385 _LIBCPP_INLINE_VISIBILITY
2386 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002387 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002388 _LIBCPP_INLINE_VISIBILITY
2389 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002390 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002391 _LIBCPP_INLINE_VISIBILITY
2392 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002393 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002394 _LIBCPP_INLINE_VISIBILITY
2395 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002396 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002397 _LIBCPP_INLINE_VISIBILITY
2398 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002399 {return begin() + (__p - cbegin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002400
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002402 void __copy_assign_alloc(const vector& __v)
2403 {__copy_assign_alloc(__v, integral_constant<bool,
2404 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002406 void __copy_assign_alloc(const vector& __c, true_type)
2407 {
2408 if (__alloc() != __c.__alloc())
2409 deallocate();
2410 __alloc() = __c.__alloc();
2411 }
2412
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002414 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002415 {}
2416
2417 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002418 void __move_assign(vector& __c, true_type)
2419 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002421 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002422 _NOEXCEPT_(
2423 !__storage_traits::propagate_on_container_move_assignment::value ||
2424 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002425 {__move_assign_alloc(__c, integral_constant<bool,
2426 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00002428 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002429 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002430 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002431 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002432 }
2433
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002435 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002436 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002437 {}
2438
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002439 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002440
2441 friend class __bit_reference<vector>;
2442 friend class __bit_const_reference<vector>;
2443 friend class __bit_iterator<vector, false>;
2444 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:18 +00002445 friend struct __bit_array<vector>;
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002446 friend struct _LIBCPP_TYPE_VIS_ONLY hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002447};
2448
2449template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002450inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002451void
2452vector<bool, _Allocator>::__invalidate_all_iterators()
2453{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002454}
2455
2456// Allocate space for __n objects
2457// throws length_error if __n > max_size()
2458// throws (probably bad_alloc) if memory run out
2459// Precondition: __begin_ == __end_ == __cap() == 0
2460// Precondition: __n > 0
2461// Postcondition: capacity() == __n
2462// Postcondition: size() == 0
2463template <class _Allocator>
2464void
2465vector<bool, _Allocator>::allocate(size_type __n)
2466{
2467 if (__n > max_size())
2468 this->__throw_length_error();
2469 __n = __external_cap_to_internal(__n);
2470 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2471 this->__size_ = 0;
2472 this->__cap() = __n;
2473}
2474
2475template <class _Allocator>
2476void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002477vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002478{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002479 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002480 {
2481 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2482 __invalidate_all_iterators();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002483 this->__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002484 this->__size_ = this->__cap() = 0;
2485 }
2486}
2487
2488template <class _Allocator>
2489typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002490vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002491{
2492 size_type __amax = __storage_traits::max_size(__alloc());
2493 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2494 if (__nmax / __bits_per_word <= __amax)
2495 return __nmax;
2496 return __internal_cap_to_external(__amax);
2497}
2498
2499// Precondition: __new_size > capacity()
2500template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002501inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002502typename vector<bool, _Allocator>::size_type
2503vector<bool, _Allocator>::__recommend(size_type __new_size) const
2504{
2505 const size_type __ms = max_size();
2506 if (__new_size > __ms)
2507 this->__throw_length_error();
2508 const size_type __cap = capacity();
2509 if (__cap >= __ms / 2)
2510 return __ms;
Howard Hinnant7f764502013-08-14 18:00:20 +00002511 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002512}
2513
2514// Default constructs __n objects starting at __end_
2515// Precondition: __n > 0
2516// Precondition: size() + __n <= capacity()
2517// Postcondition: size() == size() + __n
2518template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002519inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002520void
2521vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2522{
2523 size_type __old_size = this->__size_;
2524 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002525 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002526}
2527
2528template <class _Allocator>
2529template <class _ForwardIterator>
2530typename enable_if
2531<
2532 __is_forward_iterator<_ForwardIterator>::value,
2533 void
2534>::type
2535vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2536{
2537 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002538 this->__size_ += _VSTD::distance(__first, __last);
2539 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002540}
2541
2542template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002543inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002544vector<bool, _Allocator>::vector()
Marshall Clowc912c0c2015-06-04 02:05:41 +00002545 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002546 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002547 __size_(0),
2548 __cap_alloc_(0)
2549{
2550}
2551
2552template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002553inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002554vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow127db912015-06-04 00:10:20 +00002555#if _LIBCPP_STD_VER <= 14
2556 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2557#else
2558 _NOEXCEPT
2559#endif
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002560 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002561 __size_(0),
2562 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2563{
2564}
2565
2566template <class _Allocator>
2567vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002568 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002569 __size_(0),
2570 __cap_alloc_(0)
2571{
2572 if (__n > 0)
2573 {
2574 allocate(__n);
2575 __construct_at_end(__n, false);
2576 }
2577}
2578
Marshall Clowa49a2c92013-09-14 00:47:59 +00002579#if _LIBCPP_STD_VER > 11
2580template <class _Allocator>
2581vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2582 : __begin_(nullptr),
2583 __size_(0),
2584 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2585{
2586 if (__n > 0)
2587 {
2588 allocate(__n);
2589 __construct_at_end(__n, false);
2590 }
2591}
2592#endif
2593
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002594template <class _Allocator>
2595vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002596 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002597 __size_(0),
2598 __cap_alloc_(0)
2599{
2600 if (__n > 0)
2601 {
2602 allocate(__n);
2603 __construct_at_end(__n, __x);
2604 }
2605}
2606
2607template <class _Allocator>
2608vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002609 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002610 __size_(0),
2611 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2612{
2613 if (__n > 0)
2614 {
2615 allocate(__n);
2616 __construct_at_end(__n, __x);
2617 }
2618}
2619
2620template <class _Allocator>
2621template <class _InputIterator>
2622vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2623 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2624 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002625 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002626 __size_(0),
2627 __cap_alloc_(0)
2628{
2629#ifndef _LIBCPP_NO_EXCEPTIONS
2630 try
2631 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002632#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002633 for (; __first != __last; ++__first)
2634 push_back(*__first);
2635#ifndef _LIBCPP_NO_EXCEPTIONS
2636 }
2637 catch (...)
2638 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002639 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002640 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2641 __invalidate_all_iterators();
2642 throw;
2643 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002644#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002645}
2646
2647template <class _Allocator>
2648template <class _InputIterator>
2649vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2650 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2651 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002652 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002653 __size_(0),
2654 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2655{
2656#ifndef _LIBCPP_NO_EXCEPTIONS
2657 try
2658 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002659#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002660 for (; __first != __last; ++__first)
2661 push_back(*__first);
2662#ifndef _LIBCPP_NO_EXCEPTIONS
2663 }
2664 catch (...)
2665 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002666 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002667 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2668 __invalidate_all_iterators();
2669 throw;
2670 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002671#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002672}
2673
2674template <class _Allocator>
2675template <class _ForwardIterator>
2676vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2677 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002678 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002679 __size_(0),
2680 __cap_alloc_(0)
2681{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002682 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002683 if (__n > 0)
2684 {
2685 allocate(__n);
2686 __construct_at_end(__first, __last);
2687 }
2688}
2689
2690template <class _Allocator>
2691template <class _ForwardIterator>
2692vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2693 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002694 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002695 __size_(0),
2696 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2697{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002698 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002699 if (__n > 0)
2700 {
2701 allocate(__n);
2702 __construct_at_end(__first, __last);
2703 }
2704}
2705
Howard Hinnante3e32912011-08-12 21:56:02 +00002706#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2707
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002708template <class _Allocator>
2709vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002710 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002711 __size_(0),
2712 __cap_alloc_(0)
2713{
2714 size_type __n = static_cast<size_type>(__il.size());
2715 if (__n > 0)
2716 {
2717 allocate(__n);
2718 __construct_at_end(__il.begin(), __il.end());
2719 }
2720}
2721
2722template <class _Allocator>
2723vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002724 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002725 __size_(0),
2726 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2727{
2728 size_type __n = static_cast<size_type>(__il.size());
2729 if (__n > 0)
2730 {
2731 allocate(__n);
2732 __construct_at_end(__il.begin(), __il.end());
2733 }
2734}
2735
Howard Hinnante3e32912011-08-12 21:56:02 +00002736#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2737
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002739vector<bool, _Allocator>::~vector()
2740{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002741 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002742 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002743 __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002744}
2745
2746template <class _Allocator>
2747vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002748 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002749 __size_(0),
2750 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2751{
2752 if (__v.size() > 0)
2753 {
2754 allocate(__v.size());
2755 __construct_at_end(__v.begin(), __v.end());
2756 }
2757}
2758
2759template <class _Allocator>
2760vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002761 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002762 __size_(0),
2763 __cap_alloc_(0, __a)
2764{
2765 if (__v.size() > 0)
2766 {
2767 allocate(__v.size());
2768 __construct_at_end(__v.begin(), __v.end());
2769 }
2770}
2771
2772template <class _Allocator>
2773vector<bool, _Allocator>&
2774vector<bool, _Allocator>::operator=(const vector& __v)
2775{
2776 if (this != &__v)
2777 {
2778 __copy_assign_alloc(__v);
2779 if (__v.__size_)
2780 {
2781 if (__v.__size_ > capacity())
2782 {
2783 deallocate();
2784 allocate(__v.__size_);
2785 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002786 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002787 }
2788 __size_ = __v.__size_;
2789 }
2790 return *this;
2791}
2792
Howard Hinnant73d21a42010-09-04 23:28:19 +00002793#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2794
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002795template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002796inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002797vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clow119ed472015-07-14 14:46:32 +00002798#if _LIBCPP_STD_VER > 14
2799 _NOEXCEPT
2800#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002801 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow119ed472015-07-14 14:46:32 +00002802#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002803 : __begin_(__v.__begin_),
2804 __size_(__v.__size_),
2805 __cap_alloc_(__v.__cap_alloc_)
2806{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002807 __v.__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002808 __v.__size_ = 0;
2809 __v.__cap() = 0;
2810}
2811
2812template <class _Allocator>
2813vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002814 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002815 __size_(0),
2816 __cap_alloc_(0, __a)
2817{
2818 if (__a == allocator_type(__v.__alloc()))
2819 {
2820 this->__begin_ = __v.__begin_;
2821 this->__size_ = __v.__size_;
2822 this->__cap() = __v.__cap();
2823 __v.__begin_ = nullptr;
2824 __v.__cap() = __v.__size_ = 0;
2825 }
2826 else if (__v.size() > 0)
2827 {
2828 allocate(__v.size());
2829 __construct_at_end(__v.begin(), __v.end());
2830 }
2831}
2832
2833template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002834inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002835vector<bool, _Allocator>&
2836vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002837 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002838{
2839 __move_assign(__v, integral_constant<bool,
2840 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002841 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002842}
2843
2844template <class _Allocator>
2845void
2846vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2847{
2848 if (__alloc() != __c.__alloc())
2849 assign(__c.begin(), __c.end());
2850 else
2851 __move_assign(__c, true_type());
2852}
2853
2854template <class _Allocator>
2855void
2856vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002857 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002858{
2859 deallocate();
Marshall Clowdb5e54d2014-07-21 15:15:15 +00002860 __move_assign_alloc(__c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002861 this->__begin_ = __c.__begin_;
2862 this->__size_ = __c.__size_;
2863 this->__cap() = __c.__cap();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002864 __c.__begin_ = nullptr;
2865 __c.__cap() = __c.__size_ = 0;
2866}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002867
2868#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002869
2870template <class _Allocator>
2871void
2872vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2873{
2874 __size_ = 0;
2875 if (__n > 0)
2876 {
2877 size_type __c = capacity();
2878 if (__n <= __c)
2879 __size_ = __n;
2880 else
2881 {
2882 vector __v(__alloc());
2883 __v.reserve(__recommend(__n));
2884 __v.__size_ = __n;
2885 swap(__v);
2886 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002887 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002888 }
2889}
2890
2891template <class _Allocator>
2892template <class _InputIterator>
2893typename enable_if
2894<
2895 __is_input_iterator<_InputIterator>::value &&
2896 !__is_forward_iterator<_InputIterator>::value,
2897 void
2898>::type
2899vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2900{
2901 clear();
2902 for (; __first != __last; ++__first)
2903 push_back(*__first);
2904}
2905
2906template <class _Allocator>
2907template <class _ForwardIterator>
2908typename enable_if
2909<
2910 __is_forward_iterator<_ForwardIterator>::value,
2911 void
2912>::type
2913vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2914{
2915 clear();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002916 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002917 if (__n)
2918 {
2919 if (__n > capacity())
2920 {
2921 deallocate();
2922 allocate(__n);
2923 }
2924 __construct_at_end(__first, __last);
2925 }
2926}
2927
2928template <class _Allocator>
2929void
2930vector<bool, _Allocator>::reserve(size_type __n)
2931{
2932 if (__n > capacity())
2933 {
2934 vector __v(this->__alloc());
2935 __v.allocate(__n);
2936 __v.__construct_at_end(this->begin(), this->end());
2937 swap(__v);
2938 __invalidate_all_iterators();
2939 }
2940}
2941
2942template <class _Allocator>
2943void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002944vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002945{
2946 if (__external_cap_to_internal(size()) > __cap())
2947 {
2948#ifndef _LIBCPP_NO_EXCEPTIONS
2949 try
2950 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002951#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002952 vector(*this, allocator_type(__alloc())).swap(*this);
2953#ifndef _LIBCPP_NO_EXCEPTIONS
2954 }
2955 catch (...)
2956 {
2957 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002958#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002959 }
2960}
2961
2962template <class _Allocator>
2963typename vector<bool, _Allocator>::reference
2964vector<bool, _Allocator>::at(size_type __n)
2965{
2966 if (__n >= size())
2967 this->__throw_out_of_range();
2968 return (*this)[__n];
2969}
2970
2971template <class _Allocator>
2972typename vector<bool, _Allocator>::const_reference
2973vector<bool, _Allocator>::at(size_type __n) const
2974{
2975 if (__n >= size())
2976 this->__throw_out_of_range();
2977 return (*this)[__n];
2978}
2979
2980template <class _Allocator>
2981void
2982vector<bool, _Allocator>::push_back(const value_type& __x)
2983{
2984 if (this->__size_ == this->capacity())
2985 reserve(__recommend(this->__size_ + 1));
2986 ++this->__size_;
2987 back() = __x;
2988}
2989
2990template <class _Allocator>
2991typename vector<bool, _Allocator>::iterator
2992vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2993{
2994 iterator __r;
2995 if (size() < capacity())
2996 {
2997 const_iterator __old_end = end();
2998 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002999 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003000 __r = __const_iterator_cast(__position);
3001 }
3002 else
3003 {
3004 vector __v(__alloc());
3005 __v.reserve(__recommend(__size_ + 1));
3006 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003007 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3008 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003009 swap(__v);
3010 }
3011 *__r = __x;
3012 return __r;
3013}
3014
3015template <class _Allocator>
3016typename vector<bool, _Allocator>::iterator
3017vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3018{
3019 iterator __r;
3020 size_type __c = capacity();
3021 if (__n <= __c && size() <= __c - __n)
3022 {
3023 const_iterator __old_end = end();
3024 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003025 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003026 __r = __const_iterator_cast(__position);
3027 }
3028 else
3029 {
3030 vector __v(__alloc());
3031 __v.reserve(__recommend(__size_ + __n));
3032 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003033 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3034 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003035 swap(__v);
3036 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003037 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003038 return __r;
3039}
3040
3041template <class _Allocator>
3042template <class _InputIterator>
3043typename enable_if
3044<
3045 __is_input_iterator <_InputIterator>::value &&
3046 !__is_forward_iterator<_InputIterator>::value,
3047 typename vector<bool, _Allocator>::iterator
3048>::type
3049vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3050{
3051 difference_type __off = __position - begin();
3052 iterator __p = __const_iterator_cast(__position);
3053 iterator __old_end = end();
3054 for (; size() != capacity() && __first != __last; ++__first)
3055 {
3056 ++this->__size_;
3057 back() = *__first;
3058 }
3059 vector __v(__alloc());
3060 if (__first != __last)
3061 {
3062#ifndef _LIBCPP_NO_EXCEPTIONS
3063 try
3064 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003065#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003066 __v.assign(__first, __last);
3067 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3068 difference_type __old_p = __p - begin();
3069 reserve(__recommend(size() + __v.size()));
3070 __p = begin() + __old_p;
3071 __old_end = begin() + __old_size;
3072#ifndef _LIBCPP_NO_EXCEPTIONS
3073 }
3074 catch (...)
3075 {
3076 erase(__old_end, end());
3077 throw;
3078 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003079#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003080 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003081 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003082 insert(__p, __v.begin(), __v.end());
3083 return begin() + __off;
3084}
3085
3086template <class _Allocator>
3087template <class _ForwardIterator>
3088typename enable_if
3089<
3090 __is_forward_iterator<_ForwardIterator>::value,
3091 typename vector<bool, _Allocator>::iterator
3092>::type
3093vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3094{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003095 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003096 iterator __r;
3097 size_type __c = capacity();
3098 if (__n <= __c && size() <= __c - __n)
3099 {
3100 const_iterator __old_end = end();
3101 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003102 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003103 __r = __const_iterator_cast(__position);
3104 }
3105 else
3106 {
3107 vector __v(__alloc());
3108 __v.reserve(__recommend(__size_ + __n));
3109 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003110 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3111 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003112 swap(__v);
3113 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003114 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003115 return __r;
3116}
3117
3118template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003119inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003120typename vector<bool, _Allocator>::iterator
3121vector<bool, _Allocator>::erase(const_iterator __position)
3122{
3123 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003124 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003125 --__size_;
3126 return __r;
3127}
3128
3129template <class _Allocator>
3130typename vector<bool, _Allocator>::iterator
3131vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3132{
3133 iterator __r = __const_iterator_cast(__first);
3134 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003135 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003136 __size_ -= __d;
3137 return __r;
3138}
3139
3140template <class _Allocator>
3141void
3142vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow7d914d12015-07-13 20:04:56 +00003143#if _LIBCPP_STD_VER >= 14
3144 _NOEXCEPT
3145#else
3146 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3147 __is_nothrow_swappable<allocator_type>::value)
3148#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003149{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003150 _VSTD::swap(this->__begin_, __x.__begin_);
3151 _VSTD::swap(this->__size_, __x.__size_);
3152 _VSTD::swap(this->__cap(), __x.__cap());
Marshall Clow7d914d12015-07-13 20:04:56 +00003153 __swap_allocator(this->__alloc(), __x.__alloc(),
3154 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003155}
3156
Howard Hinnant324bb032010-08-22 00:02:43 +00003157template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003158void
3159vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3160{
3161 size_type __cs = size();
3162 if (__cs < __sz)
3163 {
3164 iterator __r;
3165 size_type __c = capacity();
3166 size_type __n = __sz - __cs;
3167 if (__n <= __c && __cs <= __c - __n)
3168 {
3169 __r = end();
3170 __size_ += __n;
3171 }
3172 else
3173 {
3174 vector __v(__alloc());
3175 __v.reserve(__recommend(__size_ + __n));
3176 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003177 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003178 swap(__v);
3179 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003180 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003181 }
3182 else
3183 __size_ = __sz;
3184}
3185
Howard Hinnant324bb032010-08-22 00:02:43 +00003186template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003187void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003188vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003189{
3190 // do middle whole words
3191 size_type __n = __size_;
3192 __storage_pointer __p = __begin_;
3193 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3194 *__p = ~*__p;
3195 // do last partial word
3196 if (__n > 0)
3197 {
3198 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3199 __storage_type __b = *__p & __m;
3200 *__p &= ~__m;
3201 *__p |= ~__b & __m;
3202 }
3203}
3204
Howard Hinnant324bb032010-08-22 00:02:43 +00003205template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003206bool
3207vector<bool, _Allocator>::__invariants() const
3208{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00003209 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003210 {
3211 if (this->__size_ != 0 || this->__cap() != 0)
3212 return false;
3213 }
3214 else
3215 {
3216 if (this->__cap() == 0)
3217 return false;
3218 if (this->__size_ > this->capacity())
3219 return false;
3220 }
3221 return true;
3222}
3223
Howard Hinnant324bb032010-08-22 00:02:43 +00003224template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003225size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003226vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003227{
3228 size_t __h = 0;
3229 // do middle whole words
3230 size_type __n = __size_;
3231 __storage_pointer __p = __begin_;
3232 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3233 __h ^= *__p;
3234 // do last partial word
3235 if (__n > 0)
3236 {
3237 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3238 __h ^= *__p & __m;
3239 }
3240 return __h;
3241}
3242
3243template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003244struct _LIBCPP_TYPE_VIS_ONLY hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003245 : public unary_function<vector<bool, _Allocator>, size_t>
3246{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00003247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003248 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003249 {return __vec.__hash_code();}
3250};
3251
3252template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003253inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003254bool
3255operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3256{
3257 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003258 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003259}
3260
3261template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003262inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003263bool
3264operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3265{
3266 return !(__x == __y);
3267}
3268
3269template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003270inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003271bool
3272operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3273{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003274 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003275}
3276
3277template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003278inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003279bool
3280operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3281{
3282 return __y < __x;
3283}
3284
3285template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003286inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003287bool
3288operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3289{
3290 return !(__x < __y);
3291}
3292
3293template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003294inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003295bool
3296operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3297{
3298 return !(__y < __x);
3299}
3300
3301template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003302inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003303void
3304swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003305 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003306{
3307 __x.swap(__y);
3308}
3309
3310_LIBCPP_END_NAMESPACE_STD
3311
3312#endif // _LIBCPP_VECTOR