blob: 258a950dc7d204a33fc108fb66fea62b8a2a18ea [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>
443_LIBCPP_INLINE_VISIBILITY inline
444void
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>
452_LIBCPP_INLINE_VISIBILITY inline
453__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>
462_LIBCPP_INLINE_VISIBILITY inline
463__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>
527 vector(_InputIterator __first, _InputIterator __last,
528 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,
532 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000533 template <class _InputIterator>
534 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
535 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000536 !__is_forward_iterator<_InputIterator>::value &&
537 is_constructible<
538 value_type,
539 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000540 template <class _ForwardIterator>
541 vector(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000542 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
543 is_constructible<
544 value_type,
545 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000546 template <class _ForwardIterator>
547 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000548 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
549 is_constructible<
550 value_type,
551 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000552#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000554 vector(initializer_list<value_type> __il);
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, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000557#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantabe26282011-09-16 17:29:17 +0000558#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7a563db2011-09-14 18:33:51 +0000560 ~vector()
561 {
562 __get_db()->__erase_c(this);
563 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000564#endif
565
566 vector(const vector& __x);
567 vector(const vector& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000569 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000570#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000572 vector(vector&& __x)
573 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000574 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000575 vector(vector&& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000577 vector& operator=(vector&& __x)
578 _NOEXCEPT_(
579 __alloc_traits::propagate_on_container_move_assignment::value &&
580 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000581#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000582#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000583 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000584 vector& operator=(initializer_list<value_type> __il)
585 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000586#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000587
588 template <class _InputIterator>
589 typename enable_if
590 <
591 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000592 !__is_forward_iterator<_InputIterator>::value &&
593 is_constructible<
594 value_type,
595 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000596 void
597 >::type
598 assign(_InputIterator __first, _InputIterator __last);
599 template <class _ForwardIterator>
600 typename enable_if
601 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000602 __is_forward_iterator<_ForwardIterator>::value &&
603 is_constructible<
604 value_type,
605 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000606 void
607 >::type
608 assign(_ForwardIterator __first, _ForwardIterator __last);
609
610 void assign(size_type __n, const_reference __u);
Howard Hinnante3e32912011-08-12 21:56:02 +0000611#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000612 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000613 void assign(initializer_list<value_type> __il)
614 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000615#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000616
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000617 _LIBCPP_INLINE_VISIBILITY
618 allocator_type get_allocator() const _NOEXCEPT
619 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000620
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000621 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
622 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
623 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
624 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000625
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000626 _LIBCPP_INLINE_VISIBILITY
627 reverse_iterator rbegin() _NOEXCEPT
628 {return reverse_iterator(end());}
629 _LIBCPP_INLINE_VISIBILITY
630 const_reverse_iterator rbegin() const _NOEXCEPT
631 {return const_reverse_iterator(end());}
632 _LIBCPP_INLINE_VISIBILITY
633 reverse_iterator rend() _NOEXCEPT
634 {return reverse_iterator(begin());}
635 _LIBCPP_INLINE_VISIBILITY
636 const_reverse_iterator rend() const _NOEXCEPT
637 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000638
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000639 _LIBCPP_INLINE_VISIBILITY
640 const_iterator cbegin() const _NOEXCEPT
641 {return begin();}
642 _LIBCPP_INLINE_VISIBILITY
643 const_iterator cend() const _NOEXCEPT
644 {return end();}
645 _LIBCPP_INLINE_VISIBILITY
646 const_reverse_iterator crbegin() const _NOEXCEPT
647 {return rbegin();}
648 _LIBCPP_INLINE_VISIBILITY
649 const_reverse_iterator crend() const _NOEXCEPT
650 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000651
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000652 _LIBCPP_INLINE_VISIBILITY
653 size_type size() const _NOEXCEPT
654 {return static_cast<size_type>(this->__end_ - this->__begin_);}
655 _LIBCPP_INLINE_VISIBILITY
656 size_type capacity() const _NOEXCEPT
657 {return __base::capacity();}
658 _LIBCPP_INLINE_VISIBILITY
659 bool empty() const _NOEXCEPT
660 {return this->__begin_ == this->__end_;}
661 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000662 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000663 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664
665 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
666 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
667 reference at(size_type __n);
668 const_reference at(size_type __n) const;
669
Howard Hinnant7a563db2011-09-14 18:33:51 +0000670 _LIBCPP_INLINE_VISIBILITY reference front()
671 {
672 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
673 return *this->__begin_;
674 }
675 _LIBCPP_INLINE_VISIBILITY const_reference front() const
676 {
677 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
678 return *this->__begin_;
679 }
680 _LIBCPP_INLINE_VISIBILITY reference back()
681 {
682 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
683 return *(this->__end_ - 1);
684 }
685 _LIBCPP_INLINE_VISIBILITY const_reference back() const
686 {
687 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
688 return *(this->__end_ - 1);
689 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000690
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000691 _LIBCPP_INLINE_VISIBILITY
692 value_type* data() _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000693 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000694 _LIBCPP_INLINE_VISIBILITY
695 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000696 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000697
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000698 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000699#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000700 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000701#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000702 template <class... _Args>
703 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000704#endif // _LIBCPP_HAS_NO_VARIADICS
705#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000706 void pop_back();
707
708 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000709#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000710 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000711#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000712 template <class... _Args>
713 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000714#endif // _LIBCPP_HAS_NO_VARIADICS
715#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000716 iterator insert(const_iterator __position, size_type __n, const_reference __x);
717 template <class _InputIterator>
718 typename enable_if
719 <
720 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000721 !__is_forward_iterator<_InputIterator>::value &&
722 is_constructible<
723 value_type,
724 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000725 iterator
726 >::type
727 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
728 template <class _ForwardIterator>
729 typename enable_if
730 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000731 __is_forward_iterator<_ForwardIterator>::value &&
732 is_constructible<
733 value_type,
734 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000735 iterator
736 >::type
737 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +0000738#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000740 iterator insert(const_iterator __position, initializer_list<value_type> __il)
741 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000742#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000743
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000744 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000745 iterator erase(const_iterator __first, const_iterator __last);
746
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000747 _LIBCPP_INLINE_VISIBILITY
748 void clear() _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000749 {
750 __base::clear();
751 __invalidate_all_iterators();
752 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000753
754 void resize(size_type __sz);
755 void resize(size_type __sz, const_reference __x);
756
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000757 void swap(vector&)
758 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
759 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000760
761 bool __invariants() const;
762
Howard Hinnantabe26282011-09-16 17:29:17 +0000763#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000764
765 bool __dereferenceable(const const_iterator* __i) const;
766 bool __decrementable(const const_iterator* __i) const;
767 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
768 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
769
Howard Hinnantabe26282011-09-16 17:29:17 +0000770#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000771
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000772private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000773 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000774 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000775 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000776 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31 +0000777 void __construct_at_end(size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000778 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000779 template <class _ForwardIterator>
780 typename enable_if
781 <
782 __is_forward_iterator<_ForwardIterator>::value,
783 void
784 >::type
785 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
786 void __move_construct_at_end(pointer __first, pointer __last);
787 void __append(size_type __n);
788 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000790 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000792 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000793 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
794 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
795 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000796 void __move_assign(vector& __c, true_type)
797 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000798 void __move_assign(vector& __c, false_type);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000799 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000800 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000801 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000802#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000803 __c_node* __c = __get_db()->__find_c_and_lock(this);
804 for (__i_node** __p = __c->end_; __p != __c->beg_; )
805 {
806 --__p;
807 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
808 if (__i->base() > __new_last)
809 {
810 (*__p)->__c_ = nullptr;
811 if (--__c->end_ != __p)
812 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
813 }
814 }
815 __get_db()->unlock();
816#endif
817 __base::__destruct_at_end(__new_last);
818 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000819 template <class _Up>
820 void
821#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
822 __push_back_slow_path(_Up&& __x);
823#else
824 __push_back_slow_path(_Up& __x);
825#endif
Howard Hinnant0438ea22012-02-26 15:30:12 +0000826#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
827 template <class... _Args>
828 void
829 __emplace_back_slow_path(_Args&&... __args);
830#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000831};
832
833template <class _Tp, class _Allocator>
834void
835vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
836{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000837 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000838 _VSTD::swap(this->__begin_, __v.__begin_);
839 _VSTD::swap(this->__end_, __v.__end_);
840 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000841 __v.__first_ = __v.__begin_;
842 __invalidate_all_iterators();
843}
844
845template <class _Tp, class _Allocator>
846typename vector<_Tp, _Allocator>::pointer
847vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
848{
849 pointer __r = __v.__begin_;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000850 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
851 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000852 _VSTD::swap(this->__begin_, __v.__begin_);
853 _VSTD::swap(this->__end_, __v.__end_);
854 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000855 __v.__first_ = __v.__begin_;
856 __invalidate_all_iterators();
857 return __r;
858}
859
860// Allocate space for __n objects
861// throws length_error if __n > max_size()
862// throws (probably bad_alloc) if memory run out
863// Precondition: __begin_ == __end_ == __end_cap() == 0
864// Precondition: __n > 0
865// Postcondition: capacity() == __n
866// Postcondition: size() == 0
867template <class _Tp, class _Allocator>
868void
869vector<_Tp, _Allocator>::allocate(size_type __n)
870{
871 if (__n > max_size())
872 this->__throw_length_error();
873 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
874 this->__end_cap() = this->__begin_ + __n;
875}
876
877template <class _Tp, class _Allocator>
878void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000879vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000880{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000881 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000882 {
883 clear();
884 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000885 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000886 }
887}
888
889template <class _Tp, class _Allocator>
890typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000891vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000892{
Sean Hunt110b8bf2011-07-29 23:31:58 +0000893 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 +0000894}
895
896// Precondition: __new_size > capacity()
897template <class _Tp, class _Allocator>
898_LIBCPP_INLINE_VISIBILITY inline
899typename vector<_Tp, _Allocator>::size_type
900vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
901{
902 const size_type __ms = max_size();
903 if (__new_size > __ms)
904 this->__throw_length_error();
905 const size_type __cap = capacity();
906 if (__cap >= __ms / 2)
907 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58 +0000908 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909}
910
911// Default constructs __n objects starting at __end_
912// throws if construction throws
913// Precondition: __n > 0
914// Precondition: size() + __n <= capacity()
915// Postcondition: size() == size() + __n
916template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000917void
918vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
919{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000920 allocator_type& __a = this->__alloc();
921 do
922 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000923 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000924 ++this->__end_;
925 --__n;
926 } while (__n > 0);
927}
928
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000929// Copy constructs __n objects starting at __end_ from __x
930// throws if construction throws
931// Precondition: __n > 0
932// Precondition: size() + __n <= capacity()
933// Postcondition: size() == old size() + __n
934// Postcondition: [i] == __x for all i in [size() - __n, __n)
935template <class _Tp, class _Allocator>
936_LIBCPP_INLINE_VISIBILITY inline
937void
938vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
939{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000940 allocator_type& __a = this->__alloc();
941 do
942 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000943 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000944 ++this->__end_;
945 --__n;
946 } while (__n > 0);
947}
948
949template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000950template <class _ForwardIterator>
951typename enable_if
952<
953 __is_forward_iterator<_ForwardIterator>::value,
954 void
955>::type
956vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
957{
958 allocator_type& __a = this->__alloc();
959 for (; __first != __last; ++__first)
960 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000961 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000962 ++this->__end_;
963 }
964}
965
966template <class _Tp, class _Allocator>
967void
968vector<_Tp, _Allocator>::__move_construct_at_end(pointer __first, pointer __last)
969{
970 allocator_type& __a = this->__alloc();
971 for (; __first != __last; ++__first)
972 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000973 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
974 _VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000975 ++this->__end_;
976 }
977}
978
979// Default constructs __n objects starting at __end_
980// throws if construction throws
981// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000982// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000983template <class _Tp, class _Allocator>
984void
985vector<_Tp, _Allocator>::__append(size_type __n)
986{
987 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
988 this->__construct_at_end(__n);
989 else
990 {
991 allocator_type& __a = this->__alloc();
992 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
993 __v.__construct_at_end(__n);
994 __swap_out_circular_buffer(__v);
995 }
996}
997
998// Default constructs __n objects starting at __end_
999// throws if construction throws
1000// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001001// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001002template <class _Tp, class _Allocator>
1003void
1004vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1005{
1006 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1007 this->__construct_at_end(__n, __x);
1008 else
1009 {
1010 allocator_type& __a = this->__alloc();
1011 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1012 __v.__construct_at_end(__n, __x);
1013 __swap_out_circular_buffer(__v);
1014 }
1015}
1016
1017template <class _Tp, class _Allocator>
1018vector<_Tp, _Allocator>::vector(size_type __n)
1019{
Howard Hinnant0442b122011-09-16 18:41:29 +00001020#if _LIBCPP_DEBUG_LEVEL >= 2
1021 __get_db()->__insert_c(this);
1022#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001023 if (__n > 0)
1024 {
1025 allocate(__n);
1026 __construct_at_end(__n);
1027 }
1028}
1029
Marshall Clowa49a2c92013-09-14 00:47:59 +00001030#if _LIBCPP_STD_VER > 11
1031template <class _Tp, class _Allocator>
1032vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1033 : __base(__a)
1034{
1035#if _LIBCPP_DEBUG_LEVEL >= 2
1036 __get_db()->__insert_c(this);
1037#endif
1038 if (__n > 0)
1039 {
1040 allocate(__n);
1041 __construct_at_end(__n);
1042 }
1043}
1044#endif
1045
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001046template <class _Tp, class _Allocator>
1047vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1048{
Howard Hinnant0442b122011-09-16 18:41:29 +00001049#if _LIBCPP_DEBUG_LEVEL >= 2
1050 __get_db()->__insert_c(this);
1051#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001052 if (__n > 0)
1053 {
1054 allocate(__n);
1055 __construct_at_end(__n, __x);
1056 }
1057}
1058
1059template <class _Tp, class _Allocator>
1060vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1061 : __base(__a)
1062{
Howard Hinnant0442b122011-09-16 18:41:29 +00001063#if _LIBCPP_DEBUG_LEVEL >= 2
1064 __get_db()->__insert_c(this);
1065#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001066 if (__n > 0)
1067 {
1068 allocate(__n);
1069 __construct_at_end(__n, __x);
1070 }
1071}
1072
1073template <class _Tp, class _Allocator>
1074template <class _InputIterator>
1075vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
1076 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001077 !__is_forward_iterator<_InputIterator>::value &&
1078 is_constructible<
1079 value_type,
1080 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001081{
Howard Hinnantabe26282011-09-16 17:29:17 +00001082#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001083 __get_db()->__insert_c(this);
1084#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001085 for (; __first != __last; ++__first)
1086 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001087}
1088
1089template <class _Tp, class _Allocator>
1090template <class _InputIterator>
1091vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1092 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001093 !__is_forward_iterator<_InputIterator>::value &&
1094 is_constructible<
1095 value_type,
1096 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001097 : __base(__a)
1098{
Howard Hinnantabe26282011-09-16 17:29:17 +00001099#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001100 __get_db()->__insert_c(this);
1101#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001102 for (; __first != __last; ++__first)
1103 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001104}
1105
1106template <class _Tp, class _Allocator>
1107template <class _ForwardIterator>
1108vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001109 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1110 is_constructible<
1111 value_type,
1112 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001113{
Howard Hinnant0442b122011-09-16 18:41:29 +00001114#if _LIBCPP_DEBUG_LEVEL >= 2
1115 __get_db()->__insert_c(this);
1116#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001117 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001118 if (__n > 0)
1119 {
1120 allocate(__n);
1121 __construct_at_end(__first, __last);
1122 }
1123}
1124
1125template <class _Tp, class _Allocator>
1126template <class _ForwardIterator>
1127vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001128 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1129 is_constructible<
1130 value_type,
1131 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001132 : __base(__a)
1133{
Howard Hinnant0442b122011-09-16 18:41:29 +00001134#if _LIBCPP_DEBUG_LEVEL >= 2
1135 __get_db()->__insert_c(this);
1136#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001137 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001138 if (__n > 0)
1139 {
1140 allocate(__n);
1141 __construct_at_end(__first, __last);
1142 }
1143}
1144
1145template <class _Tp, class _Allocator>
1146vector<_Tp, _Allocator>::vector(const vector& __x)
1147 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1148{
Howard Hinnant0442b122011-09-16 18:41:29 +00001149#if _LIBCPP_DEBUG_LEVEL >= 2
1150 __get_db()->__insert_c(this);
1151#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001152 size_type __n = __x.size();
1153 if (__n > 0)
1154 {
1155 allocate(__n);
1156 __construct_at_end(__x.__begin_, __x.__end_);
1157 }
1158}
1159
1160template <class _Tp, class _Allocator>
1161vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1162 : __base(__a)
1163{
Howard Hinnant0442b122011-09-16 18:41:29 +00001164#if _LIBCPP_DEBUG_LEVEL >= 2
1165 __get_db()->__insert_c(this);
1166#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001167 size_type __n = __x.size();
1168 if (__n > 0)
1169 {
1170 allocate(__n);
1171 __construct_at_end(__x.__begin_, __x.__end_);
1172 }
1173}
1174
Howard Hinnant73d21a42010-09-04 23:28:19 +00001175#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001176
1177template <class _Tp, class _Allocator>
1178_LIBCPP_INLINE_VISIBILITY inline
1179vector<_Tp, _Allocator>::vector(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001180 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001181 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001182{
Howard Hinnantabe26282011-09-16 17:29:17 +00001183#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001184 __get_db()->__insert_c(this);
Howard Hinnante6125bd2011-09-19 16:34:29 +00001185 __get_db()->swap(this, &__x);
Howard Hinnant7a563db2011-09-14 18:33:51 +00001186#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001187 this->__begin_ = __x.__begin_;
1188 this->__end_ = __x.__end_;
1189 this->__end_cap() = __x.__end_cap();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001190 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001191}
1192
1193template <class _Tp, class _Allocator>
1194_LIBCPP_INLINE_VISIBILITY inline
1195vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1196 : __base(__a)
1197{
Howard Hinnant0442b122011-09-16 18:41:29 +00001198#if _LIBCPP_DEBUG_LEVEL >= 2
1199 __get_db()->__insert_c(this);
1200#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001201 if (__a == __x.__alloc())
1202 {
1203 this->__begin_ = __x.__begin_;
1204 this->__end_ = __x.__end_;
1205 this->__end_cap() = __x.__end_cap();
1206 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001207#if _LIBCPP_DEBUG_LEVEL >= 2
1208 __get_db()->swap(this, &__x);
1209#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001210 }
1211 else
1212 {
Howard Hinnant99968442011-11-29 18:15:50 +00001213 typedef move_iterator<iterator> _Ip;
1214 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001215 }
1216}
1217
Howard Hinnante3e32912011-08-12 21:56:02 +00001218#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1219
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001220template <class _Tp, class _Allocator>
1221_LIBCPP_INLINE_VISIBILITY inline
1222vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1223{
Howard Hinnant0442b122011-09-16 18:41:29 +00001224#if _LIBCPP_DEBUG_LEVEL >= 2
1225 __get_db()->__insert_c(this);
1226#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001227 if (__il.size() > 0)
1228 {
1229 allocate(__il.size());
1230 __construct_at_end(__il.begin(), __il.end());
1231 }
1232}
1233
1234template <class _Tp, class _Allocator>
1235_LIBCPP_INLINE_VISIBILITY inline
1236vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1237 : __base(__a)
1238{
Howard Hinnant0442b122011-09-16 18:41:29 +00001239#if _LIBCPP_DEBUG_LEVEL >= 2
1240 __get_db()->__insert_c(this);
1241#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001242 if (__il.size() > 0)
1243 {
1244 allocate(__il.size());
1245 __construct_at_end(__il.begin(), __il.end());
1246 }
1247}
1248
Howard Hinnante3e32912011-08-12 21:56:02 +00001249#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1250
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001251template <class _Tp, class _Allocator>
1252_LIBCPP_INLINE_VISIBILITY inline
1253vector<_Tp, _Allocator>&
1254vector<_Tp, _Allocator>::operator=(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001255 _NOEXCEPT_(
1256 __alloc_traits::propagate_on_container_move_assignment::value &&
1257 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001258{
1259 __move_assign(__x, integral_constant<bool,
1260 __alloc_traits::propagate_on_container_move_assignment::value>());
1261 return *this;
1262}
1263
1264template <class _Tp, class _Allocator>
1265void
1266vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1267{
1268 if (__base::__alloc() != __c.__alloc())
1269 {
Howard Hinnant99968442011-11-29 18:15:50 +00001270 typedef move_iterator<iterator> _Ip;
1271 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001272 }
1273 else
1274 __move_assign(__c, true_type());
1275}
1276
1277template <class _Tp, class _Allocator>
1278void
1279vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001280 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001281{
1282 deallocate();
1283 this->__begin_ = __c.__begin_;
1284 this->__end_ = __c.__end_;
1285 this->__end_cap() = __c.__end_cap();
1286 __base::__move_assign_alloc(__c);
1287 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001288#if _LIBCPP_DEBUG_LEVEL >= 2
1289 __get_db()->swap(this, &__c);
1290#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001291}
1292
Howard Hinnant73d21a42010-09-04 23:28:19 +00001293#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001294
1295template <class _Tp, class _Allocator>
1296_LIBCPP_INLINE_VISIBILITY inline
1297vector<_Tp, _Allocator>&
1298vector<_Tp, _Allocator>::operator=(const vector& __x)
1299{
1300 if (this != &__x)
1301 {
1302 __base::__copy_assign_alloc(__x);
1303 assign(__x.__begin_, __x.__end_);
1304 }
1305 return *this;
1306}
1307
1308template <class _Tp, class _Allocator>
1309template <class _InputIterator>
1310typename enable_if
1311<
1312 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001313 !__is_forward_iterator<_InputIterator>::value &&
1314 is_constructible<
1315 _Tp,
1316 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001317 void
1318>::type
1319vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1320{
1321 clear();
1322 for (; __first != __last; ++__first)
1323 push_back(*__first);
1324}
1325
1326template <class _Tp, class _Allocator>
1327template <class _ForwardIterator>
1328typename enable_if
1329<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001330 __is_forward_iterator<_ForwardIterator>::value &&
1331 is_constructible<
1332 _Tp,
1333 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001334 void
1335>::type
1336vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1337{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001338 typename iterator_traits<_ForwardIterator>::difference_type __new_size = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001339 if (static_cast<size_type>(__new_size) <= capacity())
1340 {
1341 _ForwardIterator __mid = __last;
1342 bool __growing = false;
1343 if (static_cast<size_type>(__new_size) > size())
1344 {
1345 __growing = true;
1346 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001347 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001348 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001349 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001350 if (__growing)
1351 __construct_at_end(__mid, __last);
1352 else
1353 this->__destruct_at_end(__m);
1354 }
1355 else
1356 {
1357 deallocate();
1358 allocate(__recommend(static_cast<size_type>(__new_size)));
1359 __construct_at_end(__first, __last);
1360 }
1361}
1362
1363template <class _Tp, class _Allocator>
1364void
1365vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1366{
1367 if (__n <= capacity())
1368 {
1369 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001370 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001371 if (__n > __s)
1372 __construct_at_end(__n - __s, __u);
1373 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001374 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001375 }
1376 else
1377 {
1378 deallocate();
1379 allocate(__recommend(static_cast<size_type>(__n)));
1380 __construct_at_end(__n, __u);
1381 }
1382}
1383
Howard Hinnant324bb032010-08-22 00:02:43 +00001384template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001385_LIBCPP_INLINE_VISIBILITY inline
1386typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001387vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001388{
Howard Hinnantabe26282011-09-16 17:29:17 +00001389#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001390 return iterator(this, __p);
1391#else
1392 return iterator(__p);
1393#endif
1394}
1395
Howard Hinnant324bb032010-08-22 00:02:43 +00001396template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001397_LIBCPP_INLINE_VISIBILITY inline
1398typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001399vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001400{
Howard Hinnantabe26282011-09-16 17:29:17 +00001401#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001402 return const_iterator(this, __p);
1403#else
1404 return const_iterator(__p);
1405#endif
1406}
1407
Howard Hinnant324bb032010-08-22 00:02:43 +00001408template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001409_LIBCPP_INLINE_VISIBILITY inline
1410typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001411vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001412{
1413 return __make_iter(this->__begin_);
1414}
1415
Howard Hinnant324bb032010-08-22 00:02:43 +00001416template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001417_LIBCPP_INLINE_VISIBILITY inline
1418typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001419vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001420{
1421 return __make_iter(this->__begin_);
1422}
1423
Howard Hinnant324bb032010-08-22 00:02:43 +00001424template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001425_LIBCPP_INLINE_VISIBILITY inline
1426typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001427vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001428{
1429 return __make_iter(this->__end_);
1430}
1431
Howard Hinnant324bb032010-08-22 00:02:43 +00001432template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001433_LIBCPP_INLINE_VISIBILITY inline
1434typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001435vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001436{
1437 return __make_iter(this->__end_);
1438}
1439
Howard Hinnant324bb032010-08-22 00:02:43 +00001440template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441_LIBCPP_INLINE_VISIBILITY inline
1442typename vector<_Tp, _Allocator>::reference
1443vector<_Tp, _Allocator>::operator[](size_type __n)
1444{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001445 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001446 return this->__begin_[__n];
1447}
1448
Howard Hinnant324bb032010-08-22 00:02:43 +00001449template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001450_LIBCPP_INLINE_VISIBILITY inline
1451typename vector<_Tp, _Allocator>::const_reference
1452vector<_Tp, _Allocator>::operator[](size_type __n) const
1453{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001454 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001455 return this->__begin_[__n];
1456}
1457
Howard Hinnant324bb032010-08-22 00:02:43 +00001458template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001459typename vector<_Tp, _Allocator>::reference
1460vector<_Tp, _Allocator>::at(size_type __n)
1461{
1462 if (__n >= size())
1463 this->__throw_out_of_range();
1464 return this->__begin_[__n];
1465}
1466
Howard Hinnant324bb032010-08-22 00:02:43 +00001467template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001468typename vector<_Tp, _Allocator>::const_reference
1469vector<_Tp, _Allocator>::at(size_type __n) const
1470{
1471 if (__n >= size())
1472 this->__throw_out_of_range();
1473 return this->__begin_[__n];
1474}
1475
1476template <class _Tp, class _Allocator>
1477void
1478vector<_Tp, _Allocator>::reserve(size_type __n)
1479{
1480 if (__n > capacity())
1481 {
1482 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001483 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001484 __swap_out_circular_buffer(__v);
1485 }
1486}
1487
1488template <class _Tp, class _Allocator>
1489void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001490vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491{
1492 if (capacity() > size())
1493 {
1494#ifndef _LIBCPP_NO_EXCEPTIONS
1495 try
1496 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001497#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001498 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001499 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001500 __swap_out_circular_buffer(__v);
1501#ifndef _LIBCPP_NO_EXCEPTIONS
1502 }
1503 catch (...)
1504 {
1505 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001506#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001507 }
1508}
1509
1510template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001511template <class _Up>
1512void
1513#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1514vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1515#else
1516vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1517#endif
1518{
1519 allocator_type& __a = this->__alloc();
1520 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1521 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnantf619e232013-01-11 20:36:59 +00001522 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1523 __v.__end_++;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001524 __swap_out_circular_buffer(__v);
1525}
1526
1527template <class _Tp, class _Allocator>
1528_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001529void
1530vector<_Tp, _Allocator>::push_back(const_reference __x)
1531{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001532 if (this->__end_ != this->__end_cap())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001533 {
1534 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001535 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001536 ++this->__end_;
1537 }
1538 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001539 __push_back_slow_path(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001540}
1541
Howard Hinnant73d21a42010-09-04 23:28:19 +00001542#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001543
1544template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001545_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001546void
1547vector<_Tp, _Allocator>::push_back(value_type&& __x)
1548{
1549 if (this->__end_ < this->__end_cap())
1550 {
1551 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001552 _VSTD::__to_raw_pointer(this->__end_),
1553 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001554 ++this->__end_;
1555 }
1556 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001557 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001558}
1559
Howard Hinnant73d21a42010-09-04 23:28:19 +00001560#ifndef _LIBCPP_HAS_NO_VARIADICS
1561
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001562template <class _Tp, class _Allocator>
1563template <class... _Args>
1564void
Howard Hinnant0438ea22012-02-26 15:30:12 +00001565vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1566{
1567 allocator_type& __a = this->__alloc();
1568 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1569// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantf619e232013-01-11 20:36:59 +00001570 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1571 __v.__end_++;
Howard Hinnant0438ea22012-02-26 15:30:12 +00001572 __swap_out_circular_buffer(__v);
1573}
1574
1575template <class _Tp, class _Allocator>
1576template <class... _Args>
1577_LIBCPP_INLINE_VISIBILITY inline
1578void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001579vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1580{
1581 if (this->__end_ < this->__end_cap())
1582 {
1583 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001584 _VSTD::__to_raw_pointer(this->__end_),
1585 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001586 ++this->__end_;
1587 }
1588 else
Howard Hinnant0438ea22012-02-26 15:30:12 +00001589 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590}
1591
Howard Hinnant73d21a42010-09-04 23:28:19 +00001592#endif // _LIBCPP_HAS_NO_VARIADICS
1593#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001594
1595template <class _Tp, class _Allocator>
1596_LIBCPP_INLINE_VISIBILITY inline
1597void
1598vector<_Tp, _Allocator>::pop_back()
1599{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001600 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001601 this->__destruct_at_end(this->__end_ - 1);
1602}
1603
1604template <class _Tp, class _Allocator>
1605_LIBCPP_INLINE_VISIBILITY inline
1606typename vector<_Tp, _Allocator>::iterator
1607vector<_Tp, _Allocator>::erase(const_iterator __position)
1608{
Howard Hinnantabe26282011-09-16 17:29:17 +00001609#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001610 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1611 "vector::erase(iterator) called with an iterator not"
1612 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001613#endif
Howard Hinnant782da332013-03-25 22:12:26 +00001614 _LIBCPP_ASSERT(__position != end(),
1615 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001616 difference_type __ps = __position - cbegin();
1617 pointer __p = this->__begin_ + __ps;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001619 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001620 return __r;
1621}
1622
1623template <class _Tp, class _Allocator>
1624typename vector<_Tp, _Allocator>::iterator
1625vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1626{
Howard Hinnantabe26282011-09-16 17:29:17 +00001627#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001628 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1629 "vector::erase(iterator, iterator) called with an iterator not"
1630 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001631#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +00001632 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001633 pointer __p = this->__begin_ + (__first - begin());
1634 iterator __r = __make_iter(__p);
Howard Hinnantb4e67cf2013-04-18 15:02:57 +00001635 if (__first != __last)
1636 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001637 return __r;
1638}
1639
1640template <class _Tp, class _Allocator>
1641void
1642vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1643{
1644 pointer __old_last = this->__end_;
1645 difference_type __n = __old_last - __to;
1646 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1647 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001648 _VSTD::__to_raw_pointer(this->__end_),
1649 _VSTD::move(*__i));
1650 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001651}
1652
1653template <class _Tp, class _Allocator>
1654typename vector<_Tp, _Allocator>::iterator
1655vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1656{
Howard Hinnantabe26282011-09-16 17:29:17 +00001657#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001658 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1659 "vector::insert(iterator, x) called with an iterator not"
1660 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001661#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001662 pointer __p = this->__begin_ + (__position - begin());
1663 if (this->__end_ < this->__end_cap())
1664 {
1665 if (__p == this->__end_)
1666 {
1667 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001668 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001669 ++this->__end_;
1670 }
1671 else
1672 {
1673 __move_range(__p, this->__end_, __p + 1);
1674 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1675 if (__p <= __xr && __xr < this->__end_)
1676 ++__xr;
1677 *__p = *__xr;
1678 }
1679 }
1680 else
1681 {
1682 allocator_type& __a = this->__alloc();
1683 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1684 __v.push_back(__x);
1685 __p = __swap_out_circular_buffer(__v, __p);
1686 }
1687 return __make_iter(__p);
1688}
1689
Howard Hinnant73d21a42010-09-04 23:28:19 +00001690#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001691
1692template <class _Tp, class _Allocator>
1693typename vector<_Tp, _Allocator>::iterator
1694vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1695{
Howard Hinnantabe26282011-09-16 17:29:17 +00001696#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001697 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1698 "vector::insert(iterator, x) called with an iterator not"
1699 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001700#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001701 pointer __p = this->__begin_ + (__position - begin());
1702 if (this->__end_ < this->__end_cap())
1703 {
1704 if (__p == this->__end_)
1705 {
1706 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001707 _VSTD::__to_raw_pointer(this->__end_),
1708 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001709 ++this->__end_;
1710 }
1711 else
1712 {
1713 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001714 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001715 }
1716 }
1717 else
1718 {
1719 allocator_type& __a = this->__alloc();
1720 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001721 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001722 __p = __swap_out_circular_buffer(__v, __p);
1723 }
1724 return __make_iter(__p);
1725}
1726
Howard Hinnant73d21a42010-09-04 23:28:19 +00001727#ifndef _LIBCPP_HAS_NO_VARIADICS
1728
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001729template <class _Tp, class _Allocator>
1730template <class... _Args>
1731typename vector<_Tp, _Allocator>::iterator
1732vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1733{
Howard Hinnantabe26282011-09-16 17:29:17 +00001734#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001735 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1736 "vector::emplace(iterator, x) called with an iterator not"
1737 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001738#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001739 pointer __p = this->__begin_ + (__position - begin());
1740 if (this->__end_ < this->__end_cap())
1741 {
1742 if (__p == this->__end_)
1743 {
1744 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001745 _VSTD::__to_raw_pointer(this->__end_),
1746 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001747 ++this->__end_;
1748 }
1749 else
1750 {
Howard Hinnanta58402a2012-07-08 23:23:04 +00001751 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001752 __move_range(__p, this->__end_, __p + 1);
Howard Hinnanta58402a2012-07-08 23:23:04 +00001753 *__p = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001754 }
1755 }
1756 else
1757 {
1758 allocator_type& __a = this->__alloc();
1759 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001760 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001761 __p = __swap_out_circular_buffer(__v, __p);
1762 }
1763 return __make_iter(__p);
1764}
1765
Howard Hinnant73d21a42010-09-04 23:28:19 +00001766#endif // _LIBCPP_HAS_NO_VARIADICS
1767#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001768
1769template <class _Tp, class _Allocator>
1770typename vector<_Tp, _Allocator>::iterator
1771vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1772{
Howard Hinnantabe26282011-09-16 17:29:17 +00001773#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001774 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1775 "vector::insert(iterator, n, x) called with an iterator not"
1776 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001777#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001778 pointer __p = this->__begin_ + (__position - begin());
1779 if (__n > 0)
1780 {
1781 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1782 {
1783 size_type __old_n = __n;
1784 pointer __old_last = this->__end_;
1785 if (__n > static_cast<size_type>(this->__end_ - __p))
1786 {
1787 size_type __cx = __n - (this->__end_ - __p);
1788 __construct_at_end(__cx, __x);
1789 __n -= __cx;
1790 }
1791 if (__n > 0)
1792 {
1793 __move_range(__p, __old_last, __p + __old_n);
1794 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1795 if (__p <= __xr && __xr < this->__end_)
1796 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001797 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001798 }
1799 }
1800 else
1801 {
1802 allocator_type& __a = this->__alloc();
1803 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1804 __v.__construct_at_end(__n, __x);
1805 __p = __swap_out_circular_buffer(__v, __p);
1806 }
1807 }
1808 return __make_iter(__p);
1809}
1810
1811template <class _Tp, class _Allocator>
1812template <class _InputIterator>
1813typename enable_if
1814<
1815 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001816 !__is_forward_iterator<_InputIterator>::value &&
1817 is_constructible<
1818 _Tp,
1819 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001820 typename vector<_Tp, _Allocator>::iterator
1821>::type
1822vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1823{
Howard Hinnantabe26282011-09-16 17:29:17 +00001824#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001825 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1826 "vector::insert(iterator, range) called with an iterator not"
1827 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001828#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829 difference_type __off = __position - begin();
1830 pointer __p = this->__begin_ + __off;
1831 allocator_type& __a = this->__alloc();
1832 pointer __old_last = this->__end_;
1833 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1834 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001835 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001836 *__first);
1837 ++this->__end_;
1838 }
1839 __split_buffer<value_type, allocator_type&> __v(__a);
1840 if (__first != __last)
1841 {
1842#ifndef _LIBCPP_NO_EXCEPTIONS
1843 try
1844 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001845#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001846 __v.__construct_at_end(__first, __last);
1847 difference_type __old_size = __old_last - this->__begin_;
1848 difference_type __old_p = __p - this->__begin_;
1849 reserve(__recommend(size() + __v.size()));
1850 __p = this->__begin_ + __old_p;
1851 __old_last = this->__begin_ + __old_size;
1852#ifndef _LIBCPP_NO_EXCEPTIONS
1853 }
1854 catch (...)
1855 {
1856 erase(__make_iter(__old_last), end());
1857 throw;
1858 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001859#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001860 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001861 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant0442b122011-09-16 18:41:29 +00001862 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1863 make_move_iterator(__v.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001864 return begin() + __off;
1865}
1866
1867template <class _Tp, class _Allocator>
1868template <class _ForwardIterator>
1869typename enable_if
1870<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001871 __is_forward_iterator<_ForwardIterator>::value &&
1872 is_constructible<
1873 _Tp,
1874 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001875 typename vector<_Tp, _Allocator>::iterator
1876>::type
1877vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1878{
Howard Hinnantabe26282011-09-16 17:29:17 +00001879#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001880 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1881 "vector::insert(iterator, range) called with an iterator not"
1882 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001883#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001884 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00001885 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001886 if (__n > 0)
1887 {
1888 if (__n <= this->__end_cap() - this->__end_)
1889 {
1890 size_type __old_n = __n;
1891 pointer __old_last = this->__end_;
1892 _ForwardIterator __m = __last;
1893 difference_type __dx = this->__end_ - __p;
1894 if (__n > __dx)
1895 {
1896 __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001897 _VSTD::advance(__m, this->__end_ - __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001898 __construct_at_end(__m, __last);
1899 __n = __dx;
1900 }
1901 if (__n > 0)
1902 {
1903 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001904 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001905 }
1906 }
1907 else
1908 {
1909 allocator_type& __a = this->__alloc();
1910 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1911 __v.__construct_at_end(__first, __last);
1912 __p = __swap_out_circular_buffer(__v, __p);
1913 }
1914 }
1915 return __make_iter(__p);
1916}
1917
1918template <class _Tp, class _Allocator>
1919void
1920vector<_Tp, _Allocator>::resize(size_type __sz)
1921{
1922 size_type __cs = size();
1923 if (__cs < __sz)
1924 this->__append(__sz - __cs);
1925 else if (__cs > __sz)
1926 this->__destruct_at_end(this->__begin_ + __sz);
1927}
1928
1929template <class _Tp, class _Allocator>
1930void
1931vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
1932{
1933 size_type __cs = size();
1934 if (__cs < __sz)
1935 this->__append(__sz - __cs, __x);
1936 else if (__cs > __sz)
1937 this->__destruct_at_end(this->__begin_ + __sz);
1938}
1939
1940template <class _Tp, class _Allocator>
1941void
1942vector<_Tp, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001943 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1944 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001945{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001946 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
1947 this->__alloc() == __x.__alloc(),
1948 "vector::swap: Either propagate_on_container_swap must be true"
1949 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +00001950 _VSTD::swap(this->__begin_, __x.__begin_);
1951 _VSTD::swap(this->__end_, __x.__end_);
1952 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953 __base::__swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnantabe26282011-09-16 17:29:17 +00001954#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001955 __get_db()->swap(this, &__x);
Howard Hinnantabe26282011-09-16 17:29:17 +00001956#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001957}
1958
Howard Hinnant324bb032010-08-22 00:02:43 +00001959template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001960bool
1961vector<_Tp, _Allocator>::__invariants() const
1962{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001963 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001964 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001965 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001966 return false;
1967 }
1968 else
1969 {
1970 if (this->__begin_ > this->__end_)
1971 return false;
1972 if (this->__begin_ == this->__end_cap())
1973 return false;
1974 if (this->__end_ > this->__end_cap())
1975 return false;
1976 }
1977 return true;
1978}
1979
Howard Hinnantabe26282011-09-16 17:29:17 +00001980#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001981
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001982template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:51 +00001983bool
1984vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
1985{
1986 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
1987}
1988
1989template <class _Tp, class _Allocator>
1990bool
1991vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
1992{
1993 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
1994}
1995
1996template <class _Tp, class _Allocator>
1997bool
1998vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
1999{
2000 const_pointer __p = __i->base() + __n;
2001 return this->__begin_ <= __p && __p <= this->__end_;
2002}
2003
2004template <class _Tp, class _Allocator>
2005bool
2006vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2007{
2008 const_pointer __p = __i->base() + __n;
2009 return this->__begin_ <= __p && __p < this->__end_;
2010}
2011
Howard Hinnantabe26282011-09-16 17:29:17 +00002012#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002013
2014template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002015_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002016void
2017vector<_Tp, _Allocator>::__invalidate_all_iterators()
2018{
Howard Hinnantabe26282011-09-16 17:29:17 +00002019#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002020 __get_db()->__invalidate_all(this);
Howard Hinnantabe26282011-09-16 17:29:17 +00002021#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002022}
2023
2024// vector<bool>
2025
2026template <class _Allocator> class vector<bool, _Allocator>;
2027
2028template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2029
2030template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002031struct __has_storage_type<vector<bool, _Allocator> >
2032{
2033 static const bool value = true;
2034};
2035
2036template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002037class _LIBCPP_TYPE_VIS_ONLY vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002038 : private __vector_base_common<true>
2039{
2040public:
2041 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00002042 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002043 typedef _Allocator allocator_type;
2044 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002045 typedef typename __alloc_traits::size_type size_type;
2046 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantf867f632012-05-07 16:50:38 +00002047 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002048 typedef __bit_iterator<vector, false> pointer;
2049 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002050 typedef pointer iterator;
2051 typedef const_pointer const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002052 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2053 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002054
2055private:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002056 typedef typename __alloc_traits::template
2057#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
2058 rebind_alloc<__storage_type>
2059#else
2060 rebind_alloc<__storage_type>::other
2061#endif
2062 __storage_allocator;
2063 typedef allocator_traits<__storage_allocator> __storage_traits;
2064 typedef typename __storage_traits::pointer __storage_pointer;
2065 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2066
2067 __storage_pointer __begin_;
2068 size_type __size_;
2069 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002070public:
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002071 typedef __bit_reference<vector> reference;
2072 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002073private:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002074 _LIBCPP_INLINE_VISIBILITY
2075 size_type& __cap() _NOEXCEPT
2076 {return __cap_alloc_.first();}
2077 _LIBCPP_INLINE_VISIBILITY
2078 const size_type& __cap() const _NOEXCEPT
2079 {return __cap_alloc_.first();}
2080 _LIBCPP_INLINE_VISIBILITY
2081 __storage_allocator& __alloc() _NOEXCEPT
2082 {return __cap_alloc_.second();}
2083 _LIBCPP_INLINE_VISIBILITY
2084 const __storage_allocator& __alloc() const _NOEXCEPT
2085 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002086
2087 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2088
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002089 _LIBCPP_INLINE_VISIBILITY
2090 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002091 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002092 _LIBCPP_INLINE_VISIBILITY
2093 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002094 {return (__n - 1) / __bits_per_word + 1;}
2095
2096public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002097 _LIBCPP_INLINE_VISIBILITY
2098 vector()
2099 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002100 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002101 ~vector();
2102 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59 +00002103#if _LIBCPP_STD_VER > 11
2104 explicit vector(size_type __n, const allocator_type& __a);
2105#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002106 vector(size_type __n, const value_type& __v);
2107 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2108 template <class _InputIterator>
2109 vector(_InputIterator __first, _InputIterator __last,
2110 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2111 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2112 template <class _InputIterator>
2113 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2114 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2115 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2116 template <class _ForwardIterator>
2117 vector(_ForwardIterator __first, _ForwardIterator __last,
2118 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2119 template <class _ForwardIterator>
2120 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2121 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2122
2123 vector(const vector& __v);
2124 vector(const vector& __v, const allocator_type& __a);
2125 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00002126#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002127 vector(initializer_list<value_type> __il);
2128 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00002129#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130
Howard Hinnant73d21a42010-09-04 23:28:19 +00002131#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002132 _LIBCPP_INLINE_VISIBILITY
2133 vector(vector&& __v)
2134 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002135 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002136 _LIBCPP_INLINE_VISIBILITY
2137 vector& operator=(vector&& __v)
2138 _NOEXCEPT_(
2139 __alloc_traits::propagate_on_container_move_assignment::value &&
2140 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002141#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +00002142#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002143 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002144 vector& operator=(initializer_list<value_type> __il)
2145 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00002146#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002147
2148 template <class _InputIterator>
2149 typename enable_if
2150 <
2151 __is_input_iterator<_InputIterator>::value &&
2152 !__is_forward_iterator<_InputIterator>::value,
2153 void
2154 >::type
2155 assign(_InputIterator __first, _InputIterator __last);
2156 template <class _ForwardIterator>
2157 typename enable_if
2158 <
2159 __is_forward_iterator<_ForwardIterator>::value,
2160 void
2161 >::type
2162 assign(_ForwardIterator __first, _ForwardIterator __last);
2163
2164 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +00002165#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002166 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002167 void assign(initializer_list<value_type> __il)
2168 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002169#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002170
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002171 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002172 {return allocator_type(this->__alloc());}
2173
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002174 size_type max_size() const _NOEXCEPT;
2175 _LIBCPP_INLINE_VISIBILITY
2176 size_type capacity() const _NOEXCEPT
2177 {return __internal_cap_to_external(__cap());}
2178 _LIBCPP_INLINE_VISIBILITY
2179 size_type size() const _NOEXCEPT
2180 {return __size_;}
2181 _LIBCPP_INLINE_VISIBILITY
2182 bool empty() const _NOEXCEPT
2183 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002184 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002185 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002186
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002187 _LIBCPP_INLINE_VISIBILITY
2188 iterator begin() _NOEXCEPT
2189 {return __make_iter(0);}
2190 _LIBCPP_INLINE_VISIBILITY
2191 const_iterator begin() const _NOEXCEPT
2192 {return __make_iter(0);}
2193 _LIBCPP_INLINE_VISIBILITY
2194 iterator end() _NOEXCEPT
2195 {return __make_iter(__size_);}
2196 _LIBCPP_INLINE_VISIBILITY
2197 const_iterator end() const _NOEXCEPT
2198 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002199
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002200 _LIBCPP_INLINE_VISIBILITY
2201 reverse_iterator rbegin() _NOEXCEPT
2202 {return reverse_iterator(end());}
2203 _LIBCPP_INLINE_VISIBILITY
2204 const_reverse_iterator rbegin() const _NOEXCEPT
2205 {return const_reverse_iterator(end());}
2206 _LIBCPP_INLINE_VISIBILITY
2207 reverse_iterator rend() _NOEXCEPT
2208 {return reverse_iterator(begin());}
2209 _LIBCPP_INLINE_VISIBILITY
2210 const_reverse_iterator rend() const _NOEXCEPT
2211 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002212
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002213 _LIBCPP_INLINE_VISIBILITY
2214 const_iterator cbegin() const _NOEXCEPT
2215 {return __make_iter(0);}
2216 _LIBCPP_INLINE_VISIBILITY
2217 const_iterator cend() const _NOEXCEPT
2218 {return __make_iter(__size_);}
2219 _LIBCPP_INLINE_VISIBILITY
2220 const_reverse_iterator crbegin() const _NOEXCEPT
2221 {return rbegin();}
2222 _LIBCPP_INLINE_VISIBILITY
2223 const_reverse_iterator crend() const _NOEXCEPT
2224 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002225
2226 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2227 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2228 reference at(size_type __n);
2229 const_reference at(size_type __n) const;
2230
2231 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2232 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2233 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2234 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2235
2236 void push_back(const value_type& __x);
Marshall Clow198a2a52013-08-13 23:54:12 +00002237#if _LIBCPP_STD_VER > 11
2238 template <class... _Args>
2239 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2240 { push_back ( value_type ( _VSTD::forward<_Args>(__args)... )); }
2241#endif
2242
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002243 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2244
Marshall Clow198a2a52013-08-13 23:54:12 +00002245#if _LIBCPP_STD_VER > 11
2246 template <class... _Args>
2247 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2248 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2249#endif
2250
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002251 iterator insert(const_iterator __position, const value_type& __x);
2252 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2253 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2254 template <class _InputIterator>
2255 typename enable_if
2256 <
2257 __is_input_iterator <_InputIterator>::value &&
2258 !__is_forward_iterator<_InputIterator>::value,
2259 iterator
2260 >::type
2261 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2262 template <class _ForwardIterator>
2263 typename enable_if
2264 <
2265 __is_forward_iterator<_ForwardIterator>::value,
2266 iterator
2267 >::type
2268 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00002269#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002271 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2272 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002273#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002274
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002275 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002276 iterator erase(const_iterator __first, const_iterator __last);
2277
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002278 _LIBCPP_INLINE_VISIBILITY
2279 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002280
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002281 void swap(vector&)
2282 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2283 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002284
2285 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002286 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002287
2288 bool __invariants() const;
2289
2290private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002291 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002292 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002293 void deallocate() _NOEXCEPT;
2294 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00002295 static size_type __align_it(size_type __new_size) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002296 {return __new_size + (__bits_per_word-1) & ~(__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002297 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2298 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002299 template <class _ForwardIterator>
2300 typename enable_if
2301 <
2302 __is_forward_iterator<_ForwardIterator>::value,
2303 void
2304 >::type
2305 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2306 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002307 _LIBCPP_INLINE_VISIBILITY
2308 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002309 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002310 _LIBCPP_INLINE_VISIBILITY
2311 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002312 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002313 _LIBCPP_INLINE_VISIBILITY
2314 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002315 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002316 _LIBCPP_INLINE_VISIBILITY
2317 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002318 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002319 _LIBCPP_INLINE_VISIBILITY
2320 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002321 {return begin() + (__p - cbegin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002322
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002324 void __copy_assign_alloc(const vector& __v)
2325 {__copy_assign_alloc(__v, integral_constant<bool,
2326 __storage_traits::propagate_on_container_copy_assignment::value>());}
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& __c, true_type)
2329 {
2330 if (__alloc() != __c.__alloc())
2331 deallocate();
2332 __alloc() = __c.__alloc();
2333 }
2334
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002336 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002337 {}
2338
2339 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002340 void __move_assign(vector& __c, true_type)
2341 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002343 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002344 _NOEXCEPT_(
2345 !__storage_traits::propagate_on_container_move_assignment::value ||
2346 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002347 {__move_assign_alloc(__c, integral_constant<bool,
2348 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00002350 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002351 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002352 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002353 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002354 }
2355
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002356 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002357 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002358 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002359 {}
2360
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002362 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002363 _NOEXCEPT_(
2364 !__storage_traits::propagate_on_container_swap::value ||
2365 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002366 {__swap_alloc(__x, __y, integral_constant<bool,
2367 __storage_traits::propagate_on_container_swap::value>());}
2368
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002370 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002371 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002372 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002373 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002374 swap(__x, __y);
2375 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002377 static void __swap_alloc(__storage_allocator&, __storage_allocator&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002378 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002379 {}
2380
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002381 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002382
2383 friend class __bit_reference<vector>;
2384 friend class __bit_const_reference<vector>;
2385 friend class __bit_iterator<vector, false>;
2386 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:18 +00002387 friend struct __bit_array<vector>;
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002388 friend struct _LIBCPP_TYPE_VIS_ONLY hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002389};
2390
2391template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002392_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002393void
2394vector<bool, _Allocator>::__invalidate_all_iterators()
2395{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002396}
2397
2398// Allocate space for __n objects
2399// throws length_error if __n > max_size()
2400// throws (probably bad_alloc) if memory run out
2401// Precondition: __begin_ == __end_ == __cap() == 0
2402// Precondition: __n > 0
2403// Postcondition: capacity() == __n
2404// Postcondition: size() == 0
2405template <class _Allocator>
2406void
2407vector<bool, _Allocator>::allocate(size_type __n)
2408{
2409 if (__n > max_size())
2410 this->__throw_length_error();
2411 __n = __external_cap_to_internal(__n);
2412 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2413 this->__size_ = 0;
2414 this->__cap() = __n;
2415}
2416
2417template <class _Allocator>
2418void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002419vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002420{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002421 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002422 {
2423 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2424 __invalidate_all_iterators();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002425 this->__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002426 this->__size_ = this->__cap() = 0;
2427 }
2428}
2429
2430template <class _Allocator>
2431typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002432vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002433{
2434 size_type __amax = __storage_traits::max_size(__alloc());
2435 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2436 if (__nmax / __bits_per_word <= __amax)
2437 return __nmax;
2438 return __internal_cap_to_external(__amax);
2439}
2440
2441// Precondition: __new_size > capacity()
2442template <class _Allocator>
2443_LIBCPP_INLINE_VISIBILITY inline
2444typename vector<bool, _Allocator>::size_type
2445vector<bool, _Allocator>::__recommend(size_type __new_size) const
2446{
2447 const size_type __ms = max_size();
2448 if (__new_size > __ms)
2449 this->__throw_length_error();
2450 const size_type __cap = capacity();
2451 if (__cap >= __ms / 2)
2452 return __ms;
Howard Hinnant7f764502013-08-14 18:00:20 +00002453 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002454}
2455
2456// Default constructs __n objects starting at __end_
2457// Precondition: __n > 0
2458// Precondition: size() + __n <= capacity()
2459// Postcondition: size() == size() + __n
2460template <class _Allocator>
2461_LIBCPP_INLINE_VISIBILITY inline
2462void
2463vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2464{
2465 size_type __old_size = this->__size_;
2466 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002467 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002468}
2469
2470template <class _Allocator>
2471template <class _ForwardIterator>
2472typename enable_if
2473<
2474 __is_forward_iterator<_ForwardIterator>::value,
2475 void
2476>::type
2477vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2478{
2479 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002480 this->__size_ += _VSTD::distance(__first, __last);
2481 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002482}
2483
2484template <class _Allocator>
2485_LIBCPP_INLINE_VISIBILITY inline
2486vector<bool, _Allocator>::vector()
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002487 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002488 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002489 __size_(0),
2490 __cap_alloc_(0)
2491{
2492}
2493
2494template <class _Allocator>
2495_LIBCPP_INLINE_VISIBILITY inline
2496vector<bool, _Allocator>::vector(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002497 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002498 __size_(0),
2499 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2500{
2501}
2502
2503template <class _Allocator>
2504vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002505 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002506 __size_(0),
2507 __cap_alloc_(0)
2508{
2509 if (__n > 0)
2510 {
2511 allocate(__n);
2512 __construct_at_end(__n, false);
2513 }
2514}
2515
Marshall Clowa49a2c92013-09-14 00:47:59 +00002516#if _LIBCPP_STD_VER > 11
2517template <class _Allocator>
2518vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2519 : __begin_(nullptr),
2520 __size_(0),
2521 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2522{
2523 if (__n > 0)
2524 {
2525 allocate(__n);
2526 __construct_at_end(__n, false);
2527 }
2528}
2529#endif
2530
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002531template <class _Allocator>
2532vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002533 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002534 __size_(0),
2535 __cap_alloc_(0)
2536{
2537 if (__n > 0)
2538 {
2539 allocate(__n);
2540 __construct_at_end(__n, __x);
2541 }
2542}
2543
2544template <class _Allocator>
2545vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002546 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002547 __size_(0),
2548 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2549{
2550 if (__n > 0)
2551 {
2552 allocate(__n);
2553 __construct_at_end(__n, __x);
2554 }
2555}
2556
2557template <class _Allocator>
2558template <class _InputIterator>
2559vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2560 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2561 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002562 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002563 __size_(0),
2564 __cap_alloc_(0)
2565{
2566#ifndef _LIBCPP_NO_EXCEPTIONS
2567 try
2568 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002569#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002570 for (; __first != __last; ++__first)
2571 push_back(*__first);
2572#ifndef _LIBCPP_NO_EXCEPTIONS
2573 }
2574 catch (...)
2575 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002576 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002577 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2578 __invalidate_all_iterators();
2579 throw;
2580 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002581#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002582}
2583
2584template <class _Allocator>
2585template <class _InputIterator>
2586vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2587 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2588 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002589 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002590 __size_(0),
2591 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2592{
2593#ifndef _LIBCPP_NO_EXCEPTIONS
2594 try
2595 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002596#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002597 for (; __first != __last; ++__first)
2598 push_back(*__first);
2599#ifndef _LIBCPP_NO_EXCEPTIONS
2600 }
2601 catch (...)
2602 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002603 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002604 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2605 __invalidate_all_iterators();
2606 throw;
2607 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002608#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002609}
2610
2611template <class _Allocator>
2612template <class _ForwardIterator>
2613vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2614 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002615 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002616 __size_(0),
2617 __cap_alloc_(0)
2618{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002619 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002620 if (__n > 0)
2621 {
2622 allocate(__n);
2623 __construct_at_end(__first, __last);
2624 }
2625}
2626
2627template <class _Allocator>
2628template <class _ForwardIterator>
2629vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2630 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002631 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002632 __size_(0),
2633 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2634{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002635 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002636 if (__n > 0)
2637 {
2638 allocate(__n);
2639 __construct_at_end(__first, __last);
2640 }
2641}
2642
Howard Hinnante3e32912011-08-12 21:56:02 +00002643#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2644
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002645template <class _Allocator>
2646vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002647 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002648 __size_(0),
2649 __cap_alloc_(0)
2650{
2651 size_type __n = static_cast<size_type>(__il.size());
2652 if (__n > 0)
2653 {
2654 allocate(__n);
2655 __construct_at_end(__il.begin(), __il.end());
2656 }
2657}
2658
2659template <class _Allocator>
2660vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002661 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002662 __size_(0),
2663 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2664{
2665 size_type __n = static_cast<size_type>(__il.size());
2666 if (__n > 0)
2667 {
2668 allocate(__n);
2669 __construct_at_end(__il.begin(), __il.end());
2670 }
2671}
2672
Howard Hinnante3e32912011-08-12 21:56:02 +00002673#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2674
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002675template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002676vector<bool, _Allocator>::~vector()
2677{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002678 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002679 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002680 __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002681}
2682
2683template <class _Allocator>
2684vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002685 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002686 __size_(0),
2687 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2688{
2689 if (__v.size() > 0)
2690 {
2691 allocate(__v.size());
2692 __construct_at_end(__v.begin(), __v.end());
2693 }
2694}
2695
2696template <class _Allocator>
2697vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002698 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002699 __size_(0),
2700 __cap_alloc_(0, __a)
2701{
2702 if (__v.size() > 0)
2703 {
2704 allocate(__v.size());
2705 __construct_at_end(__v.begin(), __v.end());
2706 }
2707}
2708
2709template <class _Allocator>
2710vector<bool, _Allocator>&
2711vector<bool, _Allocator>::operator=(const vector& __v)
2712{
2713 if (this != &__v)
2714 {
2715 __copy_assign_alloc(__v);
2716 if (__v.__size_)
2717 {
2718 if (__v.__size_ > capacity())
2719 {
2720 deallocate();
2721 allocate(__v.__size_);
2722 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002723 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002724 }
2725 __size_ = __v.__size_;
2726 }
2727 return *this;
2728}
2729
Howard Hinnant73d21a42010-09-04 23:28:19 +00002730#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2731
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002732template <class _Allocator>
2733_LIBCPP_INLINE_VISIBILITY inline
2734vector<bool, _Allocator>::vector(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002735 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002736 : __begin_(__v.__begin_),
2737 __size_(__v.__size_),
2738 __cap_alloc_(__v.__cap_alloc_)
2739{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002740 __v.__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002741 __v.__size_ = 0;
2742 __v.__cap() = 0;
2743}
2744
2745template <class _Allocator>
2746vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002747 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002748 __size_(0),
2749 __cap_alloc_(0, __a)
2750{
2751 if (__a == allocator_type(__v.__alloc()))
2752 {
2753 this->__begin_ = __v.__begin_;
2754 this->__size_ = __v.__size_;
2755 this->__cap() = __v.__cap();
2756 __v.__begin_ = nullptr;
2757 __v.__cap() = __v.__size_ = 0;
2758 }
2759 else if (__v.size() > 0)
2760 {
2761 allocate(__v.size());
2762 __construct_at_end(__v.begin(), __v.end());
2763 }
2764}
2765
2766template <class _Allocator>
2767_LIBCPP_INLINE_VISIBILITY inline
2768vector<bool, _Allocator>&
2769vector<bool, _Allocator>::operator=(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002770 _NOEXCEPT_(
2771 __alloc_traits::propagate_on_container_move_assignment::value &&
2772 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002773{
2774 __move_assign(__v, integral_constant<bool,
2775 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002776 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002777}
2778
2779template <class _Allocator>
2780void
2781vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2782{
2783 if (__alloc() != __c.__alloc())
2784 assign(__c.begin(), __c.end());
2785 else
2786 __move_assign(__c, true_type());
2787}
2788
2789template <class _Allocator>
2790void
2791vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002792 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002793{
2794 deallocate();
2795 this->__begin_ = __c.__begin_;
2796 this->__size_ = __c.__size_;
2797 this->__cap() = __c.__cap();
2798 __move_assign_alloc(__c);
2799 __c.__begin_ = nullptr;
2800 __c.__cap() = __c.__size_ = 0;
2801}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002802
2803#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002804
2805template <class _Allocator>
2806void
2807vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2808{
2809 __size_ = 0;
2810 if (__n > 0)
2811 {
2812 size_type __c = capacity();
2813 if (__n <= __c)
2814 __size_ = __n;
2815 else
2816 {
2817 vector __v(__alloc());
2818 __v.reserve(__recommend(__n));
2819 __v.__size_ = __n;
2820 swap(__v);
2821 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002822 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002823 }
2824}
2825
2826template <class _Allocator>
2827template <class _InputIterator>
2828typename enable_if
2829<
2830 __is_input_iterator<_InputIterator>::value &&
2831 !__is_forward_iterator<_InputIterator>::value,
2832 void
2833>::type
2834vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2835{
2836 clear();
2837 for (; __first != __last; ++__first)
2838 push_back(*__first);
2839}
2840
2841template <class _Allocator>
2842template <class _ForwardIterator>
2843typename enable_if
2844<
2845 __is_forward_iterator<_ForwardIterator>::value,
2846 void
2847>::type
2848vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2849{
2850 clear();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002851 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002852 if (__n)
2853 {
2854 if (__n > capacity())
2855 {
2856 deallocate();
2857 allocate(__n);
2858 }
2859 __construct_at_end(__first, __last);
2860 }
2861}
2862
2863template <class _Allocator>
2864void
2865vector<bool, _Allocator>::reserve(size_type __n)
2866{
2867 if (__n > capacity())
2868 {
2869 vector __v(this->__alloc());
2870 __v.allocate(__n);
2871 __v.__construct_at_end(this->begin(), this->end());
2872 swap(__v);
2873 __invalidate_all_iterators();
2874 }
2875}
2876
2877template <class _Allocator>
2878void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002879vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002880{
2881 if (__external_cap_to_internal(size()) > __cap())
2882 {
2883#ifndef _LIBCPP_NO_EXCEPTIONS
2884 try
2885 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002886#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002887 vector(*this, allocator_type(__alloc())).swap(*this);
2888#ifndef _LIBCPP_NO_EXCEPTIONS
2889 }
2890 catch (...)
2891 {
2892 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002893#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002894 }
2895}
2896
2897template <class _Allocator>
2898typename vector<bool, _Allocator>::reference
2899vector<bool, _Allocator>::at(size_type __n)
2900{
2901 if (__n >= size())
2902 this->__throw_out_of_range();
2903 return (*this)[__n];
2904}
2905
2906template <class _Allocator>
2907typename vector<bool, _Allocator>::const_reference
2908vector<bool, _Allocator>::at(size_type __n) const
2909{
2910 if (__n >= size())
2911 this->__throw_out_of_range();
2912 return (*this)[__n];
2913}
2914
2915template <class _Allocator>
2916void
2917vector<bool, _Allocator>::push_back(const value_type& __x)
2918{
2919 if (this->__size_ == this->capacity())
2920 reserve(__recommend(this->__size_ + 1));
2921 ++this->__size_;
2922 back() = __x;
2923}
2924
2925template <class _Allocator>
2926typename vector<bool, _Allocator>::iterator
2927vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2928{
2929 iterator __r;
2930 if (size() < capacity())
2931 {
2932 const_iterator __old_end = end();
2933 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002934 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002935 __r = __const_iterator_cast(__position);
2936 }
2937 else
2938 {
2939 vector __v(__alloc());
2940 __v.reserve(__recommend(__size_ + 1));
2941 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002942 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2943 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002944 swap(__v);
2945 }
2946 *__r = __x;
2947 return __r;
2948}
2949
2950template <class _Allocator>
2951typename vector<bool, _Allocator>::iterator
2952vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
2953{
2954 iterator __r;
2955 size_type __c = capacity();
2956 if (__n <= __c && size() <= __c - __n)
2957 {
2958 const_iterator __old_end = end();
2959 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002960 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002961 __r = __const_iterator_cast(__position);
2962 }
2963 else
2964 {
2965 vector __v(__alloc());
2966 __v.reserve(__recommend(__size_ + __n));
2967 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002968 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2969 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002970 swap(__v);
2971 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002972 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002973 return __r;
2974}
2975
2976template <class _Allocator>
2977template <class _InputIterator>
2978typename enable_if
2979<
2980 __is_input_iterator <_InputIterator>::value &&
2981 !__is_forward_iterator<_InputIterator>::value,
2982 typename vector<bool, _Allocator>::iterator
2983>::type
2984vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
2985{
2986 difference_type __off = __position - begin();
2987 iterator __p = __const_iterator_cast(__position);
2988 iterator __old_end = end();
2989 for (; size() != capacity() && __first != __last; ++__first)
2990 {
2991 ++this->__size_;
2992 back() = *__first;
2993 }
2994 vector __v(__alloc());
2995 if (__first != __last)
2996 {
2997#ifndef _LIBCPP_NO_EXCEPTIONS
2998 try
2999 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003000#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003001 __v.assign(__first, __last);
3002 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3003 difference_type __old_p = __p - begin();
3004 reserve(__recommend(size() + __v.size()));
3005 __p = begin() + __old_p;
3006 __old_end = begin() + __old_size;
3007#ifndef _LIBCPP_NO_EXCEPTIONS
3008 }
3009 catch (...)
3010 {
3011 erase(__old_end, end());
3012 throw;
3013 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003014#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003015 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003016 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003017 insert(__p, __v.begin(), __v.end());
3018 return begin() + __off;
3019}
3020
3021template <class _Allocator>
3022template <class _ForwardIterator>
3023typename enable_if
3024<
3025 __is_forward_iterator<_ForwardIterator>::value,
3026 typename vector<bool, _Allocator>::iterator
3027>::type
3028vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3029{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003030 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003031 iterator __r;
3032 size_type __c = capacity();
3033 if (__n <= __c && size() <= __c - __n)
3034 {
3035 const_iterator __old_end = end();
3036 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003037 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003038 __r = __const_iterator_cast(__position);
3039 }
3040 else
3041 {
3042 vector __v(__alloc());
3043 __v.reserve(__recommend(__size_ + __n));
3044 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003045 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3046 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003047 swap(__v);
3048 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003049 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003050 return __r;
3051}
3052
3053template <class _Allocator>
3054_LIBCPP_INLINE_VISIBILITY inline
3055typename vector<bool, _Allocator>::iterator
3056vector<bool, _Allocator>::erase(const_iterator __position)
3057{
3058 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003059 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003060 --__size_;
3061 return __r;
3062}
3063
3064template <class _Allocator>
3065typename vector<bool, _Allocator>::iterator
3066vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3067{
3068 iterator __r = __const_iterator_cast(__first);
3069 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003070 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003071 __size_ -= __d;
3072 return __r;
3073}
3074
3075template <class _Allocator>
3076void
3077vector<bool, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003078 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3079 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003080{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003081 _VSTD::swap(this->__begin_, __x.__begin_);
3082 _VSTD::swap(this->__size_, __x.__size_);
3083 _VSTD::swap(this->__cap(), __x.__cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003084 __swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003085}
3086
Howard Hinnant324bb032010-08-22 00:02:43 +00003087template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003088void
3089vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3090{
3091 size_type __cs = size();
3092 if (__cs < __sz)
3093 {
3094 iterator __r;
3095 size_type __c = capacity();
3096 size_type __n = __sz - __cs;
3097 if (__n <= __c && __cs <= __c - __n)
3098 {
3099 __r = end();
3100 __size_ += __n;
3101 }
3102 else
3103 {
3104 vector __v(__alloc());
3105 __v.reserve(__recommend(__size_ + __n));
3106 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003107 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003108 swap(__v);
3109 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003110 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003111 }
3112 else
3113 __size_ = __sz;
3114}
3115
Howard Hinnant324bb032010-08-22 00:02:43 +00003116template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003117void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003118vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003119{
3120 // do middle whole words
3121 size_type __n = __size_;
3122 __storage_pointer __p = __begin_;
3123 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3124 *__p = ~*__p;
3125 // do last partial word
3126 if (__n > 0)
3127 {
3128 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3129 __storage_type __b = *__p & __m;
3130 *__p &= ~__m;
3131 *__p |= ~__b & __m;
3132 }
3133}
3134
Howard Hinnant324bb032010-08-22 00:02:43 +00003135template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003136bool
3137vector<bool, _Allocator>::__invariants() const
3138{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00003139 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003140 {
3141 if (this->__size_ != 0 || this->__cap() != 0)
3142 return false;
3143 }
3144 else
3145 {
3146 if (this->__cap() == 0)
3147 return false;
3148 if (this->__size_ > this->capacity())
3149 return false;
3150 }
3151 return true;
3152}
3153
Howard Hinnant324bb032010-08-22 00:02:43 +00003154template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003155size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003156vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003157{
3158 size_t __h = 0;
3159 // do middle whole words
3160 size_type __n = __size_;
3161 __storage_pointer __p = __begin_;
3162 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3163 __h ^= *__p;
3164 // do last partial word
3165 if (__n > 0)
3166 {
3167 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3168 __h ^= *__p & __m;
3169 }
3170 return __h;
3171}
3172
3173template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003174struct _LIBCPP_TYPE_VIS_ONLY hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003175 : public unary_function<vector<bool, _Allocator>, size_t>
3176{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00003177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003178 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003179 {return __vec.__hash_code();}
3180};
3181
3182template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003183_LIBCPP_INLINE_VISIBILITY inline
3184bool
3185operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3186{
3187 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003188 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003189}
3190
3191template <class _Tp, class _Allocator>
3192_LIBCPP_INLINE_VISIBILITY inline
3193bool
3194operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3195{
3196 return !(__x == __y);
3197}
3198
3199template <class _Tp, class _Allocator>
3200_LIBCPP_INLINE_VISIBILITY inline
3201bool
3202operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3203{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003204 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003205}
3206
3207template <class _Tp, class _Allocator>
3208_LIBCPP_INLINE_VISIBILITY inline
3209bool
3210operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3211{
3212 return __y < __x;
3213}
3214
3215template <class _Tp, class _Allocator>
3216_LIBCPP_INLINE_VISIBILITY inline
3217bool
3218operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3219{
3220 return !(__x < __y);
3221}
3222
3223template <class _Tp, class _Allocator>
3224_LIBCPP_INLINE_VISIBILITY inline
3225bool
3226operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3227{
3228 return !(__y < __x);
3229}
3230
3231template <class _Tp, class _Allocator>
3232_LIBCPP_INLINE_VISIBILITY inline
3233void
3234swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003235 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003236{
3237 __x.swap(__y);
3238}
3239
3240_LIBCPP_END_NAMESPACE_STD
3241
3242#endif // _LIBCPP_VECTOR