blob: e30ce7836318273fc5fe0b0ceafa7aafc9931278 [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 Hinnantbc8d3f92010-05-11 19:42:16 +0000163 explicit vector(const allocator_type& = allocator_type());
164 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);
219 void pop_back();
220
221 iterator insert(const_iterator position, const value_type& x);
222 iterator insert(const_iterator position, size_type n, const value_type& x);
223 template <class InputIterator>
224 iterator insert(const_iterator position, InputIterator first, InputIterator last);
225 iterator insert(const_iterator position, initializer_list<value_type> il);
226
227 iterator erase(const_iterator position);
228 iterator erase(const_iterator first, const_iterator last);
229
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000230 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000231
232 void resize(size_type sz);
233 void resize(size_type sz, value_type x);
234
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000235 void swap(vector&)
236 noexcept(!allocator_type::propagate_on_container_swap::value ||
237 __is_nothrow_swappable<allocator_type>::value);
238 void flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000239
240 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000241};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000242
243template <class Allocator> struct hash<std::vector<bool, Allocator>>;
244
245template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
246template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
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);
251
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000252template <class T, class Allocator>
253void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
254 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000255
256} // std
257
258*/
259
260#include <__config>
261#include <__bit_reference>
262#include <type_traits>
263#include <climits>
264#include <limits>
265#include <initializer_list>
266#include <memory>
267#include <stdexcept>
268#include <algorithm>
269#include <cstring>
270#include <__split_buffer>
271#include <__functional_base>
272#if defined(_LIBCPP_DEBUG) || defined(_LIBCPP_NO_EXCEPTIONS)
273 #include <cassert>
274#endif
275
276#pragma GCC system_header
277
278_LIBCPP_BEGIN_NAMESPACE_STD
279
280template <bool>
281class __vector_base_common
282{
283protected:
284 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
285 void __throw_length_error() const;
286 void __throw_out_of_range() const;
287};
288
289template <bool __b>
290void
291__vector_base_common<__b>::__throw_length_error() const
292{
293#ifndef _LIBCPP_NO_EXCEPTIONS
294 throw length_error("vector");
295#else
296 assert(!"vector length_error");
297#endif
298}
299
300template <bool __b>
301void
302__vector_base_common<__b>::__throw_out_of_range() const
303{
304#ifndef _LIBCPP_NO_EXCEPTIONS
305 throw out_of_range("vector");
306#else
307 assert(!"vector out_of_range");
308#endif
309}
310
311extern template class __vector_base_common<true>;
312
313template <class _Tp, class _Allocator>
314class __vector_base
315 : protected __vector_base_common<true>
316{
317protected:
Howard Hinnant324bb032010-08-22 00:02:43 +0000318 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000319 typedef _Allocator allocator_type;
320 typedef allocator_traits<allocator_type> __alloc_traits;
321 typedef value_type& reference;
322 typedef const value_type& const_reference;
323 typedef typename __alloc_traits::size_type size_type;
324 typedef typename __alloc_traits::difference_type difference_type;
325 typedef typename __alloc_traits::pointer pointer;
326 typedef typename __alloc_traits::const_pointer const_pointer;
327 typedef pointer iterator;
328 typedef const_pointer const_iterator;
329
330 pointer __begin_;
331 pointer __end_;
332 __compressed_pair<pointer, allocator_type> __end_cap_;
333
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000334 _LIBCPP_INLINE_VISIBILITY
335 allocator_type& __alloc() _NOEXCEPT
336 {return __end_cap_.second();}
337 _LIBCPP_INLINE_VISIBILITY
338 const allocator_type& __alloc() const _NOEXCEPT
339 {return __end_cap_.second();}
340 _LIBCPP_INLINE_VISIBILITY
341 pointer& __end_cap() _NOEXCEPT
342 {return __end_cap_.first();}
343 _LIBCPP_INLINE_VISIBILITY
344 const pointer& __end_cap() const _NOEXCEPT
345 {return __end_cap_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000346
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000347 _LIBCPP_INLINE_VISIBILITY
348 __vector_base()
349 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000350 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000351 ~__vector_base();
352
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000353 _LIBCPP_INLINE_VISIBILITY
354 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
355 _LIBCPP_INLINE_VISIBILITY
356 size_type capacity() const _NOEXCEPT
357 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000358
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000359 _LIBCPP_INLINE_VISIBILITY
360 void __destruct_at_end(const_pointer __new_last) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +0000361 {__destruct_at_end(__new_last, is_trivially_destructible<value_type>());}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000362 _LIBCPP_INLINE_VISIBILITY
363 void __destruct_at_end(const_pointer __new_last, false_type) _NOEXCEPT;
364 _LIBCPP_INLINE_VISIBILITY
365 void __destruct_at_end(const_pointer __new_last, true_type) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000366
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000367 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000368 void __copy_assign_alloc(const __vector_base& __c)
369 {__copy_assign_alloc(__c, integral_constant<bool,
370 __alloc_traits::propagate_on_container_copy_assignment::value>());}
371
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000373 void __move_assign_alloc(__vector_base& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000374 _NOEXCEPT_(
375 !__alloc_traits::propagate_on_container_move_assignment::value ||
376 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377 {__move_assign_alloc(__c, integral_constant<bool,
378 __alloc_traits::propagate_on_container_move_assignment::value>());}
379
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000382 _NOEXCEPT_(
383 !__alloc_traits::propagate_on_container_swap::value ||
384 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000385 {__swap_alloc(__x, __y, integral_constant<bool,
386 __alloc_traits::propagate_on_container_swap::value>());}
387private:
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000389 void __copy_assign_alloc(const __vector_base& __c, true_type)
390 {
391 if (__alloc() != __c.__alloc())
392 {
393 clear();
394 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
395 __begin_ = __end_ = __end_cap() = nullptr;
396 }
397 __alloc() = __c.__alloc();
398 }
399
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000401 void __copy_assign_alloc(const __vector_base& __c, false_type)
402 {}
403
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000405 void __move_assign_alloc(const __vector_base& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000406 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000407 {
408 __alloc() = _STD::move(__c.__alloc());
409 }
410
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000412 void __move_assign_alloc(const __vector_base& __c, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000413 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000414 {}
415
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000417 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000418 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000419 {
420 using _STD::swap;
421 swap(__x, __y);
422 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000424 static void __swap_alloc(allocator_type& __x, allocator_type& __y, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000425 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000426 {}
427};
428
429template <class _Tp, class _Allocator>
430_LIBCPP_INLINE_VISIBILITY inline
431void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000432__vector_base<_Tp, _Allocator>::__destruct_at_end(const_pointer __new_last, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000433{
434 while (__new_last < __end_)
435 __alloc_traits::destroy(__alloc(), const_cast<pointer>(--__end_));
436}
437
438template <class _Tp, class _Allocator>
439_LIBCPP_INLINE_VISIBILITY inline
440void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000441__vector_base<_Tp, _Allocator>::__destruct_at_end(const_pointer __new_last, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000442{
443 __end_ = const_cast<pointer>(__new_last);
444}
445
446template <class _Tp, class _Allocator>
447_LIBCPP_INLINE_VISIBILITY inline
448__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000449 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000450 : __begin_(0),
451 __end_(0),
452 __end_cap_(0)
453{
454}
455
456template <class _Tp, class _Allocator>
457_LIBCPP_INLINE_VISIBILITY inline
458__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
459 : __begin_(0),
460 __end_(0),
461 __end_cap_(0, __a)
462{
463}
464
465template <class _Tp, class _Allocator>
466__vector_base<_Tp, _Allocator>::~__vector_base()
467{
468 if (__begin_ != 0)
469 {
470 clear();
471 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
472 }
473}
474
475template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnant36cdf022010-09-10 16:42:26 +0000476class _LIBCPP_VISIBLE vector
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000477 : private __vector_base<_Tp, _Allocator>
478{
479private:
480 typedef __vector_base<_Tp, _Allocator> __base;
Howard Hinnant324bb032010-08-22 00:02:43 +0000481public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000482 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +0000483 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000484 typedef _Allocator allocator_type;
485 typedef typename __base::__alloc_traits __alloc_traits;
486 typedef typename __base::reference reference;
487 typedef typename __base::const_reference const_reference;
488 typedef typename __base::size_type size_type;
489 typedef typename __base::difference_type difference_type;
490 typedef typename __base::pointer pointer;
491 typedef typename __base::const_pointer const_pointer;
492#ifdef _LIBCPP_DEBUG
493 typedef __debug_iter<vector, pointer> iterator;
494 typedef __debug_iter<vector, const_pointer> const_iterator;
495
496 friend class __debug_iter<vector, pointer>;
497 friend class __debug_iter<vector, const_pointer>;
498
499 pair<iterator*, const_iterator*> __iterator_list_;
500
501 _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;}
502 _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;}
503#elif defined(_LIBCPP_RAW_ITERATORS)
504 typedef pointer iterator;
505 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000506#else // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000507 typedef __wrap_iter<pointer> iterator;
508 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000509#endif // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000510 typedef _STD::reverse_iterator<iterator> reverse_iterator;
511 typedef _STD::reverse_iterator<const_iterator> const_reverse_iterator;
512
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000513 _LIBCPP_INLINE_VISIBILITY
514 vector()
515 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
516 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000517 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a) : __base(__a) {}
518 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 &&
524 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
525 template <class _InputIterator>
526 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
527 typename enable_if<__is_input_iterator <_InputIterator>::value &&
528 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
529 template <class _ForwardIterator>
530 vector(_ForwardIterator __first, _ForwardIterator __last,
531 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
532 template <class _ForwardIterator>
533 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
534 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000536 vector(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000538 vector(initializer_list<value_type> __il, const allocator_type& __a);
539#ifdef _LIBCPP_DEBUG
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000540 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000541 ~vector() {__invalidate_all_iterators();}
542#endif
543
544 vector(const vector& __x);
545 vector(const vector& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000547 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000548#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000549 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000550 vector(vector&& __x)
551 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000553 vector(vector&& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000555 vector& operator=(vector&& __x)
556 _NOEXCEPT_(
557 __alloc_traits::propagate_on_container_move_assignment::value &&
558 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000559#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000561 vector& operator=(initializer_list<value_type> __il)
562 {assign(__il.begin(), __il.end()); return *this;}
563
564 template <class _InputIterator>
565 typename enable_if
566 <
567 __is_input_iterator <_InputIterator>::value &&
568 !__is_forward_iterator<_InputIterator>::value,
569 void
570 >::type
571 assign(_InputIterator __first, _InputIterator __last);
572 template <class _ForwardIterator>
573 typename enable_if
574 <
575 __is_forward_iterator<_ForwardIterator>::value,
576 void
577 >::type
578 assign(_ForwardIterator __first, _ForwardIterator __last);
579
580 void assign(size_type __n, const_reference __u);
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000582 void assign(initializer_list<value_type> __il)
583 {assign(__il.begin(), __il.end());}
584
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000585 _LIBCPP_INLINE_VISIBILITY
586 allocator_type get_allocator() const _NOEXCEPT
587 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000588
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000589 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
590 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
591 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
592 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000593
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000594 _LIBCPP_INLINE_VISIBILITY
595 reverse_iterator rbegin() _NOEXCEPT
596 {return reverse_iterator(end());}
597 _LIBCPP_INLINE_VISIBILITY
598 const_reverse_iterator rbegin() const _NOEXCEPT
599 {return const_reverse_iterator(end());}
600 _LIBCPP_INLINE_VISIBILITY
601 reverse_iterator rend() _NOEXCEPT
602 {return reverse_iterator(begin());}
603 _LIBCPP_INLINE_VISIBILITY
604 const_reverse_iterator rend() const _NOEXCEPT
605 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000606
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000607 _LIBCPP_INLINE_VISIBILITY
608 const_iterator cbegin() const _NOEXCEPT
609 {return begin();}
610 _LIBCPP_INLINE_VISIBILITY
611 const_iterator cend() const _NOEXCEPT
612 {return end();}
613 _LIBCPP_INLINE_VISIBILITY
614 const_reverse_iterator crbegin() const _NOEXCEPT
615 {return rbegin();}
616 _LIBCPP_INLINE_VISIBILITY
617 const_reverse_iterator crend() const _NOEXCEPT
618 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000619
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000620 _LIBCPP_INLINE_VISIBILITY
621 size_type size() const _NOEXCEPT
622 {return static_cast<size_type>(this->__end_ - this->__begin_);}
623 _LIBCPP_INLINE_VISIBILITY
624 size_type capacity() const _NOEXCEPT
625 {return __base::capacity();}
626 _LIBCPP_INLINE_VISIBILITY
627 bool empty() const _NOEXCEPT
628 {return this->__begin_ == this->__end_;}
629 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000630 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000631 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000632
633 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
634 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
635 reference at(size_type __n);
636 const_reference at(size_type __n) const;
637
638 _LIBCPP_INLINE_VISIBILITY reference front() {return *this->__begin_;}
639 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return *this->__begin_;}
640 _LIBCPP_INLINE_VISIBILITY reference back() {return *(this->__end_ - 1);}
641 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return *(this->__end_ - 1);}
642
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000643 _LIBCPP_INLINE_VISIBILITY
644 value_type* data() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000645 {return _STD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000646 _LIBCPP_INLINE_VISIBILITY
647 const value_type* data() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648 {return _STD::__to_raw_pointer(this->__begin_);}
649
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000650 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000651#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000652 void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000653#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000654 template <class... _Args>
655 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000656#endif // _LIBCPP_HAS_NO_VARIADICS
657#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000658 void pop_back();
659
660 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000661#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000662 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000663#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664 template <class... _Args>
665 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000666#endif // _LIBCPP_HAS_NO_VARIADICS
667#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000668 iterator insert(const_iterator __position, size_type __n, const_reference __x);
669 template <class _InputIterator>
670 typename enable_if
671 <
672 __is_input_iterator <_InputIterator>::value &&
673 !__is_forward_iterator<_InputIterator>::value,
674 iterator
675 >::type
676 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
677 template <class _ForwardIterator>
678 typename enable_if
679 <
680 __is_forward_iterator<_ForwardIterator>::value,
681 iterator
682 >::type
683 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000685 iterator insert(const_iterator __position, initializer_list<value_type> __il)
686 {return insert(__position, __il.begin(), __il.end());}
687
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000688 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000689 iterator erase(const_iterator __first, const_iterator __last);
690
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000691 _LIBCPP_INLINE_VISIBILITY
692 void clear() _NOEXCEPT
693 {__base::clear();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000694
695 void resize(size_type __sz);
696 void resize(size_type __sz, const_reference __x);
697
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000698 void swap(vector&)
699 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
700 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000701
702 bool __invariants() const;
703
704private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000705 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000706 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000707 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000708 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31 +0000709 void __construct_at_end(size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000710 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000711 template <class _ForwardIterator>
712 typename enable_if
713 <
714 __is_forward_iterator<_ForwardIterator>::value,
715 void
716 >::type
717 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
718 void __move_construct_at_end(pointer __first, pointer __last);
719 void __append(size_type __n);
720 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000722 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000723 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000724 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000725 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
726 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
727 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000728 void __move_assign(vector& __c, true_type)
729 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000730 void __move_assign(vector& __c, false_type);
731};
732
733template <class _Tp, class _Allocator>
734void
735vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
736{
737 for (pointer __p = this->__end_; this->__begin_ < __p;)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000738 __v.push_front(_STD::move_if_noexcept(*--__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000739 _STD::swap(this->__begin_, __v.__begin_);
740 _STD::swap(this->__end_, __v.__end_);
741 _STD::swap(this->__end_cap(), __v.__end_cap());
742 __v.__first_ = __v.__begin_;
743 __invalidate_all_iterators();
744}
745
746template <class _Tp, class _Allocator>
747typename vector<_Tp, _Allocator>::pointer
748vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
749{
750 pointer __r = __v.__begin_;
751 for (pointer __i = __p; this->__begin_ < __i;)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000752 __v.push_front(_STD::move_if_noexcept(*--__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000753 for (pointer __i = __p; __i < this->__end_; ++__i)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000754 __v.push_back(_STD::move_if_noexcept(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000755 _STD::swap(this->__begin_, __v.__begin_);
756 _STD::swap(this->__end_, __v.__end_);
757 _STD::swap(this->__end_cap(), __v.__end_cap());
758 __v.__first_ = __v.__begin_;
759 __invalidate_all_iterators();
760 return __r;
761}
762
763// Allocate space for __n objects
764// throws length_error if __n > max_size()
765// throws (probably bad_alloc) if memory run out
766// Precondition: __begin_ == __end_ == __end_cap() == 0
767// Precondition: __n > 0
768// Postcondition: capacity() == __n
769// Postcondition: size() == 0
770template <class _Tp, class _Allocator>
771void
772vector<_Tp, _Allocator>::allocate(size_type __n)
773{
774 if (__n > max_size())
775 this->__throw_length_error();
776 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
777 this->__end_cap() = this->__begin_ + __n;
778}
779
780template <class _Tp, class _Allocator>
781void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000782vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000783{
784 if (this->__begin_ != 0)
785 {
786 clear();
787 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
788 __invalidate_all_iterators();
789 this->__begin_ = this->__end_ = this->__end_cap() = 0;
790 }
791}
792
793template <class _Tp, class _Allocator>
794typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000795vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000796{
797 return _STD::min(__alloc_traits::max_size(this->__alloc()), numeric_limits<size_type>::max() / 2); // end() >= begin(), always
798}
799
800// Precondition: __new_size > capacity()
801template <class _Tp, class _Allocator>
802_LIBCPP_INLINE_VISIBILITY inline
803typename vector<_Tp, _Allocator>::size_type
804vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
805{
806 const size_type __ms = max_size();
807 if (__new_size > __ms)
808 this->__throw_length_error();
809 const size_type __cap = capacity();
810 if (__cap >= __ms / 2)
811 return __ms;
812 return _STD::max(2*__cap, __new_size);
813}
814
815// Default constructs __n objects starting at __end_
816// throws if construction throws
817// Precondition: __n > 0
818// Precondition: size() + __n <= capacity()
819// Postcondition: size() == size() + __n
820template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000821void
822vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
823{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000824 allocator_type& __a = this->__alloc();
825 do
826 {
827 __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_));
828 ++this->__end_;
829 --__n;
830 } while (__n > 0);
831}
832
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000833// Copy constructs __n objects starting at __end_ from __x
834// throws if construction throws
835// Precondition: __n > 0
836// Precondition: size() + __n <= capacity()
837// Postcondition: size() == old size() + __n
838// Postcondition: [i] == __x for all i in [size() - __n, __n)
839template <class _Tp, class _Allocator>
840_LIBCPP_INLINE_VISIBILITY inline
841void
842vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
843{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000844 allocator_type& __a = this->__alloc();
845 do
846 {
847 __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_), __x);
848 ++this->__end_;
849 --__n;
850 } while (__n > 0);
851}
852
853template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000854template <class _ForwardIterator>
855typename enable_if
856<
857 __is_forward_iterator<_ForwardIterator>::value,
858 void
859>::type
860vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
861{
862 allocator_type& __a = this->__alloc();
863 for (; __first != __last; ++__first)
864 {
865 __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_), *__first);
866 ++this->__end_;
867 }
868}
869
870template <class _Tp, class _Allocator>
871void
872vector<_Tp, _Allocator>::__move_construct_at_end(pointer __first, pointer __last)
873{
874 allocator_type& __a = this->__alloc();
875 for (; __first != __last; ++__first)
876 {
877 __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_),
878 _STD::move(*__first));
879 ++this->__end_;
880 }
881}
882
883// Default constructs __n objects starting at __end_
884// throws if construction throws
885// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000886// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000887template <class _Tp, class _Allocator>
888void
889vector<_Tp, _Allocator>::__append(size_type __n)
890{
891 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
892 this->__construct_at_end(__n);
893 else
894 {
895 allocator_type& __a = this->__alloc();
896 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
897 __v.__construct_at_end(__n);
898 __swap_out_circular_buffer(__v);
899 }
900}
901
902// Default constructs __n objects starting at __end_
903// throws if construction throws
904// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000905// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000906template <class _Tp, class _Allocator>
907void
908vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
909{
910 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
911 this->__construct_at_end(__n, __x);
912 else
913 {
914 allocator_type& __a = this->__alloc();
915 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
916 __v.__construct_at_end(__n, __x);
917 __swap_out_circular_buffer(__v);
918 }
919}
920
921template <class _Tp, class _Allocator>
922vector<_Tp, _Allocator>::vector(size_type __n)
923{
924 if (__n > 0)
925 {
926 allocate(__n);
927 __construct_at_end(__n);
928 }
929}
930
931template <class _Tp, class _Allocator>
932vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
933{
934 if (__n > 0)
935 {
936 allocate(__n);
937 __construct_at_end(__n, __x);
938 }
939}
940
941template <class _Tp, class _Allocator>
942vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
943 : __base(__a)
944{
945 if (__n > 0)
946 {
947 allocate(__n);
948 __construct_at_end(__n, __x);
949 }
950}
951
952template <class _Tp, class _Allocator>
953template <class _InputIterator>
954vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
955 typename enable_if<__is_input_iterator <_InputIterator>::value &&
956 !__is_forward_iterator<_InputIterator>::value>::type*)
957{
958 for (; __first != __last; ++__first)
959 push_back(*__first);
960}
961
962template <class _Tp, class _Allocator>
963template <class _InputIterator>
964vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
965 typename enable_if<__is_input_iterator <_InputIterator>::value &&
966 !__is_forward_iterator<_InputIterator>::value>::type*)
967 : __base(__a)
968{
969 for (; __first != __last; ++__first)
970 push_back(*__first);
971}
972
973template <class _Tp, class _Allocator>
974template <class _ForwardIterator>
975vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
976 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
977{
978 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
979 if (__n > 0)
980 {
981 allocate(__n);
982 __construct_at_end(__first, __last);
983 }
984}
985
986template <class _Tp, class _Allocator>
987template <class _ForwardIterator>
988vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
989 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
990 : __base(__a)
991{
992 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
993 if (__n > 0)
994 {
995 allocate(__n);
996 __construct_at_end(__first, __last);
997 }
998}
999
1000template <class _Tp, class _Allocator>
1001vector<_Tp, _Allocator>::vector(const vector& __x)
1002 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1003{
1004 size_type __n = __x.size();
1005 if (__n > 0)
1006 {
1007 allocate(__n);
1008 __construct_at_end(__x.__begin_, __x.__end_);
1009 }
1010}
1011
1012template <class _Tp, class _Allocator>
1013vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1014 : __base(__a)
1015{
1016 size_type __n = __x.size();
1017 if (__n > 0)
1018 {
1019 allocate(__n);
1020 __construct_at_end(__x.__begin_, __x.__end_);
1021 }
1022}
1023
Howard Hinnant73d21a42010-09-04 23:28:19 +00001024#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001025
1026template <class _Tp, class _Allocator>
1027_LIBCPP_INLINE_VISIBILITY inline
1028vector<_Tp, _Allocator>::vector(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001029 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001030 : __base(_STD::move(__x.__alloc()))
1031{
1032 this->__begin_ = __x.__begin_;
1033 this->__end_ = __x.__end_;
1034 this->__end_cap() = __x.__end_cap();
1035 __x.__begin_ = __x.__end_ = __x.__end_cap() = 0;
1036 __x.__invalidate_all_iterators();
1037}
1038
1039template <class _Tp, class _Allocator>
1040_LIBCPP_INLINE_VISIBILITY inline
1041vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1042 : __base(__a)
1043{
1044 if (__a == __x.__alloc())
1045 {
1046 this->__begin_ = __x.__begin_;
1047 this->__end_ = __x.__end_;
1048 this->__end_cap() = __x.__end_cap();
1049 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
1050 __x.__invalidate_all_iterators();
1051 }
1052 else
1053 {
1054 typedef move_iterator<iterator> _I;
1055 assign(_I(__x.begin()), _I(__x.end()));
1056 }
1057}
1058
1059template <class _Tp, class _Allocator>
1060_LIBCPP_INLINE_VISIBILITY inline
1061vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1062{
1063 if (__il.size() > 0)
1064 {
1065 allocate(__il.size());
1066 __construct_at_end(__il.begin(), __il.end());
1067 }
1068}
1069
1070template <class _Tp, class _Allocator>
1071_LIBCPP_INLINE_VISIBILITY inline
1072vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1073 : __base(__a)
1074{
1075 if (__il.size() > 0)
1076 {
1077 allocate(__il.size());
1078 __construct_at_end(__il.begin(), __il.end());
1079 }
1080}
1081
1082template <class _Tp, class _Allocator>
1083_LIBCPP_INLINE_VISIBILITY inline
1084vector<_Tp, _Allocator>&
1085vector<_Tp, _Allocator>::operator=(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001086 _NOEXCEPT_(
1087 __alloc_traits::propagate_on_container_move_assignment::value &&
1088 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001089{
1090 __move_assign(__x, integral_constant<bool,
1091 __alloc_traits::propagate_on_container_move_assignment::value>());
1092 return *this;
1093}
1094
1095template <class _Tp, class _Allocator>
1096void
1097vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1098{
1099 if (__base::__alloc() != __c.__alloc())
1100 {
1101 typedef move_iterator<iterator> _I;
1102 assign(_I(__c.begin()), _I(__c.end()));
1103 }
1104 else
1105 __move_assign(__c, true_type());
1106}
1107
1108template <class _Tp, class _Allocator>
1109void
1110vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001111 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001112{
1113 deallocate();
1114 this->__begin_ = __c.__begin_;
1115 this->__end_ = __c.__end_;
1116 this->__end_cap() = __c.__end_cap();
1117 __base::__move_assign_alloc(__c);
1118 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
1119}
1120
Howard Hinnant73d21a42010-09-04 23:28:19 +00001121#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001122
1123template <class _Tp, class _Allocator>
1124_LIBCPP_INLINE_VISIBILITY inline
1125vector<_Tp, _Allocator>&
1126vector<_Tp, _Allocator>::operator=(const vector& __x)
1127{
1128 if (this != &__x)
1129 {
1130 __base::__copy_assign_alloc(__x);
1131 assign(__x.__begin_, __x.__end_);
1132 }
1133 return *this;
1134}
1135
1136template <class _Tp, class _Allocator>
1137template <class _InputIterator>
1138typename enable_if
1139<
1140 __is_input_iterator <_InputIterator>::value &&
1141 !__is_forward_iterator<_InputIterator>::value,
1142 void
1143>::type
1144vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1145{
1146 clear();
1147 for (; __first != __last; ++__first)
1148 push_back(*__first);
1149}
1150
1151template <class _Tp, class _Allocator>
1152template <class _ForwardIterator>
1153typename enable_if
1154<
1155 __is_forward_iterator<_ForwardIterator>::value,
1156 void
1157>::type
1158vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1159{
1160 typename iterator_traits<_ForwardIterator>::difference_type __new_size = _STD::distance(__first, __last);
1161 if (static_cast<size_type>(__new_size) <= capacity())
1162 {
1163 _ForwardIterator __mid = __last;
1164 bool __growing = false;
1165 if (static_cast<size_type>(__new_size) > size())
1166 {
1167 __growing = true;
1168 __mid = __first;
1169 _STD::advance(__mid, size());
1170 }
1171 pointer __m = _STD::copy(__first, __mid, this->__begin_);
1172 if (__growing)
1173 __construct_at_end(__mid, __last);
1174 else
1175 this->__destruct_at_end(__m);
1176 }
1177 else
1178 {
1179 deallocate();
1180 allocate(__recommend(static_cast<size_type>(__new_size)));
1181 __construct_at_end(__first, __last);
1182 }
1183}
1184
1185template <class _Tp, class _Allocator>
1186void
1187vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1188{
1189 if (__n <= capacity())
1190 {
1191 size_type __s = size();
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001192 _STD::fill_n(this->__begin_, _STD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001193 if (__n > __s)
1194 __construct_at_end(__n - __s, __u);
1195 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001196 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001197 }
1198 else
1199 {
1200 deallocate();
1201 allocate(__recommend(static_cast<size_type>(__n)));
1202 __construct_at_end(__n, __u);
1203 }
1204}
1205
Howard Hinnant324bb032010-08-22 00:02:43 +00001206template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001207_LIBCPP_INLINE_VISIBILITY inline
1208typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001209vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001210{
1211#ifdef _LIBCPP_DEBUG
1212 return iterator(this, __p);
1213#else
1214 return iterator(__p);
1215#endif
1216}
1217
Howard Hinnant324bb032010-08-22 00:02:43 +00001218template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001219_LIBCPP_INLINE_VISIBILITY inline
1220typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001221vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001222{
1223#ifdef _LIBCPP_DEBUG
1224 return const_iterator(this, __p);
1225#else
1226 return const_iterator(__p);
1227#endif
1228}
1229
Howard Hinnant324bb032010-08-22 00:02:43 +00001230template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001231_LIBCPP_INLINE_VISIBILITY inline
1232typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001233vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001234{
1235 return __make_iter(this->__begin_);
1236}
1237
Howard Hinnant324bb032010-08-22 00:02:43 +00001238template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001239_LIBCPP_INLINE_VISIBILITY inline
1240typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001241vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001242{
1243 return __make_iter(this->__begin_);
1244}
1245
Howard Hinnant324bb032010-08-22 00:02:43 +00001246template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001247_LIBCPP_INLINE_VISIBILITY inline
1248typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001249vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001250{
1251 return __make_iter(this->__end_);
1252}
1253
Howard Hinnant324bb032010-08-22 00:02:43 +00001254template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001255_LIBCPP_INLINE_VISIBILITY inline
1256typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001257vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001258{
1259 return __make_iter(this->__end_);
1260}
1261
Howard Hinnant324bb032010-08-22 00:02:43 +00001262template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001263_LIBCPP_INLINE_VISIBILITY inline
1264typename vector<_Tp, _Allocator>::reference
1265vector<_Tp, _Allocator>::operator[](size_type __n)
1266{
1267#ifdef _LIBCPP_DEBUG
1268 assert(__n < size());
1269#endif
1270 return this->__begin_[__n];
1271}
1272
Howard Hinnant324bb032010-08-22 00:02:43 +00001273template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001274_LIBCPP_INLINE_VISIBILITY inline
1275typename vector<_Tp, _Allocator>::const_reference
1276vector<_Tp, _Allocator>::operator[](size_type __n) const
1277{
1278#ifdef _LIBCPP_DEBUG
1279 assert(__n < size());
1280#endif
1281 return this->__begin_[__n];
1282}
1283
Howard Hinnant324bb032010-08-22 00:02:43 +00001284template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001285typename vector<_Tp, _Allocator>::reference
1286vector<_Tp, _Allocator>::at(size_type __n)
1287{
1288 if (__n >= size())
1289 this->__throw_out_of_range();
1290 return this->__begin_[__n];
1291}
1292
Howard Hinnant324bb032010-08-22 00:02:43 +00001293template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001294typename vector<_Tp, _Allocator>::const_reference
1295vector<_Tp, _Allocator>::at(size_type __n) const
1296{
1297 if (__n >= size())
1298 this->__throw_out_of_range();
1299 return this->__begin_[__n];
1300}
1301
1302template <class _Tp, class _Allocator>
1303void
1304vector<_Tp, _Allocator>::reserve(size_type __n)
1305{
1306 if (__n > capacity())
1307 {
1308 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001309 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001310 __swap_out_circular_buffer(__v);
1311 }
1312}
1313
1314template <class _Tp, class _Allocator>
1315void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001316vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001317{
1318 if (capacity() > size())
1319 {
1320#ifndef _LIBCPP_NO_EXCEPTIONS
1321 try
1322 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001323#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001324 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001325 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326 __swap_out_circular_buffer(__v);
1327#ifndef _LIBCPP_NO_EXCEPTIONS
1328 }
1329 catch (...)
1330 {
1331 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001332#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001333 }
1334}
1335
1336template <class _Tp, class _Allocator>
1337void
1338vector<_Tp, _Allocator>::push_back(const_reference __x)
1339{
1340 if (this->__end_ < this->__end_cap())
1341 {
1342 __alloc_traits::construct(this->__alloc(),
1343 _STD::__to_raw_pointer(this->__end_), __x);
1344 ++this->__end_;
1345 }
1346 else
1347 {
1348 allocator_type& __a = this->__alloc();
1349 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1350 __v.push_back(__x);
1351 __swap_out_circular_buffer(__v);
1352 }
1353}
1354
Howard Hinnant73d21a42010-09-04 23:28:19 +00001355#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001356
1357template <class _Tp, class _Allocator>
1358void
1359vector<_Tp, _Allocator>::push_back(value_type&& __x)
1360{
1361 if (this->__end_ < this->__end_cap())
1362 {
1363 __alloc_traits::construct(this->__alloc(),
1364 _STD::__to_raw_pointer(this->__end_),
1365 _STD::move(__x));
1366 ++this->__end_;
1367 }
1368 else
1369 {
1370 allocator_type& __a = this->__alloc();
1371 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1372 __v.push_back(_STD::move(__x));
1373 __swap_out_circular_buffer(__v);
1374 }
1375}
1376
Howard Hinnant73d21a42010-09-04 23:28:19 +00001377#ifndef _LIBCPP_HAS_NO_VARIADICS
1378
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001379template <class _Tp, class _Allocator>
1380template <class... _Args>
1381void
1382vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1383{
1384 if (this->__end_ < this->__end_cap())
1385 {
1386 __alloc_traits::construct(this->__alloc(),
1387 _STD::__to_raw_pointer(this->__end_),
1388 _STD::forward<_Args>(__args)...);
1389 ++this->__end_;
1390 }
1391 else
1392 {
1393 allocator_type& __a = this->__alloc();
1394 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1395 __v.emplace_back(_STD::forward<_Args>(__args)...);
1396 __swap_out_circular_buffer(__v);
1397 }
1398}
1399
Howard Hinnant73d21a42010-09-04 23:28:19 +00001400#endif // _LIBCPP_HAS_NO_VARIADICS
1401#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001402
1403template <class _Tp, class _Allocator>
1404_LIBCPP_INLINE_VISIBILITY inline
1405void
1406vector<_Tp, _Allocator>::pop_back()
1407{
1408 this->__destruct_at_end(this->__end_ - 1);
1409}
1410
1411template <class _Tp, class _Allocator>
1412_LIBCPP_INLINE_VISIBILITY inline
1413typename vector<_Tp, _Allocator>::iterator
1414vector<_Tp, _Allocator>::erase(const_iterator __position)
1415{
1416 pointer __p = const_cast<pointer>(&*__position);
1417 iterator __r = __make_iter(__p);
1418 this->__destruct_at_end(_STD::move(__p + 1, this->__end_, __p));
1419 return __r;
1420}
1421
1422template <class _Tp, class _Allocator>
1423typename vector<_Tp, _Allocator>::iterator
1424vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1425{
1426 pointer __p = this->__begin_ + (__first - begin());
1427 iterator __r = __make_iter(__p);
1428 this->__destruct_at_end(_STD::move(__p + (__last - __first), this->__end_, __p));
1429 return __r;
1430}
1431
1432template <class _Tp, class _Allocator>
1433void
1434vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1435{
1436 pointer __old_last = this->__end_;
1437 difference_type __n = __old_last - __to;
1438 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1439 __alloc_traits::construct(this->__alloc(),
1440 _STD::__to_raw_pointer(this->__end_),
1441 _STD::move(*__i));
1442 _STD::move_backward(__from_s, __from_s + __n, __old_last);
1443}
1444
1445template <class _Tp, class _Allocator>
1446typename vector<_Tp, _Allocator>::iterator
1447vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1448{
1449 pointer __p = this->__begin_ + (__position - begin());
1450 if (this->__end_ < this->__end_cap())
1451 {
1452 if (__p == this->__end_)
1453 {
1454 __alloc_traits::construct(this->__alloc(),
1455 _STD::__to_raw_pointer(this->__end_), __x);
1456 ++this->__end_;
1457 }
1458 else
1459 {
1460 __move_range(__p, this->__end_, __p + 1);
1461 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1462 if (__p <= __xr && __xr < this->__end_)
1463 ++__xr;
1464 *__p = *__xr;
1465 }
1466 }
1467 else
1468 {
1469 allocator_type& __a = this->__alloc();
1470 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1471 __v.push_back(__x);
1472 __p = __swap_out_circular_buffer(__v, __p);
1473 }
1474 return __make_iter(__p);
1475}
1476
Howard Hinnant73d21a42010-09-04 23:28:19 +00001477#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478
1479template <class _Tp, class _Allocator>
1480typename vector<_Tp, _Allocator>::iterator
1481vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1482{
1483 pointer __p = this->__begin_ + (__position - begin());
1484 if (this->__end_ < this->__end_cap())
1485 {
1486 if (__p == this->__end_)
1487 {
1488 __alloc_traits::construct(this->__alloc(),
1489 _STD::__to_raw_pointer(this->__end_),
1490 _STD::move(__x));
1491 ++this->__end_;
1492 }
1493 else
1494 {
1495 __move_range(__p, this->__end_, __p + 1);
1496 *__p = _STD::move(__x);
1497 }
1498 }
1499 else
1500 {
1501 allocator_type& __a = this->__alloc();
1502 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1503 __v.push_back(_STD::move(__x));
1504 __p = __swap_out_circular_buffer(__v, __p);
1505 }
1506 return __make_iter(__p);
1507}
1508
Howard Hinnant73d21a42010-09-04 23:28:19 +00001509#ifndef _LIBCPP_HAS_NO_VARIADICS
1510
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001511template <class _Tp, class _Allocator>
1512template <class... _Args>
1513typename vector<_Tp, _Allocator>::iterator
1514vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1515{
1516 pointer __p = this->__begin_ + (__position - begin());
1517 if (this->__end_ < this->__end_cap())
1518 {
1519 if (__p == this->__end_)
1520 {
1521 __alloc_traits::construct(this->__alloc(),
1522 _STD::__to_raw_pointer(this->__end_),
1523 _STD::forward<_Args>(__args)...);
1524 ++this->__end_;
1525 }
1526 else
1527 {
1528 __move_range(__p, this->__end_, __p + 1);
1529 *__p = value_type(_STD::forward<_Args>(__args)...);
1530 }
1531 }
1532 else
1533 {
1534 allocator_type& __a = this->__alloc();
1535 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1536 __v.emplace_back(_STD::forward<_Args>(__args)...);
1537 __p = __swap_out_circular_buffer(__v, __p);
1538 }
1539 return __make_iter(__p);
1540}
1541
Howard Hinnant73d21a42010-09-04 23:28:19 +00001542#endif // _LIBCPP_HAS_NO_VARIADICS
1543#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001544
1545template <class _Tp, class _Allocator>
1546typename vector<_Tp, _Allocator>::iterator
1547vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1548{
1549 pointer __p = this->__begin_ + (__position - begin());
1550 if (__n > 0)
1551 {
1552 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1553 {
1554 size_type __old_n = __n;
1555 pointer __old_last = this->__end_;
1556 if (__n > static_cast<size_type>(this->__end_ - __p))
1557 {
1558 size_type __cx = __n - (this->__end_ - __p);
1559 __construct_at_end(__cx, __x);
1560 __n -= __cx;
1561 }
1562 if (__n > 0)
1563 {
1564 __move_range(__p, __old_last, __p + __old_n);
1565 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1566 if (__p <= __xr && __xr < this->__end_)
1567 __xr += __old_n;
1568 _STD::fill_n(__p, __n, *__xr);
1569 }
1570 }
1571 else
1572 {
1573 allocator_type& __a = this->__alloc();
1574 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1575 __v.__construct_at_end(__n, __x);
1576 __p = __swap_out_circular_buffer(__v, __p);
1577 }
1578 }
1579 return __make_iter(__p);
1580}
1581
1582template <class _Tp, class _Allocator>
1583template <class _InputIterator>
1584typename enable_if
1585<
1586 __is_input_iterator <_InputIterator>::value &&
1587 !__is_forward_iterator<_InputIterator>::value,
1588 typename vector<_Tp, _Allocator>::iterator
1589>::type
1590vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1591{
1592 difference_type __off = __position - begin();
1593 pointer __p = this->__begin_ + __off;
1594 allocator_type& __a = this->__alloc();
1595 pointer __old_last = this->__end_;
1596 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1597 {
1598 __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_),
1599 *__first);
1600 ++this->__end_;
1601 }
1602 __split_buffer<value_type, allocator_type&> __v(__a);
1603 if (__first != __last)
1604 {
1605#ifndef _LIBCPP_NO_EXCEPTIONS
1606 try
1607 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001608#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001609 __v.__construct_at_end(__first, __last);
1610 difference_type __old_size = __old_last - this->__begin_;
1611 difference_type __old_p = __p - this->__begin_;
1612 reserve(__recommend(size() + __v.size()));
1613 __p = this->__begin_ + __old_p;
1614 __old_last = this->__begin_ + __old_size;
1615#ifndef _LIBCPP_NO_EXCEPTIONS
1616 }
1617 catch (...)
1618 {
1619 erase(__make_iter(__old_last), end());
1620 throw;
1621 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001622#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001623 }
1624 __p = _STD::rotate(__p, __old_last, this->__end_);
1625 insert(__make_iter(__p), move_iterator<iterator>(__v.begin()),
1626 move_iterator<iterator>(__v.end()));
1627 return begin() + __off;
1628}
1629
1630template <class _Tp, class _Allocator>
1631template <class _ForwardIterator>
1632typename enable_if
1633<
1634 __is_forward_iterator<_ForwardIterator>::value,
1635 typename vector<_Tp, _Allocator>::iterator
1636>::type
1637vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1638{
1639 pointer __p = this->__begin_ + (__position - begin());
1640 difference_type __n = _STD::distance(__first, __last);
1641 if (__n > 0)
1642 {
1643 if (__n <= this->__end_cap() - this->__end_)
1644 {
1645 size_type __old_n = __n;
1646 pointer __old_last = this->__end_;
1647 _ForwardIterator __m = __last;
1648 difference_type __dx = this->__end_ - __p;
1649 if (__n > __dx)
1650 {
1651 __m = __first;
1652 _STD::advance(__m, this->__end_ - __p);
1653 __construct_at_end(__m, __last);
1654 __n = __dx;
1655 }
1656 if (__n > 0)
1657 {
1658 __move_range(__p, __old_last, __p + __old_n);
1659 _STD::copy(__first, __m, __p);
1660 }
1661 }
1662 else
1663 {
1664 allocator_type& __a = this->__alloc();
1665 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1666 __v.__construct_at_end(__first, __last);
1667 __p = __swap_out_circular_buffer(__v, __p);
1668 }
1669 }
1670 return __make_iter(__p);
1671}
1672
1673template <class _Tp, class _Allocator>
1674void
1675vector<_Tp, _Allocator>::resize(size_type __sz)
1676{
1677 size_type __cs = size();
1678 if (__cs < __sz)
1679 this->__append(__sz - __cs);
1680 else if (__cs > __sz)
1681 this->__destruct_at_end(this->__begin_ + __sz);
1682}
1683
1684template <class _Tp, class _Allocator>
1685void
1686vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
1687{
1688 size_type __cs = size();
1689 if (__cs < __sz)
1690 this->__append(__sz - __cs, __x);
1691 else if (__cs > __sz)
1692 this->__destruct_at_end(this->__begin_ + __sz);
1693}
1694
1695template <class _Tp, class _Allocator>
1696void
1697vector<_Tp, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001698 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1699 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001700{
1701 _STD::swap(this->__begin_, __x.__begin_);
1702 _STD::swap(this->__end_, __x.__end_);
1703 _STD::swap(this->__end_cap(), __x.__end_cap());
1704 __base::__swap_alloc(this->__alloc(), __x.__alloc());
1705#ifdef _LIBCPP_DEBUG
1706 iterator::swap(this, &__x);
1707 const_iterator::swap(this, &__x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001708#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001709}
1710
Howard Hinnant324bb032010-08-22 00:02:43 +00001711template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001712bool
1713vector<_Tp, _Allocator>::__invariants() const
1714{
1715 if (this->__begin_ == 0)
1716 {
1717 if (this->__end_ != 0 || this->__end_cap() != 0)
1718 return false;
1719 }
1720 else
1721 {
1722 if (this->__begin_ > this->__end_)
1723 return false;
1724 if (this->__begin_ == this->__end_cap())
1725 return false;
1726 if (this->__end_ > this->__end_cap())
1727 return false;
1728 }
1729 return true;
1730}
1731
1732template <class _Tp, class _Allocator>
1733#ifndef _LIBCPP_DEBUG
1734_LIBCPP_INLINE_VISIBILITY inline
1735#endif
1736void
1737vector<_Tp, _Allocator>::__invalidate_all_iterators()
1738{
1739#ifdef _LIBCPP_DEBUG
1740 iterator::__remove_all(this);
1741 const_iterator::__remove_all(this);
Howard Hinnant324bb032010-08-22 00:02:43 +00001742#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001743}
1744
1745// vector<bool>
1746
1747template <class _Allocator> class vector<bool, _Allocator>;
1748
1749template <class _Allocator> struct hash<vector<bool, _Allocator> >;
1750
1751template <class _Allocator>
Howard Hinnant36cdf022010-09-10 16:42:26 +00001752class _LIBCPP_VISIBLE vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001753 : private __vector_base_common<true>
1754{
1755public:
1756 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00001757 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001758 typedef _Allocator allocator_type;
1759 typedef allocator_traits<allocator_type> __alloc_traits;
1760 typedef __bit_reference<vector> reference;
1761 typedef __bit_const_reference<vector> const_reference;
1762 typedef typename __alloc_traits::size_type size_type;
1763 typedef typename __alloc_traits::difference_type difference_type;
1764 typedef __bit_iterator<vector, false> pointer;
1765 typedef __bit_iterator<vector, true> const_pointer;
1766#ifdef _LIBCPP_DEBUG
1767 typedef __debug_iter<vector, pointer> iterator;
1768 typedef __debug_iter<vector, const_pointer> const_iterator;
1769
1770 friend class __debug_iter<vector, pointer>;
1771 friend class __debug_iter<vector, const_pointer>;
1772
1773 pair<iterator*, const_iterator*> __iterator_list_;
1774
1775 _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;}
1776 _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;}
Howard Hinnant324bb032010-08-22 00:02:43 +00001777#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001778 typedef pointer iterator;
1779 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +00001780#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001781 typedef _STD::reverse_iterator<iterator> reverse_iterator;
1782 typedef _STD::reverse_iterator<const_iterator> const_reverse_iterator;
1783
1784private:
1785 typedef size_type __storage_type;
1786 typedef typename __alloc_traits::template
1787#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1788 rebind_alloc<__storage_type>
1789#else
1790 rebind_alloc<__storage_type>::other
1791#endif
1792 __storage_allocator;
1793 typedef allocator_traits<__storage_allocator> __storage_traits;
1794 typedef typename __storage_traits::pointer __storage_pointer;
1795 typedef typename __storage_traits::const_pointer __const_storage_pointer;
1796
1797 __storage_pointer __begin_;
1798 size_type __size_;
1799 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
1800
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001801 _LIBCPP_INLINE_VISIBILITY
1802 size_type& __cap() _NOEXCEPT
1803 {return __cap_alloc_.first();}
1804 _LIBCPP_INLINE_VISIBILITY
1805 const size_type& __cap() const _NOEXCEPT
1806 {return __cap_alloc_.first();}
1807 _LIBCPP_INLINE_VISIBILITY
1808 __storage_allocator& __alloc() _NOEXCEPT
1809 {return __cap_alloc_.second();}
1810 _LIBCPP_INLINE_VISIBILITY
1811 const __storage_allocator& __alloc() const _NOEXCEPT
1812 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001813
1814 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
1815
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001816 _LIBCPP_INLINE_VISIBILITY
1817 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001818 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001819 _LIBCPP_INLINE_VISIBILITY
1820 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001821 {return (__n - 1) / __bits_per_word + 1;}
1822
1823public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001824 _LIBCPP_INLINE_VISIBILITY
1825 vector()
1826 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001827 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001828 ~vector();
1829 explicit vector(size_type __n);
1830 vector(size_type __n, const value_type& __v);
1831 vector(size_type __n, const value_type& __v, const allocator_type& __a);
1832 template <class _InputIterator>
1833 vector(_InputIterator __first, _InputIterator __last,
1834 typename enable_if<__is_input_iterator <_InputIterator>::value &&
1835 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
1836 template <class _InputIterator>
1837 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1838 typename enable_if<__is_input_iterator <_InputIterator>::value &&
1839 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
1840 template <class _ForwardIterator>
1841 vector(_ForwardIterator __first, _ForwardIterator __last,
1842 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
1843 template <class _ForwardIterator>
1844 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
1845 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
1846
1847 vector(const vector& __v);
1848 vector(const vector& __v, const allocator_type& __a);
1849 vector& operator=(const vector& __v);
1850 vector(initializer_list<value_type> __il);
1851 vector(initializer_list<value_type> __il, const allocator_type& __a);
1852
Howard Hinnant73d21a42010-09-04 23:28:19 +00001853#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001854 _LIBCPP_INLINE_VISIBILITY
1855 vector(vector&& __v)
1856 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001857 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001858 _LIBCPP_INLINE_VISIBILITY
1859 vector& operator=(vector&& __v)
1860 _NOEXCEPT_(
1861 __alloc_traits::propagate_on_container_move_assignment::value &&
1862 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001863#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001865 vector& operator=(initializer_list<value_type> __il)
1866 {assign(__il.begin(), __il.end()); return *this;}
1867
1868 template <class _InputIterator>
1869 typename enable_if
1870 <
1871 __is_input_iterator<_InputIterator>::value &&
1872 !__is_forward_iterator<_InputIterator>::value,
1873 void
1874 >::type
1875 assign(_InputIterator __first, _InputIterator __last);
1876 template <class _ForwardIterator>
1877 typename enable_if
1878 <
1879 __is_forward_iterator<_ForwardIterator>::value,
1880 void
1881 >::type
1882 assign(_ForwardIterator __first, _ForwardIterator __last);
1883
1884 void assign(size_type __n, const value_type& __x);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001886 void assign(initializer_list<value_type> __il)
1887 {assign(__il.begin(), __il.end());}
1888
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001889 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001890 {return allocator_type(this->__alloc());}
1891
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001892 size_type max_size() const _NOEXCEPT;
1893 _LIBCPP_INLINE_VISIBILITY
1894 size_type capacity() const _NOEXCEPT
1895 {return __internal_cap_to_external(__cap());}
1896 _LIBCPP_INLINE_VISIBILITY
1897 size_type size() const _NOEXCEPT
1898 {return __size_;}
1899 _LIBCPP_INLINE_VISIBILITY
1900 bool empty() const _NOEXCEPT
1901 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001902 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001903 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001904
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001905 _LIBCPP_INLINE_VISIBILITY
1906 iterator begin() _NOEXCEPT
1907 {return __make_iter(0);}
1908 _LIBCPP_INLINE_VISIBILITY
1909 const_iterator begin() const _NOEXCEPT
1910 {return __make_iter(0);}
1911 _LIBCPP_INLINE_VISIBILITY
1912 iterator end() _NOEXCEPT
1913 {return __make_iter(__size_);}
1914 _LIBCPP_INLINE_VISIBILITY
1915 const_iterator end() const _NOEXCEPT
1916 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001917
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001918 _LIBCPP_INLINE_VISIBILITY
1919 reverse_iterator rbegin() _NOEXCEPT
1920 {return reverse_iterator(end());}
1921 _LIBCPP_INLINE_VISIBILITY
1922 const_reverse_iterator rbegin() const _NOEXCEPT
1923 {return const_reverse_iterator(end());}
1924 _LIBCPP_INLINE_VISIBILITY
1925 reverse_iterator rend() _NOEXCEPT
1926 {return reverse_iterator(begin());}
1927 _LIBCPP_INLINE_VISIBILITY
1928 const_reverse_iterator rend() const _NOEXCEPT
1929 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001930
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001931 _LIBCPP_INLINE_VISIBILITY
1932 const_iterator cbegin() const _NOEXCEPT
1933 {return __make_iter(0);}
1934 _LIBCPP_INLINE_VISIBILITY
1935 const_iterator cend() const _NOEXCEPT
1936 {return __make_iter(__size_);}
1937 _LIBCPP_INLINE_VISIBILITY
1938 const_reverse_iterator crbegin() const _NOEXCEPT
1939 {return rbegin();}
1940 _LIBCPP_INLINE_VISIBILITY
1941 const_reverse_iterator crend() const _NOEXCEPT
1942 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001943
1944 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
1945 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
1946 reference at(size_type __n);
1947 const_reference at(size_type __n) const;
1948
1949 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
1950 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
1951 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
1952 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
1953
1954 void push_back(const value_type& __x);
1955 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
1956
1957 iterator insert(const_iterator __position, const value_type& __x);
1958 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
1959 iterator insert(const_iterator __position, size_type __n, const_reference __x);
1960 template <class _InputIterator>
1961 typename enable_if
1962 <
1963 __is_input_iterator <_InputIterator>::value &&
1964 !__is_forward_iterator<_InputIterator>::value,
1965 iterator
1966 >::type
1967 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
1968 template <class _ForwardIterator>
1969 typename enable_if
1970 <
1971 __is_forward_iterator<_ForwardIterator>::value,
1972 iterator
1973 >::type
1974 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001975 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001976 iterator insert(const_iterator __position, initializer_list<value_type> __il)
1977 {return insert(__position, __il.begin(), __il.end());}
1978
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001979 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001980 iterator erase(const_iterator __first, const_iterator __last);
1981
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001982 _LIBCPP_INLINE_VISIBILITY
1983 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001984
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001985 void swap(vector&)
1986 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1987 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001988
1989 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001990 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001991
1992 bool __invariants() const;
1993
1994private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001995 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001996 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001997 void deallocate() _NOEXCEPT;
1998 _LIBCPP_INLINE_VISIBILITY
1999 static size_type __align(size_type __new_size) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002000 {return __new_size + (__bits_per_word-1) & ~(__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002001 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2002 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002003 template <class _ForwardIterator>
2004 typename enable_if
2005 <
2006 __is_forward_iterator<_ForwardIterator>::value,
2007 void
2008 >::type
2009 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2010 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002011 _LIBCPP_INLINE_VISIBILITY
2012 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002013 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002014 _LIBCPP_INLINE_VISIBILITY
2015 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002016 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
2017#ifdef _LIBCPP_DEBUG
2018 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_type __pos)
2019 {return iterator(this, pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2020 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_type __pos) const
2021 {return const_iterator(this, const_pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2022 _LIBCPP_INLINE_VISIBILITY iterator __const_iterator_cast(const_iterator __p)
2023 {return iterator(this, pointer(const_cast<__storage_pointer>(__p.base().__seg_), __p.base().__ctz_));}
Howard Hinnant324bb032010-08-22 00:02:43 +00002024#else // _LIBCPP_DEBUG
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002025 _LIBCPP_INLINE_VISIBILITY
2026 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002027 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002028 _LIBCPP_INLINE_VISIBILITY
2029 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002030 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002031 _LIBCPP_INLINE_VISIBILITY
2032 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002033 {return iterator(const_cast<__storage_pointer>(__p.__seg_), __p.__ctz_);}
Howard Hinnant324bb032010-08-22 00:02:43 +00002034#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002036 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002037 void __copy_assign_alloc(const vector& __v)
2038 {__copy_assign_alloc(__v, integral_constant<bool,
2039 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002040 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002041 void __copy_assign_alloc(const vector& __c, true_type)
2042 {
2043 if (__alloc() != __c.__alloc())
2044 deallocate();
2045 __alloc() = __c.__alloc();
2046 }
2047
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002049 void __copy_assign_alloc(const vector& __c, false_type)
2050 {}
2051
2052 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002053 void __move_assign(vector& __c, true_type)
2054 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002055 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002056 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002057 _NOEXCEPT_(
2058 !__storage_traits::propagate_on_container_move_assignment::value ||
2059 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002060 {__move_assign_alloc(__c, integral_constant<bool,
2061 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002063 void __move_assign_alloc(const vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002064 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002065 {
2066 __alloc() = _STD::move(__c.__alloc());
2067 }
2068
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002069 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070 void __move_assign_alloc(const vector& __c, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002071 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002072 {}
2073
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002074 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002075 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002076 _NOEXCEPT_(
2077 !__storage_traits::propagate_on_container_swap::value ||
2078 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002079 {__swap_alloc(__x, __y, integral_constant<bool,
2080 __storage_traits::propagate_on_container_swap::value>());}
2081
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002082 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002083 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002084 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002085 {
2086 using _STD::swap;
2087 swap(__x, __y);
2088 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002089 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002090 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002091 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002092 {}
2093
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002094 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002095
2096 friend class __bit_reference<vector>;
2097 friend class __bit_const_reference<vector>;
2098 friend class __bit_iterator<vector, false>;
2099 friend class __bit_iterator<vector, true>;
2100 friend class __bit_array<vector>;
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002101 friend struct _LIBCPP_VISIBLE hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002102};
2103
2104template <class _Allocator>
2105#ifndef _LIBCPP_DEBUG
2106_LIBCPP_INLINE_VISIBILITY inline
2107#endif
2108void
2109vector<bool, _Allocator>::__invalidate_all_iterators()
2110{
2111#ifdef _LIBCPP_DEBUG
2112 iterator::__remove_all(this);
2113 const_iterator::__remove_all(this);
Howard Hinnant324bb032010-08-22 00:02:43 +00002114#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002115}
2116
2117// Allocate space for __n objects
2118// throws length_error if __n > max_size()
2119// throws (probably bad_alloc) if memory run out
2120// Precondition: __begin_ == __end_ == __cap() == 0
2121// Precondition: __n > 0
2122// Postcondition: capacity() == __n
2123// Postcondition: size() == 0
2124template <class _Allocator>
2125void
2126vector<bool, _Allocator>::allocate(size_type __n)
2127{
2128 if (__n > max_size())
2129 this->__throw_length_error();
2130 __n = __external_cap_to_internal(__n);
2131 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2132 this->__size_ = 0;
2133 this->__cap() = __n;
2134}
2135
2136template <class _Allocator>
2137void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002138vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002139{
2140 if (this->__begin_ != 0)
2141 {
2142 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2143 __invalidate_all_iterators();
2144 this->__begin_ = 0;
2145 this->__size_ = this->__cap() = 0;
2146 }
2147}
2148
2149template <class _Allocator>
2150typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002151vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002152{
2153 size_type __amax = __storage_traits::max_size(__alloc());
2154 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2155 if (__nmax / __bits_per_word <= __amax)
2156 return __nmax;
2157 return __internal_cap_to_external(__amax);
2158}
2159
2160// Precondition: __new_size > capacity()
2161template <class _Allocator>
2162_LIBCPP_INLINE_VISIBILITY inline
2163typename vector<bool, _Allocator>::size_type
2164vector<bool, _Allocator>::__recommend(size_type __new_size) const
2165{
2166 const size_type __ms = max_size();
2167 if (__new_size > __ms)
2168 this->__throw_length_error();
2169 const size_type __cap = capacity();
2170 if (__cap >= __ms / 2)
2171 return __ms;
2172 return _STD::max(2*__cap, __align(__new_size));
2173}
2174
2175// Default constructs __n objects starting at __end_
2176// Precondition: __n > 0
2177// Precondition: size() + __n <= capacity()
2178// Postcondition: size() == size() + __n
2179template <class _Allocator>
2180_LIBCPP_INLINE_VISIBILITY inline
2181void
2182vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2183{
2184 size_type __old_size = this->__size_;
2185 this->__size_ += __n;
2186 _STD::fill_n(__make_iter(__old_size), __n, __x);
2187}
2188
2189template <class _Allocator>
2190template <class _ForwardIterator>
2191typename enable_if
2192<
2193 __is_forward_iterator<_ForwardIterator>::value,
2194 void
2195>::type
2196vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2197{
2198 size_type __old_size = this->__size_;
2199 this->__size_ += _STD::distance(__first, __last);
2200 _STD::copy(__first, __last, __make_iter(__old_size));
2201}
2202
2203template <class _Allocator>
2204_LIBCPP_INLINE_VISIBILITY inline
2205vector<bool, _Allocator>::vector()
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002206 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002207 : __begin_(0),
2208 __size_(0),
2209 __cap_alloc_(0)
2210{
2211}
2212
2213template <class _Allocator>
2214_LIBCPP_INLINE_VISIBILITY inline
2215vector<bool, _Allocator>::vector(const allocator_type& __a)
2216 : __begin_(0),
2217 __size_(0),
2218 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2219{
2220}
2221
2222template <class _Allocator>
2223vector<bool, _Allocator>::vector(size_type __n)
2224 : __begin_(0),
2225 __size_(0),
2226 __cap_alloc_(0)
2227{
2228 if (__n > 0)
2229 {
2230 allocate(__n);
2231 __construct_at_end(__n, false);
2232 }
2233}
2234
2235template <class _Allocator>
2236vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
2237 : __begin_(0),
2238 __size_(0),
2239 __cap_alloc_(0)
2240{
2241 if (__n > 0)
2242 {
2243 allocate(__n);
2244 __construct_at_end(__n, __x);
2245 }
2246}
2247
2248template <class _Allocator>
2249vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
2250 : __begin_(0),
2251 __size_(0),
2252 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2253{
2254 if (__n > 0)
2255 {
2256 allocate(__n);
2257 __construct_at_end(__n, __x);
2258 }
2259}
2260
2261template <class _Allocator>
2262template <class _InputIterator>
2263vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2264 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2265 !__is_forward_iterator<_InputIterator>::value>::type*)
2266 : __begin_(0),
2267 __size_(0),
2268 __cap_alloc_(0)
2269{
2270#ifndef _LIBCPP_NO_EXCEPTIONS
2271 try
2272 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002273#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002274 for (; __first != __last; ++__first)
2275 push_back(*__first);
2276#ifndef _LIBCPP_NO_EXCEPTIONS
2277 }
2278 catch (...)
2279 {
2280 if (__begin_ != 0)
2281 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2282 __invalidate_all_iterators();
2283 throw;
2284 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002285#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002286}
2287
2288template <class _Allocator>
2289template <class _InputIterator>
2290vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2291 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2292 !__is_forward_iterator<_InputIterator>::value>::type*)
2293 : __begin_(0),
2294 __size_(0),
2295 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2296{
2297#ifndef _LIBCPP_NO_EXCEPTIONS
2298 try
2299 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002300#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002301 for (; __first != __last; ++__first)
2302 push_back(*__first);
2303#ifndef _LIBCPP_NO_EXCEPTIONS
2304 }
2305 catch (...)
2306 {
2307 if (__begin_ != 0)
2308 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2309 __invalidate_all_iterators();
2310 throw;
2311 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002312#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002313}
2314
2315template <class _Allocator>
2316template <class _ForwardIterator>
2317vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2318 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2319 : __begin_(0),
2320 __size_(0),
2321 __cap_alloc_(0)
2322{
2323 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
2324 if (__n > 0)
2325 {
2326 allocate(__n);
2327 __construct_at_end(__first, __last);
2328 }
2329}
2330
2331template <class _Allocator>
2332template <class _ForwardIterator>
2333vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2334 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2335 : __begin_(0),
2336 __size_(0),
2337 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2338{
2339 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
2340 if (__n > 0)
2341 {
2342 allocate(__n);
2343 __construct_at_end(__first, __last);
2344 }
2345}
2346
2347template <class _Allocator>
2348vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
2349 : __begin_(0),
2350 __size_(0),
2351 __cap_alloc_(0)
2352{
2353 size_type __n = static_cast<size_type>(__il.size());
2354 if (__n > 0)
2355 {
2356 allocate(__n);
2357 __construct_at_end(__il.begin(), __il.end());
2358 }
2359}
2360
2361template <class _Allocator>
2362vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
2363 : __begin_(0),
2364 __size_(0),
2365 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2366{
2367 size_type __n = static_cast<size_type>(__il.size());
2368 if (__n > 0)
2369 {
2370 allocate(__n);
2371 __construct_at_end(__il.begin(), __il.end());
2372 }
2373}
2374
2375template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002376vector<bool, _Allocator>::~vector()
2377{
2378 if (__begin_ != 0)
2379 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2380#ifdef _LIBCPP_DEBUG
2381 __invalidate_all_iterators();
2382#endif
2383}
2384
2385template <class _Allocator>
2386vector<bool, _Allocator>::vector(const vector& __v)
2387 : __begin_(0),
2388 __size_(0),
2389 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2390{
2391 if (__v.size() > 0)
2392 {
2393 allocate(__v.size());
2394 __construct_at_end(__v.begin(), __v.end());
2395 }
2396}
2397
2398template <class _Allocator>
2399vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
2400 : __begin_(0),
2401 __size_(0),
2402 __cap_alloc_(0, __a)
2403{
2404 if (__v.size() > 0)
2405 {
2406 allocate(__v.size());
2407 __construct_at_end(__v.begin(), __v.end());
2408 }
2409}
2410
2411template <class _Allocator>
2412vector<bool, _Allocator>&
2413vector<bool, _Allocator>::operator=(const vector& __v)
2414{
2415 if (this != &__v)
2416 {
2417 __copy_assign_alloc(__v);
2418 if (__v.__size_)
2419 {
2420 if (__v.__size_ > capacity())
2421 {
2422 deallocate();
2423 allocate(__v.__size_);
2424 }
2425 _STD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
2426 }
2427 __size_ = __v.__size_;
2428 }
2429 return *this;
2430}
2431
Howard Hinnant73d21a42010-09-04 23:28:19 +00002432#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2433
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002434template <class _Allocator>
2435_LIBCPP_INLINE_VISIBILITY inline
2436vector<bool, _Allocator>::vector(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002437 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002438 : __begin_(__v.__begin_),
2439 __size_(__v.__size_),
2440 __cap_alloc_(__v.__cap_alloc_)
2441{
2442 __v.__begin_ = 0;
2443 __v.__size_ = 0;
2444 __v.__cap() = 0;
2445}
2446
2447template <class _Allocator>
2448vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
2449 : __begin_(0),
2450 __size_(0),
2451 __cap_alloc_(0, __a)
2452{
2453 if (__a == allocator_type(__v.__alloc()))
2454 {
2455 this->__begin_ = __v.__begin_;
2456 this->__size_ = __v.__size_;
2457 this->__cap() = __v.__cap();
2458 __v.__begin_ = nullptr;
2459 __v.__cap() = __v.__size_ = 0;
2460 }
2461 else if (__v.size() > 0)
2462 {
2463 allocate(__v.size());
2464 __construct_at_end(__v.begin(), __v.end());
2465 }
2466}
2467
2468template <class _Allocator>
2469_LIBCPP_INLINE_VISIBILITY inline
2470vector<bool, _Allocator>&
2471vector<bool, _Allocator>::operator=(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002472 _NOEXCEPT_(
2473 __alloc_traits::propagate_on_container_move_assignment::value &&
2474 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002475{
2476 __move_assign(__v, integral_constant<bool,
2477 __storage_traits::propagate_on_container_move_assignment::value>());
2478}
2479
2480template <class _Allocator>
2481void
2482vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2483{
2484 if (__alloc() != __c.__alloc())
2485 assign(__c.begin(), __c.end());
2486 else
2487 __move_assign(__c, true_type());
2488}
2489
2490template <class _Allocator>
2491void
2492vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002493 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002494{
2495 deallocate();
2496 this->__begin_ = __c.__begin_;
2497 this->__size_ = __c.__size_;
2498 this->__cap() = __c.__cap();
2499 __move_assign_alloc(__c);
2500 __c.__begin_ = nullptr;
2501 __c.__cap() = __c.__size_ = 0;
2502}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002503
2504#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002505
2506template <class _Allocator>
2507void
2508vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2509{
2510 __size_ = 0;
2511 if (__n > 0)
2512 {
2513 size_type __c = capacity();
2514 if (__n <= __c)
2515 __size_ = __n;
2516 else
2517 {
2518 vector __v(__alloc());
2519 __v.reserve(__recommend(__n));
2520 __v.__size_ = __n;
2521 swap(__v);
2522 }
2523 _STD::fill_n(begin(), __n, __x);
2524 }
2525}
2526
2527template <class _Allocator>
2528template <class _InputIterator>
2529typename enable_if
2530<
2531 __is_input_iterator<_InputIterator>::value &&
2532 !__is_forward_iterator<_InputIterator>::value,
2533 void
2534>::type
2535vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2536{
2537 clear();
2538 for (; __first != __last; ++__first)
2539 push_back(*__first);
2540}
2541
2542template <class _Allocator>
2543template <class _ForwardIterator>
2544typename enable_if
2545<
2546 __is_forward_iterator<_ForwardIterator>::value,
2547 void
2548>::type
2549vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2550{
2551 clear();
2552 difference_type __n = _STD::distance(__first, __last);
2553 if (__n)
2554 {
2555 if (__n > capacity())
2556 {
2557 deallocate();
2558 allocate(__n);
2559 }
2560 __construct_at_end(__first, __last);
2561 }
2562}
2563
2564template <class _Allocator>
2565void
2566vector<bool, _Allocator>::reserve(size_type __n)
2567{
2568 if (__n > capacity())
2569 {
2570 vector __v(this->__alloc());
2571 __v.allocate(__n);
2572 __v.__construct_at_end(this->begin(), this->end());
2573 swap(__v);
2574 __invalidate_all_iterators();
2575 }
2576}
2577
2578template <class _Allocator>
2579void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002580vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002581{
2582 if (__external_cap_to_internal(size()) > __cap())
2583 {
2584#ifndef _LIBCPP_NO_EXCEPTIONS
2585 try
2586 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002587#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002588 vector(*this, allocator_type(__alloc())).swap(*this);
2589#ifndef _LIBCPP_NO_EXCEPTIONS
2590 }
2591 catch (...)
2592 {
2593 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002594#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002595 }
2596}
2597
2598template <class _Allocator>
2599typename vector<bool, _Allocator>::reference
2600vector<bool, _Allocator>::at(size_type __n)
2601{
2602 if (__n >= size())
2603 this->__throw_out_of_range();
2604 return (*this)[__n];
2605}
2606
2607template <class _Allocator>
2608typename vector<bool, _Allocator>::const_reference
2609vector<bool, _Allocator>::at(size_type __n) const
2610{
2611 if (__n >= size())
2612 this->__throw_out_of_range();
2613 return (*this)[__n];
2614}
2615
2616template <class _Allocator>
2617void
2618vector<bool, _Allocator>::push_back(const value_type& __x)
2619{
2620 if (this->__size_ == this->capacity())
2621 reserve(__recommend(this->__size_ + 1));
2622 ++this->__size_;
2623 back() = __x;
2624}
2625
2626template <class _Allocator>
2627typename vector<bool, _Allocator>::iterator
2628vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2629{
2630 iterator __r;
2631 if (size() < capacity())
2632 {
2633 const_iterator __old_end = end();
2634 ++__size_;
2635 _STD::copy_backward(__position, __old_end, end());
2636 __r = __const_iterator_cast(__position);
2637 }
2638 else
2639 {
2640 vector __v(__alloc());
2641 __v.reserve(__recommend(__size_ + 1));
2642 __v.__size_ = __size_ + 1;
2643 __r = _STD::copy(cbegin(), __position, __v.begin());
2644 _STD::copy_backward(__position, cend(), __v.end());
2645 swap(__v);
2646 }
2647 *__r = __x;
2648 return __r;
2649}
2650
2651template <class _Allocator>
2652typename vector<bool, _Allocator>::iterator
2653vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
2654{
2655 iterator __r;
2656 size_type __c = capacity();
2657 if (__n <= __c && size() <= __c - __n)
2658 {
2659 const_iterator __old_end = end();
2660 __size_ += __n;
2661 _STD::copy_backward(__position, __old_end, end());
2662 __r = __const_iterator_cast(__position);
2663 }
2664 else
2665 {
2666 vector __v(__alloc());
2667 __v.reserve(__recommend(__size_ + __n));
2668 __v.__size_ = __size_ + __n;
2669 __r = _STD::copy(cbegin(), __position, __v.begin());
2670 _STD::copy_backward(__position, cend(), __v.end());
2671 swap(__v);
2672 }
2673 _STD::fill_n(__r, __n, __x);
2674 return __r;
2675}
2676
2677template <class _Allocator>
2678template <class _InputIterator>
2679typename enable_if
2680<
2681 __is_input_iterator <_InputIterator>::value &&
2682 !__is_forward_iterator<_InputIterator>::value,
2683 typename vector<bool, _Allocator>::iterator
2684>::type
2685vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
2686{
2687 difference_type __off = __position - begin();
2688 iterator __p = __const_iterator_cast(__position);
2689 iterator __old_end = end();
2690 for (; size() != capacity() && __first != __last; ++__first)
2691 {
2692 ++this->__size_;
2693 back() = *__first;
2694 }
2695 vector __v(__alloc());
2696 if (__first != __last)
2697 {
2698#ifndef _LIBCPP_NO_EXCEPTIONS
2699 try
2700 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002701#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002702 __v.assign(__first, __last);
2703 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
2704 difference_type __old_p = __p - begin();
2705 reserve(__recommend(size() + __v.size()));
2706 __p = begin() + __old_p;
2707 __old_end = begin() + __old_size;
2708#ifndef _LIBCPP_NO_EXCEPTIONS
2709 }
2710 catch (...)
2711 {
2712 erase(__old_end, end());
2713 throw;
2714 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002715#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002716 }
2717 __p = _STD::rotate(__p, __old_end, end());
2718 insert(__p, __v.begin(), __v.end());
2719 return begin() + __off;
2720}
2721
2722template <class _Allocator>
2723template <class _ForwardIterator>
2724typename enable_if
2725<
2726 __is_forward_iterator<_ForwardIterator>::value,
2727 typename vector<bool, _Allocator>::iterator
2728>::type
2729vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
2730{
2731 difference_type __n = _STD::distance(__first, __last);
2732 iterator __r;
2733 size_type __c = capacity();
2734 if (__n <= __c && size() <= __c - __n)
2735 {
2736 const_iterator __old_end = end();
2737 __size_ += __n;
2738 _STD::copy_backward(__position, __old_end, end());
2739 __r = __const_iterator_cast(__position);
2740 }
2741 else
2742 {
2743 vector __v(__alloc());
2744 __v.reserve(__recommend(__size_ + __n));
2745 __v.__size_ = __size_ + __n;
2746 __r = _STD::copy(cbegin(), __position, __v.begin());
2747 _STD::copy_backward(__position, cend(), __v.end());
2748 swap(__v);
2749 }
2750 _STD::copy(__first, __last, __r);
2751 return __r;
2752}
2753
2754template <class _Allocator>
2755_LIBCPP_INLINE_VISIBILITY inline
2756typename vector<bool, _Allocator>::iterator
2757vector<bool, _Allocator>::erase(const_iterator __position)
2758{
2759 iterator __r = __const_iterator_cast(__position);
2760 _STD::copy(__position + 1, this->cend(), __r);
2761 --__size_;
2762 return __r;
2763}
2764
2765template <class _Allocator>
2766typename vector<bool, _Allocator>::iterator
2767vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
2768{
2769 iterator __r = __const_iterator_cast(__first);
2770 difference_type __d = __last - __first;
2771 _STD::copy(__last, this->cend(), __r);
2772 __size_ -= __d;
2773 return __r;
2774}
2775
2776template <class _Allocator>
2777void
2778vector<bool, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002779 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2780 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002781{
2782 _STD::swap(this->__begin_, __x.__begin_);
2783 _STD::swap(this->__size_, __x.__size_);
2784 _STD::swap(this->__cap(), __x.__cap());
2785 __swap_alloc(this->__alloc(), __x.__alloc());
2786#ifdef _LIBCPP_DEBUG
2787 iterator::swap(this, &__x);
2788 const_iterator::swap(this, &__x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002789#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002790}
2791
Howard Hinnant324bb032010-08-22 00:02:43 +00002792template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002793void
2794vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
2795{
2796 size_type __cs = size();
2797 if (__cs < __sz)
2798 {
2799 iterator __r;
2800 size_type __c = capacity();
2801 size_type __n = __sz - __cs;
2802 if (__n <= __c && __cs <= __c - __n)
2803 {
2804 __r = end();
2805 __size_ += __n;
2806 }
2807 else
2808 {
2809 vector __v(__alloc());
2810 __v.reserve(__recommend(__size_ + __n));
2811 __v.__size_ = __size_ + __n;
2812 __r = _STD::copy(cbegin(), cend(), __v.begin());
2813 swap(__v);
2814 }
2815 _STD::fill_n(__r, __n, __x);
2816 }
2817 else
2818 __size_ = __sz;
2819}
2820
Howard Hinnant324bb032010-08-22 00:02:43 +00002821template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002822void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002823vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002824{
2825 // do middle whole words
2826 size_type __n = __size_;
2827 __storage_pointer __p = __begin_;
2828 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
2829 *__p = ~*__p;
2830 // do last partial word
2831 if (__n > 0)
2832 {
2833 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
2834 __storage_type __b = *__p & __m;
2835 *__p &= ~__m;
2836 *__p |= ~__b & __m;
2837 }
2838}
2839
Howard Hinnant324bb032010-08-22 00:02:43 +00002840template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002841bool
2842vector<bool, _Allocator>::__invariants() const
2843{
2844 if (this->__begin_ == 0)
2845 {
2846 if (this->__size_ != 0 || this->__cap() != 0)
2847 return false;
2848 }
2849 else
2850 {
2851 if (this->__cap() == 0)
2852 return false;
2853 if (this->__size_ > this->capacity())
2854 return false;
2855 }
2856 return true;
2857}
2858
Howard Hinnant324bb032010-08-22 00:02:43 +00002859template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002860size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002861vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002862{
2863 size_t __h = 0;
2864 // do middle whole words
2865 size_type __n = __size_;
2866 __storage_pointer __p = __begin_;
2867 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
2868 __h ^= *__p;
2869 // do last partial word
2870 if (__n > 0)
2871 {
2872 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
2873 __h ^= *__p & __m;
2874 }
2875 return __h;
2876}
2877
2878template <class _Allocator>
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002879struct _LIBCPP_VISIBLE hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002880 : public unary_function<vector<bool, _Allocator>, size_t>
2881{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002882 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002883 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002884 {return __vec.__hash_code();}
2885};
2886
2887template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002888_LIBCPP_INLINE_VISIBILITY inline
2889bool
2890operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2891{
2892 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
2893 return __sz == __y.size() && _STD::equal(__x.begin(), __x.end(), __y.begin());
2894}
2895
2896template <class _Tp, class _Allocator>
2897_LIBCPP_INLINE_VISIBILITY inline
2898bool
2899operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2900{
2901 return !(__x == __y);
2902}
2903
2904template <class _Tp, class _Allocator>
2905_LIBCPP_INLINE_VISIBILITY inline
2906bool
2907operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2908{
2909 return _STD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
2910}
2911
2912template <class _Tp, class _Allocator>
2913_LIBCPP_INLINE_VISIBILITY inline
2914bool
2915operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2916{
2917 return __y < __x;
2918}
2919
2920template <class _Tp, class _Allocator>
2921_LIBCPP_INLINE_VISIBILITY inline
2922bool
2923operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2924{
2925 return !(__x < __y);
2926}
2927
2928template <class _Tp, class _Allocator>
2929_LIBCPP_INLINE_VISIBILITY inline
2930bool
2931operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2932{
2933 return !(__y < __x);
2934}
2935
2936template <class _Tp, class _Allocator>
2937_LIBCPP_INLINE_VISIBILITY inline
2938void
2939swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002940 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002941{
2942 __x.swap(__y);
2943}
2944
2945_LIBCPP_END_NAMESPACE_STD
2946
2947#endif // _LIBCPP_VECTOR