blob: d1a67ce281bbfcb7550e50e84ecf518a3b26c548 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------------ vector --------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_VECTOR
12#define _LIBCPP_VECTOR
13
14/*
15 vector synopsis
16
17namespace std
18{
19
Howard Hinnant324bb032010-08-22 00:02:43 +000020template <class T, class Allocator = allocator<T> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021class vector
Howard Hinnant324bb032010-08-22 00:02:43 +000022{
23public:
24 typedef T value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000025 typedef Allocator allocator_type;
26 typedef typename allocator_type::reference reference;
27 typedef typename allocator_type::const_reference const_reference;
28 typedef implementation-defined iterator;
29 typedef implementation-defined const_iterator;
30 typedef typename allocator_type::size_type size_type;
31 typedef typename allocator_type::difference_type difference_type;
32 typedef typename allocator_type::pointer pointer;
33 typedef typename allocator_type::const_pointer const_pointer;
34 typedef std::reverse_iterator<iterator> reverse_iterator;
35 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
36
Howard Hinnantd1d27a42011-06-03 19:40:40 +000037 vector()
38 noexcept(is_nothrow_default_constructible<allocator_type>::value);
39 explicit vector(const allocator_type&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000040 explicit vector(size_type n);
41 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
42 template <class InputIterator>
43 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
44 vector(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +000045 vector(vector&& x)
46 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000047 vector(initializer_list<value_type> il);
48 vector(initializer_list<value_type> il, const allocator_type& a);
49 ~vector();
50 vector& operator=(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +000051 vector& operator=(vector&& x)
52 noexcept(
53 allocator_type::propagate_on_container_move_assignment::value &&
54 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000055 vector& operator=(initializer_list<value_type> il);
56 template <class InputIterator>
57 void assign(InputIterator first, InputIterator last);
58 void assign(size_type n, const value_type& u);
59 void assign(initializer_list<value_type> il);
60
Howard Hinnantd1d27a42011-06-03 19:40:40 +000061 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000062
Howard Hinnantd1d27a42011-06-03 19:40:40 +000063 iterator begin() noexcept;
64 const_iterator begin() const noexcept;
65 iterator end() noexcept;
66 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000067
Howard Hinnantd1d27a42011-06-03 19:40:40 +000068 reverse_iterator rbegin() noexcept;
69 const_reverse_iterator rbegin() const noexcept;
70 reverse_iterator rend() noexcept;
71 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000072
Howard Hinnantd1d27a42011-06-03 19:40:40 +000073 const_iterator cbegin() const noexcept;
74 const_iterator cend() const noexcept;
75 const_reverse_iterator crbegin() const noexcept;
76 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000077
Howard Hinnantd1d27a42011-06-03 19:40:40 +000078 size_type size() const noexcept;
79 size_type max_size() const noexcept;
80 size_type capacity() const noexcept;
81 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000082 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +000083 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000084
85 reference operator[](size_type n);
86 const_reference operator[](size_type n) const;
87 reference at(size_type n);
88 const_reference at(size_type n) const;
89
90 reference front();
91 const_reference front() const;
92 reference back();
93 const_reference back() const;
94
Howard Hinnantd1d27a42011-06-03 19:40:40 +000095 value_type* data() noexcept;
96 const value_type* data() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000097
98 void push_back(const value_type& x);
99 void push_back(value_type&& x);
100 template <class... Args>
101 void emplace_back(Args&&... args);
102 void pop_back();
103
104 template <class... Args> iterator emplace(const_iterator position, Args&&... args);
105 iterator insert(const_iterator position, const value_type& x);
106 iterator insert(const_iterator position, value_type&& x);
107 iterator insert(const_iterator position, size_type n, const value_type& x);
108 template <class InputIterator>
109 iterator insert(const_iterator position, InputIterator first, InputIterator last);
110 iterator insert(const_iterator position, initializer_list<value_type> il);
111
112 iterator erase(const_iterator position);
113 iterator erase(const_iterator first, const_iterator last);
114
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000115 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000116
117 void resize(size_type sz);
118 void resize(size_type sz, const value_type& c);
119
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000120 void swap(vector&)
121 noexcept(!allocator_type::propagate_on_container_swap::value ||
122 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000123
124 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000125};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000126
Howard Hinnant324bb032010-08-22 00:02:43 +0000127template <class Allocator = allocator<T> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000128class vector<bool, Allocator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000129{
130public:
131 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000132 typedef Allocator allocator_type;
133 typedef implementation-defined iterator;
134 typedef implementation-defined const_iterator;
135 typedef typename allocator_type::size_type size_type;
136 typedef typename allocator_type::difference_type difference_type;
137 typedef iterator pointer;
138 typedef const_iterator const_pointer;
139 typedef std::reverse_iterator<iterator> reverse_iterator;
140 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
141
142 class reference
143 {
144 public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000145 reference(const reference&) noexcept;
146 operator bool() const noexcept;
147 reference& operator=(const bool x) noexcept;
148 reference& operator=(const reference& x) noexcept;
149 iterator operator&() const noexcept;
150 void flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000151 };
152
153 class const_reference
154 {
155 public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000156 const_reference(const reference&) noexcept;
157 operator bool() const noexcept;
158 const_iterator operator&() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000159 };
160
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000161 vector()
162 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant9cbee432011-09-02 20:42:31 +0000163 explicit vector(const allocator_type&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000164 explicit vector(size_type n, const value_type& value = value_type(), const allocator_type& = allocator_type());
165 template <class InputIterator>
166 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
167 vector(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000168 vector(vector&& x)
169 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000170 vector(initializer_list<value_type> il);
171 vector(initializer_list<value_type> il, const allocator_type& a);
172 ~vector();
173 vector& operator=(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000174 vector& operator=(vector&& x)
175 noexcept(
176 allocator_type::propagate_on_container_move_assignment::value &&
177 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000178 vector& operator=(initializer_list<value_type> il);
179 template <class InputIterator>
180 void assign(InputIterator first, InputIterator last);
181 void assign(size_type n, const value_type& u);
182 void assign(initializer_list<value_type> il);
183
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000184 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000185
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000186 iterator begin() noexcept;
187 const_iterator begin() const noexcept;
188 iterator end() noexcept;
189 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000190
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000191 reverse_iterator rbegin() noexcept;
192 const_reverse_iterator rbegin() const noexcept;
193 reverse_iterator rend() noexcept;
194 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000195
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000196 const_iterator cbegin() const noexcept;
197 const_iterator cend() const noexcept;
198 const_reverse_iterator crbegin() const noexcept;
199 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000200
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000201 size_type size() const noexcept;
202 size_type max_size() const noexcept;
203 size_type capacity() const noexcept;
204 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000205 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000206 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000207
208 reference operator[](size_type n);
209 const_reference operator[](size_type n) const;
210 reference at(size_type n);
211 const_reference at(size_type n) const;
212
213 reference front();
214 const_reference front() const;
215 reference back();
216 const_reference back() const;
217
218 void push_back(const value_type& x);
219 void pop_back();
220
221 iterator insert(const_iterator position, const value_type& x);
222 iterator insert(const_iterator position, size_type n, const value_type& x);
223 template <class InputIterator>
224 iterator insert(const_iterator position, InputIterator first, InputIterator last);
225 iterator insert(const_iterator position, initializer_list<value_type> il);
226
227 iterator erase(const_iterator position);
228 iterator erase(const_iterator first, const_iterator last);
229
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000230 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000231
232 void resize(size_type sz);
233 void resize(size_type sz, value_type x);
234
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000235 void swap(vector&)
236 noexcept(!allocator_type::propagate_on_container_swap::value ||
237 __is_nothrow_swappable<allocator_type>::value);
238 void flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000239
240 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000241};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000242
243template <class Allocator> struct hash<std::vector<bool, Allocator>>;
244
245template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
246template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
247template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
248template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
249template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
250template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
251
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000252template <class T, class Allocator>
253void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
254 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000255
256} // std
257
258*/
259
260#include <__config>
261#include <__bit_reference>
262#include <type_traits>
263#include <climits>
264#include <limits>
265#include <initializer_list>
266#include <memory>
267#include <stdexcept>
268#include <algorithm>
269#include <cstring>
270#include <__split_buffer>
271#include <__functional_base>
272#if defined(_LIBCPP_DEBUG) || defined(_LIBCPP_NO_EXCEPTIONS)
273 #include <cassert>
274#endif
275
276#pragma GCC system_header
277
278_LIBCPP_BEGIN_NAMESPACE_STD
279
280template <bool>
281class __vector_base_common
282{
283protected:
284 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
285 void __throw_length_error() const;
286 void __throw_out_of_range() const;
287};
288
289template <bool __b>
290void
291__vector_base_common<__b>::__throw_length_error() const
292{
293#ifndef _LIBCPP_NO_EXCEPTIONS
294 throw length_error("vector");
295#else
296 assert(!"vector length_error");
297#endif
298}
299
300template <bool __b>
301void
302__vector_base_common<__b>::__throw_out_of_range() const
303{
304#ifndef _LIBCPP_NO_EXCEPTIONS
305 throw out_of_range("vector");
306#else
307 assert(!"vector out_of_range");
308#endif
309}
310
311extern template class __vector_base_common<true>;
312
313template <class _Tp, class _Allocator>
314class __vector_base
315 : protected __vector_base_common<true>
316{
317protected:
Howard Hinnant324bb032010-08-22 00:02:43 +0000318 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000319 typedef _Allocator allocator_type;
320 typedef allocator_traits<allocator_type> __alloc_traits;
321 typedef value_type& reference;
322 typedef const value_type& const_reference;
323 typedef typename __alloc_traits::size_type size_type;
324 typedef typename __alloc_traits::difference_type difference_type;
325 typedef typename __alloc_traits::pointer pointer;
326 typedef typename __alloc_traits::const_pointer const_pointer;
327 typedef pointer iterator;
328 typedef const_pointer const_iterator;
329
330 pointer __begin_;
331 pointer __end_;
332 __compressed_pair<pointer, allocator_type> __end_cap_;
333
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000334 _LIBCPP_INLINE_VISIBILITY
335 allocator_type& __alloc() _NOEXCEPT
336 {return __end_cap_.second();}
337 _LIBCPP_INLINE_VISIBILITY
338 const allocator_type& __alloc() const _NOEXCEPT
339 {return __end_cap_.second();}
340 _LIBCPP_INLINE_VISIBILITY
341 pointer& __end_cap() _NOEXCEPT
342 {return __end_cap_.first();}
343 _LIBCPP_INLINE_VISIBILITY
344 const pointer& __end_cap() const _NOEXCEPT
345 {return __end_cap_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000346
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000347 _LIBCPP_INLINE_VISIBILITY
348 __vector_base()
349 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000350 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000351 ~__vector_base();
352
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000353 _LIBCPP_INLINE_VISIBILITY
354 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
355 _LIBCPP_INLINE_VISIBILITY
356 size_type capacity() const _NOEXCEPT
357 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000358
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000359 _LIBCPP_INLINE_VISIBILITY
360 void __destruct_at_end(const_pointer __new_last) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +0000361 {__destruct_at_end(__new_last, is_trivially_destructible<value_type>());}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000362 _LIBCPP_INLINE_VISIBILITY
363 void __destruct_at_end(const_pointer __new_last, false_type) _NOEXCEPT;
364 _LIBCPP_INLINE_VISIBILITY
365 void __destruct_at_end(const_pointer __new_last, true_type) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000366
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000367 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000368 void __copy_assign_alloc(const __vector_base& __c)
369 {__copy_assign_alloc(__c, integral_constant<bool,
370 __alloc_traits::propagate_on_container_copy_assignment::value>());}
371
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000373 void __move_assign_alloc(__vector_base& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000374 _NOEXCEPT_(
375 !__alloc_traits::propagate_on_container_move_assignment::value ||
376 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377 {__move_assign_alloc(__c, integral_constant<bool,
378 __alloc_traits::propagate_on_container_move_assignment::value>());}
379
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000382 _NOEXCEPT_(
383 !__alloc_traits::propagate_on_container_swap::value ||
384 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000385 {__swap_alloc(__x, __y, integral_constant<bool,
386 __alloc_traits::propagate_on_container_swap::value>());}
387private:
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000389 void __copy_assign_alloc(const __vector_base& __c, true_type)
390 {
391 if (__alloc() != __c.__alloc())
392 {
393 clear();
394 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
395 __begin_ = __end_ = __end_cap() = nullptr;
396 }
397 __alloc() = __c.__alloc();
398 }
399
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000401 void __copy_assign_alloc(const __vector_base& __c, false_type)
402 {}
403
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000405 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000406 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000407 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000408 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409 }
410
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000412 void __move_assign_alloc(__vector_base& __c, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000413 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000414 {}
415
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000417 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000418 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000419 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000420 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000421 swap(__x, __y);
422 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000424 static void __swap_alloc(allocator_type& __x, allocator_type& __y, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000425 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000426 {}
427};
428
429template <class _Tp, class _Allocator>
430_LIBCPP_INLINE_VISIBILITY inline
431void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000432__vector_base<_Tp, _Allocator>::__destruct_at_end(const_pointer __new_last, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000433{
434 while (__new_last < __end_)
435 __alloc_traits::destroy(__alloc(), const_cast<pointer>(--__end_));
436}
437
438template <class _Tp, class _Allocator>
439_LIBCPP_INLINE_VISIBILITY inline
440void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000441__vector_base<_Tp, _Allocator>::__destruct_at_end(const_pointer __new_last, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000442{
443 __end_ = const_cast<pointer>(__new_last);
444}
445
446template <class _Tp, class _Allocator>
447_LIBCPP_INLINE_VISIBILITY inline
448__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000449 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000450 : __begin_(0),
451 __end_(0),
452 __end_cap_(0)
453{
454}
455
456template <class _Tp, class _Allocator>
457_LIBCPP_INLINE_VISIBILITY inline
458__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
459 : __begin_(0),
460 __end_(0),
461 __end_cap_(0, __a)
462{
463}
464
465template <class _Tp, class _Allocator>
466__vector_base<_Tp, _Allocator>::~__vector_base()
467{
468 if (__begin_ != 0)
469 {
470 clear();
471 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
472 }
473}
474
475template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnant36cdf022010-09-10 16:42:26 +0000476class _LIBCPP_VISIBLE vector
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000477 : private __vector_base<_Tp, _Allocator>
478{
479private:
480 typedef __vector_base<_Tp, _Allocator> __base;
Howard Hinnant324bb032010-08-22 00:02:43 +0000481public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000482 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +0000483 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000484 typedef _Allocator allocator_type;
485 typedef typename __base::__alloc_traits __alloc_traits;
486 typedef typename __base::reference reference;
487 typedef typename __base::const_reference const_reference;
488 typedef typename __base::size_type size_type;
489 typedef typename __base::difference_type difference_type;
490 typedef typename __base::pointer pointer;
491 typedef typename __base::const_pointer const_pointer;
492#ifdef _LIBCPP_DEBUG
493 typedef __debug_iter<vector, pointer> iterator;
494 typedef __debug_iter<vector, const_pointer> const_iterator;
495
496 friend class __debug_iter<vector, pointer>;
497 friend class __debug_iter<vector, const_pointer>;
498
499 pair<iterator*, const_iterator*> __iterator_list_;
500
501 _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;}
502 _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;}
503#elif defined(_LIBCPP_RAW_ITERATORS)
504 typedef pointer iterator;
505 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000506#else // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000507 typedef __wrap_iter<pointer> iterator;
508 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000509#endif // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000510 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
511 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000512
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000513 _LIBCPP_INLINE_VISIBILITY
514 vector()
515 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant7a563db2011-09-14 18:33:51 +0000516 {
517#ifdef _LIBCPP_DEBUG2
518 __get_db()->__insert_c(this);
519#endif
520 }
521 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
522 : __base(__a)
523 {
524#ifdef _LIBCPP_DEBUG2
525 __get_db()->__insert_c(this);
526#endif
527 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000528 explicit vector(size_type __n);
529 vector(size_type __n, const_reference __x);
530 vector(size_type __n, const_reference __x, const allocator_type& __a);
531 template <class _InputIterator>
532 vector(_InputIterator __first, _InputIterator __last,
533 typename enable_if<__is_input_iterator <_InputIterator>::value &&
534 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
535 template <class _InputIterator>
536 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
537 typename enable_if<__is_input_iterator <_InputIterator>::value &&
538 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
539 template <class _ForwardIterator>
540 vector(_ForwardIterator __first, _ForwardIterator __last,
541 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
542 template <class _ForwardIterator>
543 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
544 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000545#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000547 vector(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000549 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000550#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant7a563db2011-09-14 18:33:51 +0000551#ifdef _LIBCPP_DEBUG2
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7a563db2011-09-14 18:33:51 +0000553 ~vector()
554 {
555 __get_db()->__erase_c(this);
556 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000557#endif
558
559 vector(const vector& __x);
560 vector(const vector& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000562 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000563#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000565 vector(vector&& __x)
566 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000568 vector(vector&& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000570 vector& operator=(vector&& __x)
571 _NOEXCEPT_(
572 __alloc_traits::propagate_on_container_move_assignment::value &&
573 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000574#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000575#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000577 vector& operator=(initializer_list<value_type> __il)
578 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000579#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000580
581 template <class _InputIterator>
582 typename enable_if
583 <
584 __is_input_iterator <_InputIterator>::value &&
585 !__is_forward_iterator<_InputIterator>::value,
586 void
587 >::type
588 assign(_InputIterator __first, _InputIterator __last);
589 template <class _ForwardIterator>
590 typename enable_if
591 <
592 __is_forward_iterator<_ForwardIterator>::value,
593 void
594 >::type
595 assign(_ForwardIterator __first, _ForwardIterator __last);
596
597 void assign(size_type __n, const_reference __u);
Howard Hinnante3e32912011-08-12 21:56:02 +0000598#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000600 void assign(initializer_list<value_type> __il)
601 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000602#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000603
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000604 _LIBCPP_INLINE_VISIBILITY
605 allocator_type get_allocator() const _NOEXCEPT
606 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000607
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000608 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
609 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
610 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
611 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000612
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000613 _LIBCPP_INLINE_VISIBILITY
614 reverse_iterator rbegin() _NOEXCEPT
615 {return reverse_iterator(end());}
616 _LIBCPP_INLINE_VISIBILITY
617 const_reverse_iterator rbegin() const _NOEXCEPT
618 {return const_reverse_iterator(end());}
619 _LIBCPP_INLINE_VISIBILITY
620 reverse_iterator rend() _NOEXCEPT
621 {return reverse_iterator(begin());}
622 _LIBCPP_INLINE_VISIBILITY
623 const_reverse_iterator rend() const _NOEXCEPT
624 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000625
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000626 _LIBCPP_INLINE_VISIBILITY
627 const_iterator cbegin() const _NOEXCEPT
628 {return begin();}
629 _LIBCPP_INLINE_VISIBILITY
630 const_iterator cend() const _NOEXCEPT
631 {return end();}
632 _LIBCPP_INLINE_VISIBILITY
633 const_reverse_iterator crbegin() const _NOEXCEPT
634 {return rbegin();}
635 _LIBCPP_INLINE_VISIBILITY
636 const_reverse_iterator crend() const _NOEXCEPT
637 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000638
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000639 _LIBCPP_INLINE_VISIBILITY
640 size_type size() const _NOEXCEPT
641 {return static_cast<size_type>(this->__end_ - this->__begin_);}
642 _LIBCPP_INLINE_VISIBILITY
643 size_type capacity() const _NOEXCEPT
644 {return __base::capacity();}
645 _LIBCPP_INLINE_VISIBILITY
646 bool empty() const _NOEXCEPT
647 {return this->__begin_ == this->__end_;}
648 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000649 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000650 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000651
652 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
653 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
654 reference at(size_type __n);
655 const_reference at(size_type __n) const;
656
Howard Hinnant7a563db2011-09-14 18:33:51 +0000657 _LIBCPP_INLINE_VISIBILITY reference front()
658 {
659 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
660 return *this->__begin_;
661 }
662 _LIBCPP_INLINE_VISIBILITY const_reference front() const
663 {
664 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
665 return *this->__begin_;
666 }
667 _LIBCPP_INLINE_VISIBILITY reference back()
668 {
669 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
670 return *(this->__end_ - 1);
671 }
672 _LIBCPP_INLINE_VISIBILITY const_reference back() const
673 {
674 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
675 return *(this->__end_ - 1);
676 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000677
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000678 _LIBCPP_INLINE_VISIBILITY
679 value_type* data() _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000680 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000681 _LIBCPP_INLINE_VISIBILITY
682 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000683 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000684
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000685 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000686#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000687 void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000688#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000689 template <class... _Args>
690 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000691#endif // _LIBCPP_HAS_NO_VARIADICS
692#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000693 void pop_back();
694
695 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000696#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000697 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000698#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000699 template <class... _Args>
700 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000701#endif // _LIBCPP_HAS_NO_VARIADICS
702#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000703 iterator insert(const_iterator __position, size_type __n, const_reference __x);
704 template <class _InputIterator>
705 typename enable_if
706 <
707 __is_input_iterator <_InputIterator>::value &&
708 !__is_forward_iterator<_InputIterator>::value,
709 iterator
710 >::type
711 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
712 template <class _ForwardIterator>
713 typename enable_if
714 <
715 __is_forward_iterator<_ForwardIterator>::value,
716 iterator
717 >::type
718 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +0000719#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000720 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000721 iterator insert(const_iterator __position, initializer_list<value_type> __il)
722 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000723#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000724
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000725 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000726 iterator erase(const_iterator __first, const_iterator __last);
727
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000728 _LIBCPP_INLINE_VISIBILITY
729 void clear() _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000730 {
731 __base::clear();
732 __invalidate_all_iterators();
733 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000734
735 void resize(size_type __sz);
736 void resize(size_type __sz, const_reference __x);
737
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000738 void swap(vector&)
739 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
740 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000741
742 bool __invariants() const;
743
Howard Hinnant7a563db2011-09-14 18:33:51 +0000744#ifdef _LIBCPP_DEBUG2
745
746 bool __dereferenceable(const const_iterator* __i) const;
747 bool __decrementable(const const_iterator* __i) const;
748 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
749 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
750
751#endif // _LIBCPP_DEBUG2
752
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000753private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000754 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000755 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000756 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000757 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31 +0000758 void __construct_at_end(size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000759 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000760 template <class _ForwardIterator>
761 typename enable_if
762 <
763 __is_forward_iterator<_ForwardIterator>::value,
764 void
765 >::type
766 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
767 void __move_construct_at_end(pointer __first, pointer __last);
768 void __append(size_type __n);
769 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000771 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000773 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000774 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
775 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
776 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000777 void __move_assign(vector& __c, true_type)
778 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000779 void __move_assign(vector& __c, false_type);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000780 _LIBCPP_INLINE_VISIBILITY
781 void __destruct_at_end(const_pointer __new_last) _NOEXCEPT
782 {
783#ifdef _LIBCPP_DEBUG2
784 __c_node* __c = __get_db()->__find_c_and_lock(this);
785 for (__i_node** __p = __c->end_; __p != __c->beg_; )
786 {
787 --__p;
788 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
789 if (__i->base() > __new_last)
790 {
791 (*__p)->__c_ = nullptr;
792 if (--__c->end_ != __p)
793 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
794 }
795 }
796 __get_db()->unlock();
797#endif
798 __base::__destruct_at_end(__new_last);
799 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000800};
801
802template <class _Tp, class _Allocator>
803void
804vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
805{
806 for (pointer __p = this->__end_; this->__begin_ < __p;)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000807 __v.push_front(_VSTD::move_if_noexcept(*--__p));
808 _VSTD::swap(this->__begin_, __v.__begin_);
809 _VSTD::swap(this->__end_, __v.__end_);
810 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000811 __v.__first_ = __v.__begin_;
812 __invalidate_all_iterators();
813}
814
815template <class _Tp, class _Allocator>
816typename vector<_Tp, _Allocator>::pointer
817vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
818{
819 pointer __r = __v.__begin_;
820 for (pointer __i = __p; this->__begin_ < __i;)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000821 __v.push_front(_VSTD::move_if_noexcept(*--__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000822 for (pointer __i = __p; __i < this->__end_; ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000823 __v.push_back(_VSTD::move_if_noexcept(*__i));
824 _VSTD::swap(this->__begin_, __v.__begin_);
825 _VSTD::swap(this->__end_, __v.__end_);
826 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000827 __v.__first_ = __v.__begin_;
828 __invalidate_all_iterators();
829 return __r;
830}
831
832// Allocate space for __n objects
833// throws length_error if __n > max_size()
834// throws (probably bad_alloc) if memory run out
835// Precondition: __begin_ == __end_ == __end_cap() == 0
836// Precondition: __n > 0
837// Postcondition: capacity() == __n
838// Postcondition: size() == 0
839template <class _Tp, class _Allocator>
840void
841vector<_Tp, _Allocator>::allocate(size_type __n)
842{
843 if (__n > max_size())
844 this->__throw_length_error();
845 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
846 this->__end_cap() = this->__begin_ + __n;
847}
848
849template <class _Tp, class _Allocator>
850void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000851vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000852{
853 if (this->__begin_ != 0)
854 {
855 clear();
856 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000857 this->__begin_ = this->__end_ = this->__end_cap() = 0;
858 }
859}
860
861template <class _Tp, class _Allocator>
862typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000863vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000864{
Sean Hunt110b8bf2011-07-29 23:31:58 +0000865 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()), numeric_limits<size_type>::max() / 2); // end() >= begin(), always
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000866}
867
868// Precondition: __new_size > capacity()
869template <class _Tp, class _Allocator>
870_LIBCPP_INLINE_VISIBILITY inline
871typename vector<_Tp, _Allocator>::size_type
872vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
873{
874 const size_type __ms = max_size();
875 if (__new_size > __ms)
876 this->__throw_length_error();
877 const size_type __cap = capacity();
878 if (__cap >= __ms / 2)
879 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58 +0000880 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000881}
882
883// Default constructs __n objects starting at __end_
884// throws if construction throws
885// Precondition: __n > 0
886// Precondition: size() + __n <= capacity()
887// Postcondition: size() == size() + __n
888template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000889void
890vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
891{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000892 allocator_type& __a = this->__alloc();
893 do
894 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000895 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000896 ++this->__end_;
897 --__n;
898 } while (__n > 0);
899}
900
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000901// Copy constructs __n objects starting at __end_ from __x
902// throws if construction throws
903// Precondition: __n > 0
904// Precondition: size() + __n <= capacity()
905// Postcondition: size() == old size() + __n
906// Postcondition: [i] == __x for all i in [size() - __n, __n)
907template <class _Tp, class _Allocator>
908_LIBCPP_INLINE_VISIBILITY inline
909void
910vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
911{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000912 allocator_type& __a = this->__alloc();
913 do
914 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000915 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000916 ++this->__end_;
917 --__n;
918 } while (__n > 0);
919}
920
921template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000922template <class _ForwardIterator>
923typename enable_if
924<
925 __is_forward_iterator<_ForwardIterator>::value,
926 void
927>::type
928vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
929{
930 allocator_type& __a = this->__alloc();
931 for (; __first != __last; ++__first)
932 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000933 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000934 ++this->__end_;
935 }
936}
937
938template <class _Tp, class _Allocator>
939void
940vector<_Tp, _Allocator>::__move_construct_at_end(pointer __first, pointer __last)
941{
942 allocator_type& __a = this->__alloc();
943 for (; __first != __last; ++__first)
944 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000945 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
946 _VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000947 ++this->__end_;
948 }
949}
950
951// Default constructs __n objects starting at __end_
952// throws if construction throws
953// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000954// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000955template <class _Tp, class _Allocator>
956void
957vector<_Tp, _Allocator>::__append(size_type __n)
958{
959 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
960 this->__construct_at_end(__n);
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);
966 __swap_out_circular_buffer(__v);
967 }
968}
969
970// Default constructs __n objects starting at __end_
971// throws if construction throws
972// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000973// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000974template <class _Tp, class _Allocator>
975void
976vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
977{
978 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
979 this->__construct_at_end(__n, __x);
980 else
981 {
982 allocator_type& __a = this->__alloc();
983 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
984 __v.__construct_at_end(__n, __x);
985 __swap_out_circular_buffer(__v);
986 }
987}
988
989template <class _Tp, class _Allocator>
990vector<_Tp, _Allocator>::vector(size_type __n)
991{
992 if (__n > 0)
993 {
994 allocate(__n);
995 __construct_at_end(__n);
996 }
Howard Hinnant7a563db2011-09-14 18:33:51 +0000997#ifdef _LIBCPP_DEBUG2
998 __get_db()->__insert_c(this);
999#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001000}
1001
1002template <class _Tp, class _Allocator>
1003vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1004{
1005 if (__n > 0)
1006 {
1007 allocate(__n);
1008 __construct_at_end(__n, __x);
1009 }
Howard Hinnant7a563db2011-09-14 18:33:51 +00001010#ifdef _LIBCPP_DEBUG2
1011 __get_db()->__insert_c(this);
1012#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013}
1014
1015template <class _Tp, class _Allocator>
1016vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1017 : __base(__a)
1018{
1019 if (__n > 0)
1020 {
1021 allocate(__n);
1022 __construct_at_end(__n, __x);
1023 }
Howard Hinnant7a563db2011-09-14 18:33:51 +00001024#ifdef _LIBCPP_DEBUG2
1025 __get_db()->__insert_c(this);
1026#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001027}
1028
1029template <class _Tp, class _Allocator>
1030template <class _InputIterator>
1031vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
1032 typename enable_if<__is_input_iterator <_InputIterator>::value &&
1033 !__is_forward_iterator<_InputIterator>::value>::type*)
1034{
1035 for (; __first != __last; ++__first)
1036 push_back(*__first);
Howard Hinnant7a563db2011-09-14 18:33:51 +00001037#ifdef _LIBCPP_DEBUG2
1038 __get_db()->__insert_c(this);
1039#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001040}
1041
1042template <class _Tp, class _Allocator>
1043template <class _InputIterator>
1044vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1045 typename enable_if<__is_input_iterator <_InputIterator>::value &&
1046 !__is_forward_iterator<_InputIterator>::value>::type*)
1047 : __base(__a)
1048{
1049 for (; __first != __last; ++__first)
1050 push_back(*__first);
Howard Hinnant7a563db2011-09-14 18:33:51 +00001051#ifdef _LIBCPP_DEBUG2
1052 __get_db()->__insert_c(this);
1053#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001054}
1055
1056template <class _Tp, class _Allocator>
1057template <class _ForwardIterator>
1058vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
1059 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
1060{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001061 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001062 if (__n > 0)
1063 {
1064 allocate(__n);
1065 __construct_at_end(__first, __last);
1066 }
Howard Hinnant7a563db2011-09-14 18:33:51 +00001067#ifdef _LIBCPP_DEBUG2
1068 __get_db()->__insert_c(this);
1069#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001070}
1071
1072template <class _Tp, class _Allocator>
1073template <class _ForwardIterator>
1074vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
1075 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
1076 : __base(__a)
1077{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001078 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001079 if (__n > 0)
1080 {
1081 allocate(__n);
1082 __construct_at_end(__first, __last);
1083 }
Howard Hinnant7a563db2011-09-14 18:33:51 +00001084#ifdef _LIBCPP_DEBUG2
1085 __get_db()->__insert_c(this);
1086#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001087}
1088
1089template <class _Tp, class _Allocator>
1090vector<_Tp, _Allocator>::vector(const vector& __x)
1091 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1092{
1093 size_type __n = __x.size();
1094 if (__n > 0)
1095 {
1096 allocate(__n);
1097 __construct_at_end(__x.__begin_, __x.__end_);
1098 }
Howard Hinnant7a563db2011-09-14 18:33:51 +00001099#ifdef _LIBCPP_DEBUG2
1100 __get_db()->__insert_c(this);
1101#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001102}
1103
1104template <class _Tp, class _Allocator>
1105vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1106 : __base(__a)
1107{
1108 size_type __n = __x.size();
1109 if (__n > 0)
1110 {
1111 allocate(__n);
1112 __construct_at_end(__x.__begin_, __x.__end_);
1113 }
Howard Hinnant7a563db2011-09-14 18:33:51 +00001114#ifdef _LIBCPP_DEBUG2
1115 __get_db()->__insert_c(this);
1116#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001117}
1118
Howard Hinnant73d21a42010-09-04 23:28:19 +00001119#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001120
1121template <class _Tp, class _Allocator>
1122_LIBCPP_INLINE_VISIBILITY inline
1123vector<_Tp, _Allocator>::vector(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001124 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001125 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001126{
1127 this->__begin_ = __x.__begin_;
1128 this->__end_ = __x.__end_;
1129 this->__end_cap() = __x.__end_cap();
1130 __x.__begin_ = __x.__end_ = __x.__end_cap() = 0;
Howard Hinnant7a563db2011-09-14 18:33:51 +00001131#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001132 __x.__invalidate_all_iterators();
Howard Hinnant7a563db2011-09-14 18:33:51 +00001133 __get_db()->__insert_c(this);
1134#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001135}
1136
1137template <class _Tp, class _Allocator>
1138_LIBCPP_INLINE_VISIBILITY inline
1139vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1140 : __base(__a)
1141{
1142 if (__a == __x.__alloc())
1143 {
1144 this->__begin_ = __x.__begin_;
1145 this->__end_ = __x.__end_;
1146 this->__end_cap() = __x.__end_cap();
1147 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
1148 __x.__invalidate_all_iterators();
1149 }
1150 else
1151 {
1152 typedef move_iterator<iterator> _I;
1153 assign(_I(__x.begin()), _I(__x.end()));
1154 }
Howard Hinnant7a563db2011-09-14 18:33:51 +00001155#ifdef _LIBCPP_DEBUG2
1156 __get_db()->__insert_c(this);
1157#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001158}
1159
Howard Hinnante3e32912011-08-12 21:56:02 +00001160#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1161
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001162template <class _Tp, class _Allocator>
1163_LIBCPP_INLINE_VISIBILITY inline
1164vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1165{
1166 if (__il.size() > 0)
1167 {
1168 allocate(__il.size());
1169 __construct_at_end(__il.begin(), __il.end());
1170 }
Howard Hinnant7a563db2011-09-14 18:33:51 +00001171#ifdef _LIBCPP_DEBUG2
1172 __get_db()->__insert_c(this);
1173#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001174}
1175
1176template <class _Tp, class _Allocator>
1177_LIBCPP_INLINE_VISIBILITY inline
1178vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1179 : __base(__a)
1180{
1181 if (__il.size() > 0)
1182 {
1183 allocate(__il.size());
1184 __construct_at_end(__il.begin(), __il.end());
1185 }
Howard Hinnant7a563db2011-09-14 18:33:51 +00001186#ifdef _LIBCPP_DEBUG2
1187 __get_db()->__insert_c(this);
1188#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001189}
1190
Howard Hinnante3e32912011-08-12 21:56:02 +00001191#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1192
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001193template <class _Tp, class _Allocator>
1194_LIBCPP_INLINE_VISIBILITY inline
1195vector<_Tp, _Allocator>&
1196vector<_Tp, _Allocator>::operator=(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001197 _NOEXCEPT_(
1198 __alloc_traits::propagate_on_container_move_assignment::value &&
1199 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001200{
1201 __move_assign(__x, integral_constant<bool,
1202 __alloc_traits::propagate_on_container_move_assignment::value>());
1203 return *this;
1204}
1205
1206template <class _Tp, class _Allocator>
1207void
1208vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1209{
1210 if (__base::__alloc() != __c.__alloc())
1211 {
1212 typedef move_iterator<iterator> _I;
1213 assign(_I(__c.begin()), _I(__c.end()));
1214 }
1215 else
1216 __move_assign(__c, true_type());
1217}
1218
1219template <class _Tp, class _Allocator>
1220void
1221vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001222 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001223{
1224 deallocate();
1225 this->__begin_ = __c.__begin_;
1226 this->__end_ = __c.__end_;
1227 this->__end_cap() = __c.__end_cap();
1228 __base::__move_assign_alloc(__c);
1229 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
1230}
1231
Howard Hinnant73d21a42010-09-04 23:28:19 +00001232#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233
1234template <class _Tp, class _Allocator>
1235_LIBCPP_INLINE_VISIBILITY inline
1236vector<_Tp, _Allocator>&
1237vector<_Tp, _Allocator>::operator=(const vector& __x)
1238{
1239 if (this != &__x)
1240 {
1241 __base::__copy_assign_alloc(__x);
1242 assign(__x.__begin_, __x.__end_);
1243 }
1244 return *this;
1245}
1246
1247template <class _Tp, class _Allocator>
1248template <class _InputIterator>
1249typename enable_if
1250<
1251 __is_input_iterator <_InputIterator>::value &&
1252 !__is_forward_iterator<_InputIterator>::value,
1253 void
1254>::type
1255vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1256{
1257 clear();
1258 for (; __first != __last; ++__first)
1259 push_back(*__first);
1260}
1261
1262template <class _Tp, class _Allocator>
1263template <class _ForwardIterator>
1264typename enable_if
1265<
1266 __is_forward_iterator<_ForwardIterator>::value,
1267 void
1268>::type
1269vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1270{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001271 typename iterator_traits<_ForwardIterator>::difference_type __new_size = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001272 if (static_cast<size_type>(__new_size) <= capacity())
1273 {
1274 _ForwardIterator __mid = __last;
1275 bool __growing = false;
1276 if (static_cast<size_type>(__new_size) > size())
1277 {
1278 __growing = true;
1279 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001280 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001281 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001282 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001283 if (__growing)
1284 __construct_at_end(__mid, __last);
1285 else
1286 this->__destruct_at_end(__m);
1287 }
1288 else
1289 {
1290 deallocate();
1291 allocate(__recommend(static_cast<size_type>(__new_size)));
1292 __construct_at_end(__first, __last);
1293 }
1294}
1295
1296template <class _Tp, class _Allocator>
1297void
1298vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1299{
1300 if (__n <= capacity())
1301 {
1302 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001303 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001304 if (__n > __s)
1305 __construct_at_end(__n - __s, __u);
1306 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001307 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001308 }
1309 else
1310 {
1311 deallocate();
1312 allocate(__recommend(static_cast<size_type>(__n)));
1313 __construct_at_end(__n, __u);
1314 }
1315}
1316
Howard Hinnant324bb032010-08-22 00:02:43 +00001317template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001318_LIBCPP_INLINE_VISIBILITY inline
1319typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001320vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001321{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001322#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001323 return iterator(this, __p);
1324#else
1325 return iterator(__p);
1326#endif
1327}
1328
Howard Hinnant324bb032010-08-22 00:02:43 +00001329template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001330_LIBCPP_INLINE_VISIBILITY inline
1331typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001332vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001333{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001334#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001335 return const_iterator(this, __p);
1336#else
1337 return const_iterator(__p);
1338#endif
1339}
1340
Howard Hinnant324bb032010-08-22 00:02:43 +00001341template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001342_LIBCPP_INLINE_VISIBILITY inline
1343typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001344vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001345{
1346 return __make_iter(this->__begin_);
1347}
1348
Howard Hinnant324bb032010-08-22 00:02:43 +00001349template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001350_LIBCPP_INLINE_VISIBILITY inline
1351typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001352vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001353{
1354 return __make_iter(this->__begin_);
1355}
1356
Howard Hinnant324bb032010-08-22 00:02:43 +00001357template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001358_LIBCPP_INLINE_VISIBILITY inline
1359typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001360vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001361{
1362 return __make_iter(this->__end_);
1363}
1364
Howard Hinnant324bb032010-08-22 00:02:43 +00001365template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001366_LIBCPP_INLINE_VISIBILITY inline
1367typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001368vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001369{
1370 return __make_iter(this->__end_);
1371}
1372
Howard Hinnant324bb032010-08-22 00:02:43 +00001373template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001374_LIBCPP_INLINE_VISIBILITY inline
1375typename vector<_Tp, _Allocator>::reference
1376vector<_Tp, _Allocator>::operator[](size_type __n)
1377{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001378 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001379 return this->__begin_[__n];
1380}
1381
Howard Hinnant324bb032010-08-22 00:02:43 +00001382template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001383_LIBCPP_INLINE_VISIBILITY inline
1384typename vector<_Tp, _Allocator>::const_reference
1385vector<_Tp, _Allocator>::operator[](size_type __n) const
1386{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001387 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001388 return this->__begin_[__n];
1389}
1390
Howard Hinnant324bb032010-08-22 00:02:43 +00001391template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001392typename vector<_Tp, _Allocator>::reference
1393vector<_Tp, _Allocator>::at(size_type __n)
1394{
1395 if (__n >= size())
1396 this->__throw_out_of_range();
1397 return this->__begin_[__n];
1398}
1399
Howard Hinnant324bb032010-08-22 00:02:43 +00001400template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001401typename vector<_Tp, _Allocator>::const_reference
1402vector<_Tp, _Allocator>::at(size_type __n) const
1403{
1404 if (__n >= size())
1405 this->__throw_out_of_range();
1406 return this->__begin_[__n];
1407}
1408
1409template <class _Tp, class _Allocator>
1410void
1411vector<_Tp, _Allocator>::reserve(size_type __n)
1412{
1413 if (__n > capacity())
1414 {
1415 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001416 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001417 __swap_out_circular_buffer(__v);
1418 }
1419}
1420
1421template <class _Tp, class _Allocator>
1422void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001423vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001424{
1425 if (capacity() > size())
1426 {
1427#ifndef _LIBCPP_NO_EXCEPTIONS
1428 try
1429 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001430#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001431 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001432 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001433 __swap_out_circular_buffer(__v);
1434#ifndef _LIBCPP_NO_EXCEPTIONS
1435 }
1436 catch (...)
1437 {
1438 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001439#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001440 }
1441}
1442
1443template <class _Tp, class _Allocator>
1444void
1445vector<_Tp, _Allocator>::push_back(const_reference __x)
1446{
1447 if (this->__end_ < this->__end_cap())
1448 {
1449 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001450 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001451 ++this->__end_;
1452 }
1453 else
1454 {
1455 allocator_type& __a = this->__alloc();
1456 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1457 __v.push_back(__x);
1458 __swap_out_circular_buffer(__v);
1459 }
1460}
1461
Howard Hinnant73d21a42010-09-04 23:28:19 +00001462#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463
1464template <class _Tp, class _Allocator>
1465void
1466vector<_Tp, _Allocator>::push_back(value_type&& __x)
1467{
1468 if (this->__end_ < this->__end_cap())
1469 {
1470 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001471 _VSTD::__to_raw_pointer(this->__end_),
1472 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001473 ++this->__end_;
1474 }
1475 else
1476 {
1477 allocator_type& __a = this->__alloc();
1478 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001479 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001480 __swap_out_circular_buffer(__v);
1481 }
1482}
1483
Howard Hinnant73d21a42010-09-04 23:28:19 +00001484#ifndef _LIBCPP_HAS_NO_VARIADICS
1485
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486template <class _Tp, class _Allocator>
1487template <class... _Args>
1488void
1489vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1490{
1491 if (this->__end_ < this->__end_cap())
1492 {
1493 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001494 _VSTD::__to_raw_pointer(this->__end_),
1495 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001496 ++this->__end_;
1497 }
1498 else
1499 {
1500 allocator_type& __a = this->__alloc();
1501 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001502 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001503 __swap_out_circular_buffer(__v);
1504 }
1505}
1506
Howard Hinnant73d21a42010-09-04 23:28:19 +00001507#endif // _LIBCPP_HAS_NO_VARIADICS
1508#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001509
1510template <class _Tp, class _Allocator>
1511_LIBCPP_INLINE_VISIBILITY inline
1512void
1513vector<_Tp, _Allocator>::pop_back()
1514{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001515 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001516 this->__destruct_at_end(this->__end_ - 1);
1517}
1518
1519template <class _Tp, class _Allocator>
1520_LIBCPP_INLINE_VISIBILITY inline
1521typename vector<_Tp, _Allocator>::iterator
1522vector<_Tp, _Allocator>::erase(const_iterator __position)
1523{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001524 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1525 "vector::erase(iterator) called with an iterator not"
1526 " referring to this vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001527 pointer __p = const_cast<pointer>(&*__position);
1528 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001529 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530 return __r;
1531}
1532
1533template <class _Tp, class _Allocator>
1534typename vector<_Tp, _Allocator>::iterator
1535vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1536{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001537 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1538 "vector::erase(iterator, iterator) called with an iterator not"
1539 " referring to this vector");
1540 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001541 pointer __p = this->__begin_ + (__first - begin());
1542 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001543 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001544 return __r;
1545}
1546
1547template <class _Tp, class _Allocator>
1548void
1549vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1550{
1551 pointer __old_last = this->__end_;
1552 difference_type __n = __old_last - __to;
1553 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1554 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001555 _VSTD::__to_raw_pointer(this->__end_),
1556 _VSTD::move(*__i));
1557 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001558}
1559
1560template <class _Tp, class _Allocator>
1561typename vector<_Tp, _Allocator>::iterator
1562vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1563{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001564 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1565 "vector::insert(iterator, x) called with an iterator not"
1566 " referring to this vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001567 pointer __p = this->__begin_ + (__position - begin());
1568 if (this->__end_ < this->__end_cap())
1569 {
1570 if (__p == this->__end_)
1571 {
1572 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001573 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001574 ++this->__end_;
1575 }
1576 else
1577 {
1578 __move_range(__p, this->__end_, __p + 1);
1579 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1580 if (__p <= __xr && __xr < this->__end_)
1581 ++__xr;
1582 *__p = *__xr;
1583 }
1584 }
1585 else
1586 {
1587 allocator_type& __a = this->__alloc();
1588 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1589 __v.push_back(__x);
1590 __p = __swap_out_circular_buffer(__v, __p);
1591 }
1592 return __make_iter(__p);
1593}
1594
Howard Hinnant73d21a42010-09-04 23:28:19 +00001595#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001596
1597template <class _Tp, class _Allocator>
1598typename vector<_Tp, _Allocator>::iterator
1599vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1600{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001601 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1602 "vector::insert(iterator, x) called with an iterator not"
1603 " referring to this vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604 pointer __p = this->__begin_ + (__position - begin());
1605 if (this->__end_ < this->__end_cap())
1606 {
1607 if (__p == this->__end_)
1608 {
1609 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001610 _VSTD::__to_raw_pointer(this->__end_),
1611 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001612 ++this->__end_;
1613 }
1614 else
1615 {
1616 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001617 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618 }
1619 }
1620 else
1621 {
1622 allocator_type& __a = this->__alloc();
1623 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001624 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001625 __p = __swap_out_circular_buffer(__v, __p);
1626 }
1627 return __make_iter(__p);
1628}
1629
Howard Hinnant73d21a42010-09-04 23:28:19 +00001630#ifndef _LIBCPP_HAS_NO_VARIADICS
1631
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001632template <class _Tp, class _Allocator>
1633template <class... _Args>
1634typename vector<_Tp, _Allocator>::iterator
1635vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1636{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001637 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1638 "vector::emplace(iterator, x) called with an iterator not"
1639 " referring to this vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640 pointer __p = this->__begin_ + (__position - begin());
1641 if (this->__end_ < this->__end_cap())
1642 {
1643 if (__p == this->__end_)
1644 {
1645 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001646 _VSTD::__to_raw_pointer(this->__end_),
1647 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001648 ++this->__end_;
1649 }
1650 else
1651 {
1652 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001653 *__p = value_type(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001654 }
1655 }
1656 else
1657 {
1658 allocator_type& __a = this->__alloc();
1659 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001660 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001661 __p = __swap_out_circular_buffer(__v, __p);
1662 }
1663 return __make_iter(__p);
1664}
1665
Howard Hinnant73d21a42010-09-04 23:28:19 +00001666#endif // _LIBCPP_HAS_NO_VARIADICS
1667#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001668
1669template <class _Tp, class _Allocator>
1670typename vector<_Tp, _Allocator>::iterator
1671vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1672{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001673 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1674 "vector::insert(iterator, n, x) called with an iterator not"
1675 " referring to this vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676 pointer __p = this->__begin_ + (__position - begin());
1677 if (__n > 0)
1678 {
1679 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1680 {
1681 size_type __old_n = __n;
1682 pointer __old_last = this->__end_;
1683 if (__n > static_cast<size_type>(this->__end_ - __p))
1684 {
1685 size_type __cx = __n - (this->__end_ - __p);
1686 __construct_at_end(__cx, __x);
1687 __n -= __cx;
1688 }
1689 if (__n > 0)
1690 {
1691 __move_range(__p, __old_last, __p + __old_n);
1692 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1693 if (__p <= __xr && __xr < this->__end_)
1694 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001695 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001696 }
1697 }
1698 else
1699 {
1700 allocator_type& __a = this->__alloc();
1701 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1702 __v.__construct_at_end(__n, __x);
1703 __p = __swap_out_circular_buffer(__v, __p);
1704 }
1705 }
1706 return __make_iter(__p);
1707}
1708
1709template <class _Tp, class _Allocator>
1710template <class _InputIterator>
1711typename enable_if
1712<
1713 __is_input_iterator <_InputIterator>::value &&
1714 !__is_forward_iterator<_InputIterator>::value,
1715 typename vector<_Tp, _Allocator>::iterator
1716>::type
1717vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1718{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001719 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1720 "vector::insert(iterator, range) called with an iterator not"
1721 " referring to this vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001722 difference_type __off = __position - begin();
1723 pointer __p = this->__begin_ + __off;
1724 allocator_type& __a = this->__alloc();
1725 pointer __old_last = this->__end_;
1726 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1727 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001728 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001729 *__first);
1730 ++this->__end_;
1731 }
1732 __split_buffer<value_type, allocator_type&> __v(__a);
1733 if (__first != __last)
1734 {
1735#ifndef _LIBCPP_NO_EXCEPTIONS
1736 try
1737 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001738#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001739 __v.__construct_at_end(__first, __last);
1740 difference_type __old_size = __old_last - this->__begin_;
1741 difference_type __old_p = __p - this->__begin_;
1742 reserve(__recommend(size() + __v.size()));
1743 __p = this->__begin_ + __old_p;
1744 __old_last = this->__begin_ + __old_size;
1745#ifndef _LIBCPP_NO_EXCEPTIONS
1746 }
1747 catch (...)
1748 {
1749 erase(__make_iter(__old_last), end());
1750 throw;
1751 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001752#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001753 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001754 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001755 insert(__make_iter(__p), move_iterator<iterator>(__v.begin()),
1756 move_iterator<iterator>(__v.end()));
1757 return begin() + __off;
1758}
1759
1760template <class _Tp, class _Allocator>
1761template <class _ForwardIterator>
1762typename enable_if
1763<
1764 __is_forward_iterator<_ForwardIterator>::value,
1765 typename vector<_Tp, _Allocator>::iterator
1766>::type
1767vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1768{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001769 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1770 "vector::insert(iterator, range) called with an iterator not"
1771 " referring to this vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001772 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00001773 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001774 if (__n > 0)
1775 {
1776 if (__n <= this->__end_cap() - this->__end_)
1777 {
1778 size_type __old_n = __n;
1779 pointer __old_last = this->__end_;
1780 _ForwardIterator __m = __last;
1781 difference_type __dx = this->__end_ - __p;
1782 if (__n > __dx)
1783 {
1784 __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001785 _VSTD::advance(__m, this->__end_ - __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001786 __construct_at_end(__m, __last);
1787 __n = __dx;
1788 }
1789 if (__n > 0)
1790 {
1791 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001792 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001793 }
1794 }
1795 else
1796 {
1797 allocator_type& __a = this->__alloc();
1798 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1799 __v.__construct_at_end(__first, __last);
1800 __p = __swap_out_circular_buffer(__v, __p);
1801 }
1802 }
1803 return __make_iter(__p);
1804}
1805
1806template <class _Tp, class _Allocator>
1807void
1808vector<_Tp, _Allocator>::resize(size_type __sz)
1809{
1810 size_type __cs = size();
1811 if (__cs < __sz)
1812 this->__append(__sz - __cs);
1813 else if (__cs > __sz)
1814 this->__destruct_at_end(this->__begin_ + __sz);
1815}
1816
1817template <class _Tp, class _Allocator>
1818void
1819vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
1820{
1821 size_type __cs = size();
1822 if (__cs < __sz)
1823 this->__append(__sz - __cs, __x);
1824 else if (__cs > __sz)
1825 this->__destruct_at_end(this->__begin_ + __sz);
1826}
1827
1828template <class _Tp, class _Allocator>
1829void
1830vector<_Tp, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001831 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1832 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001833{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001834 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
1835 this->__alloc() == __x.__alloc(),
1836 "vector::swap: Either propagate_on_container_swap must be true"
1837 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +00001838 _VSTD::swap(this->__begin_, __x.__begin_);
1839 _VSTD::swap(this->__end_, __x.__end_);
1840 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001841 __base::__swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnant7a563db2011-09-14 18:33:51 +00001842#ifdef _LIBCPP_DEBUG2
1843 __get_db()->swap(this, &__x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001844#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001845}
1846
Howard Hinnant324bb032010-08-22 00:02:43 +00001847template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001848bool
1849vector<_Tp, _Allocator>::__invariants() const
1850{
1851 if (this->__begin_ == 0)
1852 {
1853 if (this->__end_ != 0 || this->__end_cap() != 0)
1854 return false;
1855 }
1856 else
1857 {
1858 if (this->__begin_ > this->__end_)
1859 return false;
1860 if (this->__begin_ == this->__end_cap())
1861 return false;
1862 if (this->__end_ > this->__end_cap())
1863 return false;
1864 }
1865 return true;
1866}
1867
Howard Hinnant7a563db2011-09-14 18:33:51 +00001868#ifdef _LIBCPP_DEBUG2
1869
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001870template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:51 +00001871bool
1872vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
1873{
1874 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
1875}
1876
1877template <class _Tp, class _Allocator>
1878bool
1879vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
1880{
1881 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
1882}
1883
1884template <class _Tp, class _Allocator>
1885bool
1886vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
1887{
1888 const_pointer __p = __i->base() + __n;
1889 return this->__begin_ <= __p && __p <= this->__end_;
1890}
1891
1892template <class _Tp, class _Allocator>
1893bool
1894vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
1895{
1896 const_pointer __p = __i->base() + __n;
1897 return this->__begin_ <= __p && __p < this->__end_;
1898}
1899
1900#endif // LIBCPP_DEBUG2
1901
1902template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001904void
1905vector<_Tp, _Allocator>::__invalidate_all_iterators()
1906{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001907#ifdef _LIBCPP_DEBUG2
1908 __get_db()->__invalidate_all(this);
1909// iterator::__remove_all(this);
1910// const_iterator::__remove_all(this);
Howard Hinnant324bb032010-08-22 00:02:43 +00001911#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001912}
1913
1914// vector<bool>
1915
1916template <class _Allocator> class vector<bool, _Allocator>;
1917
1918template <class _Allocator> struct hash<vector<bool, _Allocator> >;
1919
1920template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:23 +00001921struct __has_storage_type<vector<bool, _Allocator> >
1922{
1923 static const bool value = true;
1924};
1925
1926template <class _Allocator>
Howard Hinnant36cdf022010-09-10 16:42:26 +00001927class _LIBCPP_VISIBLE vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001928 : private __vector_base_common<true>
1929{
1930public:
1931 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00001932 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001933 typedef _Allocator allocator_type;
1934 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001935 typedef typename __alloc_traits::size_type size_type;
1936 typedef typename __alloc_traits::difference_type difference_type;
1937 typedef __bit_iterator<vector, false> pointer;
1938 typedef __bit_iterator<vector, true> const_pointer;
1939#ifdef _LIBCPP_DEBUG
1940 typedef __debug_iter<vector, pointer> iterator;
1941 typedef __debug_iter<vector, const_pointer> const_iterator;
1942
1943 friend class __debug_iter<vector, pointer>;
1944 friend class __debug_iter<vector, const_pointer>;
1945
1946 pair<iterator*, const_iterator*> __iterator_list_;
1947
1948 _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;}
1949 _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;}
Howard Hinnant324bb032010-08-22 00:02:43 +00001950#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001951 typedef pointer iterator;
1952 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +00001953#endif // _LIBCPP_DEBUG
Howard Hinnant0949eed2011-06-30 21:18:19 +00001954 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1955 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001956
1957private:
1958 typedef size_type __storage_type;
1959 typedef typename __alloc_traits::template
1960#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1961 rebind_alloc<__storage_type>
1962#else
1963 rebind_alloc<__storage_type>::other
1964#endif
1965 __storage_allocator;
1966 typedef allocator_traits<__storage_allocator> __storage_traits;
1967 typedef typename __storage_traits::pointer __storage_pointer;
1968 typedef typename __storage_traits::const_pointer __const_storage_pointer;
1969
1970 __storage_pointer __begin_;
1971 size_type __size_;
1972 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00001973public:
Howard Hinnantf03c3b42011-07-02 20:33:23 +00001974 typedef __bit_reference<vector> reference;
1975 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00001976private:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001977 _LIBCPP_INLINE_VISIBILITY
1978 size_type& __cap() _NOEXCEPT
1979 {return __cap_alloc_.first();}
1980 _LIBCPP_INLINE_VISIBILITY
1981 const size_type& __cap() const _NOEXCEPT
1982 {return __cap_alloc_.first();}
1983 _LIBCPP_INLINE_VISIBILITY
1984 __storage_allocator& __alloc() _NOEXCEPT
1985 {return __cap_alloc_.second();}
1986 _LIBCPP_INLINE_VISIBILITY
1987 const __storage_allocator& __alloc() const _NOEXCEPT
1988 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001989
1990 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
1991
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001992 _LIBCPP_INLINE_VISIBILITY
1993 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001994 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001995 _LIBCPP_INLINE_VISIBILITY
1996 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001997 {return (__n - 1) / __bits_per_word + 1;}
1998
1999public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002000 _LIBCPP_INLINE_VISIBILITY
2001 vector()
2002 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002003 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002004 ~vector();
2005 explicit vector(size_type __n);
2006 vector(size_type __n, const value_type& __v);
2007 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2008 template <class _InputIterator>
2009 vector(_InputIterator __first, _InputIterator __last,
2010 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2011 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2012 template <class _InputIterator>
2013 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2014 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2015 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2016 template <class _ForwardIterator>
2017 vector(_ForwardIterator __first, _ForwardIterator __last,
2018 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2019 template <class _ForwardIterator>
2020 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2021 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2022
2023 vector(const vector& __v);
2024 vector(const vector& __v, const allocator_type& __a);
2025 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00002026#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002027 vector(initializer_list<value_type> __il);
2028 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00002029#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002030
Howard Hinnant73d21a42010-09-04 23:28:19 +00002031#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002032 _LIBCPP_INLINE_VISIBILITY
2033 vector(vector&& __v)
2034 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002036 _LIBCPP_INLINE_VISIBILITY
2037 vector& operator=(vector&& __v)
2038 _NOEXCEPT_(
2039 __alloc_traits::propagate_on_container_move_assignment::value &&
2040 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002041#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +00002042#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002043 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002044 vector& operator=(initializer_list<value_type> __il)
2045 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00002046#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002047
2048 template <class _InputIterator>
2049 typename enable_if
2050 <
2051 __is_input_iterator<_InputIterator>::value &&
2052 !__is_forward_iterator<_InputIterator>::value,
2053 void
2054 >::type
2055 assign(_InputIterator __first, _InputIterator __last);
2056 template <class _ForwardIterator>
2057 typename enable_if
2058 <
2059 __is_forward_iterator<_ForwardIterator>::value,
2060 void
2061 >::type
2062 assign(_ForwardIterator __first, _ForwardIterator __last);
2063
2064 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +00002065#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002066 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002067 void assign(initializer_list<value_type> __il)
2068 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002069#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002071 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002072 {return allocator_type(this->__alloc());}
2073
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002074 size_type max_size() const _NOEXCEPT;
2075 _LIBCPP_INLINE_VISIBILITY
2076 size_type capacity() const _NOEXCEPT
2077 {return __internal_cap_to_external(__cap());}
2078 _LIBCPP_INLINE_VISIBILITY
2079 size_type size() const _NOEXCEPT
2080 {return __size_;}
2081 _LIBCPP_INLINE_VISIBILITY
2082 bool empty() const _NOEXCEPT
2083 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002084 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002085 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002086
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002087 _LIBCPP_INLINE_VISIBILITY
2088 iterator begin() _NOEXCEPT
2089 {return __make_iter(0);}
2090 _LIBCPP_INLINE_VISIBILITY
2091 const_iterator begin() const _NOEXCEPT
2092 {return __make_iter(0);}
2093 _LIBCPP_INLINE_VISIBILITY
2094 iterator end() _NOEXCEPT
2095 {return __make_iter(__size_);}
2096 _LIBCPP_INLINE_VISIBILITY
2097 const_iterator end() const _NOEXCEPT
2098 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002099
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002100 _LIBCPP_INLINE_VISIBILITY
2101 reverse_iterator rbegin() _NOEXCEPT
2102 {return reverse_iterator(end());}
2103 _LIBCPP_INLINE_VISIBILITY
2104 const_reverse_iterator rbegin() const _NOEXCEPT
2105 {return const_reverse_iterator(end());}
2106 _LIBCPP_INLINE_VISIBILITY
2107 reverse_iterator rend() _NOEXCEPT
2108 {return reverse_iterator(begin());}
2109 _LIBCPP_INLINE_VISIBILITY
2110 const_reverse_iterator rend() const _NOEXCEPT
2111 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002112
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002113 _LIBCPP_INLINE_VISIBILITY
2114 const_iterator cbegin() const _NOEXCEPT
2115 {return __make_iter(0);}
2116 _LIBCPP_INLINE_VISIBILITY
2117 const_iterator cend() const _NOEXCEPT
2118 {return __make_iter(__size_);}
2119 _LIBCPP_INLINE_VISIBILITY
2120 const_reverse_iterator crbegin() const _NOEXCEPT
2121 {return rbegin();}
2122 _LIBCPP_INLINE_VISIBILITY
2123 const_reverse_iterator crend() const _NOEXCEPT
2124 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002125
2126 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2127 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2128 reference at(size_type __n);
2129 const_reference at(size_type __n) const;
2130
2131 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2132 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2133 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2134 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2135
2136 void push_back(const value_type& __x);
2137 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2138
2139 iterator insert(const_iterator __position, const value_type& __x);
2140 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2141 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2142 template <class _InputIterator>
2143 typename enable_if
2144 <
2145 __is_input_iterator <_InputIterator>::value &&
2146 !__is_forward_iterator<_InputIterator>::value,
2147 iterator
2148 >::type
2149 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2150 template <class _ForwardIterator>
2151 typename enable_if
2152 <
2153 __is_forward_iterator<_ForwardIterator>::value,
2154 iterator
2155 >::type
2156 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00002157#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002159 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2160 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002161#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002162
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002163 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002164 iterator erase(const_iterator __first, const_iterator __last);
2165
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002166 _LIBCPP_INLINE_VISIBILITY
2167 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002168
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002169 void swap(vector&)
2170 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2171 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002172
2173 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002174 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002175
2176 bool __invariants() const;
2177
2178private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002179 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002180 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002181 void deallocate() _NOEXCEPT;
2182 _LIBCPP_INLINE_VISIBILITY
2183 static size_type __align(size_type __new_size) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002184 {return __new_size + (__bits_per_word-1) & ~(__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002185 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2186 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002187 template <class _ForwardIterator>
2188 typename enable_if
2189 <
2190 __is_forward_iterator<_ForwardIterator>::value,
2191 void
2192 >::type
2193 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2194 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002195 _LIBCPP_INLINE_VISIBILITY
2196 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002197 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002198 _LIBCPP_INLINE_VISIBILITY
2199 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002200 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
2201#ifdef _LIBCPP_DEBUG
2202 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_type __pos)
2203 {return iterator(this, pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2204 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_type __pos) const
2205 {return const_iterator(this, const_pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2206 _LIBCPP_INLINE_VISIBILITY iterator __const_iterator_cast(const_iterator __p)
2207 {return iterator(this, pointer(const_cast<__storage_pointer>(__p.base().__seg_), __p.base().__ctz_));}
Howard Hinnant324bb032010-08-22 00:02:43 +00002208#else // _LIBCPP_DEBUG
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002209 _LIBCPP_INLINE_VISIBILITY
2210 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002211 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002212 _LIBCPP_INLINE_VISIBILITY
2213 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002214 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002215 _LIBCPP_INLINE_VISIBILITY
2216 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002217 {return iterator(const_cast<__storage_pointer>(__p.__seg_), __p.__ctz_);}
Howard Hinnant324bb032010-08-22 00:02:43 +00002218#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002219
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002220 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002221 void __copy_assign_alloc(const vector& __v)
2222 {__copy_assign_alloc(__v, integral_constant<bool,
2223 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002224 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002225 void __copy_assign_alloc(const vector& __c, true_type)
2226 {
2227 if (__alloc() != __c.__alloc())
2228 deallocate();
2229 __alloc() = __c.__alloc();
2230 }
2231
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002233 void __copy_assign_alloc(const vector& __c, false_type)
2234 {}
2235
2236 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002237 void __move_assign(vector& __c, true_type)
2238 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002240 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002241 _NOEXCEPT_(
2242 !__storage_traits::propagate_on_container_move_assignment::value ||
2243 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002244 {__move_assign_alloc(__c, integral_constant<bool,
2245 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002246 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00002247 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002248 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002249 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002250 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002251 }
2252
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00002254 void __move_assign_alloc(vector& __c, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002255 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002256 {}
2257
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002258 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002259 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002260 _NOEXCEPT_(
2261 !__storage_traits::propagate_on_container_swap::value ||
2262 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002263 {__swap_alloc(__x, __y, integral_constant<bool,
2264 __storage_traits::propagate_on_container_swap::value>());}
2265
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002267 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002268 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002269 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002270 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002271 swap(__x, __y);
2272 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002274 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002275 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002276 {}
2277
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002278 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002279
2280 friend class __bit_reference<vector>;
2281 friend class __bit_const_reference<vector>;
2282 friend class __bit_iterator<vector, false>;
2283 friend class __bit_iterator<vector, true>;
2284 friend class __bit_array<vector>;
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002285 friend struct _LIBCPP_VISIBLE hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002286};
2287
2288template <class _Allocator>
2289#ifndef _LIBCPP_DEBUG
2290_LIBCPP_INLINE_VISIBILITY inline
2291#endif
2292void
2293vector<bool, _Allocator>::__invalidate_all_iterators()
2294{
2295#ifdef _LIBCPP_DEBUG
2296 iterator::__remove_all(this);
2297 const_iterator::__remove_all(this);
Howard Hinnant324bb032010-08-22 00:02:43 +00002298#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002299}
2300
2301// Allocate space for __n objects
2302// throws length_error if __n > max_size()
2303// throws (probably bad_alloc) if memory run out
2304// Precondition: __begin_ == __end_ == __cap() == 0
2305// Precondition: __n > 0
2306// Postcondition: capacity() == __n
2307// Postcondition: size() == 0
2308template <class _Allocator>
2309void
2310vector<bool, _Allocator>::allocate(size_type __n)
2311{
2312 if (__n > max_size())
2313 this->__throw_length_error();
2314 __n = __external_cap_to_internal(__n);
2315 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2316 this->__size_ = 0;
2317 this->__cap() = __n;
2318}
2319
2320template <class _Allocator>
2321void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002322vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002323{
2324 if (this->__begin_ != 0)
2325 {
2326 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2327 __invalidate_all_iterators();
2328 this->__begin_ = 0;
2329 this->__size_ = this->__cap() = 0;
2330 }
2331}
2332
2333template <class _Allocator>
2334typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002335vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002336{
2337 size_type __amax = __storage_traits::max_size(__alloc());
2338 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2339 if (__nmax / __bits_per_word <= __amax)
2340 return __nmax;
2341 return __internal_cap_to_external(__amax);
2342}
2343
2344// Precondition: __new_size > capacity()
2345template <class _Allocator>
2346_LIBCPP_INLINE_VISIBILITY inline
2347typename vector<bool, _Allocator>::size_type
2348vector<bool, _Allocator>::__recommend(size_type __new_size) const
2349{
2350 const size_type __ms = max_size();
2351 if (__new_size > __ms)
2352 this->__throw_length_error();
2353 const size_type __cap = capacity();
2354 if (__cap >= __ms / 2)
2355 return __ms;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002356 return _VSTD::max(2*__cap, __align(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002357}
2358
2359// Default constructs __n objects starting at __end_
2360// Precondition: __n > 0
2361// Precondition: size() + __n <= capacity()
2362// Postcondition: size() == size() + __n
2363template <class _Allocator>
2364_LIBCPP_INLINE_VISIBILITY inline
2365void
2366vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2367{
2368 size_type __old_size = this->__size_;
2369 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002370 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002371}
2372
2373template <class _Allocator>
2374template <class _ForwardIterator>
2375typename enable_if
2376<
2377 __is_forward_iterator<_ForwardIterator>::value,
2378 void
2379>::type
2380vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2381{
2382 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002383 this->__size_ += _VSTD::distance(__first, __last);
2384 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002385}
2386
2387template <class _Allocator>
2388_LIBCPP_INLINE_VISIBILITY inline
2389vector<bool, _Allocator>::vector()
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002390 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002391 : __begin_(0),
2392 __size_(0),
2393 __cap_alloc_(0)
2394{
2395}
2396
2397template <class _Allocator>
2398_LIBCPP_INLINE_VISIBILITY inline
2399vector<bool, _Allocator>::vector(const allocator_type& __a)
2400 : __begin_(0),
2401 __size_(0),
2402 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2403{
2404}
2405
2406template <class _Allocator>
2407vector<bool, _Allocator>::vector(size_type __n)
2408 : __begin_(0),
2409 __size_(0),
2410 __cap_alloc_(0)
2411{
2412 if (__n > 0)
2413 {
2414 allocate(__n);
2415 __construct_at_end(__n, false);
2416 }
2417}
2418
2419template <class _Allocator>
2420vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
2421 : __begin_(0),
2422 __size_(0),
2423 __cap_alloc_(0)
2424{
2425 if (__n > 0)
2426 {
2427 allocate(__n);
2428 __construct_at_end(__n, __x);
2429 }
2430}
2431
2432template <class _Allocator>
2433vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
2434 : __begin_(0),
2435 __size_(0),
2436 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2437{
2438 if (__n > 0)
2439 {
2440 allocate(__n);
2441 __construct_at_end(__n, __x);
2442 }
2443}
2444
2445template <class _Allocator>
2446template <class _InputIterator>
2447vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2448 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2449 !__is_forward_iterator<_InputIterator>::value>::type*)
2450 : __begin_(0),
2451 __size_(0),
2452 __cap_alloc_(0)
2453{
2454#ifndef _LIBCPP_NO_EXCEPTIONS
2455 try
2456 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002457#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002458 for (; __first != __last; ++__first)
2459 push_back(*__first);
2460#ifndef _LIBCPP_NO_EXCEPTIONS
2461 }
2462 catch (...)
2463 {
2464 if (__begin_ != 0)
2465 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2466 __invalidate_all_iterators();
2467 throw;
2468 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002469#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002470}
2471
2472template <class _Allocator>
2473template <class _InputIterator>
2474vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2475 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2476 !__is_forward_iterator<_InputIterator>::value>::type*)
2477 : __begin_(0),
2478 __size_(0),
2479 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2480{
2481#ifndef _LIBCPP_NO_EXCEPTIONS
2482 try
2483 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002484#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002485 for (; __first != __last; ++__first)
2486 push_back(*__first);
2487#ifndef _LIBCPP_NO_EXCEPTIONS
2488 }
2489 catch (...)
2490 {
2491 if (__begin_ != 0)
2492 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2493 __invalidate_all_iterators();
2494 throw;
2495 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002496#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002497}
2498
2499template <class _Allocator>
2500template <class _ForwardIterator>
2501vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2502 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2503 : __begin_(0),
2504 __size_(0),
2505 __cap_alloc_(0)
2506{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002507 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002508 if (__n > 0)
2509 {
2510 allocate(__n);
2511 __construct_at_end(__first, __last);
2512 }
2513}
2514
2515template <class _Allocator>
2516template <class _ForwardIterator>
2517vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2518 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2519 : __begin_(0),
2520 __size_(0),
2521 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2522{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002523 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002524 if (__n > 0)
2525 {
2526 allocate(__n);
2527 __construct_at_end(__first, __last);
2528 }
2529}
2530
Howard Hinnante3e32912011-08-12 21:56:02 +00002531#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2532
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002533template <class _Allocator>
2534vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
2535 : __begin_(0),
2536 __size_(0),
2537 __cap_alloc_(0)
2538{
2539 size_type __n = static_cast<size_type>(__il.size());
2540 if (__n > 0)
2541 {
2542 allocate(__n);
2543 __construct_at_end(__il.begin(), __il.end());
2544 }
2545}
2546
2547template <class _Allocator>
2548vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
2549 : __begin_(0),
2550 __size_(0),
2551 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2552{
2553 size_type __n = static_cast<size_type>(__il.size());
2554 if (__n > 0)
2555 {
2556 allocate(__n);
2557 __construct_at_end(__il.begin(), __il.end());
2558 }
2559}
2560
Howard Hinnante3e32912011-08-12 21:56:02 +00002561#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2562
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002563template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002564vector<bool, _Allocator>::~vector()
2565{
2566 if (__begin_ != 0)
2567 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2568#ifdef _LIBCPP_DEBUG
2569 __invalidate_all_iterators();
2570#endif
2571}
2572
2573template <class _Allocator>
2574vector<bool, _Allocator>::vector(const vector& __v)
2575 : __begin_(0),
2576 __size_(0),
2577 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2578{
2579 if (__v.size() > 0)
2580 {
2581 allocate(__v.size());
2582 __construct_at_end(__v.begin(), __v.end());
2583 }
2584}
2585
2586template <class _Allocator>
2587vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
2588 : __begin_(0),
2589 __size_(0),
2590 __cap_alloc_(0, __a)
2591{
2592 if (__v.size() > 0)
2593 {
2594 allocate(__v.size());
2595 __construct_at_end(__v.begin(), __v.end());
2596 }
2597}
2598
2599template <class _Allocator>
2600vector<bool, _Allocator>&
2601vector<bool, _Allocator>::operator=(const vector& __v)
2602{
2603 if (this != &__v)
2604 {
2605 __copy_assign_alloc(__v);
2606 if (__v.__size_)
2607 {
2608 if (__v.__size_ > capacity())
2609 {
2610 deallocate();
2611 allocate(__v.__size_);
2612 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002613 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002614 }
2615 __size_ = __v.__size_;
2616 }
2617 return *this;
2618}
2619
Howard Hinnant73d21a42010-09-04 23:28:19 +00002620#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2621
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002622template <class _Allocator>
2623_LIBCPP_INLINE_VISIBILITY inline
2624vector<bool, _Allocator>::vector(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002625 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002626 : __begin_(__v.__begin_),
2627 __size_(__v.__size_),
2628 __cap_alloc_(__v.__cap_alloc_)
2629{
2630 __v.__begin_ = 0;
2631 __v.__size_ = 0;
2632 __v.__cap() = 0;
2633}
2634
2635template <class _Allocator>
2636vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
2637 : __begin_(0),
2638 __size_(0),
2639 __cap_alloc_(0, __a)
2640{
2641 if (__a == allocator_type(__v.__alloc()))
2642 {
2643 this->__begin_ = __v.__begin_;
2644 this->__size_ = __v.__size_;
2645 this->__cap() = __v.__cap();
2646 __v.__begin_ = nullptr;
2647 __v.__cap() = __v.__size_ = 0;
2648 }
2649 else if (__v.size() > 0)
2650 {
2651 allocate(__v.size());
2652 __construct_at_end(__v.begin(), __v.end());
2653 }
2654}
2655
2656template <class _Allocator>
2657_LIBCPP_INLINE_VISIBILITY inline
2658vector<bool, _Allocator>&
2659vector<bool, _Allocator>::operator=(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002660 _NOEXCEPT_(
2661 __alloc_traits::propagate_on_container_move_assignment::value &&
2662 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002663{
2664 __move_assign(__v, integral_constant<bool,
2665 __storage_traits::propagate_on_container_move_assignment::value>());
2666}
2667
2668template <class _Allocator>
2669void
2670vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2671{
2672 if (__alloc() != __c.__alloc())
2673 assign(__c.begin(), __c.end());
2674 else
2675 __move_assign(__c, true_type());
2676}
2677
2678template <class _Allocator>
2679void
2680vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002681 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002682{
2683 deallocate();
2684 this->__begin_ = __c.__begin_;
2685 this->__size_ = __c.__size_;
2686 this->__cap() = __c.__cap();
2687 __move_assign_alloc(__c);
2688 __c.__begin_ = nullptr;
2689 __c.__cap() = __c.__size_ = 0;
2690}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002691
2692#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002693
2694template <class _Allocator>
2695void
2696vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2697{
2698 __size_ = 0;
2699 if (__n > 0)
2700 {
2701 size_type __c = capacity();
2702 if (__n <= __c)
2703 __size_ = __n;
2704 else
2705 {
2706 vector __v(__alloc());
2707 __v.reserve(__recommend(__n));
2708 __v.__size_ = __n;
2709 swap(__v);
2710 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002711 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002712 }
2713}
2714
2715template <class _Allocator>
2716template <class _InputIterator>
2717typename enable_if
2718<
2719 __is_input_iterator<_InputIterator>::value &&
2720 !__is_forward_iterator<_InputIterator>::value,
2721 void
2722>::type
2723vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2724{
2725 clear();
2726 for (; __first != __last; ++__first)
2727 push_back(*__first);
2728}
2729
2730template <class _Allocator>
2731template <class _ForwardIterator>
2732typename enable_if
2733<
2734 __is_forward_iterator<_ForwardIterator>::value,
2735 void
2736>::type
2737vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2738{
2739 clear();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002740 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002741 if (__n)
2742 {
2743 if (__n > capacity())
2744 {
2745 deallocate();
2746 allocate(__n);
2747 }
2748 __construct_at_end(__first, __last);
2749 }
2750}
2751
2752template <class _Allocator>
2753void
2754vector<bool, _Allocator>::reserve(size_type __n)
2755{
2756 if (__n > capacity())
2757 {
2758 vector __v(this->__alloc());
2759 __v.allocate(__n);
2760 __v.__construct_at_end(this->begin(), this->end());
2761 swap(__v);
2762 __invalidate_all_iterators();
2763 }
2764}
2765
2766template <class _Allocator>
2767void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002768vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002769{
2770 if (__external_cap_to_internal(size()) > __cap())
2771 {
2772#ifndef _LIBCPP_NO_EXCEPTIONS
2773 try
2774 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002775#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002776 vector(*this, allocator_type(__alloc())).swap(*this);
2777#ifndef _LIBCPP_NO_EXCEPTIONS
2778 }
2779 catch (...)
2780 {
2781 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002782#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002783 }
2784}
2785
2786template <class _Allocator>
2787typename vector<bool, _Allocator>::reference
2788vector<bool, _Allocator>::at(size_type __n)
2789{
2790 if (__n >= size())
2791 this->__throw_out_of_range();
2792 return (*this)[__n];
2793}
2794
2795template <class _Allocator>
2796typename vector<bool, _Allocator>::const_reference
2797vector<bool, _Allocator>::at(size_type __n) const
2798{
2799 if (__n >= size())
2800 this->__throw_out_of_range();
2801 return (*this)[__n];
2802}
2803
2804template <class _Allocator>
2805void
2806vector<bool, _Allocator>::push_back(const value_type& __x)
2807{
2808 if (this->__size_ == this->capacity())
2809 reserve(__recommend(this->__size_ + 1));
2810 ++this->__size_;
2811 back() = __x;
2812}
2813
2814template <class _Allocator>
2815typename vector<bool, _Allocator>::iterator
2816vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2817{
2818 iterator __r;
2819 if (size() < capacity())
2820 {
2821 const_iterator __old_end = end();
2822 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002823 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002824 __r = __const_iterator_cast(__position);
2825 }
2826 else
2827 {
2828 vector __v(__alloc());
2829 __v.reserve(__recommend(__size_ + 1));
2830 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002831 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2832 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002833 swap(__v);
2834 }
2835 *__r = __x;
2836 return __r;
2837}
2838
2839template <class _Allocator>
2840typename vector<bool, _Allocator>::iterator
2841vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
2842{
2843 iterator __r;
2844 size_type __c = capacity();
2845 if (__n <= __c && size() <= __c - __n)
2846 {
2847 const_iterator __old_end = end();
2848 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002849 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002850 __r = __const_iterator_cast(__position);
2851 }
2852 else
2853 {
2854 vector __v(__alloc());
2855 __v.reserve(__recommend(__size_ + __n));
2856 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002857 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2858 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002859 swap(__v);
2860 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002861 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002862 return __r;
2863}
2864
2865template <class _Allocator>
2866template <class _InputIterator>
2867typename enable_if
2868<
2869 __is_input_iterator <_InputIterator>::value &&
2870 !__is_forward_iterator<_InputIterator>::value,
2871 typename vector<bool, _Allocator>::iterator
2872>::type
2873vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
2874{
2875 difference_type __off = __position - begin();
2876 iterator __p = __const_iterator_cast(__position);
2877 iterator __old_end = end();
2878 for (; size() != capacity() && __first != __last; ++__first)
2879 {
2880 ++this->__size_;
2881 back() = *__first;
2882 }
2883 vector __v(__alloc());
2884 if (__first != __last)
2885 {
2886#ifndef _LIBCPP_NO_EXCEPTIONS
2887 try
2888 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002889#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002890 __v.assign(__first, __last);
2891 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
2892 difference_type __old_p = __p - begin();
2893 reserve(__recommend(size() + __v.size()));
2894 __p = begin() + __old_p;
2895 __old_end = begin() + __old_size;
2896#ifndef _LIBCPP_NO_EXCEPTIONS
2897 }
2898 catch (...)
2899 {
2900 erase(__old_end, end());
2901 throw;
2902 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002903#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002904 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002905 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002906 insert(__p, __v.begin(), __v.end());
2907 return begin() + __off;
2908}
2909
2910template <class _Allocator>
2911template <class _ForwardIterator>
2912typename enable_if
2913<
2914 __is_forward_iterator<_ForwardIterator>::value,
2915 typename vector<bool, _Allocator>::iterator
2916>::type
2917vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
2918{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002919 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002920 iterator __r;
2921 size_type __c = capacity();
2922 if (__n <= __c && size() <= __c - __n)
2923 {
2924 const_iterator __old_end = end();
2925 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002926 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002927 __r = __const_iterator_cast(__position);
2928 }
2929 else
2930 {
2931 vector __v(__alloc());
2932 __v.reserve(__recommend(__size_ + __n));
2933 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002934 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2935 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002936 swap(__v);
2937 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002938 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002939 return __r;
2940}
2941
2942template <class _Allocator>
2943_LIBCPP_INLINE_VISIBILITY inline
2944typename vector<bool, _Allocator>::iterator
2945vector<bool, _Allocator>::erase(const_iterator __position)
2946{
2947 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002948 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002949 --__size_;
2950 return __r;
2951}
2952
2953template <class _Allocator>
2954typename vector<bool, _Allocator>::iterator
2955vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
2956{
2957 iterator __r = __const_iterator_cast(__first);
2958 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002959 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002960 __size_ -= __d;
2961 return __r;
2962}
2963
2964template <class _Allocator>
2965void
2966vector<bool, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002967 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2968 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002969{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002970 _VSTD::swap(this->__begin_, __x.__begin_);
2971 _VSTD::swap(this->__size_, __x.__size_);
2972 _VSTD::swap(this->__cap(), __x.__cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002973 __swap_alloc(this->__alloc(), __x.__alloc());
2974#ifdef _LIBCPP_DEBUG
2975 iterator::swap(this, &__x);
2976 const_iterator::swap(this, &__x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002977#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002978}
2979
Howard Hinnant324bb032010-08-22 00:02:43 +00002980template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002981void
2982vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
2983{
2984 size_type __cs = size();
2985 if (__cs < __sz)
2986 {
2987 iterator __r;
2988 size_type __c = capacity();
2989 size_type __n = __sz - __cs;
2990 if (__n <= __c && __cs <= __c - __n)
2991 {
2992 __r = end();
2993 __size_ += __n;
2994 }
2995 else
2996 {
2997 vector __v(__alloc());
2998 __v.reserve(__recommend(__size_ + __n));
2999 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003000 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003001 swap(__v);
3002 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003003 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003004 }
3005 else
3006 __size_ = __sz;
3007}
3008
Howard Hinnant324bb032010-08-22 00:02:43 +00003009template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003010void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003011vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003012{
3013 // do middle whole words
3014 size_type __n = __size_;
3015 __storage_pointer __p = __begin_;
3016 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3017 *__p = ~*__p;
3018 // do last partial word
3019 if (__n > 0)
3020 {
3021 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3022 __storage_type __b = *__p & __m;
3023 *__p &= ~__m;
3024 *__p |= ~__b & __m;
3025 }
3026}
3027
Howard Hinnant324bb032010-08-22 00:02:43 +00003028template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003029bool
3030vector<bool, _Allocator>::__invariants() const
3031{
3032 if (this->__begin_ == 0)
3033 {
3034 if (this->__size_ != 0 || this->__cap() != 0)
3035 return false;
3036 }
3037 else
3038 {
3039 if (this->__cap() == 0)
3040 return false;
3041 if (this->__size_ > this->capacity())
3042 return false;
3043 }
3044 return true;
3045}
3046
Howard Hinnant324bb032010-08-22 00:02:43 +00003047template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003048size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003049vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003050{
3051 size_t __h = 0;
3052 // do middle whole words
3053 size_type __n = __size_;
3054 __storage_pointer __p = __begin_;
3055 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3056 __h ^= *__p;
3057 // do last partial word
3058 if (__n > 0)
3059 {
3060 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3061 __h ^= *__p & __m;
3062 }
3063 return __h;
3064}
3065
3066template <class _Allocator>
Howard Hinnantee6ccd02010-09-23 18:58:28 +00003067struct _LIBCPP_VISIBLE hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003068 : public unary_function<vector<bool, _Allocator>, size_t>
3069{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00003070 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003071 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003072 {return __vec.__hash_code();}
3073};
3074
3075template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003076_LIBCPP_INLINE_VISIBILITY inline
3077bool
3078operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3079{
3080 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003081 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003082}
3083
3084template <class _Tp, class _Allocator>
3085_LIBCPP_INLINE_VISIBILITY inline
3086bool
3087operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3088{
3089 return !(__x == __y);
3090}
3091
3092template <class _Tp, class _Allocator>
3093_LIBCPP_INLINE_VISIBILITY inline
3094bool
3095operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3096{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003097 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003098}
3099
3100template <class _Tp, class _Allocator>
3101_LIBCPP_INLINE_VISIBILITY inline
3102bool
3103operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3104{
3105 return __y < __x;
3106}
3107
3108template <class _Tp, class _Allocator>
3109_LIBCPP_INLINE_VISIBILITY inline
3110bool
3111operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3112{
3113 return !(__x < __y);
3114}
3115
3116template <class _Tp, class _Allocator>
3117_LIBCPP_INLINE_VISIBILITY inline
3118bool
3119operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3120{
3121 return !(__y < __x);
3122}
3123
3124template <class _Tp, class _Allocator>
3125_LIBCPP_INLINE_VISIBILITY inline
3126void
3127swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003128 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003129{
3130 __x.swap(__y);
3131}
3132
3133_LIBCPP_END_NAMESPACE_STD
3134
3135#endif // _LIBCPP_VECTOR