blob: 43a67d8f2edc9937e0dafb25d3ed0ae8cadb58a8 [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 Hinnante3e32912011-08-12 21:56:02 +0000535#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000537 vector(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000539 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000540#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000541#ifdef _LIBCPP_DEBUG
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000543 ~vector() {__invalidate_all_iterators();}
544#endif
545
546 vector(const vector& __x);
547 vector(const vector& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000549 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000550#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000552 vector(vector&& __x)
553 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000555 vector(vector&& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000557 vector& operator=(vector&& __x)
558 _NOEXCEPT_(
559 __alloc_traits::propagate_on_container_move_assignment::value &&
560 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000561#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000562#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000564 vector& operator=(initializer_list<value_type> __il)
565 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000566#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000567
568 template <class _InputIterator>
569 typename enable_if
570 <
571 __is_input_iterator <_InputIterator>::value &&
572 !__is_forward_iterator<_InputIterator>::value,
573 void
574 >::type
575 assign(_InputIterator __first, _InputIterator __last);
576 template <class _ForwardIterator>
577 typename enable_if
578 <
579 __is_forward_iterator<_ForwardIterator>::value,
580 void
581 >::type
582 assign(_ForwardIterator __first, _ForwardIterator __last);
583
584 void assign(size_type __n, const_reference __u);
Howard Hinnante3e32912011-08-12 21:56:02 +0000585#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000586 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000587 void assign(initializer_list<value_type> __il)
588 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000589#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000590
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000591 _LIBCPP_INLINE_VISIBILITY
592 allocator_type get_allocator() const _NOEXCEPT
593 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000594
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000595 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
596 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
597 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
598 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000599
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000600 _LIBCPP_INLINE_VISIBILITY
601 reverse_iterator rbegin() _NOEXCEPT
602 {return reverse_iterator(end());}
603 _LIBCPP_INLINE_VISIBILITY
604 const_reverse_iterator rbegin() const _NOEXCEPT
605 {return const_reverse_iterator(end());}
606 _LIBCPP_INLINE_VISIBILITY
607 reverse_iterator rend() _NOEXCEPT
608 {return reverse_iterator(begin());}
609 _LIBCPP_INLINE_VISIBILITY
610 const_reverse_iterator rend() const _NOEXCEPT
611 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000612
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000613 _LIBCPP_INLINE_VISIBILITY
614 const_iterator cbegin() const _NOEXCEPT
615 {return begin();}
616 _LIBCPP_INLINE_VISIBILITY
617 const_iterator cend() const _NOEXCEPT
618 {return end();}
619 _LIBCPP_INLINE_VISIBILITY
620 const_reverse_iterator crbegin() const _NOEXCEPT
621 {return rbegin();}
622 _LIBCPP_INLINE_VISIBILITY
623 const_reverse_iterator crend() const _NOEXCEPT
624 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000625
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000626 _LIBCPP_INLINE_VISIBILITY
627 size_type size() const _NOEXCEPT
628 {return static_cast<size_type>(this->__end_ - this->__begin_);}
629 _LIBCPP_INLINE_VISIBILITY
630 size_type capacity() const _NOEXCEPT
631 {return __base::capacity();}
632 _LIBCPP_INLINE_VISIBILITY
633 bool empty() const _NOEXCEPT
634 {return this->__begin_ == this->__end_;}
635 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000636 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000637 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000638
639 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
640 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
641 reference at(size_type __n);
642 const_reference at(size_type __n) const;
643
644 _LIBCPP_INLINE_VISIBILITY reference front() {return *this->__begin_;}
645 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return *this->__begin_;}
646 _LIBCPP_INLINE_VISIBILITY reference back() {return *(this->__end_ - 1);}
647 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return *(this->__end_ - 1);}
648
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000649 _LIBCPP_INLINE_VISIBILITY
650 value_type* data() _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000651 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000652 _LIBCPP_INLINE_VISIBILITY
653 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000654 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000655
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000656 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000657#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000658 void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000659#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000660 template <class... _Args>
661 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000662#endif // _LIBCPP_HAS_NO_VARIADICS
663#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664 void pop_back();
665
666 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000667#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000668 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000669#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000670 template <class... _Args>
671 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000672#endif // _LIBCPP_HAS_NO_VARIADICS
673#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000674 iterator insert(const_iterator __position, size_type __n, const_reference __x);
675 template <class _InputIterator>
676 typename enable_if
677 <
678 __is_input_iterator <_InputIterator>::value &&
679 !__is_forward_iterator<_InputIterator>::value,
680 iterator
681 >::type
682 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
683 template <class _ForwardIterator>
684 typename enable_if
685 <
686 __is_forward_iterator<_ForwardIterator>::value,
687 iterator
688 >::type
689 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +0000690#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000691 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000692 iterator insert(const_iterator __position, initializer_list<value_type> __il)
693 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000694#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000695
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000696 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000697 iterator erase(const_iterator __first, const_iterator __last);
698
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000699 _LIBCPP_INLINE_VISIBILITY
700 void clear() _NOEXCEPT
701 {__base::clear();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000702
703 void resize(size_type __sz);
704 void resize(size_type __sz, const_reference __x);
705
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000706 void swap(vector&)
707 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
708 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000709
710 bool __invariants() const;
711
712private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000713 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000714 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000715 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000716 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31 +0000717 void __construct_at_end(size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000718 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000719 template <class _ForwardIterator>
720 typename enable_if
721 <
722 __is_forward_iterator<_ForwardIterator>::value,
723 void
724 >::type
725 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
726 void __move_construct_at_end(pointer __first, pointer __last);
727 void __append(size_type __n);
728 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000730 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000732 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000733 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
734 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
735 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000736 void __move_assign(vector& __c, true_type)
737 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000738 void __move_assign(vector& __c, false_type);
739};
740
741template <class _Tp, class _Allocator>
742void
743vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
744{
745 for (pointer __p = this->__end_; this->__begin_ < __p;)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000746 __v.push_front(_VSTD::move_if_noexcept(*--__p));
747 _VSTD::swap(this->__begin_, __v.__begin_);
748 _VSTD::swap(this->__end_, __v.__end_);
749 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000750 __v.__first_ = __v.__begin_;
751 __invalidate_all_iterators();
752}
753
754template <class _Tp, class _Allocator>
755typename vector<_Tp, _Allocator>::pointer
756vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
757{
758 pointer __r = __v.__begin_;
759 for (pointer __i = __p; this->__begin_ < __i;)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000760 __v.push_front(_VSTD::move_if_noexcept(*--__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000761 for (pointer __i = __p; __i < this->__end_; ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000762 __v.push_back(_VSTD::move_if_noexcept(*__i));
763 _VSTD::swap(this->__begin_, __v.__begin_);
764 _VSTD::swap(this->__end_, __v.__end_);
765 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000766 __v.__first_ = __v.__begin_;
767 __invalidate_all_iterators();
768 return __r;
769}
770
771// Allocate space for __n objects
772// throws length_error if __n > max_size()
773// throws (probably bad_alloc) if memory run out
774// Precondition: __begin_ == __end_ == __end_cap() == 0
775// Precondition: __n > 0
776// Postcondition: capacity() == __n
777// Postcondition: size() == 0
778template <class _Tp, class _Allocator>
779void
780vector<_Tp, _Allocator>::allocate(size_type __n)
781{
782 if (__n > max_size())
783 this->__throw_length_error();
784 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
785 this->__end_cap() = this->__begin_ + __n;
786}
787
788template <class _Tp, class _Allocator>
789void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000790vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000791{
792 if (this->__begin_ != 0)
793 {
794 clear();
795 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
796 __invalidate_all_iterators();
797 this->__begin_ = this->__end_ = this->__end_cap() = 0;
798 }
799}
800
801template <class _Tp, class _Allocator>
802typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000803vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000804{
Sean Hunt110b8bf2011-07-29 23:31:58 +0000805 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()), numeric_limits<size_type>::max() / 2); // end() >= begin(), always
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000806}
807
808// Precondition: __new_size > capacity()
809template <class _Tp, class _Allocator>
810_LIBCPP_INLINE_VISIBILITY inline
811typename vector<_Tp, _Allocator>::size_type
812vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
813{
814 const size_type __ms = max_size();
815 if (__new_size > __ms)
816 this->__throw_length_error();
817 const size_type __cap = capacity();
818 if (__cap >= __ms / 2)
819 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58 +0000820 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000821}
822
823// Default constructs __n objects starting at __end_
824// throws if construction throws
825// Precondition: __n > 0
826// Precondition: size() + __n <= capacity()
827// Postcondition: size() == size() + __n
828template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000829void
830vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
831{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000832 allocator_type& __a = this->__alloc();
833 do
834 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000835 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000836 ++this->__end_;
837 --__n;
838 } while (__n > 0);
839}
840
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000841// Copy constructs __n objects starting at __end_ from __x
842// throws if construction throws
843// Precondition: __n > 0
844// Precondition: size() + __n <= capacity()
845// Postcondition: size() == old size() + __n
846// Postcondition: [i] == __x for all i in [size() - __n, __n)
847template <class _Tp, class _Allocator>
848_LIBCPP_INLINE_VISIBILITY inline
849void
850vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
851{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000852 allocator_type& __a = this->__alloc();
853 do
854 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000855 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000856 ++this->__end_;
857 --__n;
858 } while (__n > 0);
859}
860
861template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000862template <class _ForwardIterator>
863typename enable_if
864<
865 __is_forward_iterator<_ForwardIterator>::value,
866 void
867>::type
868vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
869{
870 allocator_type& __a = this->__alloc();
871 for (; __first != __last; ++__first)
872 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000873 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000874 ++this->__end_;
875 }
876}
877
878template <class _Tp, class _Allocator>
879void
880vector<_Tp, _Allocator>::__move_construct_at_end(pointer __first, pointer __last)
881{
882 allocator_type& __a = this->__alloc();
883 for (; __first != __last; ++__first)
884 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000885 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
886 _VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000887 ++this->__end_;
888 }
889}
890
891// Default constructs __n objects starting at __end_
892// throws if construction throws
893// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000894// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000895template <class _Tp, class _Allocator>
896void
897vector<_Tp, _Allocator>::__append(size_type __n)
898{
899 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
900 this->__construct_at_end(__n);
901 else
902 {
903 allocator_type& __a = this->__alloc();
904 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
905 __v.__construct_at_end(__n);
906 __swap_out_circular_buffer(__v);
907 }
908}
909
910// Default constructs __n objects starting at __end_
911// throws if construction throws
912// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000913// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000914template <class _Tp, class _Allocator>
915void
916vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
917{
918 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
919 this->__construct_at_end(__n, __x);
920 else
921 {
922 allocator_type& __a = this->__alloc();
923 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
924 __v.__construct_at_end(__n, __x);
925 __swap_out_circular_buffer(__v);
926 }
927}
928
929template <class _Tp, class _Allocator>
930vector<_Tp, _Allocator>::vector(size_type __n)
931{
932 if (__n > 0)
933 {
934 allocate(__n);
935 __construct_at_end(__n);
936 }
937}
938
939template <class _Tp, class _Allocator>
940vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
941{
942 if (__n > 0)
943 {
944 allocate(__n);
945 __construct_at_end(__n, __x);
946 }
947}
948
949template <class _Tp, class _Allocator>
950vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
951 : __base(__a)
952{
953 if (__n > 0)
954 {
955 allocate(__n);
956 __construct_at_end(__n, __x);
957 }
958}
959
960template <class _Tp, class _Allocator>
961template <class _InputIterator>
962vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
963 typename enable_if<__is_input_iterator <_InputIterator>::value &&
964 !__is_forward_iterator<_InputIterator>::value>::type*)
965{
966 for (; __first != __last; ++__first)
967 push_back(*__first);
968}
969
970template <class _Tp, class _Allocator>
971template <class _InputIterator>
972vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
973 typename enable_if<__is_input_iterator <_InputIterator>::value &&
974 !__is_forward_iterator<_InputIterator>::value>::type*)
975 : __base(__a)
976{
977 for (; __first != __last; ++__first)
978 push_back(*__first);
979}
980
981template <class _Tp, class _Allocator>
982template <class _ForwardIterator>
983vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
984 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
985{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000986 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000987 if (__n > 0)
988 {
989 allocate(__n);
990 __construct_at_end(__first, __last);
991 }
992}
993
994template <class _Tp, class _Allocator>
995template <class _ForwardIterator>
996vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
997 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
998 : __base(__a)
999{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001000 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001001 if (__n > 0)
1002 {
1003 allocate(__n);
1004 __construct_at_end(__first, __last);
1005 }
1006}
1007
1008template <class _Tp, class _Allocator>
1009vector<_Tp, _Allocator>::vector(const vector& __x)
1010 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1011{
1012 size_type __n = __x.size();
1013 if (__n > 0)
1014 {
1015 allocate(__n);
1016 __construct_at_end(__x.__begin_, __x.__end_);
1017 }
1018}
1019
1020template <class _Tp, class _Allocator>
1021vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1022 : __base(__a)
1023{
1024 size_type __n = __x.size();
1025 if (__n > 0)
1026 {
1027 allocate(__n);
1028 __construct_at_end(__x.__begin_, __x.__end_);
1029 }
1030}
1031
Howard Hinnant73d21a42010-09-04 23:28:19 +00001032#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001033
1034template <class _Tp, class _Allocator>
1035_LIBCPP_INLINE_VISIBILITY inline
1036vector<_Tp, _Allocator>::vector(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001037 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001038 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001039{
1040 this->__begin_ = __x.__begin_;
1041 this->__end_ = __x.__end_;
1042 this->__end_cap() = __x.__end_cap();
1043 __x.__begin_ = __x.__end_ = __x.__end_cap() = 0;
1044 __x.__invalidate_all_iterators();
1045}
1046
1047template <class _Tp, class _Allocator>
1048_LIBCPP_INLINE_VISIBILITY inline
1049vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1050 : __base(__a)
1051{
1052 if (__a == __x.__alloc())
1053 {
1054 this->__begin_ = __x.__begin_;
1055 this->__end_ = __x.__end_;
1056 this->__end_cap() = __x.__end_cap();
1057 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
1058 __x.__invalidate_all_iterators();
1059 }
1060 else
1061 {
1062 typedef move_iterator<iterator> _I;
1063 assign(_I(__x.begin()), _I(__x.end()));
1064 }
1065}
1066
Howard Hinnante3e32912011-08-12 21:56:02 +00001067#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1068
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001069template <class _Tp, class _Allocator>
1070_LIBCPP_INLINE_VISIBILITY inline
1071vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1072{
1073 if (__il.size() > 0)
1074 {
1075 allocate(__il.size());
1076 __construct_at_end(__il.begin(), __il.end());
1077 }
1078}
1079
1080template <class _Tp, class _Allocator>
1081_LIBCPP_INLINE_VISIBILITY inline
1082vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1083 : __base(__a)
1084{
1085 if (__il.size() > 0)
1086 {
1087 allocate(__il.size());
1088 __construct_at_end(__il.begin(), __il.end());
1089 }
1090}
1091
Howard Hinnante3e32912011-08-12 21:56:02 +00001092#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1093
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001094template <class _Tp, class _Allocator>
1095_LIBCPP_INLINE_VISIBILITY inline
1096vector<_Tp, _Allocator>&
1097vector<_Tp, _Allocator>::operator=(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001098 _NOEXCEPT_(
1099 __alloc_traits::propagate_on_container_move_assignment::value &&
1100 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001101{
1102 __move_assign(__x, integral_constant<bool,
1103 __alloc_traits::propagate_on_container_move_assignment::value>());
1104 return *this;
1105}
1106
1107template <class _Tp, class _Allocator>
1108void
1109vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1110{
1111 if (__base::__alloc() != __c.__alloc())
1112 {
1113 typedef move_iterator<iterator> _I;
1114 assign(_I(__c.begin()), _I(__c.end()));
1115 }
1116 else
1117 __move_assign(__c, true_type());
1118}
1119
1120template <class _Tp, class _Allocator>
1121void
1122vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001123 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001124{
1125 deallocate();
1126 this->__begin_ = __c.__begin_;
1127 this->__end_ = __c.__end_;
1128 this->__end_cap() = __c.__end_cap();
1129 __base::__move_assign_alloc(__c);
1130 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
1131}
1132
Howard Hinnant73d21a42010-09-04 23:28:19 +00001133#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001134
1135template <class _Tp, class _Allocator>
1136_LIBCPP_INLINE_VISIBILITY inline
1137vector<_Tp, _Allocator>&
1138vector<_Tp, _Allocator>::operator=(const vector& __x)
1139{
1140 if (this != &__x)
1141 {
1142 __base::__copy_assign_alloc(__x);
1143 assign(__x.__begin_, __x.__end_);
1144 }
1145 return *this;
1146}
1147
1148template <class _Tp, class _Allocator>
1149template <class _InputIterator>
1150typename enable_if
1151<
1152 __is_input_iterator <_InputIterator>::value &&
1153 !__is_forward_iterator<_InputIterator>::value,
1154 void
1155>::type
1156vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1157{
1158 clear();
1159 for (; __first != __last; ++__first)
1160 push_back(*__first);
1161}
1162
1163template <class _Tp, class _Allocator>
1164template <class _ForwardIterator>
1165typename enable_if
1166<
1167 __is_forward_iterator<_ForwardIterator>::value,
1168 void
1169>::type
1170vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1171{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001172 typename iterator_traits<_ForwardIterator>::difference_type __new_size = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001173 if (static_cast<size_type>(__new_size) <= capacity())
1174 {
1175 _ForwardIterator __mid = __last;
1176 bool __growing = false;
1177 if (static_cast<size_type>(__new_size) > size())
1178 {
1179 __growing = true;
1180 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001181 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001182 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001183 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001184 if (__growing)
1185 __construct_at_end(__mid, __last);
1186 else
1187 this->__destruct_at_end(__m);
1188 }
1189 else
1190 {
1191 deallocate();
1192 allocate(__recommend(static_cast<size_type>(__new_size)));
1193 __construct_at_end(__first, __last);
1194 }
1195}
1196
1197template <class _Tp, class _Allocator>
1198void
1199vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1200{
1201 if (__n <= capacity())
1202 {
1203 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001204 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001205 if (__n > __s)
1206 __construct_at_end(__n - __s, __u);
1207 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001208 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001209 }
1210 else
1211 {
1212 deallocate();
1213 allocate(__recommend(static_cast<size_type>(__n)));
1214 __construct_at_end(__n, __u);
1215 }
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>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001221vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001222{
1223#ifdef _LIBCPP_DEBUG
1224 return iterator(this, __p);
1225#else
1226 return 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>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001233vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001234{
1235#ifdef _LIBCPP_DEBUG
1236 return const_iterator(this, __p);
1237#else
1238 return const_iterator(__p);
1239#endif
1240}
1241
Howard Hinnant324bb032010-08-22 00:02:43 +00001242template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001243_LIBCPP_INLINE_VISIBILITY inline
1244typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001245vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001246{
1247 return __make_iter(this->__begin_);
1248}
1249
Howard Hinnant324bb032010-08-22 00:02:43 +00001250template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001251_LIBCPP_INLINE_VISIBILITY inline
1252typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001253vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001254{
1255 return __make_iter(this->__begin_);
1256}
1257
Howard Hinnant324bb032010-08-22 00:02:43 +00001258template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001259_LIBCPP_INLINE_VISIBILITY inline
1260typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001261vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001262{
1263 return __make_iter(this->__end_);
1264}
1265
Howard Hinnant324bb032010-08-22 00:02:43 +00001266template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001267_LIBCPP_INLINE_VISIBILITY inline
1268typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001269vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001270{
1271 return __make_iter(this->__end_);
1272}
1273
Howard Hinnant324bb032010-08-22 00:02:43 +00001274template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001275_LIBCPP_INLINE_VISIBILITY inline
1276typename vector<_Tp, _Allocator>::reference
1277vector<_Tp, _Allocator>::operator[](size_type __n)
1278{
1279#ifdef _LIBCPP_DEBUG
1280 assert(__n < size());
1281#endif
1282 return this->__begin_[__n];
1283}
1284
Howard Hinnant324bb032010-08-22 00:02:43 +00001285template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001286_LIBCPP_INLINE_VISIBILITY inline
1287typename vector<_Tp, _Allocator>::const_reference
1288vector<_Tp, _Allocator>::operator[](size_type __n) const
1289{
1290#ifdef _LIBCPP_DEBUG
1291 assert(__n < size());
1292#endif
1293 return this->__begin_[__n];
1294}
1295
Howard Hinnant324bb032010-08-22 00:02:43 +00001296template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001297typename vector<_Tp, _Allocator>::reference
1298vector<_Tp, _Allocator>::at(size_type __n)
1299{
1300 if (__n >= size())
1301 this->__throw_out_of_range();
1302 return this->__begin_[__n];
1303}
1304
Howard Hinnant324bb032010-08-22 00:02:43 +00001305template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001306typename vector<_Tp, _Allocator>::const_reference
1307vector<_Tp, _Allocator>::at(size_type __n) const
1308{
1309 if (__n >= size())
1310 this->__throw_out_of_range();
1311 return this->__begin_[__n];
1312}
1313
1314template <class _Tp, class _Allocator>
1315void
1316vector<_Tp, _Allocator>::reserve(size_type __n)
1317{
1318 if (__n > capacity())
1319 {
1320 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001321 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001322 __swap_out_circular_buffer(__v);
1323 }
1324}
1325
1326template <class _Tp, class _Allocator>
1327void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001328vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001329{
1330 if (capacity() > size())
1331 {
1332#ifndef _LIBCPP_NO_EXCEPTIONS
1333 try
1334 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001335#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001336 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001337 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001338 __swap_out_circular_buffer(__v);
1339#ifndef _LIBCPP_NO_EXCEPTIONS
1340 }
1341 catch (...)
1342 {
1343 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001344#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001345 }
1346}
1347
1348template <class _Tp, class _Allocator>
1349void
1350vector<_Tp, _Allocator>::push_back(const_reference __x)
1351{
1352 if (this->__end_ < this->__end_cap())
1353 {
1354 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001355 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001356 ++this->__end_;
1357 }
1358 else
1359 {
1360 allocator_type& __a = this->__alloc();
1361 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1362 __v.push_back(__x);
1363 __swap_out_circular_buffer(__v);
1364 }
1365}
1366
Howard Hinnant73d21a42010-09-04 23:28:19 +00001367#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001368
1369template <class _Tp, class _Allocator>
1370void
1371vector<_Tp, _Allocator>::push_back(value_type&& __x)
1372{
1373 if (this->__end_ < this->__end_cap())
1374 {
1375 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001376 _VSTD::__to_raw_pointer(this->__end_),
1377 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001378 ++this->__end_;
1379 }
1380 else
1381 {
1382 allocator_type& __a = this->__alloc();
1383 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001384 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001385 __swap_out_circular_buffer(__v);
1386 }
1387}
1388
Howard Hinnant73d21a42010-09-04 23:28:19 +00001389#ifndef _LIBCPP_HAS_NO_VARIADICS
1390
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001391template <class _Tp, class _Allocator>
1392template <class... _Args>
1393void
1394vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1395{
1396 if (this->__end_ < this->__end_cap())
1397 {
1398 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001399 _VSTD::__to_raw_pointer(this->__end_),
1400 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001401 ++this->__end_;
1402 }
1403 else
1404 {
1405 allocator_type& __a = this->__alloc();
1406 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001407 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001408 __swap_out_circular_buffer(__v);
1409 }
1410}
1411
Howard Hinnant73d21a42010-09-04 23:28:19 +00001412#endif // _LIBCPP_HAS_NO_VARIADICS
1413#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001414
1415template <class _Tp, class _Allocator>
1416_LIBCPP_INLINE_VISIBILITY inline
1417void
1418vector<_Tp, _Allocator>::pop_back()
1419{
1420 this->__destruct_at_end(this->__end_ - 1);
1421}
1422
1423template <class _Tp, class _Allocator>
1424_LIBCPP_INLINE_VISIBILITY inline
1425typename vector<_Tp, _Allocator>::iterator
1426vector<_Tp, _Allocator>::erase(const_iterator __position)
1427{
1428 pointer __p = const_cast<pointer>(&*__position);
1429 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001430 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001431 return __r;
1432}
1433
1434template <class _Tp, class _Allocator>
1435typename vector<_Tp, _Allocator>::iterator
1436vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1437{
1438 pointer __p = this->__begin_ + (__first - begin());
1439 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001440 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441 return __r;
1442}
1443
1444template <class _Tp, class _Allocator>
1445void
1446vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1447{
1448 pointer __old_last = this->__end_;
1449 difference_type __n = __old_last - __to;
1450 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1451 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001452 _VSTD::__to_raw_pointer(this->__end_),
1453 _VSTD::move(*__i));
1454 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001455}
1456
1457template <class _Tp, class _Allocator>
1458typename vector<_Tp, _Allocator>::iterator
1459vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1460{
1461 pointer __p = this->__begin_ + (__position - begin());
1462 if (this->__end_ < this->__end_cap())
1463 {
1464 if (__p == this->__end_)
1465 {
1466 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001467 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001468 ++this->__end_;
1469 }
1470 else
1471 {
1472 __move_range(__p, this->__end_, __p + 1);
1473 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1474 if (__p <= __xr && __xr < this->__end_)
1475 ++__xr;
1476 *__p = *__xr;
1477 }
1478 }
1479 else
1480 {
1481 allocator_type& __a = this->__alloc();
1482 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1483 __v.push_back(__x);
1484 __p = __swap_out_circular_buffer(__v, __p);
1485 }
1486 return __make_iter(__p);
1487}
1488
Howard Hinnant73d21a42010-09-04 23:28:19 +00001489#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001490
1491template <class _Tp, class _Allocator>
1492typename vector<_Tp, _Allocator>::iterator
1493vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1494{
1495 pointer __p = this->__begin_ + (__position - begin());
1496 if (this->__end_ < this->__end_cap())
1497 {
1498 if (__p == this->__end_)
1499 {
1500 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001501 _VSTD::__to_raw_pointer(this->__end_),
1502 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001503 ++this->__end_;
1504 }
1505 else
1506 {
1507 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001508 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001509 }
1510 }
1511 else
1512 {
1513 allocator_type& __a = this->__alloc();
1514 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001515 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001516 __p = __swap_out_circular_buffer(__v, __p);
1517 }
1518 return __make_iter(__p);
1519}
1520
Howard Hinnant73d21a42010-09-04 23:28:19 +00001521#ifndef _LIBCPP_HAS_NO_VARIADICS
1522
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001523template <class _Tp, class _Allocator>
1524template <class... _Args>
1525typename vector<_Tp, _Allocator>::iterator
1526vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1527{
1528 pointer __p = this->__begin_ + (__position - begin());
1529 if (this->__end_ < this->__end_cap())
1530 {
1531 if (__p == this->__end_)
1532 {
1533 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001534 _VSTD::__to_raw_pointer(this->__end_),
1535 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001536 ++this->__end_;
1537 }
1538 else
1539 {
1540 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001541 *__p = value_type(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001542 }
1543 }
1544 else
1545 {
1546 allocator_type& __a = this->__alloc();
1547 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001548 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001549 __p = __swap_out_circular_buffer(__v, __p);
1550 }
1551 return __make_iter(__p);
1552}
1553
Howard Hinnant73d21a42010-09-04 23:28:19 +00001554#endif // _LIBCPP_HAS_NO_VARIADICS
1555#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001556
1557template <class _Tp, class _Allocator>
1558typename vector<_Tp, _Allocator>::iterator
1559vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1560{
1561 pointer __p = this->__begin_ + (__position - begin());
1562 if (__n > 0)
1563 {
1564 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1565 {
1566 size_type __old_n = __n;
1567 pointer __old_last = this->__end_;
1568 if (__n > static_cast<size_type>(this->__end_ - __p))
1569 {
1570 size_type __cx = __n - (this->__end_ - __p);
1571 __construct_at_end(__cx, __x);
1572 __n -= __cx;
1573 }
1574 if (__n > 0)
1575 {
1576 __move_range(__p, __old_last, __p + __old_n);
1577 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1578 if (__p <= __xr && __xr < this->__end_)
1579 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001580 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001581 }
1582 }
1583 else
1584 {
1585 allocator_type& __a = this->__alloc();
1586 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1587 __v.__construct_at_end(__n, __x);
1588 __p = __swap_out_circular_buffer(__v, __p);
1589 }
1590 }
1591 return __make_iter(__p);
1592}
1593
1594template <class _Tp, class _Allocator>
1595template <class _InputIterator>
1596typename enable_if
1597<
1598 __is_input_iterator <_InputIterator>::value &&
1599 !__is_forward_iterator<_InputIterator>::value,
1600 typename vector<_Tp, _Allocator>::iterator
1601>::type
1602vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1603{
1604 difference_type __off = __position - begin();
1605 pointer __p = this->__begin_ + __off;
1606 allocator_type& __a = this->__alloc();
1607 pointer __old_last = this->__end_;
1608 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1609 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001610 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611 *__first);
1612 ++this->__end_;
1613 }
1614 __split_buffer<value_type, allocator_type&> __v(__a);
1615 if (__first != __last)
1616 {
1617#ifndef _LIBCPP_NO_EXCEPTIONS
1618 try
1619 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001620#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001621 __v.__construct_at_end(__first, __last);
1622 difference_type __old_size = __old_last - this->__begin_;
1623 difference_type __old_p = __p - this->__begin_;
1624 reserve(__recommend(size() + __v.size()));
1625 __p = this->__begin_ + __old_p;
1626 __old_last = this->__begin_ + __old_size;
1627#ifndef _LIBCPP_NO_EXCEPTIONS
1628 }
1629 catch (...)
1630 {
1631 erase(__make_iter(__old_last), end());
1632 throw;
1633 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001634#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001635 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001636 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001637 insert(__make_iter(__p), move_iterator<iterator>(__v.begin()),
1638 move_iterator<iterator>(__v.end()));
1639 return begin() + __off;
1640}
1641
1642template <class _Tp, class _Allocator>
1643template <class _ForwardIterator>
1644typename enable_if
1645<
1646 __is_forward_iterator<_ForwardIterator>::value,
1647 typename vector<_Tp, _Allocator>::iterator
1648>::type
1649vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1650{
1651 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00001652 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001653 if (__n > 0)
1654 {
1655 if (__n <= this->__end_cap() - this->__end_)
1656 {
1657 size_type __old_n = __n;
1658 pointer __old_last = this->__end_;
1659 _ForwardIterator __m = __last;
1660 difference_type __dx = this->__end_ - __p;
1661 if (__n > __dx)
1662 {
1663 __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001664 _VSTD::advance(__m, this->__end_ - __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001665 __construct_at_end(__m, __last);
1666 __n = __dx;
1667 }
1668 if (__n > 0)
1669 {
1670 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001671 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001672 }
1673 }
1674 else
1675 {
1676 allocator_type& __a = this->__alloc();
1677 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1678 __v.__construct_at_end(__first, __last);
1679 __p = __swap_out_circular_buffer(__v, __p);
1680 }
1681 }
1682 return __make_iter(__p);
1683}
1684
1685template <class _Tp, class _Allocator>
1686void
1687vector<_Tp, _Allocator>::resize(size_type __sz)
1688{
1689 size_type __cs = size();
1690 if (__cs < __sz)
1691 this->__append(__sz - __cs);
1692 else if (__cs > __sz)
1693 this->__destruct_at_end(this->__begin_ + __sz);
1694}
1695
1696template <class _Tp, class _Allocator>
1697void
1698vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
1699{
1700 size_type __cs = size();
1701 if (__cs < __sz)
1702 this->__append(__sz - __cs, __x);
1703 else if (__cs > __sz)
1704 this->__destruct_at_end(this->__begin_ + __sz);
1705}
1706
1707template <class _Tp, class _Allocator>
1708void
1709vector<_Tp, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001710 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1711 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001712{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001713 _VSTD::swap(this->__begin_, __x.__begin_);
1714 _VSTD::swap(this->__end_, __x.__end_);
1715 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001716 __base::__swap_alloc(this->__alloc(), __x.__alloc());
1717#ifdef _LIBCPP_DEBUG
1718 iterator::swap(this, &__x);
1719 const_iterator::swap(this, &__x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001720#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001721}
1722
Howard Hinnant324bb032010-08-22 00:02:43 +00001723template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001724bool
1725vector<_Tp, _Allocator>::__invariants() const
1726{
1727 if (this->__begin_ == 0)
1728 {
1729 if (this->__end_ != 0 || this->__end_cap() != 0)
1730 return false;
1731 }
1732 else
1733 {
1734 if (this->__begin_ > this->__end_)
1735 return false;
1736 if (this->__begin_ == this->__end_cap())
1737 return false;
1738 if (this->__end_ > this->__end_cap())
1739 return false;
1740 }
1741 return true;
1742}
1743
1744template <class _Tp, class _Allocator>
1745#ifndef _LIBCPP_DEBUG
1746_LIBCPP_INLINE_VISIBILITY inline
1747#endif
1748void
1749vector<_Tp, _Allocator>::__invalidate_all_iterators()
1750{
1751#ifdef _LIBCPP_DEBUG
1752 iterator::__remove_all(this);
1753 const_iterator::__remove_all(this);
Howard Hinnant324bb032010-08-22 00:02:43 +00001754#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001755}
1756
1757// vector<bool>
1758
1759template <class _Allocator> class vector<bool, _Allocator>;
1760
1761template <class _Allocator> struct hash<vector<bool, _Allocator> >;
1762
1763template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:23 +00001764struct __has_storage_type<vector<bool, _Allocator> >
1765{
1766 static const bool value = true;
1767};
1768
1769template <class _Allocator>
Howard Hinnant36cdf022010-09-10 16:42:26 +00001770class _LIBCPP_VISIBLE vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001771 : private __vector_base_common<true>
1772{
1773public:
1774 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00001775 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001776 typedef _Allocator allocator_type;
1777 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001778 typedef typename __alloc_traits::size_type size_type;
1779 typedef typename __alloc_traits::difference_type difference_type;
1780 typedef __bit_iterator<vector, false> pointer;
1781 typedef __bit_iterator<vector, true> const_pointer;
1782#ifdef _LIBCPP_DEBUG
1783 typedef __debug_iter<vector, pointer> iterator;
1784 typedef __debug_iter<vector, const_pointer> const_iterator;
1785
1786 friend class __debug_iter<vector, pointer>;
1787 friend class __debug_iter<vector, const_pointer>;
1788
1789 pair<iterator*, const_iterator*> __iterator_list_;
1790
1791 _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;}
1792 _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;}
Howard Hinnant324bb032010-08-22 00:02:43 +00001793#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001794 typedef pointer iterator;
1795 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +00001796#endif // _LIBCPP_DEBUG
Howard Hinnant0949eed2011-06-30 21:18:19 +00001797 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1798 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001799
1800private:
1801 typedef size_type __storage_type;
1802 typedef typename __alloc_traits::template
1803#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1804 rebind_alloc<__storage_type>
1805#else
1806 rebind_alloc<__storage_type>::other
1807#endif
1808 __storage_allocator;
1809 typedef allocator_traits<__storage_allocator> __storage_traits;
1810 typedef typename __storage_traits::pointer __storage_pointer;
1811 typedef typename __storage_traits::const_pointer __const_storage_pointer;
1812
1813 __storage_pointer __begin_;
1814 size_type __size_;
1815 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00001816public:
Howard Hinnantf03c3b42011-07-02 20:33:23 +00001817 typedef __bit_reference<vector> reference;
1818 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00001819private:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001820 _LIBCPP_INLINE_VISIBILITY
1821 size_type& __cap() _NOEXCEPT
1822 {return __cap_alloc_.first();}
1823 _LIBCPP_INLINE_VISIBILITY
1824 const size_type& __cap() const _NOEXCEPT
1825 {return __cap_alloc_.first();}
1826 _LIBCPP_INLINE_VISIBILITY
1827 __storage_allocator& __alloc() _NOEXCEPT
1828 {return __cap_alloc_.second();}
1829 _LIBCPP_INLINE_VISIBILITY
1830 const __storage_allocator& __alloc() const _NOEXCEPT
1831 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001832
1833 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
1834
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001835 _LIBCPP_INLINE_VISIBILITY
1836 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001838 _LIBCPP_INLINE_VISIBILITY
1839 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001840 {return (__n - 1) / __bits_per_word + 1;}
1841
1842public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001843 _LIBCPP_INLINE_VISIBILITY
1844 vector()
1845 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001846 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001847 ~vector();
1848 explicit vector(size_type __n);
1849 vector(size_type __n, const value_type& __v);
1850 vector(size_type __n, const value_type& __v, const allocator_type& __a);
1851 template <class _InputIterator>
1852 vector(_InputIterator __first, _InputIterator __last,
1853 typename enable_if<__is_input_iterator <_InputIterator>::value &&
1854 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
1855 template <class _InputIterator>
1856 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1857 typename enable_if<__is_input_iterator <_InputIterator>::value &&
1858 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
1859 template <class _ForwardIterator>
1860 vector(_ForwardIterator __first, _ForwardIterator __last,
1861 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
1862 template <class _ForwardIterator>
1863 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
1864 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
1865
1866 vector(const vector& __v);
1867 vector(const vector& __v, const allocator_type& __a);
1868 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00001869#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001870 vector(initializer_list<value_type> __il);
1871 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00001872#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001873
Howard Hinnant73d21a42010-09-04 23:28:19 +00001874#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001875 _LIBCPP_INLINE_VISIBILITY
1876 vector(vector&& __v)
1877 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001878 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001879 _LIBCPP_INLINE_VISIBILITY
1880 vector& operator=(vector&& __v)
1881 _NOEXCEPT_(
1882 __alloc_traits::propagate_on_container_move_assignment::value &&
1883 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001884#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +00001885#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001886 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001887 vector& operator=(initializer_list<value_type> __il)
1888 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00001889#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001890
1891 template <class _InputIterator>
1892 typename enable_if
1893 <
1894 __is_input_iterator<_InputIterator>::value &&
1895 !__is_forward_iterator<_InputIterator>::value,
1896 void
1897 >::type
1898 assign(_InputIterator __first, _InputIterator __last);
1899 template <class _ForwardIterator>
1900 typename enable_if
1901 <
1902 __is_forward_iterator<_ForwardIterator>::value,
1903 void
1904 >::type
1905 assign(_ForwardIterator __first, _ForwardIterator __last);
1906
1907 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +00001908#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001910 void assign(initializer_list<value_type> __il)
1911 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001912#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001913
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001914 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001915 {return allocator_type(this->__alloc());}
1916
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001917 size_type max_size() const _NOEXCEPT;
1918 _LIBCPP_INLINE_VISIBILITY
1919 size_type capacity() const _NOEXCEPT
1920 {return __internal_cap_to_external(__cap());}
1921 _LIBCPP_INLINE_VISIBILITY
1922 size_type size() const _NOEXCEPT
1923 {return __size_;}
1924 _LIBCPP_INLINE_VISIBILITY
1925 bool empty() const _NOEXCEPT
1926 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001927 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001928 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001929
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001930 _LIBCPP_INLINE_VISIBILITY
1931 iterator begin() _NOEXCEPT
1932 {return __make_iter(0);}
1933 _LIBCPP_INLINE_VISIBILITY
1934 const_iterator begin() const _NOEXCEPT
1935 {return __make_iter(0);}
1936 _LIBCPP_INLINE_VISIBILITY
1937 iterator end() _NOEXCEPT
1938 {return __make_iter(__size_);}
1939 _LIBCPP_INLINE_VISIBILITY
1940 const_iterator end() const _NOEXCEPT
1941 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001942
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001943 _LIBCPP_INLINE_VISIBILITY
1944 reverse_iterator rbegin() _NOEXCEPT
1945 {return reverse_iterator(end());}
1946 _LIBCPP_INLINE_VISIBILITY
1947 const_reverse_iterator rbegin() const _NOEXCEPT
1948 {return const_reverse_iterator(end());}
1949 _LIBCPP_INLINE_VISIBILITY
1950 reverse_iterator rend() _NOEXCEPT
1951 {return reverse_iterator(begin());}
1952 _LIBCPP_INLINE_VISIBILITY
1953 const_reverse_iterator rend() const _NOEXCEPT
1954 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001955
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001956 _LIBCPP_INLINE_VISIBILITY
1957 const_iterator cbegin() const _NOEXCEPT
1958 {return __make_iter(0);}
1959 _LIBCPP_INLINE_VISIBILITY
1960 const_iterator cend() const _NOEXCEPT
1961 {return __make_iter(__size_);}
1962 _LIBCPP_INLINE_VISIBILITY
1963 const_reverse_iterator crbegin() const _NOEXCEPT
1964 {return rbegin();}
1965 _LIBCPP_INLINE_VISIBILITY
1966 const_reverse_iterator crend() const _NOEXCEPT
1967 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001968
1969 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
1970 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
1971 reference at(size_type __n);
1972 const_reference at(size_type __n) const;
1973
1974 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
1975 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
1976 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
1977 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
1978
1979 void push_back(const value_type& __x);
1980 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
1981
1982 iterator insert(const_iterator __position, const value_type& __x);
1983 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
1984 iterator insert(const_iterator __position, size_type __n, const_reference __x);
1985 template <class _InputIterator>
1986 typename enable_if
1987 <
1988 __is_input_iterator <_InputIterator>::value &&
1989 !__is_forward_iterator<_InputIterator>::value,
1990 iterator
1991 >::type
1992 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
1993 template <class _ForwardIterator>
1994 typename enable_if
1995 <
1996 __is_forward_iterator<_ForwardIterator>::value,
1997 iterator
1998 >::type
1999 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00002000#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002001 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002002 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2003 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002004#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002005
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002006 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002007 iterator erase(const_iterator __first, const_iterator __last);
2008
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002009 _LIBCPP_INLINE_VISIBILITY
2010 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002011
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002012 void swap(vector&)
2013 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2014 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002015
2016 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002017 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002018
2019 bool __invariants() const;
2020
2021private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002022 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002023 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002024 void deallocate() _NOEXCEPT;
2025 _LIBCPP_INLINE_VISIBILITY
2026 static size_type __align(size_type __new_size) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002027 {return __new_size + (__bits_per_word-1) & ~(__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002028 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2029 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002030 template <class _ForwardIterator>
2031 typename enable_if
2032 <
2033 __is_forward_iterator<_ForwardIterator>::value,
2034 void
2035 >::type
2036 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2037 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002038 _LIBCPP_INLINE_VISIBILITY
2039 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002040 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002041 _LIBCPP_INLINE_VISIBILITY
2042 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002043 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
2044#ifdef _LIBCPP_DEBUG
2045 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_type __pos)
2046 {return iterator(this, pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2047 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_type __pos) const
2048 {return const_iterator(this, const_pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2049 _LIBCPP_INLINE_VISIBILITY iterator __const_iterator_cast(const_iterator __p)
2050 {return iterator(this, pointer(const_cast<__storage_pointer>(__p.base().__seg_), __p.base().__ctz_));}
Howard Hinnant324bb032010-08-22 00:02:43 +00002051#else // _LIBCPP_DEBUG
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002052 _LIBCPP_INLINE_VISIBILITY
2053 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002054 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002055 _LIBCPP_INLINE_VISIBILITY
2056 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002057 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002058 _LIBCPP_INLINE_VISIBILITY
2059 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002060 {return iterator(const_cast<__storage_pointer>(__p.__seg_), __p.__ctz_);}
Howard Hinnant324bb032010-08-22 00:02:43 +00002061#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002062
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002063 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002064 void __copy_assign_alloc(const vector& __v)
2065 {__copy_assign_alloc(__v, integral_constant<bool,
2066 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002068 void __copy_assign_alloc(const vector& __c, true_type)
2069 {
2070 if (__alloc() != __c.__alloc())
2071 deallocate();
2072 __alloc() = __c.__alloc();
2073 }
2074
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002075 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002076 void __copy_assign_alloc(const vector& __c, false_type)
2077 {}
2078
2079 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002080 void __move_assign(vector& __c, true_type)
2081 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002082 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002083 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002084 _NOEXCEPT_(
2085 !__storage_traits::propagate_on_container_move_assignment::value ||
2086 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002087 {__move_assign_alloc(__c, integral_constant<bool,
2088 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002089 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002090 void __move_assign_alloc(const vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002091 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002092 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002093 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002094 }
2095
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002096 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002097 void __move_assign_alloc(const vector& __c, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002098 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002099 {}
2100
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002101 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002102 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002103 _NOEXCEPT_(
2104 !__storage_traits::propagate_on_container_swap::value ||
2105 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002106 {__swap_alloc(__x, __y, integral_constant<bool,
2107 __storage_traits::propagate_on_container_swap::value>());}
2108
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002109 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002110 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002111 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002112 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002113 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002114 swap(__x, __y);
2115 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002116 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002117 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002118 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002119 {}
2120
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002121 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002122
2123 friend class __bit_reference<vector>;
2124 friend class __bit_const_reference<vector>;
2125 friend class __bit_iterator<vector, false>;
2126 friend class __bit_iterator<vector, true>;
2127 friend class __bit_array<vector>;
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002128 friend struct _LIBCPP_VISIBLE hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002129};
2130
2131template <class _Allocator>
2132#ifndef _LIBCPP_DEBUG
2133_LIBCPP_INLINE_VISIBILITY inline
2134#endif
2135void
2136vector<bool, _Allocator>::__invalidate_all_iterators()
2137{
2138#ifdef _LIBCPP_DEBUG
2139 iterator::__remove_all(this);
2140 const_iterator::__remove_all(this);
Howard Hinnant324bb032010-08-22 00:02:43 +00002141#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002142}
2143
2144// Allocate space for __n objects
2145// throws length_error if __n > max_size()
2146// throws (probably bad_alloc) if memory run out
2147// Precondition: __begin_ == __end_ == __cap() == 0
2148// Precondition: __n > 0
2149// Postcondition: capacity() == __n
2150// Postcondition: size() == 0
2151template <class _Allocator>
2152void
2153vector<bool, _Allocator>::allocate(size_type __n)
2154{
2155 if (__n > max_size())
2156 this->__throw_length_error();
2157 __n = __external_cap_to_internal(__n);
2158 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2159 this->__size_ = 0;
2160 this->__cap() = __n;
2161}
2162
2163template <class _Allocator>
2164void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002165vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002166{
2167 if (this->__begin_ != 0)
2168 {
2169 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2170 __invalidate_all_iterators();
2171 this->__begin_ = 0;
2172 this->__size_ = this->__cap() = 0;
2173 }
2174}
2175
2176template <class _Allocator>
2177typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002178vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002179{
2180 size_type __amax = __storage_traits::max_size(__alloc());
2181 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2182 if (__nmax / __bits_per_word <= __amax)
2183 return __nmax;
2184 return __internal_cap_to_external(__amax);
2185}
2186
2187// Precondition: __new_size > capacity()
2188template <class _Allocator>
2189_LIBCPP_INLINE_VISIBILITY inline
2190typename vector<bool, _Allocator>::size_type
2191vector<bool, _Allocator>::__recommend(size_type __new_size) const
2192{
2193 const size_type __ms = max_size();
2194 if (__new_size > __ms)
2195 this->__throw_length_error();
2196 const size_type __cap = capacity();
2197 if (__cap >= __ms / 2)
2198 return __ms;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002199 return _VSTD::max(2*__cap, __align(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002200}
2201
2202// Default constructs __n objects starting at __end_
2203// Precondition: __n > 0
2204// Precondition: size() + __n <= capacity()
2205// Postcondition: size() == size() + __n
2206template <class _Allocator>
2207_LIBCPP_INLINE_VISIBILITY inline
2208void
2209vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2210{
2211 size_type __old_size = this->__size_;
2212 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002213 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002214}
2215
2216template <class _Allocator>
2217template <class _ForwardIterator>
2218typename enable_if
2219<
2220 __is_forward_iterator<_ForwardIterator>::value,
2221 void
2222>::type
2223vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2224{
2225 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002226 this->__size_ += _VSTD::distance(__first, __last);
2227 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002228}
2229
2230template <class _Allocator>
2231_LIBCPP_INLINE_VISIBILITY inline
2232vector<bool, _Allocator>::vector()
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002233 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002234 : __begin_(0),
2235 __size_(0),
2236 __cap_alloc_(0)
2237{
2238}
2239
2240template <class _Allocator>
2241_LIBCPP_INLINE_VISIBILITY inline
2242vector<bool, _Allocator>::vector(const allocator_type& __a)
2243 : __begin_(0),
2244 __size_(0),
2245 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2246{
2247}
2248
2249template <class _Allocator>
2250vector<bool, _Allocator>::vector(size_type __n)
2251 : __begin_(0),
2252 __size_(0),
2253 __cap_alloc_(0)
2254{
2255 if (__n > 0)
2256 {
2257 allocate(__n);
2258 __construct_at_end(__n, false);
2259 }
2260}
2261
2262template <class _Allocator>
2263vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
2264 : __begin_(0),
2265 __size_(0),
2266 __cap_alloc_(0)
2267{
2268 if (__n > 0)
2269 {
2270 allocate(__n);
2271 __construct_at_end(__n, __x);
2272 }
2273}
2274
2275template <class _Allocator>
2276vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
2277 : __begin_(0),
2278 __size_(0),
2279 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2280{
2281 if (__n > 0)
2282 {
2283 allocate(__n);
2284 __construct_at_end(__n, __x);
2285 }
2286}
2287
2288template <class _Allocator>
2289template <class _InputIterator>
2290vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2291 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2292 !__is_forward_iterator<_InputIterator>::value>::type*)
2293 : __begin_(0),
2294 __size_(0),
2295 __cap_alloc_(0)
2296{
2297#ifndef _LIBCPP_NO_EXCEPTIONS
2298 try
2299 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002300#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002301 for (; __first != __last; ++__first)
2302 push_back(*__first);
2303#ifndef _LIBCPP_NO_EXCEPTIONS
2304 }
2305 catch (...)
2306 {
2307 if (__begin_ != 0)
2308 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2309 __invalidate_all_iterators();
2310 throw;
2311 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002312#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002313}
2314
2315template <class _Allocator>
2316template <class _InputIterator>
2317vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2318 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2319 !__is_forward_iterator<_InputIterator>::value>::type*)
2320 : __begin_(0),
2321 __size_(0),
2322 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2323{
2324#ifndef _LIBCPP_NO_EXCEPTIONS
2325 try
2326 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002327#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002328 for (; __first != __last; ++__first)
2329 push_back(*__first);
2330#ifndef _LIBCPP_NO_EXCEPTIONS
2331 }
2332 catch (...)
2333 {
2334 if (__begin_ != 0)
2335 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2336 __invalidate_all_iterators();
2337 throw;
2338 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002339#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002340}
2341
2342template <class _Allocator>
2343template <class _ForwardIterator>
2344vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2345 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2346 : __begin_(0),
2347 __size_(0),
2348 __cap_alloc_(0)
2349{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002350 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002351 if (__n > 0)
2352 {
2353 allocate(__n);
2354 __construct_at_end(__first, __last);
2355 }
2356}
2357
2358template <class _Allocator>
2359template <class _ForwardIterator>
2360vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2361 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2362 : __begin_(0),
2363 __size_(0),
2364 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2365{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002366 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002367 if (__n > 0)
2368 {
2369 allocate(__n);
2370 __construct_at_end(__first, __last);
2371 }
2372}
2373
Howard Hinnante3e32912011-08-12 21:56:02 +00002374#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2375
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002376template <class _Allocator>
2377vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
2378 : __begin_(0),
2379 __size_(0),
2380 __cap_alloc_(0)
2381{
2382 size_type __n = static_cast<size_type>(__il.size());
2383 if (__n > 0)
2384 {
2385 allocate(__n);
2386 __construct_at_end(__il.begin(), __il.end());
2387 }
2388}
2389
2390template <class _Allocator>
2391vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
2392 : __begin_(0),
2393 __size_(0),
2394 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2395{
2396 size_type __n = static_cast<size_type>(__il.size());
2397 if (__n > 0)
2398 {
2399 allocate(__n);
2400 __construct_at_end(__il.begin(), __il.end());
2401 }
2402}
2403
Howard Hinnante3e32912011-08-12 21:56:02 +00002404#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2405
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002406template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002407vector<bool, _Allocator>::~vector()
2408{
2409 if (__begin_ != 0)
2410 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2411#ifdef _LIBCPP_DEBUG
2412 __invalidate_all_iterators();
2413#endif
2414}
2415
2416template <class _Allocator>
2417vector<bool, _Allocator>::vector(const vector& __v)
2418 : __begin_(0),
2419 __size_(0),
2420 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2421{
2422 if (__v.size() > 0)
2423 {
2424 allocate(__v.size());
2425 __construct_at_end(__v.begin(), __v.end());
2426 }
2427}
2428
2429template <class _Allocator>
2430vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
2431 : __begin_(0),
2432 __size_(0),
2433 __cap_alloc_(0, __a)
2434{
2435 if (__v.size() > 0)
2436 {
2437 allocate(__v.size());
2438 __construct_at_end(__v.begin(), __v.end());
2439 }
2440}
2441
2442template <class _Allocator>
2443vector<bool, _Allocator>&
2444vector<bool, _Allocator>::operator=(const vector& __v)
2445{
2446 if (this != &__v)
2447 {
2448 __copy_assign_alloc(__v);
2449 if (__v.__size_)
2450 {
2451 if (__v.__size_ > capacity())
2452 {
2453 deallocate();
2454 allocate(__v.__size_);
2455 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002456 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002457 }
2458 __size_ = __v.__size_;
2459 }
2460 return *this;
2461}
2462
Howard Hinnant73d21a42010-09-04 23:28:19 +00002463#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2464
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002465template <class _Allocator>
2466_LIBCPP_INLINE_VISIBILITY inline
2467vector<bool, _Allocator>::vector(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002468 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002469 : __begin_(__v.__begin_),
2470 __size_(__v.__size_),
2471 __cap_alloc_(__v.__cap_alloc_)
2472{
2473 __v.__begin_ = 0;
2474 __v.__size_ = 0;
2475 __v.__cap() = 0;
2476}
2477
2478template <class _Allocator>
2479vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
2480 : __begin_(0),
2481 __size_(0),
2482 __cap_alloc_(0, __a)
2483{
2484 if (__a == allocator_type(__v.__alloc()))
2485 {
2486 this->__begin_ = __v.__begin_;
2487 this->__size_ = __v.__size_;
2488 this->__cap() = __v.__cap();
2489 __v.__begin_ = nullptr;
2490 __v.__cap() = __v.__size_ = 0;
2491 }
2492 else if (__v.size() > 0)
2493 {
2494 allocate(__v.size());
2495 __construct_at_end(__v.begin(), __v.end());
2496 }
2497}
2498
2499template <class _Allocator>
2500_LIBCPP_INLINE_VISIBILITY inline
2501vector<bool, _Allocator>&
2502vector<bool, _Allocator>::operator=(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002503 _NOEXCEPT_(
2504 __alloc_traits::propagate_on_container_move_assignment::value &&
2505 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002506{
2507 __move_assign(__v, integral_constant<bool,
2508 __storage_traits::propagate_on_container_move_assignment::value>());
2509}
2510
2511template <class _Allocator>
2512void
2513vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2514{
2515 if (__alloc() != __c.__alloc())
2516 assign(__c.begin(), __c.end());
2517 else
2518 __move_assign(__c, true_type());
2519}
2520
2521template <class _Allocator>
2522void
2523vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002524 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002525{
2526 deallocate();
2527 this->__begin_ = __c.__begin_;
2528 this->__size_ = __c.__size_;
2529 this->__cap() = __c.__cap();
2530 __move_assign_alloc(__c);
2531 __c.__begin_ = nullptr;
2532 __c.__cap() = __c.__size_ = 0;
2533}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002534
2535#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002536
2537template <class _Allocator>
2538void
2539vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2540{
2541 __size_ = 0;
2542 if (__n > 0)
2543 {
2544 size_type __c = capacity();
2545 if (__n <= __c)
2546 __size_ = __n;
2547 else
2548 {
2549 vector __v(__alloc());
2550 __v.reserve(__recommend(__n));
2551 __v.__size_ = __n;
2552 swap(__v);
2553 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002554 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002555 }
2556}
2557
2558template <class _Allocator>
2559template <class _InputIterator>
2560typename enable_if
2561<
2562 __is_input_iterator<_InputIterator>::value &&
2563 !__is_forward_iterator<_InputIterator>::value,
2564 void
2565>::type
2566vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2567{
2568 clear();
2569 for (; __first != __last; ++__first)
2570 push_back(*__first);
2571}
2572
2573template <class _Allocator>
2574template <class _ForwardIterator>
2575typename enable_if
2576<
2577 __is_forward_iterator<_ForwardIterator>::value,
2578 void
2579>::type
2580vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2581{
2582 clear();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002583 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002584 if (__n)
2585 {
2586 if (__n > capacity())
2587 {
2588 deallocate();
2589 allocate(__n);
2590 }
2591 __construct_at_end(__first, __last);
2592 }
2593}
2594
2595template <class _Allocator>
2596void
2597vector<bool, _Allocator>::reserve(size_type __n)
2598{
2599 if (__n > capacity())
2600 {
2601 vector __v(this->__alloc());
2602 __v.allocate(__n);
2603 __v.__construct_at_end(this->begin(), this->end());
2604 swap(__v);
2605 __invalidate_all_iterators();
2606 }
2607}
2608
2609template <class _Allocator>
2610void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002611vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002612{
2613 if (__external_cap_to_internal(size()) > __cap())
2614 {
2615#ifndef _LIBCPP_NO_EXCEPTIONS
2616 try
2617 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002618#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002619 vector(*this, allocator_type(__alloc())).swap(*this);
2620#ifndef _LIBCPP_NO_EXCEPTIONS
2621 }
2622 catch (...)
2623 {
2624 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002625#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002626 }
2627}
2628
2629template <class _Allocator>
2630typename vector<bool, _Allocator>::reference
2631vector<bool, _Allocator>::at(size_type __n)
2632{
2633 if (__n >= size())
2634 this->__throw_out_of_range();
2635 return (*this)[__n];
2636}
2637
2638template <class _Allocator>
2639typename vector<bool, _Allocator>::const_reference
2640vector<bool, _Allocator>::at(size_type __n) const
2641{
2642 if (__n >= size())
2643 this->__throw_out_of_range();
2644 return (*this)[__n];
2645}
2646
2647template <class _Allocator>
2648void
2649vector<bool, _Allocator>::push_back(const value_type& __x)
2650{
2651 if (this->__size_ == this->capacity())
2652 reserve(__recommend(this->__size_ + 1));
2653 ++this->__size_;
2654 back() = __x;
2655}
2656
2657template <class _Allocator>
2658typename vector<bool, _Allocator>::iterator
2659vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2660{
2661 iterator __r;
2662 if (size() < capacity())
2663 {
2664 const_iterator __old_end = end();
2665 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002666 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002667 __r = __const_iterator_cast(__position);
2668 }
2669 else
2670 {
2671 vector __v(__alloc());
2672 __v.reserve(__recommend(__size_ + 1));
2673 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002674 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2675 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002676 swap(__v);
2677 }
2678 *__r = __x;
2679 return __r;
2680}
2681
2682template <class _Allocator>
2683typename vector<bool, _Allocator>::iterator
2684vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
2685{
2686 iterator __r;
2687 size_type __c = capacity();
2688 if (__n <= __c && size() <= __c - __n)
2689 {
2690 const_iterator __old_end = end();
2691 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002692 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002693 __r = __const_iterator_cast(__position);
2694 }
2695 else
2696 {
2697 vector __v(__alloc());
2698 __v.reserve(__recommend(__size_ + __n));
2699 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002700 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2701 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002702 swap(__v);
2703 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002704 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002705 return __r;
2706}
2707
2708template <class _Allocator>
2709template <class _InputIterator>
2710typename enable_if
2711<
2712 __is_input_iterator <_InputIterator>::value &&
2713 !__is_forward_iterator<_InputIterator>::value,
2714 typename vector<bool, _Allocator>::iterator
2715>::type
2716vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
2717{
2718 difference_type __off = __position - begin();
2719 iterator __p = __const_iterator_cast(__position);
2720 iterator __old_end = end();
2721 for (; size() != capacity() && __first != __last; ++__first)
2722 {
2723 ++this->__size_;
2724 back() = *__first;
2725 }
2726 vector __v(__alloc());
2727 if (__first != __last)
2728 {
2729#ifndef _LIBCPP_NO_EXCEPTIONS
2730 try
2731 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002732#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002733 __v.assign(__first, __last);
2734 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
2735 difference_type __old_p = __p - begin();
2736 reserve(__recommend(size() + __v.size()));
2737 __p = begin() + __old_p;
2738 __old_end = begin() + __old_size;
2739#ifndef _LIBCPP_NO_EXCEPTIONS
2740 }
2741 catch (...)
2742 {
2743 erase(__old_end, end());
2744 throw;
2745 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002746#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002747 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002748 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002749 insert(__p, __v.begin(), __v.end());
2750 return begin() + __off;
2751}
2752
2753template <class _Allocator>
2754template <class _ForwardIterator>
2755typename enable_if
2756<
2757 __is_forward_iterator<_ForwardIterator>::value,
2758 typename vector<bool, _Allocator>::iterator
2759>::type
2760vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
2761{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002762 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002763 iterator __r;
2764 size_type __c = capacity();
2765 if (__n <= __c && size() <= __c - __n)
2766 {
2767 const_iterator __old_end = end();
2768 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002769 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002770 __r = __const_iterator_cast(__position);
2771 }
2772 else
2773 {
2774 vector __v(__alloc());
2775 __v.reserve(__recommend(__size_ + __n));
2776 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002777 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2778 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002779 swap(__v);
2780 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002781 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002782 return __r;
2783}
2784
2785template <class _Allocator>
2786_LIBCPP_INLINE_VISIBILITY inline
2787typename vector<bool, _Allocator>::iterator
2788vector<bool, _Allocator>::erase(const_iterator __position)
2789{
2790 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002791 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002792 --__size_;
2793 return __r;
2794}
2795
2796template <class _Allocator>
2797typename vector<bool, _Allocator>::iterator
2798vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
2799{
2800 iterator __r = __const_iterator_cast(__first);
2801 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002802 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002803 __size_ -= __d;
2804 return __r;
2805}
2806
2807template <class _Allocator>
2808void
2809vector<bool, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002810 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2811 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002812{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002813 _VSTD::swap(this->__begin_, __x.__begin_);
2814 _VSTD::swap(this->__size_, __x.__size_);
2815 _VSTD::swap(this->__cap(), __x.__cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002816 __swap_alloc(this->__alloc(), __x.__alloc());
2817#ifdef _LIBCPP_DEBUG
2818 iterator::swap(this, &__x);
2819 const_iterator::swap(this, &__x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002820#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002821}
2822
Howard Hinnant324bb032010-08-22 00:02:43 +00002823template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002824void
2825vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
2826{
2827 size_type __cs = size();
2828 if (__cs < __sz)
2829 {
2830 iterator __r;
2831 size_type __c = capacity();
2832 size_type __n = __sz - __cs;
2833 if (__n <= __c && __cs <= __c - __n)
2834 {
2835 __r = end();
2836 __size_ += __n;
2837 }
2838 else
2839 {
2840 vector __v(__alloc());
2841 __v.reserve(__recommend(__size_ + __n));
2842 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002843 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002844 swap(__v);
2845 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002846 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002847 }
2848 else
2849 __size_ = __sz;
2850}
2851
Howard Hinnant324bb032010-08-22 00:02:43 +00002852template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002853void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002854vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002855{
2856 // do middle whole words
2857 size_type __n = __size_;
2858 __storage_pointer __p = __begin_;
2859 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
2860 *__p = ~*__p;
2861 // do last partial word
2862 if (__n > 0)
2863 {
2864 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
2865 __storage_type __b = *__p & __m;
2866 *__p &= ~__m;
2867 *__p |= ~__b & __m;
2868 }
2869}
2870
Howard Hinnant324bb032010-08-22 00:02:43 +00002871template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002872bool
2873vector<bool, _Allocator>::__invariants() const
2874{
2875 if (this->__begin_ == 0)
2876 {
2877 if (this->__size_ != 0 || this->__cap() != 0)
2878 return false;
2879 }
2880 else
2881 {
2882 if (this->__cap() == 0)
2883 return false;
2884 if (this->__size_ > this->capacity())
2885 return false;
2886 }
2887 return true;
2888}
2889
Howard Hinnant324bb032010-08-22 00:02:43 +00002890template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002891size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002892vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002893{
2894 size_t __h = 0;
2895 // do middle whole words
2896 size_type __n = __size_;
2897 __storage_pointer __p = __begin_;
2898 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
2899 __h ^= *__p;
2900 // do last partial word
2901 if (__n > 0)
2902 {
2903 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
2904 __h ^= *__p & __m;
2905 }
2906 return __h;
2907}
2908
2909template <class _Allocator>
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002910struct _LIBCPP_VISIBLE hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002911 : public unary_function<vector<bool, _Allocator>, size_t>
2912{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002914 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002915 {return __vec.__hash_code();}
2916};
2917
2918template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002919_LIBCPP_INLINE_VISIBILITY inline
2920bool
2921operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2922{
2923 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002924 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002925}
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{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002940 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002941}
2942
2943template <class _Tp, class _Allocator>
2944_LIBCPP_INLINE_VISIBILITY inline
2945bool
2946operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2947{
2948 return __y < __x;
2949}
2950
2951template <class _Tp, class _Allocator>
2952_LIBCPP_INLINE_VISIBILITY inline
2953bool
2954operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2955{
2956 return !(__x < __y);
2957}
2958
2959template <class _Tp, class _Allocator>
2960_LIBCPP_INLINE_VISIBILITY inline
2961bool
2962operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2963{
2964 return !(__y < __x);
2965}
2966
2967template <class _Tp, class _Allocator>
2968_LIBCPP_INLINE_VISIBILITY inline
2969void
2970swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002971 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002972{
2973 __x.swap(__y);
2974}
2975
2976_LIBCPP_END_NAMESPACE_STD
2977
2978#endif // _LIBCPP_VECTOR