blob: 11d9a1b6d3338f7c8f551368cc84505ca86e8e05 [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>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000272
Howard Hinnant66c6f972011-11-29 16:45:27 +0000273#include <__undef_min_max>
274
Howard Hinnant08e17472011-10-17 20:05:10 +0000275#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000276#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000277#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000278
279_LIBCPP_BEGIN_NAMESPACE_STD
280
281template <bool>
282class __vector_base_common
283{
284protected:
285 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
286 void __throw_length_error() const;
287 void __throw_out_of_range() const;
288};
289
290template <bool __b>
291void
292__vector_base_common<__b>::__throw_length_error() const
293{
294#ifndef _LIBCPP_NO_EXCEPTIONS
295 throw length_error("vector");
296#else
297 assert(!"vector length_error");
298#endif
299}
300
301template <bool __b>
302void
303__vector_base_common<__b>::__throw_out_of_range() const
304{
305#ifndef _LIBCPP_NO_EXCEPTIONS
306 throw out_of_range("vector");
307#else
308 assert(!"vector out_of_range");
309#endif
310}
311
Howard Hinnant78b68282011-10-22 20:59:45 +0000312#ifdef _MSC_VER
313#pragma warning( push )
314#pragma warning( disable: 4231 )
315#endif // _MSC_VER
Howard Hinnantff926772012-11-06 21:08:48 +0000316_LIBCPP_EXTERN_TEMPLATE(class __vector_base_common<true>)
Howard Hinnant78b68282011-10-22 20:59:45 +0000317#ifdef _MSC_VER
318#pragma warning( pop )
319#endif // _MSC_VER
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000320
321template <class _Tp, class _Allocator>
322class __vector_base
323 : protected __vector_base_common<true>
324{
325protected:
Howard Hinnant324bb032010-08-22 00:02:43 +0000326 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000327 typedef _Allocator allocator_type;
328 typedef allocator_traits<allocator_type> __alloc_traits;
329 typedef value_type& reference;
330 typedef const value_type& const_reference;
331 typedef typename __alloc_traits::size_type size_type;
332 typedef typename __alloc_traits::difference_type difference_type;
333 typedef typename __alloc_traits::pointer pointer;
334 typedef typename __alloc_traits::const_pointer const_pointer;
335 typedef pointer iterator;
336 typedef const_pointer const_iterator;
337
338 pointer __begin_;
339 pointer __end_;
340 __compressed_pair<pointer, allocator_type> __end_cap_;
341
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000342 _LIBCPP_INLINE_VISIBILITY
343 allocator_type& __alloc() _NOEXCEPT
344 {return __end_cap_.second();}
345 _LIBCPP_INLINE_VISIBILITY
346 const allocator_type& __alloc() const _NOEXCEPT
347 {return __end_cap_.second();}
348 _LIBCPP_INLINE_VISIBILITY
349 pointer& __end_cap() _NOEXCEPT
350 {return __end_cap_.first();}
351 _LIBCPP_INLINE_VISIBILITY
352 const pointer& __end_cap() const _NOEXCEPT
353 {return __end_cap_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000354
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000355 _LIBCPP_INLINE_VISIBILITY
356 __vector_base()
357 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000358 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000359 ~__vector_base();
360
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000361 _LIBCPP_INLINE_VISIBILITY
362 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
363 _LIBCPP_INLINE_VISIBILITY
364 size_type capacity() const _NOEXCEPT
365 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000366
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000367 _LIBCPP_INLINE_VISIBILITY
368 void __destruct_at_end(const_pointer __new_last) _NOEXCEPT
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000369 {__destruct_at_end(__new_last, false_type());}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000370 _LIBCPP_INLINE_VISIBILITY
371 void __destruct_at_end(const_pointer __new_last, false_type) _NOEXCEPT;
372 _LIBCPP_INLINE_VISIBILITY
373 void __destruct_at_end(const_pointer __new_last, true_type) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000376 void __copy_assign_alloc(const __vector_base& __c)
377 {__copy_assign_alloc(__c, integral_constant<bool,
378 __alloc_traits::propagate_on_container_copy_assignment::value>());}
379
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381 void __move_assign_alloc(__vector_base& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000382 _NOEXCEPT_(
383 !__alloc_traits::propagate_on_container_move_assignment::value ||
384 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000385 {__move_assign_alloc(__c, integral_constant<bool,
386 __alloc_traits::propagate_on_container_move_assignment::value>());}
387
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000389 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000390 _NOEXCEPT_(
391 !__alloc_traits::propagate_on_container_swap::value ||
392 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000393 {__swap_alloc(__x, __y, integral_constant<bool,
394 __alloc_traits::propagate_on_container_swap::value>());}
395private:
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000397 void __copy_assign_alloc(const __vector_base& __c, true_type)
398 {
399 if (__alloc() != __c.__alloc())
400 {
401 clear();
402 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
403 __begin_ = __end_ = __end_cap() = nullptr;
404 }
405 __alloc() = __c.__alloc();
406 }
407
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000409 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000410 {}
411
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000412 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000413 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000414 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000415 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000416 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000417 }
418
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000420 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000421 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000422 {}
423
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000425 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000426 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000427 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000428 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000429 swap(__x, __y);
430 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000431 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000432 static void __swap_alloc(allocator_type&, allocator_type&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000433 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000434 {}
435};
436
437template <class _Tp, class _Allocator>
438_LIBCPP_INLINE_VISIBILITY inline
439void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000440__vector_base<_Tp, _Allocator>::__destruct_at_end(const_pointer __new_last, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000441{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000442 while (__new_last != __end_)
Howard Hinnant635bbbb2013-02-07 15:31:44 +0000443 __alloc_traits::destroy(__alloc(), const_cast<pointer>(--__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000444}
445
446template <class _Tp, class _Allocator>
447_LIBCPP_INLINE_VISIBILITY inline
448void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000449__vector_base<_Tp, _Allocator>::__destruct_at_end(const_pointer __new_last, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000450{
Howard Hinnant635bbbb2013-02-07 15:31:44 +0000451 __end_ = const_cast<pointer>(__new_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000452}
453
454template <class _Tp, class _Allocator>
455_LIBCPP_INLINE_VISIBILITY inline
456__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000457 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000458 : __begin_(0),
459 __end_(0),
460 __end_cap_(0)
461{
462}
463
464template <class _Tp, class _Allocator>
465_LIBCPP_INLINE_VISIBILITY inline
466__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
467 : __begin_(0),
468 __end_(0),
469 __end_cap_(0, __a)
470{
471}
472
473template <class _Tp, class _Allocator>
474__vector_base<_Tp, _Allocator>::~__vector_base()
475{
476 if (__begin_ != 0)
477 {
478 clear();
479 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
480 }
481}
482
483template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnant83eade62013-03-06 23:30:19 +0000484class _LIBCPP_TYPE_VIS vector
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000485 : private __vector_base<_Tp, _Allocator>
486{
487private:
488 typedef __vector_base<_Tp, _Allocator> __base;
Howard Hinnant324bb032010-08-22 00:02:43 +0000489public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000490 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +0000491 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000492 typedef _Allocator allocator_type;
493 typedef typename __base::__alloc_traits __alloc_traits;
494 typedef typename __base::reference reference;
495 typedef typename __base::const_reference const_reference;
496 typedef typename __base::size_type size_type;
497 typedef typename __base::difference_type difference_type;
498 typedef typename __base::pointer pointer;
499 typedef typename __base::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000500 typedef __wrap_iter<pointer> iterator;
501 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000502 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
503 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000504
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000505 _LIBCPP_INLINE_VISIBILITY
506 vector()
507 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant7a563db2011-09-14 18:33:51 +0000508 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000509#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000510 __get_db()->__insert_c(this);
511#endif
512 }
513 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
514 : __base(__a)
515 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000516#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000517 __get_db()->__insert_c(this);
518#endif
519 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520 explicit vector(size_type __n);
521 vector(size_type __n, const_reference __x);
522 vector(size_type __n, const_reference __x, const allocator_type& __a);
523 template <class _InputIterator>
524 vector(_InputIterator __first, _InputIterator __last,
525 typename enable_if<__is_input_iterator <_InputIterator>::value &&
526 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
527 template <class _InputIterator>
528 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
529 typename enable_if<__is_input_iterator <_InputIterator>::value &&
530 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
531 template <class _ForwardIterator>
532 vector(_ForwardIterator __first, _ForwardIterator __last,
533 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
534 template <class _ForwardIterator>
535 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
536 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000537#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000539 vector(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000540 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000541 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000542#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantabe26282011-09-16 17:29:17 +0000543#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7a563db2011-09-14 18:33:51 +0000545 ~vector()
546 {
547 __get_db()->__erase_c(this);
548 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000549#endif
550
551 vector(const vector& __x);
552 vector(const vector& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000554 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000555#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000557 vector(vector&& __x)
558 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000560 vector(vector&& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000562 vector& operator=(vector&& __x)
563 _NOEXCEPT_(
564 __alloc_traits::propagate_on_container_move_assignment::value &&
565 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000566#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000567#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000569 vector& operator=(initializer_list<value_type> __il)
570 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000571#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572
573 template <class _InputIterator>
574 typename enable_if
575 <
576 __is_input_iterator <_InputIterator>::value &&
577 !__is_forward_iterator<_InputIterator>::value,
578 void
579 >::type
580 assign(_InputIterator __first, _InputIterator __last);
581 template <class _ForwardIterator>
582 typename enable_if
583 <
584 __is_forward_iterator<_ForwardIterator>::value,
585 void
586 >::type
587 assign(_ForwardIterator __first, _ForwardIterator __last);
588
589 void assign(size_type __n, const_reference __u);
Howard Hinnante3e32912011-08-12 21:56:02 +0000590#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000591 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000592 void assign(initializer_list<value_type> __il)
593 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000594#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000595
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000596 _LIBCPP_INLINE_VISIBILITY
597 allocator_type get_allocator() const _NOEXCEPT
598 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000599
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000600 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
601 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
602 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
603 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000604
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000605 _LIBCPP_INLINE_VISIBILITY
606 reverse_iterator rbegin() _NOEXCEPT
607 {return reverse_iterator(end());}
608 _LIBCPP_INLINE_VISIBILITY
609 const_reverse_iterator rbegin() const _NOEXCEPT
610 {return const_reverse_iterator(end());}
611 _LIBCPP_INLINE_VISIBILITY
612 reverse_iterator rend() _NOEXCEPT
613 {return reverse_iterator(begin());}
614 _LIBCPP_INLINE_VISIBILITY
615 const_reverse_iterator rend() const _NOEXCEPT
616 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000617
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000618 _LIBCPP_INLINE_VISIBILITY
619 const_iterator cbegin() const _NOEXCEPT
620 {return begin();}
621 _LIBCPP_INLINE_VISIBILITY
622 const_iterator cend() const _NOEXCEPT
623 {return end();}
624 _LIBCPP_INLINE_VISIBILITY
625 const_reverse_iterator crbegin() const _NOEXCEPT
626 {return rbegin();}
627 _LIBCPP_INLINE_VISIBILITY
628 const_reverse_iterator crend() const _NOEXCEPT
629 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000630
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000631 _LIBCPP_INLINE_VISIBILITY
632 size_type size() const _NOEXCEPT
633 {return static_cast<size_type>(this->__end_ - this->__begin_);}
634 _LIBCPP_INLINE_VISIBILITY
635 size_type capacity() const _NOEXCEPT
636 {return __base::capacity();}
637 _LIBCPP_INLINE_VISIBILITY
638 bool empty() const _NOEXCEPT
639 {return this->__begin_ == this->__end_;}
640 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000641 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000642 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000643
644 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
645 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
646 reference at(size_type __n);
647 const_reference at(size_type __n) const;
648
Howard Hinnant7a563db2011-09-14 18:33:51 +0000649 _LIBCPP_INLINE_VISIBILITY reference front()
650 {
651 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
652 return *this->__begin_;
653 }
654 _LIBCPP_INLINE_VISIBILITY const_reference front() const
655 {
656 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
657 return *this->__begin_;
658 }
659 _LIBCPP_INLINE_VISIBILITY reference back()
660 {
661 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
662 return *(this->__end_ - 1);
663 }
664 _LIBCPP_INLINE_VISIBILITY const_reference back() const
665 {
666 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
667 return *(this->__end_ - 1);
668 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000669
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000670 _LIBCPP_INLINE_VISIBILITY
671 value_type* data() _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000672 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000673 _LIBCPP_INLINE_VISIBILITY
674 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000675 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000676
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000677 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000678#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000679 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000680#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000681 template <class... _Args>
682 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000683#endif // _LIBCPP_HAS_NO_VARIADICS
684#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000685 void pop_back();
686
687 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000688#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000689 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000690#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000691 template <class... _Args>
692 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000693#endif // _LIBCPP_HAS_NO_VARIADICS
694#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000695 iterator insert(const_iterator __position, size_type __n, const_reference __x);
696 template <class _InputIterator>
697 typename enable_if
698 <
699 __is_input_iterator <_InputIterator>::value &&
700 !__is_forward_iterator<_InputIterator>::value,
701 iterator
702 >::type
703 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
704 template <class _ForwardIterator>
705 typename enable_if
706 <
707 __is_forward_iterator<_ForwardIterator>::value,
708 iterator
709 >::type
710 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +0000711#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000713 iterator insert(const_iterator __position, initializer_list<value_type> __il)
714 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000715#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000716
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000717 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000718 iterator erase(const_iterator __first, const_iterator __last);
719
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000720 _LIBCPP_INLINE_VISIBILITY
721 void clear() _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000722 {
723 __base::clear();
724 __invalidate_all_iterators();
725 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000726
727 void resize(size_type __sz);
728 void resize(size_type __sz, const_reference __x);
729
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000730 void swap(vector&)
731 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
732 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000733
734 bool __invariants() const;
735
Howard Hinnantabe26282011-09-16 17:29:17 +0000736#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000737
738 bool __dereferenceable(const const_iterator* __i) const;
739 bool __decrementable(const const_iterator* __i) const;
740 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
741 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
742
Howard Hinnantabe26282011-09-16 17:29:17 +0000743#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000744
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000745private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000746 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000747 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000748 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000749 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31 +0000750 void __construct_at_end(size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000751 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000752 template <class _ForwardIterator>
753 typename enable_if
754 <
755 __is_forward_iterator<_ForwardIterator>::value,
756 void
757 >::type
758 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
759 void __move_construct_at_end(pointer __first, pointer __last);
760 void __append(size_type __n);
761 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000762 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000763 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000764 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000765 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000766 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
767 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
768 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000769 void __move_assign(vector& __c, true_type)
770 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000771 void __move_assign(vector& __c, false_type);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000772 _LIBCPP_INLINE_VISIBILITY
773 void __destruct_at_end(const_pointer __new_last) _NOEXCEPT
774 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000775#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000776 __c_node* __c = __get_db()->__find_c_and_lock(this);
777 for (__i_node** __p = __c->end_; __p != __c->beg_; )
778 {
779 --__p;
780 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
781 if (__i->base() > __new_last)
782 {
783 (*__p)->__c_ = nullptr;
784 if (--__c->end_ != __p)
785 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
786 }
787 }
788 __get_db()->unlock();
789#endif
790 __base::__destruct_at_end(__new_last);
791 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000792 template <class _Up>
793 void
794#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
795 __push_back_slow_path(_Up&& __x);
796#else
797 __push_back_slow_path(_Up& __x);
798#endif
Howard Hinnant0438ea22012-02-26 15:30:12 +0000799#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
800 template <class... _Args>
801 void
802 __emplace_back_slow_path(_Args&&... __args);
803#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000804};
805
806template <class _Tp, class _Allocator>
807void
808vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
809{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000810 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000811 _VSTD::swap(this->__begin_, __v.__begin_);
812 _VSTD::swap(this->__end_, __v.__end_);
813 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000814 __v.__first_ = __v.__begin_;
815 __invalidate_all_iterators();
816}
817
818template <class _Tp, class _Allocator>
819typename vector<_Tp, _Allocator>::pointer
820vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
821{
822 pointer __r = __v.__begin_;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000823 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
824 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000825 _VSTD::swap(this->__begin_, __v.__begin_);
826 _VSTD::swap(this->__end_, __v.__end_);
827 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000828 __v.__first_ = __v.__begin_;
829 __invalidate_all_iterators();
830 return __r;
831}
832
833// Allocate space for __n objects
834// throws length_error if __n > max_size()
835// throws (probably bad_alloc) if memory run out
836// Precondition: __begin_ == __end_ == __end_cap() == 0
837// Precondition: __n > 0
838// Postcondition: capacity() == __n
839// Postcondition: size() == 0
840template <class _Tp, class _Allocator>
841void
842vector<_Tp, _Allocator>::allocate(size_type __n)
843{
844 if (__n > max_size())
845 this->__throw_length_error();
846 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
847 this->__end_cap() = this->__begin_ + __n;
848}
849
850template <class _Tp, class _Allocator>
851void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000852vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000853{
854 if (this->__begin_ != 0)
855 {
856 clear();
857 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000858 this->__begin_ = this->__end_ = this->__end_cap() = 0;
859 }
860}
861
862template <class _Tp, class _Allocator>
863typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000864vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000865{
Sean Hunt110b8bf2011-07-29 23:31:58 +0000866 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 +0000867}
868
869// Precondition: __new_size > capacity()
870template <class _Tp, class _Allocator>
871_LIBCPP_INLINE_VISIBILITY inline
872typename vector<_Tp, _Allocator>::size_type
873vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
874{
875 const size_type __ms = max_size();
876 if (__new_size > __ms)
877 this->__throw_length_error();
878 const size_type __cap = capacity();
879 if (__cap >= __ms / 2)
880 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58 +0000881 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000882}
883
884// Default constructs __n objects starting at __end_
885// throws if construction throws
886// Precondition: __n > 0
887// Precondition: size() + __n <= capacity()
888// Postcondition: size() == size() + __n
889template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000890void
891vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
892{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000893 allocator_type& __a = this->__alloc();
894 do
895 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000896 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000897 ++this->__end_;
898 --__n;
899 } while (__n > 0);
900}
901
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000902// Copy constructs __n objects starting at __end_ from __x
903// throws if construction throws
904// Precondition: __n > 0
905// Precondition: size() + __n <= capacity()
906// Postcondition: size() == old size() + __n
907// Postcondition: [i] == __x for all i in [size() - __n, __n)
908template <class _Tp, class _Allocator>
909_LIBCPP_INLINE_VISIBILITY inline
910void
911vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
912{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000913 allocator_type& __a = this->__alloc();
914 do
915 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000916 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000917 ++this->__end_;
918 --__n;
919 } while (__n > 0);
920}
921
922template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000923template <class _ForwardIterator>
924typename enable_if
925<
926 __is_forward_iterator<_ForwardIterator>::value,
927 void
928>::type
929vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
930{
931 allocator_type& __a = this->__alloc();
932 for (; __first != __last; ++__first)
933 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000934 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000935 ++this->__end_;
936 }
937}
938
939template <class _Tp, class _Allocator>
940void
941vector<_Tp, _Allocator>::__move_construct_at_end(pointer __first, pointer __last)
942{
943 allocator_type& __a = this->__alloc();
944 for (; __first != __last; ++__first)
945 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000946 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
947 _VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000948 ++this->__end_;
949 }
950}
951
952// Default constructs __n objects starting at __end_
953// throws if construction throws
954// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000955// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000956template <class _Tp, class _Allocator>
957void
958vector<_Tp, _Allocator>::__append(size_type __n)
959{
960 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
961 this->__construct_at_end(__n);
962 else
963 {
964 allocator_type& __a = this->__alloc();
965 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
966 __v.__construct_at_end(__n);
967 __swap_out_circular_buffer(__v);
968 }
969}
970
971// Default constructs __n objects starting at __end_
972// throws if construction throws
973// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000974// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000975template <class _Tp, class _Allocator>
976void
977vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
978{
979 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
980 this->__construct_at_end(__n, __x);
981 else
982 {
983 allocator_type& __a = this->__alloc();
984 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
985 __v.__construct_at_end(__n, __x);
986 __swap_out_circular_buffer(__v);
987 }
988}
989
990template <class _Tp, class _Allocator>
991vector<_Tp, _Allocator>::vector(size_type __n)
992{
Howard Hinnant0442b122011-09-16 18:41:29 +0000993#if _LIBCPP_DEBUG_LEVEL >= 2
994 __get_db()->__insert_c(this);
995#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000996 if (__n > 0)
997 {
998 allocate(__n);
999 __construct_at_end(__n);
1000 }
1001}
1002
1003template <class _Tp, class _Allocator>
1004vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1005{
Howard Hinnant0442b122011-09-16 18:41:29 +00001006#if _LIBCPP_DEBUG_LEVEL >= 2
1007 __get_db()->__insert_c(this);
1008#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001009 if (__n > 0)
1010 {
1011 allocate(__n);
1012 __construct_at_end(__n, __x);
1013 }
1014}
1015
1016template <class _Tp, class _Allocator>
1017vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1018 : __base(__a)
1019{
Howard Hinnant0442b122011-09-16 18:41:29 +00001020#if _LIBCPP_DEBUG_LEVEL >= 2
1021 __get_db()->__insert_c(this);
1022#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001023 if (__n > 0)
1024 {
1025 allocate(__n);
1026 __construct_at_end(__n, __x);
1027 }
1028}
1029
1030template <class _Tp, class _Allocator>
1031template <class _InputIterator>
1032vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
1033 typename enable_if<__is_input_iterator <_InputIterator>::value &&
1034 !__is_forward_iterator<_InputIterator>::value>::type*)
1035{
Howard Hinnantabe26282011-09-16 17:29:17 +00001036#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001037 __get_db()->__insert_c(this);
1038#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001039 for (; __first != __last; ++__first)
1040 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001041}
1042
1043template <class _Tp, class _Allocator>
1044template <class _InputIterator>
1045vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1046 typename enable_if<__is_input_iterator <_InputIterator>::value &&
1047 !__is_forward_iterator<_InputIterator>::value>::type*)
1048 : __base(__a)
1049{
Howard Hinnantabe26282011-09-16 17:29:17 +00001050#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001051 __get_db()->__insert_c(this);
1052#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001053 for (; __first != __last; ++__first)
1054 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001055}
1056
1057template <class _Tp, class _Allocator>
1058template <class _ForwardIterator>
1059vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
1060 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
1061{
Howard Hinnant0442b122011-09-16 18:41:29 +00001062#if _LIBCPP_DEBUG_LEVEL >= 2
1063 __get_db()->__insert_c(this);
1064#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001065 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001066 if (__n > 0)
1067 {
1068 allocate(__n);
1069 __construct_at_end(__first, __last);
1070 }
1071}
1072
1073template <class _Tp, class _Allocator>
1074template <class _ForwardIterator>
1075vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
1076 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
1077 : __base(__a)
1078{
Howard Hinnant0442b122011-09-16 18:41:29 +00001079#if _LIBCPP_DEBUG_LEVEL >= 2
1080 __get_db()->__insert_c(this);
1081#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001082 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001083 if (__n > 0)
1084 {
1085 allocate(__n);
1086 __construct_at_end(__first, __last);
1087 }
1088}
1089
1090template <class _Tp, class _Allocator>
1091vector<_Tp, _Allocator>::vector(const vector& __x)
1092 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1093{
Howard Hinnant0442b122011-09-16 18:41:29 +00001094#if _LIBCPP_DEBUG_LEVEL >= 2
1095 __get_db()->__insert_c(this);
1096#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001097 size_type __n = __x.size();
1098 if (__n > 0)
1099 {
1100 allocate(__n);
1101 __construct_at_end(__x.__begin_, __x.__end_);
1102 }
1103}
1104
1105template <class _Tp, class _Allocator>
1106vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1107 : __base(__a)
1108{
Howard Hinnant0442b122011-09-16 18:41:29 +00001109#if _LIBCPP_DEBUG_LEVEL >= 2
1110 __get_db()->__insert_c(this);
1111#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001112 size_type __n = __x.size();
1113 if (__n > 0)
1114 {
1115 allocate(__n);
1116 __construct_at_end(__x.__begin_, __x.__end_);
1117 }
1118}
1119
Howard Hinnant73d21a42010-09-04 23:28:19 +00001120#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001121
1122template <class _Tp, class _Allocator>
1123_LIBCPP_INLINE_VISIBILITY inline
1124vector<_Tp, _Allocator>::vector(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001125 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001126 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001127{
Howard Hinnantabe26282011-09-16 17:29:17 +00001128#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001129 __get_db()->__insert_c(this);
Howard Hinnante6125bd2011-09-19 16:34:29 +00001130 __get_db()->swap(this, &__x);
Howard Hinnant7a563db2011-09-14 18:33:51 +00001131#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001132 this->__begin_ = __x.__begin_;
1133 this->__end_ = __x.__end_;
1134 this->__end_cap() = __x.__end_cap();
1135 __x.__begin_ = __x.__end_ = __x.__end_cap() = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001136}
1137
1138template <class _Tp, class _Allocator>
1139_LIBCPP_INLINE_VISIBILITY inline
1140vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1141 : __base(__a)
1142{
Howard Hinnant0442b122011-09-16 18:41:29 +00001143#if _LIBCPP_DEBUG_LEVEL >= 2
1144 __get_db()->__insert_c(this);
1145#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001146 if (__a == __x.__alloc())
1147 {
1148 this->__begin_ = __x.__begin_;
1149 this->__end_ = __x.__end_;
1150 this->__end_cap() = __x.__end_cap();
1151 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001152#if _LIBCPP_DEBUG_LEVEL >= 2
1153 __get_db()->swap(this, &__x);
1154#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001155 }
1156 else
1157 {
Howard Hinnant99968442011-11-29 18:15:50 +00001158 typedef move_iterator<iterator> _Ip;
1159 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001160 }
1161}
1162
Howard Hinnante3e32912011-08-12 21:56:02 +00001163#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1164
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001165template <class _Tp, class _Allocator>
1166_LIBCPP_INLINE_VISIBILITY inline
1167vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1168{
Howard Hinnant0442b122011-09-16 18:41:29 +00001169#if _LIBCPP_DEBUG_LEVEL >= 2
1170 __get_db()->__insert_c(this);
1171#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001172 if (__il.size() > 0)
1173 {
1174 allocate(__il.size());
1175 __construct_at_end(__il.begin(), __il.end());
1176 }
1177}
1178
1179template <class _Tp, class _Allocator>
1180_LIBCPP_INLINE_VISIBILITY inline
1181vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1182 : __base(__a)
1183{
Howard Hinnant0442b122011-09-16 18:41:29 +00001184#if _LIBCPP_DEBUG_LEVEL >= 2
1185 __get_db()->__insert_c(this);
1186#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001187 if (__il.size() > 0)
1188 {
1189 allocate(__il.size());
1190 __construct_at_end(__il.begin(), __il.end());
1191 }
1192}
1193
Howard Hinnante3e32912011-08-12 21:56:02 +00001194#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1195
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001196template <class _Tp, class _Allocator>
1197_LIBCPP_INLINE_VISIBILITY inline
1198vector<_Tp, _Allocator>&
1199vector<_Tp, _Allocator>::operator=(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001200 _NOEXCEPT_(
1201 __alloc_traits::propagate_on_container_move_assignment::value &&
1202 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001203{
1204 __move_assign(__x, integral_constant<bool,
1205 __alloc_traits::propagate_on_container_move_assignment::value>());
1206 return *this;
1207}
1208
1209template <class _Tp, class _Allocator>
1210void
1211vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1212{
1213 if (__base::__alloc() != __c.__alloc())
1214 {
Howard Hinnant99968442011-11-29 18:15:50 +00001215 typedef move_iterator<iterator> _Ip;
1216 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001217 }
1218 else
1219 __move_assign(__c, true_type());
1220}
1221
1222template <class _Tp, class _Allocator>
1223void
1224vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001225 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001226{
1227 deallocate();
1228 this->__begin_ = __c.__begin_;
1229 this->__end_ = __c.__end_;
1230 this->__end_cap() = __c.__end_cap();
1231 __base::__move_assign_alloc(__c);
1232 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001233#if _LIBCPP_DEBUG_LEVEL >= 2
1234 __get_db()->swap(this, &__c);
1235#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001236}
1237
Howard Hinnant73d21a42010-09-04 23:28:19 +00001238#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001239
1240template <class _Tp, class _Allocator>
1241_LIBCPP_INLINE_VISIBILITY inline
1242vector<_Tp, _Allocator>&
1243vector<_Tp, _Allocator>::operator=(const vector& __x)
1244{
1245 if (this != &__x)
1246 {
1247 __base::__copy_assign_alloc(__x);
1248 assign(__x.__begin_, __x.__end_);
1249 }
1250 return *this;
1251}
1252
1253template <class _Tp, class _Allocator>
1254template <class _InputIterator>
1255typename enable_if
1256<
1257 __is_input_iterator <_InputIterator>::value &&
1258 !__is_forward_iterator<_InputIterator>::value,
1259 void
1260>::type
1261vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1262{
1263 clear();
1264 for (; __first != __last; ++__first)
1265 push_back(*__first);
1266}
1267
1268template <class _Tp, class _Allocator>
1269template <class _ForwardIterator>
1270typename enable_if
1271<
1272 __is_forward_iterator<_ForwardIterator>::value,
1273 void
1274>::type
1275vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1276{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001277 typename iterator_traits<_ForwardIterator>::difference_type __new_size = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001278 if (static_cast<size_type>(__new_size) <= capacity())
1279 {
1280 _ForwardIterator __mid = __last;
1281 bool __growing = false;
1282 if (static_cast<size_type>(__new_size) > size())
1283 {
1284 __growing = true;
1285 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001286 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001287 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001288 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001289 if (__growing)
1290 __construct_at_end(__mid, __last);
1291 else
1292 this->__destruct_at_end(__m);
1293 }
1294 else
1295 {
1296 deallocate();
1297 allocate(__recommend(static_cast<size_type>(__new_size)));
1298 __construct_at_end(__first, __last);
1299 }
1300}
1301
1302template <class _Tp, class _Allocator>
1303void
1304vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1305{
1306 if (__n <= capacity())
1307 {
1308 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001309 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001310 if (__n > __s)
1311 __construct_at_end(__n - __s, __u);
1312 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001313 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001314 }
1315 else
1316 {
1317 deallocate();
1318 allocate(__recommend(static_cast<size_type>(__n)));
1319 __construct_at_end(__n, __u);
1320 }
1321}
1322
Howard Hinnant324bb032010-08-22 00:02:43 +00001323template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001324_LIBCPP_INLINE_VISIBILITY inline
1325typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001326vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001327{
Howard Hinnantabe26282011-09-16 17:29:17 +00001328#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001329 return iterator(this, __p);
1330#else
1331 return iterator(__p);
1332#endif
1333}
1334
Howard Hinnant324bb032010-08-22 00:02:43 +00001335template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001336_LIBCPP_INLINE_VISIBILITY inline
1337typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001338vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001339{
Howard Hinnantabe26282011-09-16 17:29:17 +00001340#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001341 return const_iterator(this, __p);
1342#else
1343 return const_iterator(__p);
1344#endif
1345}
1346
Howard Hinnant324bb032010-08-22 00:02:43 +00001347template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001348_LIBCPP_INLINE_VISIBILITY inline
1349typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001350vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001351{
1352 return __make_iter(this->__begin_);
1353}
1354
Howard Hinnant324bb032010-08-22 00:02:43 +00001355template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001356_LIBCPP_INLINE_VISIBILITY inline
1357typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001358vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001359{
1360 return __make_iter(this->__begin_);
1361}
1362
Howard Hinnant324bb032010-08-22 00:02:43 +00001363template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001364_LIBCPP_INLINE_VISIBILITY inline
1365typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001366vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001367{
1368 return __make_iter(this->__end_);
1369}
1370
Howard Hinnant324bb032010-08-22 00:02:43 +00001371template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001372_LIBCPP_INLINE_VISIBILITY inline
1373typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001374vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001375{
1376 return __make_iter(this->__end_);
1377}
1378
Howard Hinnant324bb032010-08-22 00:02:43 +00001379template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001380_LIBCPP_INLINE_VISIBILITY inline
1381typename vector<_Tp, _Allocator>::reference
1382vector<_Tp, _Allocator>::operator[](size_type __n)
1383{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001384 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001385 return this->__begin_[__n];
1386}
1387
Howard Hinnant324bb032010-08-22 00:02:43 +00001388template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001389_LIBCPP_INLINE_VISIBILITY inline
1390typename vector<_Tp, _Allocator>::const_reference
1391vector<_Tp, _Allocator>::operator[](size_type __n) const
1392{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001393 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001394 return this->__begin_[__n];
1395}
1396
Howard Hinnant324bb032010-08-22 00:02:43 +00001397template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001398typename vector<_Tp, _Allocator>::reference
1399vector<_Tp, _Allocator>::at(size_type __n)
1400{
1401 if (__n >= size())
1402 this->__throw_out_of_range();
1403 return this->__begin_[__n];
1404}
1405
Howard Hinnant324bb032010-08-22 00:02:43 +00001406template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001407typename vector<_Tp, _Allocator>::const_reference
1408vector<_Tp, _Allocator>::at(size_type __n) const
1409{
1410 if (__n >= size())
1411 this->__throw_out_of_range();
1412 return this->__begin_[__n];
1413}
1414
1415template <class _Tp, class _Allocator>
1416void
1417vector<_Tp, _Allocator>::reserve(size_type __n)
1418{
1419 if (__n > capacity())
1420 {
1421 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001422 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001423 __swap_out_circular_buffer(__v);
1424 }
1425}
1426
1427template <class _Tp, class _Allocator>
1428void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001429vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001430{
1431 if (capacity() > size())
1432 {
1433#ifndef _LIBCPP_NO_EXCEPTIONS
1434 try
1435 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001436#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001437 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001438 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001439 __swap_out_circular_buffer(__v);
1440#ifndef _LIBCPP_NO_EXCEPTIONS
1441 }
1442 catch (...)
1443 {
1444 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001445#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001446 }
1447}
1448
1449template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001450template <class _Up>
1451void
1452#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1453vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1454#else
1455vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1456#endif
1457{
1458 allocator_type& __a = this->__alloc();
1459 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1460 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnantf619e232013-01-11 20:36:59 +00001461 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1462 __v.__end_++;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001463 __swap_out_circular_buffer(__v);
1464}
1465
1466template <class _Tp, class _Allocator>
1467_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001468void
1469vector<_Tp, _Allocator>::push_back(const_reference __x)
1470{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001471 if (this->__end_ != this->__end_cap())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001472 {
1473 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001474 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001475 ++this->__end_;
1476 }
1477 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001478 __push_back_slow_path(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001479}
1480
Howard Hinnant73d21a42010-09-04 23:28:19 +00001481#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001482
1483template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001484_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001485void
1486vector<_Tp, _Allocator>::push_back(value_type&& __x)
1487{
1488 if (this->__end_ < this->__end_cap())
1489 {
1490 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001491 _VSTD::__to_raw_pointer(this->__end_),
1492 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001493 ++this->__end_;
1494 }
1495 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001496 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001497}
1498
Howard Hinnant73d21a42010-09-04 23:28:19 +00001499#ifndef _LIBCPP_HAS_NO_VARIADICS
1500
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001501template <class _Tp, class _Allocator>
1502template <class... _Args>
1503void
Howard Hinnant0438ea22012-02-26 15:30:12 +00001504vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1505{
1506 allocator_type& __a = this->__alloc();
1507 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1508// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantf619e232013-01-11 20:36:59 +00001509 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1510 __v.__end_++;
Howard Hinnant0438ea22012-02-26 15:30:12 +00001511 __swap_out_circular_buffer(__v);
1512}
1513
1514template <class _Tp, class _Allocator>
1515template <class... _Args>
1516_LIBCPP_INLINE_VISIBILITY inline
1517void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001518vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1519{
1520 if (this->__end_ < this->__end_cap())
1521 {
1522 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001523 _VSTD::__to_raw_pointer(this->__end_),
1524 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001525 ++this->__end_;
1526 }
1527 else
Howard Hinnant0438ea22012-02-26 15:30:12 +00001528 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001529}
1530
Howard Hinnant73d21a42010-09-04 23:28:19 +00001531#endif // _LIBCPP_HAS_NO_VARIADICS
1532#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001533
1534template <class _Tp, class _Allocator>
1535_LIBCPP_INLINE_VISIBILITY inline
1536void
1537vector<_Tp, _Allocator>::pop_back()
1538{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001539 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001540 this->__destruct_at_end(this->__end_ - 1);
1541}
1542
1543template <class _Tp, class _Allocator>
1544_LIBCPP_INLINE_VISIBILITY inline
1545typename vector<_Tp, _Allocator>::iterator
1546vector<_Tp, _Allocator>::erase(const_iterator __position)
1547{
Howard Hinnantabe26282011-09-16 17:29:17 +00001548#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001549 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1550 "vector::erase(iterator) called with an iterator not"
1551 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001552#endif
Howard Hinnant782da332013-03-25 22:12:26 +00001553 _LIBCPP_ASSERT(__position != end(),
1554 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant635bbbb2013-02-07 15:31:44 +00001555 pointer __p = const_cast<pointer>(&*__position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001556 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001557 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001558 return __r;
1559}
1560
1561template <class _Tp, class _Allocator>
1562typename vector<_Tp, _Allocator>::iterator
1563vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1564{
Howard Hinnantabe26282011-09-16 17:29:17 +00001565#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001566 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1567 "vector::erase(iterator, iterator) called with an iterator not"
1568 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001569#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +00001570 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001571 pointer __p = this->__begin_ + (__first - begin());
1572 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001573 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001574 return __r;
1575}
1576
1577template <class _Tp, class _Allocator>
1578void
1579vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1580{
1581 pointer __old_last = this->__end_;
1582 difference_type __n = __old_last - __to;
1583 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1584 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001585 _VSTD::__to_raw_pointer(this->__end_),
1586 _VSTD::move(*__i));
1587 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001588}
1589
1590template <class _Tp, class _Allocator>
1591typename vector<_Tp, _Allocator>::iterator
1592vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1593{
Howard Hinnantabe26282011-09-16 17:29:17 +00001594#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001595 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1596 "vector::insert(iterator, x) called with an iterator not"
1597 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001598#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001599 pointer __p = this->__begin_ + (__position - begin());
1600 if (this->__end_ < this->__end_cap())
1601 {
1602 if (__p == this->__end_)
1603 {
1604 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001605 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001606 ++this->__end_;
1607 }
1608 else
1609 {
1610 __move_range(__p, this->__end_, __p + 1);
1611 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1612 if (__p <= __xr && __xr < this->__end_)
1613 ++__xr;
1614 *__p = *__xr;
1615 }
1616 }
1617 else
1618 {
1619 allocator_type& __a = this->__alloc();
1620 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1621 __v.push_back(__x);
1622 __p = __swap_out_circular_buffer(__v, __p);
1623 }
1624 return __make_iter(__p);
1625}
1626
Howard Hinnant73d21a42010-09-04 23:28:19 +00001627#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001628
1629template <class _Tp, class _Allocator>
1630typename vector<_Tp, _Allocator>::iterator
1631vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1632{
Howard Hinnantabe26282011-09-16 17:29:17 +00001633#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001634 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1635 "vector::insert(iterator, x) called with an iterator not"
1636 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001637#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001638 pointer __p = this->__begin_ + (__position - begin());
1639 if (this->__end_ < this->__end_cap())
1640 {
1641 if (__p == this->__end_)
1642 {
1643 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001644 _VSTD::__to_raw_pointer(this->__end_),
1645 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001646 ++this->__end_;
1647 }
1648 else
1649 {
1650 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001651 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001652 }
1653 }
1654 else
1655 {
1656 allocator_type& __a = this->__alloc();
1657 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001658 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001659 __p = __swap_out_circular_buffer(__v, __p);
1660 }
1661 return __make_iter(__p);
1662}
1663
Howard Hinnant73d21a42010-09-04 23:28:19 +00001664#ifndef _LIBCPP_HAS_NO_VARIADICS
1665
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001666template <class _Tp, class _Allocator>
1667template <class... _Args>
1668typename vector<_Tp, _Allocator>::iterator
1669vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1670{
Howard Hinnantabe26282011-09-16 17:29:17 +00001671#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001672 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1673 "vector::emplace(iterator, x) called with an iterator not"
1674 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001675#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676 pointer __p = this->__begin_ + (__position - begin());
1677 if (this->__end_ < this->__end_cap())
1678 {
1679 if (__p == this->__end_)
1680 {
1681 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001682 _VSTD::__to_raw_pointer(this->__end_),
1683 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001684 ++this->__end_;
1685 }
1686 else
1687 {
Howard Hinnanta58402a2012-07-08 23:23:04 +00001688 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001689 __move_range(__p, this->__end_, __p + 1);
Howard Hinnanta58402a2012-07-08 23:23:04 +00001690 *__p = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001691 }
1692 }
1693 else
1694 {
1695 allocator_type& __a = this->__alloc();
1696 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001697 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001698 __p = __swap_out_circular_buffer(__v, __p);
1699 }
1700 return __make_iter(__p);
1701}
1702
Howard Hinnant73d21a42010-09-04 23:28:19 +00001703#endif // _LIBCPP_HAS_NO_VARIADICS
1704#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001705
1706template <class _Tp, class _Allocator>
1707typename vector<_Tp, _Allocator>::iterator
1708vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1709{
Howard Hinnantabe26282011-09-16 17:29:17 +00001710#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001711 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1712 "vector::insert(iterator, n, x) called with an iterator not"
1713 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001714#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001715 pointer __p = this->__begin_ + (__position - begin());
1716 if (__n > 0)
1717 {
1718 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1719 {
1720 size_type __old_n = __n;
1721 pointer __old_last = this->__end_;
1722 if (__n > static_cast<size_type>(this->__end_ - __p))
1723 {
1724 size_type __cx = __n - (this->__end_ - __p);
1725 __construct_at_end(__cx, __x);
1726 __n -= __cx;
1727 }
1728 if (__n > 0)
1729 {
1730 __move_range(__p, __old_last, __p + __old_n);
1731 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1732 if (__p <= __xr && __xr < this->__end_)
1733 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001734 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001735 }
1736 }
1737 else
1738 {
1739 allocator_type& __a = this->__alloc();
1740 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1741 __v.__construct_at_end(__n, __x);
1742 __p = __swap_out_circular_buffer(__v, __p);
1743 }
1744 }
1745 return __make_iter(__p);
1746}
1747
1748template <class _Tp, class _Allocator>
1749template <class _InputIterator>
1750typename enable_if
1751<
1752 __is_input_iterator <_InputIterator>::value &&
1753 !__is_forward_iterator<_InputIterator>::value,
1754 typename vector<_Tp, _Allocator>::iterator
1755>::type
1756vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1757{
Howard Hinnantabe26282011-09-16 17:29:17 +00001758#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001759 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1760 "vector::insert(iterator, range) called with an iterator not"
1761 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001762#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001763 difference_type __off = __position - begin();
1764 pointer __p = this->__begin_ + __off;
1765 allocator_type& __a = this->__alloc();
1766 pointer __old_last = this->__end_;
1767 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1768 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001769 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001770 *__first);
1771 ++this->__end_;
1772 }
1773 __split_buffer<value_type, allocator_type&> __v(__a);
1774 if (__first != __last)
1775 {
1776#ifndef _LIBCPP_NO_EXCEPTIONS
1777 try
1778 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001779#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001780 __v.__construct_at_end(__first, __last);
1781 difference_type __old_size = __old_last - this->__begin_;
1782 difference_type __old_p = __p - this->__begin_;
1783 reserve(__recommend(size() + __v.size()));
1784 __p = this->__begin_ + __old_p;
1785 __old_last = this->__begin_ + __old_size;
1786#ifndef _LIBCPP_NO_EXCEPTIONS
1787 }
1788 catch (...)
1789 {
1790 erase(__make_iter(__old_last), end());
1791 throw;
1792 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001793#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001794 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001795 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant0442b122011-09-16 18:41:29 +00001796 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1797 make_move_iterator(__v.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001798 return begin() + __off;
1799}
1800
1801template <class _Tp, class _Allocator>
1802template <class _ForwardIterator>
1803typename enable_if
1804<
1805 __is_forward_iterator<_ForwardIterator>::value,
1806 typename vector<_Tp, _Allocator>::iterator
1807>::type
1808vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1809{
Howard Hinnantabe26282011-09-16 17:29:17 +00001810#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001811 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1812 "vector::insert(iterator, range) called with an iterator not"
1813 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001814#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001815 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00001816 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817 if (__n > 0)
1818 {
1819 if (__n <= this->__end_cap() - this->__end_)
1820 {
1821 size_type __old_n = __n;
1822 pointer __old_last = this->__end_;
1823 _ForwardIterator __m = __last;
1824 difference_type __dx = this->__end_ - __p;
1825 if (__n > __dx)
1826 {
1827 __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001828 _VSTD::advance(__m, this->__end_ - __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829 __construct_at_end(__m, __last);
1830 __n = __dx;
1831 }
1832 if (__n > 0)
1833 {
1834 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001835 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001836 }
1837 }
1838 else
1839 {
1840 allocator_type& __a = this->__alloc();
1841 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1842 __v.__construct_at_end(__first, __last);
1843 __p = __swap_out_circular_buffer(__v, __p);
1844 }
1845 }
1846 return __make_iter(__p);
1847}
1848
1849template <class _Tp, class _Allocator>
1850void
1851vector<_Tp, _Allocator>::resize(size_type __sz)
1852{
1853 size_type __cs = size();
1854 if (__cs < __sz)
1855 this->__append(__sz - __cs);
1856 else if (__cs > __sz)
1857 this->__destruct_at_end(this->__begin_ + __sz);
1858}
1859
1860template <class _Tp, class _Allocator>
1861void
1862vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
1863{
1864 size_type __cs = size();
1865 if (__cs < __sz)
1866 this->__append(__sz - __cs, __x);
1867 else if (__cs > __sz)
1868 this->__destruct_at_end(this->__begin_ + __sz);
1869}
1870
1871template <class _Tp, class _Allocator>
1872void
1873vector<_Tp, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001874 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1875 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001876{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001877 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
1878 this->__alloc() == __x.__alloc(),
1879 "vector::swap: Either propagate_on_container_swap must be true"
1880 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +00001881 _VSTD::swap(this->__begin_, __x.__begin_);
1882 _VSTD::swap(this->__end_, __x.__end_);
1883 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001884 __base::__swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnantabe26282011-09-16 17:29:17 +00001885#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001886 __get_db()->swap(this, &__x);
Howard Hinnantabe26282011-09-16 17:29:17 +00001887#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001888}
1889
Howard Hinnant324bb032010-08-22 00:02:43 +00001890template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001891bool
1892vector<_Tp, _Allocator>::__invariants() const
1893{
1894 if (this->__begin_ == 0)
1895 {
1896 if (this->__end_ != 0 || this->__end_cap() != 0)
1897 return false;
1898 }
1899 else
1900 {
1901 if (this->__begin_ > this->__end_)
1902 return false;
1903 if (this->__begin_ == this->__end_cap())
1904 return false;
1905 if (this->__end_ > this->__end_cap())
1906 return false;
1907 }
1908 return true;
1909}
1910
Howard Hinnantabe26282011-09-16 17:29:17 +00001911#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001912
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001913template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:51 +00001914bool
1915vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
1916{
1917 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
1918}
1919
1920template <class _Tp, class _Allocator>
1921bool
1922vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
1923{
1924 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
1925}
1926
1927template <class _Tp, class _Allocator>
1928bool
1929vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
1930{
1931 const_pointer __p = __i->base() + __n;
1932 return this->__begin_ <= __p && __p <= this->__end_;
1933}
1934
1935template <class _Tp, class _Allocator>
1936bool
1937vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
1938{
1939 const_pointer __p = __i->base() + __n;
1940 return this->__begin_ <= __p && __p < this->__end_;
1941}
1942
Howard Hinnantabe26282011-09-16 17:29:17 +00001943#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001944
1945template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001946_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001947void
1948vector<_Tp, _Allocator>::__invalidate_all_iterators()
1949{
Howard Hinnantabe26282011-09-16 17:29:17 +00001950#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001951 __get_db()->__invalidate_all(this);
Howard Hinnantabe26282011-09-16 17:29:17 +00001952#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953}
1954
1955// vector<bool>
1956
1957template <class _Allocator> class vector<bool, _Allocator>;
1958
1959template <class _Allocator> struct hash<vector<bool, _Allocator> >;
1960
1961template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:23 +00001962struct __has_storage_type<vector<bool, _Allocator> >
1963{
1964 static const bool value = true;
1965};
1966
1967template <class _Allocator>
Howard Hinnant83eade62013-03-06 23:30:19 +00001968class _LIBCPP_TYPE_VIS vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001969 : private __vector_base_common<true>
1970{
1971public:
1972 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00001973 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001974 typedef _Allocator allocator_type;
1975 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001976 typedef typename __alloc_traits::size_type size_type;
1977 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantf867f632012-05-07 16:50:38 +00001978 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001979 typedef __bit_iterator<vector, false> pointer;
1980 typedef __bit_iterator<vector, true> const_pointer;
1981#ifdef _LIBCPP_DEBUG
1982 typedef __debug_iter<vector, pointer> iterator;
1983 typedef __debug_iter<vector, const_pointer> const_iterator;
1984
1985 friend class __debug_iter<vector, pointer>;
1986 friend class __debug_iter<vector, const_pointer>;
1987
1988 pair<iterator*, const_iterator*> __iterator_list_;
1989
1990 _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;}
1991 _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;}
Howard Hinnant324bb032010-08-22 00:02:43 +00001992#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001993 typedef pointer iterator;
1994 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +00001995#endif // _LIBCPP_DEBUG
Howard Hinnant0949eed2011-06-30 21:18:19 +00001996 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1997 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001998
1999private:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002000 typedef typename __alloc_traits::template
2001#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
2002 rebind_alloc<__storage_type>
2003#else
2004 rebind_alloc<__storage_type>::other
2005#endif
2006 __storage_allocator;
2007 typedef allocator_traits<__storage_allocator> __storage_traits;
2008 typedef typename __storage_traits::pointer __storage_pointer;
2009 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2010
2011 __storage_pointer __begin_;
2012 size_type __size_;
2013 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002014public:
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002015 typedef __bit_reference<vector> reference;
2016 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002017private:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002018 _LIBCPP_INLINE_VISIBILITY
2019 size_type& __cap() _NOEXCEPT
2020 {return __cap_alloc_.first();}
2021 _LIBCPP_INLINE_VISIBILITY
2022 const size_type& __cap() const _NOEXCEPT
2023 {return __cap_alloc_.first();}
2024 _LIBCPP_INLINE_VISIBILITY
2025 __storage_allocator& __alloc() _NOEXCEPT
2026 {return __cap_alloc_.second();}
2027 _LIBCPP_INLINE_VISIBILITY
2028 const __storage_allocator& __alloc() const _NOEXCEPT
2029 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002030
2031 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2032
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002033 _LIBCPP_INLINE_VISIBILITY
2034 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002036 _LIBCPP_INLINE_VISIBILITY
2037 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002038 {return (__n - 1) / __bits_per_word + 1;}
2039
2040public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002041 _LIBCPP_INLINE_VISIBILITY
2042 vector()
2043 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002044 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002045 ~vector();
2046 explicit vector(size_type __n);
2047 vector(size_type __n, const value_type& __v);
2048 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2049 template <class _InputIterator>
2050 vector(_InputIterator __first, _InputIterator __last,
2051 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2052 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2053 template <class _InputIterator>
2054 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2055 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2056 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2057 template <class _ForwardIterator>
2058 vector(_ForwardIterator __first, _ForwardIterator __last,
2059 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2060 template <class _ForwardIterator>
2061 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2062 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2063
2064 vector(const vector& __v);
2065 vector(const vector& __v, const allocator_type& __a);
2066 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00002067#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002068 vector(initializer_list<value_type> __il);
2069 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00002070#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002071
Howard Hinnant73d21a42010-09-04 23:28:19 +00002072#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002073 _LIBCPP_INLINE_VISIBILITY
2074 vector(vector&& __v)
2075 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002076 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002077 _LIBCPP_INLINE_VISIBILITY
2078 vector& operator=(vector&& __v)
2079 _NOEXCEPT_(
2080 __alloc_traits::propagate_on_container_move_assignment::value &&
2081 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002082#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +00002083#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002084 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002085 vector& operator=(initializer_list<value_type> __il)
2086 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00002087#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002088
2089 template <class _InputIterator>
2090 typename enable_if
2091 <
2092 __is_input_iterator<_InputIterator>::value &&
2093 !__is_forward_iterator<_InputIterator>::value,
2094 void
2095 >::type
2096 assign(_InputIterator __first, _InputIterator __last);
2097 template <class _ForwardIterator>
2098 typename enable_if
2099 <
2100 __is_forward_iterator<_ForwardIterator>::value,
2101 void
2102 >::type
2103 assign(_ForwardIterator __first, _ForwardIterator __last);
2104
2105 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +00002106#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002107 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002108 void assign(initializer_list<value_type> __il)
2109 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002110#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002111
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002112 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002113 {return allocator_type(this->__alloc());}
2114
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002115 size_type max_size() const _NOEXCEPT;
2116 _LIBCPP_INLINE_VISIBILITY
2117 size_type capacity() const _NOEXCEPT
2118 {return __internal_cap_to_external(__cap());}
2119 _LIBCPP_INLINE_VISIBILITY
2120 size_type size() const _NOEXCEPT
2121 {return __size_;}
2122 _LIBCPP_INLINE_VISIBILITY
2123 bool empty() const _NOEXCEPT
2124 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002125 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002126 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002127
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002128 _LIBCPP_INLINE_VISIBILITY
2129 iterator begin() _NOEXCEPT
2130 {return __make_iter(0);}
2131 _LIBCPP_INLINE_VISIBILITY
2132 const_iterator begin() const _NOEXCEPT
2133 {return __make_iter(0);}
2134 _LIBCPP_INLINE_VISIBILITY
2135 iterator end() _NOEXCEPT
2136 {return __make_iter(__size_);}
2137 _LIBCPP_INLINE_VISIBILITY
2138 const_iterator end() const _NOEXCEPT
2139 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002140
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002141 _LIBCPP_INLINE_VISIBILITY
2142 reverse_iterator rbegin() _NOEXCEPT
2143 {return reverse_iterator(end());}
2144 _LIBCPP_INLINE_VISIBILITY
2145 const_reverse_iterator rbegin() const _NOEXCEPT
2146 {return const_reverse_iterator(end());}
2147 _LIBCPP_INLINE_VISIBILITY
2148 reverse_iterator rend() _NOEXCEPT
2149 {return reverse_iterator(begin());}
2150 _LIBCPP_INLINE_VISIBILITY
2151 const_reverse_iterator rend() const _NOEXCEPT
2152 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002153
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002154 _LIBCPP_INLINE_VISIBILITY
2155 const_iterator cbegin() const _NOEXCEPT
2156 {return __make_iter(0);}
2157 _LIBCPP_INLINE_VISIBILITY
2158 const_iterator cend() const _NOEXCEPT
2159 {return __make_iter(__size_);}
2160 _LIBCPP_INLINE_VISIBILITY
2161 const_reverse_iterator crbegin() const _NOEXCEPT
2162 {return rbegin();}
2163 _LIBCPP_INLINE_VISIBILITY
2164 const_reverse_iterator crend() const _NOEXCEPT
2165 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002166
2167 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2168 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2169 reference at(size_type __n);
2170 const_reference at(size_type __n) const;
2171
2172 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2173 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2174 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2175 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2176
2177 void push_back(const value_type& __x);
2178 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2179
2180 iterator insert(const_iterator __position, const value_type& __x);
2181 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2182 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2183 template <class _InputIterator>
2184 typename enable_if
2185 <
2186 __is_input_iterator <_InputIterator>::value &&
2187 !__is_forward_iterator<_InputIterator>::value,
2188 iterator
2189 >::type
2190 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2191 template <class _ForwardIterator>
2192 typename enable_if
2193 <
2194 __is_forward_iterator<_ForwardIterator>::value,
2195 iterator
2196 >::type
2197 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00002198#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002200 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2201 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002202#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002203
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002204 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002205 iterator erase(const_iterator __first, const_iterator __last);
2206
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002207 _LIBCPP_INLINE_VISIBILITY
2208 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002209
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002210 void swap(vector&)
2211 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2212 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002213
2214 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002215 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002216
2217 bool __invariants() const;
2218
2219private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002220 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002221 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002222 void deallocate() _NOEXCEPT;
2223 _LIBCPP_INLINE_VISIBILITY
2224 static size_type __align(size_type __new_size) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002225 {return __new_size + (__bits_per_word-1) & ~(__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002226 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2227 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002228 template <class _ForwardIterator>
2229 typename enable_if
2230 <
2231 __is_forward_iterator<_ForwardIterator>::value,
2232 void
2233 >::type
2234 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2235 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002236 _LIBCPP_INLINE_VISIBILITY
2237 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002238 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002239 _LIBCPP_INLINE_VISIBILITY
2240 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002241 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
2242#ifdef _LIBCPP_DEBUG
2243 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_type __pos)
2244 {return iterator(this, pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2245 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_type __pos) const
2246 {return const_iterator(this, const_pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2247 _LIBCPP_INLINE_VISIBILITY iterator __const_iterator_cast(const_iterator __p)
2248 {return iterator(this, pointer(const_cast<__storage_pointer>(__p.base().__seg_), __p.base().__ctz_));}
Howard Hinnant324bb032010-08-22 00:02:43 +00002249#else // _LIBCPP_DEBUG
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002250 _LIBCPP_INLINE_VISIBILITY
2251 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002252 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002253 _LIBCPP_INLINE_VISIBILITY
2254 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002255 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002256 _LIBCPP_INLINE_VISIBILITY
2257 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002258 {return iterator(const_cast<__storage_pointer>(__p.__seg_), __p.__ctz_);}
Howard Hinnant324bb032010-08-22 00:02:43 +00002259#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002260
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002262 void __copy_assign_alloc(const vector& __v)
2263 {__copy_assign_alloc(__v, integral_constant<bool,
2264 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002266 void __copy_assign_alloc(const vector& __c, true_type)
2267 {
2268 if (__alloc() != __c.__alloc())
2269 deallocate();
2270 __alloc() = __c.__alloc();
2271 }
2272
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002274 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002275 {}
2276
2277 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002278 void __move_assign(vector& __c, true_type)
2279 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002280 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002281 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002282 _NOEXCEPT_(
2283 !__storage_traits::propagate_on_container_move_assignment::value ||
2284 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002285 {__move_assign_alloc(__c, integral_constant<bool,
2286 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00002288 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002289 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002290 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002291 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002292 }
2293
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002294 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002295 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002296 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002297 {}
2298
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002300 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002301 _NOEXCEPT_(
2302 !__storage_traits::propagate_on_container_swap::value ||
2303 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002304 {__swap_alloc(__x, __y, integral_constant<bool,
2305 __storage_traits::propagate_on_container_swap::value>());}
2306
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002308 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002309 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002310 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002311 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002312 swap(__x, __y);
2313 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002314 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002315 static void __swap_alloc(__storage_allocator&, __storage_allocator&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002316 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002317 {}
2318
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002319 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002320
2321 friend class __bit_reference<vector>;
2322 friend class __bit_const_reference<vector>;
2323 friend class __bit_iterator<vector, false>;
2324 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:18 +00002325 friend struct __bit_array<vector>;
Howard Hinnant83eade62013-03-06 23:30:19 +00002326 friend struct _LIBCPP_TYPE_VIS hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002327};
2328
2329template <class _Allocator>
2330#ifndef _LIBCPP_DEBUG
2331_LIBCPP_INLINE_VISIBILITY inline
2332#endif
2333void
2334vector<bool, _Allocator>::__invalidate_all_iterators()
2335{
2336#ifdef _LIBCPP_DEBUG
2337 iterator::__remove_all(this);
2338 const_iterator::__remove_all(this);
Howard Hinnant324bb032010-08-22 00:02:43 +00002339#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002340}
2341
2342// Allocate space for __n objects
2343// throws length_error if __n > max_size()
2344// throws (probably bad_alloc) if memory run out
2345// Precondition: __begin_ == __end_ == __cap() == 0
2346// Precondition: __n > 0
2347// Postcondition: capacity() == __n
2348// Postcondition: size() == 0
2349template <class _Allocator>
2350void
2351vector<bool, _Allocator>::allocate(size_type __n)
2352{
2353 if (__n > max_size())
2354 this->__throw_length_error();
2355 __n = __external_cap_to_internal(__n);
2356 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2357 this->__size_ = 0;
2358 this->__cap() = __n;
2359}
2360
2361template <class _Allocator>
2362void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002363vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002364{
2365 if (this->__begin_ != 0)
2366 {
2367 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2368 __invalidate_all_iterators();
2369 this->__begin_ = 0;
2370 this->__size_ = this->__cap() = 0;
2371 }
2372}
2373
2374template <class _Allocator>
2375typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002376vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002377{
2378 size_type __amax = __storage_traits::max_size(__alloc());
2379 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2380 if (__nmax / __bits_per_word <= __amax)
2381 return __nmax;
2382 return __internal_cap_to_external(__amax);
2383}
2384
2385// Precondition: __new_size > capacity()
2386template <class _Allocator>
2387_LIBCPP_INLINE_VISIBILITY inline
2388typename vector<bool, _Allocator>::size_type
2389vector<bool, _Allocator>::__recommend(size_type __new_size) const
2390{
2391 const size_type __ms = max_size();
2392 if (__new_size > __ms)
2393 this->__throw_length_error();
2394 const size_type __cap = capacity();
2395 if (__cap >= __ms / 2)
2396 return __ms;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002397 return _VSTD::max(2*__cap, __align(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002398}
2399
2400// Default constructs __n objects starting at __end_
2401// Precondition: __n > 0
2402// Precondition: size() + __n <= capacity()
2403// Postcondition: size() == size() + __n
2404template <class _Allocator>
2405_LIBCPP_INLINE_VISIBILITY inline
2406void
2407vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2408{
2409 size_type __old_size = this->__size_;
2410 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002411 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002412}
2413
2414template <class _Allocator>
2415template <class _ForwardIterator>
2416typename enable_if
2417<
2418 __is_forward_iterator<_ForwardIterator>::value,
2419 void
2420>::type
2421vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2422{
2423 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002424 this->__size_ += _VSTD::distance(__first, __last);
2425 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002426}
2427
2428template <class _Allocator>
2429_LIBCPP_INLINE_VISIBILITY inline
2430vector<bool, _Allocator>::vector()
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002431 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002432 : __begin_(0),
2433 __size_(0),
2434 __cap_alloc_(0)
2435{
2436}
2437
2438template <class _Allocator>
2439_LIBCPP_INLINE_VISIBILITY inline
2440vector<bool, _Allocator>::vector(const allocator_type& __a)
2441 : __begin_(0),
2442 __size_(0),
2443 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2444{
2445}
2446
2447template <class _Allocator>
2448vector<bool, _Allocator>::vector(size_type __n)
2449 : __begin_(0),
2450 __size_(0),
2451 __cap_alloc_(0)
2452{
2453 if (__n > 0)
2454 {
2455 allocate(__n);
2456 __construct_at_end(__n, false);
2457 }
2458}
2459
2460template <class _Allocator>
2461vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
2462 : __begin_(0),
2463 __size_(0),
2464 __cap_alloc_(0)
2465{
2466 if (__n > 0)
2467 {
2468 allocate(__n);
2469 __construct_at_end(__n, __x);
2470 }
2471}
2472
2473template <class _Allocator>
2474vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
2475 : __begin_(0),
2476 __size_(0),
2477 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2478{
2479 if (__n > 0)
2480 {
2481 allocate(__n);
2482 __construct_at_end(__n, __x);
2483 }
2484}
2485
2486template <class _Allocator>
2487template <class _InputIterator>
2488vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2489 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2490 !__is_forward_iterator<_InputIterator>::value>::type*)
2491 : __begin_(0),
2492 __size_(0),
2493 __cap_alloc_(0)
2494{
2495#ifndef _LIBCPP_NO_EXCEPTIONS
2496 try
2497 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002498#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002499 for (; __first != __last; ++__first)
2500 push_back(*__first);
2501#ifndef _LIBCPP_NO_EXCEPTIONS
2502 }
2503 catch (...)
2504 {
2505 if (__begin_ != 0)
2506 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2507 __invalidate_all_iterators();
2508 throw;
2509 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002510#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002511}
2512
2513template <class _Allocator>
2514template <class _InputIterator>
2515vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2516 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2517 !__is_forward_iterator<_InputIterator>::value>::type*)
2518 : __begin_(0),
2519 __size_(0),
2520 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2521{
2522#ifndef _LIBCPP_NO_EXCEPTIONS
2523 try
2524 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002525#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002526 for (; __first != __last; ++__first)
2527 push_back(*__first);
2528#ifndef _LIBCPP_NO_EXCEPTIONS
2529 }
2530 catch (...)
2531 {
2532 if (__begin_ != 0)
2533 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2534 __invalidate_all_iterators();
2535 throw;
2536 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002537#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002538}
2539
2540template <class _Allocator>
2541template <class _ForwardIterator>
2542vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2543 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2544 : __begin_(0),
2545 __size_(0),
2546 __cap_alloc_(0)
2547{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002548 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002549 if (__n > 0)
2550 {
2551 allocate(__n);
2552 __construct_at_end(__first, __last);
2553 }
2554}
2555
2556template <class _Allocator>
2557template <class _ForwardIterator>
2558vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2559 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2560 : __begin_(0),
2561 __size_(0),
2562 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2563{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002564 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002565 if (__n > 0)
2566 {
2567 allocate(__n);
2568 __construct_at_end(__first, __last);
2569 }
2570}
2571
Howard Hinnante3e32912011-08-12 21:56:02 +00002572#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2573
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002574template <class _Allocator>
2575vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
2576 : __begin_(0),
2577 __size_(0),
2578 __cap_alloc_(0)
2579{
2580 size_type __n = static_cast<size_type>(__il.size());
2581 if (__n > 0)
2582 {
2583 allocate(__n);
2584 __construct_at_end(__il.begin(), __il.end());
2585 }
2586}
2587
2588template <class _Allocator>
2589vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
2590 : __begin_(0),
2591 __size_(0),
2592 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2593{
2594 size_type __n = static_cast<size_type>(__il.size());
2595 if (__n > 0)
2596 {
2597 allocate(__n);
2598 __construct_at_end(__il.begin(), __il.end());
2599 }
2600}
2601
Howard Hinnante3e32912011-08-12 21:56:02 +00002602#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2603
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002604template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002605vector<bool, _Allocator>::~vector()
2606{
2607 if (__begin_ != 0)
2608 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2609#ifdef _LIBCPP_DEBUG
2610 __invalidate_all_iterators();
2611#endif
2612}
2613
2614template <class _Allocator>
2615vector<bool, _Allocator>::vector(const vector& __v)
2616 : __begin_(0),
2617 __size_(0),
2618 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2619{
2620 if (__v.size() > 0)
2621 {
2622 allocate(__v.size());
2623 __construct_at_end(__v.begin(), __v.end());
2624 }
2625}
2626
2627template <class _Allocator>
2628vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
2629 : __begin_(0),
2630 __size_(0),
2631 __cap_alloc_(0, __a)
2632{
2633 if (__v.size() > 0)
2634 {
2635 allocate(__v.size());
2636 __construct_at_end(__v.begin(), __v.end());
2637 }
2638}
2639
2640template <class _Allocator>
2641vector<bool, _Allocator>&
2642vector<bool, _Allocator>::operator=(const vector& __v)
2643{
2644 if (this != &__v)
2645 {
2646 __copy_assign_alloc(__v);
2647 if (__v.__size_)
2648 {
2649 if (__v.__size_ > capacity())
2650 {
2651 deallocate();
2652 allocate(__v.__size_);
2653 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002654 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002655 }
2656 __size_ = __v.__size_;
2657 }
2658 return *this;
2659}
2660
Howard Hinnant73d21a42010-09-04 23:28:19 +00002661#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2662
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002663template <class _Allocator>
2664_LIBCPP_INLINE_VISIBILITY inline
2665vector<bool, _Allocator>::vector(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002666 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002667 : __begin_(__v.__begin_),
2668 __size_(__v.__size_),
2669 __cap_alloc_(__v.__cap_alloc_)
2670{
2671 __v.__begin_ = 0;
2672 __v.__size_ = 0;
2673 __v.__cap() = 0;
2674}
2675
2676template <class _Allocator>
2677vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
2678 : __begin_(0),
2679 __size_(0),
2680 __cap_alloc_(0, __a)
2681{
2682 if (__a == allocator_type(__v.__alloc()))
2683 {
2684 this->__begin_ = __v.__begin_;
2685 this->__size_ = __v.__size_;
2686 this->__cap() = __v.__cap();
2687 __v.__begin_ = nullptr;
2688 __v.__cap() = __v.__size_ = 0;
2689 }
2690 else if (__v.size() > 0)
2691 {
2692 allocate(__v.size());
2693 __construct_at_end(__v.begin(), __v.end());
2694 }
2695}
2696
2697template <class _Allocator>
2698_LIBCPP_INLINE_VISIBILITY inline
2699vector<bool, _Allocator>&
2700vector<bool, _Allocator>::operator=(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002701 _NOEXCEPT_(
2702 __alloc_traits::propagate_on_container_move_assignment::value &&
2703 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002704{
2705 __move_assign(__v, integral_constant<bool,
2706 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002707 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002708}
2709
2710template <class _Allocator>
2711void
2712vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2713{
2714 if (__alloc() != __c.__alloc())
2715 assign(__c.begin(), __c.end());
2716 else
2717 __move_assign(__c, true_type());
2718}
2719
2720template <class _Allocator>
2721void
2722vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002723 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002724{
2725 deallocate();
2726 this->__begin_ = __c.__begin_;
2727 this->__size_ = __c.__size_;
2728 this->__cap() = __c.__cap();
2729 __move_assign_alloc(__c);
2730 __c.__begin_ = nullptr;
2731 __c.__cap() = __c.__size_ = 0;
2732}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002733
2734#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002735
2736template <class _Allocator>
2737void
2738vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2739{
2740 __size_ = 0;
2741 if (__n > 0)
2742 {
2743 size_type __c = capacity();
2744 if (__n <= __c)
2745 __size_ = __n;
2746 else
2747 {
2748 vector __v(__alloc());
2749 __v.reserve(__recommend(__n));
2750 __v.__size_ = __n;
2751 swap(__v);
2752 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002753 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002754 }
2755}
2756
2757template <class _Allocator>
2758template <class _InputIterator>
2759typename enable_if
2760<
2761 __is_input_iterator<_InputIterator>::value &&
2762 !__is_forward_iterator<_InputIterator>::value,
2763 void
2764>::type
2765vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2766{
2767 clear();
2768 for (; __first != __last; ++__first)
2769 push_back(*__first);
2770}
2771
2772template <class _Allocator>
2773template <class _ForwardIterator>
2774typename enable_if
2775<
2776 __is_forward_iterator<_ForwardIterator>::value,
2777 void
2778>::type
2779vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2780{
2781 clear();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002782 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002783 if (__n)
2784 {
2785 if (__n > capacity())
2786 {
2787 deallocate();
2788 allocate(__n);
2789 }
2790 __construct_at_end(__first, __last);
2791 }
2792}
2793
2794template <class _Allocator>
2795void
2796vector<bool, _Allocator>::reserve(size_type __n)
2797{
2798 if (__n > capacity())
2799 {
2800 vector __v(this->__alloc());
2801 __v.allocate(__n);
2802 __v.__construct_at_end(this->begin(), this->end());
2803 swap(__v);
2804 __invalidate_all_iterators();
2805 }
2806}
2807
2808template <class _Allocator>
2809void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002810vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002811{
2812 if (__external_cap_to_internal(size()) > __cap())
2813 {
2814#ifndef _LIBCPP_NO_EXCEPTIONS
2815 try
2816 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002817#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002818 vector(*this, allocator_type(__alloc())).swap(*this);
2819#ifndef _LIBCPP_NO_EXCEPTIONS
2820 }
2821 catch (...)
2822 {
2823 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002824#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002825 }
2826}
2827
2828template <class _Allocator>
2829typename vector<bool, _Allocator>::reference
2830vector<bool, _Allocator>::at(size_type __n)
2831{
2832 if (__n >= size())
2833 this->__throw_out_of_range();
2834 return (*this)[__n];
2835}
2836
2837template <class _Allocator>
2838typename vector<bool, _Allocator>::const_reference
2839vector<bool, _Allocator>::at(size_type __n) const
2840{
2841 if (__n >= size())
2842 this->__throw_out_of_range();
2843 return (*this)[__n];
2844}
2845
2846template <class _Allocator>
2847void
2848vector<bool, _Allocator>::push_back(const value_type& __x)
2849{
2850 if (this->__size_ == this->capacity())
2851 reserve(__recommend(this->__size_ + 1));
2852 ++this->__size_;
2853 back() = __x;
2854}
2855
2856template <class _Allocator>
2857typename vector<bool, _Allocator>::iterator
2858vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2859{
2860 iterator __r;
2861 if (size() < capacity())
2862 {
2863 const_iterator __old_end = end();
2864 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002865 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002866 __r = __const_iterator_cast(__position);
2867 }
2868 else
2869 {
2870 vector __v(__alloc());
2871 __v.reserve(__recommend(__size_ + 1));
2872 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002873 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2874 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002875 swap(__v);
2876 }
2877 *__r = __x;
2878 return __r;
2879}
2880
2881template <class _Allocator>
2882typename vector<bool, _Allocator>::iterator
2883vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
2884{
2885 iterator __r;
2886 size_type __c = capacity();
2887 if (__n <= __c && size() <= __c - __n)
2888 {
2889 const_iterator __old_end = end();
2890 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002891 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002892 __r = __const_iterator_cast(__position);
2893 }
2894 else
2895 {
2896 vector __v(__alloc());
2897 __v.reserve(__recommend(__size_ + __n));
2898 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002899 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2900 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002901 swap(__v);
2902 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002903 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002904 return __r;
2905}
2906
2907template <class _Allocator>
2908template <class _InputIterator>
2909typename enable_if
2910<
2911 __is_input_iterator <_InputIterator>::value &&
2912 !__is_forward_iterator<_InputIterator>::value,
2913 typename vector<bool, _Allocator>::iterator
2914>::type
2915vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
2916{
2917 difference_type __off = __position - begin();
2918 iterator __p = __const_iterator_cast(__position);
2919 iterator __old_end = end();
2920 for (; size() != capacity() && __first != __last; ++__first)
2921 {
2922 ++this->__size_;
2923 back() = *__first;
2924 }
2925 vector __v(__alloc());
2926 if (__first != __last)
2927 {
2928#ifndef _LIBCPP_NO_EXCEPTIONS
2929 try
2930 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002931#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002932 __v.assign(__first, __last);
2933 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
2934 difference_type __old_p = __p - begin();
2935 reserve(__recommend(size() + __v.size()));
2936 __p = begin() + __old_p;
2937 __old_end = begin() + __old_size;
2938#ifndef _LIBCPP_NO_EXCEPTIONS
2939 }
2940 catch (...)
2941 {
2942 erase(__old_end, end());
2943 throw;
2944 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002945#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002946 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002947 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002948 insert(__p, __v.begin(), __v.end());
2949 return begin() + __off;
2950}
2951
2952template <class _Allocator>
2953template <class _ForwardIterator>
2954typename enable_if
2955<
2956 __is_forward_iterator<_ForwardIterator>::value,
2957 typename vector<bool, _Allocator>::iterator
2958>::type
2959vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
2960{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002961 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002962 iterator __r;
2963 size_type __c = capacity();
2964 if (__n <= __c && size() <= __c - __n)
2965 {
2966 const_iterator __old_end = end();
2967 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002968 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002969 __r = __const_iterator_cast(__position);
2970 }
2971 else
2972 {
2973 vector __v(__alloc());
2974 __v.reserve(__recommend(__size_ + __n));
2975 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002976 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2977 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002978 swap(__v);
2979 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002980 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002981 return __r;
2982}
2983
2984template <class _Allocator>
2985_LIBCPP_INLINE_VISIBILITY inline
2986typename vector<bool, _Allocator>::iterator
2987vector<bool, _Allocator>::erase(const_iterator __position)
2988{
2989 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002990 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002991 --__size_;
2992 return __r;
2993}
2994
2995template <class _Allocator>
2996typename vector<bool, _Allocator>::iterator
2997vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
2998{
2999 iterator __r = __const_iterator_cast(__first);
3000 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003001 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003002 __size_ -= __d;
3003 return __r;
3004}
3005
3006template <class _Allocator>
3007void
3008vector<bool, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003009 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3010 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003011{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003012 _VSTD::swap(this->__begin_, __x.__begin_);
3013 _VSTD::swap(this->__size_, __x.__size_);
3014 _VSTD::swap(this->__cap(), __x.__cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003015 __swap_alloc(this->__alloc(), __x.__alloc());
3016#ifdef _LIBCPP_DEBUG
3017 iterator::swap(this, &__x);
3018 const_iterator::swap(this, &__x);
Howard Hinnant324bb032010-08-22 00:02:43 +00003019#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003020}
3021
Howard Hinnant324bb032010-08-22 00:02:43 +00003022template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003023void
3024vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3025{
3026 size_type __cs = size();
3027 if (__cs < __sz)
3028 {
3029 iterator __r;
3030 size_type __c = capacity();
3031 size_type __n = __sz - __cs;
3032 if (__n <= __c && __cs <= __c - __n)
3033 {
3034 __r = end();
3035 __size_ += __n;
3036 }
3037 else
3038 {
3039 vector __v(__alloc());
3040 __v.reserve(__recommend(__size_ + __n));
3041 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003042 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003043 swap(__v);
3044 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003045 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003046 }
3047 else
3048 __size_ = __sz;
3049}
3050
Howard Hinnant324bb032010-08-22 00:02:43 +00003051template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003052void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003053vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003054{
3055 // do middle whole words
3056 size_type __n = __size_;
3057 __storage_pointer __p = __begin_;
3058 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3059 *__p = ~*__p;
3060 // do last partial word
3061 if (__n > 0)
3062 {
3063 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3064 __storage_type __b = *__p & __m;
3065 *__p &= ~__m;
3066 *__p |= ~__b & __m;
3067 }
3068}
3069
Howard Hinnant324bb032010-08-22 00:02:43 +00003070template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003071bool
3072vector<bool, _Allocator>::__invariants() const
3073{
3074 if (this->__begin_ == 0)
3075 {
3076 if (this->__size_ != 0 || this->__cap() != 0)
3077 return false;
3078 }
3079 else
3080 {
3081 if (this->__cap() == 0)
3082 return false;
3083 if (this->__size_ > this->capacity())
3084 return false;
3085 }
3086 return true;
3087}
3088
Howard Hinnant324bb032010-08-22 00:02:43 +00003089template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003090size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003091vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003092{
3093 size_t __h = 0;
3094 // do middle whole words
3095 size_type __n = __size_;
3096 __storage_pointer __p = __begin_;
3097 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3098 __h ^= *__p;
3099 // do last partial word
3100 if (__n > 0)
3101 {
3102 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3103 __h ^= *__p & __m;
3104 }
3105 return __h;
3106}
3107
3108template <class _Allocator>
Howard Hinnant83eade62013-03-06 23:30:19 +00003109struct _LIBCPP_TYPE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003110 : public unary_function<vector<bool, _Allocator>, size_t>
3111{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00003112 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003113 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003114 {return __vec.__hash_code();}
3115};
3116
3117template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003118_LIBCPP_INLINE_VISIBILITY inline
3119bool
3120operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3121{
3122 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003123 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003124}
3125
3126template <class _Tp, class _Allocator>
3127_LIBCPP_INLINE_VISIBILITY inline
3128bool
3129operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3130{
3131 return !(__x == __y);
3132}
3133
3134template <class _Tp, class _Allocator>
3135_LIBCPP_INLINE_VISIBILITY inline
3136bool
3137operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3138{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003139 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003140}
3141
3142template <class _Tp, class _Allocator>
3143_LIBCPP_INLINE_VISIBILITY inline
3144bool
3145operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3146{
3147 return __y < __x;
3148}
3149
3150template <class _Tp, class _Allocator>
3151_LIBCPP_INLINE_VISIBILITY inline
3152bool
3153operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3154{
3155 return !(__x < __y);
3156}
3157
3158template <class _Tp, class _Allocator>
3159_LIBCPP_INLINE_VISIBILITY inline
3160bool
3161operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3162{
3163 return !(__y < __x);
3164}
3165
3166template <class _Tp, class _Allocator>
3167_LIBCPP_INLINE_VISIBILITY inline
3168void
3169swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003170 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003171{
3172 __x.swap(__y);
3173}
3174
3175_LIBCPP_END_NAMESPACE_STD
3176
3177#endif // _LIBCPP_VECTOR