blob: d8c9feb4e69ff2ef84b57f8b4e4aa8cf364792c9 [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 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000408 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409 }
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 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000420 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000421 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 Hinnant0949eed2011-06-30 21:18:19 +0000510 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
511 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000512
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 Hinnant0949eed2011-06-30 21:18:19 +0000645 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000646 _LIBCPP_INLINE_VISIBILITY
647 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000648 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000649
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 Hinnant0949eed2011-06-30 21:18:19 +0000738 __v.push_front(_VSTD::move_if_noexcept(*--__p));
739 _VSTD::swap(this->__begin_, __v.__begin_);
740 _VSTD::swap(this->__end_, __v.__end_);
741 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000742 __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 Hinnant0949eed2011-06-30 21:18:19 +0000752 __v.push_front(_VSTD::move_if_noexcept(*--__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000753 for (pointer __i = __p; __i < this->__end_; ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000754 __v.push_back(_VSTD::move_if_noexcept(*__i));
755 _VSTD::swap(this->__begin_, __v.__begin_);
756 _VSTD::swap(this->__end_, __v.__end_);
757 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000758 __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{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000797 return _VSTD::min(__alloc_traits::max_size(this->__alloc()), numeric_limits<size_type>::max() / 2); // end() >= begin(), always
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000798}
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;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000812 return _VSTD::max(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000813}
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 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000827 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000828 ++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 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000847 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000848 ++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 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000865 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000866 ++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 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000877 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
878 _VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000879 ++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{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000978 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000979 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{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000992 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000993 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 Hinnant0949eed2011-06-30 21:18:19 +00001030 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001031{
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{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001160 typename iterator_traits<_ForwardIterator>::difference_type __new_size = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001161 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;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001169 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001170 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001171 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001172 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 Hinnant0949eed2011-06-30 21:18:19 +00001192 _VSTD::fill_n(this->__begin_, _VSTD::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(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001343 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001344 ++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(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001364 _VSTD::__to_raw_pointer(this->__end_),
1365 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001366 ++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);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001372 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001373 __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(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001387 _VSTD::__to_raw_pointer(this->__end_),
1388 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001389 ++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);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001395 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001396 __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);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001418 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001419 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);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001428 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001429 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(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001440 _VSTD::__to_raw_pointer(this->__end_),
1441 _VSTD::move(*__i));
1442 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001443}
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(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001455 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001456 ++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(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001489 _VSTD::__to_raw_pointer(this->__end_),
1490 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491 ++this->__end_;
1492 }
1493 else
1494 {
1495 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001496 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001497 }
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);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001503 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504 __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(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001522 _VSTD::__to_raw_pointer(this->__end_),
1523 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001524 ++this->__end_;
1525 }
1526 else
1527 {
1528 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001529 *__p = value_type(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530 }
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);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001536 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001537 __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;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001568 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001569 }
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 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001598 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001599 *__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 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001624 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001625 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());
Howard Hinnant0949eed2011-06-30 21:18:19 +00001640 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001641 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;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001652 _VSTD::advance(__m, this->__end_ - __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001653 __construct_at_end(__m, __last);
1654 __n = __dx;
1655 }
1656 if (__n > 0)
1657 {
1658 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001659 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001660 }
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{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001701 _VSTD::swap(this->__begin_, __x.__begin_);
1702 _VSTD::swap(this->__end_, __x.__end_);
1703 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001704 __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 Hinnantf03c3b42011-07-02 20:33:23 +00001752struct __has_storage_type<vector<bool, _Allocator> >
1753{
1754 static const bool value = true;
1755};
1756
1757template <class _Allocator>
Howard Hinnant36cdf022010-09-10 16:42:26 +00001758class _LIBCPP_VISIBLE vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001759 : private __vector_base_common<true>
1760{
1761public:
1762 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00001763 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001764 typedef _Allocator allocator_type;
1765 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001766 typedef typename __alloc_traits::size_type size_type;
1767 typedef typename __alloc_traits::difference_type difference_type;
1768 typedef __bit_iterator<vector, false> pointer;
1769 typedef __bit_iterator<vector, true> const_pointer;
1770#ifdef _LIBCPP_DEBUG
1771 typedef __debug_iter<vector, pointer> iterator;
1772 typedef __debug_iter<vector, const_pointer> const_iterator;
1773
1774 friend class __debug_iter<vector, pointer>;
1775 friend class __debug_iter<vector, const_pointer>;
1776
1777 pair<iterator*, const_iterator*> __iterator_list_;
1778
1779 _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;}
1780 _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;}
Howard Hinnant324bb032010-08-22 00:02:43 +00001781#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001782 typedef pointer iterator;
1783 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +00001784#endif // _LIBCPP_DEBUG
Howard Hinnant0949eed2011-06-30 21:18:19 +00001785 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1786 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001787
1788private:
1789 typedef size_type __storage_type;
1790 typedef typename __alloc_traits::template
1791#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1792 rebind_alloc<__storage_type>
1793#else
1794 rebind_alloc<__storage_type>::other
1795#endif
1796 __storage_allocator;
1797 typedef allocator_traits<__storage_allocator> __storage_traits;
1798 typedef typename __storage_traits::pointer __storage_pointer;
1799 typedef typename __storage_traits::const_pointer __const_storage_pointer;
1800
1801 __storage_pointer __begin_;
1802 size_type __size_;
1803 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
1804
Howard Hinnantf03c3b42011-07-02 20:33:23 +00001805 typedef __bit_reference<vector> reference;
1806 typedef __bit_const_reference<vector> const_reference;
1807
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001808 _LIBCPP_INLINE_VISIBILITY
1809 size_type& __cap() _NOEXCEPT
1810 {return __cap_alloc_.first();}
1811 _LIBCPP_INLINE_VISIBILITY
1812 const size_type& __cap() const _NOEXCEPT
1813 {return __cap_alloc_.first();}
1814 _LIBCPP_INLINE_VISIBILITY
1815 __storage_allocator& __alloc() _NOEXCEPT
1816 {return __cap_alloc_.second();}
1817 _LIBCPP_INLINE_VISIBILITY
1818 const __storage_allocator& __alloc() const _NOEXCEPT
1819 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001820
1821 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
1822
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001823 _LIBCPP_INLINE_VISIBILITY
1824 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001825 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001826 _LIBCPP_INLINE_VISIBILITY
1827 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001828 {return (__n - 1) / __bits_per_word + 1;}
1829
1830public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001831 _LIBCPP_INLINE_VISIBILITY
1832 vector()
1833 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001834 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001835 ~vector();
1836 explicit vector(size_type __n);
1837 vector(size_type __n, const value_type& __v);
1838 vector(size_type __n, const value_type& __v, const allocator_type& __a);
1839 template <class _InputIterator>
1840 vector(_InputIterator __first, _InputIterator __last,
1841 typename enable_if<__is_input_iterator <_InputIterator>::value &&
1842 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
1843 template <class _InputIterator>
1844 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1845 typename enable_if<__is_input_iterator <_InputIterator>::value &&
1846 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
1847 template <class _ForwardIterator>
1848 vector(_ForwardIterator __first, _ForwardIterator __last,
1849 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
1850 template <class _ForwardIterator>
1851 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
1852 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
1853
1854 vector(const vector& __v);
1855 vector(const vector& __v, const allocator_type& __a);
1856 vector& operator=(const vector& __v);
1857 vector(initializer_list<value_type> __il);
1858 vector(initializer_list<value_type> __il, const allocator_type& __a);
1859
Howard Hinnant73d21a42010-09-04 23:28:19 +00001860#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001861 _LIBCPP_INLINE_VISIBILITY
1862 vector(vector&& __v)
1863 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001864 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001865 _LIBCPP_INLINE_VISIBILITY
1866 vector& operator=(vector&& __v)
1867 _NOEXCEPT_(
1868 __alloc_traits::propagate_on_container_move_assignment::value &&
1869 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001870#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001872 vector& operator=(initializer_list<value_type> __il)
1873 {assign(__il.begin(), __il.end()); return *this;}
1874
1875 template <class _InputIterator>
1876 typename enable_if
1877 <
1878 __is_input_iterator<_InputIterator>::value &&
1879 !__is_forward_iterator<_InputIterator>::value,
1880 void
1881 >::type
1882 assign(_InputIterator __first, _InputIterator __last);
1883 template <class _ForwardIterator>
1884 typename enable_if
1885 <
1886 __is_forward_iterator<_ForwardIterator>::value,
1887 void
1888 >::type
1889 assign(_ForwardIterator __first, _ForwardIterator __last);
1890
1891 void assign(size_type __n, const value_type& __x);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001892 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001893 void assign(initializer_list<value_type> __il)
1894 {assign(__il.begin(), __il.end());}
1895
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001896 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001897 {return allocator_type(this->__alloc());}
1898
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001899 size_type max_size() const _NOEXCEPT;
1900 _LIBCPP_INLINE_VISIBILITY
1901 size_type capacity() const _NOEXCEPT
1902 {return __internal_cap_to_external(__cap());}
1903 _LIBCPP_INLINE_VISIBILITY
1904 size_type size() const _NOEXCEPT
1905 {return __size_;}
1906 _LIBCPP_INLINE_VISIBILITY
1907 bool empty() const _NOEXCEPT
1908 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001909 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001910 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001911
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001912 _LIBCPP_INLINE_VISIBILITY
1913 iterator begin() _NOEXCEPT
1914 {return __make_iter(0);}
1915 _LIBCPP_INLINE_VISIBILITY
1916 const_iterator begin() const _NOEXCEPT
1917 {return __make_iter(0);}
1918 _LIBCPP_INLINE_VISIBILITY
1919 iterator end() _NOEXCEPT
1920 {return __make_iter(__size_);}
1921 _LIBCPP_INLINE_VISIBILITY
1922 const_iterator end() const _NOEXCEPT
1923 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001924
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001925 _LIBCPP_INLINE_VISIBILITY
1926 reverse_iterator rbegin() _NOEXCEPT
1927 {return reverse_iterator(end());}
1928 _LIBCPP_INLINE_VISIBILITY
1929 const_reverse_iterator rbegin() const _NOEXCEPT
1930 {return const_reverse_iterator(end());}
1931 _LIBCPP_INLINE_VISIBILITY
1932 reverse_iterator rend() _NOEXCEPT
1933 {return reverse_iterator(begin());}
1934 _LIBCPP_INLINE_VISIBILITY
1935 const_reverse_iterator rend() const _NOEXCEPT
1936 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001937
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001938 _LIBCPP_INLINE_VISIBILITY
1939 const_iterator cbegin() const _NOEXCEPT
1940 {return __make_iter(0);}
1941 _LIBCPP_INLINE_VISIBILITY
1942 const_iterator cend() const _NOEXCEPT
1943 {return __make_iter(__size_);}
1944 _LIBCPP_INLINE_VISIBILITY
1945 const_reverse_iterator crbegin() const _NOEXCEPT
1946 {return rbegin();}
1947 _LIBCPP_INLINE_VISIBILITY
1948 const_reverse_iterator crend() const _NOEXCEPT
1949 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001950
1951 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
1952 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
1953 reference at(size_type __n);
1954 const_reference at(size_type __n) const;
1955
1956 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
1957 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
1958 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
1959 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
1960
1961 void push_back(const value_type& __x);
1962 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
1963
1964 iterator insert(const_iterator __position, const value_type& __x);
1965 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
1966 iterator insert(const_iterator __position, size_type __n, const_reference __x);
1967 template <class _InputIterator>
1968 typename enable_if
1969 <
1970 __is_input_iterator <_InputIterator>::value &&
1971 !__is_forward_iterator<_InputIterator>::value,
1972 iterator
1973 >::type
1974 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
1975 template <class _ForwardIterator>
1976 typename enable_if
1977 <
1978 __is_forward_iterator<_ForwardIterator>::value,
1979 iterator
1980 >::type
1981 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001983 iterator insert(const_iterator __position, initializer_list<value_type> __il)
1984 {return insert(__position, __il.begin(), __il.end());}
1985
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001986 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001987 iterator erase(const_iterator __first, const_iterator __last);
1988
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001989 _LIBCPP_INLINE_VISIBILITY
1990 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001991
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001992 void swap(vector&)
1993 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1994 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001995
1996 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001997 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001998
1999 bool __invariants() const;
2000
2001private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002002 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002003 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002004 void deallocate() _NOEXCEPT;
2005 _LIBCPP_INLINE_VISIBILITY
2006 static size_type __align(size_type __new_size) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002007 {return __new_size + (__bits_per_word-1) & ~(__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002008 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2009 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002010 template <class _ForwardIterator>
2011 typename enable_if
2012 <
2013 __is_forward_iterator<_ForwardIterator>::value,
2014 void
2015 >::type
2016 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2017 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002018 _LIBCPP_INLINE_VISIBILITY
2019 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002020 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002021 _LIBCPP_INLINE_VISIBILITY
2022 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002023 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
2024#ifdef _LIBCPP_DEBUG
2025 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_type __pos)
2026 {return iterator(this, pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2027 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_type __pos) const
2028 {return const_iterator(this, const_pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2029 _LIBCPP_INLINE_VISIBILITY iterator __const_iterator_cast(const_iterator __p)
2030 {return iterator(this, pointer(const_cast<__storage_pointer>(__p.base().__seg_), __p.base().__ctz_));}
Howard Hinnant324bb032010-08-22 00:02:43 +00002031#else // _LIBCPP_DEBUG
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002032 _LIBCPP_INLINE_VISIBILITY
2033 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002034 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002035 _LIBCPP_INLINE_VISIBILITY
2036 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002037 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002038 _LIBCPP_INLINE_VISIBILITY
2039 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002040 {return iterator(const_cast<__storage_pointer>(__p.__seg_), __p.__ctz_);}
Howard Hinnant324bb032010-08-22 00:02:43 +00002041#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002042
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002043 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002044 void __copy_assign_alloc(const vector& __v)
2045 {__copy_assign_alloc(__v, integral_constant<bool,
2046 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002047 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002048 void __copy_assign_alloc(const vector& __c, true_type)
2049 {
2050 if (__alloc() != __c.__alloc())
2051 deallocate();
2052 __alloc() = __c.__alloc();
2053 }
2054
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002055 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002056 void __copy_assign_alloc(const vector& __c, false_type)
2057 {}
2058
2059 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002060 void __move_assign(vector& __c, true_type)
2061 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002063 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002064 _NOEXCEPT_(
2065 !__storage_traits::propagate_on_container_move_assignment::value ||
2066 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002067 {__move_assign_alloc(__c, integral_constant<bool,
2068 __storage_traits::propagate_on_container_move_assignment::value>());}
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, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002071 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002072 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002073 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002074 }
2075
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002076 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002077 void __move_assign_alloc(const vector& __c, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002078 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002079 {}
2080
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002081 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002082 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002083 _NOEXCEPT_(
2084 !__storage_traits::propagate_on_container_swap::value ||
2085 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002086 {__swap_alloc(__x, __y, integral_constant<bool,
2087 __storage_traits::propagate_on_container_swap::value>());}
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, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002091 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002092 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002093 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002094 swap(__x, __y);
2095 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002096 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002097 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002098 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002099 {}
2100
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002101 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002102
2103 friend class __bit_reference<vector>;
2104 friend class __bit_const_reference<vector>;
2105 friend class __bit_iterator<vector, false>;
2106 friend class __bit_iterator<vector, true>;
2107 friend class __bit_array<vector>;
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002108 friend struct _LIBCPP_VISIBLE hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002109};
2110
2111template <class _Allocator>
2112#ifndef _LIBCPP_DEBUG
2113_LIBCPP_INLINE_VISIBILITY inline
2114#endif
2115void
2116vector<bool, _Allocator>::__invalidate_all_iterators()
2117{
2118#ifdef _LIBCPP_DEBUG
2119 iterator::__remove_all(this);
2120 const_iterator::__remove_all(this);
Howard Hinnant324bb032010-08-22 00:02:43 +00002121#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002122}
2123
2124// Allocate space for __n objects
2125// throws length_error if __n > max_size()
2126// throws (probably bad_alloc) if memory run out
2127// Precondition: __begin_ == __end_ == __cap() == 0
2128// Precondition: __n > 0
2129// Postcondition: capacity() == __n
2130// Postcondition: size() == 0
2131template <class _Allocator>
2132void
2133vector<bool, _Allocator>::allocate(size_type __n)
2134{
2135 if (__n > max_size())
2136 this->__throw_length_error();
2137 __n = __external_cap_to_internal(__n);
2138 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2139 this->__size_ = 0;
2140 this->__cap() = __n;
2141}
2142
2143template <class _Allocator>
2144void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002145vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002146{
2147 if (this->__begin_ != 0)
2148 {
2149 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2150 __invalidate_all_iterators();
2151 this->__begin_ = 0;
2152 this->__size_ = this->__cap() = 0;
2153 }
2154}
2155
2156template <class _Allocator>
2157typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002158vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002159{
2160 size_type __amax = __storage_traits::max_size(__alloc());
2161 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2162 if (__nmax / __bits_per_word <= __amax)
2163 return __nmax;
2164 return __internal_cap_to_external(__amax);
2165}
2166
2167// Precondition: __new_size > capacity()
2168template <class _Allocator>
2169_LIBCPP_INLINE_VISIBILITY inline
2170typename vector<bool, _Allocator>::size_type
2171vector<bool, _Allocator>::__recommend(size_type __new_size) const
2172{
2173 const size_type __ms = max_size();
2174 if (__new_size > __ms)
2175 this->__throw_length_error();
2176 const size_type __cap = capacity();
2177 if (__cap >= __ms / 2)
2178 return __ms;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002179 return _VSTD::max(2*__cap, __align(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002180}
2181
2182// Default constructs __n objects starting at __end_
2183// Precondition: __n > 0
2184// Precondition: size() + __n <= capacity()
2185// Postcondition: size() == size() + __n
2186template <class _Allocator>
2187_LIBCPP_INLINE_VISIBILITY inline
2188void
2189vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2190{
2191 size_type __old_size = this->__size_;
2192 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002193 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002194}
2195
2196template <class _Allocator>
2197template <class _ForwardIterator>
2198typename enable_if
2199<
2200 __is_forward_iterator<_ForwardIterator>::value,
2201 void
2202>::type
2203vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2204{
2205 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002206 this->__size_ += _VSTD::distance(__first, __last);
2207 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002208}
2209
2210template <class _Allocator>
2211_LIBCPP_INLINE_VISIBILITY inline
2212vector<bool, _Allocator>::vector()
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002213 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002214 : __begin_(0),
2215 __size_(0),
2216 __cap_alloc_(0)
2217{
2218}
2219
2220template <class _Allocator>
2221_LIBCPP_INLINE_VISIBILITY inline
2222vector<bool, _Allocator>::vector(const allocator_type& __a)
2223 : __begin_(0),
2224 __size_(0),
2225 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2226{
2227}
2228
2229template <class _Allocator>
2230vector<bool, _Allocator>::vector(size_type __n)
2231 : __begin_(0),
2232 __size_(0),
2233 __cap_alloc_(0)
2234{
2235 if (__n > 0)
2236 {
2237 allocate(__n);
2238 __construct_at_end(__n, false);
2239 }
2240}
2241
2242template <class _Allocator>
2243vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
2244 : __begin_(0),
2245 __size_(0),
2246 __cap_alloc_(0)
2247{
2248 if (__n > 0)
2249 {
2250 allocate(__n);
2251 __construct_at_end(__n, __x);
2252 }
2253}
2254
2255template <class _Allocator>
2256vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
2257 : __begin_(0),
2258 __size_(0),
2259 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2260{
2261 if (__n > 0)
2262 {
2263 allocate(__n);
2264 __construct_at_end(__n, __x);
2265 }
2266}
2267
2268template <class _Allocator>
2269template <class _InputIterator>
2270vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2271 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2272 !__is_forward_iterator<_InputIterator>::value>::type*)
2273 : __begin_(0),
2274 __size_(0),
2275 __cap_alloc_(0)
2276{
2277#ifndef _LIBCPP_NO_EXCEPTIONS
2278 try
2279 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002280#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002281 for (; __first != __last; ++__first)
2282 push_back(*__first);
2283#ifndef _LIBCPP_NO_EXCEPTIONS
2284 }
2285 catch (...)
2286 {
2287 if (__begin_ != 0)
2288 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2289 __invalidate_all_iterators();
2290 throw;
2291 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002292#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002293}
2294
2295template <class _Allocator>
2296template <class _InputIterator>
2297vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2298 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2299 !__is_forward_iterator<_InputIterator>::value>::type*)
2300 : __begin_(0),
2301 __size_(0),
2302 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2303{
2304#ifndef _LIBCPP_NO_EXCEPTIONS
2305 try
2306 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002307#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002308 for (; __first != __last; ++__first)
2309 push_back(*__first);
2310#ifndef _LIBCPP_NO_EXCEPTIONS
2311 }
2312 catch (...)
2313 {
2314 if (__begin_ != 0)
2315 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2316 __invalidate_all_iterators();
2317 throw;
2318 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002319#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002320}
2321
2322template <class _Allocator>
2323template <class _ForwardIterator>
2324vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2325 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2326 : __begin_(0),
2327 __size_(0),
2328 __cap_alloc_(0)
2329{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002330 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002331 if (__n > 0)
2332 {
2333 allocate(__n);
2334 __construct_at_end(__first, __last);
2335 }
2336}
2337
2338template <class _Allocator>
2339template <class _ForwardIterator>
2340vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2341 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2342 : __begin_(0),
2343 __size_(0),
2344 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2345{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002346 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002347 if (__n > 0)
2348 {
2349 allocate(__n);
2350 __construct_at_end(__first, __last);
2351 }
2352}
2353
2354template <class _Allocator>
2355vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
2356 : __begin_(0),
2357 __size_(0),
2358 __cap_alloc_(0)
2359{
2360 size_type __n = static_cast<size_type>(__il.size());
2361 if (__n > 0)
2362 {
2363 allocate(__n);
2364 __construct_at_end(__il.begin(), __il.end());
2365 }
2366}
2367
2368template <class _Allocator>
2369vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
2370 : __begin_(0),
2371 __size_(0),
2372 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2373{
2374 size_type __n = static_cast<size_type>(__il.size());
2375 if (__n > 0)
2376 {
2377 allocate(__n);
2378 __construct_at_end(__il.begin(), __il.end());
2379 }
2380}
2381
2382template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002383vector<bool, _Allocator>::~vector()
2384{
2385 if (__begin_ != 0)
2386 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2387#ifdef _LIBCPP_DEBUG
2388 __invalidate_all_iterators();
2389#endif
2390}
2391
2392template <class _Allocator>
2393vector<bool, _Allocator>::vector(const vector& __v)
2394 : __begin_(0),
2395 __size_(0),
2396 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2397{
2398 if (__v.size() > 0)
2399 {
2400 allocate(__v.size());
2401 __construct_at_end(__v.begin(), __v.end());
2402 }
2403}
2404
2405template <class _Allocator>
2406vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
2407 : __begin_(0),
2408 __size_(0),
2409 __cap_alloc_(0, __a)
2410{
2411 if (__v.size() > 0)
2412 {
2413 allocate(__v.size());
2414 __construct_at_end(__v.begin(), __v.end());
2415 }
2416}
2417
2418template <class _Allocator>
2419vector<bool, _Allocator>&
2420vector<bool, _Allocator>::operator=(const vector& __v)
2421{
2422 if (this != &__v)
2423 {
2424 __copy_assign_alloc(__v);
2425 if (__v.__size_)
2426 {
2427 if (__v.__size_ > capacity())
2428 {
2429 deallocate();
2430 allocate(__v.__size_);
2431 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002432 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002433 }
2434 __size_ = __v.__size_;
2435 }
2436 return *this;
2437}
2438
Howard Hinnant73d21a42010-09-04 23:28:19 +00002439#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2440
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002441template <class _Allocator>
2442_LIBCPP_INLINE_VISIBILITY inline
2443vector<bool, _Allocator>::vector(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002444 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002445 : __begin_(__v.__begin_),
2446 __size_(__v.__size_),
2447 __cap_alloc_(__v.__cap_alloc_)
2448{
2449 __v.__begin_ = 0;
2450 __v.__size_ = 0;
2451 __v.__cap() = 0;
2452}
2453
2454template <class _Allocator>
2455vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
2456 : __begin_(0),
2457 __size_(0),
2458 __cap_alloc_(0, __a)
2459{
2460 if (__a == allocator_type(__v.__alloc()))
2461 {
2462 this->__begin_ = __v.__begin_;
2463 this->__size_ = __v.__size_;
2464 this->__cap() = __v.__cap();
2465 __v.__begin_ = nullptr;
2466 __v.__cap() = __v.__size_ = 0;
2467 }
2468 else if (__v.size() > 0)
2469 {
2470 allocate(__v.size());
2471 __construct_at_end(__v.begin(), __v.end());
2472 }
2473}
2474
2475template <class _Allocator>
2476_LIBCPP_INLINE_VISIBILITY inline
2477vector<bool, _Allocator>&
2478vector<bool, _Allocator>::operator=(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002479 _NOEXCEPT_(
2480 __alloc_traits::propagate_on_container_move_assignment::value &&
2481 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002482{
2483 __move_assign(__v, integral_constant<bool,
2484 __storage_traits::propagate_on_container_move_assignment::value>());
2485}
2486
2487template <class _Allocator>
2488void
2489vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2490{
2491 if (__alloc() != __c.__alloc())
2492 assign(__c.begin(), __c.end());
2493 else
2494 __move_assign(__c, true_type());
2495}
2496
2497template <class _Allocator>
2498void
2499vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002500 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002501{
2502 deallocate();
2503 this->__begin_ = __c.__begin_;
2504 this->__size_ = __c.__size_;
2505 this->__cap() = __c.__cap();
2506 __move_assign_alloc(__c);
2507 __c.__begin_ = nullptr;
2508 __c.__cap() = __c.__size_ = 0;
2509}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002510
2511#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002512
2513template <class _Allocator>
2514void
2515vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2516{
2517 __size_ = 0;
2518 if (__n > 0)
2519 {
2520 size_type __c = capacity();
2521 if (__n <= __c)
2522 __size_ = __n;
2523 else
2524 {
2525 vector __v(__alloc());
2526 __v.reserve(__recommend(__n));
2527 __v.__size_ = __n;
2528 swap(__v);
2529 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002530 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002531 }
2532}
2533
2534template <class _Allocator>
2535template <class _InputIterator>
2536typename enable_if
2537<
2538 __is_input_iterator<_InputIterator>::value &&
2539 !__is_forward_iterator<_InputIterator>::value,
2540 void
2541>::type
2542vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2543{
2544 clear();
2545 for (; __first != __last; ++__first)
2546 push_back(*__first);
2547}
2548
2549template <class _Allocator>
2550template <class _ForwardIterator>
2551typename enable_if
2552<
2553 __is_forward_iterator<_ForwardIterator>::value,
2554 void
2555>::type
2556vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2557{
2558 clear();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002559 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002560 if (__n)
2561 {
2562 if (__n > capacity())
2563 {
2564 deallocate();
2565 allocate(__n);
2566 }
2567 __construct_at_end(__first, __last);
2568 }
2569}
2570
2571template <class _Allocator>
2572void
2573vector<bool, _Allocator>::reserve(size_type __n)
2574{
2575 if (__n > capacity())
2576 {
2577 vector __v(this->__alloc());
2578 __v.allocate(__n);
2579 __v.__construct_at_end(this->begin(), this->end());
2580 swap(__v);
2581 __invalidate_all_iterators();
2582 }
2583}
2584
2585template <class _Allocator>
2586void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002587vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002588{
2589 if (__external_cap_to_internal(size()) > __cap())
2590 {
2591#ifndef _LIBCPP_NO_EXCEPTIONS
2592 try
2593 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002594#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002595 vector(*this, allocator_type(__alloc())).swap(*this);
2596#ifndef _LIBCPP_NO_EXCEPTIONS
2597 }
2598 catch (...)
2599 {
2600 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002601#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002602 }
2603}
2604
2605template <class _Allocator>
2606typename vector<bool, _Allocator>::reference
2607vector<bool, _Allocator>::at(size_type __n)
2608{
2609 if (__n >= size())
2610 this->__throw_out_of_range();
2611 return (*this)[__n];
2612}
2613
2614template <class _Allocator>
2615typename vector<bool, _Allocator>::const_reference
2616vector<bool, _Allocator>::at(size_type __n) const
2617{
2618 if (__n >= size())
2619 this->__throw_out_of_range();
2620 return (*this)[__n];
2621}
2622
2623template <class _Allocator>
2624void
2625vector<bool, _Allocator>::push_back(const value_type& __x)
2626{
2627 if (this->__size_ == this->capacity())
2628 reserve(__recommend(this->__size_ + 1));
2629 ++this->__size_;
2630 back() = __x;
2631}
2632
2633template <class _Allocator>
2634typename vector<bool, _Allocator>::iterator
2635vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2636{
2637 iterator __r;
2638 if (size() < capacity())
2639 {
2640 const_iterator __old_end = end();
2641 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002642 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002643 __r = __const_iterator_cast(__position);
2644 }
2645 else
2646 {
2647 vector __v(__alloc());
2648 __v.reserve(__recommend(__size_ + 1));
2649 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002650 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2651 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002652 swap(__v);
2653 }
2654 *__r = __x;
2655 return __r;
2656}
2657
2658template <class _Allocator>
2659typename vector<bool, _Allocator>::iterator
2660vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
2661{
2662 iterator __r;
2663 size_type __c = capacity();
2664 if (__n <= __c && size() <= __c - __n)
2665 {
2666 const_iterator __old_end = end();
2667 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002668 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002669 __r = __const_iterator_cast(__position);
2670 }
2671 else
2672 {
2673 vector __v(__alloc());
2674 __v.reserve(__recommend(__size_ + __n));
2675 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002676 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2677 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002678 swap(__v);
2679 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002680 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002681 return __r;
2682}
2683
2684template <class _Allocator>
2685template <class _InputIterator>
2686typename enable_if
2687<
2688 __is_input_iterator <_InputIterator>::value &&
2689 !__is_forward_iterator<_InputIterator>::value,
2690 typename vector<bool, _Allocator>::iterator
2691>::type
2692vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
2693{
2694 difference_type __off = __position - begin();
2695 iterator __p = __const_iterator_cast(__position);
2696 iterator __old_end = end();
2697 for (; size() != capacity() && __first != __last; ++__first)
2698 {
2699 ++this->__size_;
2700 back() = *__first;
2701 }
2702 vector __v(__alloc());
2703 if (__first != __last)
2704 {
2705#ifndef _LIBCPP_NO_EXCEPTIONS
2706 try
2707 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002708#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002709 __v.assign(__first, __last);
2710 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
2711 difference_type __old_p = __p - begin();
2712 reserve(__recommend(size() + __v.size()));
2713 __p = begin() + __old_p;
2714 __old_end = begin() + __old_size;
2715#ifndef _LIBCPP_NO_EXCEPTIONS
2716 }
2717 catch (...)
2718 {
2719 erase(__old_end, end());
2720 throw;
2721 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002722#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002723 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002724 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002725 insert(__p, __v.begin(), __v.end());
2726 return begin() + __off;
2727}
2728
2729template <class _Allocator>
2730template <class _ForwardIterator>
2731typename enable_if
2732<
2733 __is_forward_iterator<_ForwardIterator>::value,
2734 typename vector<bool, _Allocator>::iterator
2735>::type
2736vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
2737{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002738 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002739 iterator __r;
2740 size_type __c = capacity();
2741 if (__n <= __c && size() <= __c - __n)
2742 {
2743 const_iterator __old_end = end();
2744 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002745 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002746 __r = __const_iterator_cast(__position);
2747 }
2748 else
2749 {
2750 vector __v(__alloc());
2751 __v.reserve(__recommend(__size_ + __n));
2752 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002753 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2754 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002755 swap(__v);
2756 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002757 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002758 return __r;
2759}
2760
2761template <class _Allocator>
2762_LIBCPP_INLINE_VISIBILITY inline
2763typename vector<bool, _Allocator>::iterator
2764vector<bool, _Allocator>::erase(const_iterator __position)
2765{
2766 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002767 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002768 --__size_;
2769 return __r;
2770}
2771
2772template <class _Allocator>
2773typename vector<bool, _Allocator>::iterator
2774vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
2775{
2776 iterator __r = __const_iterator_cast(__first);
2777 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002778 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002779 __size_ -= __d;
2780 return __r;
2781}
2782
2783template <class _Allocator>
2784void
2785vector<bool, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002786 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2787 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002788{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002789 _VSTD::swap(this->__begin_, __x.__begin_);
2790 _VSTD::swap(this->__size_, __x.__size_);
2791 _VSTD::swap(this->__cap(), __x.__cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002792 __swap_alloc(this->__alloc(), __x.__alloc());
2793#ifdef _LIBCPP_DEBUG
2794 iterator::swap(this, &__x);
2795 const_iterator::swap(this, &__x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002796#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002797}
2798
Howard Hinnant324bb032010-08-22 00:02:43 +00002799template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002800void
2801vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
2802{
2803 size_type __cs = size();
2804 if (__cs < __sz)
2805 {
2806 iterator __r;
2807 size_type __c = capacity();
2808 size_type __n = __sz - __cs;
2809 if (__n <= __c && __cs <= __c - __n)
2810 {
2811 __r = end();
2812 __size_ += __n;
2813 }
2814 else
2815 {
2816 vector __v(__alloc());
2817 __v.reserve(__recommend(__size_ + __n));
2818 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002819 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002820 swap(__v);
2821 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002822 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002823 }
2824 else
2825 __size_ = __sz;
2826}
2827
Howard Hinnant324bb032010-08-22 00:02:43 +00002828template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002829void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002830vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002831{
2832 // do middle whole words
2833 size_type __n = __size_;
2834 __storage_pointer __p = __begin_;
2835 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
2836 *__p = ~*__p;
2837 // do last partial word
2838 if (__n > 0)
2839 {
2840 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
2841 __storage_type __b = *__p & __m;
2842 *__p &= ~__m;
2843 *__p |= ~__b & __m;
2844 }
2845}
2846
Howard Hinnant324bb032010-08-22 00:02:43 +00002847template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002848bool
2849vector<bool, _Allocator>::__invariants() const
2850{
2851 if (this->__begin_ == 0)
2852 {
2853 if (this->__size_ != 0 || this->__cap() != 0)
2854 return false;
2855 }
2856 else
2857 {
2858 if (this->__cap() == 0)
2859 return false;
2860 if (this->__size_ > this->capacity())
2861 return false;
2862 }
2863 return true;
2864}
2865
Howard Hinnant324bb032010-08-22 00:02:43 +00002866template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002867size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002868vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002869{
2870 size_t __h = 0;
2871 // do middle whole words
2872 size_type __n = __size_;
2873 __storage_pointer __p = __begin_;
2874 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
2875 __h ^= *__p;
2876 // do last partial word
2877 if (__n > 0)
2878 {
2879 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
2880 __h ^= *__p & __m;
2881 }
2882 return __h;
2883}
2884
2885template <class _Allocator>
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002886struct _LIBCPP_VISIBLE hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002887 : public unary_function<vector<bool, _Allocator>, size_t>
2888{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002890 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002891 {return __vec.__hash_code();}
2892};
2893
2894template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002895_LIBCPP_INLINE_VISIBILITY inline
2896bool
2897operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2898{
2899 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002900 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002901}
2902
2903template <class _Tp, class _Allocator>
2904_LIBCPP_INLINE_VISIBILITY inline
2905bool
2906operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2907{
2908 return !(__x == __y);
2909}
2910
2911template <class _Tp, class _Allocator>
2912_LIBCPP_INLINE_VISIBILITY inline
2913bool
2914operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2915{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002916 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002917}
2918
2919template <class _Tp, class _Allocator>
2920_LIBCPP_INLINE_VISIBILITY inline
2921bool
2922operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2923{
2924 return __y < __x;
2925}
2926
2927template <class _Tp, class _Allocator>
2928_LIBCPP_INLINE_VISIBILITY inline
2929bool
2930operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2931{
2932 return !(__x < __y);
2933}
2934
2935template <class _Tp, class _Allocator>
2936_LIBCPP_INLINE_VISIBILITY inline
2937bool
2938operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2939{
2940 return !(__y < __x);
2941}
2942
2943template <class _Tp, class _Allocator>
2944_LIBCPP_INLINE_VISIBILITY inline
2945void
2946swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002947 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002948{
2949 __x.swap(__y);
2950}
2951
2952_LIBCPP_END_NAMESPACE_STD
2953
2954#endif // _LIBCPP_VECTOR