blob: 6ac78d5d50b5b8eca9392ee1b554004bf8f7cbc6 [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(
54 allocator_type::propagate_on_container_move_assignment::value &&
55 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056 vector& operator=(initializer_list<value_type> il);
57 template <class InputIterator>
58 void assign(InputIterator first, InputIterator last);
59 void assign(size_type n, const value_type& u);
60 void assign(initializer_list<value_type> il);
61
Howard Hinnantd1d27a42011-06-03 19:40:40 +000062 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063
Howard Hinnantd1d27a42011-06-03 19:40:40 +000064 iterator begin() noexcept;
65 const_iterator begin() const noexcept;
66 iterator end() noexcept;
67 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000068
Howard Hinnantd1d27a42011-06-03 19:40:40 +000069 reverse_iterator rbegin() noexcept;
70 const_reverse_iterator rbegin() const noexcept;
71 reverse_iterator rend() noexcept;
72 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000073
Howard Hinnantd1d27a42011-06-03 19:40:40 +000074 const_iterator cbegin() const noexcept;
75 const_iterator cend() const noexcept;
76 const_reverse_iterator crbegin() const noexcept;
77 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000078
Howard Hinnantd1d27a42011-06-03 19:40:40 +000079 size_type size() const noexcept;
80 size_type max_size() const noexcept;
81 size_type capacity() const noexcept;
82 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +000084 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000085
86 reference operator[](size_type n);
87 const_reference operator[](size_type n) const;
88 reference at(size_type n);
89 const_reference at(size_type n) const;
90
91 reference front();
92 const_reference front() const;
93 reference back();
94 const_reference back() const;
95
Howard Hinnantd1d27a42011-06-03 19:40:40 +000096 value_type* data() noexcept;
97 const value_type* data() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098
99 void push_back(const value_type& x);
100 void push_back(value_type&& x);
101 template <class... Args>
102 void emplace_back(Args&&... args);
103 void pop_back();
104
105 template <class... Args> iterator emplace(const_iterator position, Args&&... args);
106 iterator insert(const_iterator position, const value_type& x);
107 iterator insert(const_iterator position, value_type&& x);
108 iterator insert(const_iterator position, size_type n, const value_type& x);
109 template <class InputIterator>
110 iterator insert(const_iterator position, InputIterator first, InputIterator last);
111 iterator insert(const_iterator position, initializer_list<value_type> il);
112
113 iterator erase(const_iterator position);
114 iterator erase(const_iterator first, const_iterator last);
115
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000116 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000117
118 void resize(size_type sz);
119 void resize(size_type sz, const value_type& c);
120
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000121 void swap(vector&)
122 noexcept(!allocator_type::propagate_on_container_swap::value ||
123 __is_nothrow_swappable<allocator_type>::value);
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(
178 allocator_type::propagate_on_container_move_assignment::value &&
179 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000180 vector& operator=(initializer_list<value_type> il);
181 template <class InputIterator>
182 void assign(InputIterator first, InputIterator last);
183 void assign(size_type n, const value_type& u);
184 void assign(initializer_list<value_type> il);
185
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000186 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000188 iterator begin() noexcept;
189 const_iterator begin() const noexcept;
190 iterator end() noexcept;
191 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000192
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000193 reverse_iterator rbegin() noexcept;
194 const_reverse_iterator rbegin() const noexcept;
195 reverse_iterator rend() noexcept;
196 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000197
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000198 const_iterator cbegin() const noexcept;
199 const_iterator cend() const noexcept;
200 const_reverse_iterator crbegin() const noexcept;
201 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000203 size_type size() const noexcept;
204 size_type max_size() const noexcept;
205 size_type capacity() const noexcept;
206 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000207 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000208 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000209
210 reference operator[](size_type n);
211 const_reference operator[](size_type n) const;
212 reference at(size_type n);
213 const_reference at(size_type n) const;
214
215 reference front();
216 const_reference front() const;
217 reference back();
218 const_reference back() const;
219
220 void push_back(const value_type& x);
Marshall Clow198a2a52013-08-13 23:54:12 +0000221 template <class... Args> void emplace_back(Args&&... args); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000222 void pop_back();
223
Marshall Clow198a2a52013-08-13 23:54:12 +0000224 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000225 iterator insert(const_iterator position, const value_type& x);
226 iterator insert(const_iterator position, size_type n, const value_type& x);
227 template <class InputIterator>
228 iterator insert(const_iterator position, InputIterator first, InputIterator last);
229 iterator insert(const_iterator position, initializer_list<value_type> il);
230
231 iterator erase(const_iterator position);
232 iterator erase(const_iterator first, const_iterator last);
233
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000234 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235
236 void resize(size_type sz);
237 void resize(size_type sz, value_type x);
238
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000239 void swap(vector&)
240 noexcept(!allocator_type::propagate_on_container_swap::value ||
241 __is_nothrow_swappable<allocator_type>::value);
242 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
Howard Hinnant5e571422013-08-23 20:10:18 +0000279#ifdef _LIBCPP_DEBUG
Howard Hinnant8b00e6c2013-08-02 00:26:35 +0000280# include <__debug>
281#else
282# define _LIBCPP_ASSERT(x, m) ((void)0)
283#endif
284
Howard Hinnant08e17472011-10-17 20:05:10 +0000285#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000286#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000287#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000288
289_LIBCPP_BEGIN_NAMESPACE_STD
290
291template <bool>
292class __vector_base_common
293{
294protected:
295 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
296 void __throw_length_error() const;
297 void __throw_out_of_range() const;
298};
299
300template <bool __b>
301void
302__vector_base_common<__b>::__throw_length_error() const
303{
304#ifndef _LIBCPP_NO_EXCEPTIONS
305 throw length_error("vector");
306#else
307 assert(!"vector length_error");
308#endif
309}
310
311template <bool __b>
312void
313__vector_base_common<__b>::__throw_out_of_range() const
314{
315#ifndef _LIBCPP_NO_EXCEPTIONS
316 throw out_of_range("vector");
317#else
318 assert(!"vector out_of_range");
319#endif
320}
321
Howard Hinnante9df0a52013-08-01 18:17:34 +0000322#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000323#pragma warning( push )
324#pragma warning( disable: 4231 )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000325#endif // _LIBCPP_MSVC
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000326_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS __vector_base_common<true>)
Howard Hinnante9df0a52013-08-01 18:17:34 +0000327#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000328#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000329#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000330
331template <class _Tp, class _Allocator>
332class __vector_base
333 : protected __vector_base_common<true>
334{
335protected:
Howard Hinnant324bb032010-08-22 00:02:43 +0000336 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337 typedef _Allocator allocator_type;
338 typedef allocator_traits<allocator_type> __alloc_traits;
339 typedef value_type& reference;
340 typedef const value_type& const_reference;
341 typedef typename __alloc_traits::size_type size_type;
342 typedef typename __alloc_traits::difference_type difference_type;
343 typedef typename __alloc_traits::pointer pointer;
344 typedef typename __alloc_traits::const_pointer const_pointer;
345 typedef pointer iterator;
346 typedef const_pointer const_iterator;
347
348 pointer __begin_;
349 pointer __end_;
350 __compressed_pair<pointer, allocator_type> __end_cap_;
351
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000352 _LIBCPP_INLINE_VISIBILITY
353 allocator_type& __alloc() _NOEXCEPT
354 {return __end_cap_.second();}
355 _LIBCPP_INLINE_VISIBILITY
356 const allocator_type& __alloc() const _NOEXCEPT
357 {return __end_cap_.second();}
358 _LIBCPP_INLINE_VISIBILITY
359 pointer& __end_cap() _NOEXCEPT
360 {return __end_cap_.first();}
361 _LIBCPP_INLINE_VISIBILITY
362 const pointer& __end_cap() const _NOEXCEPT
363 {return __end_cap_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000364
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000365 _LIBCPP_INLINE_VISIBILITY
366 __vector_base()
367 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000368 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000369 ~__vector_base();
370
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000371 _LIBCPP_INLINE_VISIBILITY
372 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
373 _LIBCPP_INLINE_VISIBILITY
374 size_type capacity() const _NOEXCEPT
375 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000376
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000378 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000379
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381 void __copy_assign_alloc(const __vector_base& __c)
382 {__copy_assign_alloc(__c, integral_constant<bool,
383 __alloc_traits::propagate_on_container_copy_assignment::value>());}
384
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000386 void __move_assign_alloc(__vector_base& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000387 _NOEXCEPT_(
388 !__alloc_traits::propagate_on_container_move_assignment::value ||
389 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000390 {__move_assign_alloc(__c, integral_constant<bool,
391 __alloc_traits::propagate_on_container_move_assignment::value>());}
392
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000394 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000395 _NOEXCEPT_(
396 !__alloc_traits::propagate_on_container_swap::value ||
397 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000398 {__swap_alloc(__x, __y, integral_constant<bool,
399 __alloc_traits::propagate_on_container_swap::value>());}
400private:
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000402 void __copy_assign_alloc(const __vector_base& __c, true_type)
403 {
404 if (__alloc() != __c.__alloc())
405 {
406 clear();
407 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
408 __begin_ = __end_ = __end_cap() = nullptr;
409 }
410 __alloc() = __c.__alloc();
411 }
412
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000414 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000415 {}
416
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000418 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000419 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000420 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000421 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000422 }
423
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000425 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000426 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000427 {}
428
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000429 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000431 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000432 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000433 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000434 swap(__x, __y);
435 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000436 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000437 static void __swap_alloc(allocator_type&, allocator_type&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000438 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000439 {}
440};
441
442template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000443inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000444void
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000445__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000446{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000447 while (__new_last != __end_)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000448 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000449}
450
451template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000452inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000453__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000454 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000455 : __begin_(nullptr),
456 __end_(nullptr),
457 __end_cap_(nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000458{
459}
460
461template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000462inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000463__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000464 : __begin_(nullptr),
465 __end_(nullptr),
466 __end_cap_(nullptr, __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000467{
468}
469
470template <class _Tp, class _Allocator>
471__vector_base<_Tp, _Allocator>::~__vector_base()
472{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000473 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000474 {
475 clear();
476 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
477 }
478}
479
480template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000481class _LIBCPP_TYPE_VIS_ONLY vector
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000482 : private __vector_base<_Tp, _Allocator>
483{
484private:
485 typedef __vector_base<_Tp, _Allocator> __base;
Howard Hinnant324bb032010-08-22 00:02:43 +0000486public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000487 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +0000488 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000489 typedef _Allocator allocator_type;
490 typedef typename __base::__alloc_traits __alloc_traits;
491 typedef typename __base::reference reference;
492 typedef typename __base::const_reference const_reference;
493 typedef typename __base::size_type size_type;
494 typedef typename __base::difference_type difference_type;
495 typedef typename __base::pointer pointer;
496 typedef typename __base::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000497 typedef __wrap_iter<pointer> iterator;
498 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000499 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
500 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000501
Howard Hinnant02d5e182013-03-26 19:04:56 +0000502 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
503 "Allocator::value_type must be same type as value_type");
504
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000505 _LIBCPP_INLINE_VISIBILITY
506 vector()
507 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant7a563db2011-09-14 18:33:51 +0000508 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000509#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000510 __get_db()->__insert_c(this);
511#endif
512 }
513 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
514 : __base(__a)
515 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000516#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000517 __get_db()->__insert_c(this);
518#endif
519 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59 +0000521#if _LIBCPP_STD_VER > 11
522 explicit vector(size_type __n, const allocator_type& __a);
523#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000524 vector(size_type __n, const_reference __x);
525 vector(size_type __n, const_reference __x, const allocator_type& __a);
526 template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +0000527 vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000528 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000529 !__is_forward_iterator<_InputIterator>::value &&
530 is_constructible<
531 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +0000532 typename iterator_traits<_InputIterator>::reference>::value,
533 _InputIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000534 template <class _InputIterator>
535 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
536 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000537 !__is_forward_iterator<_InputIterator>::value &&
538 is_constructible<
539 value_type,
540 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000541 template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +0000542 vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000543 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
544 is_constructible<
545 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +0000546 typename iterator_traits<_ForwardIterator>::reference>::value,
547 _ForwardIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000548 template <class _ForwardIterator>
549 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000550 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
551 is_constructible<
552 value_type,
553 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000554#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000556 vector(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000558 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000559#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantabe26282011-09-16 17:29:17 +0000560#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7a563db2011-09-14 18:33:51 +0000562 ~vector()
563 {
564 __get_db()->__erase_c(this);
565 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000566#endif
567
568 vector(const vector& __x);
569 vector(const vector& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000571 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000572#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000574 vector(vector&& __x)
575 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000577 vector(vector&& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000579 vector& operator=(vector&& __x)
580 _NOEXCEPT_(
581 __alloc_traits::propagate_on_container_move_assignment::value &&
582 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000583#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000584#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000585 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000586 vector& operator=(initializer_list<value_type> __il)
587 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000588#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000589
590 template <class _InputIterator>
591 typename enable_if
592 <
593 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000594 !__is_forward_iterator<_InputIterator>::value &&
595 is_constructible<
596 value_type,
597 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000598 void
599 >::type
600 assign(_InputIterator __first, _InputIterator __last);
601 template <class _ForwardIterator>
602 typename enable_if
603 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000604 __is_forward_iterator<_ForwardIterator>::value &&
605 is_constructible<
606 value_type,
607 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000608 void
609 >::type
610 assign(_ForwardIterator __first, _ForwardIterator __last);
611
612 void assign(size_type __n, const_reference __u);
Howard Hinnante3e32912011-08-12 21:56:02 +0000613#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000614 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000615 void assign(initializer_list<value_type> __il)
616 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000617#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000618
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000619 _LIBCPP_INLINE_VISIBILITY
620 allocator_type get_allocator() const _NOEXCEPT
621 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000622
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000623 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
624 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
625 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
626 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000627
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000628 _LIBCPP_INLINE_VISIBILITY
629 reverse_iterator rbegin() _NOEXCEPT
630 {return reverse_iterator(end());}
631 _LIBCPP_INLINE_VISIBILITY
632 const_reverse_iterator rbegin() const _NOEXCEPT
633 {return const_reverse_iterator(end());}
634 _LIBCPP_INLINE_VISIBILITY
635 reverse_iterator rend() _NOEXCEPT
636 {return reverse_iterator(begin());}
637 _LIBCPP_INLINE_VISIBILITY
638 const_reverse_iterator rend() const _NOEXCEPT
639 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000640
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000641 _LIBCPP_INLINE_VISIBILITY
642 const_iterator cbegin() const _NOEXCEPT
643 {return begin();}
644 _LIBCPP_INLINE_VISIBILITY
645 const_iterator cend() const _NOEXCEPT
646 {return end();}
647 _LIBCPP_INLINE_VISIBILITY
648 const_reverse_iterator crbegin() const _NOEXCEPT
649 {return rbegin();}
650 _LIBCPP_INLINE_VISIBILITY
651 const_reverse_iterator crend() const _NOEXCEPT
652 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000653
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000654 _LIBCPP_INLINE_VISIBILITY
655 size_type size() const _NOEXCEPT
656 {return static_cast<size_type>(this->__end_ - this->__begin_);}
657 _LIBCPP_INLINE_VISIBILITY
658 size_type capacity() const _NOEXCEPT
659 {return __base::capacity();}
660 _LIBCPP_INLINE_VISIBILITY
661 bool empty() const _NOEXCEPT
662 {return this->__begin_ == this->__end_;}
663 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000665 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000666
667 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
668 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
669 reference at(size_type __n);
670 const_reference at(size_type __n) const;
671
Howard Hinnant7a563db2011-09-14 18:33:51 +0000672 _LIBCPP_INLINE_VISIBILITY reference front()
673 {
674 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
675 return *this->__begin_;
676 }
677 _LIBCPP_INLINE_VISIBILITY const_reference front() const
678 {
679 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
680 return *this->__begin_;
681 }
682 _LIBCPP_INLINE_VISIBILITY reference back()
683 {
684 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
685 return *(this->__end_ - 1);
686 }
687 _LIBCPP_INLINE_VISIBILITY const_reference back() const
688 {
689 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
690 return *(this->__end_ - 1);
691 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000692
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000693 _LIBCPP_INLINE_VISIBILITY
694 value_type* data() _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000695 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000696 _LIBCPP_INLINE_VISIBILITY
697 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000698 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000699
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000700 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000701#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000702 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000703#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000704 template <class... _Args>
705 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000706#endif // _LIBCPP_HAS_NO_VARIADICS
707#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000708 void pop_back();
709
710 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000711#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000712 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000713#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000714 template <class... _Args>
715 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000716#endif // _LIBCPP_HAS_NO_VARIADICS
717#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000718 iterator insert(const_iterator __position, size_type __n, const_reference __x);
719 template <class _InputIterator>
720 typename enable_if
721 <
722 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000723 !__is_forward_iterator<_InputIterator>::value &&
724 is_constructible<
725 value_type,
726 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000727 iterator
728 >::type
729 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
730 template <class _ForwardIterator>
731 typename enable_if
732 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000733 __is_forward_iterator<_ForwardIterator>::value &&
734 is_constructible<
735 value_type,
736 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000737 iterator
738 >::type
739 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +0000740#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000742 iterator insert(const_iterator __position, initializer_list<value_type> __il)
743 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000744#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000745
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000746 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000747 iterator erase(const_iterator __first, const_iterator __last);
748
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000749 _LIBCPP_INLINE_VISIBILITY
750 void clear() _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000751 {
752 __base::clear();
753 __invalidate_all_iterators();
754 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000755
756 void resize(size_type __sz);
757 void resize(size_type __sz, const_reference __x);
758
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000759 void swap(vector&)
760 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
761 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000762
763 bool __invariants() const;
764
Howard Hinnantabe26282011-09-16 17:29:17 +0000765#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000766
767 bool __dereferenceable(const const_iterator* __i) const;
768 bool __decrementable(const const_iterator* __i) const;
769 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
770 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
771
Howard Hinnantabe26282011-09-16 17:29:17 +0000772#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000773
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000774private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000775 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000776 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000777 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000778 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31 +0000779 void __construct_at_end(size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000780 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000781 template <class _ForwardIterator>
782 typename enable_if
783 <
784 __is_forward_iterator<_ForwardIterator>::value,
785 void
786 >::type
787 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
788 void __move_construct_at_end(pointer __first, pointer __last);
789 void __append(size_type __n);
790 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000792 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000794 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000795 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
796 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
797 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000798 void __move_assign(vector& __c, true_type)
799 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000800 void __move_assign(vector& __c, false_type);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000802 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000803 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000804#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000805 __c_node* __c = __get_db()->__find_c_and_lock(this);
806 for (__i_node** __p = __c->end_; __p != __c->beg_; )
807 {
808 --__p;
809 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
810 if (__i->base() > __new_last)
811 {
812 (*__p)->__c_ = nullptr;
813 if (--__c->end_ != __p)
814 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
815 }
816 }
817 __get_db()->unlock();
818#endif
819 __base::__destruct_at_end(__new_last);
820 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000821 template <class _Up>
822 void
823#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
824 __push_back_slow_path(_Up&& __x);
825#else
826 __push_back_slow_path(_Up& __x);
827#endif
Howard Hinnant0438ea22012-02-26 15:30:12 +0000828#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
829 template <class... _Args>
830 void
831 __emplace_back_slow_path(_Args&&... __args);
832#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000833};
834
835template <class _Tp, class _Allocator>
836void
837vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
838{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000839 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000840 _VSTD::swap(this->__begin_, __v.__begin_);
841 _VSTD::swap(this->__end_, __v.__end_);
842 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000843 __v.__first_ = __v.__begin_;
844 __invalidate_all_iterators();
845}
846
847template <class _Tp, class _Allocator>
848typename vector<_Tp, _Allocator>::pointer
849vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
850{
851 pointer __r = __v.__begin_;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000852 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
853 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000854 _VSTD::swap(this->__begin_, __v.__begin_);
855 _VSTD::swap(this->__end_, __v.__end_);
856 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000857 __v.__first_ = __v.__begin_;
858 __invalidate_all_iterators();
859 return __r;
860}
861
862// Allocate space for __n objects
863// throws length_error if __n > max_size()
864// throws (probably bad_alloc) if memory run out
865// Precondition: __begin_ == __end_ == __end_cap() == 0
866// Precondition: __n > 0
867// Postcondition: capacity() == __n
868// Postcondition: size() == 0
869template <class _Tp, class _Allocator>
870void
871vector<_Tp, _Allocator>::allocate(size_type __n)
872{
873 if (__n > max_size())
874 this->__throw_length_error();
875 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
876 this->__end_cap() = this->__begin_ + __n;
877}
878
879template <class _Tp, class _Allocator>
880void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000881vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000882{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000883 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000884 {
885 clear();
886 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000887 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000888 }
889}
890
891template <class _Tp, class _Allocator>
892typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000893vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000894{
Sean Hunt110b8bf2011-07-29 23:31:58 +0000895 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 +0000896}
897
898// Precondition: __new_size > capacity()
899template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000900inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000901typename vector<_Tp, _Allocator>::size_type
902vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
903{
904 const size_type __ms = max_size();
905 if (__new_size > __ms)
906 this->__throw_length_error();
907 const size_type __cap = capacity();
908 if (__cap >= __ms / 2)
909 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58 +0000910 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000911}
912
913// Default constructs __n objects starting at __end_
914// throws if construction throws
915// Precondition: __n > 0
916// Precondition: size() + __n <= capacity()
917// Postcondition: size() == size() + __n
918template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000919void
920vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
921{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000922 allocator_type& __a = this->__alloc();
923 do
924 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000925 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000926 ++this->__end_;
927 --__n;
928 } while (__n > 0);
929}
930
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000931// Copy constructs __n objects starting at __end_ from __x
932// throws if construction throws
933// Precondition: __n > 0
934// Precondition: size() + __n <= capacity()
935// Postcondition: size() == old size() + __n
936// Postcondition: [i] == __x for all i in [size() - __n, __n)
937template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000938inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000939void
940vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
941{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000942 allocator_type& __a = this->__alloc();
943 do
944 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000945 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000946 ++this->__end_;
947 --__n;
948 } while (__n > 0);
949}
950
951template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000952template <class _ForwardIterator>
953typename enable_if
954<
955 __is_forward_iterator<_ForwardIterator>::value,
956 void
957>::type
958vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
959{
960 allocator_type& __a = this->__alloc();
961 for (; __first != __last; ++__first)
962 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000963 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000964 ++this->__end_;
965 }
966}
967
968template <class _Tp, class _Allocator>
969void
970vector<_Tp, _Allocator>::__move_construct_at_end(pointer __first, pointer __last)
971{
972 allocator_type& __a = this->__alloc();
973 for (; __first != __last; ++__first)
974 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000975 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
976 _VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000977 ++this->__end_;
978 }
979}
980
981// Default constructs __n objects starting at __end_
982// throws if construction throws
983// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000984// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000985template <class _Tp, class _Allocator>
986void
987vector<_Tp, _Allocator>::__append(size_type __n)
988{
989 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
990 this->__construct_at_end(__n);
991 else
992 {
993 allocator_type& __a = this->__alloc();
994 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
995 __v.__construct_at_end(__n);
996 __swap_out_circular_buffer(__v);
997 }
998}
999
1000// Default constructs __n objects starting at __end_
1001// throws if construction throws
1002// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001003// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001004template <class _Tp, class _Allocator>
1005void
1006vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1007{
1008 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1009 this->__construct_at_end(__n, __x);
1010 else
1011 {
1012 allocator_type& __a = this->__alloc();
1013 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1014 __v.__construct_at_end(__n, __x);
1015 __swap_out_circular_buffer(__v);
1016 }
1017}
1018
1019template <class _Tp, class _Allocator>
1020vector<_Tp, _Allocator>::vector(size_type __n)
1021{
Howard Hinnant0442b122011-09-16 18:41:29 +00001022#if _LIBCPP_DEBUG_LEVEL >= 2
1023 __get_db()->__insert_c(this);
1024#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001025 if (__n > 0)
1026 {
1027 allocate(__n);
1028 __construct_at_end(__n);
1029 }
1030}
1031
Marshall Clowa49a2c92013-09-14 00:47:59 +00001032#if _LIBCPP_STD_VER > 11
1033template <class _Tp, class _Allocator>
1034vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1035 : __base(__a)
1036{
1037#if _LIBCPP_DEBUG_LEVEL >= 2
1038 __get_db()->__insert_c(this);
1039#endif
1040 if (__n > 0)
1041 {
1042 allocate(__n);
1043 __construct_at_end(__n);
1044 }
1045}
1046#endif
1047
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001048template <class _Tp, class _Allocator>
1049vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1050{
Howard Hinnant0442b122011-09-16 18:41:29 +00001051#if _LIBCPP_DEBUG_LEVEL >= 2
1052 __get_db()->__insert_c(this);
1053#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001054 if (__n > 0)
1055 {
1056 allocate(__n);
1057 __construct_at_end(__n, __x);
1058 }
1059}
1060
1061template <class _Tp, class _Allocator>
1062vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1063 : __base(__a)
1064{
Howard Hinnant0442b122011-09-16 18:41:29 +00001065#if _LIBCPP_DEBUG_LEVEL >= 2
1066 __get_db()->__insert_c(this);
1067#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001068 if (__n > 0)
1069 {
1070 allocate(__n);
1071 __construct_at_end(__n, __x);
1072 }
1073}
1074
1075template <class _Tp, class _Allocator>
1076template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +00001077vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001078 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001079 !__is_forward_iterator<_InputIterator>::value &&
1080 is_constructible<
1081 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +00001082 typename iterator_traits<_InputIterator>::reference>::value,
1083 _InputIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001084{
Howard Hinnantabe26282011-09-16 17:29:17 +00001085#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001086 __get_db()->__insert_c(this);
1087#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001088 for (; __first != __last; ++__first)
1089 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001090}
1091
1092template <class _Tp, class _Allocator>
1093template <class _InputIterator>
1094vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1095 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001096 !__is_forward_iterator<_InputIterator>::value &&
1097 is_constructible<
1098 value_type,
1099 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001100 : __base(__a)
1101{
Howard Hinnantabe26282011-09-16 17:29:17 +00001102#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001103 __get_db()->__insert_c(this);
1104#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001105 for (; __first != __last; ++__first)
1106 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001107}
1108
1109template <class _Tp, class _Allocator>
1110template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +00001111vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001112 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1113 is_constructible<
1114 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +00001115 typename iterator_traits<_ForwardIterator>::reference>::value,
1116 _ForwardIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001117{
Howard Hinnant0442b122011-09-16 18:41:29 +00001118#if _LIBCPP_DEBUG_LEVEL >= 2
1119 __get_db()->__insert_c(this);
1120#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001121 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001122 if (__n > 0)
1123 {
1124 allocate(__n);
1125 __construct_at_end(__first, __last);
1126 }
1127}
1128
1129template <class _Tp, class _Allocator>
1130template <class _ForwardIterator>
1131vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001132 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1133 is_constructible<
1134 value_type,
1135 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001136 : __base(__a)
1137{
Howard Hinnant0442b122011-09-16 18:41:29 +00001138#if _LIBCPP_DEBUG_LEVEL >= 2
1139 __get_db()->__insert_c(this);
1140#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001141 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001142 if (__n > 0)
1143 {
1144 allocate(__n);
1145 __construct_at_end(__first, __last);
1146 }
1147}
1148
1149template <class _Tp, class _Allocator>
1150vector<_Tp, _Allocator>::vector(const vector& __x)
1151 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1152{
Howard Hinnant0442b122011-09-16 18:41:29 +00001153#if _LIBCPP_DEBUG_LEVEL >= 2
1154 __get_db()->__insert_c(this);
1155#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001156 size_type __n = __x.size();
1157 if (__n > 0)
1158 {
1159 allocate(__n);
1160 __construct_at_end(__x.__begin_, __x.__end_);
1161 }
1162}
1163
1164template <class _Tp, class _Allocator>
1165vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1166 : __base(__a)
1167{
Howard Hinnant0442b122011-09-16 18:41:29 +00001168#if _LIBCPP_DEBUG_LEVEL >= 2
1169 __get_db()->__insert_c(this);
1170#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001171 size_type __n = __x.size();
1172 if (__n > 0)
1173 {
1174 allocate(__n);
1175 __construct_at_end(__x.__begin_, __x.__end_);
1176 }
1177}
1178
Howard Hinnant73d21a42010-09-04 23:28:19 +00001179#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001180
1181template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001182inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001183vector<_Tp, _Allocator>::vector(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001184 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001185 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001186{
Howard Hinnantabe26282011-09-16 17:29:17 +00001187#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001188 __get_db()->__insert_c(this);
Howard Hinnante6125bd2011-09-19 16:34:29 +00001189 __get_db()->swap(this, &__x);
Howard Hinnant7a563db2011-09-14 18:33:51 +00001190#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001191 this->__begin_ = __x.__begin_;
1192 this->__end_ = __x.__end_;
1193 this->__end_cap() = __x.__end_cap();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001194 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001195}
1196
1197template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001198inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001199vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1200 : __base(__a)
1201{
Howard Hinnant0442b122011-09-16 18:41:29 +00001202#if _LIBCPP_DEBUG_LEVEL >= 2
1203 __get_db()->__insert_c(this);
1204#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001205 if (__a == __x.__alloc())
1206 {
1207 this->__begin_ = __x.__begin_;
1208 this->__end_ = __x.__end_;
1209 this->__end_cap() = __x.__end_cap();
1210 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001211#if _LIBCPP_DEBUG_LEVEL >= 2
1212 __get_db()->swap(this, &__x);
1213#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001214 }
1215 else
1216 {
Howard Hinnant99968442011-11-29 18:15:50 +00001217 typedef move_iterator<iterator> _Ip;
1218 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001219 }
1220}
1221
Howard Hinnante3e32912011-08-12 21:56:02 +00001222#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1223
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001224template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001225inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001226vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1227{
Howard Hinnant0442b122011-09-16 18:41:29 +00001228#if _LIBCPP_DEBUG_LEVEL >= 2
1229 __get_db()->__insert_c(this);
1230#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001231 if (__il.size() > 0)
1232 {
1233 allocate(__il.size());
1234 __construct_at_end(__il.begin(), __il.end());
1235 }
1236}
1237
1238template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001239inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001240vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1241 : __base(__a)
1242{
Howard Hinnant0442b122011-09-16 18:41:29 +00001243#if _LIBCPP_DEBUG_LEVEL >= 2
1244 __get_db()->__insert_c(this);
1245#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001246 if (__il.size() > 0)
1247 {
1248 allocate(__il.size());
1249 __construct_at_end(__il.begin(), __il.end());
1250 }
1251}
1252
Howard Hinnante3e32912011-08-12 21:56:02 +00001253#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1254
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001255template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001256inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001257vector<_Tp, _Allocator>&
1258vector<_Tp, _Allocator>::operator=(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001259 _NOEXCEPT_(
1260 __alloc_traits::propagate_on_container_move_assignment::value &&
1261 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001262{
1263 __move_assign(__x, integral_constant<bool,
1264 __alloc_traits::propagate_on_container_move_assignment::value>());
1265 return *this;
1266}
1267
1268template <class _Tp, class _Allocator>
1269void
1270vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1271{
1272 if (__base::__alloc() != __c.__alloc())
1273 {
Howard Hinnant99968442011-11-29 18:15:50 +00001274 typedef move_iterator<iterator> _Ip;
1275 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001276 }
1277 else
1278 __move_assign(__c, true_type());
1279}
1280
1281template <class _Tp, class _Allocator>
1282void
1283vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001284 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001285{
1286 deallocate();
1287 this->__begin_ = __c.__begin_;
1288 this->__end_ = __c.__end_;
1289 this->__end_cap() = __c.__end_cap();
1290 __base::__move_assign_alloc(__c);
1291 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001292#if _LIBCPP_DEBUG_LEVEL >= 2
1293 __get_db()->swap(this, &__c);
1294#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001295}
1296
Howard Hinnant73d21a42010-09-04 23:28:19 +00001297#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001298
1299template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001300inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001301vector<_Tp, _Allocator>&
1302vector<_Tp, _Allocator>::operator=(const vector& __x)
1303{
1304 if (this != &__x)
1305 {
1306 __base::__copy_assign_alloc(__x);
1307 assign(__x.__begin_, __x.__end_);
1308 }
1309 return *this;
1310}
1311
1312template <class _Tp, class _Allocator>
1313template <class _InputIterator>
1314typename enable_if
1315<
1316 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001317 !__is_forward_iterator<_InputIterator>::value &&
1318 is_constructible<
1319 _Tp,
1320 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001321 void
1322>::type
1323vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1324{
1325 clear();
1326 for (; __first != __last; ++__first)
1327 push_back(*__first);
1328}
1329
1330template <class _Tp, class _Allocator>
1331template <class _ForwardIterator>
1332typename enable_if
1333<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001334 __is_forward_iterator<_ForwardIterator>::value &&
1335 is_constructible<
1336 _Tp,
1337 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001338 void
1339>::type
1340vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1341{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001342 typename iterator_traits<_ForwardIterator>::difference_type __new_size = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001343 if (static_cast<size_type>(__new_size) <= capacity())
1344 {
1345 _ForwardIterator __mid = __last;
1346 bool __growing = false;
1347 if (static_cast<size_type>(__new_size) > size())
1348 {
1349 __growing = true;
1350 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001351 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001352 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001353 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001354 if (__growing)
1355 __construct_at_end(__mid, __last);
1356 else
1357 this->__destruct_at_end(__m);
1358 }
1359 else
1360 {
1361 deallocate();
1362 allocate(__recommend(static_cast<size_type>(__new_size)));
1363 __construct_at_end(__first, __last);
1364 }
1365}
1366
1367template <class _Tp, class _Allocator>
1368void
1369vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1370{
1371 if (__n <= capacity())
1372 {
1373 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001374 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001375 if (__n > __s)
1376 __construct_at_end(__n - __s, __u);
1377 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001378 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001379 }
1380 else
1381 {
1382 deallocate();
1383 allocate(__recommend(static_cast<size_type>(__n)));
1384 __construct_at_end(__n, __u);
1385 }
1386}
1387
Howard Hinnant324bb032010-08-22 00:02:43 +00001388template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001389inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001390typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001391vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001392{
Howard Hinnantabe26282011-09-16 17:29:17 +00001393#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001394 return iterator(this, __p);
1395#else
1396 return iterator(__p);
1397#endif
1398}
1399
Howard Hinnant324bb032010-08-22 00:02:43 +00001400template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001401inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001402typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001403vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001404{
Howard Hinnantabe26282011-09-16 17:29:17 +00001405#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001406 return const_iterator(this, __p);
1407#else
1408 return const_iterator(__p);
1409#endif
1410}
1411
Howard Hinnant324bb032010-08-22 00:02:43 +00001412template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001413inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001414typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001415vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001416{
1417 return __make_iter(this->__begin_);
1418}
1419
Howard Hinnant324bb032010-08-22 00:02:43 +00001420template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001421inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001422typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001423vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001424{
1425 return __make_iter(this->__begin_);
1426}
1427
Howard Hinnant324bb032010-08-22 00:02:43 +00001428template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001429inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001430typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001431vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001432{
1433 return __make_iter(this->__end_);
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>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001439vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001440{
1441 return __make_iter(this->__end_);
1442}
1443
Howard Hinnant324bb032010-08-22 00:02:43 +00001444template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001445inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001446typename vector<_Tp, _Allocator>::reference
1447vector<_Tp, _Allocator>::operator[](size_type __n)
1448{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001449 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001450 return this->__begin_[__n];
1451}
1452
Howard Hinnant324bb032010-08-22 00:02:43 +00001453template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001454inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001455typename vector<_Tp, _Allocator>::const_reference
1456vector<_Tp, _Allocator>::operator[](size_type __n) const
1457{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001458 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001459 return this->__begin_[__n];
1460}
1461
Howard Hinnant324bb032010-08-22 00:02:43 +00001462template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463typename vector<_Tp, _Allocator>::reference
1464vector<_Tp, _Allocator>::at(size_type __n)
1465{
1466 if (__n >= size())
1467 this->__throw_out_of_range();
1468 return this->__begin_[__n];
1469}
1470
Howard Hinnant324bb032010-08-22 00:02:43 +00001471template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001472typename vector<_Tp, _Allocator>::const_reference
1473vector<_Tp, _Allocator>::at(size_type __n) const
1474{
1475 if (__n >= size())
1476 this->__throw_out_of_range();
1477 return this->__begin_[__n];
1478}
1479
1480template <class _Tp, class _Allocator>
1481void
1482vector<_Tp, _Allocator>::reserve(size_type __n)
1483{
1484 if (__n > capacity())
1485 {
1486 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001487 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001488 __swap_out_circular_buffer(__v);
1489 }
1490}
1491
1492template <class _Tp, class _Allocator>
1493void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001494vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001495{
1496 if (capacity() > size())
1497 {
1498#ifndef _LIBCPP_NO_EXCEPTIONS
1499 try
1500 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001501#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001502 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001503 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504 __swap_out_circular_buffer(__v);
1505#ifndef _LIBCPP_NO_EXCEPTIONS
1506 }
1507 catch (...)
1508 {
1509 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001510#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001511 }
1512}
1513
1514template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001515template <class _Up>
1516void
1517#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1518vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1519#else
1520vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1521#endif
1522{
1523 allocator_type& __a = this->__alloc();
1524 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1525 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnantf619e232013-01-11 20:36:59 +00001526 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1527 __v.__end_++;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001528 __swap_out_circular_buffer(__v);
1529}
1530
1531template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001532inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001533void
1534vector<_Tp, _Allocator>::push_back(const_reference __x)
1535{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001536 if (this->__end_ != this->__end_cap())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001537 {
1538 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001539 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001540 ++this->__end_;
1541 }
1542 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001543 __push_back_slow_path(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001544}
1545
Howard Hinnant73d21a42010-09-04 23:28:19 +00001546#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001547
1548template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001549inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001550void
1551vector<_Tp, _Allocator>::push_back(value_type&& __x)
1552{
1553 if (this->__end_ < this->__end_cap())
1554 {
1555 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001556 _VSTD::__to_raw_pointer(this->__end_),
1557 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001558 ++this->__end_;
1559 }
1560 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001561 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001562}
1563
Howard Hinnant73d21a42010-09-04 23:28:19 +00001564#ifndef _LIBCPP_HAS_NO_VARIADICS
1565
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001566template <class _Tp, class _Allocator>
1567template <class... _Args>
1568void
Howard Hinnant0438ea22012-02-26 15:30:12 +00001569vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1570{
1571 allocator_type& __a = this->__alloc();
1572 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1573// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantf619e232013-01-11 20:36:59 +00001574 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1575 __v.__end_++;
Howard Hinnant0438ea22012-02-26 15:30:12 +00001576 __swap_out_circular_buffer(__v);
1577}
1578
1579template <class _Tp, class _Allocator>
1580template <class... _Args>
Howard Hinnant1e564242013-10-04 22:09:00 +00001581inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0438ea22012-02-26 15:30:12 +00001582void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001583vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1584{
1585 if (this->__end_ < this->__end_cap())
1586 {
1587 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001588 _VSTD::__to_raw_pointer(this->__end_),
1589 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590 ++this->__end_;
1591 }
1592 else
Howard Hinnant0438ea22012-02-26 15:30:12 +00001593 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001594}
1595
Howard Hinnant73d21a42010-09-04 23:28:19 +00001596#endif // _LIBCPP_HAS_NO_VARIADICS
1597#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001598
1599template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001600inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001601void
1602vector<_Tp, _Allocator>::pop_back()
1603{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001604 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001605 this->__destruct_at_end(this->__end_ - 1);
1606}
1607
1608template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001609inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001610typename vector<_Tp, _Allocator>::iterator
1611vector<_Tp, _Allocator>::erase(const_iterator __position)
1612{
Howard Hinnantabe26282011-09-16 17:29:17 +00001613#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001614 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1615 "vector::erase(iterator) called with an iterator not"
1616 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001617#endif
Howard Hinnant782da332013-03-25 22:12:26 +00001618 _LIBCPP_ASSERT(__position != end(),
1619 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001620 difference_type __ps = __position - cbegin();
1621 pointer __p = this->__begin_ + __ps;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001622 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001623 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001624 return __r;
1625}
1626
1627template <class _Tp, class _Allocator>
1628typename vector<_Tp, _Allocator>::iterator
1629vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1630{
Howard Hinnantabe26282011-09-16 17:29:17 +00001631#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001632 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1633 "vector::erase(iterator, iterator) called with an iterator not"
1634 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001635#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +00001636 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001637 pointer __p = this->__begin_ + (__first - begin());
1638 iterator __r = __make_iter(__p);
Howard Hinnantb4e67cf2013-04-18 15:02:57 +00001639 if (__first != __last)
1640 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001641 return __r;
1642}
1643
1644template <class _Tp, class _Allocator>
1645void
1646vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1647{
1648 pointer __old_last = this->__end_;
1649 difference_type __n = __old_last - __to;
1650 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1651 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001652 _VSTD::__to_raw_pointer(this->__end_),
1653 _VSTD::move(*__i));
1654 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655}
1656
1657template <class _Tp, class _Allocator>
1658typename vector<_Tp, _Allocator>::iterator
1659vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1660{
Howard Hinnantabe26282011-09-16 17:29:17 +00001661#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001662 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1663 "vector::insert(iterator, x) called with an iterator not"
1664 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001665#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001666 pointer __p = this->__begin_ + (__position - begin());
1667 if (this->__end_ < this->__end_cap())
1668 {
1669 if (__p == this->__end_)
1670 {
1671 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001672 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001673 ++this->__end_;
1674 }
1675 else
1676 {
1677 __move_range(__p, this->__end_, __p + 1);
1678 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1679 if (__p <= __xr && __xr < this->__end_)
1680 ++__xr;
1681 *__p = *__xr;
1682 }
1683 }
1684 else
1685 {
1686 allocator_type& __a = this->__alloc();
1687 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1688 __v.push_back(__x);
1689 __p = __swap_out_circular_buffer(__v, __p);
1690 }
1691 return __make_iter(__p);
1692}
1693
Howard Hinnant73d21a42010-09-04 23:28:19 +00001694#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001695
1696template <class _Tp, class _Allocator>
1697typename vector<_Tp, _Allocator>::iterator
1698vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1699{
Howard Hinnantabe26282011-09-16 17:29:17 +00001700#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001701 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1702 "vector::insert(iterator, x) called with an iterator not"
1703 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001704#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001705 pointer __p = this->__begin_ + (__position - begin());
1706 if (this->__end_ < this->__end_cap())
1707 {
1708 if (__p == this->__end_)
1709 {
1710 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001711 _VSTD::__to_raw_pointer(this->__end_),
1712 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001713 ++this->__end_;
1714 }
1715 else
1716 {
1717 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001718 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001719 }
1720 }
1721 else
1722 {
1723 allocator_type& __a = this->__alloc();
1724 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001725 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001726 __p = __swap_out_circular_buffer(__v, __p);
1727 }
1728 return __make_iter(__p);
1729}
1730
Howard Hinnant73d21a42010-09-04 23:28:19 +00001731#ifndef _LIBCPP_HAS_NO_VARIADICS
1732
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001733template <class _Tp, class _Allocator>
1734template <class... _Args>
1735typename vector<_Tp, _Allocator>::iterator
1736vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1737{
Howard Hinnantabe26282011-09-16 17:29:17 +00001738#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001739 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1740 "vector::emplace(iterator, x) called with an iterator not"
1741 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001742#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001743 pointer __p = this->__begin_ + (__position - begin());
1744 if (this->__end_ < this->__end_cap())
1745 {
1746 if (__p == this->__end_)
1747 {
1748 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001749 _VSTD::__to_raw_pointer(this->__end_),
1750 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001751 ++this->__end_;
1752 }
1753 else
1754 {
Howard Hinnanta58402a2012-07-08 23:23:04 +00001755 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001756 __move_range(__p, this->__end_, __p + 1);
Howard Hinnanta58402a2012-07-08 23:23:04 +00001757 *__p = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001758 }
1759 }
1760 else
1761 {
1762 allocator_type& __a = this->__alloc();
1763 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001764 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001765 __p = __swap_out_circular_buffer(__v, __p);
1766 }
1767 return __make_iter(__p);
1768}
1769
Howard Hinnant73d21a42010-09-04 23:28:19 +00001770#endif // _LIBCPP_HAS_NO_VARIADICS
1771#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001772
1773template <class _Tp, class _Allocator>
1774typename vector<_Tp, _Allocator>::iterator
1775vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1776{
Howard Hinnantabe26282011-09-16 17:29:17 +00001777#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001778 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1779 "vector::insert(iterator, n, x) called with an iterator not"
1780 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001781#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001782 pointer __p = this->__begin_ + (__position - begin());
1783 if (__n > 0)
1784 {
1785 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1786 {
1787 size_type __old_n = __n;
1788 pointer __old_last = this->__end_;
1789 if (__n > static_cast<size_type>(this->__end_ - __p))
1790 {
1791 size_type __cx = __n - (this->__end_ - __p);
1792 __construct_at_end(__cx, __x);
1793 __n -= __cx;
1794 }
1795 if (__n > 0)
1796 {
1797 __move_range(__p, __old_last, __p + __old_n);
1798 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1799 if (__p <= __xr && __xr < this->__end_)
1800 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001801 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001802 }
1803 }
1804 else
1805 {
1806 allocator_type& __a = this->__alloc();
1807 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1808 __v.__construct_at_end(__n, __x);
1809 __p = __swap_out_circular_buffer(__v, __p);
1810 }
1811 }
1812 return __make_iter(__p);
1813}
1814
1815template <class _Tp, class _Allocator>
1816template <class _InputIterator>
1817typename enable_if
1818<
1819 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001820 !__is_forward_iterator<_InputIterator>::value &&
1821 is_constructible<
1822 _Tp,
1823 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001824 typename vector<_Tp, _Allocator>::iterator
1825>::type
1826vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1827{
Howard Hinnantabe26282011-09-16 17:29:17 +00001828#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001829 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1830 "vector::insert(iterator, range) called with an iterator not"
1831 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001832#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001833 difference_type __off = __position - begin();
1834 pointer __p = this->__begin_ + __off;
1835 allocator_type& __a = this->__alloc();
1836 pointer __old_last = this->__end_;
1837 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1838 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001839 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001840 *__first);
1841 ++this->__end_;
1842 }
1843 __split_buffer<value_type, allocator_type&> __v(__a);
1844 if (__first != __last)
1845 {
1846#ifndef _LIBCPP_NO_EXCEPTIONS
1847 try
1848 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001849#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001850 __v.__construct_at_end(__first, __last);
1851 difference_type __old_size = __old_last - this->__begin_;
1852 difference_type __old_p = __p - this->__begin_;
1853 reserve(__recommend(size() + __v.size()));
1854 __p = this->__begin_ + __old_p;
1855 __old_last = this->__begin_ + __old_size;
1856#ifndef _LIBCPP_NO_EXCEPTIONS
1857 }
1858 catch (...)
1859 {
1860 erase(__make_iter(__old_last), end());
1861 throw;
1862 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001863#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001864 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001865 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant0442b122011-09-16 18:41:29 +00001866 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1867 make_move_iterator(__v.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001868 return begin() + __off;
1869}
1870
1871template <class _Tp, class _Allocator>
1872template <class _ForwardIterator>
1873typename enable_if
1874<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001875 __is_forward_iterator<_ForwardIterator>::value &&
1876 is_constructible<
1877 _Tp,
1878 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001879 typename vector<_Tp, _Allocator>::iterator
1880>::type
1881vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1882{
Howard Hinnantabe26282011-09-16 17:29:17 +00001883#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001884 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1885 "vector::insert(iterator, range) called with an iterator not"
1886 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001887#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001888 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00001889 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001890 if (__n > 0)
1891 {
1892 if (__n <= this->__end_cap() - this->__end_)
1893 {
1894 size_type __old_n = __n;
1895 pointer __old_last = this->__end_;
1896 _ForwardIterator __m = __last;
1897 difference_type __dx = this->__end_ - __p;
1898 if (__n > __dx)
1899 {
1900 __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001901 _VSTD::advance(__m, this->__end_ - __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001902 __construct_at_end(__m, __last);
1903 __n = __dx;
1904 }
1905 if (__n > 0)
1906 {
1907 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001908 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001909 }
1910 }
1911 else
1912 {
1913 allocator_type& __a = this->__alloc();
1914 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1915 __v.__construct_at_end(__first, __last);
1916 __p = __swap_out_circular_buffer(__v, __p);
1917 }
1918 }
1919 return __make_iter(__p);
1920}
1921
1922template <class _Tp, class _Allocator>
1923void
1924vector<_Tp, _Allocator>::resize(size_type __sz)
1925{
1926 size_type __cs = size();
1927 if (__cs < __sz)
1928 this->__append(__sz - __cs);
1929 else if (__cs > __sz)
1930 this->__destruct_at_end(this->__begin_ + __sz);
1931}
1932
1933template <class _Tp, class _Allocator>
1934void
1935vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
1936{
1937 size_type __cs = size();
1938 if (__cs < __sz)
1939 this->__append(__sz - __cs, __x);
1940 else if (__cs > __sz)
1941 this->__destruct_at_end(this->__begin_ + __sz);
1942}
1943
1944template <class _Tp, class _Allocator>
1945void
1946vector<_Tp, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001947 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1948 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001949{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001950 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
1951 this->__alloc() == __x.__alloc(),
1952 "vector::swap: Either propagate_on_container_swap must be true"
1953 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +00001954 _VSTD::swap(this->__begin_, __x.__begin_);
1955 _VSTD::swap(this->__end_, __x.__end_);
1956 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001957 __base::__swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnantabe26282011-09-16 17:29:17 +00001958#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001959 __get_db()->swap(this, &__x);
Howard Hinnantabe26282011-09-16 17:29:17 +00001960#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001961}
1962
Howard Hinnant324bb032010-08-22 00:02:43 +00001963template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001964bool
1965vector<_Tp, _Allocator>::__invariants() const
1966{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001967 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001968 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001969 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001970 return false;
1971 }
1972 else
1973 {
1974 if (this->__begin_ > this->__end_)
1975 return false;
1976 if (this->__begin_ == this->__end_cap())
1977 return false;
1978 if (this->__end_ > this->__end_cap())
1979 return false;
1980 }
1981 return true;
1982}
1983
Howard Hinnantabe26282011-09-16 17:29:17 +00001984#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001985
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001986template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:51 +00001987bool
1988vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
1989{
1990 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
1991}
1992
1993template <class _Tp, class _Allocator>
1994bool
1995vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
1996{
1997 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
1998}
1999
2000template <class _Tp, class _Allocator>
2001bool
2002vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2003{
2004 const_pointer __p = __i->base() + __n;
2005 return this->__begin_ <= __p && __p <= this->__end_;
2006}
2007
2008template <class _Tp, class _Allocator>
2009bool
2010vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2011{
2012 const_pointer __p = __i->base() + __n;
2013 return this->__begin_ <= __p && __p < this->__end_;
2014}
2015
Howard Hinnantabe26282011-09-16 17:29:17 +00002016#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002017
2018template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002019inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002020void
2021vector<_Tp, _Allocator>::__invalidate_all_iterators()
2022{
Howard Hinnantabe26282011-09-16 17:29:17 +00002023#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002024 __get_db()->__invalidate_all(this);
Howard Hinnantabe26282011-09-16 17:29:17 +00002025#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002026}
2027
2028// vector<bool>
2029
2030template <class _Allocator> class vector<bool, _Allocator>;
2031
2032template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2033
2034template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002035struct __has_storage_type<vector<bool, _Allocator> >
2036{
2037 static const bool value = true;
2038};
2039
2040template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002041class _LIBCPP_TYPE_VIS_ONLY vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002042 : private __vector_base_common<true>
2043{
2044public:
2045 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00002046 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002047 typedef _Allocator allocator_type;
2048 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002049 typedef typename __alloc_traits::size_type size_type;
2050 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantf867f632012-05-07 16:50:38 +00002051 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002052 typedef __bit_iterator<vector, false> pointer;
2053 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002054 typedef pointer iterator;
2055 typedef const_pointer const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002056 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2057 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002058
2059private:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002060 typedef typename __alloc_traits::template
2061#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
2062 rebind_alloc<__storage_type>
2063#else
2064 rebind_alloc<__storage_type>::other
2065#endif
2066 __storage_allocator;
2067 typedef allocator_traits<__storage_allocator> __storage_traits;
2068 typedef typename __storage_traits::pointer __storage_pointer;
2069 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2070
2071 __storage_pointer __begin_;
2072 size_type __size_;
2073 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002074public:
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002075 typedef __bit_reference<vector> reference;
2076 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002077private:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002078 _LIBCPP_INLINE_VISIBILITY
2079 size_type& __cap() _NOEXCEPT
2080 {return __cap_alloc_.first();}
2081 _LIBCPP_INLINE_VISIBILITY
2082 const size_type& __cap() const _NOEXCEPT
2083 {return __cap_alloc_.first();}
2084 _LIBCPP_INLINE_VISIBILITY
2085 __storage_allocator& __alloc() _NOEXCEPT
2086 {return __cap_alloc_.second();}
2087 _LIBCPP_INLINE_VISIBILITY
2088 const __storage_allocator& __alloc() const _NOEXCEPT
2089 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002090
2091 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2092
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002093 _LIBCPP_INLINE_VISIBILITY
2094 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002095 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002096 _LIBCPP_INLINE_VISIBILITY
2097 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002098 {return (__n - 1) / __bits_per_word + 1;}
2099
2100public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002101 _LIBCPP_INLINE_VISIBILITY
2102 vector()
2103 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002104 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002105 ~vector();
2106 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59 +00002107#if _LIBCPP_STD_VER > 11
2108 explicit vector(size_type __n, const allocator_type& __a);
2109#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002110 vector(size_type __n, const value_type& __v);
2111 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2112 template <class _InputIterator>
2113 vector(_InputIterator __first, _InputIterator __last,
2114 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2115 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2116 template <class _InputIterator>
2117 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2118 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2119 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2120 template <class _ForwardIterator>
2121 vector(_ForwardIterator __first, _ForwardIterator __last,
2122 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2123 template <class _ForwardIterator>
2124 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2125 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2126
2127 vector(const vector& __v);
2128 vector(const vector& __v, const allocator_type& __a);
2129 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00002130#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002131 vector(initializer_list<value_type> __il);
2132 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00002133#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002134
Howard Hinnant73d21a42010-09-04 23:28:19 +00002135#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002136 _LIBCPP_INLINE_VISIBILITY
2137 vector(vector&& __v)
2138 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002139 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002140 _LIBCPP_INLINE_VISIBILITY
2141 vector& operator=(vector&& __v)
2142 _NOEXCEPT_(
2143 __alloc_traits::propagate_on_container_move_assignment::value &&
2144 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002145#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +00002146#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002147 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002148 vector& operator=(initializer_list<value_type> __il)
2149 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00002150#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002151
2152 template <class _InputIterator>
2153 typename enable_if
2154 <
2155 __is_input_iterator<_InputIterator>::value &&
2156 !__is_forward_iterator<_InputIterator>::value,
2157 void
2158 >::type
2159 assign(_InputIterator __first, _InputIterator __last);
2160 template <class _ForwardIterator>
2161 typename enable_if
2162 <
2163 __is_forward_iterator<_ForwardIterator>::value,
2164 void
2165 >::type
2166 assign(_ForwardIterator __first, _ForwardIterator __last);
2167
2168 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +00002169#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002171 void assign(initializer_list<value_type> __il)
2172 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002173#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002174
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002175 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002176 {return allocator_type(this->__alloc());}
2177
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002178 size_type max_size() const _NOEXCEPT;
2179 _LIBCPP_INLINE_VISIBILITY
2180 size_type capacity() const _NOEXCEPT
2181 {return __internal_cap_to_external(__cap());}
2182 _LIBCPP_INLINE_VISIBILITY
2183 size_type size() const _NOEXCEPT
2184 {return __size_;}
2185 _LIBCPP_INLINE_VISIBILITY
2186 bool empty() const _NOEXCEPT
2187 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002188 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002189 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002190
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002191 _LIBCPP_INLINE_VISIBILITY
2192 iterator begin() _NOEXCEPT
2193 {return __make_iter(0);}
2194 _LIBCPP_INLINE_VISIBILITY
2195 const_iterator begin() const _NOEXCEPT
2196 {return __make_iter(0);}
2197 _LIBCPP_INLINE_VISIBILITY
2198 iterator end() _NOEXCEPT
2199 {return __make_iter(__size_);}
2200 _LIBCPP_INLINE_VISIBILITY
2201 const_iterator end() const _NOEXCEPT
2202 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002203
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002204 _LIBCPP_INLINE_VISIBILITY
2205 reverse_iterator rbegin() _NOEXCEPT
2206 {return reverse_iterator(end());}
2207 _LIBCPP_INLINE_VISIBILITY
2208 const_reverse_iterator rbegin() const _NOEXCEPT
2209 {return const_reverse_iterator(end());}
2210 _LIBCPP_INLINE_VISIBILITY
2211 reverse_iterator rend() _NOEXCEPT
2212 {return reverse_iterator(begin());}
2213 _LIBCPP_INLINE_VISIBILITY
2214 const_reverse_iterator rend() const _NOEXCEPT
2215 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002216
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002217 _LIBCPP_INLINE_VISIBILITY
2218 const_iterator cbegin() const _NOEXCEPT
2219 {return __make_iter(0);}
2220 _LIBCPP_INLINE_VISIBILITY
2221 const_iterator cend() const _NOEXCEPT
2222 {return __make_iter(__size_);}
2223 _LIBCPP_INLINE_VISIBILITY
2224 const_reverse_iterator crbegin() const _NOEXCEPT
2225 {return rbegin();}
2226 _LIBCPP_INLINE_VISIBILITY
2227 const_reverse_iterator crend() const _NOEXCEPT
2228 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002229
2230 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2231 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2232 reference at(size_type __n);
2233 const_reference at(size_type __n) const;
2234
2235 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2236 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2237 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2238 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2239
2240 void push_back(const value_type& __x);
Marshall Clow198a2a52013-08-13 23:54:12 +00002241#if _LIBCPP_STD_VER > 11
2242 template <class... _Args>
2243 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2244 { push_back ( value_type ( _VSTD::forward<_Args>(__args)... )); }
2245#endif
2246
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002247 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2248
Marshall Clow198a2a52013-08-13 23:54:12 +00002249#if _LIBCPP_STD_VER > 11
2250 template <class... _Args>
2251 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2252 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2253#endif
2254
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002255 iterator insert(const_iterator __position, const value_type& __x);
2256 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2257 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2258 template <class _InputIterator>
2259 typename enable_if
2260 <
2261 __is_input_iterator <_InputIterator>::value &&
2262 !__is_forward_iterator<_InputIterator>::value,
2263 iterator
2264 >::type
2265 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2266 template <class _ForwardIterator>
2267 typename enable_if
2268 <
2269 __is_forward_iterator<_ForwardIterator>::value,
2270 iterator
2271 >::type
2272 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00002273#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002275 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2276 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002277#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002278
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002279 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002280 iterator erase(const_iterator __first, const_iterator __last);
2281
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002282 _LIBCPP_INLINE_VISIBILITY
2283 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002284
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002285 void swap(vector&)
2286 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2287 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002288
2289 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002290 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002291
2292 bool __invariants() const;
2293
2294private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002295 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002296 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002297 void deallocate() _NOEXCEPT;
2298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00002299 static size_type __align_it(size_type __new_size) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002300 {return __new_size + (__bits_per_word-1) & ~(__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002301 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2302 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002303 template <class _ForwardIterator>
2304 typename enable_if
2305 <
2306 __is_forward_iterator<_ForwardIterator>::value,
2307 void
2308 >::type
2309 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2310 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002311 _LIBCPP_INLINE_VISIBILITY
2312 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002313 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002314 _LIBCPP_INLINE_VISIBILITY
2315 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002316 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002317 _LIBCPP_INLINE_VISIBILITY
2318 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002319 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002320 _LIBCPP_INLINE_VISIBILITY
2321 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002322 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002323 _LIBCPP_INLINE_VISIBILITY
2324 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002325 {return begin() + (__p - cbegin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002326
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002328 void __copy_assign_alloc(const vector& __v)
2329 {__copy_assign_alloc(__v, integral_constant<bool,
2330 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002332 void __copy_assign_alloc(const vector& __c, true_type)
2333 {
2334 if (__alloc() != __c.__alloc())
2335 deallocate();
2336 __alloc() = __c.__alloc();
2337 }
2338
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002340 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002341 {}
2342
2343 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002344 void __move_assign(vector& __c, true_type)
2345 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002347 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002348 _NOEXCEPT_(
2349 !__storage_traits::propagate_on_container_move_assignment::value ||
2350 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002351 {__move_assign_alloc(__c, integral_constant<bool,
2352 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002353 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00002354 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002355 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002356 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002357 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002358 }
2359
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002361 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002362 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002363 {}
2364
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002366 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002367 _NOEXCEPT_(
2368 !__storage_traits::propagate_on_container_swap::value ||
2369 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002370 {__swap_alloc(__x, __y, integral_constant<bool,
2371 __storage_traits::propagate_on_container_swap::value>());}
2372
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002373 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002374 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002375 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002376 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002377 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002378 swap(__x, __y);
2379 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002381 static void __swap_alloc(__storage_allocator&, __storage_allocator&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002382 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002383 {}
2384
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002385 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002386
2387 friend class __bit_reference<vector>;
2388 friend class __bit_const_reference<vector>;
2389 friend class __bit_iterator<vector, false>;
2390 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:18 +00002391 friend struct __bit_array<vector>;
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002392 friend struct _LIBCPP_TYPE_VIS_ONLY hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002393};
2394
2395template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002396inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002397void
2398vector<bool, _Allocator>::__invalidate_all_iterators()
2399{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002400}
2401
2402// Allocate space for __n objects
2403// throws length_error if __n > max_size()
2404// throws (probably bad_alloc) if memory run out
2405// Precondition: __begin_ == __end_ == __cap() == 0
2406// Precondition: __n > 0
2407// Postcondition: capacity() == __n
2408// Postcondition: size() == 0
2409template <class _Allocator>
2410void
2411vector<bool, _Allocator>::allocate(size_type __n)
2412{
2413 if (__n > max_size())
2414 this->__throw_length_error();
2415 __n = __external_cap_to_internal(__n);
2416 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2417 this->__size_ = 0;
2418 this->__cap() = __n;
2419}
2420
2421template <class _Allocator>
2422void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002423vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002424{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002425 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002426 {
2427 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2428 __invalidate_all_iterators();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002429 this->__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002430 this->__size_ = this->__cap() = 0;
2431 }
2432}
2433
2434template <class _Allocator>
2435typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002436vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002437{
2438 size_type __amax = __storage_traits::max_size(__alloc());
2439 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2440 if (__nmax / __bits_per_word <= __amax)
2441 return __nmax;
2442 return __internal_cap_to_external(__amax);
2443}
2444
2445// Precondition: __new_size > capacity()
2446template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002447inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002448typename vector<bool, _Allocator>::size_type
2449vector<bool, _Allocator>::__recommend(size_type __new_size) const
2450{
2451 const size_type __ms = max_size();
2452 if (__new_size > __ms)
2453 this->__throw_length_error();
2454 const size_type __cap = capacity();
2455 if (__cap >= __ms / 2)
2456 return __ms;
Howard Hinnant7f764502013-08-14 18:00:20 +00002457 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002458}
2459
2460// Default constructs __n objects starting at __end_
2461// Precondition: __n > 0
2462// Precondition: size() + __n <= capacity()
2463// Postcondition: size() == size() + __n
2464template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002465inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002466void
2467vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2468{
2469 size_type __old_size = this->__size_;
2470 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002471 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002472}
2473
2474template <class _Allocator>
2475template <class _ForwardIterator>
2476typename enable_if
2477<
2478 __is_forward_iterator<_ForwardIterator>::value,
2479 void
2480>::type
2481vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2482{
2483 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002484 this->__size_ += _VSTD::distance(__first, __last);
2485 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002486}
2487
2488template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002489inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002490vector<bool, _Allocator>::vector()
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002491 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002492 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002493 __size_(0),
2494 __cap_alloc_(0)
2495{
2496}
2497
2498template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002499inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002500vector<bool, _Allocator>::vector(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002501 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002502 __size_(0),
2503 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2504{
2505}
2506
2507template <class _Allocator>
2508vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002509 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002510 __size_(0),
2511 __cap_alloc_(0)
2512{
2513 if (__n > 0)
2514 {
2515 allocate(__n);
2516 __construct_at_end(__n, false);
2517 }
2518}
2519
Marshall Clowa49a2c92013-09-14 00:47:59 +00002520#if _LIBCPP_STD_VER > 11
2521template <class _Allocator>
2522vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2523 : __begin_(nullptr),
2524 __size_(0),
2525 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2526{
2527 if (__n > 0)
2528 {
2529 allocate(__n);
2530 __construct_at_end(__n, false);
2531 }
2532}
2533#endif
2534
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002535template <class _Allocator>
2536vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002537 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002538 __size_(0),
2539 __cap_alloc_(0)
2540{
2541 if (__n > 0)
2542 {
2543 allocate(__n);
2544 __construct_at_end(__n, __x);
2545 }
2546}
2547
2548template <class _Allocator>
2549vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002550 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002551 __size_(0),
2552 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2553{
2554 if (__n > 0)
2555 {
2556 allocate(__n);
2557 __construct_at_end(__n, __x);
2558 }
2559}
2560
2561template <class _Allocator>
2562template <class _InputIterator>
2563vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2564 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2565 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002566 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002567 __size_(0),
2568 __cap_alloc_(0)
2569{
2570#ifndef _LIBCPP_NO_EXCEPTIONS
2571 try
2572 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002573#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002574 for (; __first != __last; ++__first)
2575 push_back(*__first);
2576#ifndef _LIBCPP_NO_EXCEPTIONS
2577 }
2578 catch (...)
2579 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002580 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002581 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2582 __invalidate_all_iterators();
2583 throw;
2584 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002585#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002586}
2587
2588template <class _Allocator>
2589template <class _InputIterator>
2590vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2591 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2592 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002593 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002594 __size_(0),
2595 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2596{
2597#ifndef _LIBCPP_NO_EXCEPTIONS
2598 try
2599 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002600#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002601 for (; __first != __last; ++__first)
2602 push_back(*__first);
2603#ifndef _LIBCPP_NO_EXCEPTIONS
2604 }
2605 catch (...)
2606 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002607 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002608 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2609 __invalidate_all_iterators();
2610 throw;
2611 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002612#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002613}
2614
2615template <class _Allocator>
2616template <class _ForwardIterator>
2617vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2618 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002619 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002620 __size_(0),
2621 __cap_alloc_(0)
2622{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002623 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002624 if (__n > 0)
2625 {
2626 allocate(__n);
2627 __construct_at_end(__first, __last);
2628 }
2629}
2630
2631template <class _Allocator>
2632template <class _ForwardIterator>
2633vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2634 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002635 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002636 __size_(0),
2637 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2638{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002639 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002640 if (__n > 0)
2641 {
2642 allocate(__n);
2643 __construct_at_end(__first, __last);
2644 }
2645}
2646
Howard Hinnante3e32912011-08-12 21:56:02 +00002647#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2648
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002649template <class _Allocator>
2650vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002651 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002652 __size_(0),
2653 __cap_alloc_(0)
2654{
2655 size_type __n = static_cast<size_type>(__il.size());
2656 if (__n > 0)
2657 {
2658 allocate(__n);
2659 __construct_at_end(__il.begin(), __il.end());
2660 }
2661}
2662
2663template <class _Allocator>
2664vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002665 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002666 __size_(0),
2667 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2668{
2669 size_type __n = static_cast<size_type>(__il.size());
2670 if (__n > 0)
2671 {
2672 allocate(__n);
2673 __construct_at_end(__il.begin(), __il.end());
2674 }
2675}
2676
Howard Hinnante3e32912011-08-12 21:56:02 +00002677#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2678
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002679template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002680vector<bool, _Allocator>::~vector()
2681{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002682 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002683 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002684 __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002685}
2686
2687template <class _Allocator>
2688vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002689 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002690 __size_(0),
2691 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2692{
2693 if (__v.size() > 0)
2694 {
2695 allocate(__v.size());
2696 __construct_at_end(__v.begin(), __v.end());
2697 }
2698}
2699
2700template <class _Allocator>
2701vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002702 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002703 __size_(0),
2704 __cap_alloc_(0, __a)
2705{
2706 if (__v.size() > 0)
2707 {
2708 allocate(__v.size());
2709 __construct_at_end(__v.begin(), __v.end());
2710 }
2711}
2712
2713template <class _Allocator>
2714vector<bool, _Allocator>&
2715vector<bool, _Allocator>::operator=(const vector& __v)
2716{
2717 if (this != &__v)
2718 {
2719 __copy_assign_alloc(__v);
2720 if (__v.__size_)
2721 {
2722 if (__v.__size_ > capacity())
2723 {
2724 deallocate();
2725 allocate(__v.__size_);
2726 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002727 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002728 }
2729 __size_ = __v.__size_;
2730 }
2731 return *this;
2732}
2733
Howard Hinnant73d21a42010-09-04 23:28:19 +00002734#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2735
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002736template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002737inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738vector<bool, _Allocator>::vector(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002739 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002740 : __begin_(__v.__begin_),
2741 __size_(__v.__size_),
2742 __cap_alloc_(__v.__cap_alloc_)
2743{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002744 __v.__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002745 __v.__size_ = 0;
2746 __v.__cap() = 0;
2747}
2748
2749template <class _Allocator>
2750vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002751 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002752 __size_(0),
2753 __cap_alloc_(0, __a)
2754{
2755 if (__a == allocator_type(__v.__alloc()))
2756 {
2757 this->__begin_ = __v.__begin_;
2758 this->__size_ = __v.__size_;
2759 this->__cap() = __v.__cap();
2760 __v.__begin_ = nullptr;
2761 __v.__cap() = __v.__size_ = 0;
2762 }
2763 else if (__v.size() > 0)
2764 {
2765 allocate(__v.size());
2766 __construct_at_end(__v.begin(), __v.end());
2767 }
2768}
2769
2770template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002771inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002772vector<bool, _Allocator>&
2773vector<bool, _Allocator>::operator=(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002774 _NOEXCEPT_(
2775 __alloc_traits::propagate_on_container_move_assignment::value &&
2776 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002777{
2778 __move_assign(__v, integral_constant<bool,
2779 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002780 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002781}
2782
2783template <class _Allocator>
2784void
2785vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2786{
2787 if (__alloc() != __c.__alloc())
2788 assign(__c.begin(), __c.end());
2789 else
2790 __move_assign(__c, true_type());
2791}
2792
2793template <class _Allocator>
2794void
2795vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002796 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002797{
2798 deallocate();
2799 this->__begin_ = __c.__begin_;
2800 this->__size_ = __c.__size_;
2801 this->__cap() = __c.__cap();
2802 __move_assign_alloc(__c);
2803 __c.__begin_ = nullptr;
2804 __c.__cap() = __c.__size_ = 0;
2805}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002806
2807#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002808
2809template <class _Allocator>
2810void
2811vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2812{
2813 __size_ = 0;
2814 if (__n > 0)
2815 {
2816 size_type __c = capacity();
2817 if (__n <= __c)
2818 __size_ = __n;
2819 else
2820 {
2821 vector __v(__alloc());
2822 __v.reserve(__recommend(__n));
2823 __v.__size_ = __n;
2824 swap(__v);
2825 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002826 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002827 }
2828}
2829
2830template <class _Allocator>
2831template <class _InputIterator>
2832typename enable_if
2833<
2834 __is_input_iterator<_InputIterator>::value &&
2835 !__is_forward_iterator<_InputIterator>::value,
2836 void
2837>::type
2838vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2839{
2840 clear();
2841 for (; __first != __last; ++__first)
2842 push_back(*__first);
2843}
2844
2845template <class _Allocator>
2846template <class _ForwardIterator>
2847typename enable_if
2848<
2849 __is_forward_iterator<_ForwardIterator>::value,
2850 void
2851>::type
2852vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2853{
2854 clear();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002855 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002856 if (__n)
2857 {
2858 if (__n > capacity())
2859 {
2860 deallocate();
2861 allocate(__n);
2862 }
2863 __construct_at_end(__first, __last);
2864 }
2865}
2866
2867template <class _Allocator>
2868void
2869vector<bool, _Allocator>::reserve(size_type __n)
2870{
2871 if (__n > capacity())
2872 {
2873 vector __v(this->__alloc());
2874 __v.allocate(__n);
2875 __v.__construct_at_end(this->begin(), this->end());
2876 swap(__v);
2877 __invalidate_all_iterators();
2878 }
2879}
2880
2881template <class _Allocator>
2882void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002883vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002884{
2885 if (__external_cap_to_internal(size()) > __cap())
2886 {
2887#ifndef _LIBCPP_NO_EXCEPTIONS
2888 try
2889 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002890#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002891 vector(*this, allocator_type(__alloc())).swap(*this);
2892#ifndef _LIBCPP_NO_EXCEPTIONS
2893 }
2894 catch (...)
2895 {
2896 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002897#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002898 }
2899}
2900
2901template <class _Allocator>
2902typename vector<bool, _Allocator>::reference
2903vector<bool, _Allocator>::at(size_type __n)
2904{
2905 if (__n >= size())
2906 this->__throw_out_of_range();
2907 return (*this)[__n];
2908}
2909
2910template <class _Allocator>
2911typename vector<bool, _Allocator>::const_reference
2912vector<bool, _Allocator>::at(size_type __n) const
2913{
2914 if (__n >= size())
2915 this->__throw_out_of_range();
2916 return (*this)[__n];
2917}
2918
2919template <class _Allocator>
2920void
2921vector<bool, _Allocator>::push_back(const value_type& __x)
2922{
2923 if (this->__size_ == this->capacity())
2924 reserve(__recommend(this->__size_ + 1));
2925 ++this->__size_;
2926 back() = __x;
2927}
2928
2929template <class _Allocator>
2930typename vector<bool, _Allocator>::iterator
2931vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2932{
2933 iterator __r;
2934 if (size() < capacity())
2935 {
2936 const_iterator __old_end = end();
2937 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002938 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002939 __r = __const_iterator_cast(__position);
2940 }
2941 else
2942 {
2943 vector __v(__alloc());
2944 __v.reserve(__recommend(__size_ + 1));
2945 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002946 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2947 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002948 swap(__v);
2949 }
2950 *__r = __x;
2951 return __r;
2952}
2953
2954template <class _Allocator>
2955typename vector<bool, _Allocator>::iterator
2956vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
2957{
2958 iterator __r;
2959 size_type __c = capacity();
2960 if (__n <= __c && size() <= __c - __n)
2961 {
2962 const_iterator __old_end = end();
2963 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002964 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002965 __r = __const_iterator_cast(__position);
2966 }
2967 else
2968 {
2969 vector __v(__alloc());
2970 __v.reserve(__recommend(__size_ + __n));
2971 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002972 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2973 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002974 swap(__v);
2975 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002976 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002977 return __r;
2978}
2979
2980template <class _Allocator>
2981template <class _InputIterator>
2982typename enable_if
2983<
2984 __is_input_iterator <_InputIterator>::value &&
2985 !__is_forward_iterator<_InputIterator>::value,
2986 typename vector<bool, _Allocator>::iterator
2987>::type
2988vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
2989{
2990 difference_type __off = __position - begin();
2991 iterator __p = __const_iterator_cast(__position);
2992 iterator __old_end = end();
2993 for (; size() != capacity() && __first != __last; ++__first)
2994 {
2995 ++this->__size_;
2996 back() = *__first;
2997 }
2998 vector __v(__alloc());
2999 if (__first != __last)
3000 {
3001#ifndef _LIBCPP_NO_EXCEPTIONS
3002 try
3003 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003004#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003005 __v.assign(__first, __last);
3006 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3007 difference_type __old_p = __p - begin();
3008 reserve(__recommend(size() + __v.size()));
3009 __p = begin() + __old_p;
3010 __old_end = begin() + __old_size;
3011#ifndef _LIBCPP_NO_EXCEPTIONS
3012 }
3013 catch (...)
3014 {
3015 erase(__old_end, end());
3016 throw;
3017 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003018#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003019 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003020 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003021 insert(__p, __v.begin(), __v.end());
3022 return begin() + __off;
3023}
3024
3025template <class _Allocator>
3026template <class _ForwardIterator>
3027typename enable_if
3028<
3029 __is_forward_iterator<_ForwardIterator>::value,
3030 typename vector<bool, _Allocator>::iterator
3031>::type
3032vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3033{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003034 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003035 iterator __r;
3036 size_type __c = capacity();
3037 if (__n <= __c && size() <= __c - __n)
3038 {
3039 const_iterator __old_end = end();
3040 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003041 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003042 __r = __const_iterator_cast(__position);
3043 }
3044 else
3045 {
3046 vector __v(__alloc());
3047 __v.reserve(__recommend(__size_ + __n));
3048 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003049 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3050 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003051 swap(__v);
3052 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003053 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003054 return __r;
3055}
3056
3057template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003058inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003059typename vector<bool, _Allocator>::iterator
3060vector<bool, _Allocator>::erase(const_iterator __position)
3061{
3062 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003063 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003064 --__size_;
3065 return __r;
3066}
3067
3068template <class _Allocator>
3069typename vector<bool, _Allocator>::iterator
3070vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3071{
3072 iterator __r = __const_iterator_cast(__first);
3073 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003074 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003075 __size_ -= __d;
3076 return __r;
3077}
3078
3079template <class _Allocator>
3080void
3081vector<bool, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003082 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3083 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003084{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003085 _VSTD::swap(this->__begin_, __x.__begin_);
3086 _VSTD::swap(this->__size_, __x.__size_);
3087 _VSTD::swap(this->__cap(), __x.__cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003088 __swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003089}
3090
Howard Hinnant324bb032010-08-22 00:02:43 +00003091template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003092void
3093vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3094{
3095 size_type __cs = size();
3096 if (__cs < __sz)
3097 {
3098 iterator __r;
3099 size_type __c = capacity();
3100 size_type __n = __sz - __cs;
3101 if (__n <= __c && __cs <= __c - __n)
3102 {
3103 __r = end();
3104 __size_ += __n;
3105 }
3106 else
3107 {
3108 vector __v(__alloc());
3109 __v.reserve(__recommend(__size_ + __n));
3110 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003111 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003112 swap(__v);
3113 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003114 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003115 }
3116 else
3117 __size_ = __sz;
3118}
3119
Howard Hinnant324bb032010-08-22 00:02:43 +00003120template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003121void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003122vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003123{
3124 // do middle whole words
3125 size_type __n = __size_;
3126 __storage_pointer __p = __begin_;
3127 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3128 *__p = ~*__p;
3129 // do last partial word
3130 if (__n > 0)
3131 {
3132 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3133 __storage_type __b = *__p & __m;
3134 *__p &= ~__m;
3135 *__p |= ~__b & __m;
3136 }
3137}
3138
Howard Hinnant324bb032010-08-22 00:02:43 +00003139template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003140bool
3141vector<bool, _Allocator>::__invariants() const
3142{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00003143 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003144 {
3145 if (this->__size_ != 0 || this->__cap() != 0)
3146 return false;
3147 }
3148 else
3149 {
3150 if (this->__cap() == 0)
3151 return false;
3152 if (this->__size_ > this->capacity())
3153 return false;
3154 }
3155 return true;
3156}
3157
Howard Hinnant324bb032010-08-22 00:02:43 +00003158template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003159size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003160vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003161{
3162 size_t __h = 0;
3163 // do middle whole words
3164 size_type __n = __size_;
3165 __storage_pointer __p = __begin_;
3166 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3167 __h ^= *__p;
3168 // do last partial word
3169 if (__n > 0)
3170 {
3171 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3172 __h ^= *__p & __m;
3173 }
3174 return __h;
3175}
3176
3177template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003178struct _LIBCPP_TYPE_VIS_ONLY hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003179 : public unary_function<vector<bool, _Allocator>, size_t>
3180{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00003181 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003182 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003183 {return __vec.__hash_code();}
3184};
3185
3186template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003187inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003188bool
3189operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3190{
3191 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003192 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003193}
3194
3195template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003196inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003197bool
3198operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3199{
3200 return !(__x == __y);
3201}
3202
3203template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003204inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003205bool
3206operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3207{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003208 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003209}
3210
3211template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003212inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003213bool
3214operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3215{
3216 return __y < __x;
3217}
3218
3219template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003220inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003221bool
3222operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3223{
3224 return !(__x < __y);
3225}
3226
3227template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003228inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003229bool
3230operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3231{
3232 return !(__y < __x);
3233}
3234
3235template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003236inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003237void
3238swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003239 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003240{
3241 __x.swap(__y);
3242}
3243
3244_LIBCPP_END_NAMESPACE_STD
3245
3246#endif // _LIBCPP_VECTOR