blob: dbc0dd321824b489359536adbe9b1abda477956a [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>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000688 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000689 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000690#endif // _LIBCPP_HAS_NO_VARIADICS
691#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000693 void pop_back();
694
695 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000696#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000697 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000698#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000699 template <class... _Args>
700 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000701#endif // _LIBCPP_HAS_NO_VARIADICS
702#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000703 iterator insert(const_iterator __position, size_type __n, const_reference __x);
704 template <class _InputIterator>
705 typename enable_if
706 <
707 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000708 !__is_forward_iterator<_InputIterator>::value &&
709 is_constructible<
710 value_type,
711 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000712 iterator
713 >::type
714 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
715 template <class _ForwardIterator>
716 typename enable_if
717 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000718 __is_forward_iterator<_ForwardIterator>::value &&
719 is_constructible<
720 value_type,
721 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000722 iterator
723 >::type
724 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +0000725#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000727 iterator insert(const_iterator __position, initializer_list<value_type> __il)
728 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000729#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000730
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000731 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000732 iterator erase(const_iterator __first, const_iterator __last);
733
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000734 _LIBCPP_INLINE_VISIBILITY
735 void clear() _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000736 {
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000737 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000738 __base::clear();
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000739 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000740 __invalidate_all_iterators();
741 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000742
743 void resize(size_type __sz);
744 void resize(size_type __sz, const_reference __x);
745
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000746 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +0000747#if _LIBCPP_STD_VER >= 14
748 _NOEXCEPT;
749#else
750 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
751 __is_nothrow_swappable<allocator_type>::value);
752#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000753
754 bool __invariants() const;
755
Howard Hinnantabe26282011-09-16 17:29:17 +0000756#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000757
758 bool __dereferenceable(const const_iterator* __i) const;
759 bool __decrementable(const const_iterator* __i) const;
760 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
761 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
762
Howard Hinnantabe26282011-09-16 17:29:17 +0000763#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000764
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000766 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000767 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000768 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000769 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31 +0000770 void __construct_at_end(size_type __n);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000771 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000772 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000773 template <class _ForwardIterator>
774 typename enable_if
775 <
776 __is_forward_iterator<_ForwardIterator>::value,
777 void
778 >::type
Eric Fiselier088ed9f2015-03-31 16:54:19 +0000779 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000780 void __append(size_type __n);
781 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000783 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000785 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000786 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
787 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
788 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000789 void __move_assign(vector& __c, true_type)
790 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clowaf961ed2015-08-18 18:57:00 +0000791 void __move_assign(vector& __c, false_type)
792 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000794 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000795 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000796#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000797 __c_node* __c = __get_db()->__find_c_and_lock(this);
798 for (__i_node** __p = __c->end_; __p != __c->beg_; )
799 {
800 --__p;
801 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
802 if (__i->base() > __new_last)
803 {
804 (*__p)->__c_ = nullptr;
805 if (--__c->end_ != __p)
806 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
807 }
808 }
809 __get_db()->unlock();
810#endif
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000811 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000812 __base::__destruct_at_end(__new_last);
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000813 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000814 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000815 template <class _Up>
816 void
817#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
818 __push_back_slow_path(_Up&& __x);
819#else
820 __push_back_slow_path(_Up& __x);
821#endif
Howard Hinnant0438ea22012-02-26 15:30:12 +0000822#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
823 template <class... _Args>
824 void
825 __emplace_back_slow_path(_Args&&... __args);
826#endif
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000827 // The following functions are no-ops outside of AddressSanitizer mode.
828 // We call annotatations only for the default Allocator because other allocators
829 // may not meet the AddressSanitizer alignment constraints.
830 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
831 void __annotate_contiguous_container
Kostya Serebryany497f9122014-09-02 23:43:38 +0000832 (const void *__beg, const void *__end, const void *__old_mid, const void *__new_mid) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000833 {
834#ifndef _LIBCPP_HAS_NO_ASAN
835 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
836 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
837#endif
838 }
839
Kostya Serebryany497f9122014-09-02 23:43:38 +0000840 void __annotate_new(size_type __current_size) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000841 {
842 __annotate_contiguous_container(data(), data() + capacity(),
843 data() + capacity(), data() + __current_size);
844 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000845 void __annotate_delete() const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000846 {
847 __annotate_contiguous_container(data(), data() + capacity(),
848 data() + size(), data() + capacity());
849 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000850 void __annotate_increase(size_type __n) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000851 {
852 __annotate_contiguous_container(data(), data() + capacity(),
853 data() + size(), data() + size() + __n);
854 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000855 void __annotate_shrink(size_type __old_size) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000856 {
857 __annotate_contiguous_container(data(), data() + capacity(),
858 data() + __old_size, data() + size());
859 }
Marshall Clow26f472d2014-09-03 21:37:43 +0000860#ifndef _LIBCPP_HAS_NO_ASAN
Kostya Serebryany497f9122014-09-02 23:43:38 +0000861 // The annotation for size increase should happen before the actual increase,
862 // but if an exception is thrown after that the annotation has to be undone.
863 struct __RAII_IncreaseAnnotator {
864 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000865 : __commit(false), __v(__v), __old_size(__v.size() + __n) {
Kostya Serebryany497f9122014-09-02 23:43:38 +0000866 __v.__annotate_increase(__n);
867 }
868 void __done() { __commit = true; }
869 ~__RAII_IncreaseAnnotator() {
870 if (__commit) return;
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000871 __v.__annotate_shrink(__old_size);
Kostya Serebryany497f9122014-09-02 23:43:38 +0000872 }
873 bool __commit;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000874 const vector &__v;
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000875 size_type __old_size;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000876 };
Marshall Clow26f472d2014-09-03 21:37:43 +0000877#else
878 struct __RAII_IncreaseAnnotator {
879 inline __RAII_IncreaseAnnotator(const vector &, size_type __n = 1) {}
880 inline void __done() {}
881 };
882#endif
883
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000884};
885
886template <class _Tp, class _Allocator>
887void
888vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
889{
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000890 __annotate_delete();
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000891 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000892 _VSTD::swap(this->__begin_, __v.__begin_);
893 _VSTD::swap(this->__end_, __v.__end_);
894 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000895 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000896 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000897 __invalidate_all_iterators();
898}
899
900template <class _Tp, class _Allocator>
901typename vector<_Tp, _Allocator>::pointer
902vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
903{
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000904 __annotate_delete();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000905 pointer __r = __v.__begin_;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000906 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
907 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000908 _VSTD::swap(this->__begin_, __v.__begin_);
909 _VSTD::swap(this->__end_, __v.__end_);
910 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000911 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000912 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000913 __invalidate_all_iterators();
914 return __r;
915}
916
917// Allocate space for __n objects
918// throws length_error if __n > max_size()
919// throws (probably bad_alloc) if memory run out
920// Precondition: __begin_ == __end_ == __end_cap() == 0
921// Precondition: __n > 0
922// Postcondition: capacity() == __n
923// Postcondition: size() == 0
924template <class _Tp, class _Allocator>
925void
926vector<_Tp, _Allocator>::allocate(size_type __n)
927{
928 if (__n > max_size())
929 this->__throw_length_error();
930 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
931 this->__end_cap() = this->__begin_ + __n;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000932 __annotate_new(0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000933}
934
935template <class _Tp, class _Allocator>
936void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000937vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000938{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000939 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000940 {
941 clear();
942 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000943 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000944 }
945}
946
947template <class _Tp, class _Allocator>
948typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000949vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000950{
Sean Hunt110b8bf2011-07-29 23:31:58 +0000951 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 +0000952}
953
954// Precondition: __new_size > capacity()
955template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000956inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000957typename vector<_Tp, _Allocator>::size_type
958vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
959{
960 const size_type __ms = max_size();
961 if (__new_size > __ms)
962 this->__throw_length_error();
963 const size_type __cap = capacity();
964 if (__cap >= __ms / 2)
965 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58 +0000966 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000967}
968
969// Default constructs __n objects starting at __end_
970// throws if construction throws
971// Precondition: __n > 0
972// Precondition: size() + __n <= capacity()
973// Postcondition: size() == size() + __n
974template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000975void
976vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
977{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000978 allocator_type& __a = this->__alloc();
979 do
980 {
Kostya Serebryany497f9122014-09-02 23:43:38 +0000981 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000982 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000983 ++this->__end_;
984 --__n;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000985 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000986 } while (__n > 0);
987}
988
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000989// Copy constructs __n objects starting at __end_ from __x
990// throws if construction throws
991// Precondition: __n > 0
992// Precondition: size() + __n <= capacity()
993// Postcondition: size() == old size() + __n
994// Postcondition: [i] == __x for all i in [size() - __n, __n)
995template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000996inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000997void
998vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
999{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001000 allocator_type& __a = this->__alloc();
1001 do
1002 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001003 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001004 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001005 ++this->__end_;
1006 --__n;
Kostya Serebryany497f9122014-09-02 23:43:38 +00001007 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001008 } while (__n > 0);
1009}
1010
1011template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001012template <class _ForwardIterator>
1013typename enable_if
1014<
1015 __is_forward_iterator<_ForwardIterator>::value,
1016 void
1017>::type
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001018vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001019{
1020 allocator_type& __a = this->__alloc();
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001021 __RAII_IncreaseAnnotator __annotator(*this, __n);
1022 __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
1023 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001024}
1025
1026// Default constructs __n objects starting at __end_
1027// throws if construction throws
1028// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001029// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001030template <class _Tp, class _Allocator>
1031void
1032vector<_Tp, _Allocator>::__append(size_type __n)
1033{
1034 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1035 this->__construct_at_end(__n);
1036 else
1037 {
1038 allocator_type& __a = this->__alloc();
1039 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1040 __v.__construct_at_end(__n);
1041 __swap_out_circular_buffer(__v);
1042 }
1043}
1044
1045// Default constructs __n objects starting at __end_
1046// throws if construction throws
1047// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001048// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001049template <class _Tp, class _Allocator>
1050void
1051vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1052{
1053 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1054 this->__construct_at_end(__n, __x);
1055 else
1056 {
1057 allocator_type& __a = this->__alloc();
1058 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1059 __v.__construct_at_end(__n, __x);
1060 __swap_out_circular_buffer(__v);
1061 }
1062}
1063
1064template <class _Tp, class _Allocator>
1065vector<_Tp, _Allocator>::vector(size_type __n)
1066{
Howard Hinnant0442b122011-09-16 18:41:29 +00001067#if _LIBCPP_DEBUG_LEVEL >= 2
1068 __get_db()->__insert_c(this);
1069#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001070 if (__n > 0)
1071 {
1072 allocate(__n);
1073 __construct_at_end(__n);
1074 }
1075}
1076
Marshall Clowa49a2c92013-09-14 00:47:59 +00001077#if _LIBCPP_STD_VER > 11
1078template <class _Tp, class _Allocator>
1079vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1080 : __base(__a)
1081{
1082#if _LIBCPP_DEBUG_LEVEL >= 2
1083 __get_db()->__insert_c(this);
1084#endif
1085 if (__n > 0)
1086 {
1087 allocate(__n);
1088 __construct_at_end(__n);
1089 }
1090}
1091#endif
1092
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001093template <class _Tp, class _Allocator>
1094vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1095{
Howard Hinnant0442b122011-09-16 18:41:29 +00001096#if _LIBCPP_DEBUG_LEVEL >= 2
1097 __get_db()->__insert_c(this);
1098#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001099 if (__n > 0)
1100 {
1101 allocate(__n);
1102 __construct_at_end(__n, __x);
1103 }
1104}
1105
1106template <class _Tp, class _Allocator>
1107vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1108 : __base(__a)
1109{
Howard Hinnant0442b122011-09-16 18:41:29 +00001110#if _LIBCPP_DEBUG_LEVEL >= 2
1111 __get_db()->__insert_c(this);
1112#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001113 if (__n > 0)
1114 {
1115 allocate(__n);
1116 __construct_at_end(__n, __x);
1117 }
1118}
1119
1120template <class _Tp, class _Allocator>
1121template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +00001122vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001123 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001124 !__is_forward_iterator<_InputIterator>::value &&
1125 is_constructible<
1126 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +00001127 typename iterator_traits<_InputIterator>::reference>::value,
1128 _InputIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001129{
Howard Hinnantabe26282011-09-16 17:29:17 +00001130#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001131 __get_db()->__insert_c(this);
1132#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001133 for (; __first != __last; ++__first)
1134 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001135}
1136
1137template <class _Tp, class _Allocator>
1138template <class _InputIterator>
1139vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1140 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001141 !__is_forward_iterator<_InputIterator>::value &&
1142 is_constructible<
1143 value_type,
1144 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001145 : __base(__a)
1146{
Howard Hinnantabe26282011-09-16 17:29:17 +00001147#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001148 __get_db()->__insert_c(this);
1149#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001150 for (; __first != __last; ++__first)
1151 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001152}
1153
1154template <class _Tp, class _Allocator>
1155template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +00001156vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001157 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1158 is_constructible<
1159 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +00001160 typename iterator_traits<_ForwardIterator>::reference>::value,
1161 _ForwardIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001162{
Howard Hinnant0442b122011-09-16 18:41:29 +00001163#if _LIBCPP_DEBUG_LEVEL >= 2
1164 __get_db()->__insert_c(this);
1165#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001166 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001167 if (__n > 0)
1168 {
1169 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001170 __construct_at_end(__first, __last, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001171 }
1172}
1173
1174template <class _Tp, class _Allocator>
1175template <class _ForwardIterator>
1176vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001177 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1178 is_constructible<
1179 value_type,
1180 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001181 : __base(__a)
1182{
Howard Hinnant0442b122011-09-16 18:41:29 +00001183#if _LIBCPP_DEBUG_LEVEL >= 2
1184 __get_db()->__insert_c(this);
1185#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001186 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001187 if (__n > 0)
1188 {
1189 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001190 __construct_at_end(__first, __last, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001191 }
1192}
1193
1194template <class _Tp, class _Allocator>
1195vector<_Tp, _Allocator>::vector(const vector& __x)
1196 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1197{
Howard Hinnant0442b122011-09-16 18:41:29 +00001198#if _LIBCPP_DEBUG_LEVEL >= 2
1199 __get_db()->__insert_c(this);
1200#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001201 size_type __n = __x.size();
1202 if (__n > 0)
1203 {
1204 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001205 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001206 }
1207}
1208
1209template <class _Tp, class _Allocator>
1210vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1211 : __base(__a)
1212{
Howard Hinnant0442b122011-09-16 18:41:29 +00001213#if _LIBCPP_DEBUG_LEVEL >= 2
1214 __get_db()->__insert_c(this);
1215#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001216 size_type __n = __x.size();
1217 if (__n > 0)
1218 {
1219 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001220 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001221 }
1222}
1223
Howard Hinnant73d21a42010-09-04 23:28:19 +00001224#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001225
1226template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001227inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001228vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clow119ed472015-07-14 14:46:32 +00001229#if _LIBCPP_STD_VER > 14
1230 _NOEXCEPT
1231#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001232 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow119ed472015-07-14 14:46:32 +00001233#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001234 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001235{
Howard Hinnantabe26282011-09-16 17:29:17 +00001236#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001237 __get_db()->__insert_c(this);
Howard Hinnante6125bd2011-09-19 16:34:29 +00001238 __get_db()->swap(this, &__x);
Howard Hinnant7a563db2011-09-14 18:33:51 +00001239#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001240 this->__begin_ = __x.__begin_;
1241 this->__end_ = __x.__end_;
1242 this->__end_cap() = __x.__end_cap();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001243 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001244}
1245
1246template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001247inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001248vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1249 : __base(__a)
1250{
Howard Hinnant0442b122011-09-16 18:41:29 +00001251#if _LIBCPP_DEBUG_LEVEL >= 2
1252 __get_db()->__insert_c(this);
1253#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001254 if (__a == __x.__alloc())
1255 {
1256 this->__begin_ = __x.__begin_;
1257 this->__end_ = __x.__end_;
1258 this->__end_cap() = __x.__end_cap();
1259 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001260#if _LIBCPP_DEBUG_LEVEL >= 2
1261 __get_db()->swap(this, &__x);
1262#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001263 }
1264 else
1265 {
Howard Hinnant99968442011-11-29 18:15:50 +00001266 typedef move_iterator<iterator> _Ip;
1267 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001268 }
1269}
1270
Howard Hinnante3e32912011-08-12 21:56:02 +00001271#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1272
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001273template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001274inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001275vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1276{
Howard Hinnant0442b122011-09-16 18:41:29 +00001277#if _LIBCPP_DEBUG_LEVEL >= 2
1278 __get_db()->__insert_c(this);
1279#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001280 if (__il.size() > 0)
1281 {
1282 allocate(__il.size());
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001283 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001284 }
1285}
1286
1287template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001288inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001289vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1290 : __base(__a)
1291{
Howard Hinnant0442b122011-09-16 18:41:29 +00001292#if _LIBCPP_DEBUG_LEVEL >= 2
1293 __get_db()->__insert_c(this);
1294#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001295 if (__il.size() > 0)
1296 {
1297 allocate(__il.size());
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001298 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001299 }
1300}
1301
Howard Hinnante3e32912011-08-12 21:56:02 +00001302#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1303
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001304template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001305inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001306vector<_Tp, _Allocator>&
1307vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001308 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001309{
1310 __move_assign(__x, integral_constant<bool,
1311 __alloc_traits::propagate_on_container_move_assignment::value>());
1312 return *this;
1313}
1314
1315template <class _Tp, class _Allocator>
1316void
1317vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001318 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001319{
1320 if (__base::__alloc() != __c.__alloc())
1321 {
Howard Hinnant99968442011-11-29 18:15:50 +00001322 typedef move_iterator<iterator> _Ip;
1323 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001324 }
1325 else
1326 __move_assign(__c, true_type());
1327}
1328
1329template <class _Tp, class _Allocator>
1330void
1331vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001332 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001333{
1334 deallocate();
Marshall Clow3c2eac62014-07-21 15:11:13 +00001335 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001336 this->__begin_ = __c.__begin_;
1337 this->__end_ = __c.__end_;
1338 this->__end_cap() = __c.__end_cap();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001339 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001340#if _LIBCPP_DEBUG_LEVEL >= 2
1341 __get_db()->swap(this, &__c);
1342#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001343}
1344
Howard Hinnant73d21a42010-09-04 23:28:19 +00001345#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001346
1347template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001348inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001349vector<_Tp, _Allocator>&
1350vector<_Tp, _Allocator>::operator=(const vector& __x)
1351{
1352 if (this != &__x)
1353 {
1354 __base::__copy_assign_alloc(__x);
1355 assign(__x.__begin_, __x.__end_);
1356 }
1357 return *this;
1358}
1359
1360template <class _Tp, class _Allocator>
1361template <class _InputIterator>
1362typename enable_if
1363<
1364 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001365 !__is_forward_iterator<_InputIterator>::value &&
1366 is_constructible<
1367 _Tp,
1368 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001369 void
1370>::type
1371vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1372{
1373 clear();
1374 for (; __first != __last; ++__first)
1375 push_back(*__first);
1376}
1377
1378template <class _Tp, class _Allocator>
1379template <class _ForwardIterator>
1380typename enable_if
1381<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001382 __is_forward_iterator<_ForwardIterator>::value &&
1383 is_constructible<
1384 _Tp,
1385 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001386 void
1387>::type
1388vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1389{
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001390 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1391 if (__new_size <= capacity())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001392 {
1393 _ForwardIterator __mid = __last;
1394 bool __growing = false;
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001395 if (__new_size > size())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001396 {
1397 __growing = true;
1398 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001399 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001400 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001401 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001402 if (__growing)
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001403 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001404 else
1405 this->__destruct_at_end(__m);
1406 }
1407 else
1408 {
1409 deallocate();
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001410 allocate(__recommend(__new_size));
1411 __construct_at_end(__first, __last, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001412 }
1413}
1414
1415template <class _Tp, class _Allocator>
1416void
1417vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1418{
1419 if (__n <= capacity())
1420 {
1421 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001422 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001423 if (__n > __s)
1424 __construct_at_end(__n - __s, __u);
1425 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001426 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001427 }
1428 else
1429 {
1430 deallocate();
1431 allocate(__recommend(static_cast<size_type>(__n)));
1432 __construct_at_end(__n, __u);
1433 }
1434}
1435
Howard Hinnant324bb032010-08-22 00:02:43 +00001436template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001437inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001438typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001439vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001440{
Howard Hinnantabe26282011-09-16 17:29:17 +00001441#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001442 return iterator(this, __p);
1443#else
1444 return iterator(__p);
1445#endif
1446}
1447
Howard Hinnant324bb032010-08-22 00:02:43 +00001448template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001449inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001450typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001451vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001452{
Howard Hinnantabe26282011-09-16 17:29:17 +00001453#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001454 return const_iterator(this, __p);
1455#else
1456 return const_iterator(__p);
1457#endif
1458}
1459
Howard Hinnant324bb032010-08-22 00:02:43 +00001460template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001461inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001462typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001463vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001464{
1465 return __make_iter(this->__begin_);
1466}
1467
Howard Hinnant324bb032010-08-22 00:02:43 +00001468template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001469inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001470typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001471vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001472{
1473 return __make_iter(this->__begin_);
1474}
1475
Howard Hinnant324bb032010-08-22 00:02:43 +00001476template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001477inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001479vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001480{
1481 return __make_iter(this->__end_);
1482}
1483
Howard Hinnant324bb032010-08-22 00:02:43 +00001484template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001485inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001487vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001488{
1489 return __make_iter(this->__end_);
1490}
1491
Howard Hinnant324bb032010-08-22 00:02:43 +00001492template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001493inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001494typename vector<_Tp, _Allocator>::reference
1495vector<_Tp, _Allocator>::operator[](size_type __n)
1496{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001497 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001498 return this->__begin_[__n];
1499}
1500
Howard Hinnant324bb032010-08-22 00:02:43 +00001501template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001502inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001503typename vector<_Tp, _Allocator>::const_reference
1504vector<_Tp, _Allocator>::operator[](size_type __n) const
1505{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001506 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001507 return this->__begin_[__n];
1508}
1509
Howard Hinnant324bb032010-08-22 00:02:43 +00001510template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001511typename vector<_Tp, _Allocator>::reference
1512vector<_Tp, _Allocator>::at(size_type __n)
1513{
1514 if (__n >= size())
1515 this->__throw_out_of_range();
1516 return this->__begin_[__n];
1517}
1518
Howard Hinnant324bb032010-08-22 00:02:43 +00001519template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001520typename vector<_Tp, _Allocator>::const_reference
1521vector<_Tp, _Allocator>::at(size_type __n) const
1522{
1523 if (__n >= size())
1524 this->__throw_out_of_range();
1525 return this->__begin_[__n];
1526}
1527
1528template <class _Tp, class _Allocator>
1529void
1530vector<_Tp, _Allocator>::reserve(size_type __n)
1531{
1532 if (__n > capacity())
1533 {
1534 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001535 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001536 __swap_out_circular_buffer(__v);
1537 }
1538}
1539
1540template <class _Tp, class _Allocator>
1541void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001542vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001543{
1544 if (capacity() > size())
1545 {
1546#ifndef _LIBCPP_NO_EXCEPTIONS
1547 try
1548 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001549#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001550 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001551 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001552 __swap_out_circular_buffer(__v);
1553#ifndef _LIBCPP_NO_EXCEPTIONS
1554 }
1555 catch (...)
1556 {
1557 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001558#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001559 }
1560}
1561
1562template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001563template <class _Up>
1564void
1565#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1566vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1567#else
1568vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1569#endif
1570{
1571 allocator_type& __a = this->__alloc();
1572 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1573 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnantf619e232013-01-11 20:36:59 +00001574 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1575 __v.__end_++;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001576 __swap_out_circular_buffer(__v);
1577}
1578
1579template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001580inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001581void
1582vector<_Tp, _Allocator>::push_back(const_reference __x)
1583{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001584 if (this->__end_ != this->__end_cap())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001585 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001586 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001587 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001588 _VSTD::__to_raw_pointer(this->__end_), __x);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001589 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590 ++this->__end_;
1591 }
1592 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001593 __push_back_slow_path(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001594}
1595
Howard Hinnant73d21a42010-09-04 23:28:19 +00001596#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597
1598template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001599inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001600void
1601vector<_Tp, _Allocator>::push_back(value_type&& __x)
1602{
1603 if (this->__end_ < this->__end_cap())
1604 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001605 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001606 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001607 _VSTD::__to_raw_pointer(this->__end_),
1608 _VSTD::move(__x));
Kostya Serebryany497f9122014-09-02 23:43:38 +00001609 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001610 ++this->__end_;
1611 }
1612 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001613 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001614}
1615
Howard Hinnant73d21a42010-09-04 23:28:19 +00001616#ifndef _LIBCPP_HAS_NO_VARIADICS
1617
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618template <class _Tp, class _Allocator>
1619template <class... _Args>
1620void
Howard Hinnant0438ea22012-02-26 15:30:12 +00001621vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1622{
1623 allocator_type& __a = this->__alloc();
1624 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1625// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantf619e232013-01-11 20:36:59 +00001626 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1627 __v.__end_++;
Howard Hinnant0438ea22012-02-26 15:30:12 +00001628 __swap_out_circular_buffer(__v);
1629}
1630
1631template <class _Tp, class _Allocator>
1632template <class... _Args>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001633inline
Howard Hinnant0438ea22012-02-26 15:30:12 +00001634void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001635vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1636{
1637 if (this->__end_ < this->__end_cap())
1638 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001639 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001641 _VSTD::__to_raw_pointer(this->__end_),
1642 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001643 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001644 ++this->__end_;
1645 }
1646 else
Howard Hinnant0438ea22012-02-26 15:30:12 +00001647 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001648}
1649
Howard Hinnant73d21a42010-09-04 23:28:19 +00001650#endif // _LIBCPP_HAS_NO_VARIADICS
1651#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001652
1653template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001654inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655void
1656vector<_Tp, _Allocator>::pop_back()
1657{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001658 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001659 this->__destruct_at_end(this->__end_ - 1);
1660}
1661
1662template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001663inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001664typename vector<_Tp, _Allocator>::iterator
1665vector<_Tp, _Allocator>::erase(const_iterator __position)
1666{
Howard Hinnantabe26282011-09-16 17:29:17 +00001667#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001668 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1669 "vector::erase(iterator) called with an iterator not"
1670 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001671#endif
Howard Hinnant782da332013-03-25 22:12:26 +00001672 _LIBCPP_ASSERT(__position != end(),
1673 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001674 difference_type __ps = __position - cbegin();
1675 pointer __p = this->__begin_ + __ps;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001677 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001678 return __r;
1679}
1680
1681template <class _Tp, class _Allocator>
1682typename vector<_Tp, _Allocator>::iterator
1683vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1684{
Howard Hinnantabe26282011-09-16 17:29:17 +00001685#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001686 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1687 "vector::erase(iterator, iterator) called with an iterator not"
1688 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001689#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +00001690 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001691 pointer __p = this->__begin_ + (__first - begin());
1692 iterator __r = __make_iter(__p);
Howard Hinnantb4e67cf2013-04-18 15:02:57 +00001693 if (__first != __last)
1694 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001695 return __r;
1696}
1697
1698template <class _Tp, class _Allocator>
1699void
1700vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1701{
1702 pointer __old_last = this->__end_;
1703 difference_type __n = __old_last - __to;
1704 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1705 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001706 _VSTD::__to_raw_pointer(this->__end_),
1707 _VSTD::move(*__i));
1708 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001709}
1710
1711template <class _Tp, class _Allocator>
1712typename vector<_Tp, _Allocator>::iterator
1713vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1714{
Howard Hinnantabe26282011-09-16 17:29:17 +00001715#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001716 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1717 "vector::insert(iterator, x) called with an iterator not"
1718 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001719#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001720 pointer __p = this->__begin_ + (__position - begin());
1721 if (this->__end_ < this->__end_cap())
1722 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001723 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001724 if (__p == this->__end_)
1725 {
1726 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001727 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001728 ++this->__end_;
1729 }
1730 else
1731 {
1732 __move_range(__p, this->__end_, __p + 1);
1733 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1734 if (__p <= __xr && __xr < this->__end_)
1735 ++__xr;
1736 *__p = *__xr;
1737 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001738 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001739 }
1740 else
1741 {
1742 allocator_type& __a = this->__alloc();
1743 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1744 __v.push_back(__x);
1745 __p = __swap_out_circular_buffer(__v, __p);
1746 }
1747 return __make_iter(__p);
1748}
1749
Howard Hinnant73d21a42010-09-04 23:28:19 +00001750#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001751
1752template <class _Tp, class _Allocator>
1753typename vector<_Tp, _Allocator>::iterator
1754vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1755{
Howard Hinnantabe26282011-09-16 17:29:17 +00001756#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001757 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1758 "vector::insert(iterator, x) called with an iterator not"
1759 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001760#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001761 pointer __p = this->__begin_ + (__position - begin());
1762 if (this->__end_ < this->__end_cap())
1763 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001764 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001765 if (__p == this->__end_)
1766 {
1767 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001768 _VSTD::__to_raw_pointer(this->__end_),
1769 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001770 ++this->__end_;
1771 }
1772 else
1773 {
1774 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001775 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001776 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001777 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001778 }
1779 else
1780 {
1781 allocator_type& __a = this->__alloc();
1782 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001783 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001784 __p = __swap_out_circular_buffer(__v, __p);
1785 }
1786 return __make_iter(__p);
1787}
1788
Howard Hinnant73d21a42010-09-04 23:28:19 +00001789#ifndef _LIBCPP_HAS_NO_VARIADICS
1790
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001791template <class _Tp, class _Allocator>
1792template <class... _Args>
1793typename vector<_Tp, _Allocator>::iterator
1794vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1795{
Howard Hinnantabe26282011-09-16 17:29:17 +00001796#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001797 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1798 "vector::emplace(iterator, x) called with an iterator not"
1799 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001800#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001801 pointer __p = this->__begin_ + (__position - begin());
1802 if (this->__end_ < this->__end_cap())
1803 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001804 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805 if (__p == this->__end_)
1806 {
1807 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001808 _VSTD::__to_raw_pointer(this->__end_),
1809 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001810 ++this->__end_;
1811 }
1812 else
1813 {
Howard Hinnanta58402a2012-07-08 23:23:04 +00001814 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001815 __move_range(__p, this->__end_, __p + 1);
Howard Hinnanta58402a2012-07-08 23:23:04 +00001816 *__p = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001818 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001819 }
1820 else
1821 {
1822 allocator_type& __a = this->__alloc();
1823 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001824 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001825 __p = __swap_out_circular_buffer(__v, __p);
1826 }
1827 return __make_iter(__p);
1828}
1829
Howard Hinnant73d21a42010-09-04 23:28:19 +00001830#endif // _LIBCPP_HAS_NO_VARIADICS
1831#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001832
1833template <class _Tp, class _Allocator>
1834typename vector<_Tp, _Allocator>::iterator
1835vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1836{
Howard Hinnantabe26282011-09-16 17:29:17 +00001837#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001838 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1839 "vector::insert(iterator, n, x) called with an iterator not"
1840 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001841#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001842 pointer __p = this->__begin_ + (__position - begin());
1843 if (__n > 0)
1844 {
1845 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1846 {
1847 size_type __old_n = __n;
1848 pointer __old_last = this->__end_;
1849 if (__n > static_cast<size_type>(this->__end_ - __p))
1850 {
1851 size_type __cx = __n - (this->__end_ - __p);
1852 __construct_at_end(__cx, __x);
1853 __n -= __cx;
1854 }
1855 if (__n > 0)
1856 {
Eric Fiselierd7590952014-11-14 18:28:36 +00001857 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001858 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001859 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001860 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1861 if (__p <= __xr && __xr < this->__end_)
1862 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001863 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001864 }
1865 }
1866 else
1867 {
1868 allocator_type& __a = this->__alloc();
1869 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1870 __v.__construct_at_end(__n, __x);
1871 __p = __swap_out_circular_buffer(__v, __p);
1872 }
1873 }
1874 return __make_iter(__p);
1875}
1876
1877template <class _Tp, class _Allocator>
1878template <class _InputIterator>
1879typename enable_if
1880<
1881 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001882 !__is_forward_iterator<_InputIterator>::value &&
1883 is_constructible<
1884 _Tp,
1885 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001886 typename vector<_Tp, _Allocator>::iterator
1887>::type
1888vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1889{
Howard Hinnantabe26282011-09-16 17:29:17 +00001890#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001891 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1892 "vector::insert(iterator, range) called with an iterator not"
1893 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001894#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001895 difference_type __off = __position - begin();
1896 pointer __p = this->__begin_ + __off;
1897 allocator_type& __a = this->__alloc();
1898 pointer __old_last = this->__end_;
1899 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1900 {
Eric Fiselier7d439a42015-07-18 18:22:12 +00001901 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001902 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903 *__first);
1904 ++this->__end_;
Eric Fiselier7d439a42015-07-18 18:22:12 +00001905 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001906 }
1907 __split_buffer<value_type, allocator_type&> __v(__a);
1908 if (__first != __last)
1909 {
1910#ifndef _LIBCPP_NO_EXCEPTIONS
1911 try
1912 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001913#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001914 __v.__construct_at_end(__first, __last);
1915 difference_type __old_size = __old_last - this->__begin_;
1916 difference_type __old_p = __p - this->__begin_;
1917 reserve(__recommend(size() + __v.size()));
1918 __p = this->__begin_ + __old_p;
1919 __old_last = this->__begin_ + __old_size;
1920#ifndef _LIBCPP_NO_EXCEPTIONS
1921 }
1922 catch (...)
1923 {
1924 erase(__make_iter(__old_last), end());
1925 throw;
1926 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001927#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001928 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001929 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant0442b122011-09-16 18:41:29 +00001930 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1931 make_move_iterator(__v.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001932 return begin() + __off;
1933}
1934
1935template <class _Tp, class _Allocator>
1936template <class _ForwardIterator>
1937typename enable_if
1938<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001939 __is_forward_iterator<_ForwardIterator>::value &&
1940 is_constructible<
1941 _Tp,
1942 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001943 typename vector<_Tp, _Allocator>::iterator
1944>::type
1945vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1946{
Howard Hinnantabe26282011-09-16 17:29:17 +00001947#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001948 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1949 "vector::insert(iterator, range) called with an iterator not"
1950 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001951#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001952 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00001953 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001954 if (__n > 0)
1955 {
1956 if (__n <= this->__end_cap() - this->__end_)
1957 {
1958 size_type __old_n = __n;
1959 pointer __old_last = this->__end_;
1960 _ForwardIterator __m = __last;
1961 difference_type __dx = this->__end_ - __p;
1962 if (__n > __dx)
1963 {
1964 __m = __first;
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001965 difference_type __diff = this->__end_ - __p;
1966 _VSTD::advance(__m, __diff);
1967 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001968 __n = __dx;
1969 }
1970 if (__n > 0)
1971 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001972 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001973 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001974 __annotator.__done();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001975 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001976 }
1977 }
1978 else
1979 {
1980 allocator_type& __a = this->__alloc();
1981 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1982 __v.__construct_at_end(__first, __last);
1983 __p = __swap_out_circular_buffer(__v, __p);
1984 }
1985 }
1986 return __make_iter(__p);
1987}
1988
1989template <class _Tp, class _Allocator>
1990void
1991vector<_Tp, _Allocator>::resize(size_type __sz)
1992{
1993 size_type __cs = size();
1994 if (__cs < __sz)
1995 this->__append(__sz - __cs);
1996 else if (__cs > __sz)
1997 this->__destruct_at_end(this->__begin_ + __sz);
1998}
1999
2000template <class _Tp, class _Allocator>
2001void
2002vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2003{
2004 size_type __cs = size();
2005 if (__cs < __sz)
2006 this->__append(__sz - __cs, __x);
2007 else if (__cs > __sz)
2008 this->__destruct_at_end(this->__begin_ + __sz);
2009}
2010
2011template <class _Tp, class _Allocator>
2012void
2013vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow7d914d12015-07-13 20:04:56 +00002014#if _LIBCPP_STD_VER >= 14
2015 _NOEXCEPT
2016#else
2017 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2018 __is_nothrow_swappable<allocator_type>::value)
2019#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002020{
Howard Hinnant7a563db2011-09-14 18:33:51 +00002021 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2022 this->__alloc() == __x.__alloc(),
2023 "vector::swap: Either propagate_on_container_swap must be true"
2024 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +00002025 _VSTD::swap(this->__begin_, __x.__begin_);
2026 _VSTD::swap(this->__end_, __x.__end_);
2027 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Marshall Clow7d914d12015-07-13 20:04:56 +00002028 __swap_allocator(this->__alloc(), __x.__alloc(),
2029 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantabe26282011-09-16 17:29:17 +00002030#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002031 __get_db()->swap(this, &__x);
Howard Hinnantabe26282011-09-16 17:29:17 +00002032#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002033}
2034
Howard Hinnant324bb032010-08-22 00:02:43 +00002035template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002036bool
2037vector<_Tp, _Allocator>::__invariants() const
2038{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002039 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002040 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002041 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002042 return false;
2043 }
2044 else
2045 {
2046 if (this->__begin_ > this->__end_)
2047 return false;
2048 if (this->__begin_ == this->__end_cap())
2049 return false;
2050 if (this->__end_ > this->__end_cap())
2051 return false;
2052 }
2053 return true;
2054}
2055
Howard Hinnantabe26282011-09-16 17:29:17 +00002056#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002057
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002058template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:51 +00002059bool
2060vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2061{
2062 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2063}
2064
2065template <class _Tp, class _Allocator>
2066bool
2067vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2068{
2069 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2070}
2071
2072template <class _Tp, class _Allocator>
2073bool
2074vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2075{
2076 const_pointer __p = __i->base() + __n;
2077 return this->__begin_ <= __p && __p <= this->__end_;
2078}
2079
2080template <class _Tp, class _Allocator>
2081bool
2082vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2083{
2084 const_pointer __p = __i->base() + __n;
2085 return this->__begin_ <= __p && __p < this->__end_;
2086}
2087
Howard Hinnantabe26282011-09-16 17:29:17 +00002088#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002089
2090template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002091inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002092void
2093vector<_Tp, _Allocator>::__invalidate_all_iterators()
2094{
Howard Hinnantabe26282011-09-16 17:29:17 +00002095#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002096 __get_db()->__invalidate_all(this);
Howard Hinnantabe26282011-09-16 17:29:17 +00002097#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002098}
2099
2100// vector<bool>
2101
2102template <class _Allocator> class vector<bool, _Allocator>;
2103
2104template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2105
2106template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002107struct __has_storage_type<vector<bool, _Allocator> >
2108{
2109 static const bool value = true;
2110};
2111
2112template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002113class _LIBCPP_TYPE_VIS_ONLY vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002114 : private __vector_base_common<true>
2115{
2116public:
2117 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00002118 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002119 typedef _Allocator allocator_type;
2120 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002121 typedef typename __alloc_traits::size_type size_type;
2122 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantf867f632012-05-07 16:50:38 +00002123 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002124 typedef __bit_iterator<vector, false> pointer;
2125 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002126 typedef pointer iterator;
2127 typedef const_pointer const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002128 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2129 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130
2131private:
Marshall Clow66302c62015-04-07 05:21:38 +00002132 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002133 typedef allocator_traits<__storage_allocator> __storage_traits;
2134 typedef typename __storage_traits::pointer __storage_pointer;
2135 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2136
2137 __storage_pointer __begin_;
2138 size_type __size_;
2139 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002140public:
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002141 typedef __bit_reference<vector> reference;
2142 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002143private:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002144 _LIBCPP_INLINE_VISIBILITY
2145 size_type& __cap() _NOEXCEPT
2146 {return __cap_alloc_.first();}
2147 _LIBCPP_INLINE_VISIBILITY
2148 const size_type& __cap() const _NOEXCEPT
2149 {return __cap_alloc_.first();}
2150 _LIBCPP_INLINE_VISIBILITY
2151 __storage_allocator& __alloc() _NOEXCEPT
2152 {return __cap_alloc_.second();}
2153 _LIBCPP_INLINE_VISIBILITY
2154 const __storage_allocator& __alloc() const _NOEXCEPT
2155 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002156
2157 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2158
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002159 _LIBCPP_INLINE_VISIBILITY
2160 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002161 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002162 _LIBCPP_INLINE_VISIBILITY
2163 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002164 {return (__n - 1) / __bits_per_word + 1;}
2165
2166public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002167 _LIBCPP_INLINE_VISIBILITY
Marshall Clowc912c0c2015-06-04 02:05:41 +00002168 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow127db912015-06-04 00:10:20 +00002169
2170 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2171#if _LIBCPP_STD_VER <= 14
2172 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2173#else
2174 _NOEXCEPT;
2175#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002176 ~vector();
2177 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59 +00002178#if _LIBCPP_STD_VER > 11
2179 explicit vector(size_type __n, const allocator_type& __a);
2180#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002181 vector(size_type __n, const value_type& __v);
2182 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2183 template <class _InputIterator>
2184 vector(_InputIterator __first, _InputIterator __last,
2185 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2186 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2187 template <class _InputIterator>
2188 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2189 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2190 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2191 template <class _ForwardIterator>
2192 vector(_ForwardIterator __first, _ForwardIterator __last,
2193 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2194 template <class _ForwardIterator>
2195 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2196 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2197
2198 vector(const vector& __v);
2199 vector(const vector& __v, const allocator_type& __a);
2200 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00002201#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002202 vector(initializer_list<value_type> __il);
2203 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00002204#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002205
Howard Hinnant73d21a42010-09-04 23:28:19 +00002206#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002207 _LIBCPP_INLINE_VISIBILITY
2208 vector(vector&& __v)
Marshall Clow119ed472015-07-14 14:46:32 +00002209#if _LIBCPP_STD_VER > 14
2210 _NOEXCEPT;
2211#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002212 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow119ed472015-07-14 14:46:32 +00002213#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002214 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002215 _LIBCPP_INLINE_VISIBILITY
2216 vector& operator=(vector&& __v)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002217 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnant73d21a42010-09-04 23:28:19 +00002218#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +00002219#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002220 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002221 vector& operator=(initializer_list<value_type> __il)
2222 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00002223#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002224
2225 template <class _InputIterator>
2226 typename enable_if
2227 <
2228 __is_input_iterator<_InputIterator>::value &&
2229 !__is_forward_iterator<_InputIterator>::value,
2230 void
2231 >::type
2232 assign(_InputIterator __first, _InputIterator __last);
2233 template <class _ForwardIterator>
2234 typename enable_if
2235 <
2236 __is_forward_iterator<_ForwardIterator>::value,
2237 void
2238 >::type
2239 assign(_ForwardIterator __first, _ForwardIterator __last);
2240
2241 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +00002242#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002244 void assign(initializer_list<value_type> __il)
2245 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002246#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002247
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002248 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002249 {return allocator_type(this->__alloc());}
2250
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002251 size_type max_size() const _NOEXCEPT;
2252 _LIBCPP_INLINE_VISIBILITY
2253 size_type capacity() const _NOEXCEPT
2254 {return __internal_cap_to_external(__cap());}
2255 _LIBCPP_INLINE_VISIBILITY
2256 size_type size() const _NOEXCEPT
2257 {return __size_;}
2258 _LIBCPP_INLINE_VISIBILITY
2259 bool empty() const _NOEXCEPT
2260 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002261 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002262 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002263
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002264 _LIBCPP_INLINE_VISIBILITY
2265 iterator begin() _NOEXCEPT
2266 {return __make_iter(0);}
2267 _LIBCPP_INLINE_VISIBILITY
2268 const_iterator begin() const _NOEXCEPT
2269 {return __make_iter(0);}
2270 _LIBCPP_INLINE_VISIBILITY
2271 iterator end() _NOEXCEPT
2272 {return __make_iter(__size_);}
2273 _LIBCPP_INLINE_VISIBILITY
2274 const_iterator end() const _NOEXCEPT
2275 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002276
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002277 _LIBCPP_INLINE_VISIBILITY
2278 reverse_iterator rbegin() _NOEXCEPT
2279 {return reverse_iterator(end());}
2280 _LIBCPP_INLINE_VISIBILITY
2281 const_reverse_iterator rbegin() const _NOEXCEPT
2282 {return const_reverse_iterator(end());}
2283 _LIBCPP_INLINE_VISIBILITY
2284 reverse_iterator rend() _NOEXCEPT
2285 {return reverse_iterator(begin());}
2286 _LIBCPP_INLINE_VISIBILITY
2287 const_reverse_iterator rend() const _NOEXCEPT
2288 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002289
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002290 _LIBCPP_INLINE_VISIBILITY
2291 const_iterator cbegin() const _NOEXCEPT
2292 {return __make_iter(0);}
2293 _LIBCPP_INLINE_VISIBILITY
2294 const_iterator cend() const _NOEXCEPT
2295 {return __make_iter(__size_);}
2296 _LIBCPP_INLINE_VISIBILITY
2297 const_reverse_iterator crbegin() const _NOEXCEPT
2298 {return rbegin();}
2299 _LIBCPP_INLINE_VISIBILITY
2300 const_reverse_iterator crend() const _NOEXCEPT
2301 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002302
2303 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2304 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2305 reference at(size_type __n);
2306 const_reference at(size_type __n) const;
2307
2308 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2309 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2310 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2311 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2312
2313 void push_back(const value_type& __x);
Marshall Clow198a2a52013-08-13 23:54:12 +00002314#if _LIBCPP_STD_VER > 11
2315 template <class... _Args>
2316 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2317 { push_back ( value_type ( _VSTD::forward<_Args>(__args)... )); }
2318#endif
2319
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002320 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2321
Marshall Clow198a2a52013-08-13 23:54:12 +00002322#if _LIBCPP_STD_VER > 11
2323 template <class... _Args>
2324 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2325 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2326#endif
2327
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002328 iterator insert(const_iterator __position, const value_type& __x);
2329 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2330 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2331 template <class _InputIterator>
2332 typename enable_if
2333 <
2334 __is_input_iterator <_InputIterator>::value &&
2335 !__is_forward_iterator<_InputIterator>::value,
2336 iterator
2337 >::type
2338 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2339 template <class _ForwardIterator>
2340 typename enable_if
2341 <
2342 __is_forward_iterator<_ForwardIterator>::value,
2343 iterator
2344 >::type
2345 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00002346#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002347 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002348 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2349 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002350#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002351
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002352 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002353 iterator erase(const_iterator __first, const_iterator __last);
2354
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002355 _LIBCPP_INLINE_VISIBILITY
2356 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002357
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002358 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +00002359#if _LIBCPP_STD_VER >= 14
2360 _NOEXCEPT;
2361#else
2362 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2363 __is_nothrow_swappable<allocator_type>::value);
2364#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002365
2366 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002367 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002368
2369 bool __invariants() const;
2370
2371private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002372 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002373 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002374 void deallocate() _NOEXCEPT;
2375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00002376 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow0d1965d2014-07-28 15:02:42 +00002377 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002378 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2379 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002380 template <class _ForwardIterator>
2381 typename enable_if
2382 <
2383 __is_forward_iterator<_ForwardIterator>::value,
2384 void
2385 >::type
2386 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2387 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002388 _LIBCPP_INLINE_VISIBILITY
2389 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002390 {return 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 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002393 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002394 _LIBCPP_INLINE_VISIBILITY
2395 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002396 {return 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 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002399 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002400 _LIBCPP_INLINE_VISIBILITY
2401 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002402 {return begin() + (__p - cbegin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002403
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002405 void __copy_assign_alloc(const vector& __v)
2406 {__copy_assign_alloc(__v, integral_constant<bool,
2407 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002409 void __copy_assign_alloc(const vector& __c, true_type)
2410 {
2411 if (__alloc() != __c.__alloc())
2412 deallocate();
2413 __alloc() = __c.__alloc();
2414 }
2415
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002417 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002418 {}
2419
2420 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002421 void __move_assign(vector& __c, true_type)
2422 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002424 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002425 _NOEXCEPT_(
2426 !__storage_traits::propagate_on_container_move_assignment::value ||
2427 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002428 {__move_assign_alloc(__c, integral_constant<bool,
2429 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00002431 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002432 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002433 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002434 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002435 }
2436
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002437 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002438 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002439 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002440 {}
2441
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002442 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002443
2444 friend class __bit_reference<vector>;
2445 friend class __bit_const_reference<vector>;
2446 friend class __bit_iterator<vector, false>;
2447 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:18 +00002448 friend struct __bit_array<vector>;
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002449 friend struct _LIBCPP_TYPE_VIS_ONLY hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002450};
2451
2452template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002453inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002454void
2455vector<bool, _Allocator>::__invalidate_all_iterators()
2456{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002457}
2458
2459// Allocate space for __n objects
2460// throws length_error if __n > max_size()
2461// throws (probably bad_alloc) if memory run out
2462// Precondition: __begin_ == __end_ == __cap() == 0
2463// Precondition: __n > 0
2464// Postcondition: capacity() == __n
2465// Postcondition: size() == 0
2466template <class _Allocator>
2467void
2468vector<bool, _Allocator>::allocate(size_type __n)
2469{
2470 if (__n > max_size())
2471 this->__throw_length_error();
2472 __n = __external_cap_to_internal(__n);
2473 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2474 this->__size_ = 0;
2475 this->__cap() = __n;
2476}
2477
2478template <class _Allocator>
2479void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002480vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002481{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002482 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002483 {
2484 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2485 __invalidate_all_iterators();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002486 this->__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002487 this->__size_ = this->__cap() = 0;
2488 }
2489}
2490
2491template <class _Allocator>
2492typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002493vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002494{
2495 size_type __amax = __storage_traits::max_size(__alloc());
2496 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2497 if (__nmax / __bits_per_word <= __amax)
2498 return __nmax;
2499 return __internal_cap_to_external(__amax);
2500}
2501
2502// Precondition: __new_size > capacity()
2503template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002504inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002505typename vector<bool, _Allocator>::size_type
2506vector<bool, _Allocator>::__recommend(size_type __new_size) const
2507{
2508 const size_type __ms = max_size();
2509 if (__new_size > __ms)
2510 this->__throw_length_error();
2511 const size_type __cap = capacity();
2512 if (__cap >= __ms / 2)
2513 return __ms;
Howard Hinnant7f764502013-08-14 18:00:20 +00002514 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002515}
2516
2517// Default constructs __n objects starting at __end_
2518// Precondition: __n > 0
2519// Precondition: size() + __n <= capacity()
2520// Postcondition: size() == size() + __n
2521template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002522inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002523void
2524vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2525{
2526 size_type __old_size = this->__size_;
2527 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002528 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002529}
2530
2531template <class _Allocator>
2532template <class _ForwardIterator>
2533typename enable_if
2534<
2535 __is_forward_iterator<_ForwardIterator>::value,
2536 void
2537>::type
2538vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2539{
2540 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002541 this->__size_ += _VSTD::distance(__first, __last);
2542 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002543}
2544
2545template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002546inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002547vector<bool, _Allocator>::vector()
Marshall Clowc912c0c2015-06-04 02:05:41 +00002548 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002549 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002550 __size_(0),
2551 __cap_alloc_(0)
2552{
2553}
2554
2555template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002556inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002557vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow127db912015-06-04 00:10:20 +00002558#if _LIBCPP_STD_VER <= 14
2559 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2560#else
2561 _NOEXCEPT
2562#endif
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002563 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002564 __size_(0),
2565 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2566{
2567}
2568
2569template <class _Allocator>
2570vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002571 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002572 __size_(0),
2573 __cap_alloc_(0)
2574{
2575 if (__n > 0)
2576 {
2577 allocate(__n);
2578 __construct_at_end(__n, false);
2579 }
2580}
2581
Marshall Clowa49a2c92013-09-14 00:47:59 +00002582#if _LIBCPP_STD_VER > 11
2583template <class _Allocator>
2584vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2585 : __begin_(nullptr),
2586 __size_(0),
2587 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2588{
2589 if (__n > 0)
2590 {
2591 allocate(__n);
2592 __construct_at_end(__n, false);
2593 }
2594}
2595#endif
2596
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002597template <class _Allocator>
2598vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002599 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002600 __size_(0),
2601 __cap_alloc_(0)
2602{
2603 if (__n > 0)
2604 {
2605 allocate(__n);
2606 __construct_at_end(__n, __x);
2607 }
2608}
2609
2610template <class _Allocator>
2611vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002612 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002613 __size_(0),
2614 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2615{
2616 if (__n > 0)
2617 {
2618 allocate(__n);
2619 __construct_at_end(__n, __x);
2620 }
2621}
2622
2623template <class _Allocator>
2624template <class _InputIterator>
2625vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2626 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2627 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002628 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002629 __size_(0),
2630 __cap_alloc_(0)
2631{
2632#ifndef _LIBCPP_NO_EXCEPTIONS
2633 try
2634 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002635#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002636 for (; __first != __last; ++__first)
2637 push_back(*__first);
2638#ifndef _LIBCPP_NO_EXCEPTIONS
2639 }
2640 catch (...)
2641 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002642 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002643 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2644 __invalidate_all_iterators();
2645 throw;
2646 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002647#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002648}
2649
2650template <class _Allocator>
2651template <class _InputIterator>
2652vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2653 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2654 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002655 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002656 __size_(0),
2657 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2658{
2659#ifndef _LIBCPP_NO_EXCEPTIONS
2660 try
2661 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002662#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002663 for (; __first != __last; ++__first)
2664 push_back(*__first);
2665#ifndef _LIBCPP_NO_EXCEPTIONS
2666 }
2667 catch (...)
2668 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002669 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002670 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2671 __invalidate_all_iterators();
2672 throw;
2673 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002674#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002675}
2676
2677template <class _Allocator>
2678template <class _ForwardIterator>
2679vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2680 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002681 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002682 __size_(0),
2683 __cap_alloc_(0)
2684{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002685 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002686 if (__n > 0)
2687 {
2688 allocate(__n);
2689 __construct_at_end(__first, __last);
2690 }
2691}
2692
2693template <class _Allocator>
2694template <class _ForwardIterator>
2695vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2696 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002697 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002698 __size_(0),
2699 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2700{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002701 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002702 if (__n > 0)
2703 {
2704 allocate(__n);
2705 __construct_at_end(__first, __last);
2706 }
2707}
2708
Howard Hinnante3e32912011-08-12 21:56:02 +00002709#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2710
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002711template <class _Allocator>
2712vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002713 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002714 __size_(0),
2715 __cap_alloc_(0)
2716{
2717 size_type __n = static_cast<size_type>(__il.size());
2718 if (__n > 0)
2719 {
2720 allocate(__n);
2721 __construct_at_end(__il.begin(), __il.end());
2722 }
2723}
2724
2725template <class _Allocator>
2726vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002727 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002728 __size_(0),
2729 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2730{
2731 size_type __n = static_cast<size_type>(__il.size());
2732 if (__n > 0)
2733 {
2734 allocate(__n);
2735 __construct_at_end(__il.begin(), __il.end());
2736 }
2737}
2738
Howard Hinnante3e32912011-08-12 21:56:02 +00002739#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2740
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002741template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002742vector<bool, _Allocator>::~vector()
2743{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002744 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002745 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002746 __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002747}
2748
2749template <class _Allocator>
2750vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002751 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002752 __size_(0),
2753 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2754{
2755 if (__v.size() > 0)
2756 {
2757 allocate(__v.size());
2758 __construct_at_end(__v.begin(), __v.end());
2759 }
2760}
2761
2762template <class _Allocator>
2763vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002764 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002765 __size_(0),
2766 __cap_alloc_(0, __a)
2767{
2768 if (__v.size() > 0)
2769 {
2770 allocate(__v.size());
2771 __construct_at_end(__v.begin(), __v.end());
2772 }
2773}
2774
2775template <class _Allocator>
2776vector<bool, _Allocator>&
2777vector<bool, _Allocator>::operator=(const vector& __v)
2778{
2779 if (this != &__v)
2780 {
2781 __copy_assign_alloc(__v);
2782 if (__v.__size_)
2783 {
2784 if (__v.__size_ > capacity())
2785 {
2786 deallocate();
2787 allocate(__v.__size_);
2788 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002789 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002790 }
2791 __size_ = __v.__size_;
2792 }
2793 return *this;
2794}
2795
Howard Hinnant73d21a42010-09-04 23:28:19 +00002796#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2797
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002798template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002799inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002800vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clow119ed472015-07-14 14:46:32 +00002801#if _LIBCPP_STD_VER > 14
2802 _NOEXCEPT
2803#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002804 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow119ed472015-07-14 14:46:32 +00002805#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002806 : __begin_(__v.__begin_),
2807 __size_(__v.__size_),
2808 __cap_alloc_(__v.__cap_alloc_)
2809{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002810 __v.__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002811 __v.__size_ = 0;
2812 __v.__cap() = 0;
2813}
2814
2815template <class _Allocator>
2816vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002817 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002818 __size_(0),
2819 __cap_alloc_(0, __a)
2820{
2821 if (__a == allocator_type(__v.__alloc()))
2822 {
2823 this->__begin_ = __v.__begin_;
2824 this->__size_ = __v.__size_;
2825 this->__cap() = __v.__cap();
2826 __v.__begin_ = nullptr;
2827 __v.__cap() = __v.__size_ = 0;
2828 }
2829 else if (__v.size() > 0)
2830 {
2831 allocate(__v.size());
2832 __construct_at_end(__v.begin(), __v.end());
2833 }
2834}
2835
2836template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002837inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002838vector<bool, _Allocator>&
2839vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002840 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002841{
2842 __move_assign(__v, integral_constant<bool,
2843 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002844 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002845}
2846
2847template <class _Allocator>
2848void
2849vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2850{
2851 if (__alloc() != __c.__alloc())
2852 assign(__c.begin(), __c.end());
2853 else
2854 __move_assign(__c, true_type());
2855}
2856
2857template <class _Allocator>
2858void
2859vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002860 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002861{
2862 deallocate();
Marshall Clowdb5e54d2014-07-21 15:15:15 +00002863 __move_assign_alloc(__c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002864 this->__begin_ = __c.__begin_;
2865 this->__size_ = __c.__size_;
2866 this->__cap() = __c.__cap();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002867 __c.__begin_ = nullptr;
2868 __c.__cap() = __c.__size_ = 0;
2869}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002870
2871#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002872
2873template <class _Allocator>
2874void
2875vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2876{
2877 __size_ = 0;
2878 if (__n > 0)
2879 {
2880 size_type __c = capacity();
2881 if (__n <= __c)
2882 __size_ = __n;
2883 else
2884 {
2885 vector __v(__alloc());
2886 __v.reserve(__recommend(__n));
2887 __v.__size_ = __n;
2888 swap(__v);
2889 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002890 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002891 }
2892}
2893
2894template <class _Allocator>
2895template <class _InputIterator>
2896typename enable_if
2897<
2898 __is_input_iterator<_InputIterator>::value &&
2899 !__is_forward_iterator<_InputIterator>::value,
2900 void
2901>::type
2902vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2903{
2904 clear();
2905 for (; __first != __last; ++__first)
2906 push_back(*__first);
2907}
2908
2909template <class _Allocator>
2910template <class _ForwardIterator>
2911typename enable_if
2912<
2913 __is_forward_iterator<_ForwardIterator>::value,
2914 void
2915>::type
2916vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2917{
2918 clear();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002919 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002920 if (__n)
2921 {
2922 if (__n > capacity())
2923 {
2924 deallocate();
2925 allocate(__n);
2926 }
2927 __construct_at_end(__first, __last);
2928 }
2929}
2930
2931template <class _Allocator>
2932void
2933vector<bool, _Allocator>::reserve(size_type __n)
2934{
2935 if (__n > capacity())
2936 {
2937 vector __v(this->__alloc());
2938 __v.allocate(__n);
2939 __v.__construct_at_end(this->begin(), this->end());
2940 swap(__v);
2941 __invalidate_all_iterators();
2942 }
2943}
2944
2945template <class _Allocator>
2946void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002947vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002948{
2949 if (__external_cap_to_internal(size()) > __cap())
2950 {
2951#ifndef _LIBCPP_NO_EXCEPTIONS
2952 try
2953 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002954#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002955 vector(*this, allocator_type(__alloc())).swap(*this);
2956#ifndef _LIBCPP_NO_EXCEPTIONS
2957 }
2958 catch (...)
2959 {
2960 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002961#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002962 }
2963}
2964
2965template <class _Allocator>
2966typename vector<bool, _Allocator>::reference
2967vector<bool, _Allocator>::at(size_type __n)
2968{
2969 if (__n >= size())
2970 this->__throw_out_of_range();
2971 return (*this)[__n];
2972}
2973
2974template <class _Allocator>
2975typename vector<bool, _Allocator>::const_reference
2976vector<bool, _Allocator>::at(size_type __n) const
2977{
2978 if (__n >= size())
2979 this->__throw_out_of_range();
2980 return (*this)[__n];
2981}
2982
2983template <class _Allocator>
2984void
2985vector<bool, _Allocator>::push_back(const value_type& __x)
2986{
2987 if (this->__size_ == this->capacity())
2988 reserve(__recommend(this->__size_ + 1));
2989 ++this->__size_;
2990 back() = __x;
2991}
2992
2993template <class _Allocator>
2994typename vector<bool, _Allocator>::iterator
2995vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2996{
2997 iterator __r;
2998 if (size() < capacity())
2999 {
3000 const_iterator __old_end = end();
3001 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003002 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003003 __r = __const_iterator_cast(__position);
3004 }
3005 else
3006 {
3007 vector __v(__alloc());
3008 __v.reserve(__recommend(__size_ + 1));
3009 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003010 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3011 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003012 swap(__v);
3013 }
3014 *__r = __x;
3015 return __r;
3016}
3017
3018template <class _Allocator>
3019typename vector<bool, _Allocator>::iterator
3020vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3021{
3022 iterator __r;
3023 size_type __c = capacity();
3024 if (__n <= __c && size() <= __c - __n)
3025 {
3026 const_iterator __old_end = end();
3027 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003028 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003029 __r = __const_iterator_cast(__position);
3030 }
3031 else
3032 {
3033 vector __v(__alloc());
3034 __v.reserve(__recommend(__size_ + __n));
3035 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003036 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3037 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003038 swap(__v);
3039 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003040 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003041 return __r;
3042}
3043
3044template <class _Allocator>
3045template <class _InputIterator>
3046typename enable_if
3047<
3048 __is_input_iterator <_InputIterator>::value &&
3049 !__is_forward_iterator<_InputIterator>::value,
3050 typename vector<bool, _Allocator>::iterator
3051>::type
3052vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3053{
3054 difference_type __off = __position - begin();
3055 iterator __p = __const_iterator_cast(__position);
3056 iterator __old_end = end();
3057 for (; size() != capacity() && __first != __last; ++__first)
3058 {
3059 ++this->__size_;
3060 back() = *__first;
3061 }
3062 vector __v(__alloc());
3063 if (__first != __last)
3064 {
3065#ifndef _LIBCPP_NO_EXCEPTIONS
3066 try
3067 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003068#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003069 __v.assign(__first, __last);
3070 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3071 difference_type __old_p = __p - begin();
3072 reserve(__recommend(size() + __v.size()));
3073 __p = begin() + __old_p;
3074 __old_end = begin() + __old_size;
3075#ifndef _LIBCPP_NO_EXCEPTIONS
3076 }
3077 catch (...)
3078 {
3079 erase(__old_end, end());
3080 throw;
3081 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003082#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003083 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003084 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003085 insert(__p, __v.begin(), __v.end());
3086 return begin() + __off;
3087}
3088
3089template <class _Allocator>
3090template <class _ForwardIterator>
3091typename enable_if
3092<
3093 __is_forward_iterator<_ForwardIterator>::value,
3094 typename vector<bool, _Allocator>::iterator
3095>::type
3096vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3097{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003098 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003099 iterator __r;
3100 size_type __c = capacity();
3101 if (__n <= __c && size() <= __c - __n)
3102 {
3103 const_iterator __old_end = end();
3104 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003105 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003106 __r = __const_iterator_cast(__position);
3107 }
3108 else
3109 {
3110 vector __v(__alloc());
3111 __v.reserve(__recommend(__size_ + __n));
3112 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003113 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3114 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003115 swap(__v);
3116 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003117 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003118 return __r;
3119}
3120
3121template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003122inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003123typename vector<bool, _Allocator>::iterator
3124vector<bool, _Allocator>::erase(const_iterator __position)
3125{
3126 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003127 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003128 --__size_;
3129 return __r;
3130}
3131
3132template <class _Allocator>
3133typename vector<bool, _Allocator>::iterator
3134vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3135{
3136 iterator __r = __const_iterator_cast(__first);
3137 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003138 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003139 __size_ -= __d;
3140 return __r;
3141}
3142
3143template <class _Allocator>
3144void
3145vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow7d914d12015-07-13 20:04:56 +00003146#if _LIBCPP_STD_VER >= 14
3147 _NOEXCEPT
3148#else
3149 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3150 __is_nothrow_swappable<allocator_type>::value)
3151#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003152{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003153 _VSTD::swap(this->__begin_, __x.__begin_);
3154 _VSTD::swap(this->__size_, __x.__size_);
3155 _VSTD::swap(this->__cap(), __x.__cap());
Marshall Clow7d914d12015-07-13 20:04:56 +00003156 __swap_allocator(this->__alloc(), __x.__alloc(),
3157 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003158}
3159
Howard Hinnant324bb032010-08-22 00:02:43 +00003160template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003161void
3162vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3163{
3164 size_type __cs = size();
3165 if (__cs < __sz)
3166 {
3167 iterator __r;
3168 size_type __c = capacity();
3169 size_type __n = __sz - __cs;
3170 if (__n <= __c && __cs <= __c - __n)
3171 {
3172 __r = end();
3173 __size_ += __n;
3174 }
3175 else
3176 {
3177 vector __v(__alloc());
3178 __v.reserve(__recommend(__size_ + __n));
3179 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003180 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003181 swap(__v);
3182 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003183 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003184 }
3185 else
3186 __size_ = __sz;
3187}
3188
Howard Hinnant324bb032010-08-22 00:02:43 +00003189template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003190void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003191vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003192{
3193 // do middle whole words
3194 size_type __n = __size_;
3195 __storage_pointer __p = __begin_;
3196 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3197 *__p = ~*__p;
3198 // do last partial word
3199 if (__n > 0)
3200 {
3201 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3202 __storage_type __b = *__p & __m;
3203 *__p &= ~__m;
3204 *__p |= ~__b & __m;
3205 }
3206}
3207
Howard Hinnant324bb032010-08-22 00:02:43 +00003208template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003209bool
3210vector<bool, _Allocator>::__invariants() const
3211{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00003212 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003213 {
3214 if (this->__size_ != 0 || this->__cap() != 0)
3215 return false;
3216 }
3217 else
3218 {
3219 if (this->__cap() == 0)
3220 return false;
3221 if (this->__size_ > this->capacity())
3222 return false;
3223 }
3224 return true;
3225}
3226
Howard Hinnant324bb032010-08-22 00:02:43 +00003227template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003228size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003229vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003230{
3231 size_t __h = 0;
3232 // do middle whole words
3233 size_type __n = __size_;
3234 __storage_pointer __p = __begin_;
3235 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3236 __h ^= *__p;
3237 // do last partial word
3238 if (__n > 0)
3239 {
3240 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3241 __h ^= *__p & __m;
3242 }
3243 return __h;
3244}
3245
3246template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003247struct _LIBCPP_TYPE_VIS_ONLY hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003248 : public unary_function<vector<bool, _Allocator>, size_t>
3249{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00003250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003251 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003252 {return __vec.__hash_code();}
3253};
3254
3255template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003256inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003257bool
3258operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3259{
3260 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003261 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003262}
3263
3264template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003265inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003266bool
3267operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3268{
3269 return !(__x == __y);
3270}
3271
3272template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003273inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003274bool
3275operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3276{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003277 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003278}
3279
3280template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003281inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003282bool
3283operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3284{
3285 return __y < __x;
3286}
3287
3288template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003289inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003290bool
3291operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3292{
3293 return !(__x < __y);
3294}
3295
3296template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003297inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003298bool
3299operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3300{
3301 return !(__y < __x);
3302}
3303
3304template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003305inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003306void
3307swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003308 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003309{
3310 __x.swap(__y);
3311}
3312
3313_LIBCPP_END_NAMESPACE_STD
3314
3315#endif // _LIBCPP_VECTOR