blob: 0855e8b930e653fc970e7b6dc8ad79787ee75bd6 [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);
41 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
42 template <class InputIterator>
43 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
44 vector(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +000045 vector(vector&& x)
46 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000047 vector(initializer_list<value_type> il);
48 vector(initializer_list<value_type> il, const allocator_type& a);
49 ~vector();
50 vector& operator=(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +000051 vector& operator=(vector&& x)
52 noexcept(
53 allocator_type::propagate_on_container_move_assignment::value &&
54 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000055 vector& operator=(initializer_list<value_type> il);
56 template <class InputIterator>
57 void assign(InputIterator first, InputIterator last);
58 void assign(size_type n, const value_type& u);
59 void assign(initializer_list<value_type> il);
60
Howard Hinnantd1d27a42011-06-03 19:40:40 +000061 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000062
Howard Hinnantd1d27a42011-06-03 19:40:40 +000063 iterator begin() noexcept;
64 const_iterator begin() const noexcept;
65 iterator end() noexcept;
66 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000067
Howard Hinnantd1d27a42011-06-03 19:40:40 +000068 reverse_iterator rbegin() noexcept;
69 const_reverse_iterator rbegin() const noexcept;
70 reverse_iterator rend() noexcept;
71 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000072
Howard Hinnantd1d27a42011-06-03 19:40:40 +000073 const_iterator cbegin() const noexcept;
74 const_iterator cend() const noexcept;
75 const_reverse_iterator crbegin() const noexcept;
76 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000077
Howard Hinnantd1d27a42011-06-03 19:40:40 +000078 size_type size() const noexcept;
79 size_type max_size() const noexcept;
80 size_type capacity() const noexcept;
81 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000082 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +000083 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000084
85 reference operator[](size_type n);
86 const_reference operator[](size_type n) const;
87 reference at(size_type n);
88 const_reference at(size_type n) const;
89
90 reference front();
91 const_reference front() const;
92 reference back();
93 const_reference back() const;
94
Howard Hinnantd1d27a42011-06-03 19:40:40 +000095 value_type* data() noexcept;
96 const value_type* data() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000097
98 void push_back(const value_type& x);
99 void push_back(value_type&& x);
100 template <class... Args>
101 void emplace_back(Args&&... args);
102 void pop_back();
103
104 template <class... Args> iterator emplace(const_iterator position, Args&&... args);
105 iterator insert(const_iterator position, const value_type& x);
106 iterator insert(const_iterator position, value_type&& x);
107 iterator insert(const_iterator position, size_type n, const value_type& x);
108 template <class InputIterator>
109 iterator insert(const_iterator position, InputIterator first, InputIterator last);
110 iterator insert(const_iterator position, initializer_list<value_type> il);
111
112 iterator erase(const_iterator position);
113 iterator erase(const_iterator first, const_iterator last);
114
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000115 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000116
117 void resize(size_type sz);
118 void resize(size_type sz, const value_type& c);
119
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000120 void swap(vector&)
121 noexcept(!allocator_type::propagate_on_container_swap::value ||
122 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000123
124 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000125};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000126
Howard Hinnant324bb032010-08-22 00:02:43 +0000127template <class Allocator = allocator<T> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000128class vector<bool, Allocator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000129{
130public:
131 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000132 typedef Allocator allocator_type;
133 typedef implementation-defined iterator;
134 typedef implementation-defined const_iterator;
135 typedef typename allocator_type::size_type size_type;
136 typedef typename allocator_type::difference_type difference_type;
137 typedef iterator pointer;
138 typedef const_iterator const_pointer;
139 typedef std::reverse_iterator<iterator> reverse_iterator;
140 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
141
142 class reference
143 {
144 public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000145 reference(const reference&) noexcept;
146 operator bool() const noexcept;
147 reference& operator=(const bool x) noexcept;
148 reference& operator=(const reference& x) noexcept;
149 iterator operator&() const noexcept;
150 void flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000151 };
152
153 class const_reference
154 {
155 public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000156 const_reference(const reference&) noexcept;
157 operator bool() const noexcept;
158 const_iterator operator&() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000159 };
160
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000161 vector()
162 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant9cbee432011-09-02 20:42:31 +0000163 explicit vector(const allocator_type&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000164 explicit vector(size_type n, const value_type& value = value_type(), const allocator_type& = allocator_type());
165 template <class InputIterator>
166 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
167 vector(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000168 vector(vector&& x)
169 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000170 vector(initializer_list<value_type> il);
171 vector(initializer_list<value_type> il, const allocator_type& a);
172 ~vector();
173 vector& operator=(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000174 vector& operator=(vector&& x)
175 noexcept(
176 allocator_type::propagate_on_container_move_assignment::value &&
177 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000178 vector& operator=(initializer_list<value_type> il);
179 template <class InputIterator>
180 void assign(InputIterator first, InputIterator last);
181 void assign(size_type n, const value_type& u);
182 void assign(initializer_list<value_type> il);
183
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000184 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000185
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000186 iterator begin() noexcept;
187 const_iterator begin() const noexcept;
188 iterator end() noexcept;
189 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000190
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000191 reverse_iterator rbegin() noexcept;
192 const_reverse_iterator rbegin() const noexcept;
193 reverse_iterator rend() noexcept;
194 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000195
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000196 const_iterator cbegin() const noexcept;
197 const_iterator cend() const noexcept;
198 const_reverse_iterator crbegin() const noexcept;
199 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000200
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000201 size_type size() const noexcept;
202 size_type max_size() const noexcept;
203 size_type capacity() const noexcept;
204 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000205 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000206 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000207
208 reference operator[](size_type n);
209 const_reference operator[](size_type n) const;
210 reference at(size_type n);
211 const_reference at(size_type n) const;
212
213 reference front();
214 const_reference front() const;
215 reference back();
216 const_reference back() const;
217
218 void push_back(const value_type& x);
Marshall Clow198a2a52013-08-13 23:54:12 +0000219 template <class... Args> void emplace_back(Args&&... args); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000220 void pop_back();
221
Marshall Clow198a2a52013-08-13 23:54:12 +0000222 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000223 iterator insert(const_iterator position, const value_type& x);
224 iterator insert(const_iterator position, size_type n, const value_type& x);
225 template <class InputIterator>
226 iterator insert(const_iterator position, InputIterator first, InputIterator last);
227 iterator insert(const_iterator position, initializer_list<value_type> il);
228
229 iterator erase(const_iterator position);
230 iterator erase(const_iterator first, const_iterator last);
231
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000232 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000233
234 void resize(size_type sz);
235 void resize(size_type sz, value_type x);
236
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000237 void swap(vector&)
238 noexcept(!allocator_type::propagate_on_container_swap::value ||
239 __is_nothrow_swappable<allocator_type>::value);
240 void flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000241
242 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000243};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000244
245template <class Allocator> struct hash<std::vector<bool, Allocator>>;
246
247template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
248template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
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);
253
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000254template <class T, class Allocator>
255void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
256 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000257
258} // std
259
260*/
261
262#include <__config>
263#include <__bit_reference>
264#include <type_traits>
265#include <climits>
266#include <limits>
267#include <initializer_list>
268#include <memory>
269#include <stdexcept>
270#include <algorithm>
271#include <cstring>
272#include <__split_buffer>
273#include <__functional_base>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000274
Howard Hinnant66c6f972011-11-29 16:45:27 +0000275#include <__undef_min_max>
276
Howard Hinnant8b00e6c2013-08-02 00:26:35 +0000277#ifdef _LIBCPP_DEBUG2
278# include <__debug>
279#else
280# define _LIBCPP_ASSERT(x, m) ((void)0)
281#endif
282
Howard Hinnant08e17472011-10-17 20:05:10 +0000283#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000284#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000285#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000286
287_LIBCPP_BEGIN_NAMESPACE_STD
288
289template <bool>
290class __vector_base_common
291{
292protected:
293 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
294 void __throw_length_error() const;
295 void __throw_out_of_range() const;
296};
297
298template <bool __b>
299void
300__vector_base_common<__b>::__throw_length_error() const
301{
302#ifndef _LIBCPP_NO_EXCEPTIONS
303 throw length_error("vector");
304#else
305 assert(!"vector length_error");
306#endif
307}
308
309template <bool __b>
310void
311__vector_base_common<__b>::__throw_out_of_range() const
312{
313#ifndef _LIBCPP_NO_EXCEPTIONS
314 throw out_of_range("vector");
315#else
316 assert(!"vector out_of_range");
317#endif
318}
319
Howard Hinnante9df0a52013-08-01 18:17:34 +0000320#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000321#pragma warning( push )
322#pragma warning( disable: 4231 )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000323#endif // _LIBCPP_MSVC
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000324_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS __vector_base_common<true>)
Howard Hinnante9df0a52013-08-01 18:17:34 +0000325#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000326#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000327#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000328
329template <class _Tp, class _Allocator>
330class __vector_base
331 : protected __vector_base_common<true>
332{
333protected:
Howard Hinnant324bb032010-08-22 00:02:43 +0000334 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000335 typedef _Allocator allocator_type;
336 typedef allocator_traits<allocator_type> __alloc_traits;
337 typedef value_type& reference;
338 typedef const value_type& const_reference;
339 typedef typename __alloc_traits::size_type size_type;
340 typedef typename __alloc_traits::difference_type difference_type;
341 typedef typename __alloc_traits::pointer pointer;
342 typedef typename __alloc_traits::const_pointer const_pointer;
343 typedef pointer iterator;
344 typedef const_pointer const_iterator;
345
346 pointer __begin_;
347 pointer __end_;
348 __compressed_pair<pointer, allocator_type> __end_cap_;
349
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000350 _LIBCPP_INLINE_VISIBILITY
351 allocator_type& __alloc() _NOEXCEPT
352 {return __end_cap_.second();}
353 _LIBCPP_INLINE_VISIBILITY
354 const allocator_type& __alloc() const _NOEXCEPT
355 {return __end_cap_.second();}
356 _LIBCPP_INLINE_VISIBILITY
357 pointer& __end_cap() _NOEXCEPT
358 {return __end_cap_.first();}
359 _LIBCPP_INLINE_VISIBILITY
360 const pointer& __end_cap() const _NOEXCEPT
361 {return __end_cap_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000362
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000363 _LIBCPP_INLINE_VISIBILITY
364 __vector_base()
365 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000366 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000367 ~__vector_base();
368
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000369 _LIBCPP_INLINE_VISIBILITY
370 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
371 _LIBCPP_INLINE_VISIBILITY
372 size_type capacity() const _NOEXCEPT
373 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000376 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000379 void __copy_assign_alloc(const __vector_base& __c)
380 {__copy_assign_alloc(__c, integral_constant<bool,
381 __alloc_traits::propagate_on_container_copy_assignment::value>());}
382
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000383 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384 void __move_assign_alloc(__vector_base& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000385 _NOEXCEPT_(
386 !__alloc_traits::propagate_on_container_move_assignment::value ||
387 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000388 {__move_assign_alloc(__c, integral_constant<bool,
389 __alloc_traits::propagate_on_container_move_assignment::value>());}
390
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000393 _NOEXCEPT_(
394 !__alloc_traits::propagate_on_container_swap::value ||
395 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000396 {__swap_alloc(__x, __y, integral_constant<bool,
397 __alloc_traits::propagate_on_container_swap::value>());}
398private:
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000399 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000400 void __copy_assign_alloc(const __vector_base& __c, true_type)
401 {
402 if (__alloc() != __c.__alloc())
403 {
404 clear();
405 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
406 __begin_ = __end_ = __end_cap() = nullptr;
407 }
408 __alloc() = __c.__alloc();
409 }
410
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000412 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000413 {}
414
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000416 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000417 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000418 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000419 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000420 }
421
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000422 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000423 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000424 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000425 {}
426
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000428 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000429 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000431 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000432 swap(__x, __y);
433 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000435 static void __swap_alloc(allocator_type&, allocator_type&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000436 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000437 {}
438};
439
440template <class _Tp, class _Allocator>
441_LIBCPP_INLINE_VISIBILITY inline
442void
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000443__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000444{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000445 while (__new_last != __end_)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000446 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000447}
448
449template <class _Tp, class _Allocator>
450_LIBCPP_INLINE_VISIBILITY inline
451__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000452 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000453 : __begin_(nullptr),
454 __end_(nullptr),
455 __end_cap_(nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000456{
457}
458
459template <class _Tp, class _Allocator>
460_LIBCPP_INLINE_VISIBILITY inline
461__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000462 : __begin_(nullptr),
463 __end_(nullptr),
464 __end_cap_(nullptr, __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465{
466}
467
468template <class _Tp, class _Allocator>
469__vector_base<_Tp, _Allocator>::~__vector_base()
470{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000471 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000472 {
473 clear();
474 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
475 }
476}
477
478template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000479class _LIBCPP_TYPE_VIS_ONLY vector
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000480 : private __vector_base<_Tp, _Allocator>
481{
482private:
483 typedef __vector_base<_Tp, _Allocator> __base;
Howard Hinnant324bb032010-08-22 00:02:43 +0000484public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000485 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +0000486 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000487 typedef _Allocator allocator_type;
488 typedef typename __base::__alloc_traits __alloc_traits;
489 typedef typename __base::reference reference;
490 typedef typename __base::const_reference const_reference;
491 typedef typename __base::size_type size_type;
492 typedef typename __base::difference_type difference_type;
493 typedef typename __base::pointer pointer;
494 typedef typename __base::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000495 typedef __wrap_iter<pointer> iterator;
496 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000497 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
498 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000499
Howard Hinnant02d5e182013-03-26 19:04:56 +0000500 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
501 "Allocator::value_type must be same type as value_type");
502
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000503 _LIBCPP_INLINE_VISIBILITY
504 vector()
505 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant7a563db2011-09-14 18:33:51 +0000506 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000507#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000508 __get_db()->__insert_c(this);
509#endif
510 }
511 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
512 : __base(__a)
513 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000514#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000515 __get_db()->__insert_c(this);
516#endif
517 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000518 explicit vector(size_type __n);
519 vector(size_type __n, const_reference __x);
520 vector(size_type __n, const_reference __x, const allocator_type& __a);
521 template <class _InputIterator>
522 vector(_InputIterator __first, _InputIterator __last,
523 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000524 !__is_forward_iterator<_InputIterator>::value &&
525 is_constructible<
526 value_type,
527 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000528 template <class _InputIterator>
529 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
530 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000531 !__is_forward_iterator<_InputIterator>::value &&
532 is_constructible<
533 value_type,
534 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000535 template <class _ForwardIterator>
536 vector(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000537 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
538 is_constructible<
539 value_type,
540 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000541 template <class _ForwardIterator>
542 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000543 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
544 is_constructible<
545 value_type,
546 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000547#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000549 vector(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000551 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000552#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantabe26282011-09-16 17:29:17 +0000553#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7a563db2011-09-14 18:33:51 +0000555 ~vector()
556 {
557 __get_db()->__erase_c(this);
558 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000559#endif
560
561 vector(const vector& __x);
562 vector(const vector& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000564 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000565#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000566 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000567 vector(vector&& __x)
568 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000570 vector(vector&& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000572 vector& operator=(vector&& __x)
573 _NOEXCEPT_(
574 __alloc_traits::propagate_on_container_move_assignment::value &&
575 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000576#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000577#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000579 vector& operator=(initializer_list<value_type> __il)
580 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000581#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000582
583 template <class _InputIterator>
584 typename enable_if
585 <
586 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000587 !__is_forward_iterator<_InputIterator>::value &&
588 is_constructible<
589 value_type,
590 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000591 void
592 >::type
593 assign(_InputIterator __first, _InputIterator __last);
594 template <class _ForwardIterator>
595 typename enable_if
596 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000597 __is_forward_iterator<_ForwardIterator>::value &&
598 is_constructible<
599 value_type,
600 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000601 void
602 >::type
603 assign(_ForwardIterator __first, _ForwardIterator __last);
604
605 void assign(size_type __n, const_reference __u);
Howard Hinnante3e32912011-08-12 21:56:02 +0000606#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000607 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000608 void assign(initializer_list<value_type> __il)
609 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000610#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000611
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000612 _LIBCPP_INLINE_VISIBILITY
613 allocator_type get_allocator() const _NOEXCEPT
614 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000615
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000616 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
617 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
618 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
619 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000620
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000621 _LIBCPP_INLINE_VISIBILITY
622 reverse_iterator rbegin() _NOEXCEPT
623 {return reverse_iterator(end());}
624 _LIBCPP_INLINE_VISIBILITY
625 const_reverse_iterator rbegin() const _NOEXCEPT
626 {return const_reverse_iterator(end());}
627 _LIBCPP_INLINE_VISIBILITY
628 reverse_iterator rend() _NOEXCEPT
629 {return reverse_iterator(begin());}
630 _LIBCPP_INLINE_VISIBILITY
631 const_reverse_iterator rend() const _NOEXCEPT
632 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000633
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000634 _LIBCPP_INLINE_VISIBILITY
635 const_iterator cbegin() const _NOEXCEPT
636 {return begin();}
637 _LIBCPP_INLINE_VISIBILITY
638 const_iterator cend() const _NOEXCEPT
639 {return end();}
640 _LIBCPP_INLINE_VISIBILITY
641 const_reverse_iterator crbegin() const _NOEXCEPT
642 {return rbegin();}
643 _LIBCPP_INLINE_VISIBILITY
644 const_reverse_iterator crend() const _NOEXCEPT
645 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000646
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000647 _LIBCPP_INLINE_VISIBILITY
648 size_type size() const _NOEXCEPT
649 {return static_cast<size_type>(this->__end_ - this->__begin_);}
650 _LIBCPP_INLINE_VISIBILITY
651 size_type capacity() const _NOEXCEPT
652 {return __base::capacity();}
653 _LIBCPP_INLINE_VISIBILITY
654 bool empty() const _NOEXCEPT
655 {return this->__begin_ == this->__end_;}
656 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000657 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000658 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000659
660 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
661 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
662 reference at(size_type __n);
663 const_reference at(size_type __n) const;
664
Howard Hinnant7a563db2011-09-14 18:33:51 +0000665 _LIBCPP_INLINE_VISIBILITY reference front()
666 {
667 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
668 return *this->__begin_;
669 }
670 _LIBCPP_INLINE_VISIBILITY const_reference front() const
671 {
672 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
673 return *this->__begin_;
674 }
675 _LIBCPP_INLINE_VISIBILITY reference back()
676 {
677 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
678 return *(this->__end_ - 1);
679 }
680 _LIBCPP_INLINE_VISIBILITY const_reference back() const
681 {
682 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
683 return *(this->__end_ - 1);
684 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000685
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000686 _LIBCPP_INLINE_VISIBILITY
687 value_type* data() _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000688 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000689 _LIBCPP_INLINE_VISIBILITY
690 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000691 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000692
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000693 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000694#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000695 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000696#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000697 template <class... _Args>
698 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000699#endif // _LIBCPP_HAS_NO_VARIADICS
700#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000701 void pop_back();
702
703 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000704#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000705 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000706#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000707 template <class... _Args>
708 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000709#endif // _LIBCPP_HAS_NO_VARIADICS
710#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000711 iterator insert(const_iterator __position, size_type __n, const_reference __x);
712 template <class _InputIterator>
713 typename enable_if
714 <
715 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000716 !__is_forward_iterator<_InputIterator>::value &&
717 is_constructible<
718 value_type,
719 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000720 iterator
721 >::type
722 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
723 template <class _ForwardIterator>
724 typename enable_if
725 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000726 __is_forward_iterator<_ForwardIterator>::value &&
727 is_constructible<
728 value_type,
729 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000730 iterator
731 >::type
732 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +0000733#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000734 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000735 iterator insert(const_iterator __position, initializer_list<value_type> __il)
736 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000737#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000738
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000739 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000740 iterator erase(const_iterator __first, const_iterator __last);
741
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000742 _LIBCPP_INLINE_VISIBILITY
743 void clear() _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000744 {
745 __base::clear();
746 __invalidate_all_iterators();
747 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000748
749 void resize(size_type __sz);
750 void resize(size_type __sz, const_reference __x);
751
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000752 void swap(vector&)
753 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
754 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000755
756 bool __invariants() const;
757
Howard Hinnantabe26282011-09-16 17:29:17 +0000758#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000759
760 bool __dereferenceable(const const_iterator* __i) const;
761 bool __decrementable(const const_iterator* __i) const;
762 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
763 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
764
Howard Hinnantabe26282011-09-16 17:29:17 +0000765#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000766
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000767private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000768 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000769 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000770 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000771 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31 +0000772 void __construct_at_end(size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000773 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000774 template <class _ForwardIterator>
775 typename enable_if
776 <
777 __is_forward_iterator<_ForwardIterator>::value,
778 void
779 >::type
780 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
781 void __move_construct_at_end(pointer __first, pointer __last);
782 void __append(size_type __n);
783 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000785 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000787 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000788 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
789 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
790 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000791 void __move_assign(vector& __c, true_type)
792 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000793 void __move_assign(vector& __c, false_type);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000795 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000796 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000797#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000798 __c_node* __c = __get_db()->__find_c_and_lock(this);
799 for (__i_node** __p = __c->end_; __p != __c->beg_; )
800 {
801 --__p;
802 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
803 if (__i->base() > __new_last)
804 {
805 (*__p)->__c_ = nullptr;
806 if (--__c->end_ != __p)
807 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
808 }
809 }
810 __get_db()->unlock();
811#endif
812 __base::__destruct_at_end(__new_last);
813 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000814 template <class _Up>
815 void
816#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
817 __push_back_slow_path(_Up&& __x);
818#else
819 __push_back_slow_path(_Up& __x);
820#endif
Howard Hinnant0438ea22012-02-26 15:30:12 +0000821#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
822 template <class... _Args>
823 void
824 __emplace_back_slow_path(_Args&&... __args);
825#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000826};
827
828template <class _Tp, class _Allocator>
829void
830vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
831{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000832 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000833 _VSTD::swap(this->__begin_, __v.__begin_);
834 _VSTD::swap(this->__end_, __v.__end_);
835 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000836 __v.__first_ = __v.__begin_;
837 __invalidate_all_iterators();
838}
839
840template <class _Tp, class _Allocator>
841typename vector<_Tp, _Allocator>::pointer
842vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
843{
844 pointer __r = __v.__begin_;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000845 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
846 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000847 _VSTD::swap(this->__begin_, __v.__begin_);
848 _VSTD::swap(this->__end_, __v.__end_);
849 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000850 __v.__first_ = __v.__begin_;
851 __invalidate_all_iterators();
852 return __r;
853}
854
855// Allocate space for __n objects
856// throws length_error if __n > max_size()
857// throws (probably bad_alloc) if memory run out
858// Precondition: __begin_ == __end_ == __end_cap() == 0
859// Precondition: __n > 0
860// Postcondition: capacity() == __n
861// Postcondition: size() == 0
862template <class _Tp, class _Allocator>
863void
864vector<_Tp, _Allocator>::allocate(size_type __n)
865{
866 if (__n > max_size())
867 this->__throw_length_error();
868 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
869 this->__end_cap() = this->__begin_ + __n;
870}
871
872template <class _Tp, class _Allocator>
873void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000874vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000875{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000876 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000877 {
878 clear();
879 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000880 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000881 }
882}
883
884template <class _Tp, class _Allocator>
885typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000886vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000887{
Sean Hunt110b8bf2011-07-29 23:31:58 +0000888 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 +0000889}
890
891// Precondition: __new_size > capacity()
892template <class _Tp, class _Allocator>
893_LIBCPP_INLINE_VISIBILITY inline
894typename vector<_Tp, _Allocator>::size_type
895vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
896{
897 const size_type __ms = max_size();
898 if (__new_size > __ms)
899 this->__throw_length_error();
900 const size_type __cap = capacity();
901 if (__cap >= __ms / 2)
902 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58 +0000903 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000904}
905
906// Default constructs __n objects starting at __end_
907// throws if construction throws
908// Precondition: __n > 0
909// Precondition: size() + __n <= capacity()
910// Postcondition: size() == size() + __n
911template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000912void
913vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
914{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000915 allocator_type& __a = this->__alloc();
916 do
917 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000918 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000919 ++this->__end_;
920 --__n;
921 } while (__n > 0);
922}
923
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000924// Copy constructs __n objects starting at __end_ from __x
925// throws if construction throws
926// Precondition: __n > 0
927// Precondition: size() + __n <= capacity()
928// Postcondition: size() == old size() + __n
929// Postcondition: [i] == __x for all i in [size() - __n, __n)
930template <class _Tp, class _Allocator>
931_LIBCPP_INLINE_VISIBILITY inline
932void
933vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
934{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000935 allocator_type& __a = this->__alloc();
936 do
937 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000938 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000939 ++this->__end_;
940 --__n;
941 } while (__n > 0);
942}
943
944template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000945template <class _ForwardIterator>
946typename enable_if
947<
948 __is_forward_iterator<_ForwardIterator>::value,
949 void
950>::type
951vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
952{
953 allocator_type& __a = this->__alloc();
954 for (; __first != __last; ++__first)
955 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000956 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000957 ++this->__end_;
958 }
959}
960
961template <class _Tp, class _Allocator>
962void
963vector<_Tp, _Allocator>::__move_construct_at_end(pointer __first, pointer __last)
964{
965 allocator_type& __a = this->__alloc();
966 for (; __first != __last; ++__first)
967 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000968 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
969 _VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000970 ++this->__end_;
971 }
972}
973
974// Default constructs __n objects starting at __end_
975// throws if construction throws
976// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000977// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000978template <class _Tp, class _Allocator>
979void
980vector<_Tp, _Allocator>::__append(size_type __n)
981{
982 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
983 this->__construct_at_end(__n);
984 else
985 {
986 allocator_type& __a = this->__alloc();
987 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
988 __v.__construct_at_end(__n);
989 __swap_out_circular_buffer(__v);
990 }
991}
992
993// Default constructs __n objects starting at __end_
994// throws if construction throws
995// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000996// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000997template <class _Tp, class _Allocator>
998void
999vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1000{
1001 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1002 this->__construct_at_end(__n, __x);
1003 else
1004 {
1005 allocator_type& __a = this->__alloc();
1006 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1007 __v.__construct_at_end(__n, __x);
1008 __swap_out_circular_buffer(__v);
1009 }
1010}
1011
1012template <class _Tp, class _Allocator>
1013vector<_Tp, _Allocator>::vector(size_type __n)
1014{
Howard Hinnant0442b122011-09-16 18:41:29 +00001015#if _LIBCPP_DEBUG_LEVEL >= 2
1016 __get_db()->__insert_c(this);
1017#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001018 if (__n > 0)
1019 {
1020 allocate(__n);
1021 __construct_at_end(__n);
1022 }
1023}
1024
1025template <class _Tp, class _Allocator>
1026vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1027{
Howard Hinnant0442b122011-09-16 18:41:29 +00001028#if _LIBCPP_DEBUG_LEVEL >= 2
1029 __get_db()->__insert_c(this);
1030#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001031 if (__n > 0)
1032 {
1033 allocate(__n);
1034 __construct_at_end(__n, __x);
1035 }
1036}
1037
1038template <class _Tp, class _Allocator>
1039vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1040 : __base(__a)
1041{
Howard Hinnant0442b122011-09-16 18:41:29 +00001042#if _LIBCPP_DEBUG_LEVEL >= 2
1043 __get_db()->__insert_c(this);
1044#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001045 if (__n > 0)
1046 {
1047 allocate(__n);
1048 __construct_at_end(__n, __x);
1049 }
1050}
1051
1052template <class _Tp, class _Allocator>
1053template <class _InputIterator>
1054vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
1055 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001056 !__is_forward_iterator<_InputIterator>::value &&
1057 is_constructible<
1058 value_type,
1059 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001060{
Howard Hinnantabe26282011-09-16 17:29:17 +00001061#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001062 __get_db()->__insert_c(this);
1063#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001064 for (; __first != __last; ++__first)
1065 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001066}
1067
1068template <class _Tp, class _Allocator>
1069template <class _InputIterator>
1070vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1071 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001072 !__is_forward_iterator<_InputIterator>::value &&
1073 is_constructible<
1074 value_type,
1075 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001076 : __base(__a)
1077{
Howard Hinnantabe26282011-09-16 17:29:17 +00001078#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001079 __get_db()->__insert_c(this);
1080#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001081 for (; __first != __last; ++__first)
1082 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001083}
1084
1085template <class _Tp, class _Allocator>
1086template <class _ForwardIterator>
1087vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001088 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1089 is_constructible<
1090 value_type,
1091 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001092{
Howard Hinnant0442b122011-09-16 18:41:29 +00001093#if _LIBCPP_DEBUG_LEVEL >= 2
1094 __get_db()->__insert_c(this);
1095#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001096 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001097 if (__n > 0)
1098 {
1099 allocate(__n);
1100 __construct_at_end(__first, __last);
1101 }
1102}
1103
1104template <class _Tp, class _Allocator>
1105template <class _ForwardIterator>
1106vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001107 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1108 is_constructible<
1109 value_type,
1110 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001111 : __base(__a)
1112{
Howard Hinnant0442b122011-09-16 18:41:29 +00001113#if _LIBCPP_DEBUG_LEVEL >= 2
1114 __get_db()->__insert_c(this);
1115#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001116 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001117 if (__n > 0)
1118 {
1119 allocate(__n);
1120 __construct_at_end(__first, __last);
1121 }
1122}
1123
1124template <class _Tp, class _Allocator>
1125vector<_Tp, _Allocator>::vector(const vector& __x)
1126 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1127{
Howard Hinnant0442b122011-09-16 18:41:29 +00001128#if _LIBCPP_DEBUG_LEVEL >= 2
1129 __get_db()->__insert_c(this);
1130#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001131 size_type __n = __x.size();
1132 if (__n > 0)
1133 {
1134 allocate(__n);
1135 __construct_at_end(__x.__begin_, __x.__end_);
1136 }
1137}
1138
1139template <class _Tp, class _Allocator>
1140vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1141 : __base(__a)
1142{
Howard Hinnant0442b122011-09-16 18:41:29 +00001143#if _LIBCPP_DEBUG_LEVEL >= 2
1144 __get_db()->__insert_c(this);
1145#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001146 size_type __n = __x.size();
1147 if (__n > 0)
1148 {
1149 allocate(__n);
1150 __construct_at_end(__x.__begin_, __x.__end_);
1151 }
1152}
1153
Howard Hinnant73d21a42010-09-04 23:28:19 +00001154#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001155
1156template <class _Tp, class _Allocator>
1157_LIBCPP_INLINE_VISIBILITY inline
1158vector<_Tp, _Allocator>::vector(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001159 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001160 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001161{
Howard Hinnantabe26282011-09-16 17:29:17 +00001162#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001163 __get_db()->__insert_c(this);
Howard Hinnante6125bd2011-09-19 16:34:29 +00001164 __get_db()->swap(this, &__x);
Howard Hinnant7a563db2011-09-14 18:33:51 +00001165#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001166 this->__begin_ = __x.__begin_;
1167 this->__end_ = __x.__end_;
1168 this->__end_cap() = __x.__end_cap();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001169 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001170}
1171
1172template <class _Tp, class _Allocator>
1173_LIBCPP_INLINE_VISIBILITY inline
1174vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1175 : __base(__a)
1176{
Howard Hinnant0442b122011-09-16 18:41:29 +00001177#if _LIBCPP_DEBUG_LEVEL >= 2
1178 __get_db()->__insert_c(this);
1179#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001180 if (__a == __x.__alloc())
1181 {
1182 this->__begin_ = __x.__begin_;
1183 this->__end_ = __x.__end_;
1184 this->__end_cap() = __x.__end_cap();
1185 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001186#if _LIBCPP_DEBUG_LEVEL >= 2
1187 __get_db()->swap(this, &__x);
1188#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001189 }
1190 else
1191 {
Howard Hinnant99968442011-11-29 18:15:50 +00001192 typedef move_iterator<iterator> _Ip;
1193 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001194 }
1195}
1196
Howard Hinnante3e32912011-08-12 21:56:02 +00001197#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1198
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001199template <class _Tp, class _Allocator>
1200_LIBCPP_INLINE_VISIBILITY inline
1201vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1202{
Howard Hinnant0442b122011-09-16 18:41:29 +00001203#if _LIBCPP_DEBUG_LEVEL >= 2
1204 __get_db()->__insert_c(this);
1205#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001206 if (__il.size() > 0)
1207 {
1208 allocate(__il.size());
1209 __construct_at_end(__il.begin(), __il.end());
1210 }
1211}
1212
1213template <class _Tp, class _Allocator>
1214_LIBCPP_INLINE_VISIBILITY inline
1215vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1216 : __base(__a)
1217{
Howard Hinnant0442b122011-09-16 18:41:29 +00001218#if _LIBCPP_DEBUG_LEVEL >= 2
1219 __get_db()->__insert_c(this);
1220#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001221 if (__il.size() > 0)
1222 {
1223 allocate(__il.size());
1224 __construct_at_end(__il.begin(), __il.end());
1225 }
1226}
1227
Howard Hinnante3e32912011-08-12 21:56:02 +00001228#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1229
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001230template <class _Tp, class _Allocator>
1231_LIBCPP_INLINE_VISIBILITY inline
1232vector<_Tp, _Allocator>&
1233vector<_Tp, _Allocator>::operator=(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001234 _NOEXCEPT_(
1235 __alloc_traits::propagate_on_container_move_assignment::value &&
1236 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001237{
1238 __move_assign(__x, integral_constant<bool,
1239 __alloc_traits::propagate_on_container_move_assignment::value>());
1240 return *this;
1241}
1242
1243template <class _Tp, class _Allocator>
1244void
1245vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1246{
1247 if (__base::__alloc() != __c.__alloc())
1248 {
Howard Hinnant99968442011-11-29 18:15:50 +00001249 typedef move_iterator<iterator> _Ip;
1250 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001251 }
1252 else
1253 __move_assign(__c, true_type());
1254}
1255
1256template <class _Tp, class _Allocator>
1257void
1258vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001259 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001260{
1261 deallocate();
1262 this->__begin_ = __c.__begin_;
1263 this->__end_ = __c.__end_;
1264 this->__end_cap() = __c.__end_cap();
1265 __base::__move_assign_alloc(__c);
1266 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001267#if _LIBCPP_DEBUG_LEVEL >= 2
1268 __get_db()->swap(this, &__c);
1269#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001270}
1271
Howard Hinnant73d21a42010-09-04 23:28:19 +00001272#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001273
1274template <class _Tp, class _Allocator>
1275_LIBCPP_INLINE_VISIBILITY inline
1276vector<_Tp, _Allocator>&
1277vector<_Tp, _Allocator>::operator=(const vector& __x)
1278{
1279 if (this != &__x)
1280 {
1281 __base::__copy_assign_alloc(__x);
1282 assign(__x.__begin_, __x.__end_);
1283 }
1284 return *this;
1285}
1286
1287template <class _Tp, class _Allocator>
1288template <class _InputIterator>
1289typename enable_if
1290<
1291 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001292 !__is_forward_iterator<_InputIterator>::value &&
1293 is_constructible<
1294 _Tp,
1295 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001296 void
1297>::type
1298vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1299{
1300 clear();
1301 for (; __first != __last; ++__first)
1302 push_back(*__first);
1303}
1304
1305template <class _Tp, class _Allocator>
1306template <class _ForwardIterator>
1307typename enable_if
1308<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001309 __is_forward_iterator<_ForwardIterator>::value &&
1310 is_constructible<
1311 _Tp,
1312 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001313 void
1314>::type
1315vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1316{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001317 typename iterator_traits<_ForwardIterator>::difference_type __new_size = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001318 if (static_cast<size_type>(__new_size) <= capacity())
1319 {
1320 _ForwardIterator __mid = __last;
1321 bool __growing = false;
1322 if (static_cast<size_type>(__new_size) > size())
1323 {
1324 __growing = true;
1325 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001326 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001327 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001328 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001329 if (__growing)
1330 __construct_at_end(__mid, __last);
1331 else
1332 this->__destruct_at_end(__m);
1333 }
1334 else
1335 {
1336 deallocate();
1337 allocate(__recommend(static_cast<size_type>(__new_size)));
1338 __construct_at_end(__first, __last);
1339 }
1340}
1341
1342template <class _Tp, class _Allocator>
1343void
1344vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1345{
1346 if (__n <= capacity())
1347 {
1348 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001349 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001350 if (__n > __s)
1351 __construct_at_end(__n - __s, __u);
1352 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001353 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001354 }
1355 else
1356 {
1357 deallocate();
1358 allocate(__recommend(static_cast<size_type>(__n)));
1359 __construct_at_end(__n, __u);
1360 }
1361}
1362
Howard Hinnant324bb032010-08-22 00:02:43 +00001363template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001364_LIBCPP_INLINE_VISIBILITY inline
1365typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001366vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001367{
Howard Hinnantabe26282011-09-16 17:29:17 +00001368#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001369 return iterator(this, __p);
1370#else
1371 return iterator(__p);
1372#endif
1373}
1374
Howard Hinnant324bb032010-08-22 00:02:43 +00001375template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001376_LIBCPP_INLINE_VISIBILITY inline
1377typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001378vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001379{
Howard Hinnantabe26282011-09-16 17:29:17 +00001380#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001381 return const_iterator(this, __p);
1382#else
1383 return const_iterator(__p);
1384#endif
1385}
1386
Howard Hinnant324bb032010-08-22 00:02:43 +00001387template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001388_LIBCPP_INLINE_VISIBILITY inline
1389typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001390vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001391{
1392 return __make_iter(this->__begin_);
1393}
1394
Howard Hinnant324bb032010-08-22 00:02:43 +00001395template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001396_LIBCPP_INLINE_VISIBILITY inline
1397typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001398vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001399{
1400 return __make_iter(this->__begin_);
1401}
1402
Howard Hinnant324bb032010-08-22 00:02:43 +00001403template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001404_LIBCPP_INLINE_VISIBILITY inline
1405typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001406vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001407{
1408 return __make_iter(this->__end_);
1409}
1410
Howard Hinnant324bb032010-08-22 00:02:43 +00001411template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001412_LIBCPP_INLINE_VISIBILITY inline
1413typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001414vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001415{
1416 return __make_iter(this->__end_);
1417}
1418
Howard Hinnant324bb032010-08-22 00:02:43 +00001419template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001420_LIBCPP_INLINE_VISIBILITY inline
1421typename vector<_Tp, _Allocator>::reference
1422vector<_Tp, _Allocator>::operator[](size_type __n)
1423{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001424 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001425 return this->__begin_[__n];
1426}
1427
Howard Hinnant324bb032010-08-22 00:02:43 +00001428template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001429_LIBCPP_INLINE_VISIBILITY inline
1430typename vector<_Tp, _Allocator>::const_reference
1431vector<_Tp, _Allocator>::operator[](size_type __n) const
1432{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001433 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001434 return this->__begin_[__n];
1435}
1436
Howard Hinnant324bb032010-08-22 00:02:43 +00001437template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001438typename vector<_Tp, _Allocator>::reference
1439vector<_Tp, _Allocator>::at(size_type __n)
1440{
1441 if (__n >= size())
1442 this->__throw_out_of_range();
1443 return this->__begin_[__n];
1444}
1445
Howard Hinnant324bb032010-08-22 00:02:43 +00001446template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001447typename vector<_Tp, _Allocator>::const_reference
1448vector<_Tp, _Allocator>::at(size_type __n) const
1449{
1450 if (__n >= size())
1451 this->__throw_out_of_range();
1452 return this->__begin_[__n];
1453}
1454
1455template <class _Tp, class _Allocator>
1456void
1457vector<_Tp, _Allocator>::reserve(size_type __n)
1458{
1459 if (__n > capacity())
1460 {
1461 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001462 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463 __swap_out_circular_buffer(__v);
1464 }
1465}
1466
1467template <class _Tp, class _Allocator>
1468void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001469vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001470{
1471 if (capacity() > size())
1472 {
1473#ifndef _LIBCPP_NO_EXCEPTIONS
1474 try
1475 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001476#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001477 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001478 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001479 __swap_out_circular_buffer(__v);
1480#ifndef _LIBCPP_NO_EXCEPTIONS
1481 }
1482 catch (...)
1483 {
1484 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001485#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486 }
1487}
1488
1489template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001490template <class _Up>
1491void
1492#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1493vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1494#else
1495vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1496#endif
1497{
1498 allocator_type& __a = this->__alloc();
1499 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1500 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnantf619e232013-01-11 20:36:59 +00001501 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1502 __v.__end_++;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001503 __swap_out_circular_buffer(__v);
1504}
1505
1506template <class _Tp, class _Allocator>
1507_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001508void
1509vector<_Tp, _Allocator>::push_back(const_reference __x)
1510{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001511 if (this->__end_ != this->__end_cap())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001512 {
1513 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001514 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001515 ++this->__end_;
1516 }
1517 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001518 __push_back_slow_path(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001519}
1520
Howard Hinnant73d21a42010-09-04 23:28:19 +00001521#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001522
1523template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001524_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001525void
1526vector<_Tp, _Allocator>::push_back(value_type&& __x)
1527{
1528 if (this->__end_ < this->__end_cap())
1529 {
1530 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001531 _VSTD::__to_raw_pointer(this->__end_),
1532 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001533 ++this->__end_;
1534 }
1535 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001536 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001537}
1538
Howard Hinnant73d21a42010-09-04 23:28:19 +00001539#ifndef _LIBCPP_HAS_NO_VARIADICS
1540
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001541template <class _Tp, class _Allocator>
1542template <class... _Args>
1543void
Howard Hinnant0438ea22012-02-26 15:30:12 +00001544vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1545{
1546 allocator_type& __a = this->__alloc();
1547 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1548// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantf619e232013-01-11 20:36:59 +00001549 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1550 __v.__end_++;
Howard Hinnant0438ea22012-02-26 15:30:12 +00001551 __swap_out_circular_buffer(__v);
1552}
1553
1554template <class _Tp, class _Allocator>
1555template <class... _Args>
1556_LIBCPP_INLINE_VISIBILITY inline
1557void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001558vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1559{
1560 if (this->__end_ < this->__end_cap())
1561 {
1562 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001563 _VSTD::__to_raw_pointer(this->__end_),
1564 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001565 ++this->__end_;
1566 }
1567 else
Howard Hinnant0438ea22012-02-26 15:30:12 +00001568 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001569}
1570
Howard Hinnant73d21a42010-09-04 23:28:19 +00001571#endif // _LIBCPP_HAS_NO_VARIADICS
1572#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001573
1574template <class _Tp, class _Allocator>
1575_LIBCPP_INLINE_VISIBILITY inline
1576void
1577vector<_Tp, _Allocator>::pop_back()
1578{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001579 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001580 this->__destruct_at_end(this->__end_ - 1);
1581}
1582
1583template <class _Tp, class _Allocator>
1584_LIBCPP_INLINE_VISIBILITY inline
1585typename vector<_Tp, _Allocator>::iterator
1586vector<_Tp, _Allocator>::erase(const_iterator __position)
1587{
Howard Hinnantabe26282011-09-16 17:29:17 +00001588#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001589 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1590 "vector::erase(iterator) called with an iterator not"
1591 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001592#endif
Howard Hinnant782da332013-03-25 22:12:26 +00001593 _LIBCPP_ASSERT(__position != end(),
1594 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001595 difference_type __ps = __position - cbegin();
1596 pointer __p = this->__begin_ + __ps;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001598 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001599 return __r;
1600}
1601
1602template <class _Tp, class _Allocator>
1603typename vector<_Tp, _Allocator>::iterator
1604vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1605{
Howard Hinnantabe26282011-09-16 17:29:17 +00001606#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001607 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1608 "vector::erase(iterator, iterator) called with an iterator not"
1609 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001610#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +00001611 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001612 pointer __p = this->__begin_ + (__first - begin());
1613 iterator __r = __make_iter(__p);
Howard Hinnantb4e67cf2013-04-18 15:02:57 +00001614 if (__first != __last)
1615 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001616 return __r;
1617}
1618
1619template <class _Tp, class _Allocator>
1620void
1621vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1622{
1623 pointer __old_last = this->__end_;
1624 difference_type __n = __old_last - __to;
1625 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1626 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001627 _VSTD::__to_raw_pointer(this->__end_),
1628 _VSTD::move(*__i));
1629 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001630}
1631
1632template <class _Tp, class _Allocator>
1633typename vector<_Tp, _Allocator>::iterator
1634vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1635{
Howard Hinnantabe26282011-09-16 17:29:17 +00001636#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001637 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1638 "vector::insert(iterator, x) called with an iterator not"
1639 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001640#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001641 pointer __p = this->__begin_ + (__position - begin());
1642 if (this->__end_ < this->__end_cap())
1643 {
1644 if (__p == this->__end_)
1645 {
1646 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001647 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001648 ++this->__end_;
1649 }
1650 else
1651 {
1652 __move_range(__p, this->__end_, __p + 1);
1653 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1654 if (__p <= __xr && __xr < this->__end_)
1655 ++__xr;
1656 *__p = *__xr;
1657 }
1658 }
1659 else
1660 {
1661 allocator_type& __a = this->__alloc();
1662 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1663 __v.push_back(__x);
1664 __p = __swap_out_circular_buffer(__v, __p);
1665 }
1666 return __make_iter(__p);
1667}
1668
Howard Hinnant73d21a42010-09-04 23:28:19 +00001669#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001670
1671template <class _Tp, class _Allocator>
1672typename vector<_Tp, _Allocator>::iterator
1673vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1674{
Howard Hinnantabe26282011-09-16 17:29:17 +00001675#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001676 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1677 "vector::insert(iterator, x) called with an iterator not"
1678 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001679#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001680 pointer __p = this->__begin_ + (__position - begin());
1681 if (this->__end_ < this->__end_cap())
1682 {
1683 if (__p == this->__end_)
1684 {
1685 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001686 _VSTD::__to_raw_pointer(this->__end_),
1687 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001688 ++this->__end_;
1689 }
1690 else
1691 {
1692 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001693 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001694 }
1695 }
1696 else
1697 {
1698 allocator_type& __a = this->__alloc();
1699 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001700 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001701 __p = __swap_out_circular_buffer(__v, __p);
1702 }
1703 return __make_iter(__p);
1704}
1705
Howard Hinnant73d21a42010-09-04 23:28:19 +00001706#ifndef _LIBCPP_HAS_NO_VARIADICS
1707
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001708template <class _Tp, class _Allocator>
1709template <class... _Args>
1710typename vector<_Tp, _Allocator>::iterator
1711vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1712{
Howard Hinnantabe26282011-09-16 17:29:17 +00001713#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001714 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1715 "vector::emplace(iterator, x) called with an iterator not"
1716 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001717#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001718 pointer __p = this->__begin_ + (__position - begin());
1719 if (this->__end_ < this->__end_cap())
1720 {
1721 if (__p == this->__end_)
1722 {
1723 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001724 _VSTD::__to_raw_pointer(this->__end_),
1725 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001726 ++this->__end_;
1727 }
1728 else
1729 {
Howard Hinnanta58402a2012-07-08 23:23:04 +00001730 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001731 __move_range(__p, this->__end_, __p + 1);
Howard Hinnanta58402a2012-07-08 23:23:04 +00001732 *__p = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001733 }
1734 }
1735 else
1736 {
1737 allocator_type& __a = this->__alloc();
1738 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001739 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001740 __p = __swap_out_circular_buffer(__v, __p);
1741 }
1742 return __make_iter(__p);
1743}
1744
Howard Hinnant73d21a42010-09-04 23:28:19 +00001745#endif // _LIBCPP_HAS_NO_VARIADICS
1746#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001747
1748template <class _Tp, class _Allocator>
1749typename vector<_Tp, _Allocator>::iterator
1750vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1751{
Howard Hinnantabe26282011-09-16 17:29:17 +00001752#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001753 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1754 "vector::insert(iterator, n, x) called with an iterator not"
1755 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001756#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001757 pointer __p = this->__begin_ + (__position - begin());
1758 if (__n > 0)
1759 {
1760 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1761 {
1762 size_type __old_n = __n;
1763 pointer __old_last = this->__end_;
1764 if (__n > static_cast<size_type>(this->__end_ - __p))
1765 {
1766 size_type __cx = __n - (this->__end_ - __p);
1767 __construct_at_end(__cx, __x);
1768 __n -= __cx;
1769 }
1770 if (__n > 0)
1771 {
1772 __move_range(__p, __old_last, __p + __old_n);
1773 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1774 if (__p <= __xr && __xr < this->__end_)
1775 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001776 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001777 }
1778 }
1779 else
1780 {
1781 allocator_type& __a = this->__alloc();
1782 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1783 __v.__construct_at_end(__n, __x);
1784 __p = __swap_out_circular_buffer(__v, __p);
1785 }
1786 }
1787 return __make_iter(__p);
1788}
1789
1790template <class _Tp, class _Allocator>
1791template <class _InputIterator>
1792typename enable_if
1793<
1794 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001795 !__is_forward_iterator<_InputIterator>::value &&
1796 is_constructible<
1797 _Tp,
1798 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001799 typename vector<_Tp, _Allocator>::iterator
1800>::type
1801vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1802{
Howard Hinnantabe26282011-09-16 17:29:17 +00001803#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001804 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1805 "vector::insert(iterator, range) called with an iterator not"
1806 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001807#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001808 difference_type __off = __position - begin();
1809 pointer __p = this->__begin_ + __off;
1810 allocator_type& __a = this->__alloc();
1811 pointer __old_last = this->__end_;
1812 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1813 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001814 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001815 *__first);
1816 ++this->__end_;
1817 }
1818 __split_buffer<value_type, allocator_type&> __v(__a);
1819 if (__first != __last)
1820 {
1821#ifndef _LIBCPP_NO_EXCEPTIONS
1822 try
1823 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001824#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001825 __v.__construct_at_end(__first, __last);
1826 difference_type __old_size = __old_last - this->__begin_;
1827 difference_type __old_p = __p - this->__begin_;
1828 reserve(__recommend(size() + __v.size()));
1829 __p = this->__begin_ + __old_p;
1830 __old_last = this->__begin_ + __old_size;
1831#ifndef _LIBCPP_NO_EXCEPTIONS
1832 }
1833 catch (...)
1834 {
1835 erase(__make_iter(__old_last), end());
1836 throw;
1837 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001838#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001839 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001840 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant0442b122011-09-16 18:41:29 +00001841 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1842 make_move_iterator(__v.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001843 return begin() + __off;
1844}
1845
1846template <class _Tp, class _Allocator>
1847template <class _ForwardIterator>
1848typename enable_if
1849<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001850 __is_forward_iterator<_ForwardIterator>::value &&
1851 is_constructible<
1852 _Tp,
1853 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001854 typename vector<_Tp, _Allocator>::iterator
1855>::type
1856vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1857{
Howard Hinnantabe26282011-09-16 17:29:17 +00001858#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001859 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1860 "vector::insert(iterator, range) called with an iterator not"
1861 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001862#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001863 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00001864 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001865 if (__n > 0)
1866 {
1867 if (__n <= this->__end_cap() - this->__end_)
1868 {
1869 size_type __old_n = __n;
1870 pointer __old_last = this->__end_;
1871 _ForwardIterator __m = __last;
1872 difference_type __dx = this->__end_ - __p;
1873 if (__n > __dx)
1874 {
1875 __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001876 _VSTD::advance(__m, this->__end_ - __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001877 __construct_at_end(__m, __last);
1878 __n = __dx;
1879 }
1880 if (__n > 0)
1881 {
1882 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001883 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001884 }
1885 }
1886 else
1887 {
1888 allocator_type& __a = this->__alloc();
1889 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1890 __v.__construct_at_end(__first, __last);
1891 __p = __swap_out_circular_buffer(__v, __p);
1892 }
1893 }
1894 return __make_iter(__p);
1895}
1896
1897template <class _Tp, class _Allocator>
1898void
1899vector<_Tp, _Allocator>::resize(size_type __sz)
1900{
1901 size_type __cs = size();
1902 if (__cs < __sz)
1903 this->__append(__sz - __cs);
1904 else if (__cs > __sz)
1905 this->__destruct_at_end(this->__begin_ + __sz);
1906}
1907
1908template <class _Tp, class _Allocator>
1909void
1910vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
1911{
1912 size_type __cs = size();
1913 if (__cs < __sz)
1914 this->__append(__sz - __cs, __x);
1915 else if (__cs > __sz)
1916 this->__destruct_at_end(this->__begin_ + __sz);
1917}
1918
1919template <class _Tp, class _Allocator>
1920void
1921vector<_Tp, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001922 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1923 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001924{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001925 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
1926 this->__alloc() == __x.__alloc(),
1927 "vector::swap: Either propagate_on_container_swap must be true"
1928 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +00001929 _VSTD::swap(this->__begin_, __x.__begin_);
1930 _VSTD::swap(this->__end_, __x.__end_);
1931 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001932 __base::__swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnantabe26282011-09-16 17:29:17 +00001933#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001934 __get_db()->swap(this, &__x);
Howard Hinnantabe26282011-09-16 17:29:17 +00001935#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001936}
1937
Howard Hinnant324bb032010-08-22 00:02:43 +00001938template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001939bool
1940vector<_Tp, _Allocator>::__invariants() const
1941{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001942 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001943 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001944 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001945 return false;
1946 }
1947 else
1948 {
1949 if (this->__begin_ > this->__end_)
1950 return false;
1951 if (this->__begin_ == this->__end_cap())
1952 return false;
1953 if (this->__end_ > this->__end_cap())
1954 return false;
1955 }
1956 return true;
1957}
1958
Howard Hinnantabe26282011-09-16 17:29:17 +00001959#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001960
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001961template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:51 +00001962bool
1963vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
1964{
1965 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
1966}
1967
1968template <class _Tp, class _Allocator>
1969bool
1970vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
1971{
1972 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
1973}
1974
1975template <class _Tp, class _Allocator>
1976bool
1977vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
1978{
1979 const_pointer __p = __i->base() + __n;
1980 return this->__begin_ <= __p && __p <= this->__end_;
1981}
1982
1983template <class _Tp, class _Allocator>
1984bool
1985vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
1986{
1987 const_pointer __p = __i->base() + __n;
1988 return this->__begin_ <= __p && __p < this->__end_;
1989}
1990
Howard Hinnantabe26282011-09-16 17:29:17 +00001991#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001992
1993template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001994_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001995void
1996vector<_Tp, _Allocator>::__invalidate_all_iterators()
1997{
Howard Hinnantabe26282011-09-16 17:29:17 +00001998#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001999 __get_db()->__invalidate_all(this);
Howard Hinnantabe26282011-09-16 17:29:17 +00002000#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002001}
2002
2003// vector<bool>
2004
2005template <class _Allocator> class vector<bool, _Allocator>;
2006
2007template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2008
2009template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002010struct __has_storage_type<vector<bool, _Allocator> >
2011{
2012 static const bool value = true;
2013};
2014
2015template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002016class _LIBCPP_TYPE_VIS_ONLY vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002017 : private __vector_base_common<true>
2018{
2019public:
2020 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00002021 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002022 typedef _Allocator allocator_type;
2023 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002024 typedef typename __alloc_traits::size_type size_type;
2025 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantf867f632012-05-07 16:50:38 +00002026 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002027 typedef __bit_iterator<vector, false> pointer;
2028 typedef __bit_iterator<vector, true> const_pointer;
2029#ifdef _LIBCPP_DEBUG
2030 typedef __debug_iter<vector, pointer> iterator;
2031 typedef __debug_iter<vector, const_pointer> const_iterator;
2032
2033 friend class __debug_iter<vector, pointer>;
2034 friend class __debug_iter<vector, const_pointer>;
2035
2036 pair<iterator*, const_iterator*> __iterator_list_;
2037
2038 _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;}
2039 _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;}
Howard Hinnant324bb032010-08-22 00:02:43 +00002040#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002041 typedef pointer iterator;
2042 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +00002043#endif // _LIBCPP_DEBUG
Howard Hinnant0949eed2011-06-30 21:18:19 +00002044 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2045 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002046
2047private:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002048 typedef typename __alloc_traits::template
2049#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
2050 rebind_alloc<__storage_type>
2051#else
2052 rebind_alloc<__storage_type>::other
2053#endif
2054 __storage_allocator;
2055 typedef allocator_traits<__storage_allocator> __storage_traits;
2056 typedef typename __storage_traits::pointer __storage_pointer;
2057 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2058
2059 __storage_pointer __begin_;
2060 size_type __size_;
2061 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002062public:
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002063 typedef __bit_reference<vector> reference;
2064 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002065private:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002066 _LIBCPP_INLINE_VISIBILITY
2067 size_type& __cap() _NOEXCEPT
2068 {return __cap_alloc_.first();}
2069 _LIBCPP_INLINE_VISIBILITY
2070 const size_type& __cap() const _NOEXCEPT
2071 {return __cap_alloc_.first();}
2072 _LIBCPP_INLINE_VISIBILITY
2073 __storage_allocator& __alloc() _NOEXCEPT
2074 {return __cap_alloc_.second();}
2075 _LIBCPP_INLINE_VISIBILITY
2076 const __storage_allocator& __alloc() const _NOEXCEPT
2077 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002078
2079 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2080
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002081 _LIBCPP_INLINE_VISIBILITY
2082 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002083 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002084 _LIBCPP_INLINE_VISIBILITY
2085 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002086 {return (__n - 1) / __bits_per_word + 1;}
2087
2088public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002089 _LIBCPP_INLINE_VISIBILITY
2090 vector()
2091 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002092 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002093 ~vector();
2094 explicit vector(size_type __n);
2095 vector(size_type __n, const value_type& __v);
2096 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2097 template <class _InputIterator>
2098 vector(_InputIterator __first, _InputIterator __last,
2099 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2100 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2101 template <class _InputIterator>
2102 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2103 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2104 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2105 template <class _ForwardIterator>
2106 vector(_ForwardIterator __first, _ForwardIterator __last,
2107 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2108 template <class _ForwardIterator>
2109 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2110 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2111
2112 vector(const vector& __v);
2113 vector(const vector& __v, const allocator_type& __a);
2114 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00002115#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002116 vector(initializer_list<value_type> __il);
2117 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00002118#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002119
Howard Hinnant73d21a42010-09-04 23:28:19 +00002120#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002121 _LIBCPP_INLINE_VISIBILITY
2122 vector(vector&& __v)
2123 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002124 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002125 _LIBCPP_INLINE_VISIBILITY
2126 vector& operator=(vector&& __v)
2127 _NOEXCEPT_(
2128 __alloc_traits::propagate_on_container_move_assignment::value &&
2129 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002130#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +00002131#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002133 vector& operator=(initializer_list<value_type> __il)
2134 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00002135#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002136
2137 template <class _InputIterator>
2138 typename enable_if
2139 <
2140 __is_input_iterator<_InputIterator>::value &&
2141 !__is_forward_iterator<_InputIterator>::value,
2142 void
2143 >::type
2144 assign(_InputIterator __first, _InputIterator __last);
2145 template <class _ForwardIterator>
2146 typename enable_if
2147 <
2148 __is_forward_iterator<_ForwardIterator>::value,
2149 void
2150 >::type
2151 assign(_ForwardIterator __first, _ForwardIterator __last);
2152
2153 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +00002154#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002155 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002156 void assign(initializer_list<value_type> __il)
2157 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002158#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002159
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002160 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002161 {return allocator_type(this->__alloc());}
2162
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002163 size_type max_size() const _NOEXCEPT;
2164 _LIBCPP_INLINE_VISIBILITY
2165 size_type capacity() const _NOEXCEPT
2166 {return __internal_cap_to_external(__cap());}
2167 _LIBCPP_INLINE_VISIBILITY
2168 size_type size() const _NOEXCEPT
2169 {return __size_;}
2170 _LIBCPP_INLINE_VISIBILITY
2171 bool empty() const _NOEXCEPT
2172 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002173 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002174 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002175
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002176 _LIBCPP_INLINE_VISIBILITY
2177 iterator begin() _NOEXCEPT
2178 {return __make_iter(0);}
2179 _LIBCPP_INLINE_VISIBILITY
2180 const_iterator begin() const _NOEXCEPT
2181 {return __make_iter(0);}
2182 _LIBCPP_INLINE_VISIBILITY
2183 iterator end() _NOEXCEPT
2184 {return __make_iter(__size_);}
2185 _LIBCPP_INLINE_VISIBILITY
2186 const_iterator end() const _NOEXCEPT
2187 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002188
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002189 _LIBCPP_INLINE_VISIBILITY
2190 reverse_iterator rbegin() _NOEXCEPT
2191 {return reverse_iterator(end());}
2192 _LIBCPP_INLINE_VISIBILITY
2193 const_reverse_iterator rbegin() const _NOEXCEPT
2194 {return const_reverse_iterator(end());}
2195 _LIBCPP_INLINE_VISIBILITY
2196 reverse_iterator rend() _NOEXCEPT
2197 {return reverse_iterator(begin());}
2198 _LIBCPP_INLINE_VISIBILITY
2199 const_reverse_iterator rend() const _NOEXCEPT
2200 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002201
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002202 _LIBCPP_INLINE_VISIBILITY
2203 const_iterator cbegin() const _NOEXCEPT
2204 {return __make_iter(0);}
2205 _LIBCPP_INLINE_VISIBILITY
2206 const_iterator cend() const _NOEXCEPT
2207 {return __make_iter(__size_);}
2208 _LIBCPP_INLINE_VISIBILITY
2209 const_reverse_iterator crbegin() const _NOEXCEPT
2210 {return rbegin();}
2211 _LIBCPP_INLINE_VISIBILITY
2212 const_reverse_iterator crend() const _NOEXCEPT
2213 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002214
2215 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2216 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2217 reference at(size_type __n);
2218 const_reference at(size_type __n) const;
2219
2220 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2221 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2222 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2223 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2224
2225 void push_back(const value_type& __x);
Marshall Clow198a2a52013-08-13 23:54:12 +00002226#if _LIBCPP_STD_VER > 11
2227 template <class... _Args>
2228 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2229 { push_back ( value_type ( _VSTD::forward<_Args>(__args)... )); }
2230#endif
2231
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002232 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2233
Marshall Clow198a2a52013-08-13 23:54:12 +00002234#if _LIBCPP_STD_VER > 11
2235 template <class... _Args>
2236 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2237 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2238#endif
2239
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002240 iterator insert(const_iterator __position, const value_type& __x);
2241 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2242 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2243 template <class _InputIterator>
2244 typename enable_if
2245 <
2246 __is_input_iterator <_InputIterator>::value &&
2247 !__is_forward_iterator<_InputIterator>::value,
2248 iterator
2249 >::type
2250 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2251 template <class _ForwardIterator>
2252 typename enable_if
2253 <
2254 __is_forward_iterator<_ForwardIterator>::value,
2255 iterator
2256 >::type
2257 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00002258#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002260 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2261 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002262#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002263
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002264 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002265 iterator erase(const_iterator __first, const_iterator __last);
2266
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002267 _LIBCPP_INLINE_VISIBILITY
2268 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002269
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002270 void swap(vector&)
2271 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2272 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002273
2274 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002275 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002276
2277 bool __invariants() const;
2278
2279private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002280 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002281 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002282 void deallocate() _NOEXCEPT;
2283 _LIBCPP_INLINE_VISIBILITY
2284 static size_type __align(size_type __new_size) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002285 {return __new_size + (__bits_per_word-1) & ~(__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002286 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2287 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002288 template <class _ForwardIterator>
2289 typename enable_if
2290 <
2291 __is_forward_iterator<_ForwardIterator>::value,
2292 void
2293 >::type
2294 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2295 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002296 _LIBCPP_INLINE_VISIBILITY
2297 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002298 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002299 _LIBCPP_INLINE_VISIBILITY
2300 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002301 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
2302#ifdef _LIBCPP_DEBUG
2303 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_type __pos)
2304 {return iterator(this, pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2305 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_type __pos) const
2306 {return const_iterator(this, const_pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2307 _LIBCPP_INLINE_VISIBILITY iterator __const_iterator_cast(const_iterator __p)
2308 {return iterator(this, pointer(const_cast<__storage_pointer>(__p.base().__seg_), __p.base().__ctz_));}
Howard Hinnant324bb032010-08-22 00:02:43 +00002309#else // _LIBCPP_DEBUG
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002310 _LIBCPP_INLINE_VISIBILITY
2311 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002312 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002313 _LIBCPP_INLINE_VISIBILITY
2314 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002315 {return const_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 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002318 {return begin() + (__p - cbegin());}
Howard Hinnant324bb032010-08-22 00:02:43 +00002319#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002320
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002322 void __copy_assign_alloc(const vector& __v)
2323 {__copy_assign_alloc(__v, integral_constant<bool,
2324 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002326 void __copy_assign_alloc(const vector& __c, true_type)
2327 {
2328 if (__alloc() != __c.__alloc())
2329 deallocate();
2330 __alloc() = __c.__alloc();
2331 }
2332
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002334 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002335 {}
2336
2337 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002338 void __move_assign(vector& __c, true_type)
2339 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002341 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002342 _NOEXCEPT_(
2343 !__storage_traits::propagate_on_container_move_assignment::value ||
2344 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002345 {__move_assign_alloc(__c, integral_constant<bool,
2346 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002347 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00002348 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002349 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002350 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002351 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002352 }
2353
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002354 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002355 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002356 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002357 {}
2358
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002360 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002361 _NOEXCEPT_(
2362 !__storage_traits::propagate_on_container_swap::value ||
2363 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002364 {__swap_alloc(__x, __y, integral_constant<bool,
2365 __storage_traits::propagate_on_container_swap::value>());}
2366
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002367 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002368 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002369 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002370 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002371 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002372 swap(__x, __y);
2373 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002375 static void __swap_alloc(__storage_allocator&, __storage_allocator&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002376 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002377 {}
2378
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002379 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002380
2381 friend class __bit_reference<vector>;
2382 friend class __bit_const_reference<vector>;
2383 friend class __bit_iterator<vector, false>;
2384 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:18 +00002385 friend struct __bit_array<vector>;
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002386 friend struct _LIBCPP_TYPE_VIS_ONLY hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002387};
2388
2389template <class _Allocator>
2390#ifndef _LIBCPP_DEBUG
2391_LIBCPP_INLINE_VISIBILITY inline
2392#endif
2393void
2394vector<bool, _Allocator>::__invalidate_all_iterators()
2395{
2396#ifdef _LIBCPP_DEBUG
2397 iterator::__remove_all(this);
2398 const_iterator::__remove_all(this);
Howard Hinnant324bb032010-08-22 00:02:43 +00002399#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002400}
2401
2402// Allocate space for __n objects
2403// throws length_error if __n > max_size()
2404// throws (probably bad_alloc) if memory run out
2405// Precondition: __begin_ == __end_ == __cap() == 0
2406// Precondition: __n > 0
2407// Postcondition: capacity() == __n
2408// Postcondition: size() == 0
2409template <class _Allocator>
2410void
2411vector<bool, _Allocator>::allocate(size_type __n)
2412{
2413 if (__n > max_size())
2414 this->__throw_length_error();
2415 __n = __external_cap_to_internal(__n);
2416 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2417 this->__size_ = 0;
2418 this->__cap() = __n;
2419}
2420
2421template <class _Allocator>
2422void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002423vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002424{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002425 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002426 {
2427 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2428 __invalidate_all_iterators();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002429 this->__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002430 this->__size_ = this->__cap() = 0;
2431 }
2432}
2433
2434template <class _Allocator>
2435typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002436vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002437{
2438 size_type __amax = __storage_traits::max_size(__alloc());
2439 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2440 if (__nmax / __bits_per_word <= __amax)
2441 return __nmax;
2442 return __internal_cap_to_external(__amax);
2443}
2444
2445// Precondition: __new_size > capacity()
2446template <class _Allocator>
2447_LIBCPP_INLINE_VISIBILITY inline
2448typename vector<bool, _Allocator>::size_type
2449vector<bool, _Allocator>::__recommend(size_type __new_size) const
2450{
2451 const size_type __ms = max_size();
2452 if (__new_size > __ms)
2453 this->__throw_length_error();
2454 const size_type __cap = capacity();
2455 if (__cap >= __ms / 2)
2456 return __ms;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002457 return _VSTD::max(2*__cap, __align(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002458}
2459
2460// Default constructs __n objects starting at __end_
2461// Precondition: __n > 0
2462// Precondition: size() + __n <= capacity()
2463// Postcondition: size() == size() + __n
2464template <class _Allocator>
2465_LIBCPP_INLINE_VISIBILITY inline
2466void
2467vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2468{
2469 size_type __old_size = this->__size_;
2470 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002471 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002472}
2473
2474template <class _Allocator>
2475template <class _ForwardIterator>
2476typename enable_if
2477<
2478 __is_forward_iterator<_ForwardIterator>::value,
2479 void
2480>::type
2481vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2482{
2483 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002484 this->__size_ += _VSTD::distance(__first, __last);
2485 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002486}
2487
2488template <class _Allocator>
2489_LIBCPP_INLINE_VISIBILITY inline
2490vector<bool, _Allocator>::vector()
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002491 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002492 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002493 __size_(0),
2494 __cap_alloc_(0)
2495{
2496}
2497
2498template <class _Allocator>
2499_LIBCPP_INLINE_VISIBILITY inline
2500vector<bool, _Allocator>::vector(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002501 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002502 __size_(0),
2503 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2504{
2505}
2506
2507template <class _Allocator>
2508vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002509 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002510 __size_(0),
2511 __cap_alloc_(0)
2512{
2513 if (__n > 0)
2514 {
2515 allocate(__n);
2516 __construct_at_end(__n, false);
2517 }
2518}
2519
2520template <class _Allocator>
2521vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002522 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002523 __size_(0),
2524 __cap_alloc_(0)
2525{
2526 if (__n > 0)
2527 {
2528 allocate(__n);
2529 __construct_at_end(__n, __x);
2530 }
2531}
2532
2533template <class _Allocator>
2534vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002535 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002536 __size_(0),
2537 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2538{
2539 if (__n > 0)
2540 {
2541 allocate(__n);
2542 __construct_at_end(__n, __x);
2543 }
2544}
2545
2546template <class _Allocator>
2547template <class _InputIterator>
2548vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2549 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2550 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002551 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002552 __size_(0),
2553 __cap_alloc_(0)
2554{
2555#ifndef _LIBCPP_NO_EXCEPTIONS
2556 try
2557 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002558#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002559 for (; __first != __last; ++__first)
2560 push_back(*__first);
2561#ifndef _LIBCPP_NO_EXCEPTIONS
2562 }
2563 catch (...)
2564 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002565 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002566 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2567 __invalidate_all_iterators();
2568 throw;
2569 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002570#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002571}
2572
2573template <class _Allocator>
2574template <class _InputIterator>
2575vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2576 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2577 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002578 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002579 __size_(0),
2580 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2581{
2582#ifndef _LIBCPP_NO_EXCEPTIONS
2583 try
2584 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002585#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002586 for (; __first != __last; ++__first)
2587 push_back(*__first);
2588#ifndef _LIBCPP_NO_EXCEPTIONS
2589 }
2590 catch (...)
2591 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002592 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002593 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2594 __invalidate_all_iterators();
2595 throw;
2596 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002597#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002598}
2599
2600template <class _Allocator>
2601template <class _ForwardIterator>
2602vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2603 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002604 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002605 __size_(0),
2606 __cap_alloc_(0)
2607{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002608 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002609 if (__n > 0)
2610 {
2611 allocate(__n);
2612 __construct_at_end(__first, __last);
2613 }
2614}
2615
2616template <class _Allocator>
2617template <class _ForwardIterator>
2618vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2619 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002620 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002621 __size_(0),
2622 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2623{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002624 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002625 if (__n > 0)
2626 {
2627 allocate(__n);
2628 __construct_at_end(__first, __last);
2629 }
2630}
2631
Howard Hinnante3e32912011-08-12 21:56:02 +00002632#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2633
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002634template <class _Allocator>
2635vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002636 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002637 __size_(0),
2638 __cap_alloc_(0)
2639{
2640 size_type __n = static_cast<size_type>(__il.size());
2641 if (__n > 0)
2642 {
2643 allocate(__n);
2644 __construct_at_end(__il.begin(), __il.end());
2645 }
2646}
2647
2648template <class _Allocator>
2649vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002650 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002651 __size_(0),
2652 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2653{
2654 size_type __n = static_cast<size_type>(__il.size());
2655 if (__n > 0)
2656 {
2657 allocate(__n);
2658 __construct_at_end(__il.begin(), __il.end());
2659 }
2660}
2661
Howard Hinnante3e32912011-08-12 21:56:02 +00002662#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2663
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002664template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002665vector<bool, _Allocator>::~vector()
2666{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002667 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002668 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2669#ifdef _LIBCPP_DEBUG
2670 __invalidate_all_iterators();
2671#endif
2672}
2673
2674template <class _Allocator>
2675vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002676 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002677 __size_(0),
2678 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2679{
2680 if (__v.size() > 0)
2681 {
2682 allocate(__v.size());
2683 __construct_at_end(__v.begin(), __v.end());
2684 }
2685}
2686
2687template <class _Allocator>
2688vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002689 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002690 __size_(0),
2691 __cap_alloc_(0, __a)
2692{
2693 if (__v.size() > 0)
2694 {
2695 allocate(__v.size());
2696 __construct_at_end(__v.begin(), __v.end());
2697 }
2698}
2699
2700template <class _Allocator>
2701vector<bool, _Allocator>&
2702vector<bool, _Allocator>::operator=(const vector& __v)
2703{
2704 if (this != &__v)
2705 {
2706 __copy_assign_alloc(__v);
2707 if (__v.__size_)
2708 {
2709 if (__v.__size_ > capacity())
2710 {
2711 deallocate();
2712 allocate(__v.__size_);
2713 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002714 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002715 }
2716 __size_ = __v.__size_;
2717 }
2718 return *this;
2719}
2720
Howard Hinnant73d21a42010-09-04 23:28:19 +00002721#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2722
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002723template <class _Allocator>
2724_LIBCPP_INLINE_VISIBILITY inline
2725vector<bool, _Allocator>::vector(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002726 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002727 : __begin_(__v.__begin_),
2728 __size_(__v.__size_),
2729 __cap_alloc_(__v.__cap_alloc_)
2730{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002731 __v.__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002732 __v.__size_ = 0;
2733 __v.__cap() = 0;
2734}
2735
2736template <class _Allocator>
2737vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002738 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002739 __size_(0),
2740 __cap_alloc_(0, __a)
2741{
2742 if (__a == allocator_type(__v.__alloc()))
2743 {
2744 this->__begin_ = __v.__begin_;
2745 this->__size_ = __v.__size_;
2746 this->__cap() = __v.__cap();
2747 __v.__begin_ = nullptr;
2748 __v.__cap() = __v.__size_ = 0;
2749 }
2750 else if (__v.size() > 0)
2751 {
2752 allocate(__v.size());
2753 __construct_at_end(__v.begin(), __v.end());
2754 }
2755}
2756
2757template <class _Allocator>
2758_LIBCPP_INLINE_VISIBILITY inline
2759vector<bool, _Allocator>&
2760vector<bool, _Allocator>::operator=(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002761 _NOEXCEPT_(
2762 __alloc_traits::propagate_on_container_move_assignment::value &&
2763 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002764{
2765 __move_assign(__v, integral_constant<bool,
2766 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002767 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002768}
2769
2770template <class _Allocator>
2771void
2772vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2773{
2774 if (__alloc() != __c.__alloc())
2775 assign(__c.begin(), __c.end());
2776 else
2777 __move_assign(__c, true_type());
2778}
2779
2780template <class _Allocator>
2781void
2782vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002783 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002784{
2785 deallocate();
2786 this->__begin_ = __c.__begin_;
2787 this->__size_ = __c.__size_;
2788 this->__cap() = __c.__cap();
2789 __move_assign_alloc(__c);
2790 __c.__begin_ = nullptr;
2791 __c.__cap() = __c.__size_ = 0;
2792}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002793
2794#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002795
2796template <class _Allocator>
2797void
2798vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2799{
2800 __size_ = 0;
2801 if (__n > 0)
2802 {
2803 size_type __c = capacity();
2804 if (__n <= __c)
2805 __size_ = __n;
2806 else
2807 {
2808 vector __v(__alloc());
2809 __v.reserve(__recommend(__n));
2810 __v.__size_ = __n;
2811 swap(__v);
2812 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002813 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002814 }
2815}
2816
2817template <class _Allocator>
2818template <class _InputIterator>
2819typename enable_if
2820<
2821 __is_input_iterator<_InputIterator>::value &&
2822 !__is_forward_iterator<_InputIterator>::value,
2823 void
2824>::type
2825vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2826{
2827 clear();
2828 for (; __first != __last; ++__first)
2829 push_back(*__first);
2830}
2831
2832template <class _Allocator>
2833template <class _ForwardIterator>
2834typename enable_if
2835<
2836 __is_forward_iterator<_ForwardIterator>::value,
2837 void
2838>::type
2839vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2840{
2841 clear();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002842 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002843 if (__n)
2844 {
2845 if (__n > capacity())
2846 {
2847 deallocate();
2848 allocate(__n);
2849 }
2850 __construct_at_end(__first, __last);
2851 }
2852}
2853
2854template <class _Allocator>
2855void
2856vector<bool, _Allocator>::reserve(size_type __n)
2857{
2858 if (__n > capacity())
2859 {
2860 vector __v(this->__alloc());
2861 __v.allocate(__n);
2862 __v.__construct_at_end(this->begin(), this->end());
2863 swap(__v);
2864 __invalidate_all_iterators();
2865 }
2866}
2867
2868template <class _Allocator>
2869void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002870vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002871{
2872 if (__external_cap_to_internal(size()) > __cap())
2873 {
2874#ifndef _LIBCPP_NO_EXCEPTIONS
2875 try
2876 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002877#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002878 vector(*this, allocator_type(__alloc())).swap(*this);
2879#ifndef _LIBCPP_NO_EXCEPTIONS
2880 }
2881 catch (...)
2882 {
2883 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002884#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002885 }
2886}
2887
2888template <class _Allocator>
2889typename vector<bool, _Allocator>::reference
2890vector<bool, _Allocator>::at(size_type __n)
2891{
2892 if (__n >= size())
2893 this->__throw_out_of_range();
2894 return (*this)[__n];
2895}
2896
2897template <class _Allocator>
2898typename vector<bool, _Allocator>::const_reference
2899vector<bool, _Allocator>::at(size_type __n) const
2900{
2901 if (__n >= size())
2902 this->__throw_out_of_range();
2903 return (*this)[__n];
2904}
2905
2906template <class _Allocator>
2907void
2908vector<bool, _Allocator>::push_back(const value_type& __x)
2909{
2910 if (this->__size_ == this->capacity())
2911 reserve(__recommend(this->__size_ + 1));
2912 ++this->__size_;
2913 back() = __x;
2914}
2915
2916template <class _Allocator>
2917typename vector<bool, _Allocator>::iterator
2918vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2919{
2920 iterator __r;
2921 if (size() < capacity())
2922 {
2923 const_iterator __old_end = end();
2924 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002925 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002926 __r = __const_iterator_cast(__position);
2927 }
2928 else
2929 {
2930 vector __v(__alloc());
2931 __v.reserve(__recommend(__size_ + 1));
2932 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002933 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2934 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002935 swap(__v);
2936 }
2937 *__r = __x;
2938 return __r;
2939}
2940
2941template <class _Allocator>
2942typename vector<bool, _Allocator>::iterator
2943vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
2944{
2945 iterator __r;
2946 size_type __c = capacity();
2947 if (__n <= __c && size() <= __c - __n)
2948 {
2949 const_iterator __old_end = end();
2950 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002951 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002952 __r = __const_iterator_cast(__position);
2953 }
2954 else
2955 {
2956 vector __v(__alloc());
2957 __v.reserve(__recommend(__size_ + __n));
2958 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002959 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2960 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002961 swap(__v);
2962 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002963 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002964 return __r;
2965}
2966
2967template <class _Allocator>
2968template <class _InputIterator>
2969typename enable_if
2970<
2971 __is_input_iterator <_InputIterator>::value &&
2972 !__is_forward_iterator<_InputIterator>::value,
2973 typename vector<bool, _Allocator>::iterator
2974>::type
2975vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
2976{
2977 difference_type __off = __position - begin();
2978 iterator __p = __const_iterator_cast(__position);
2979 iterator __old_end = end();
2980 for (; size() != capacity() && __first != __last; ++__first)
2981 {
2982 ++this->__size_;
2983 back() = *__first;
2984 }
2985 vector __v(__alloc());
2986 if (__first != __last)
2987 {
2988#ifndef _LIBCPP_NO_EXCEPTIONS
2989 try
2990 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002991#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002992 __v.assign(__first, __last);
2993 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
2994 difference_type __old_p = __p - begin();
2995 reserve(__recommend(size() + __v.size()));
2996 __p = begin() + __old_p;
2997 __old_end = begin() + __old_size;
2998#ifndef _LIBCPP_NO_EXCEPTIONS
2999 }
3000 catch (...)
3001 {
3002 erase(__old_end, end());
3003 throw;
3004 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003005#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003006 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003007 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003008 insert(__p, __v.begin(), __v.end());
3009 return begin() + __off;
3010}
3011
3012template <class _Allocator>
3013template <class _ForwardIterator>
3014typename enable_if
3015<
3016 __is_forward_iterator<_ForwardIterator>::value,
3017 typename vector<bool, _Allocator>::iterator
3018>::type
3019vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3020{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003021 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003022 iterator __r;
3023 size_type __c = capacity();
3024 if (__n <= __c && size() <= __c - __n)
3025 {
3026 const_iterator __old_end = end();
3027 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003028 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003029 __r = __const_iterator_cast(__position);
3030 }
3031 else
3032 {
3033 vector __v(__alloc());
3034 __v.reserve(__recommend(__size_ + __n));
3035 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003036 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3037 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003038 swap(__v);
3039 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003040 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003041 return __r;
3042}
3043
3044template <class _Allocator>
3045_LIBCPP_INLINE_VISIBILITY inline
3046typename vector<bool, _Allocator>::iterator
3047vector<bool, _Allocator>::erase(const_iterator __position)
3048{
3049 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003050 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003051 --__size_;
3052 return __r;
3053}
3054
3055template <class _Allocator>
3056typename vector<bool, _Allocator>::iterator
3057vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3058{
3059 iterator __r = __const_iterator_cast(__first);
3060 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003061 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003062 __size_ -= __d;
3063 return __r;
3064}
3065
3066template <class _Allocator>
3067void
3068vector<bool, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003069 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3070 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003071{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003072 _VSTD::swap(this->__begin_, __x.__begin_);
3073 _VSTD::swap(this->__size_, __x.__size_);
3074 _VSTD::swap(this->__cap(), __x.__cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003075 __swap_alloc(this->__alloc(), __x.__alloc());
3076#ifdef _LIBCPP_DEBUG
3077 iterator::swap(this, &__x);
3078 const_iterator::swap(this, &__x);
Howard Hinnant324bb032010-08-22 00:02:43 +00003079#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003080}
3081
Howard Hinnant324bb032010-08-22 00:02:43 +00003082template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003083void
3084vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3085{
3086 size_type __cs = size();
3087 if (__cs < __sz)
3088 {
3089 iterator __r;
3090 size_type __c = capacity();
3091 size_type __n = __sz - __cs;
3092 if (__n <= __c && __cs <= __c - __n)
3093 {
3094 __r = end();
3095 __size_ += __n;
3096 }
3097 else
3098 {
3099 vector __v(__alloc());
3100 __v.reserve(__recommend(__size_ + __n));
3101 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003102 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003103 swap(__v);
3104 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003105 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003106 }
3107 else
3108 __size_ = __sz;
3109}
3110
Howard Hinnant324bb032010-08-22 00:02:43 +00003111template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003112void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003113vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003114{
3115 // do middle whole words
3116 size_type __n = __size_;
3117 __storage_pointer __p = __begin_;
3118 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3119 *__p = ~*__p;
3120 // do last partial word
3121 if (__n > 0)
3122 {
3123 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3124 __storage_type __b = *__p & __m;
3125 *__p &= ~__m;
3126 *__p |= ~__b & __m;
3127 }
3128}
3129
Howard Hinnant324bb032010-08-22 00:02:43 +00003130template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003131bool
3132vector<bool, _Allocator>::__invariants() const
3133{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00003134 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003135 {
3136 if (this->__size_ != 0 || this->__cap() != 0)
3137 return false;
3138 }
3139 else
3140 {
3141 if (this->__cap() == 0)
3142 return false;
3143 if (this->__size_ > this->capacity())
3144 return false;
3145 }
3146 return true;
3147}
3148
Howard Hinnant324bb032010-08-22 00:02:43 +00003149template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003150size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003151vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003152{
3153 size_t __h = 0;
3154 // do middle whole words
3155 size_type __n = __size_;
3156 __storage_pointer __p = __begin_;
3157 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3158 __h ^= *__p;
3159 // do last partial word
3160 if (__n > 0)
3161 {
3162 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3163 __h ^= *__p & __m;
3164 }
3165 return __h;
3166}
3167
3168template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003169struct _LIBCPP_TYPE_VIS_ONLY hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003170 : public unary_function<vector<bool, _Allocator>, size_t>
3171{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00003172 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003173 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003174 {return __vec.__hash_code();}
3175};
3176
3177template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003178_LIBCPP_INLINE_VISIBILITY inline
3179bool
3180operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3181{
3182 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003183 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003184}
3185
3186template <class _Tp, class _Allocator>
3187_LIBCPP_INLINE_VISIBILITY inline
3188bool
3189operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3190{
3191 return !(__x == __y);
3192}
3193
3194template <class _Tp, class _Allocator>
3195_LIBCPP_INLINE_VISIBILITY inline
3196bool
3197operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3198{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003199 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003200}
3201
3202template <class _Tp, class _Allocator>
3203_LIBCPP_INLINE_VISIBILITY inline
3204bool
3205operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3206{
3207 return __y < __x;
3208}
3209
3210template <class _Tp, class _Allocator>
3211_LIBCPP_INLINE_VISIBILITY inline
3212bool
3213operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3214{
3215 return !(__x < __y);
3216}
3217
3218template <class _Tp, class _Allocator>
3219_LIBCPP_INLINE_VISIBILITY inline
3220bool
3221operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3222{
3223 return !(__y < __x);
3224}
3225
3226template <class _Tp, class _Allocator>
3227_LIBCPP_INLINE_VISIBILITY inline
3228void
3229swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003230 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003231{
3232 __x.swap(__y);
3233}
3234
3235_LIBCPP_END_NAMESPACE_STD
3236
3237#endif // _LIBCPP_VECTOR