blob: f469a3a0de5389f711f0a4827cc2f5ba3092fe09 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------------ vector --------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-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 Hinnantc51e1022010-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 Hinnant3b6579a2010-08-22 00:02:43 +000020template <class T, class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000021class vector
Howard Hinnant3b6579a2010-08-22 00:02:43 +000022{
23public:
24 typedef T value_type;
Howard Hinnantc51e1022010-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 Hinnant1c936782011-06-03 19:40:40 +000037 vector()
38 noexcept(is_nothrow_default_constructible<allocator_type>::value);
39 explicit vector(const allocator_type&);
Howard Hinnantc51e1022010-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 Hinnant1c936782011-06-03 19:40:40 +000045 vector(vector&& x)
46 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-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 Hinnant1c936782011-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 Hinnantc51e1022010-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 Hinnant1c936782011-06-03 19:40:40 +000061 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000062
Howard Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +000067
Howard Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +000072
Howard Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +000077
Howard Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +000082 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +000083 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-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 Hinnant1c936782011-06-03 19:40:40 +000095 value_type* data() noexcept;
96 const value_type* data() const noexcept;
Howard Hinnantc51e1022010-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 Hinnant1c936782011-06-03 19:40:40 +0000115 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000116
117 void resize(size_type sz);
118 void resize(size_type sz, const value_type& c);
119
Howard Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +0000123
124 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000125};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000127template <class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128class vector<bool, Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000129{
130public:
131 typedef bool value_type;
Howard Hinnantc51e1022010-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 Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +0000151 };
152
153 class const_reference
154 {
155 public:
Howard Hinnant1c936782011-06-03 19:40:40 +0000156 const_reference(const reference&) noexcept;
157 operator bool() const noexcept;
158 const_iterator operator&() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159 };
160
Howard Hinnant1c936782011-06-03 19:40:40 +0000161 vector()
162 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantc2734962011-09-02 20:42:31 +0000163 explicit vector(const allocator_type&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000164 explicit vector(size_type n, const value_type& value = value_type(), const allocator_type& = allocator_type());
165 template <class InputIterator>
166 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
167 vector(const vector& x);
Howard Hinnant1c936782011-06-03 19:40:40 +0000168 vector(vector&& x)
169 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-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 Hinnant1c936782011-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 Hinnantc51e1022010-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 Hinnant1c936782011-06-03 19:40:40 +0000184 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185
Howard Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +0000190
Howard Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +0000195
Howard Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +0000200
Howard Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +0000205 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000206 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-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 Hinnant1c936782011-06-03 19:40:40 +0000230 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231
232 void resize(size_type sz);
233 void resize(size_type sz, value_type x);
234
Howard Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +0000239
240 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000241};
Howard Hinnantc51e1022010-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 Hinnant1c936782011-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 Hinnantc51e1022010-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>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000272
273#pragma GCC system_header
274
275_LIBCPP_BEGIN_NAMESPACE_STD
276
277template <bool>
278class __vector_base_common
279{
280protected:
281 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
282 void __throw_length_error() const;
283 void __throw_out_of_range() const;
284};
285
286template <bool __b>
287void
288__vector_base_common<__b>::__throw_length_error() const
289{
290#ifndef _LIBCPP_NO_EXCEPTIONS
291 throw length_error("vector");
292#else
293 assert(!"vector length_error");
294#endif
295}
296
297template <bool __b>
298void
299__vector_base_common<__b>::__throw_out_of_range() const
300{
301#ifndef _LIBCPP_NO_EXCEPTIONS
302 throw out_of_range("vector");
303#else
304 assert(!"vector out_of_range");
305#endif
306}
307
308extern template class __vector_base_common<true>;
309
310template <class _Tp, class _Allocator>
311class __vector_base
312 : protected __vector_base_common<true>
313{
314protected:
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000315 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 typedef _Allocator allocator_type;
317 typedef allocator_traits<allocator_type> __alloc_traits;
318 typedef value_type& reference;
319 typedef const value_type& const_reference;
320 typedef typename __alloc_traits::size_type size_type;
321 typedef typename __alloc_traits::difference_type difference_type;
322 typedef typename __alloc_traits::pointer pointer;
323 typedef typename __alloc_traits::const_pointer const_pointer;
324 typedef pointer iterator;
325 typedef const_pointer const_iterator;
326
327 pointer __begin_;
328 pointer __end_;
329 __compressed_pair<pointer, allocator_type> __end_cap_;
330
Howard Hinnant1c936782011-06-03 19:40:40 +0000331 _LIBCPP_INLINE_VISIBILITY
332 allocator_type& __alloc() _NOEXCEPT
333 {return __end_cap_.second();}
334 _LIBCPP_INLINE_VISIBILITY
335 const allocator_type& __alloc() const _NOEXCEPT
336 {return __end_cap_.second();}
337 _LIBCPP_INLINE_VISIBILITY
338 pointer& __end_cap() _NOEXCEPT
339 {return __end_cap_.first();}
340 _LIBCPP_INLINE_VISIBILITY
341 const pointer& __end_cap() const _NOEXCEPT
342 {return __end_cap_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000343
Howard Hinnant1c936782011-06-03 19:40:40 +0000344 _LIBCPP_INLINE_VISIBILITY
345 __vector_base()
346 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantcf823322010-12-17 14:46:43 +0000347 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000348 ~__vector_base();
349
Howard Hinnant1c936782011-06-03 19:40:40 +0000350 _LIBCPP_INLINE_VISIBILITY
351 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
352 _LIBCPP_INLINE_VISIBILITY
353 size_type capacity() const _NOEXCEPT
354 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000355
Howard Hinnant1c936782011-06-03 19:40:40 +0000356 _LIBCPP_INLINE_VISIBILITY
357 void __destruct_at_end(const_pointer __new_last) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +0000358 {__destruct_at_end(__new_last, is_trivially_destructible<value_type>());}
Howard Hinnant1c936782011-06-03 19:40:40 +0000359 _LIBCPP_INLINE_VISIBILITY
360 void __destruct_at_end(const_pointer __new_last, false_type) _NOEXCEPT;
361 _LIBCPP_INLINE_VISIBILITY
362 void __destruct_at_end(const_pointer __new_last, true_type) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000363
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000364 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000365 void __copy_assign_alloc(const __vector_base& __c)
366 {__copy_assign_alloc(__c, integral_constant<bool,
367 __alloc_traits::propagate_on_container_copy_assignment::value>());}
368
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370 void __move_assign_alloc(__vector_base& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +0000371 _NOEXCEPT_(
372 !__alloc_traits::propagate_on_container_move_assignment::value ||
373 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000374 {__move_assign_alloc(__c, integral_constant<bool,
375 __alloc_traits::propagate_on_container_move_assignment::value>());}
376
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000378 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +0000379 _NOEXCEPT_(
380 !__alloc_traits::propagate_on_container_swap::value ||
381 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000382 {__swap_alloc(__x, __y, integral_constant<bool,
383 __alloc_traits::propagate_on_container_swap::value>());}
384private:
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386 void __copy_assign_alloc(const __vector_base& __c, true_type)
387 {
388 if (__alloc() != __c.__alloc())
389 {
390 clear();
391 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
392 __begin_ = __end_ = __end_cap() = nullptr;
393 }
394 __alloc() = __c.__alloc();
395 }
396
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000398 void __copy_assign_alloc(const __vector_base& __c, false_type)
399 {}
400
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +0000402 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000403 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000404 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000405 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406 }
407
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +0000409 void __move_assign_alloc(__vector_base& __c, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000410 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411 {}
412
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000414 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000415 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000417 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000418 swap(__x, __y);
419 }
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421 static void __swap_alloc(allocator_type& __x, allocator_type& __y, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000422 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423 {}
424};
425
426template <class _Tp, class _Allocator>
427_LIBCPP_INLINE_VISIBILITY inline
428void
Howard Hinnant1c936782011-06-03 19:40:40 +0000429__vector_base<_Tp, _Allocator>::__destruct_at_end(const_pointer __new_last, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000430{
431 while (__new_last < __end_)
432 __alloc_traits::destroy(__alloc(), const_cast<pointer>(--__end_));
433}
434
435template <class _Tp, class _Allocator>
436_LIBCPP_INLINE_VISIBILITY inline
437void
Howard Hinnant1c936782011-06-03 19:40:40 +0000438__vector_base<_Tp, _Allocator>::__destruct_at_end(const_pointer __new_last, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000439{
440 __end_ = const_cast<pointer>(__new_last);
441}
442
443template <class _Tp, class _Allocator>
444_LIBCPP_INLINE_VISIBILITY inline
445__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnant1c936782011-06-03 19:40:40 +0000446 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447 : __begin_(0),
448 __end_(0),
449 __end_cap_(0)
450{
451}
452
453template <class _Tp, class _Allocator>
454_LIBCPP_INLINE_VISIBILITY inline
455__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
456 : __begin_(0),
457 __end_(0),
458 __end_cap_(0, __a)
459{
460}
461
462template <class _Tp, class _Allocator>
463__vector_base<_Tp, _Allocator>::~__vector_base()
464{
465 if (__begin_ != 0)
466 {
467 clear();
468 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
469 }
470}
471
472template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnanta99f0ce2010-09-10 16:42:26 +0000473class _LIBCPP_VISIBLE vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000474 : private __vector_base<_Tp, _Allocator>
475{
476private:
477 typedef __vector_base<_Tp, _Allocator> __base;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000478public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000479 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000480 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000481 typedef _Allocator allocator_type;
482 typedef typename __base::__alloc_traits __alloc_traits;
483 typedef typename __base::reference reference;
484 typedef typename __base::const_reference const_reference;
485 typedef typename __base::size_type size_type;
486 typedef typename __base::difference_type difference_type;
487 typedef typename __base::pointer pointer;
488 typedef typename __base::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000489 typedef __wrap_iter<pointer> iterator;
490 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000491 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
492 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000493
Howard Hinnant1c936782011-06-03 19:40:40 +0000494 _LIBCPP_INLINE_VISIBILITY
495 vector()
496 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000497 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000498#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000499 __get_db()->__insert_c(this);
500#endif
501 }
502 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
503 : __base(__a)
504 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000505#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000506 __get_db()->__insert_c(this);
507#endif
508 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000509 explicit vector(size_type __n);
510 vector(size_type __n, const_reference __x);
511 vector(size_type __n, const_reference __x, const allocator_type& __a);
512 template <class _InputIterator>
513 vector(_InputIterator __first, _InputIterator __last,
514 typename enable_if<__is_input_iterator <_InputIterator>::value &&
515 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
516 template <class _InputIterator>
517 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
518 typename enable_if<__is_input_iterator <_InputIterator>::value &&
519 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
520 template <class _ForwardIterator>
521 vector(_ForwardIterator __first, _ForwardIterator __last,
522 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
523 template <class _ForwardIterator>
524 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
525 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnant33711792011-08-12 21:56:02 +0000526#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantcf823322010-12-17 14:46:43 +0000527 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000528 vector(initializer_list<value_type> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000530 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnant33711792011-08-12 21:56:02 +0000531#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000532#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000534 ~vector()
535 {
536 __get_db()->__erase_c(this);
537 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000538#endif
539
540 vector(const vector& __x);
541 vector(const vector& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543 vector& operator=(const vector& __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000544#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantcf823322010-12-17 14:46:43 +0000545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000546 vector(vector&& __x)
547 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantcf823322010-12-17 14:46:43 +0000548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000549 vector(vector&& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000551 vector& operator=(vector&& __x)
552 _NOEXCEPT_(
553 __alloc_traits::propagate_on_container_move_assignment::value &&
554 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant74279a52010-09-04 23:28:19 +0000555#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant33711792011-08-12 21:56:02 +0000556#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000558 vector& operator=(initializer_list<value_type> __il)
559 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnant33711792011-08-12 21:56:02 +0000560#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000561
562 template <class _InputIterator>
563 typename enable_if
564 <
565 __is_input_iterator <_InputIterator>::value &&
566 !__is_forward_iterator<_InputIterator>::value,
567 void
568 >::type
569 assign(_InputIterator __first, _InputIterator __last);
570 template <class _ForwardIterator>
571 typename enable_if
572 <
573 __is_forward_iterator<_ForwardIterator>::value,
574 void
575 >::type
576 assign(_ForwardIterator __first, _ForwardIterator __last);
577
578 void assign(size_type __n, const_reference __u);
Howard Hinnant33711792011-08-12 21:56:02 +0000579#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000581 void assign(initializer_list<value_type> __il)
582 {assign(__il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +0000583#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584
Howard Hinnant1c936782011-06-03 19:40:40 +0000585 _LIBCPP_INLINE_VISIBILITY
586 allocator_type get_allocator() const _NOEXCEPT
587 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000588
Howard Hinnant1c936782011-06-03 19:40:40 +0000589 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
590 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
591 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
592 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593
Howard Hinnant1c936782011-06-03 19:40:40 +0000594 _LIBCPP_INLINE_VISIBILITY
595 reverse_iterator rbegin() _NOEXCEPT
596 {return reverse_iterator(end());}
597 _LIBCPP_INLINE_VISIBILITY
598 const_reverse_iterator rbegin() const _NOEXCEPT
599 {return const_reverse_iterator(end());}
600 _LIBCPP_INLINE_VISIBILITY
601 reverse_iterator rend() _NOEXCEPT
602 {return reverse_iterator(begin());}
603 _LIBCPP_INLINE_VISIBILITY
604 const_reverse_iterator rend() const _NOEXCEPT
605 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000606
Howard Hinnant1c936782011-06-03 19:40:40 +0000607 _LIBCPP_INLINE_VISIBILITY
608 const_iterator cbegin() const _NOEXCEPT
609 {return begin();}
610 _LIBCPP_INLINE_VISIBILITY
611 const_iterator cend() const _NOEXCEPT
612 {return end();}
613 _LIBCPP_INLINE_VISIBILITY
614 const_reverse_iterator crbegin() const _NOEXCEPT
615 {return rbegin();}
616 _LIBCPP_INLINE_VISIBILITY
617 const_reverse_iterator crend() const _NOEXCEPT
618 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000619
Howard Hinnant1c936782011-06-03 19:40:40 +0000620 _LIBCPP_INLINE_VISIBILITY
621 size_type size() const _NOEXCEPT
622 {return static_cast<size_type>(this->__end_ - this->__begin_);}
623 _LIBCPP_INLINE_VISIBILITY
624 size_type capacity() const _NOEXCEPT
625 {return __base::capacity();}
626 _LIBCPP_INLINE_VISIBILITY
627 bool empty() const _NOEXCEPT
628 {return this->__begin_ == this->__end_;}
629 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000630 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000631 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000632
633 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
634 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
635 reference at(size_type __n);
636 const_reference at(size_type __n) const;
637
Howard Hinnant27e0e772011-09-14 18:33:51 +0000638 _LIBCPP_INLINE_VISIBILITY reference front()
639 {
640 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
641 return *this->__begin_;
642 }
643 _LIBCPP_INLINE_VISIBILITY const_reference front() const
644 {
645 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
646 return *this->__begin_;
647 }
648 _LIBCPP_INLINE_VISIBILITY reference back()
649 {
650 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
651 return *(this->__end_ - 1);
652 }
653 _LIBCPP_INLINE_VISIBILITY const_reference back() const
654 {
655 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
656 return *(this->__end_ - 1);
657 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000658
Howard Hinnant1c936782011-06-03 19:40:40 +0000659 _LIBCPP_INLINE_VISIBILITY
660 value_type* data() _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000661 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000662 _LIBCPP_INLINE_VISIBILITY
663 const value_type* data() const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000664 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000665
Howard Hinnantcf823322010-12-17 14:46:43 +0000666 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000667#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000668 void push_back(value_type&& __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000669#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000670 template <class... _Args>
671 void emplace_back(_Args&&... __args);
Howard Hinnant74279a52010-09-04 23:28:19 +0000672#endif // _LIBCPP_HAS_NO_VARIADICS
673#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000674 void pop_back();
675
676 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000677#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000679#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680 template <class... _Args>
681 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant74279a52010-09-04 23:28:19 +0000682#endif // _LIBCPP_HAS_NO_VARIADICS
683#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000684 iterator insert(const_iterator __position, size_type __n, const_reference __x);
685 template <class _InputIterator>
686 typename enable_if
687 <
688 __is_input_iterator <_InputIterator>::value &&
689 !__is_forward_iterator<_InputIterator>::value,
690 iterator
691 >::type
692 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
693 template <class _ForwardIterator>
694 typename enable_if
695 <
696 __is_forward_iterator<_ForwardIterator>::value,
697 iterator
698 >::type
699 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant33711792011-08-12 21:56:02 +0000700#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000701 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702 iterator insert(const_iterator __position, initializer_list<value_type> __il)
703 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +0000704#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000705
Howard Hinnantcf823322010-12-17 14:46:43 +0000706 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000707 iterator erase(const_iterator __first, const_iterator __last);
708
Howard Hinnant1c936782011-06-03 19:40:40 +0000709 _LIBCPP_INLINE_VISIBILITY
710 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000711 {
712 __base::clear();
713 __invalidate_all_iterators();
714 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000715
716 void resize(size_type __sz);
717 void resize(size_type __sz, const_reference __x);
718
Howard Hinnant1c936782011-06-03 19:40:40 +0000719 void swap(vector&)
720 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
721 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722
723 bool __invariants() const;
724
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000725#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000726
727 bool __dereferenceable(const const_iterator* __i) const;
728 bool __decrementable(const const_iterator* __i) const;
729 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
730 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
731
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000732#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000733
Howard Hinnantc51e1022010-05-11 19:42:16 +0000734private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000735 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000736 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000737 void deallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000738 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000739 void __construct_at_end(size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000741 template <class _ForwardIterator>
742 typename enable_if
743 <
744 __is_forward_iterator<_ForwardIterator>::value,
745 void
746 >::type
747 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
748 void __move_construct_at_end(pointer __first, pointer __last);
749 void __append(size_type __n);
750 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000751 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000752 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000753 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000754 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
756 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
757 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000758 void __move_assign(vector& __c, true_type)
759 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760 void __move_assign(vector& __c, false_type);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000761 _LIBCPP_INLINE_VISIBILITY
762 void __destruct_at_end(const_pointer __new_last) _NOEXCEPT
763 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000764#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000765 __c_node* __c = __get_db()->__find_c_and_lock(this);
766 for (__i_node** __p = __c->end_; __p != __c->beg_; )
767 {
768 --__p;
769 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
770 if (__i->base() > __new_last)
771 {
772 (*__p)->__c_ = nullptr;
773 if (--__c->end_ != __p)
774 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
775 }
776 }
777 __get_db()->unlock();
778#endif
779 __base::__destruct_at_end(__new_last);
780 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781};
782
783template <class _Tp, class _Allocator>
784void
785vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
786{
787 for (pointer __p = this->__end_; this->__begin_ < __p;)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000788 __v.push_front(_VSTD::move_if_noexcept(*--__p));
789 _VSTD::swap(this->__begin_, __v.__begin_);
790 _VSTD::swap(this->__end_, __v.__end_);
791 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000792 __v.__first_ = __v.__begin_;
793 __invalidate_all_iterators();
794}
795
796template <class _Tp, class _Allocator>
797typename vector<_Tp, _Allocator>::pointer
798vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
799{
800 pointer __r = __v.__begin_;
801 for (pointer __i = __p; this->__begin_ < __i;)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000802 __v.push_front(_VSTD::move_if_noexcept(*--__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000803 for (pointer __i = __p; __i < this->__end_; ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000804 __v.push_back(_VSTD::move_if_noexcept(*__i));
805 _VSTD::swap(this->__begin_, __v.__begin_);
806 _VSTD::swap(this->__end_, __v.__end_);
807 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808 __v.__first_ = __v.__begin_;
809 __invalidate_all_iterators();
810 return __r;
811}
812
813// Allocate space for __n objects
814// throws length_error if __n > max_size()
815// throws (probably bad_alloc) if memory run out
816// Precondition: __begin_ == __end_ == __end_cap() == 0
817// Precondition: __n > 0
818// Postcondition: capacity() == __n
819// Postcondition: size() == 0
820template <class _Tp, class _Allocator>
821void
822vector<_Tp, _Allocator>::allocate(size_type __n)
823{
824 if (__n > max_size())
825 this->__throw_length_error();
826 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
827 this->__end_cap() = this->__begin_ + __n;
828}
829
830template <class _Tp, class _Allocator>
831void
Howard Hinnant1c936782011-06-03 19:40:40 +0000832vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000833{
834 if (this->__begin_ != 0)
835 {
836 clear();
837 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000838 this->__begin_ = this->__end_ = this->__end_cap() = 0;
839 }
840}
841
842template <class _Tp, class _Allocator>
843typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +0000844vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000845{
Alexis Hunt991d29b2011-07-29 23:31:58 +0000846 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()), numeric_limits<size_type>::max() / 2); // end() >= begin(), always
Howard Hinnantc51e1022010-05-11 19:42:16 +0000847}
848
849// Precondition: __new_size > capacity()
850template <class _Tp, class _Allocator>
851_LIBCPP_INLINE_VISIBILITY inline
852typename vector<_Tp, _Allocator>::size_type
853vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
854{
855 const size_type __ms = max_size();
856 if (__new_size > __ms)
857 this->__throw_length_error();
858 const size_type __cap = capacity();
859 if (__cap >= __ms / 2)
860 return __ms;
Alexis Hunt991d29b2011-07-29 23:31:58 +0000861 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862}
863
864// Default constructs __n objects starting at __end_
865// throws if construction throws
866// Precondition: __n > 0
867// Precondition: size() + __n <= capacity()
868// Postcondition: size() == size() + __n
869template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000870void
871vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
872{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000873 allocator_type& __a = this->__alloc();
874 do
875 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000876 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877 ++this->__end_;
878 --__n;
879 } while (__n > 0);
880}
881
Howard Hinnantc51e1022010-05-11 19:42:16 +0000882// Copy constructs __n objects starting at __end_ from __x
883// throws if construction throws
884// Precondition: __n > 0
885// Precondition: size() + __n <= capacity()
886// Postcondition: size() == old size() + __n
887// Postcondition: [i] == __x for all i in [size() - __n, __n)
888template <class _Tp, class _Allocator>
889_LIBCPP_INLINE_VISIBILITY inline
890void
891vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
892{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000893 allocator_type& __a = this->__alloc();
894 do
895 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000896 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000897 ++this->__end_;
898 --__n;
899 } while (__n > 0);
900}
901
902template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000903template <class _ForwardIterator>
904typename enable_if
905<
906 __is_forward_iterator<_ForwardIterator>::value,
907 void
908>::type
909vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
910{
911 allocator_type& __a = this->__alloc();
912 for (; __first != __last; ++__first)
913 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000914 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000915 ++this->__end_;
916 }
917}
918
919template <class _Tp, class _Allocator>
920void
921vector<_Tp, _Allocator>::__move_construct_at_end(pointer __first, pointer __last)
922{
923 allocator_type& __a = this->__alloc();
924 for (; __first != __last; ++__first)
925 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000926 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
927 _VSTD::move(*__first));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928 ++this->__end_;
929 }
930}
931
932// Default constructs __n objects starting at __end_
933// throws if construction throws
934// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +0000935// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000936template <class _Tp, class _Allocator>
937void
938vector<_Tp, _Allocator>::__append(size_type __n)
939{
940 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
941 this->__construct_at_end(__n);
942 else
943 {
944 allocator_type& __a = this->__alloc();
945 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
946 __v.__construct_at_end(__n);
947 __swap_out_circular_buffer(__v);
948 }
949}
950
951// Default constructs __n objects starting at __end_
952// throws if construction throws
953// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +0000954// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000955template <class _Tp, class _Allocator>
956void
957vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
958{
959 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
960 this->__construct_at_end(__n, __x);
961 else
962 {
963 allocator_type& __a = this->__alloc();
964 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
965 __v.__construct_at_end(__n, __x);
966 __swap_out_circular_buffer(__v);
967 }
968}
969
970template <class _Tp, class _Allocator>
971vector<_Tp, _Allocator>::vector(size_type __n)
972{
Howard Hinnant9cd22302011-09-16 18:41:29 +0000973#if _LIBCPP_DEBUG_LEVEL >= 2
974 __get_db()->__insert_c(this);
975#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976 if (__n > 0)
977 {
978 allocate(__n);
979 __construct_at_end(__n);
980 }
981}
982
983template <class _Tp, class _Allocator>
984vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
985{
Howard Hinnant9cd22302011-09-16 18:41:29 +0000986#if _LIBCPP_DEBUG_LEVEL >= 2
987 __get_db()->__insert_c(this);
988#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989 if (__n > 0)
990 {
991 allocate(__n);
992 __construct_at_end(__n, __x);
993 }
994}
995
996template <class _Tp, class _Allocator>
997vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
998 : __base(__a)
999{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001000#if _LIBCPP_DEBUG_LEVEL >= 2
1001 __get_db()->__insert_c(this);
1002#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003 if (__n > 0)
1004 {
1005 allocate(__n);
1006 __construct_at_end(__n, __x);
1007 }
1008}
1009
1010template <class _Tp, class _Allocator>
1011template <class _InputIterator>
1012vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
1013 typename enable_if<__is_input_iterator <_InputIterator>::value &&
1014 !__is_forward_iterator<_InputIterator>::value>::type*)
1015{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001016#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001017 __get_db()->__insert_c(this);
1018#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001019 for (; __first != __last; ++__first)
1020 push_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001021}
1022
1023template <class _Tp, class _Allocator>
1024template <class _InputIterator>
1025vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1026 typename enable_if<__is_input_iterator <_InputIterator>::value &&
1027 !__is_forward_iterator<_InputIterator>::value>::type*)
1028 : __base(__a)
1029{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001030#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001031 __get_db()->__insert_c(this);
1032#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001033 for (; __first != __last; ++__first)
1034 push_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035}
1036
1037template <class _Tp, class _Allocator>
1038template <class _ForwardIterator>
1039vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
1040 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
1041{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001042#if _LIBCPP_DEBUG_LEVEL >= 2
1043 __get_db()->__insert_c(this);
1044#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001045 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046 if (__n > 0)
1047 {
1048 allocate(__n);
1049 __construct_at_end(__first, __last);
1050 }
1051}
1052
1053template <class _Tp, class _Allocator>
1054template <class _ForwardIterator>
1055vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
1056 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
1057 : __base(__a)
1058{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001059#if _LIBCPP_DEBUG_LEVEL >= 2
1060 __get_db()->__insert_c(this);
1061#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001062 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063 if (__n > 0)
1064 {
1065 allocate(__n);
1066 __construct_at_end(__first, __last);
1067 }
1068}
1069
1070template <class _Tp, class _Allocator>
1071vector<_Tp, _Allocator>::vector(const vector& __x)
1072 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1073{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001074#if _LIBCPP_DEBUG_LEVEL >= 2
1075 __get_db()->__insert_c(this);
1076#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077 size_type __n = __x.size();
1078 if (__n > 0)
1079 {
1080 allocate(__n);
1081 __construct_at_end(__x.__begin_, __x.__end_);
1082 }
1083}
1084
1085template <class _Tp, class _Allocator>
1086vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1087 : __base(__a)
1088{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001089#if _LIBCPP_DEBUG_LEVEL >= 2
1090 __get_db()->__insert_c(this);
1091#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092 size_type __n = __x.size();
1093 if (__n > 0)
1094 {
1095 allocate(__n);
1096 __construct_at_end(__x.__begin_, __x.__end_);
1097 }
1098}
1099
Howard Hinnant74279a52010-09-04 23:28:19 +00001100#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101
1102template <class _Tp, class _Allocator>
1103_LIBCPP_INLINE_VISIBILITY inline
1104vector<_Tp, _Allocator>::vector(vector&& __x)
Howard Hinnant1c936782011-06-03 19:40:40 +00001105 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001106 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001107{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001108#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109 __x.__invalidate_all_iterators();
Howard Hinnant27e0e772011-09-14 18:33:51 +00001110 __get_db()->__insert_c(this);
1111#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001112 this->__begin_ = __x.__begin_;
1113 this->__end_ = __x.__end_;
1114 this->__end_cap() = __x.__end_cap();
1115 __x.__begin_ = __x.__end_ = __x.__end_cap() = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116}
1117
1118template <class _Tp, class _Allocator>
1119_LIBCPP_INLINE_VISIBILITY inline
1120vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1121 : __base(__a)
1122{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001123#if _LIBCPP_DEBUG_LEVEL >= 2
1124 __get_db()->__insert_c(this);
1125#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126 if (__a == __x.__alloc())
1127 {
1128 this->__begin_ = __x.__begin_;
1129 this->__end_ = __x.__end_;
1130 this->__end_cap() = __x.__end_cap();
1131 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
1132 __x.__invalidate_all_iterators();
1133 }
1134 else
1135 {
1136 typedef move_iterator<iterator> _I;
1137 assign(_I(__x.begin()), _I(__x.end()));
1138 }
1139}
1140
Howard Hinnant33711792011-08-12 21:56:02 +00001141#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1142
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143template <class _Tp, class _Allocator>
1144_LIBCPP_INLINE_VISIBILITY inline
1145vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1146{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001147#if _LIBCPP_DEBUG_LEVEL >= 2
1148 __get_db()->__insert_c(this);
1149#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150 if (__il.size() > 0)
1151 {
1152 allocate(__il.size());
1153 __construct_at_end(__il.begin(), __il.end());
1154 }
1155}
1156
1157template <class _Tp, class _Allocator>
1158_LIBCPP_INLINE_VISIBILITY inline
1159vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1160 : __base(__a)
1161{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001162#if _LIBCPP_DEBUG_LEVEL >= 2
1163 __get_db()->__insert_c(this);
1164#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 if (__il.size() > 0)
1166 {
1167 allocate(__il.size());
1168 __construct_at_end(__il.begin(), __il.end());
1169 }
1170}
1171
Howard Hinnant33711792011-08-12 21:56:02 +00001172#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1173
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174template <class _Tp, class _Allocator>
1175_LIBCPP_INLINE_VISIBILITY inline
1176vector<_Tp, _Allocator>&
1177vector<_Tp, _Allocator>::operator=(vector&& __x)
Howard Hinnant1c936782011-06-03 19:40:40 +00001178 _NOEXCEPT_(
1179 __alloc_traits::propagate_on_container_move_assignment::value &&
1180 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181{
1182 __move_assign(__x, integral_constant<bool,
1183 __alloc_traits::propagate_on_container_move_assignment::value>());
1184 return *this;
1185}
1186
1187template <class _Tp, class _Allocator>
1188void
1189vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1190{
1191 if (__base::__alloc() != __c.__alloc())
1192 {
1193 typedef move_iterator<iterator> _I;
1194 assign(_I(__c.begin()), _I(__c.end()));
1195 }
1196 else
1197 __move_assign(__c, true_type());
1198}
1199
1200template <class _Tp, class _Allocator>
1201void
1202vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001203 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001204{
1205 deallocate();
1206 this->__begin_ = __c.__begin_;
1207 this->__end_ = __c.__end_;
1208 this->__end_cap() = __c.__end_cap();
1209 __base::__move_assign_alloc(__c);
1210 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
1211}
1212
Howard Hinnant74279a52010-09-04 23:28:19 +00001213#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214
1215template <class _Tp, class _Allocator>
1216_LIBCPP_INLINE_VISIBILITY inline
1217vector<_Tp, _Allocator>&
1218vector<_Tp, _Allocator>::operator=(const vector& __x)
1219{
1220 if (this != &__x)
1221 {
1222 __base::__copy_assign_alloc(__x);
1223 assign(__x.__begin_, __x.__end_);
1224 }
1225 return *this;
1226}
1227
1228template <class _Tp, class _Allocator>
1229template <class _InputIterator>
1230typename enable_if
1231<
1232 __is_input_iterator <_InputIterator>::value &&
1233 !__is_forward_iterator<_InputIterator>::value,
1234 void
1235>::type
1236vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1237{
1238 clear();
1239 for (; __first != __last; ++__first)
1240 push_back(*__first);
1241}
1242
1243template <class _Tp, class _Allocator>
1244template <class _ForwardIterator>
1245typename enable_if
1246<
1247 __is_forward_iterator<_ForwardIterator>::value,
1248 void
1249>::type
1250vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1251{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001252 typename iterator_traits<_ForwardIterator>::difference_type __new_size = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253 if (static_cast<size_type>(__new_size) <= capacity())
1254 {
1255 _ForwardIterator __mid = __last;
1256 bool __growing = false;
1257 if (static_cast<size_type>(__new_size) > size())
1258 {
1259 __growing = true;
1260 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001261 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001263 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264 if (__growing)
1265 __construct_at_end(__mid, __last);
1266 else
1267 this->__destruct_at_end(__m);
1268 }
1269 else
1270 {
1271 deallocate();
1272 allocate(__recommend(static_cast<size_type>(__new_size)));
1273 __construct_at_end(__first, __last);
1274 }
1275}
1276
1277template <class _Tp, class _Allocator>
1278void
1279vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1280{
1281 if (__n <= capacity())
1282 {
1283 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001284 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285 if (__n > __s)
1286 __construct_at_end(__n - __s, __u);
1287 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001288 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289 }
1290 else
1291 {
1292 deallocate();
1293 allocate(__recommend(static_cast<size_type>(__n)));
1294 __construct_at_end(__n, __u);
1295 }
1296}
1297
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001298template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001299_LIBCPP_INLINE_VISIBILITY inline
1300typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001301vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001303#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304 return iterator(this, __p);
1305#else
1306 return iterator(__p);
1307#endif
1308}
1309
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001310template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001311_LIBCPP_INLINE_VISIBILITY inline
1312typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001313vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001314{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001315#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001316 return const_iterator(this, __p);
1317#else
1318 return const_iterator(__p);
1319#endif
1320}
1321
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001322template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323_LIBCPP_INLINE_VISIBILITY inline
1324typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001325vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326{
1327 return __make_iter(this->__begin_);
1328}
1329
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001330template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331_LIBCPP_INLINE_VISIBILITY inline
1332typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001333vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334{
1335 return __make_iter(this->__begin_);
1336}
1337
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001338template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001339_LIBCPP_INLINE_VISIBILITY inline
1340typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001341vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001342{
1343 return __make_iter(this->__end_);
1344}
1345
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001346template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347_LIBCPP_INLINE_VISIBILITY inline
1348typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001349vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001350{
1351 return __make_iter(this->__end_);
1352}
1353
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001354template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355_LIBCPP_INLINE_VISIBILITY inline
1356typename vector<_Tp, _Allocator>::reference
1357vector<_Tp, _Allocator>::operator[](size_type __n)
1358{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001359 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001360 return this->__begin_[__n];
1361}
1362
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001363template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001364_LIBCPP_INLINE_VISIBILITY inline
1365typename vector<_Tp, _Allocator>::const_reference
1366vector<_Tp, _Allocator>::operator[](size_type __n) const
1367{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001368 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369 return this->__begin_[__n];
1370}
1371
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001372template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001373typename vector<_Tp, _Allocator>::reference
1374vector<_Tp, _Allocator>::at(size_type __n)
1375{
1376 if (__n >= size())
1377 this->__throw_out_of_range();
1378 return this->__begin_[__n];
1379}
1380
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001381template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382typename vector<_Tp, _Allocator>::const_reference
1383vector<_Tp, _Allocator>::at(size_type __n) const
1384{
1385 if (__n >= size())
1386 this->__throw_out_of_range();
1387 return this->__begin_[__n];
1388}
1389
1390template <class _Tp, class _Allocator>
1391void
1392vector<_Tp, _Allocator>::reserve(size_type __n)
1393{
1394 if (__n > capacity())
1395 {
1396 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001397 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001398 __swap_out_circular_buffer(__v);
1399 }
1400}
1401
1402template <class _Tp, class _Allocator>
1403void
Howard Hinnant1c936782011-06-03 19:40:40 +00001404vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405{
1406 if (capacity() > size())
1407 {
1408#ifndef _LIBCPP_NO_EXCEPTIONS
1409 try
1410 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001411#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001412 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001413 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001414 __swap_out_circular_buffer(__v);
1415#ifndef _LIBCPP_NO_EXCEPTIONS
1416 }
1417 catch (...)
1418 {
1419 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001420#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421 }
1422}
1423
1424template <class _Tp, class _Allocator>
1425void
1426vector<_Tp, _Allocator>::push_back(const_reference __x)
1427{
1428 if (this->__end_ < this->__end_cap())
1429 {
1430 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001431 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432 ++this->__end_;
1433 }
1434 else
1435 {
1436 allocator_type& __a = this->__alloc();
1437 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1438 __v.push_back(__x);
1439 __swap_out_circular_buffer(__v);
1440 }
1441}
1442
Howard Hinnant74279a52010-09-04 23:28:19 +00001443#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001444
1445template <class _Tp, class _Allocator>
1446void
1447vector<_Tp, _Allocator>::push_back(value_type&& __x)
1448{
1449 if (this->__end_ < this->__end_cap())
1450 {
1451 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001452 _VSTD::__to_raw_pointer(this->__end_),
1453 _VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454 ++this->__end_;
1455 }
1456 else
1457 {
1458 allocator_type& __a = this->__alloc();
1459 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001460 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461 __swap_out_circular_buffer(__v);
1462 }
1463}
1464
Howard Hinnant74279a52010-09-04 23:28:19 +00001465#ifndef _LIBCPP_HAS_NO_VARIADICS
1466
Howard Hinnantc51e1022010-05-11 19:42:16 +00001467template <class _Tp, class _Allocator>
1468template <class... _Args>
1469void
1470vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1471{
1472 if (this->__end_ < this->__end_cap())
1473 {
1474 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001475 _VSTD::__to_raw_pointer(this->__end_),
1476 _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477 ++this->__end_;
1478 }
1479 else
1480 {
1481 allocator_type& __a = this->__alloc();
1482 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001483 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001484 __swap_out_circular_buffer(__v);
1485 }
1486}
1487
Howard Hinnant74279a52010-09-04 23:28:19 +00001488#endif // _LIBCPP_HAS_NO_VARIADICS
1489#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490
1491template <class _Tp, class _Allocator>
1492_LIBCPP_INLINE_VISIBILITY inline
1493void
1494vector<_Tp, _Allocator>::pop_back()
1495{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001496 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001497 this->__destruct_at_end(this->__end_ - 1);
1498}
1499
1500template <class _Tp, class _Allocator>
1501_LIBCPP_INLINE_VISIBILITY inline
1502typename vector<_Tp, _Allocator>::iterator
1503vector<_Tp, _Allocator>::erase(const_iterator __position)
1504{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001505#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001506 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1507 "vector::erase(iterator) called with an iterator not"
1508 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001509#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001510 pointer __p = const_cast<pointer>(&*__position);
1511 iterator __r = __make_iter(__p);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001512 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001513 return __r;
1514}
1515
1516template <class _Tp, class _Allocator>
1517typename vector<_Tp, _Allocator>::iterator
1518vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1519{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001520#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001521 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1522 "vector::erase(iterator, iterator) called with an iterator not"
1523 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001524#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001525 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001526 pointer __p = this->__begin_ + (__first - begin());
1527 iterator __r = __make_iter(__p);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001528 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529 return __r;
1530}
1531
1532template <class _Tp, class _Allocator>
1533void
1534vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1535{
1536 pointer __old_last = this->__end_;
1537 difference_type __n = __old_last - __to;
1538 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1539 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001540 _VSTD::__to_raw_pointer(this->__end_),
1541 _VSTD::move(*__i));
1542 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543}
1544
1545template <class _Tp, class _Allocator>
1546typename vector<_Tp, _Allocator>::iterator
1547vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1548{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001549#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001550 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1551 "vector::insert(iterator, x) called with an iterator not"
1552 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001553#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 pointer __p = this->__begin_ + (__position - begin());
1555 if (this->__end_ < this->__end_cap())
1556 {
1557 if (__p == this->__end_)
1558 {
1559 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001560 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 ++this->__end_;
1562 }
1563 else
1564 {
1565 __move_range(__p, this->__end_, __p + 1);
1566 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1567 if (__p <= __xr && __xr < this->__end_)
1568 ++__xr;
1569 *__p = *__xr;
1570 }
1571 }
1572 else
1573 {
1574 allocator_type& __a = this->__alloc();
1575 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1576 __v.push_back(__x);
1577 __p = __swap_out_circular_buffer(__v, __p);
1578 }
1579 return __make_iter(__p);
1580}
1581
Howard Hinnant74279a52010-09-04 23:28:19 +00001582#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583
1584template <class _Tp, class _Allocator>
1585typename vector<_Tp, _Allocator>::iterator
1586vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1587{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001588#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001589 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1590 "vector::insert(iterator, x) called with an iterator not"
1591 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001592#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 pointer __p = this->__begin_ + (__position - begin());
1594 if (this->__end_ < this->__end_cap())
1595 {
1596 if (__p == this->__end_)
1597 {
1598 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001599 _VSTD::__to_raw_pointer(this->__end_),
1600 _VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601 ++this->__end_;
1602 }
1603 else
1604 {
1605 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001606 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607 }
1608 }
1609 else
1610 {
1611 allocator_type& __a = this->__alloc();
1612 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001613 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001614 __p = __swap_out_circular_buffer(__v, __p);
1615 }
1616 return __make_iter(__p);
1617}
1618
Howard Hinnant74279a52010-09-04 23:28:19 +00001619#ifndef _LIBCPP_HAS_NO_VARIADICS
1620
Howard Hinnantc51e1022010-05-11 19:42:16 +00001621template <class _Tp, class _Allocator>
1622template <class... _Args>
1623typename vector<_Tp, _Allocator>::iterator
1624vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1625{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001626#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001627 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1628 "vector::emplace(iterator, x) called with an iterator not"
1629 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001630#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001631 pointer __p = this->__begin_ + (__position - begin());
1632 if (this->__end_ < this->__end_cap())
1633 {
1634 if (__p == this->__end_)
1635 {
1636 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001637 _VSTD::__to_raw_pointer(this->__end_),
1638 _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001639 ++this->__end_;
1640 }
1641 else
1642 {
1643 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001644 *__p = value_type(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001645 }
1646 }
1647 else
1648 {
1649 allocator_type& __a = this->__alloc();
1650 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001651 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001652 __p = __swap_out_circular_buffer(__v, __p);
1653 }
1654 return __make_iter(__p);
1655}
1656
Howard Hinnant74279a52010-09-04 23:28:19 +00001657#endif // _LIBCPP_HAS_NO_VARIADICS
1658#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659
1660template <class _Tp, class _Allocator>
1661typename vector<_Tp, _Allocator>::iterator
1662vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1663{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001664#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001665 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1666 "vector::insert(iterator, n, x) called with an iterator not"
1667 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001668#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669 pointer __p = this->__begin_ + (__position - begin());
1670 if (__n > 0)
1671 {
1672 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1673 {
1674 size_type __old_n = __n;
1675 pointer __old_last = this->__end_;
1676 if (__n > static_cast<size_type>(this->__end_ - __p))
1677 {
1678 size_type __cx = __n - (this->__end_ - __p);
1679 __construct_at_end(__cx, __x);
1680 __n -= __cx;
1681 }
1682 if (__n > 0)
1683 {
1684 __move_range(__p, __old_last, __p + __old_n);
1685 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1686 if (__p <= __xr && __xr < this->__end_)
1687 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001688 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001689 }
1690 }
1691 else
1692 {
1693 allocator_type& __a = this->__alloc();
1694 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1695 __v.__construct_at_end(__n, __x);
1696 __p = __swap_out_circular_buffer(__v, __p);
1697 }
1698 }
1699 return __make_iter(__p);
1700}
1701
1702template <class _Tp, class _Allocator>
1703template <class _InputIterator>
1704typename enable_if
1705<
1706 __is_input_iterator <_InputIterator>::value &&
1707 !__is_forward_iterator<_InputIterator>::value,
1708 typename vector<_Tp, _Allocator>::iterator
1709>::type
1710vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1711{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001712#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001713 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1714 "vector::insert(iterator, range) called with an iterator not"
1715 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001716#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717 difference_type __off = __position - begin();
1718 pointer __p = this->__begin_ + __off;
1719 allocator_type& __a = this->__alloc();
1720 pointer __old_last = this->__end_;
1721 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1722 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001723 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724 *__first);
1725 ++this->__end_;
1726 }
1727 __split_buffer<value_type, allocator_type&> __v(__a);
1728 if (__first != __last)
1729 {
1730#ifndef _LIBCPP_NO_EXCEPTIONS
1731 try
1732 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001733#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001734 __v.__construct_at_end(__first, __last);
1735 difference_type __old_size = __old_last - this->__begin_;
1736 difference_type __old_p = __p - this->__begin_;
1737 reserve(__recommend(size() + __v.size()));
1738 __p = this->__begin_ + __old_p;
1739 __old_last = this->__begin_ + __old_size;
1740#ifndef _LIBCPP_NO_EXCEPTIONS
1741 }
1742 catch (...)
1743 {
1744 erase(__make_iter(__old_last), end());
1745 throw;
1746 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001747#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001748 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001749 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant9cd22302011-09-16 18:41:29 +00001750 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1751 make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752 return begin() + __off;
1753}
1754
1755template <class _Tp, class _Allocator>
1756template <class _ForwardIterator>
1757typename enable_if
1758<
1759 __is_forward_iterator<_ForwardIterator>::value,
1760 typename vector<_Tp, _Allocator>::iterator
1761>::type
1762vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1763{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001764#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001765 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1766 "vector::insert(iterator, range) called with an iterator not"
1767 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001768#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001769 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001770 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001771 if (__n > 0)
1772 {
1773 if (__n <= this->__end_cap() - this->__end_)
1774 {
1775 size_type __old_n = __n;
1776 pointer __old_last = this->__end_;
1777 _ForwardIterator __m = __last;
1778 difference_type __dx = this->__end_ - __p;
1779 if (__n > __dx)
1780 {
1781 __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001782 _VSTD::advance(__m, this->__end_ - __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001783 __construct_at_end(__m, __last);
1784 __n = __dx;
1785 }
1786 if (__n > 0)
1787 {
1788 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001789 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001790 }
1791 }
1792 else
1793 {
1794 allocator_type& __a = this->__alloc();
1795 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1796 __v.__construct_at_end(__first, __last);
1797 __p = __swap_out_circular_buffer(__v, __p);
1798 }
1799 }
1800 return __make_iter(__p);
1801}
1802
1803template <class _Tp, class _Allocator>
1804void
1805vector<_Tp, _Allocator>::resize(size_type __sz)
1806{
1807 size_type __cs = size();
1808 if (__cs < __sz)
1809 this->__append(__sz - __cs);
1810 else if (__cs > __sz)
1811 this->__destruct_at_end(this->__begin_ + __sz);
1812}
1813
1814template <class _Tp, class _Allocator>
1815void
1816vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
1817{
1818 size_type __cs = size();
1819 if (__cs < __sz)
1820 this->__append(__sz - __cs, __x);
1821 else if (__cs > __sz)
1822 this->__destruct_at_end(this->__begin_ + __sz);
1823}
1824
1825template <class _Tp, class _Allocator>
1826void
1827vector<_Tp, _Allocator>::swap(vector& __x)
Howard Hinnant1c936782011-06-03 19:40:40 +00001828 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1829 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001830{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001831 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
1832 this->__alloc() == __x.__alloc(),
1833 "vector::swap: Either propagate_on_container_swap must be true"
1834 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001835 _VSTD::swap(this->__begin_, __x.__begin_);
1836 _VSTD::swap(this->__end_, __x.__end_);
1837 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838 __base::__swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001839#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001840 __get_db()->swap(this, &__x);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001841#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001842}
1843
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001844template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845bool
1846vector<_Tp, _Allocator>::__invariants() const
1847{
1848 if (this->__begin_ == 0)
1849 {
1850 if (this->__end_ != 0 || this->__end_cap() != 0)
1851 return false;
1852 }
1853 else
1854 {
1855 if (this->__begin_ > this->__end_)
1856 return false;
1857 if (this->__begin_ == this->__end_cap())
1858 return false;
1859 if (this->__end_ > this->__end_cap())
1860 return false;
1861 }
1862 return true;
1863}
1864
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001865#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001866
Howard Hinnantc51e1022010-05-11 19:42:16 +00001867template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00001868bool
1869vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
1870{
1871 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
1872}
1873
1874template <class _Tp, class _Allocator>
1875bool
1876vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
1877{
1878 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
1879}
1880
1881template <class _Tp, class _Allocator>
1882bool
1883vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
1884{
1885 const_pointer __p = __i->base() + __n;
1886 return this->__begin_ <= __p && __p <= this->__end_;
1887}
1888
1889template <class _Tp, class _Allocator>
1890bool
1891vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
1892{
1893 const_pointer __p = __i->base() + __n;
1894 return this->__begin_ <= __p && __p < this->__end_;
1895}
1896
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001897#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001898
1899template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001900_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901void
1902vector<_Tp, _Allocator>::__invalidate_all_iterators()
1903{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001904#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001905 __get_db()->__invalidate_all(this);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001906#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001907}
1908
1909// vector<bool>
1910
1911template <class _Allocator> class vector<bool, _Allocator>;
1912
1913template <class _Allocator> struct hash<vector<bool, _Allocator> >;
1914
1915template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00001916struct __has_storage_type<vector<bool, _Allocator> >
1917{
1918 static const bool value = true;
1919};
1920
1921template <class _Allocator>
Howard Hinnanta99f0ce2010-09-10 16:42:26 +00001922class _LIBCPP_VISIBLE vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001923 : private __vector_base_common<true>
1924{
1925public:
1926 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001927 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001928 typedef _Allocator allocator_type;
1929 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930 typedef typename __alloc_traits::size_type size_type;
1931 typedef typename __alloc_traits::difference_type difference_type;
1932 typedef __bit_iterator<vector, false> pointer;
1933 typedef __bit_iterator<vector, true> const_pointer;
1934#ifdef _LIBCPP_DEBUG
1935 typedef __debug_iter<vector, pointer> iterator;
1936 typedef __debug_iter<vector, const_pointer> const_iterator;
1937
1938 friend class __debug_iter<vector, pointer>;
1939 friend class __debug_iter<vector, const_pointer>;
1940
1941 pair<iterator*, const_iterator*> __iterator_list_;
1942
1943 _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;}
1944 _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001945#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946 typedef pointer iterator;
1947 typedef const_pointer const_iterator;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001948#endif // _LIBCPP_DEBUG
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001949 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1950 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951
1952private:
1953 typedef size_type __storage_type;
1954 typedef typename __alloc_traits::template
1955#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1956 rebind_alloc<__storage_type>
1957#else
1958 rebind_alloc<__storage_type>::other
1959#endif
1960 __storage_allocator;
1961 typedef allocator_traits<__storage_allocator> __storage_traits;
1962 typedef typename __storage_traits::pointer __storage_pointer;
1963 typedef typename __storage_traits::const_pointer __const_storage_pointer;
1964
1965 __storage_pointer __begin_;
1966 size_type __size_;
1967 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00001968public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00001969 typedef __bit_reference<vector> reference;
1970 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00001971private:
Howard Hinnant1c936782011-06-03 19:40:40 +00001972 _LIBCPP_INLINE_VISIBILITY
1973 size_type& __cap() _NOEXCEPT
1974 {return __cap_alloc_.first();}
1975 _LIBCPP_INLINE_VISIBILITY
1976 const size_type& __cap() const _NOEXCEPT
1977 {return __cap_alloc_.first();}
1978 _LIBCPP_INLINE_VISIBILITY
1979 __storage_allocator& __alloc() _NOEXCEPT
1980 {return __cap_alloc_.second();}
1981 _LIBCPP_INLINE_VISIBILITY
1982 const __storage_allocator& __alloc() const _NOEXCEPT
1983 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001984
1985 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
1986
Howard Hinnant1c936782011-06-03 19:40:40 +00001987 _LIBCPP_INLINE_VISIBILITY
1988 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001989 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00001990 _LIBCPP_INLINE_VISIBILITY
1991 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992 {return (__n - 1) / __bits_per_word + 1;}
1993
1994public:
Howard Hinnant1c936782011-06-03 19:40:40 +00001995 _LIBCPP_INLINE_VISIBILITY
1996 vector()
1997 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001998 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999 ~vector();
2000 explicit vector(size_type __n);
2001 vector(size_type __n, const value_type& __v);
2002 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2003 template <class _InputIterator>
2004 vector(_InputIterator __first, _InputIterator __last,
2005 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2006 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2007 template <class _InputIterator>
2008 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2009 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2010 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2011 template <class _ForwardIterator>
2012 vector(_ForwardIterator __first, _ForwardIterator __last,
2013 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2014 template <class _ForwardIterator>
2015 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2016 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2017
2018 vector(const vector& __v);
2019 vector(const vector& __v, const allocator_type& __a);
2020 vector& operator=(const vector& __v);
Howard Hinnant33711792011-08-12 21:56:02 +00002021#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022 vector(initializer_list<value_type> __il);
2023 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnant33711792011-08-12 21:56:02 +00002024#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002025
Howard Hinnant74279a52010-09-04 23:28:19 +00002026#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1c936782011-06-03 19:40:40 +00002027 _LIBCPP_INLINE_VISIBILITY
2028 vector(vector&& __v)
2029 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002030 vector(vector&& __v, const allocator_type& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002031 _LIBCPP_INLINE_VISIBILITY
2032 vector& operator=(vector&& __v)
2033 _NOEXCEPT_(
2034 __alloc_traits::propagate_on_container_move_assignment::value &&
2035 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant74279a52010-09-04 23:28:19 +00002036#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant33711792011-08-12 21:56:02 +00002037#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002038 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002039 vector& operator=(initializer_list<value_type> __il)
2040 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnant33711792011-08-12 21:56:02 +00002041#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002042
2043 template <class _InputIterator>
2044 typename enable_if
2045 <
2046 __is_input_iterator<_InputIterator>::value &&
2047 !__is_forward_iterator<_InputIterator>::value,
2048 void
2049 >::type
2050 assign(_InputIterator __first, _InputIterator __last);
2051 template <class _ForwardIterator>
2052 typename enable_if
2053 <
2054 __is_forward_iterator<_ForwardIterator>::value,
2055 void
2056 >::type
2057 assign(_ForwardIterator __first, _ForwardIterator __last);
2058
2059 void assign(size_type __n, const value_type& __x);
Howard Hinnant33711792011-08-12 21:56:02 +00002060#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002061 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002062 void assign(initializer_list<value_type> __il)
2063 {assign(__il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +00002064#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002065
Howard Hinnant1c936782011-06-03 19:40:40 +00002066 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067 {return allocator_type(this->__alloc());}
2068
Howard Hinnant1c936782011-06-03 19:40:40 +00002069 size_type max_size() const _NOEXCEPT;
2070 _LIBCPP_INLINE_VISIBILITY
2071 size_type capacity() const _NOEXCEPT
2072 {return __internal_cap_to_external(__cap());}
2073 _LIBCPP_INLINE_VISIBILITY
2074 size_type size() const _NOEXCEPT
2075 {return __size_;}
2076 _LIBCPP_INLINE_VISIBILITY
2077 bool empty() const _NOEXCEPT
2078 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002079 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002080 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002081
Howard Hinnant1c936782011-06-03 19:40:40 +00002082 _LIBCPP_INLINE_VISIBILITY
2083 iterator begin() _NOEXCEPT
2084 {return __make_iter(0);}
2085 _LIBCPP_INLINE_VISIBILITY
2086 const_iterator begin() const _NOEXCEPT
2087 {return __make_iter(0);}
2088 _LIBCPP_INLINE_VISIBILITY
2089 iterator end() _NOEXCEPT
2090 {return __make_iter(__size_);}
2091 _LIBCPP_INLINE_VISIBILITY
2092 const_iterator end() const _NOEXCEPT
2093 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002094
Howard Hinnant1c936782011-06-03 19:40:40 +00002095 _LIBCPP_INLINE_VISIBILITY
2096 reverse_iterator rbegin() _NOEXCEPT
2097 {return reverse_iterator(end());}
2098 _LIBCPP_INLINE_VISIBILITY
2099 const_reverse_iterator rbegin() const _NOEXCEPT
2100 {return const_reverse_iterator(end());}
2101 _LIBCPP_INLINE_VISIBILITY
2102 reverse_iterator rend() _NOEXCEPT
2103 {return reverse_iterator(begin());}
2104 _LIBCPP_INLINE_VISIBILITY
2105 const_reverse_iterator rend() const _NOEXCEPT
2106 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002107
Howard Hinnant1c936782011-06-03 19:40:40 +00002108 _LIBCPP_INLINE_VISIBILITY
2109 const_iterator cbegin() const _NOEXCEPT
2110 {return __make_iter(0);}
2111 _LIBCPP_INLINE_VISIBILITY
2112 const_iterator cend() const _NOEXCEPT
2113 {return __make_iter(__size_);}
2114 _LIBCPP_INLINE_VISIBILITY
2115 const_reverse_iterator crbegin() const _NOEXCEPT
2116 {return rbegin();}
2117 _LIBCPP_INLINE_VISIBILITY
2118 const_reverse_iterator crend() const _NOEXCEPT
2119 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002120
2121 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2122 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2123 reference at(size_type __n);
2124 const_reference at(size_type __n) const;
2125
2126 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2127 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2128 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2129 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2130
2131 void push_back(const value_type& __x);
2132 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2133
2134 iterator insert(const_iterator __position, const value_type& __x);
2135 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2136 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2137 template <class _InputIterator>
2138 typename enable_if
2139 <
2140 __is_input_iterator <_InputIterator>::value &&
2141 !__is_forward_iterator<_InputIterator>::value,
2142 iterator
2143 >::type
2144 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2145 template <class _ForwardIterator>
2146 typename enable_if
2147 <
2148 __is_forward_iterator<_ForwardIterator>::value,
2149 iterator
2150 >::type
2151 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant33711792011-08-12 21:56:02 +00002152#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002154 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2155 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +00002156#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002157
Howard Hinnantcf823322010-12-17 14:46:43 +00002158 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002159 iterator erase(const_iterator __first, const_iterator __last);
2160
Howard Hinnant1c936782011-06-03 19:40:40 +00002161 _LIBCPP_INLINE_VISIBILITY
2162 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002163
Howard Hinnant1c936782011-06-03 19:40:40 +00002164 void swap(vector&)
2165 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2166 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167
2168 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002169 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170
2171 bool __invariants() const;
2172
2173private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002174 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002175 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002176 void deallocate() _NOEXCEPT;
2177 _LIBCPP_INLINE_VISIBILITY
2178 static size_type __align(size_type __new_size) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179 {return __new_size + (__bits_per_word-1) & ~(__bits_per_word-1);};
Howard Hinnantcf823322010-12-17 14:46:43 +00002180 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2181 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002182 template <class _ForwardIterator>
2183 typename enable_if
2184 <
2185 __is_forward_iterator<_ForwardIterator>::value,
2186 void
2187 >::type
2188 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2189 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002190 _LIBCPP_INLINE_VISIBILITY
2191 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002192 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002193 _LIBCPP_INLINE_VISIBILITY
2194 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
2196#ifdef _LIBCPP_DEBUG
2197 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_type __pos)
2198 {return iterator(this, pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2199 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_type __pos) const
2200 {return const_iterator(this, const_pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2201 _LIBCPP_INLINE_VISIBILITY iterator __const_iterator_cast(const_iterator __p)
2202 {return iterator(this, pointer(const_cast<__storage_pointer>(__p.base().__seg_), __p.base().__ctz_));}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002203#else // _LIBCPP_DEBUG
Howard Hinnant1c936782011-06-03 19:40:40 +00002204 _LIBCPP_INLINE_VISIBILITY
2205 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002206 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002207 _LIBCPP_INLINE_VISIBILITY
2208 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002210 _LIBCPP_INLINE_VISIBILITY
2211 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212 {return iterator(const_cast<__storage_pointer>(__p.__seg_), __p.__ctz_);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002213#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216 void __copy_assign_alloc(const vector& __v)
2217 {__copy_assign_alloc(__v, integral_constant<bool,
2218 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002219 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220 void __copy_assign_alloc(const vector& __c, true_type)
2221 {
2222 if (__alloc() != __c.__alloc())
2223 deallocate();
2224 __alloc() = __c.__alloc();
2225 }
2226
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228 void __copy_assign_alloc(const vector& __c, false_type)
2229 {}
2230
2231 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002232 void __move_assign(vector& __c, true_type)
2233 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002235 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002236 _NOEXCEPT_(
2237 !__storage_traits::propagate_on_container_move_assignment::value ||
2238 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239 {__move_assign_alloc(__c, integral_constant<bool,
2240 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002242 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002243 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002245 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246 }
2247
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002249 void __move_assign_alloc(vector& __c, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002250 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251 {}
2252
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00002255 _NOEXCEPT_(
2256 !__storage_traits::propagate_on_container_swap::value ||
2257 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258 {__swap_alloc(__x, __y, integral_constant<bool,
2259 __storage_traits::propagate_on_container_swap::value>());}
2260
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002263 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002265 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002266 swap(__x, __y);
2267 }
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002270 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002271 {}
2272
Howard Hinnant1c936782011-06-03 19:40:40 +00002273 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002274
2275 friend class __bit_reference<vector>;
2276 friend class __bit_const_reference<vector>;
2277 friend class __bit_iterator<vector, false>;
2278 friend class __bit_iterator<vector, true>;
2279 friend class __bit_array<vector>;
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002280 friend struct _LIBCPP_VISIBLE hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002281};
2282
2283template <class _Allocator>
2284#ifndef _LIBCPP_DEBUG
2285_LIBCPP_INLINE_VISIBILITY inline
2286#endif
2287void
2288vector<bool, _Allocator>::__invalidate_all_iterators()
2289{
2290#ifdef _LIBCPP_DEBUG
2291 iterator::__remove_all(this);
2292 const_iterator::__remove_all(this);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002293#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294}
2295
2296// Allocate space for __n objects
2297// throws length_error if __n > max_size()
2298// throws (probably bad_alloc) if memory run out
2299// Precondition: __begin_ == __end_ == __cap() == 0
2300// Precondition: __n > 0
2301// Postcondition: capacity() == __n
2302// Postcondition: size() == 0
2303template <class _Allocator>
2304void
2305vector<bool, _Allocator>::allocate(size_type __n)
2306{
2307 if (__n > max_size())
2308 this->__throw_length_error();
2309 __n = __external_cap_to_internal(__n);
2310 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2311 this->__size_ = 0;
2312 this->__cap() = __n;
2313}
2314
2315template <class _Allocator>
2316void
Howard Hinnant1c936782011-06-03 19:40:40 +00002317vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002318{
2319 if (this->__begin_ != 0)
2320 {
2321 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2322 __invalidate_all_iterators();
2323 this->__begin_ = 0;
2324 this->__size_ = this->__cap() = 0;
2325 }
2326}
2327
2328template <class _Allocator>
2329typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002330vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002331{
2332 size_type __amax = __storage_traits::max_size(__alloc());
2333 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2334 if (__nmax / __bits_per_word <= __amax)
2335 return __nmax;
2336 return __internal_cap_to_external(__amax);
2337}
2338
2339// Precondition: __new_size > capacity()
2340template <class _Allocator>
2341_LIBCPP_INLINE_VISIBILITY inline
2342typename vector<bool, _Allocator>::size_type
2343vector<bool, _Allocator>::__recommend(size_type __new_size) const
2344{
2345 const size_type __ms = max_size();
2346 if (__new_size > __ms)
2347 this->__throw_length_error();
2348 const size_type __cap = capacity();
2349 if (__cap >= __ms / 2)
2350 return __ms;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002351 return _VSTD::max(2*__cap, __align(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002352}
2353
2354// Default constructs __n objects starting at __end_
2355// Precondition: __n > 0
2356// Precondition: size() + __n <= capacity()
2357// Postcondition: size() == size() + __n
2358template <class _Allocator>
2359_LIBCPP_INLINE_VISIBILITY inline
2360void
2361vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2362{
2363 size_type __old_size = this->__size_;
2364 this->__size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002365 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002366}
2367
2368template <class _Allocator>
2369template <class _ForwardIterator>
2370typename enable_if
2371<
2372 __is_forward_iterator<_ForwardIterator>::value,
2373 void
2374>::type
2375vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2376{
2377 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002378 this->__size_ += _VSTD::distance(__first, __last);
2379 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002380}
2381
2382template <class _Allocator>
2383_LIBCPP_INLINE_VISIBILITY inline
2384vector<bool, _Allocator>::vector()
Howard Hinnant1c936782011-06-03 19:40:40 +00002385 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002386 : __begin_(0),
2387 __size_(0),
2388 __cap_alloc_(0)
2389{
2390}
2391
2392template <class _Allocator>
2393_LIBCPP_INLINE_VISIBILITY inline
2394vector<bool, _Allocator>::vector(const allocator_type& __a)
2395 : __begin_(0),
2396 __size_(0),
2397 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2398{
2399}
2400
2401template <class _Allocator>
2402vector<bool, _Allocator>::vector(size_type __n)
2403 : __begin_(0),
2404 __size_(0),
2405 __cap_alloc_(0)
2406{
2407 if (__n > 0)
2408 {
2409 allocate(__n);
2410 __construct_at_end(__n, false);
2411 }
2412}
2413
2414template <class _Allocator>
2415vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
2416 : __begin_(0),
2417 __size_(0),
2418 __cap_alloc_(0)
2419{
2420 if (__n > 0)
2421 {
2422 allocate(__n);
2423 __construct_at_end(__n, __x);
2424 }
2425}
2426
2427template <class _Allocator>
2428vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
2429 : __begin_(0),
2430 __size_(0),
2431 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2432{
2433 if (__n > 0)
2434 {
2435 allocate(__n);
2436 __construct_at_end(__n, __x);
2437 }
2438}
2439
2440template <class _Allocator>
2441template <class _InputIterator>
2442vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2443 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2444 !__is_forward_iterator<_InputIterator>::value>::type*)
2445 : __begin_(0),
2446 __size_(0),
2447 __cap_alloc_(0)
2448{
2449#ifndef _LIBCPP_NO_EXCEPTIONS
2450 try
2451 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002452#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002453 for (; __first != __last; ++__first)
2454 push_back(*__first);
2455#ifndef _LIBCPP_NO_EXCEPTIONS
2456 }
2457 catch (...)
2458 {
2459 if (__begin_ != 0)
2460 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2461 __invalidate_all_iterators();
2462 throw;
2463 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002464#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465}
2466
2467template <class _Allocator>
2468template <class _InputIterator>
2469vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2470 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2471 !__is_forward_iterator<_InputIterator>::value>::type*)
2472 : __begin_(0),
2473 __size_(0),
2474 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2475{
2476#ifndef _LIBCPP_NO_EXCEPTIONS
2477 try
2478 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002479#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480 for (; __first != __last; ++__first)
2481 push_back(*__first);
2482#ifndef _LIBCPP_NO_EXCEPTIONS
2483 }
2484 catch (...)
2485 {
2486 if (__begin_ != 0)
2487 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2488 __invalidate_all_iterators();
2489 throw;
2490 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002491#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002492}
2493
2494template <class _Allocator>
2495template <class _ForwardIterator>
2496vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2497 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2498 : __begin_(0),
2499 __size_(0),
2500 __cap_alloc_(0)
2501{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002502 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002503 if (__n > 0)
2504 {
2505 allocate(__n);
2506 __construct_at_end(__first, __last);
2507 }
2508}
2509
2510template <class _Allocator>
2511template <class _ForwardIterator>
2512vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2513 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2514 : __begin_(0),
2515 __size_(0),
2516 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2517{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002518 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519 if (__n > 0)
2520 {
2521 allocate(__n);
2522 __construct_at_end(__first, __last);
2523 }
2524}
2525
Howard Hinnant33711792011-08-12 21:56:02 +00002526#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2527
Howard Hinnantc51e1022010-05-11 19:42:16 +00002528template <class _Allocator>
2529vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
2530 : __begin_(0),
2531 __size_(0),
2532 __cap_alloc_(0)
2533{
2534 size_type __n = static_cast<size_type>(__il.size());
2535 if (__n > 0)
2536 {
2537 allocate(__n);
2538 __construct_at_end(__il.begin(), __il.end());
2539 }
2540}
2541
2542template <class _Allocator>
2543vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
2544 : __begin_(0),
2545 __size_(0),
2546 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2547{
2548 size_type __n = static_cast<size_type>(__il.size());
2549 if (__n > 0)
2550 {
2551 allocate(__n);
2552 __construct_at_end(__il.begin(), __il.end());
2553 }
2554}
2555
Howard Hinnant33711792011-08-12 21:56:02 +00002556#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2557
Howard Hinnantc51e1022010-05-11 19:42:16 +00002558template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002559vector<bool, _Allocator>::~vector()
2560{
2561 if (__begin_ != 0)
2562 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2563#ifdef _LIBCPP_DEBUG
2564 __invalidate_all_iterators();
2565#endif
2566}
2567
2568template <class _Allocator>
2569vector<bool, _Allocator>::vector(const vector& __v)
2570 : __begin_(0),
2571 __size_(0),
2572 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2573{
2574 if (__v.size() > 0)
2575 {
2576 allocate(__v.size());
2577 __construct_at_end(__v.begin(), __v.end());
2578 }
2579}
2580
2581template <class _Allocator>
2582vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
2583 : __begin_(0),
2584 __size_(0),
2585 __cap_alloc_(0, __a)
2586{
2587 if (__v.size() > 0)
2588 {
2589 allocate(__v.size());
2590 __construct_at_end(__v.begin(), __v.end());
2591 }
2592}
2593
2594template <class _Allocator>
2595vector<bool, _Allocator>&
2596vector<bool, _Allocator>::operator=(const vector& __v)
2597{
2598 if (this != &__v)
2599 {
2600 __copy_assign_alloc(__v);
2601 if (__v.__size_)
2602 {
2603 if (__v.__size_ > capacity())
2604 {
2605 deallocate();
2606 allocate(__v.__size_);
2607 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002608 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609 }
2610 __size_ = __v.__size_;
2611 }
2612 return *this;
2613}
2614
Howard Hinnant74279a52010-09-04 23:28:19 +00002615#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2616
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617template <class _Allocator>
2618_LIBCPP_INLINE_VISIBILITY inline
2619vector<bool, _Allocator>::vector(vector&& __v)
Howard Hinnant1c936782011-06-03 19:40:40 +00002620 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002621 : __begin_(__v.__begin_),
2622 __size_(__v.__size_),
2623 __cap_alloc_(__v.__cap_alloc_)
2624{
2625 __v.__begin_ = 0;
2626 __v.__size_ = 0;
2627 __v.__cap() = 0;
2628}
2629
2630template <class _Allocator>
2631vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
2632 : __begin_(0),
2633 __size_(0),
2634 __cap_alloc_(0, __a)
2635{
2636 if (__a == allocator_type(__v.__alloc()))
2637 {
2638 this->__begin_ = __v.__begin_;
2639 this->__size_ = __v.__size_;
2640 this->__cap() = __v.__cap();
2641 __v.__begin_ = nullptr;
2642 __v.__cap() = __v.__size_ = 0;
2643 }
2644 else if (__v.size() > 0)
2645 {
2646 allocate(__v.size());
2647 __construct_at_end(__v.begin(), __v.end());
2648 }
2649}
2650
2651template <class _Allocator>
2652_LIBCPP_INLINE_VISIBILITY inline
2653vector<bool, _Allocator>&
2654vector<bool, _Allocator>::operator=(vector&& __v)
Howard Hinnant1c936782011-06-03 19:40:40 +00002655 _NOEXCEPT_(
2656 __alloc_traits::propagate_on_container_move_assignment::value &&
2657 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002658{
2659 __move_assign(__v, integral_constant<bool,
2660 __storage_traits::propagate_on_container_move_assignment::value>());
2661}
2662
2663template <class _Allocator>
2664void
2665vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2666{
2667 if (__alloc() != __c.__alloc())
2668 assign(__c.begin(), __c.end());
2669 else
2670 __move_assign(__c, true_type());
2671}
2672
2673template <class _Allocator>
2674void
2675vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002676 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677{
2678 deallocate();
2679 this->__begin_ = __c.__begin_;
2680 this->__size_ = __c.__size_;
2681 this->__cap() = __c.__cap();
2682 __move_assign_alloc(__c);
2683 __c.__begin_ = nullptr;
2684 __c.__cap() = __c.__size_ = 0;
2685}
Howard Hinnant74279a52010-09-04 23:28:19 +00002686
2687#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002688
2689template <class _Allocator>
2690void
2691vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2692{
2693 __size_ = 0;
2694 if (__n > 0)
2695 {
2696 size_type __c = capacity();
2697 if (__n <= __c)
2698 __size_ = __n;
2699 else
2700 {
2701 vector __v(__alloc());
2702 __v.reserve(__recommend(__n));
2703 __v.__size_ = __n;
2704 swap(__v);
2705 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002706 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002707 }
2708}
2709
2710template <class _Allocator>
2711template <class _InputIterator>
2712typename enable_if
2713<
2714 __is_input_iterator<_InputIterator>::value &&
2715 !__is_forward_iterator<_InputIterator>::value,
2716 void
2717>::type
2718vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2719{
2720 clear();
2721 for (; __first != __last; ++__first)
2722 push_back(*__first);
2723}
2724
2725template <class _Allocator>
2726template <class _ForwardIterator>
2727typename enable_if
2728<
2729 __is_forward_iterator<_ForwardIterator>::value,
2730 void
2731>::type
2732vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2733{
2734 clear();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002735 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002736 if (__n)
2737 {
2738 if (__n > capacity())
2739 {
2740 deallocate();
2741 allocate(__n);
2742 }
2743 __construct_at_end(__first, __last);
2744 }
2745}
2746
2747template <class _Allocator>
2748void
2749vector<bool, _Allocator>::reserve(size_type __n)
2750{
2751 if (__n > capacity())
2752 {
2753 vector __v(this->__alloc());
2754 __v.allocate(__n);
2755 __v.__construct_at_end(this->begin(), this->end());
2756 swap(__v);
2757 __invalidate_all_iterators();
2758 }
2759}
2760
2761template <class _Allocator>
2762void
Howard Hinnant1c936782011-06-03 19:40:40 +00002763vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764{
2765 if (__external_cap_to_internal(size()) > __cap())
2766 {
2767#ifndef _LIBCPP_NO_EXCEPTIONS
2768 try
2769 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002770#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771 vector(*this, allocator_type(__alloc())).swap(*this);
2772#ifndef _LIBCPP_NO_EXCEPTIONS
2773 }
2774 catch (...)
2775 {
2776 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002777#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778 }
2779}
2780
2781template <class _Allocator>
2782typename vector<bool, _Allocator>::reference
2783vector<bool, _Allocator>::at(size_type __n)
2784{
2785 if (__n >= size())
2786 this->__throw_out_of_range();
2787 return (*this)[__n];
2788}
2789
2790template <class _Allocator>
2791typename vector<bool, _Allocator>::const_reference
2792vector<bool, _Allocator>::at(size_type __n) const
2793{
2794 if (__n >= size())
2795 this->__throw_out_of_range();
2796 return (*this)[__n];
2797}
2798
2799template <class _Allocator>
2800void
2801vector<bool, _Allocator>::push_back(const value_type& __x)
2802{
2803 if (this->__size_ == this->capacity())
2804 reserve(__recommend(this->__size_ + 1));
2805 ++this->__size_;
2806 back() = __x;
2807}
2808
2809template <class _Allocator>
2810typename vector<bool, _Allocator>::iterator
2811vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2812{
2813 iterator __r;
2814 if (size() < capacity())
2815 {
2816 const_iterator __old_end = end();
2817 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002818 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002819 __r = __const_iterator_cast(__position);
2820 }
2821 else
2822 {
2823 vector __v(__alloc());
2824 __v.reserve(__recommend(__size_ + 1));
2825 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002826 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2827 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002828 swap(__v);
2829 }
2830 *__r = __x;
2831 return __r;
2832}
2833
2834template <class _Allocator>
2835typename vector<bool, _Allocator>::iterator
2836vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
2837{
2838 iterator __r;
2839 size_type __c = capacity();
2840 if (__n <= __c && size() <= __c - __n)
2841 {
2842 const_iterator __old_end = end();
2843 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002844 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845 __r = __const_iterator_cast(__position);
2846 }
2847 else
2848 {
2849 vector __v(__alloc());
2850 __v.reserve(__recommend(__size_ + __n));
2851 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002852 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2853 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854 swap(__v);
2855 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002856 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002857 return __r;
2858}
2859
2860template <class _Allocator>
2861template <class _InputIterator>
2862typename enable_if
2863<
2864 __is_input_iterator <_InputIterator>::value &&
2865 !__is_forward_iterator<_InputIterator>::value,
2866 typename vector<bool, _Allocator>::iterator
2867>::type
2868vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
2869{
2870 difference_type __off = __position - begin();
2871 iterator __p = __const_iterator_cast(__position);
2872 iterator __old_end = end();
2873 for (; size() != capacity() && __first != __last; ++__first)
2874 {
2875 ++this->__size_;
2876 back() = *__first;
2877 }
2878 vector __v(__alloc());
2879 if (__first != __last)
2880 {
2881#ifndef _LIBCPP_NO_EXCEPTIONS
2882 try
2883 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002884#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002885 __v.assign(__first, __last);
2886 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
2887 difference_type __old_p = __p - begin();
2888 reserve(__recommend(size() + __v.size()));
2889 __p = begin() + __old_p;
2890 __old_end = begin() + __old_size;
2891#ifndef _LIBCPP_NO_EXCEPTIONS
2892 }
2893 catch (...)
2894 {
2895 erase(__old_end, end());
2896 throw;
2897 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002898#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002899 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002900 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002901 insert(__p, __v.begin(), __v.end());
2902 return begin() + __off;
2903}
2904
2905template <class _Allocator>
2906template <class _ForwardIterator>
2907typename enable_if
2908<
2909 __is_forward_iterator<_ForwardIterator>::value,
2910 typename vector<bool, _Allocator>::iterator
2911>::type
2912vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
2913{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002914 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002915 iterator __r;
2916 size_type __c = capacity();
2917 if (__n <= __c && size() <= __c - __n)
2918 {
2919 const_iterator __old_end = end();
2920 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002921 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002922 __r = __const_iterator_cast(__position);
2923 }
2924 else
2925 {
2926 vector __v(__alloc());
2927 __v.reserve(__recommend(__size_ + __n));
2928 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002929 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2930 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002931 swap(__v);
2932 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002933 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002934 return __r;
2935}
2936
2937template <class _Allocator>
2938_LIBCPP_INLINE_VISIBILITY inline
2939typename vector<bool, _Allocator>::iterator
2940vector<bool, _Allocator>::erase(const_iterator __position)
2941{
2942 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002943 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002944 --__size_;
2945 return __r;
2946}
2947
2948template <class _Allocator>
2949typename vector<bool, _Allocator>::iterator
2950vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
2951{
2952 iterator __r = __const_iterator_cast(__first);
2953 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002954 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002955 __size_ -= __d;
2956 return __r;
2957}
2958
2959template <class _Allocator>
2960void
2961vector<bool, _Allocator>::swap(vector& __x)
Howard Hinnant1c936782011-06-03 19:40:40 +00002962 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2963 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002965 _VSTD::swap(this->__begin_, __x.__begin_);
2966 _VSTD::swap(this->__size_, __x.__size_);
2967 _VSTD::swap(this->__cap(), __x.__cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968 __swap_alloc(this->__alloc(), __x.__alloc());
2969#ifdef _LIBCPP_DEBUG
2970 iterator::swap(this, &__x);
2971 const_iterator::swap(this, &__x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002972#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002973}
2974
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002975template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002976void
2977vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
2978{
2979 size_type __cs = size();
2980 if (__cs < __sz)
2981 {
2982 iterator __r;
2983 size_type __c = capacity();
2984 size_type __n = __sz - __cs;
2985 if (__n <= __c && __cs <= __c - __n)
2986 {
2987 __r = end();
2988 __size_ += __n;
2989 }
2990 else
2991 {
2992 vector __v(__alloc());
2993 __v.reserve(__recommend(__size_ + __n));
2994 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002995 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002996 swap(__v);
2997 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002998 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002999 }
3000 else
3001 __size_ = __sz;
3002}
3003
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003004template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003005void
Howard Hinnant1c936782011-06-03 19:40:40 +00003006vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003007{
3008 // do middle whole words
3009 size_type __n = __size_;
3010 __storage_pointer __p = __begin_;
3011 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3012 *__p = ~*__p;
3013 // do last partial word
3014 if (__n > 0)
3015 {
3016 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3017 __storage_type __b = *__p & __m;
3018 *__p &= ~__m;
3019 *__p |= ~__b & __m;
3020 }
3021}
3022
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003023template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003024bool
3025vector<bool, _Allocator>::__invariants() const
3026{
3027 if (this->__begin_ == 0)
3028 {
3029 if (this->__size_ != 0 || this->__cap() != 0)
3030 return false;
3031 }
3032 else
3033 {
3034 if (this->__cap() == 0)
3035 return false;
3036 if (this->__size_ > this->capacity())
3037 return false;
3038 }
3039 return true;
3040}
3041
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003042template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003043size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003044vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003045{
3046 size_t __h = 0;
3047 // do middle whole words
3048 size_type __n = __size_;
3049 __storage_pointer __p = __begin_;
3050 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3051 __h ^= *__p;
3052 // do last partial word
3053 if (__n > 0)
3054 {
3055 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3056 __h ^= *__p & __m;
3057 }
3058 return __h;
3059}
3060
3061template <class _Allocator>
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003062struct _LIBCPP_VISIBLE hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003063 : public unary_function<vector<bool, _Allocator>, size_t>
3064{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003065 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003066 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003067 {return __vec.__hash_code();}
3068};
3069
3070template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003071_LIBCPP_INLINE_VISIBILITY inline
3072bool
3073operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3074{
3075 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003076 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003077}
3078
3079template <class _Tp, class _Allocator>
3080_LIBCPP_INLINE_VISIBILITY inline
3081bool
3082operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3083{
3084 return !(__x == __y);
3085}
3086
3087template <class _Tp, class _Allocator>
3088_LIBCPP_INLINE_VISIBILITY inline
3089bool
3090operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3091{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003092 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003093}
3094
3095template <class _Tp, class _Allocator>
3096_LIBCPP_INLINE_VISIBILITY inline
3097bool
3098operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3099{
3100 return __y < __x;
3101}
3102
3103template <class _Tp, class _Allocator>
3104_LIBCPP_INLINE_VISIBILITY inline
3105bool
3106operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3107{
3108 return !(__x < __y);
3109}
3110
3111template <class _Tp, class _Allocator>
3112_LIBCPP_INLINE_VISIBILITY inline
3113bool
3114operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3115{
3116 return !(__y < __x);
3117}
3118
3119template <class _Tp, class _Allocator>
3120_LIBCPP_INLINE_VISIBILITY inline
3121void
3122swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003123 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003124{
3125 __x.swap(__y);
3126}
3127
3128_LIBCPP_END_NAMESPACE_STD
3129
3130#endif // _LIBCPP_VECTOR