blob: e4fc024da52cc06f46badf43775ad1585dd4624c [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 Hinnant5e571422013-08-23 20:10:18 +0000277#ifdef _LIBCPP_DEBUG
Howard Hinnant8b00e6c2013-08-02 00:26:35 +0000278# 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;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002029 typedef pointer iterator;
2030 typedef const_pointer const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002031 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2032 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002033
2034private:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035 typedef typename __alloc_traits::template
2036#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
2037 rebind_alloc<__storage_type>
2038#else
2039 rebind_alloc<__storage_type>::other
2040#endif
2041 __storage_allocator;
2042 typedef allocator_traits<__storage_allocator> __storage_traits;
2043 typedef typename __storage_traits::pointer __storage_pointer;
2044 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2045
2046 __storage_pointer __begin_;
2047 size_type __size_;
2048 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002049public:
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002050 typedef __bit_reference<vector> reference;
2051 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002052private:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002053 _LIBCPP_INLINE_VISIBILITY
2054 size_type& __cap() _NOEXCEPT
2055 {return __cap_alloc_.first();}
2056 _LIBCPP_INLINE_VISIBILITY
2057 const size_type& __cap() const _NOEXCEPT
2058 {return __cap_alloc_.first();}
2059 _LIBCPP_INLINE_VISIBILITY
2060 __storage_allocator& __alloc() _NOEXCEPT
2061 {return __cap_alloc_.second();}
2062 _LIBCPP_INLINE_VISIBILITY
2063 const __storage_allocator& __alloc() const _NOEXCEPT
2064 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002065
2066 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2067
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002068 _LIBCPP_INLINE_VISIBILITY
2069 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002071 _LIBCPP_INLINE_VISIBILITY
2072 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002073 {return (__n - 1) / __bits_per_word + 1;}
2074
2075public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002076 _LIBCPP_INLINE_VISIBILITY
2077 vector()
2078 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002079 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002080 ~vector();
2081 explicit vector(size_type __n);
2082 vector(size_type __n, const value_type& __v);
2083 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2084 template <class _InputIterator>
2085 vector(_InputIterator __first, _InputIterator __last,
2086 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2087 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2088 template <class _InputIterator>
2089 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2090 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2091 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2092 template <class _ForwardIterator>
2093 vector(_ForwardIterator __first, _ForwardIterator __last,
2094 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2095 template <class _ForwardIterator>
2096 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2097 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2098
2099 vector(const vector& __v);
2100 vector(const vector& __v, const allocator_type& __a);
2101 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00002102#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002103 vector(initializer_list<value_type> __il);
2104 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00002105#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002106
Howard Hinnant73d21a42010-09-04 23:28:19 +00002107#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002108 _LIBCPP_INLINE_VISIBILITY
2109 vector(vector&& __v)
2110 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002111 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002112 _LIBCPP_INLINE_VISIBILITY
2113 vector& operator=(vector&& __v)
2114 _NOEXCEPT_(
2115 __alloc_traits::propagate_on_container_move_assignment::value &&
2116 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002117#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +00002118#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002119 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002120 vector& operator=(initializer_list<value_type> __il)
2121 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00002122#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002123
2124 template <class _InputIterator>
2125 typename enable_if
2126 <
2127 __is_input_iterator<_InputIterator>::value &&
2128 !__is_forward_iterator<_InputIterator>::value,
2129 void
2130 >::type
2131 assign(_InputIterator __first, _InputIterator __last);
2132 template <class _ForwardIterator>
2133 typename enable_if
2134 <
2135 __is_forward_iterator<_ForwardIterator>::value,
2136 void
2137 >::type
2138 assign(_ForwardIterator __first, _ForwardIterator __last);
2139
2140 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +00002141#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002143 void assign(initializer_list<value_type> __il)
2144 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002145#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002146
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002147 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002148 {return allocator_type(this->__alloc());}
2149
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002150 size_type max_size() const _NOEXCEPT;
2151 _LIBCPP_INLINE_VISIBILITY
2152 size_type capacity() const _NOEXCEPT
2153 {return __internal_cap_to_external(__cap());}
2154 _LIBCPP_INLINE_VISIBILITY
2155 size_type size() const _NOEXCEPT
2156 {return __size_;}
2157 _LIBCPP_INLINE_VISIBILITY
2158 bool empty() const _NOEXCEPT
2159 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002160 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002161 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002162
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002163 _LIBCPP_INLINE_VISIBILITY
2164 iterator begin() _NOEXCEPT
2165 {return __make_iter(0);}
2166 _LIBCPP_INLINE_VISIBILITY
2167 const_iterator begin() const _NOEXCEPT
2168 {return __make_iter(0);}
2169 _LIBCPP_INLINE_VISIBILITY
2170 iterator end() _NOEXCEPT
2171 {return __make_iter(__size_);}
2172 _LIBCPP_INLINE_VISIBILITY
2173 const_iterator end() const _NOEXCEPT
2174 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002175
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002176 _LIBCPP_INLINE_VISIBILITY
2177 reverse_iterator rbegin() _NOEXCEPT
2178 {return reverse_iterator(end());}
2179 _LIBCPP_INLINE_VISIBILITY
2180 const_reverse_iterator rbegin() const _NOEXCEPT
2181 {return const_reverse_iterator(end());}
2182 _LIBCPP_INLINE_VISIBILITY
2183 reverse_iterator rend() _NOEXCEPT
2184 {return reverse_iterator(begin());}
2185 _LIBCPP_INLINE_VISIBILITY
2186 const_reverse_iterator rend() const _NOEXCEPT
2187 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002188
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002189 _LIBCPP_INLINE_VISIBILITY
2190 const_iterator cbegin() const _NOEXCEPT
2191 {return __make_iter(0);}
2192 _LIBCPP_INLINE_VISIBILITY
2193 const_iterator cend() const _NOEXCEPT
2194 {return __make_iter(__size_);}
2195 _LIBCPP_INLINE_VISIBILITY
2196 const_reverse_iterator crbegin() const _NOEXCEPT
2197 {return rbegin();}
2198 _LIBCPP_INLINE_VISIBILITY
2199 const_reverse_iterator crend() const _NOEXCEPT
2200 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002201
2202 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2203 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2204 reference at(size_type __n);
2205 const_reference at(size_type __n) const;
2206
2207 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2208 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2209 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2210 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2211
2212 void push_back(const value_type& __x);
Marshall Clow198a2a52013-08-13 23:54:12 +00002213#if _LIBCPP_STD_VER > 11
2214 template <class... _Args>
2215 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2216 { push_back ( value_type ( _VSTD::forward<_Args>(__args)... )); }
2217#endif
2218
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002219 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2220
Marshall Clow198a2a52013-08-13 23:54:12 +00002221#if _LIBCPP_STD_VER > 11
2222 template <class... _Args>
2223 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2224 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2225#endif
2226
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002227 iterator insert(const_iterator __position, const value_type& __x);
2228 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2229 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2230 template <class _InputIterator>
2231 typename enable_if
2232 <
2233 __is_input_iterator <_InputIterator>::value &&
2234 !__is_forward_iterator<_InputIterator>::value,
2235 iterator
2236 >::type
2237 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2238 template <class _ForwardIterator>
2239 typename enable_if
2240 <
2241 __is_forward_iterator<_ForwardIterator>::value,
2242 iterator
2243 >::type
2244 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00002245#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002246 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002247 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2248 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002249#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002250
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002251 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002252 iterator erase(const_iterator __first, const_iterator __last);
2253
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002254 _LIBCPP_INLINE_VISIBILITY
2255 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002256
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002257 void swap(vector&)
2258 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2259 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002260
2261 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002262 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002263
2264 bool __invariants() const;
2265
2266private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002267 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002268 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002269 void deallocate() _NOEXCEPT;
2270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00002271 static size_type __align_it(size_type __new_size) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002272 {return __new_size + (__bits_per_word-1) & ~(__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002273 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2274 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002275 template <class _ForwardIterator>
2276 typename enable_if
2277 <
2278 __is_forward_iterator<_ForwardIterator>::value,
2279 void
2280 >::type
2281 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2282 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002283 _LIBCPP_INLINE_VISIBILITY
2284 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002285 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002286 _LIBCPP_INLINE_VISIBILITY
2287 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002288 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002289 _LIBCPP_INLINE_VISIBILITY
2290 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002291 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002292 _LIBCPP_INLINE_VISIBILITY
2293 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002294 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002295 _LIBCPP_INLINE_VISIBILITY
2296 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002297 {return begin() + (__p - cbegin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002298
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002300 void __copy_assign_alloc(const vector& __v)
2301 {__copy_assign_alloc(__v, integral_constant<bool,
2302 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002304 void __copy_assign_alloc(const vector& __c, true_type)
2305 {
2306 if (__alloc() != __c.__alloc())
2307 deallocate();
2308 __alloc() = __c.__alloc();
2309 }
2310
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002311 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002312 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002313 {}
2314
2315 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002316 void __move_assign(vector& __c, true_type)
2317 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002319 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002320 _NOEXCEPT_(
2321 !__storage_traits::propagate_on_container_move_assignment::value ||
2322 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002323 {__move_assign_alloc(__c, integral_constant<bool,
2324 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00002326 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002327 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002328 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002329 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002330 }
2331
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002333 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002334 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002335 {}
2336
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002338 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002339 _NOEXCEPT_(
2340 !__storage_traits::propagate_on_container_swap::value ||
2341 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002342 {__swap_alloc(__x, __y, integral_constant<bool,
2343 __storage_traits::propagate_on_container_swap::value>());}
2344
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002345 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002346 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002347 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002348 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002349 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002350 swap(__x, __y);
2351 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002352 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002353 static void __swap_alloc(__storage_allocator&, __storage_allocator&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002354 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002355 {}
2356
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002357 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002358
2359 friend class __bit_reference<vector>;
2360 friend class __bit_const_reference<vector>;
2361 friend class __bit_iterator<vector, false>;
2362 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:18 +00002363 friend struct __bit_array<vector>;
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002364 friend struct _LIBCPP_TYPE_VIS_ONLY hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002365};
2366
2367template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002368_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002369void
2370vector<bool, _Allocator>::__invalidate_all_iterators()
2371{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002372}
2373
2374// Allocate space for __n objects
2375// throws length_error if __n > max_size()
2376// throws (probably bad_alloc) if memory run out
2377// Precondition: __begin_ == __end_ == __cap() == 0
2378// Precondition: __n > 0
2379// Postcondition: capacity() == __n
2380// Postcondition: size() == 0
2381template <class _Allocator>
2382void
2383vector<bool, _Allocator>::allocate(size_type __n)
2384{
2385 if (__n > max_size())
2386 this->__throw_length_error();
2387 __n = __external_cap_to_internal(__n);
2388 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2389 this->__size_ = 0;
2390 this->__cap() = __n;
2391}
2392
2393template <class _Allocator>
2394void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002395vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002396{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002397 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002398 {
2399 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2400 __invalidate_all_iterators();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002401 this->__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002402 this->__size_ = this->__cap() = 0;
2403 }
2404}
2405
2406template <class _Allocator>
2407typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002408vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002409{
2410 size_type __amax = __storage_traits::max_size(__alloc());
2411 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2412 if (__nmax / __bits_per_word <= __amax)
2413 return __nmax;
2414 return __internal_cap_to_external(__amax);
2415}
2416
2417// Precondition: __new_size > capacity()
2418template <class _Allocator>
2419_LIBCPP_INLINE_VISIBILITY inline
2420typename vector<bool, _Allocator>::size_type
2421vector<bool, _Allocator>::__recommend(size_type __new_size) const
2422{
2423 const size_type __ms = max_size();
2424 if (__new_size > __ms)
2425 this->__throw_length_error();
2426 const size_type __cap = capacity();
2427 if (__cap >= __ms / 2)
2428 return __ms;
Howard Hinnant7f764502013-08-14 18:00:20 +00002429 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002430}
2431
2432// Default constructs __n objects starting at __end_
2433// Precondition: __n > 0
2434// Precondition: size() + __n <= capacity()
2435// Postcondition: size() == size() + __n
2436template <class _Allocator>
2437_LIBCPP_INLINE_VISIBILITY inline
2438void
2439vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2440{
2441 size_type __old_size = this->__size_;
2442 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002443 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002444}
2445
2446template <class _Allocator>
2447template <class _ForwardIterator>
2448typename enable_if
2449<
2450 __is_forward_iterator<_ForwardIterator>::value,
2451 void
2452>::type
2453vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2454{
2455 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002456 this->__size_ += _VSTD::distance(__first, __last);
2457 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002458}
2459
2460template <class _Allocator>
2461_LIBCPP_INLINE_VISIBILITY inline
2462vector<bool, _Allocator>::vector()
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002463 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002464 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002465 __size_(0),
2466 __cap_alloc_(0)
2467{
2468}
2469
2470template <class _Allocator>
2471_LIBCPP_INLINE_VISIBILITY inline
2472vector<bool, _Allocator>::vector(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002473 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002474 __size_(0),
2475 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2476{
2477}
2478
2479template <class _Allocator>
2480vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002481 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002482 __size_(0),
2483 __cap_alloc_(0)
2484{
2485 if (__n > 0)
2486 {
2487 allocate(__n);
2488 __construct_at_end(__n, false);
2489 }
2490}
2491
2492template <class _Allocator>
2493vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002494 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002495 __size_(0),
2496 __cap_alloc_(0)
2497{
2498 if (__n > 0)
2499 {
2500 allocate(__n);
2501 __construct_at_end(__n, __x);
2502 }
2503}
2504
2505template <class _Allocator>
2506vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002507 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002508 __size_(0),
2509 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2510{
2511 if (__n > 0)
2512 {
2513 allocate(__n);
2514 __construct_at_end(__n, __x);
2515 }
2516}
2517
2518template <class _Allocator>
2519template <class _InputIterator>
2520vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2521 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2522 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002523 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002524 __size_(0),
2525 __cap_alloc_(0)
2526{
2527#ifndef _LIBCPP_NO_EXCEPTIONS
2528 try
2529 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002530#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002531 for (; __first != __last; ++__first)
2532 push_back(*__first);
2533#ifndef _LIBCPP_NO_EXCEPTIONS
2534 }
2535 catch (...)
2536 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002537 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002538 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2539 __invalidate_all_iterators();
2540 throw;
2541 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002542#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002543}
2544
2545template <class _Allocator>
2546template <class _InputIterator>
2547vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2548 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2549 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002550 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002551 __size_(0),
2552 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2553{
2554#ifndef _LIBCPP_NO_EXCEPTIONS
2555 try
2556 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002557#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002558 for (; __first != __last; ++__first)
2559 push_back(*__first);
2560#ifndef _LIBCPP_NO_EXCEPTIONS
2561 }
2562 catch (...)
2563 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002564 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002565 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2566 __invalidate_all_iterators();
2567 throw;
2568 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002569#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002570}
2571
2572template <class _Allocator>
2573template <class _ForwardIterator>
2574vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2575 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002576 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002577 __size_(0),
2578 __cap_alloc_(0)
2579{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002580 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002581 if (__n > 0)
2582 {
2583 allocate(__n);
2584 __construct_at_end(__first, __last);
2585 }
2586}
2587
2588template <class _Allocator>
2589template <class _ForwardIterator>
2590vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2591 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002592 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002593 __size_(0),
2594 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2595{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002596 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002597 if (__n > 0)
2598 {
2599 allocate(__n);
2600 __construct_at_end(__first, __last);
2601 }
2602}
2603
Howard Hinnante3e32912011-08-12 21:56:02 +00002604#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2605
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002606template <class _Allocator>
2607vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002608 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002609 __size_(0),
2610 __cap_alloc_(0)
2611{
2612 size_type __n = static_cast<size_type>(__il.size());
2613 if (__n > 0)
2614 {
2615 allocate(__n);
2616 __construct_at_end(__il.begin(), __il.end());
2617 }
2618}
2619
2620template <class _Allocator>
2621vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002622 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002623 __size_(0),
2624 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2625{
2626 size_type __n = static_cast<size_type>(__il.size());
2627 if (__n > 0)
2628 {
2629 allocate(__n);
2630 __construct_at_end(__il.begin(), __il.end());
2631 }
2632}
2633
Howard Hinnante3e32912011-08-12 21:56:02 +00002634#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2635
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002636template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002637vector<bool, _Allocator>::~vector()
2638{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002639 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002640 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002641 __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002642}
2643
2644template <class _Allocator>
2645vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002646 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002647 __size_(0),
2648 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2649{
2650 if (__v.size() > 0)
2651 {
2652 allocate(__v.size());
2653 __construct_at_end(__v.begin(), __v.end());
2654 }
2655}
2656
2657template <class _Allocator>
2658vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002659 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002660 __size_(0),
2661 __cap_alloc_(0, __a)
2662{
2663 if (__v.size() > 0)
2664 {
2665 allocate(__v.size());
2666 __construct_at_end(__v.begin(), __v.end());
2667 }
2668}
2669
2670template <class _Allocator>
2671vector<bool, _Allocator>&
2672vector<bool, _Allocator>::operator=(const vector& __v)
2673{
2674 if (this != &__v)
2675 {
2676 __copy_assign_alloc(__v);
2677 if (__v.__size_)
2678 {
2679 if (__v.__size_ > capacity())
2680 {
2681 deallocate();
2682 allocate(__v.__size_);
2683 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002684 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002685 }
2686 __size_ = __v.__size_;
2687 }
2688 return *this;
2689}
2690
Howard Hinnant73d21a42010-09-04 23:28:19 +00002691#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2692
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002693template <class _Allocator>
2694_LIBCPP_INLINE_VISIBILITY inline
2695vector<bool, _Allocator>::vector(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002696 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002697 : __begin_(__v.__begin_),
2698 __size_(__v.__size_),
2699 __cap_alloc_(__v.__cap_alloc_)
2700{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002701 __v.__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002702 __v.__size_ = 0;
2703 __v.__cap() = 0;
2704}
2705
2706template <class _Allocator>
2707vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002708 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002709 __size_(0),
2710 __cap_alloc_(0, __a)
2711{
2712 if (__a == allocator_type(__v.__alloc()))
2713 {
2714 this->__begin_ = __v.__begin_;
2715 this->__size_ = __v.__size_;
2716 this->__cap() = __v.__cap();
2717 __v.__begin_ = nullptr;
2718 __v.__cap() = __v.__size_ = 0;
2719 }
2720 else if (__v.size() > 0)
2721 {
2722 allocate(__v.size());
2723 __construct_at_end(__v.begin(), __v.end());
2724 }
2725}
2726
2727template <class _Allocator>
2728_LIBCPP_INLINE_VISIBILITY inline
2729vector<bool, _Allocator>&
2730vector<bool, _Allocator>::operator=(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002731 _NOEXCEPT_(
2732 __alloc_traits::propagate_on_container_move_assignment::value &&
2733 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002734{
2735 __move_assign(__v, integral_constant<bool,
2736 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002737 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738}
2739
2740template <class _Allocator>
2741void
2742vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2743{
2744 if (__alloc() != __c.__alloc())
2745 assign(__c.begin(), __c.end());
2746 else
2747 __move_assign(__c, true_type());
2748}
2749
2750template <class _Allocator>
2751void
2752vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002753 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002754{
2755 deallocate();
2756 this->__begin_ = __c.__begin_;
2757 this->__size_ = __c.__size_;
2758 this->__cap() = __c.__cap();
2759 __move_assign_alloc(__c);
2760 __c.__begin_ = nullptr;
2761 __c.__cap() = __c.__size_ = 0;
2762}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002763
2764#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002765
2766template <class _Allocator>
2767void
2768vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2769{
2770 __size_ = 0;
2771 if (__n > 0)
2772 {
2773 size_type __c = capacity();
2774 if (__n <= __c)
2775 __size_ = __n;
2776 else
2777 {
2778 vector __v(__alloc());
2779 __v.reserve(__recommend(__n));
2780 __v.__size_ = __n;
2781 swap(__v);
2782 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002783 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002784 }
2785}
2786
2787template <class _Allocator>
2788template <class _InputIterator>
2789typename enable_if
2790<
2791 __is_input_iterator<_InputIterator>::value &&
2792 !__is_forward_iterator<_InputIterator>::value,
2793 void
2794>::type
2795vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2796{
2797 clear();
2798 for (; __first != __last; ++__first)
2799 push_back(*__first);
2800}
2801
2802template <class _Allocator>
2803template <class _ForwardIterator>
2804typename enable_if
2805<
2806 __is_forward_iterator<_ForwardIterator>::value,
2807 void
2808>::type
2809vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2810{
2811 clear();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002812 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002813 if (__n)
2814 {
2815 if (__n > capacity())
2816 {
2817 deallocate();
2818 allocate(__n);
2819 }
2820 __construct_at_end(__first, __last);
2821 }
2822}
2823
2824template <class _Allocator>
2825void
2826vector<bool, _Allocator>::reserve(size_type __n)
2827{
2828 if (__n > capacity())
2829 {
2830 vector __v(this->__alloc());
2831 __v.allocate(__n);
2832 __v.__construct_at_end(this->begin(), this->end());
2833 swap(__v);
2834 __invalidate_all_iterators();
2835 }
2836}
2837
2838template <class _Allocator>
2839void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002840vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002841{
2842 if (__external_cap_to_internal(size()) > __cap())
2843 {
2844#ifndef _LIBCPP_NO_EXCEPTIONS
2845 try
2846 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002847#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002848 vector(*this, allocator_type(__alloc())).swap(*this);
2849#ifndef _LIBCPP_NO_EXCEPTIONS
2850 }
2851 catch (...)
2852 {
2853 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002854#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002855 }
2856}
2857
2858template <class _Allocator>
2859typename vector<bool, _Allocator>::reference
2860vector<bool, _Allocator>::at(size_type __n)
2861{
2862 if (__n >= size())
2863 this->__throw_out_of_range();
2864 return (*this)[__n];
2865}
2866
2867template <class _Allocator>
2868typename vector<bool, _Allocator>::const_reference
2869vector<bool, _Allocator>::at(size_type __n) const
2870{
2871 if (__n >= size())
2872 this->__throw_out_of_range();
2873 return (*this)[__n];
2874}
2875
2876template <class _Allocator>
2877void
2878vector<bool, _Allocator>::push_back(const value_type& __x)
2879{
2880 if (this->__size_ == this->capacity())
2881 reserve(__recommend(this->__size_ + 1));
2882 ++this->__size_;
2883 back() = __x;
2884}
2885
2886template <class _Allocator>
2887typename vector<bool, _Allocator>::iterator
2888vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2889{
2890 iterator __r;
2891 if (size() < capacity())
2892 {
2893 const_iterator __old_end = end();
2894 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002895 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002896 __r = __const_iterator_cast(__position);
2897 }
2898 else
2899 {
2900 vector __v(__alloc());
2901 __v.reserve(__recommend(__size_ + 1));
2902 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002903 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2904 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002905 swap(__v);
2906 }
2907 *__r = __x;
2908 return __r;
2909}
2910
2911template <class _Allocator>
2912typename vector<bool, _Allocator>::iterator
2913vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
2914{
2915 iterator __r;
2916 size_type __c = capacity();
2917 if (__n <= __c && size() <= __c - __n)
2918 {
2919 const_iterator __old_end = end();
2920 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002921 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002922 __r = __const_iterator_cast(__position);
2923 }
2924 else
2925 {
2926 vector __v(__alloc());
2927 __v.reserve(__recommend(__size_ + __n));
2928 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002929 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2930 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002931 swap(__v);
2932 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002933 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002934 return __r;
2935}
2936
2937template <class _Allocator>
2938template <class _InputIterator>
2939typename enable_if
2940<
2941 __is_input_iterator <_InputIterator>::value &&
2942 !__is_forward_iterator<_InputIterator>::value,
2943 typename vector<bool, _Allocator>::iterator
2944>::type
2945vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
2946{
2947 difference_type __off = __position - begin();
2948 iterator __p = __const_iterator_cast(__position);
2949 iterator __old_end = end();
2950 for (; size() != capacity() && __first != __last; ++__first)
2951 {
2952 ++this->__size_;
2953 back() = *__first;
2954 }
2955 vector __v(__alloc());
2956 if (__first != __last)
2957 {
2958#ifndef _LIBCPP_NO_EXCEPTIONS
2959 try
2960 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002961#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002962 __v.assign(__first, __last);
2963 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
2964 difference_type __old_p = __p - begin();
2965 reserve(__recommend(size() + __v.size()));
2966 __p = begin() + __old_p;
2967 __old_end = begin() + __old_size;
2968#ifndef _LIBCPP_NO_EXCEPTIONS
2969 }
2970 catch (...)
2971 {
2972 erase(__old_end, end());
2973 throw;
2974 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002975#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002976 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002977 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002978 insert(__p, __v.begin(), __v.end());
2979 return begin() + __off;
2980}
2981
2982template <class _Allocator>
2983template <class _ForwardIterator>
2984typename enable_if
2985<
2986 __is_forward_iterator<_ForwardIterator>::value,
2987 typename vector<bool, _Allocator>::iterator
2988>::type
2989vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
2990{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002991 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002992 iterator __r;
2993 size_type __c = capacity();
2994 if (__n <= __c && size() <= __c - __n)
2995 {
2996 const_iterator __old_end = end();
2997 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002998 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002999 __r = __const_iterator_cast(__position);
3000 }
3001 else
3002 {
3003 vector __v(__alloc());
3004 __v.reserve(__recommend(__size_ + __n));
3005 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003006 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3007 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003008 swap(__v);
3009 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003010 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003011 return __r;
3012}
3013
3014template <class _Allocator>
3015_LIBCPP_INLINE_VISIBILITY inline
3016typename vector<bool, _Allocator>::iterator
3017vector<bool, _Allocator>::erase(const_iterator __position)
3018{
3019 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003020 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003021 --__size_;
3022 return __r;
3023}
3024
3025template <class _Allocator>
3026typename vector<bool, _Allocator>::iterator
3027vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3028{
3029 iterator __r = __const_iterator_cast(__first);
3030 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003031 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003032 __size_ -= __d;
3033 return __r;
3034}
3035
3036template <class _Allocator>
3037void
3038vector<bool, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003039 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3040 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003041{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003042 _VSTD::swap(this->__begin_, __x.__begin_);
3043 _VSTD::swap(this->__size_, __x.__size_);
3044 _VSTD::swap(this->__cap(), __x.__cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003045 __swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003046}
3047
Howard Hinnant324bb032010-08-22 00:02:43 +00003048template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003049void
3050vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3051{
3052 size_type __cs = size();
3053 if (__cs < __sz)
3054 {
3055 iterator __r;
3056 size_type __c = capacity();
3057 size_type __n = __sz - __cs;
3058 if (__n <= __c && __cs <= __c - __n)
3059 {
3060 __r = end();
3061 __size_ += __n;
3062 }
3063 else
3064 {
3065 vector __v(__alloc());
3066 __v.reserve(__recommend(__size_ + __n));
3067 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003068 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003069 swap(__v);
3070 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003071 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003072 }
3073 else
3074 __size_ = __sz;
3075}
3076
Howard Hinnant324bb032010-08-22 00:02:43 +00003077template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003078void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003079vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003080{
3081 // do middle whole words
3082 size_type __n = __size_;
3083 __storage_pointer __p = __begin_;
3084 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3085 *__p = ~*__p;
3086 // do last partial word
3087 if (__n > 0)
3088 {
3089 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3090 __storage_type __b = *__p & __m;
3091 *__p &= ~__m;
3092 *__p |= ~__b & __m;
3093 }
3094}
3095
Howard Hinnant324bb032010-08-22 00:02:43 +00003096template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003097bool
3098vector<bool, _Allocator>::__invariants() const
3099{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00003100 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003101 {
3102 if (this->__size_ != 0 || this->__cap() != 0)
3103 return false;
3104 }
3105 else
3106 {
3107 if (this->__cap() == 0)
3108 return false;
3109 if (this->__size_ > this->capacity())
3110 return false;
3111 }
3112 return true;
3113}
3114
Howard Hinnant324bb032010-08-22 00:02:43 +00003115template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003116size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003117vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003118{
3119 size_t __h = 0;
3120 // do middle whole words
3121 size_type __n = __size_;
3122 __storage_pointer __p = __begin_;
3123 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3124 __h ^= *__p;
3125 // do last partial word
3126 if (__n > 0)
3127 {
3128 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3129 __h ^= *__p & __m;
3130 }
3131 return __h;
3132}
3133
3134template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003135struct _LIBCPP_TYPE_VIS_ONLY hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003136 : public unary_function<vector<bool, _Allocator>, size_t>
3137{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00003138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003139 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003140 {return __vec.__hash_code();}
3141};
3142
3143template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003144_LIBCPP_INLINE_VISIBILITY inline
3145bool
3146operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3147{
3148 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003149 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003150}
3151
3152template <class _Tp, class _Allocator>
3153_LIBCPP_INLINE_VISIBILITY inline
3154bool
3155operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3156{
3157 return !(__x == __y);
3158}
3159
3160template <class _Tp, class _Allocator>
3161_LIBCPP_INLINE_VISIBILITY inline
3162bool
3163operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3164{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003165 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003166}
3167
3168template <class _Tp, class _Allocator>
3169_LIBCPP_INLINE_VISIBILITY inline
3170bool
3171operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3172{
3173 return __y < __x;
3174}
3175
3176template <class _Tp, class _Allocator>
3177_LIBCPP_INLINE_VISIBILITY inline
3178bool
3179operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3180{
3181 return !(__x < __y);
3182}
3183
3184template <class _Tp, class _Allocator>
3185_LIBCPP_INLINE_VISIBILITY inline
3186bool
3187operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3188{
3189 return !(__y < __x);
3190}
3191
3192template <class _Tp, class _Allocator>
3193_LIBCPP_INLINE_VISIBILITY inline
3194void
3195swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003196 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003197{
3198 __x.swap(__y);
3199}
3200
3201_LIBCPP_END_NAMESPACE_STD
3202
3203#endif // _LIBCPP_VECTOR