blob: a083bfe09df500e4de2b4cb45cc28274953c434c [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 Hinnant02d5e182013-03-26 19:04:56 +0000505 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
506 "Allocator::value_type must be same type as value_type");
507
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000508 _LIBCPP_INLINE_VISIBILITY
509 vector()
510 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant7a563db2011-09-14 18:33:51 +0000511 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000512#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000513 __get_db()->__insert_c(this);
514#endif
515 }
516 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
517 : __base(__a)
518 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000519#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000520 __get_db()->__insert_c(this);
521#endif
522 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000523 explicit vector(size_type __n);
524 vector(size_type __n, const_reference __x);
525 vector(size_type __n, const_reference __x, const allocator_type& __a);
526 template <class _InputIterator>
527 vector(_InputIterator __first, _InputIterator __last,
528 typename enable_if<__is_input_iterator <_InputIterator>::value &&
529 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
530 template <class _InputIterator>
531 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
532 typename enable_if<__is_input_iterator <_InputIterator>::value &&
533 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
534 template <class _ForwardIterator>
535 vector(_ForwardIterator __first, _ForwardIterator __last,
536 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
537 template <class _ForwardIterator>
538 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
539 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000540#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000542 vector(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000544 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000545#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantabe26282011-09-16 17:29:17 +0000546#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7a563db2011-09-14 18:33:51 +0000548 ~vector()
549 {
550 __get_db()->__erase_c(this);
551 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000552#endif
553
554 vector(const vector& __x);
555 vector(const vector& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000557 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000558#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000560 vector(vector&& __x)
561 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000563 vector(vector&& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000565 vector& operator=(vector&& __x)
566 _NOEXCEPT_(
567 __alloc_traits::propagate_on_container_move_assignment::value &&
568 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000569#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000570#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572 vector& operator=(initializer_list<value_type> __il)
573 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000574#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000575
576 template <class _InputIterator>
577 typename enable_if
578 <
579 __is_input_iterator <_InputIterator>::value &&
580 !__is_forward_iterator<_InputIterator>::value,
581 void
582 >::type
583 assign(_InputIterator __first, _InputIterator __last);
584 template <class _ForwardIterator>
585 typename enable_if
586 <
587 __is_forward_iterator<_ForwardIterator>::value,
588 void
589 >::type
590 assign(_ForwardIterator __first, _ForwardIterator __last);
591
592 void assign(size_type __n, const_reference __u);
Howard Hinnante3e32912011-08-12 21:56:02 +0000593#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000594 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000595 void assign(initializer_list<value_type> __il)
596 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000597#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000598
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000599 _LIBCPP_INLINE_VISIBILITY
600 allocator_type get_allocator() const _NOEXCEPT
601 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000602
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000603 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
604 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
605 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
606 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000607
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000608 _LIBCPP_INLINE_VISIBILITY
609 reverse_iterator rbegin() _NOEXCEPT
610 {return reverse_iterator(end());}
611 _LIBCPP_INLINE_VISIBILITY
612 const_reverse_iterator rbegin() const _NOEXCEPT
613 {return const_reverse_iterator(end());}
614 _LIBCPP_INLINE_VISIBILITY
615 reverse_iterator rend() _NOEXCEPT
616 {return reverse_iterator(begin());}
617 _LIBCPP_INLINE_VISIBILITY
618 const_reverse_iterator rend() const _NOEXCEPT
619 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000620
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000621 _LIBCPP_INLINE_VISIBILITY
622 const_iterator cbegin() const _NOEXCEPT
623 {return begin();}
624 _LIBCPP_INLINE_VISIBILITY
625 const_iterator cend() const _NOEXCEPT
626 {return end();}
627 _LIBCPP_INLINE_VISIBILITY
628 const_reverse_iterator crbegin() const _NOEXCEPT
629 {return rbegin();}
630 _LIBCPP_INLINE_VISIBILITY
631 const_reverse_iterator crend() const _NOEXCEPT
632 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000633
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000634 _LIBCPP_INLINE_VISIBILITY
635 size_type size() const _NOEXCEPT
636 {return static_cast<size_type>(this->__end_ - this->__begin_);}
637 _LIBCPP_INLINE_VISIBILITY
638 size_type capacity() const _NOEXCEPT
639 {return __base::capacity();}
640 _LIBCPP_INLINE_VISIBILITY
641 bool empty() const _NOEXCEPT
642 {return this->__begin_ == this->__end_;}
643 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000644 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000645 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000646
647 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
648 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
649 reference at(size_type __n);
650 const_reference at(size_type __n) const;
651
Howard Hinnant7a563db2011-09-14 18:33:51 +0000652 _LIBCPP_INLINE_VISIBILITY reference front()
653 {
654 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
655 return *this->__begin_;
656 }
657 _LIBCPP_INLINE_VISIBILITY const_reference front() const
658 {
659 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
660 return *this->__begin_;
661 }
662 _LIBCPP_INLINE_VISIBILITY reference back()
663 {
664 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
665 return *(this->__end_ - 1);
666 }
667 _LIBCPP_INLINE_VISIBILITY const_reference back() const
668 {
669 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
670 return *(this->__end_ - 1);
671 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000672
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000673 _LIBCPP_INLINE_VISIBILITY
674 value_type* data() _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000675 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000676 _LIBCPP_INLINE_VISIBILITY
677 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000678 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000679
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000680 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000681#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000682 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000683#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000684 template <class... _Args>
685 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000686#endif // _LIBCPP_HAS_NO_VARIADICS
687#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000688 void pop_back();
689
690 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000691#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000692 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000693#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000694 template <class... _Args>
695 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000696#endif // _LIBCPP_HAS_NO_VARIADICS
697#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000698 iterator insert(const_iterator __position, size_type __n, const_reference __x);
699 template <class _InputIterator>
700 typename enable_if
701 <
702 __is_input_iterator <_InputIterator>::value &&
703 !__is_forward_iterator<_InputIterator>::value,
704 iterator
705 >::type
706 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
707 template <class _ForwardIterator>
708 typename enable_if
709 <
710 __is_forward_iterator<_ForwardIterator>::value,
711 iterator
712 >::type
713 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +0000714#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000716 iterator insert(const_iterator __position, initializer_list<value_type> __il)
717 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000718#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000719
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000720 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000721 iterator erase(const_iterator __first, const_iterator __last);
722
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000723 _LIBCPP_INLINE_VISIBILITY
724 void clear() _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000725 {
726 __base::clear();
727 __invalidate_all_iterators();
728 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000729
730 void resize(size_type __sz);
731 void resize(size_type __sz, const_reference __x);
732
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000733 void swap(vector&)
734 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
735 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000736
737 bool __invariants() const;
738
Howard Hinnantabe26282011-09-16 17:29:17 +0000739#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000740
741 bool __dereferenceable(const const_iterator* __i) const;
742 bool __decrementable(const const_iterator* __i) const;
743 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
744 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
745
Howard Hinnantabe26282011-09-16 17:29:17 +0000746#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000747
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000748private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000749 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000750 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000751 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000752 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31 +0000753 void __construct_at_end(size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000754 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000755 template <class _ForwardIterator>
756 typename enable_if
757 <
758 __is_forward_iterator<_ForwardIterator>::value,
759 void
760 >::type
761 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
762 void __move_construct_at_end(pointer __first, pointer __last);
763 void __append(size_type __n);
764 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000765 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000766 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000768 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000769 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
770 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
771 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000772 void __move_assign(vector& __c, true_type)
773 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000774 void __move_assign(vector& __c, false_type);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000775 _LIBCPP_INLINE_VISIBILITY
776 void __destruct_at_end(const_pointer __new_last) _NOEXCEPT
777 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000778#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000779 __c_node* __c = __get_db()->__find_c_and_lock(this);
780 for (__i_node** __p = __c->end_; __p != __c->beg_; )
781 {
782 --__p;
783 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
784 if (__i->base() > __new_last)
785 {
786 (*__p)->__c_ = nullptr;
787 if (--__c->end_ != __p)
788 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
789 }
790 }
791 __get_db()->unlock();
792#endif
793 __base::__destruct_at_end(__new_last);
794 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000795 template <class _Up>
796 void
797#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
798 __push_back_slow_path(_Up&& __x);
799#else
800 __push_back_slow_path(_Up& __x);
801#endif
Howard Hinnant0438ea22012-02-26 15:30:12 +0000802#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
803 template <class... _Args>
804 void
805 __emplace_back_slow_path(_Args&&... __args);
806#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000807};
808
809template <class _Tp, class _Allocator>
810void
811vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
812{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000813 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000814 _VSTD::swap(this->__begin_, __v.__begin_);
815 _VSTD::swap(this->__end_, __v.__end_);
816 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000817 __v.__first_ = __v.__begin_;
818 __invalidate_all_iterators();
819}
820
821template <class _Tp, class _Allocator>
822typename vector<_Tp, _Allocator>::pointer
823vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
824{
825 pointer __r = __v.__begin_;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000826 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
827 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000828 _VSTD::swap(this->__begin_, __v.__begin_);
829 _VSTD::swap(this->__end_, __v.__end_);
830 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000831 __v.__first_ = __v.__begin_;
832 __invalidate_all_iterators();
833 return __r;
834}
835
836// Allocate space for __n objects
837// throws length_error if __n > max_size()
838// throws (probably bad_alloc) if memory run out
839// Precondition: __begin_ == __end_ == __end_cap() == 0
840// Precondition: __n > 0
841// Postcondition: capacity() == __n
842// Postcondition: size() == 0
843template <class _Tp, class _Allocator>
844void
845vector<_Tp, _Allocator>::allocate(size_type __n)
846{
847 if (__n > max_size())
848 this->__throw_length_error();
849 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
850 this->__end_cap() = this->__begin_ + __n;
851}
852
853template <class _Tp, class _Allocator>
854void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000855vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000856{
857 if (this->__begin_ != 0)
858 {
859 clear();
860 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000861 this->__begin_ = this->__end_ = this->__end_cap() = 0;
862 }
863}
864
865template <class _Tp, class _Allocator>
866typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000867vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000868{
Sean Hunt110b8bf2011-07-29 23:31:58 +0000869 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 +0000870}
871
872// Precondition: __new_size > capacity()
873template <class _Tp, class _Allocator>
874_LIBCPP_INLINE_VISIBILITY inline
875typename vector<_Tp, _Allocator>::size_type
876vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
877{
878 const size_type __ms = max_size();
879 if (__new_size > __ms)
880 this->__throw_length_error();
881 const size_type __cap = capacity();
882 if (__cap >= __ms / 2)
883 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58 +0000884 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000885}
886
887// Default constructs __n objects starting at __end_
888// throws if construction throws
889// Precondition: __n > 0
890// Precondition: size() + __n <= capacity()
891// Postcondition: size() == size() + __n
892template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000893void
894vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
895{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000896 allocator_type& __a = this->__alloc();
897 do
898 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000899 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000900 ++this->__end_;
901 --__n;
902 } while (__n > 0);
903}
904
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000905// Copy constructs __n objects starting at __end_ from __x
906// throws if construction throws
907// Precondition: __n > 0
908// Precondition: size() + __n <= capacity()
909// Postcondition: size() == old size() + __n
910// Postcondition: [i] == __x for all i in [size() - __n, __n)
911template <class _Tp, class _Allocator>
912_LIBCPP_INLINE_VISIBILITY inline
913void
914vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
915{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000916 allocator_type& __a = this->__alloc();
917 do
918 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000919 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000920 ++this->__end_;
921 --__n;
922 } while (__n > 0);
923}
924
925template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000926template <class _ForwardIterator>
927typename enable_if
928<
929 __is_forward_iterator<_ForwardIterator>::value,
930 void
931>::type
932vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
933{
934 allocator_type& __a = this->__alloc();
935 for (; __first != __last; ++__first)
936 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000937 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000938 ++this->__end_;
939 }
940}
941
942template <class _Tp, class _Allocator>
943void
944vector<_Tp, _Allocator>::__move_construct_at_end(pointer __first, pointer __last)
945{
946 allocator_type& __a = this->__alloc();
947 for (; __first != __last; ++__first)
948 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000949 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
950 _VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000951 ++this->__end_;
952 }
953}
954
955// Default constructs __n objects starting at __end_
956// throws if construction throws
957// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000958// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000959template <class _Tp, class _Allocator>
960void
961vector<_Tp, _Allocator>::__append(size_type __n)
962{
963 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
964 this->__construct_at_end(__n);
965 else
966 {
967 allocator_type& __a = this->__alloc();
968 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
969 __v.__construct_at_end(__n);
970 __swap_out_circular_buffer(__v);
971 }
972}
973
974// Default constructs __n objects starting at __end_
975// throws if construction throws
976// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000977// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000978template <class _Tp, class _Allocator>
979void
980vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
981{
982 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
983 this->__construct_at_end(__n, __x);
984 else
985 {
986 allocator_type& __a = this->__alloc();
987 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
988 __v.__construct_at_end(__n, __x);
989 __swap_out_circular_buffer(__v);
990 }
991}
992
993template <class _Tp, class _Allocator>
994vector<_Tp, _Allocator>::vector(size_type __n)
995{
Howard Hinnant0442b122011-09-16 18:41:29 +0000996#if _LIBCPP_DEBUG_LEVEL >= 2
997 __get_db()->__insert_c(this);
998#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000999 if (__n > 0)
1000 {
1001 allocate(__n);
1002 __construct_at_end(__n);
1003 }
1004}
1005
1006template <class _Tp, class _Allocator>
1007vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1008{
Howard Hinnant0442b122011-09-16 18:41:29 +00001009#if _LIBCPP_DEBUG_LEVEL >= 2
1010 __get_db()->__insert_c(this);
1011#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001012 if (__n > 0)
1013 {
1014 allocate(__n);
1015 __construct_at_end(__n, __x);
1016 }
1017}
1018
1019template <class _Tp, class _Allocator>
1020vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1021 : __base(__a)
1022{
Howard Hinnant0442b122011-09-16 18:41:29 +00001023#if _LIBCPP_DEBUG_LEVEL >= 2
1024 __get_db()->__insert_c(this);
1025#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001026 if (__n > 0)
1027 {
1028 allocate(__n);
1029 __construct_at_end(__n, __x);
1030 }
1031}
1032
1033template <class _Tp, class _Allocator>
1034template <class _InputIterator>
1035vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
1036 typename enable_if<__is_input_iterator <_InputIterator>::value &&
1037 !__is_forward_iterator<_InputIterator>::value>::type*)
1038{
Howard Hinnantabe26282011-09-16 17:29:17 +00001039#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001040 __get_db()->__insert_c(this);
1041#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001042 for (; __first != __last; ++__first)
1043 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001044}
1045
1046template <class _Tp, class _Allocator>
1047template <class _InputIterator>
1048vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1049 typename enable_if<__is_input_iterator <_InputIterator>::value &&
1050 !__is_forward_iterator<_InputIterator>::value>::type*)
1051 : __base(__a)
1052{
Howard Hinnantabe26282011-09-16 17:29:17 +00001053#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001054 __get_db()->__insert_c(this);
1055#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001056 for (; __first != __last; ++__first)
1057 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001058}
1059
1060template <class _Tp, class _Allocator>
1061template <class _ForwardIterator>
1062vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
1063 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
1064{
Howard Hinnant0442b122011-09-16 18:41:29 +00001065#if _LIBCPP_DEBUG_LEVEL >= 2
1066 __get_db()->__insert_c(this);
1067#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001068 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001069 if (__n > 0)
1070 {
1071 allocate(__n);
1072 __construct_at_end(__first, __last);
1073 }
1074}
1075
1076template <class _Tp, class _Allocator>
1077template <class _ForwardIterator>
1078vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
1079 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
1080 : __base(__a)
1081{
Howard Hinnant0442b122011-09-16 18:41:29 +00001082#if _LIBCPP_DEBUG_LEVEL >= 2
1083 __get_db()->__insert_c(this);
1084#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001085 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001086 if (__n > 0)
1087 {
1088 allocate(__n);
1089 __construct_at_end(__first, __last);
1090 }
1091}
1092
1093template <class _Tp, class _Allocator>
1094vector<_Tp, _Allocator>::vector(const vector& __x)
1095 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1096{
Howard Hinnant0442b122011-09-16 18:41:29 +00001097#if _LIBCPP_DEBUG_LEVEL >= 2
1098 __get_db()->__insert_c(this);
1099#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001100 size_type __n = __x.size();
1101 if (__n > 0)
1102 {
1103 allocate(__n);
1104 __construct_at_end(__x.__begin_, __x.__end_);
1105 }
1106}
1107
1108template <class _Tp, class _Allocator>
1109vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1110 : __base(__a)
1111{
Howard Hinnant0442b122011-09-16 18:41:29 +00001112#if _LIBCPP_DEBUG_LEVEL >= 2
1113 __get_db()->__insert_c(this);
1114#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001115 size_type __n = __x.size();
1116 if (__n > 0)
1117 {
1118 allocate(__n);
1119 __construct_at_end(__x.__begin_, __x.__end_);
1120 }
1121}
1122
Howard Hinnant73d21a42010-09-04 23:28:19 +00001123#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001124
1125template <class _Tp, class _Allocator>
1126_LIBCPP_INLINE_VISIBILITY inline
1127vector<_Tp, _Allocator>::vector(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001128 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001129 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001130{
Howard Hinnantabe26282011-09-16 17:29:17 +00001131#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001132 __get_db()->__insert_c(this);
Howard Hinnante6125bd2011-09-19 16:34:29 +00001133 __get_db()->swap(this, &__x);
Howard Hinnant7a563db2011-09-14 18:33:51 +00001134#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001135 this->__begin_ = __x.__begin_;
1136 this->__end_ = __x.__end_;
1137 this->__end_cap() = __x.__end_cap();
1138 __x.__begin_ = __x.__end_ = __x.__end_cap() = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001139}
1140
1141template <class _Tp, class _Allocator>
1142_LIBCPP_INLINE_VISIBILITY inline
1143vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1144 : __base(__a)
1145{
Howard Hinnant0442b122011-09-16 18:41:29 +00001146#if _LIBCPP_DEBUG_LEVEL >= 2
1147 __get_db()->__insert_c(this);
1148#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001149 if (__a == __x.__alloc())
1150 {
1151 this->__begin_ = __x.__begin_;
1152 this->__end_ = __x.__end_;
1153 this->__end_cap() = __x.__end_cap();
1154 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001155#if _LIBCPP_DEBUG_LEVEL >= 2
1156 __get_db()->swap(this, &__x);
1157#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001158 }
1159 else
1160 {
Howard Hinnant99968442011-11-29 18:15:50 +00001161 typedef move_iterator<iterator> _Ip;
1162 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001163 }
1164}
1165
Howard Hinnante3e32912011-08-12 21:56:02 +00001166#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1167
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001168template <class _Tp, class _Allocator>
1169_LIBCPP_INLINE_VISIBILITY inline
1170vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1171{
Howard Hinnant0442b122011-09-16 18:41:29 +00001172#if _LIBCPP_DEBUG_LEVEL >= 2
1173 __get_db()->__insert_c(this);
1174#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001175 if (__il.size() > 0)
1176 {
1177 allocate(__il.size());
1178 __construct_at_end(__il.begin(), __il.end());
1179 }
1180}
1181
1182template <class _Tp, class _Allocator>
1183_LIBCPP_INLINE_VISIBILITY inline
1184vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1185 : __base(__a)
1186{
Howard Hinnant0442b122011-09-16 18:41:29 +00001187#if _LIBCPP_DEBUG_LEVEL >= 2
1188 __get_db()->__insert_c(this);
1189#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001190 if (__il.size() > 0)
1191 {
1192 allocate(__il.size());
1193 __construct_at_end(__il.begin(), __il.end());
1194 }
1195}
1196
Howard Hinnante3e32912011-08-12 21:56:02 +00001197#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1198
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001199template <class _Tp, class _Allocator>
1200_LIBCPP_INLINE_VISIBILITY inline
1201vector<_Tp, _Allocator>&
1202vector<_Tp, _Allocator>::operator=(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001203 _NOEXCEPT_(
1204 __alloc_traits::propagate_on_container_move_assignment::value &&
1205 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001206{
1207 __move_assign(__x, integral_constant<bool,
1208 __alloc_traits::propagate_on_container_move_assignment::value>());
1209 return *this;
1210}
1211
1212template <class _Tp, class _Allocator>
1213void
1214vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1215{
1216 if (__base::__alloc() != __c.__alloc())
1217 {
Howard Hinnant99968442011-11-29 18:15:50 +00001218 typedef move_iterator<iterator> _Ip;
1219 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001220 }
1221 else
1222 __move_assign(__c, true_type());
1223}
1224
1225template <class _Tp, class _Allocator>
1226void
1227vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001228 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001229{
1230 deallocate();
1231 this->__begin_ = __c.__begin_;
1232 this->__end_ = __c.__end_;
1233 this->__end_cap() = __c.__end_cap();
1234 __base::__move_assign_alloc(__c);
1235 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001236#if _LIBCPP_DEBUG_LEVEL >= 2
1237 __get_db()->swap(this, &__c);
1238#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001239}
1240
Howard Hinnant73d21a42010-09-04 23:28:19 +00001241#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001242
1243template <class _Tp, class _Allocator>
1244_LIBCPP_INLINE_VISIBILITY inline
1245vector<_Tp, _Allocator>&
1246vector<_Tp, _Allocator>::operator=(const vector& __x)
1247{
1248 if (this != &__x)
1249 {
1250 __base::__copy_assign_alloc(__x);
1251 assign(__x.__begin_, __x.__end_);
1252 }
1253 return *this;
1254}
1255
1256template <class _Tp, class _Allocator>
1257template <class _InputIterator>
1258typename enable_if
1259<
1260 __is_input_iterator <_InputIterator>::value &&
1261 !__is_forward_iterator<_InputIterator>::value,
1262 void
1263>::type
1264vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1265{
1266 clear();
1267 for (; __first != __last; ++__first)
1268 push_back(*__first);
1269}
1270
1271template <class _Tp, class _Allocator>
1272template <class _ForwardIterator>
1273typename enable_if
1274<
1275 __is_forward_iterator<_ForwardIterator>::value,
1276 void
1277>::type
1278vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1279{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001280 typename iterator_traits<_ForwardIterator>::difference_type __new_size = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001281 if (static_cast<size_type>(__new_size) <= capacity())
1282 {
1283 _ForwardIterator __mid = __last;
1284 bool __growing = false;
1285 if (static_cast<size_type>(__new_size) > size())
1286 {
1287 __growing = true;
1288 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001289 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001290 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001291 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001292 if (__growing)
1293 __construct_at_end(__mid, __last);
1294 else
1295 this->__destruct_at_end(__m);
1296 }
1297 else
1298 {
1299 deallocate();
1300 allocate(__recommend(static_cast<size_type>(__new_size)));
1301 __construct_at_end(__first, __last);
1302 }
1303}
1304
1305template <class _Tp, class _Allocator>
1306void
1307vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1308{
1309 if (__n <= capacity())
1310 {
1311 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001312 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001313 if (__n > __s)
1314 __construct_at_end(__n - __s, __u);
1315 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001316 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001317 }
1318 else
1319 {
1320 deallocate();
1321 allocate(__recommend(static_cast<size_type>(__n)));
1322 __construct_at_end(__n, __u);
1323 }
1324}
1325
Howard Hinnant324bb032010-08-22 00:02:43 +00001326template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001327_LIBCPP_INLINE_VISIBILITY inline
1328typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001329vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001330{
Howard Hinnantabe26282011-09-16 17:29:17 +00001331#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001332 return iterator(this, __p);
1333#else
1334 return iterator(__p);
1335#endif
1336}
1337
Howard Hinnant324bb032010-08-22 00:02:43 +00001338template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001339_LIBCPP_INLINE_VISIBILITY inline
1340typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001341vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001342{
Howard Hinnantabe26282011-09-16 17:29:17 +00001343#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001344 return const_iterator(this, __p);
1345#else
1346 return const_iterator(__p);
1347#endif
1348}
1349
Howard Hinnant324bb032010-08-22 00:02:43 +00001350template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001351_LIBCPP_INLINE_VISIBILITY inline
1352typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001353vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001354{
1355 return __make_iter(this->__begin_);
1356}
1357
Howard Hinnant324bb032010-08-22 00:02:43 +00001358template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001359_LIBCPP_INLINE_VISIBILITY inline
1360typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001361vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001362{
1363 return __make_iter(this->__begin_);
1364}
1365
Howard Hinnant324bb032010-08-22 00:02:43 +00001366template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001367_LIBCPP_INLINE_VISIBILITY inline
1368typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001369vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001370{
1371 return __make_iter(this->__end_);
1372}
1373
Howard Hinnant324bb032010-08-22 00:02:43 +00001374template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001375_LIBCPP_INLINE_VISIBILITY inline
1376typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001377vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001378{
1379 return __make_iter(this->__end_);
1380}
1381
Howard Hinnant324bb032010-08-22 00:02:43 +00001382template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001383_LIBCPP_INLINE_VISIBILITY inline
1384typename vector<_Tp, _Allocator>::reference
1385vector<_Tp, _Allocator>::operator[](size_type __n)
1386{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001387 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001388 return this->__begin_[__n];
1389}
1390
Howard Hinnant324bb032010-08-22 00:02:43 +00001391template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001392_LIBCPP_INLINE_VISIBILITY inline
1393typename vector<_Tp, _Allocator>::const_reference
1394vector<_Tp, _Allocator>::operator[](size_type __n) const
1395{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001396 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001397 return this->__begin_[__n];
1398}
1399
Howard Hinnant324bb032010-08-22 00:02:43 +00001400template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001401typename vector<_Tp, _Allocator>::reference
1402vector<_Tp, _Allocator>::at(size_type __n)
1403{
1404 if (__n >= size())
1405 this->__throw_out_of_range();
1406 return this->__begin_[__n];
1407}
1408
Howard Hinnant324bb032010-08-22 00:02:43 +00001409template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001410typename vector<_Tp, _Allocator>::const_reference
1411vector<_Tp, _Allocator>::at(size_type __n) const
1412{
1413 if (__n >= size())
1414 this->__throw_out_of_range();
1415 return this->__begin_[__n];
1416}
1417
1418template <class _Tp, class _Allocator>
1419void
1420vector<_Tp, _Allocator>::reserve(size_type __n)
1421{
1422 if (__n > capacity())
1423 {
1424 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001425 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001426 __swap_out_circular_buffer(__v);
1427 }
1428}
1429
1430template <class _Tp, class _Allocator>
1431void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001432vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001433{
1434 if (capacity() > size())
1435 {
1436#ifndef _LIBCPP_NO_EXCEPTIONS
1437 try
1438 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001439#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001440 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001441 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001442 __swap_out_circular_buffer(__v);
1443#ifndef _LIBCPP_NO_EXCEPTIONS
1444 }
1445 catch (...)
1446 {
1447 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001448#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001449 }
1450}
1451
1452template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001453template <class _Up>
1454void
1455#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1456vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1457#else
1458vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1459#endif
1460{
1461 allocator_type& __a = this->__alloc();
1462 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1463 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnantf619e232013-01-11 20:36:59 +00001464 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1465 __v.__end_++;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001466 __swap_out_circular_buffer(__v);
1467}
1468
1469template <class _Tp, class _Allocator>
1470_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001471void
1472vector<_Tp, _Allocator>::push_back(const_reference __x)
1473{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001474 if (this->__end_ != this->__end_cap())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001475 {
1476 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001477 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478 ++this->__end_;
1479 }
1480 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001481 __push_back_slow_path(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001482}
1483
Howard Hinnant73d21a42010-09-04 23:28:19 +00001484#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001485
1486template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001487_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001488void
1489vector<_Tp, _Allocator>::push_back(value_type&& __x)
1490{
1491 if (this->__end_ < this->__end_cap())
1492 {
1493 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001494 _VSTD::__to_raw_pointer(this->__end_),
1495 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001496 ++this->__end_;
1497 }
1498 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001499 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001500}
1501
Howard Hinnant73d21a42010-09-04 23:28:19 +00001502#ifndef _LIBCPP_HAS_NO_VARIADICS
1503
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504template <class _Tp, class _Allocator>
1505template <class... _Args>
1506void
Howard Hinnant0438ea22012-02-26 15:30:12 +00001507vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1508{
1509 allocator_type& __a = this->__alloc();
1510 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1511// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantf619e232013-01-11 20:36:59 +00001512 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1513 __v.__end_++;
Howard Hinnant0438ea22012-02-26 15:30:12 +00001514 __swap_out_circular_buffer(__v);
1515}
1516
1517template <class _Tp, class _Allocator>
1518template <class... _Args>
1519_LIBCPP_INLINE_VISIBILITY inline
1520void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001521vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1522{
1523 if (this->__end_ < this->__end_cap())
1524 {
1525 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001526 _VSTD::__to_raw_pointer(this->__end_),
1527 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001528 ++this->__end_;
1529 }
1530 else
Howard Hinnant0438ea22012-02-26 15:30:12 +00001531 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001532}
1533
Howard Hinnant73d21a42010-09-04 23:28:19 +00001534#endif // _LIBCPP_HAS_NO_VARIADICS
1535#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001536
1537template <class _Tp, class _Allocator>
1538_LIBCPP_INLINE_VISIBILITY inline
1539void
1540vector<_Tp, _Allocator>::pop_back()
1541{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001542 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001543 this->__destruct_at_end(this->__end_ - 1);
1544}
1545
1546template <class _Tp, class _Allocator>
1547_LIBCPP_INLINE_VISIBILITY inline
1548typename vector<_Tp, _Allocator>::iterator
1549vector<_Tp, _Allocator>::erase(const_iterator __position)
1550{
Howard Hinnantabe26282011-09-16 17:29:17 +00001551#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001552 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1553 "vector::erase(iterator) called with an iterator not"
1554 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001555#endif
Howard Hinnant782da332013-03-25 22:12:26 +00001556 _LIBCPP_ASSERT(__position != end(),
1557 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant635bbbb2013-02-07 15:31:44 +00001558 pointer __p = const_cast<pointer>(&*__position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001559 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001560 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001561 return __r;
1562}
1563
1564template <class _Tp, class _Allocator>
1565typename vector<_Tp, _Allocator>::iterator
1566vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1567{
Howard Hinnantabe26282011-09-16 17:29:17 +00001568#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001569 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1570 "vector::erase(iterator, iterator) called with an iterator not"
1571 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001572#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +00001573 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001574 pointer __p = this->__begin_ + (__first - begin());
1575 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001576 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001577 return __r;
1578}
1579
1580template <class _Tp, class _Allocator>
1581void
1582vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1583{
1584 pointer __old_last = this->__end_;
1585 difference_type __n = __old_last - __to;
1586 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1587 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001588 _VSTD::__to_raw_pointer(this->__end_),
1589 _VSTD::move(*__i));
1590 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001591}
1592
1593template <class _Tp, class _Allocator>
1594typename vector<_Tp, _Allocator>::iterator
1595vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1596{
Howard Hinnantabe26282011-09-16 17:29:17 +00001597#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001598 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1599 "vector::insert(iterator, x) called with an iterator not"
1600 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001601#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001602 pointer __p = this->__begin_ + (__position - begin());
1603 if (this->__end_ < this->__end_cap())
1604 {
1605 if (__p == this->__end_)
1606 {
1607 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001608 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001609 ++this->__end_;
1610 }
1611 else
1612 {
1613 __move_range(__p, this->__end_, __p + 1);
1614 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1615 if (__p <= __xr && __xr < this->__end_)
1616 ++__xr;
1617 *__p = *__xr;
1618 }
1619 }
1620 else
1621 {
1622 allocator_type& __a = this->__alloc();
1623 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1624 __v.push_back(__x);
1625 __p = __swap_out_circular_buffer(__v, __p);
1626 }
1627 return __make_iter(__p);
1628}
1629
Howard Hinnant73d21a42010-09-04 23:28:19 +00001630#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001631
1632template <class _Tp, class _Allocator>
1633typename vector<_Tp, _Allocator>::iterator
1634vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1635{
Howard Hinnantabe26282011-09-16 17:29:17 +00001636#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001637 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1638 "vector::insert(iterator, x) called with an iterator not"
1639 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001640#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001641 pointer __p = this->__begin_ + (__position - begin());
1642 if (this->__end_ < this->__end_cap())
1643 {
1644 if (__p == this->__end_)
1645 {
1646 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001647 _VSTD::__to_raw_pointer(this->__end_),
1648 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001649 ++this->__end_;
1650 }
1651 else
1652 {
1653 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001654 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655 }
1656 }
1657 else
1658 {
1659 allocator_type& __a = this->__alloc();
1660 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001661 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001662 __p = __swap_out_circular_buffer(__v, __p);
1663 }
1664 return __make_iter(__p);
1665}
1666
Howard Hinnant73d21a42010-09-04 23:28:19 +00001667#ifndef _LIBCPP_HAS_NO_VARIADICS
1668
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001669template <class _Tp, class _Allocator>
1670template <class... _Args>
1671typename vector<_Tp, _Allocator>::iterator
1672vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1673{
Howard Hinnantabe26282011-09-16 17:29:17 +00001674#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001675 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1676 "vector::emplace(iterator, x) called with an iterator not"
1677 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001678#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001679 pointer __p = this->__begin_ + (__position - begin());
1680 if (this->__end_ < this->__end_cap())
1681 {
1682 if (__p == this->__end_)
1683 {
1684 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001685 _VSTD::__to_raw_pointer(this->__end_),
1686 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001687 ++this->__end_;
1688 }
1689 else
1690 {
Howard Hinnanta58402a2012-07-08 23:23:04 +00001691 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001692 __move_range(__p, this->__end_, __p + 1);
Howard Hinnanta58402a2012-07-08 23:23:04 +00001693 *__p = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001694 }
1695 }
1696 else
1697 {
1698 allocator_type& __a = this->__alloc();
1699 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001700 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001701 __p = __swap_out_circular_buffer(__v, __p);
1702 }
1703 return __make_iter(__p);
1704}
1705
Howard Hinnant73d21a42010-09-04 23:28:19 +00001706#endif // _LIBCPP_HAS_NO_VARIADICS
1707#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001708
1709template <class _Tp, class _Allocator>
1710typename vector<_Tp, _Allocator>::iterator
1711vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1712{
Howard Hinnantabe26282011-09-16 17:29:17 +00001713#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001714 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1715 "vector::insert(iterator, n, x) called with an iterator not"
1716 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001717#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001718 pointer __p = this->__begin_ + (__position - begin());
1719 if (__n > 0)
1720 {
1721 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1722 {
1723 size_type __old_n = __n;
1724 pointer __old_last = this->__end_;
1725 if (__n > static_cast<size_type>(this->__end_ - __p))
1726 {
1727 size_type __cx = __n - (this->__end_ - __p);
1728 __construct_at_end(__cx, __x);
1729 __n -= __cx;
1730 }
1731 if (__n > 0)
1732 {
1733 __move_range(__p, __old_last, __p + __old_n);
1734 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1735 if (__p <= __xr && __xr < this->__end_)
1736 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001737 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001738 }
1739 }
1740 else
1741 {
1742 allocator_type& __a = this->__alloc();
1743 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1744 __v.__construct_at_end(__n, __x);
1745 __p = __swap_out_circular_buffer(__v, __p);
1746 }
1747 }
1748 return __make_iter(__p);
1749}
1750
1751template <class _Tp, class _Allocator>
1752template <class _InputIterator>
1753typename enable_if
1754<
1755 __is_input_iterator <_InputIterator>::value &&
1756 !__is_forward_iterator<_InputIterator>::value,
1757 typename vector<_Tp, _Allocator>::iterator
1758>::type
1759vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1760{
Howard Hinnantabe26282011-09-16 17:29:17 +00001761#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001762 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1763 "vector::insert(iterator, range) called with an iterator not"
1764 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001765#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001766 difference_type __off = __position - begin();
1767 pointer __p = this->__begin_ + __off;
1768 allocator_type& __a = this->__alloc();
1769 pointer __old_last = this->__end_;
1770 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1771 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001772 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001773 *__first);
1774 ++this->__end_;
1775 }
1776 __split_buffer<value_type, allocator_type&> __v(__a);
1777 if (__first != __last)
1778 {
1779#ifndef _LIBCPP_NO_EXCEPTIONS
1780 try
1781 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001782#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001783 __v.__construct_at_end(__first, __last);
1784 difference_type __old_size = __old_last - this->__begin_;
1785 difference_type __old_p = __p - this->__begin_;
1786 reserve(__recommend(size() + __v.size()));
1787 __p = this->__begin_ + __old_p;
1788 __old_last = this->__begin_ + __old_size;
1789#ifndef _LIBCPP_NO_EXCEPTIONS
1790 }
1791 catch (...)
1792 {
1793 erase(__make_iter(__old_last), end());
1794 throw;
1795 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001796#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001797 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001798 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant0442b122011-09-16 18:41:29 +00001799 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1800 make_move_iterator(__v.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001801 return begin() + __off;
1802}
1803
1804template <class _Tp, class _Allocator>
1805template <class _ForwardIterator>
1806typename enable_if
1807<
1808 __is_forward_iterator<_ForwardIterator>::value,
1809 typename vector<_Tp, _Allocator>::iterator
1810>::type
1811vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1812{
Howard Hinnantabe26282011-09-16 17:29:17 +00001813#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001814 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1815 "vector::insert(iterator, range) called with an iterator not"
1816 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001817#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001818 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00001819 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001820 if (__n > 0)
1821 {
1822 if (__n <= this->__end_cap() - this->__end_)
1823 {
1824 size_type __old_n = __n;
1825 pointer __old_last = this->__end_;
1826 _ForwardIterator __m = __last;
1827 difference_type __dx = this->__end_ - __p;
1828 if (__n > __dx)
1829 {
1830 __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001831 _VSTD::advance(__m, this->__end_ - __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001832 __construct_at_end(__m, __last);
1833 __n = __dx;
1834 }
1835 if (__n > 0)
1836 {
1837 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001838 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001839 }
1840 }
1841 else
1842 {
1843 allocator_type& __a = this->__alloc();
1844 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1845 __v.__construct_at_end(__first, __last);
1846 __p = __swap_out_circular_buffer(__v, __p);
1847 }
1848 }
1849 return __make_iter(__p);
1850}
1851
1852template <class _Tp, class _Allocator>
1853void
1854vector<_Tp, _Allocator>::resize(size_type __sz)
1855{
1856 size_type __cs = size();
1857 if (__cs < __sz)
1858 this->__append(__sz - __cs);
1859 else if (__cs > __sz)
1860 this->__destruct_at_end(this->__begin_ + __sz);
1861}
1862
1863template <class _Tp, class _Allocator>
1864void
1865vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
1866{
1867 size_type __cs = size();
1868 if (__cs < __sz)
1869 this->__append(__sz - __cs, __x);
1870 else if (__cs > __sz)
1871 this->__destruct_at_end(this->__begin_ + __sz);
1872}
1873
1874template <class _Tp, class _Allocator>
1875void
1876vector<_Tp, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001877 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1878 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001879{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001880 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
1881 this->__alloc() == __x.__alloc(),
1882 "vector::swap: Either propagate_on_container_swap must be true"
1883 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +00001884 _VSTD::swap(this->__begin_, __x.__begin_);
1885 _VSTD::swap(this->__end_, __x.__end_);
1886 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001887 __base::__swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnantabe26282011-09-16 17:29:17 +00001888#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001889 __get_db()->swap(this, &__x);
Howard Hinnantabe26282011-09-16 17:29:17 +00001890#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001891}
1892
Howard Hinnant324bb032010-08-22 00:02:43 +00001893template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001894bool
1895vector<_Tp, _Allocator>::__invariants() const
1896{
1897 if (this->__begin_ == 0)
1898 {
1899 if (this->__end_ != 0 || this->__end_cap() != 0)
1900 return false;
1901 }
1902 else
1903 {
1904 if (this->__begin_ > this->__end_)
1905 return false;
1906 if (this->__begin_ == this->__end_cap())
1907 return false;
1908 if (this->__end_ > this->__end_cap())
1909 return false;
1910 }
1911 return true;
1912}
1913
Howard Hinnantabe26282011-09-16 17:29:17 +00001914#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001915
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001916template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:51 +00001917bool
1918vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
1919{
1920 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
1921}
1922
1923template <class _Tp, class _Allocator>
1924bool
1925vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
1926{
1927 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
1928}
1929
1930template <class _Tp, class _Allocator>
1931bool
1932vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
1933{
1934 const_pointer __p = __i->base() + __n;
1935 return this->__begin_ <= __p && __p <= this->__end_;
1936}
1937
1938template <class _Tp, class _Allocator>
1939bool
1940vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
1941{
1942 const_pointer __p = __i->base() + __n;
1943 return this->__begin_ <= __p && __p < this->__end_;
1944}
1945
Howard Hinnantabe26282011-09-16 17:29:17 +00001946#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001947
1948template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001949_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001950void
1951vector<_Tp, _Allocator>::__invalidate_all_iterators()
1952{
Howard Hinnantabe26282011-09-16 17:29:17 +00001953#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001954 __get_db()->__invalidate_all(this);
Howard Hinnantabe26282011-09-16 17:29:17 +00001955#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001956}
1957
1958// vector<bool>
1959
1960template <class _Allocator> class vector<bool, _Allocator>;
1961
1962template <class _Allocator> struct hash<vector<bool, _Allocator> >;
1963
1964template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:23 +00001965struct __has_storage_type<vector<bool, _Allocator> >
1966{
1967 static const bool value = true;
1968};
1969
1970template <class _Allocator>
Howard Hinnant83eade62013-03-06 23:30:19 +00001971class _LIBCPP_TYPE_VIS vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001972 : private __vector_base_common<true>
1973{
1974public:
1975 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00001976 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001977 typedef _Allocator allocator_type;
1978 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001979 typedef typename __alloc_traits::size_type size_type;
1980 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantf867f632012-05-07 16:50:38 +00001981 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001982 typedef __bit_iterator<vector, false> pointer;
1983 typedef __bit_iterator<vector, true> const_pointer;
1984#ifdef _LIBCPP_DEBUG
1985 typedef __debug_iter<vector, pointer> iterator;
1986 typedef __debug_iter<vector, const_pointer> const_iterator;
1987
1988 friend class __debug_iter<vector, pointer>;
1989 friend class __debug_iter<vector, const_pointer>;
1990
1991 pair<iterator*, const_iterator*> __iterator_list_;
1992
1993 _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;}
1994 _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;}
Howard Hinnant324bb032010-08-22 00:02:43 +00001995#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001996 typedef pointer iterator;
1997 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +00001998#endif // _LIBCPP_DEBUG
Howard Hinnant0949eed2011-06-30 21:18:19 +00001999 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2000 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002001
2002private:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002003 typedef typename __alloc_traits::template
2004#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
2005 rebind_alloc<__storage_type>
2006#else
2007 rebind_alloc<__storage_type>::other
2008#endif
2009 __storage_allocator;
2010 typedef allocator_traits<__storage_allocator> __storage_traits;
2011 typedef typename __storage_traits::pointer __storage_pointer;
2012 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2013
2014 __storage_pointer __begin_;
2015 size_type __size_;
2016 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002017public:
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002018 typedef __bit_reference<vector> reference;
2019 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002020private:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002021 _LIBCPP_INLINE_VISIBILITY
2022 size_type& __cap() _NOEXCEPT
2023 {return __cap_alloc_.first();}
2024 _LIBCPP_INLINE_VISIBILITY
2025 const size_type& __cap() const _NOEXCEPT
2026 {return __cap_alloc_.first();}
2027 _LIBCPP_INLINE_VISIBILITY
2028 __storage_allocator& __alloc() _NOEXCEPT
2029 {return __cap_alloc_.second();}
2030 _LIBCPP_INLINE_VISIBILITY
2031 const __storage_allocator& __alloc() const _NOEXCEPT
2032 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002033
2034 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2035
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002036 _LIBCPP_INLINE_VISIBILITY
2037 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002038 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002039 _LIBCPP_INLINE_VISIBILITY
2040 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002041 {return (__n - 1) / __bits_per_word + 1;}
2042
2043public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002044 _LIBCPP_INLINE_VISIBILITY
2045 vector()
2046 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002047 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002048 ~vector();
2049 explicit vector(size_type __n);
2050 vector(size_type __n, const value_type& __v);
2051 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2052 template <class _InputIterator>
2053 vector(_InputIterator __first, _InputIterator __last,
2054 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2055 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2056 template <class _InputIterator>
2057 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2058 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2059 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2060 template <class _ForwardIterator>
2061 vector(_ForwardIterator __first, _ForwardIterator __last,
2062 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2063 template <class _ForwardIterator>
2064 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2065 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2066
2067 vector(const vector& __v);
2068 vector(const vector& __v, const allocator_type& __a);
2069 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00002070#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002071 vector(initializer_list<value_type> __il);
2072 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00002073#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002074
Howard Hinnant73d21a42010-09-04 23:28:19 +00002075#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002076 _LIBCPP_INLINE_VISIBILITY
2077 vector(vector&& __v)
2078 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002079 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002080 _LIBCPP_INLINE_VISIBILITY
2081 vector& operator=(vector&& __v)
2082 _NOEXCEPT_(
2083 __alloc_traits::propagate_on_container_move_assignment::value &&
2084 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002085#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +00002086#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002087 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002088 vector& operator=(initializer_list<value_type> __il)
2089 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00002090#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002091
2092 template <class _InputIterator>
2093 typename enable_if
2094 <
2095 __is_input_iterator<_InputIterator>::value &&
2096 !__is_forward_iterator<_InputIterator>::value,
2097 void
2098 >::type
2099 assign(_InputIterator __first, _InputIterator __last);
2100 template <class _ForwardIterator>
2101 typename enable_if
2102 <
2103 __is_forward_iterator<_ForwardIterator>::value,
2104 void
2105 >::type
2106 assign(_ForwardIterator __first, _ForwardIterator __last);
2107
2108 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +00002109#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002110 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002111 void assign(initializer_list<value_type> __il)
2112 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002113#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002114
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002115 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002116 {return allocator_type(this->__alloc());}
2117
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002118 size_type max_size() const _NOEXCEPT;
2119 _LIBCPP_INLINE_VISIBILITY
2120 size_type capacity() const _NOEXCEPT
2121 {return __internal_cap_to_external(__cap());}
2122 _LIBCPP_INLINE_VISIBILITY
2123 size_type size() const _NOEXCEPT
2124 {return __size_;}
2125 _LIBCPP_INLINE_VISIBILITY
2126 bool empty() const _NOEXCEPT
2127 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002128 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002129 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002131 _LIBCPP_INLINE_VISIBILITY
2132 iterator begin() _NOEXCEPT
2133 {return __make_iter(0);}
2134 _LIBCPP_INLINE_VISIBILITY
2135 const_iterator begin() const _NOEXCEPT
2136 {return __make_iter(0);}
2137 _LIBCPP_INLINE_VISIBILITY
2138 iterator end() _NOEXCEPT
2139 {return __make_iter(__size_);}
2140 _LIBCPP_INLINE_VISIBILITY
2141 const_iterator end() const _NOEXCEPT
2142 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002143
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002144 _LIBCPP_INLINE_VISIBILITY
2145 reverse_iterator rbegin() _NOEXCEPT
2146 {return reverse_iterator(end());}
2147 _LIBCPP_INLINE_VISIBILITY
2148 const_reverse_iterator rbegin() const _NOEXCEPT
2149 {return const_reverse_iterator(end());}
2150 _LIBCPP_INLINE_VISIBILITY
2151 reverse_iterator rend() _NOEXCEPT
2152 {return reverse_iterator(begin());}
2153 _LIBCPP_INLINE_VISIBILITY
2154 const_reverse_iterator rend() const _NOEXCEPT
2155 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002156
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002157 _LIBCPP_INLINE_VISIBILITY
2158 const_iterator cbegin() const _NOEXCEPT
2159 {return __make_iter(0);}
2160 _LIBCPP_INLINE_VISIBILITY
2161 const_iterator cend() const _NOEXCEPT
2162 {return __make_iter(__size_);}
2163 _LIBCPP_INLINE_VISIBILITY
2164 const_reverse_iterator crbegin() const _NOEXCEPT
2165 {return rbegin();}
2166 _LIBCPP_INLINE_VISIBILITY
2167 const_reverse_iterator crend() const _NOEXCEPT
2168 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002169
2170 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2171 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2172 reference at(size_type __n);
2173 const_reference at(size_type __n) const;
2174
2175 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2176 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2177 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2178 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2179
2180 void push_back(const value_type& __x);
2181 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2182
2183 iterator insert(const_iterator __position, const value_type& __x);
2184 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2185 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2186 template <class _InputIterator>
2187 typename enable_if
2188 <
2189 __is_input_iterator <_InputIterator>::value &&
2190 !__is_forward_iterator<_InputIterator>::value,
2191 iterator
2192 >::type
2193 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2194 template <class _ForwardIterator>
2195 typename enable_if
2196 <
2197 __is_forward_iterator<_ForwardIterator>::value,
2198 iterator
2199 >::type
2200 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00002201#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002203 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2204 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002205#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002206
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002207 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002208 iterator erase(const_iterator __first, const_iterator __last);
2209
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002210 _LIBCPP_INLINE_VISIBILITY
2211 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002212
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002213 void swap(vector&)
2214 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2215 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002216
2217 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002218 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002219
2220 bool __invariants() const;
2221
2222private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002223 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002224 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002225 void deallocate() _NOEXCEPT;
2226 _LIBCPP_INLINE_VISIBILITY
2227 static size_type __align(size_type __new_size) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002228 {return __new_size + (__bits_per_word-1) & ~(__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002229 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2230 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002231 template <class _ForwardIterator>
2232 typename enable_if
2233 <
2234 __is_forward_iterator<_ForwardIterator>::value,
2235 void
2236 >::type
2237 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2238 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002239 _LIBCPP_INLINE_VISIBILITY
2240 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002241 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002242 _LIBCPP_INLINE_VISIBILITY
2243 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002244 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
2245#ifdef _LIBCPP_DEBUG
2246 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_type __pos)
2247 {return iterator(this, pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2248 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_type __pos) const
2249 {return const_iterator(this, const_pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2250 _LIBCPP_INLINE_VISIBILITY iterator __const_iterator_cast(const_iterator __p)
2251 {return iterator(this, pointer(const_cast<__storage_pointer>(__p.base().__seg_), __p.base().__ctz_));}
Howard Hinnant324bb032010-08-22 00:02:43 +00002252#else // _LIBCPP_DEBUG
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002253 _LIBCPP_INLINE_VISIBILITY
2254 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002255 {return 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 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002258 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002259 _LIBCPP_INLINE_VISIBILITY
2260 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002261 {return iterator(const_cast<__storage_pointer>(__p.__seg_), __p.__ctz_);}
Howard Hinnant324bb032010-08-22 00:02:43 +00002262#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002263
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002265 void __copy_assign_alloc(const vector& __v)
2266 {__copy_assign_alloc(__v, integral_constant<bool,
2267 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002269 void __copy_assign_alloc(const vector& __c, true_type)
2270 {
2271 if (__alloc() != __c.__alloc())
2272 deallocate();
2273 __alloc() = __c.__alloc();
2274 }
2275
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002277 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002278 {}
2279
2280 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002281 void __move_assign(vector& __c, true_type)
2282 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002284 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002285 _NOEXCEPT_(
2286 !__storage_traits::propagate_on_container_move_assignment::value ||
2287 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002288 {__move_assign_alloc(__c, integral_constant<bool,
2289 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00002291 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002292 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002293 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002294 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002295 }
2296
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002298 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002299 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002300 {}
2301
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002303 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002304 _NOEXCEPT_(
2305 !__storage_traits::propagate_on_container_swap::value ||
2306 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002307 {__swap_alloc(__x, __y, integral_constant<bool,
2308 __storage_traits::propagate_on_container_swap::value>());}
2309
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002311 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002312 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002313 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002314 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002315 swap(__x, __y);
2316 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002318 static void __swap_alloc(__storage_allocator&, __storage_allocator&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002319 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002320 {}
2321
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002322 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002323
2324 friend class __bit_reference<vector>;
2325 friend class __bit_const_reference<vector>;
2326 friend class __bit_iterator<vector, false>;
2327 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:18 +00002328 friend struct __bit_array<vector>;
Howard Hinnant83eade62013-03-06 23:30:19 +00002329 friend struct _LIBCPP_TYPE_VIS hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002330};
2331
2332template <class _Allocator>
2333#ifndef _LIBCPP_DEBUG
2334_LIBCPP_INLINE_VISIBILITY inline
2335#endif
2336void
2337vector<bool, _Allocator>::__invalidate_all_iterators()
2338{
2339#ifdef _LIBCPP_DEBUG
2340 iterator::__remove_all(this);
2341 const_iterator::__remove_all(this);
Howard Hinnant324bb032010-08-22 00:02:43 +00002342#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002343}
2344
2345// Allocate space for __n objects
2346// throws length_error if __n > max_size()
2347// throws (probably bad_alloc) if memory run out
2348// Precondition: __begin_ == __end_ == __cap() == 0
2349// Precondition: __n > 0
2350// Postcondition: capacity() == __n
2351// Postcondition: size() == 0
2352template <class _Allocator>
2353void
2354vector<bool, _Allocator>::allocate(size_type __n)
2355{
2356 if (__n > max_size())
2357 this->__throw_length_error();
2358 __n = __external_cap_to_internal(__n);
2359 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2360 this->__size_ = 0;
2361 this->__cap() = __n;
2362}
2363
2364template <class _Allocator>
2365void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002366vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002367{
2368 if (this->__begin_ != 0)
2369 {
2370 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2371 __invalidate_all_iterators();
2372 this->__begin_ = 0;
2373 this->__size_ = this->__cap() = 0;
2374 }
2375}
2376
2377template <class _Allocator>
2378typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002379vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002380{
2381 size_type __amax = __storage_traits::max_size(__alloc());
2382 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2383 if (__nmax / __bits_per_word <= __amax)
2384 return __nmax;
2385 return __internal_cap_to_external(__amax);
2386}
2387
2388// Precondition: __new_size > capacity()
2389template <class _Allocator>
2390_LIBCPP_INLINE_VISIBILITY inline
2391typename vector<bool, _Allocator>::size_type
2392vector<bool, _Allocator>::__recommend(size_type __new_size) const
2393{
2394 const size_type __ms = max_size();
2395 if (__new_size > __ms)
2396 this->__throw_length_error();
2397 const size_type __cap = capacity();
2398 if (__cap >= __ms / 2)
2399 return __ms;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002400 return _VSTD::max(2*__cap, __align(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002401}
2402
2403// Default constructs __n objects starting at __end_
2404// Precondition: __n > 0
2405// Precondition: size() + __n <= capacity()
2406// Postcondition: size() == size() + __n
2407template <class _Allocator>
2408_LIBCPP_INLINE_VISIBILITY inline
2409void
2410vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2411{
2412 size_type __old_size = this->__size_;
2413 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002414 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002415}
2416
2417template <class _Allocator>
2418template <class _ForwardIterator>
2419typename enable_if
2420<
2421 __is_forward_iterator<_ForwardIterator>::value,
2422 void
2423>::type
2424vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2425{
2426 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002427 this->__size_ += _VSTD::distance(__first, __last);
2428 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002429}
2430
2431template <class _Allocator>
2432_LIBCPP_INLINE_VISIBILITY inline
2433vector<bool, _Allocator>::vector()
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002434 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002435 : __begin_(0),
2436 __size_(0),
2437 __cap_alloc_(0)
2438{
2439}
2440
2441template <class _Allocator>
2442_LIBCPP_INLINE_VISIBILITY inline
2443vector<bool, _Allocator>::vector(const allocator_type& __a)
2444 : __begin_(0),
2445 __size_(0),
2446 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2447{
2448}
2449
2450template <class _Allocator>
2451vector<bool, _Allocator>::vector(size_type __n)
2452 : __begin_(0),
2453 __size_(0),
2454 __cap_alloc_(0)
2455{
2456 if (__n > 0)
2457 {
2458 allocate(__n);
2459 __construct_at_end(__n, false);
2460 }
2461}
2462
2463template <class _Allocator>
2464vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
2465 : __begin_(0),
2466 __size_(0),
2467 __cap_alloc_(0)
2468{
2469 if (__n > 0)
2470 {
2471 allocate(__n);
2472 __construct_at_end(__n, __x);
2473 }
2474}
2475
2476template <class _Allocator>
2477vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
2478 : __begin_(0),
2479 __size_(0),
2480 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2481{
2482 if (__n > 0)
2483 {
2484 allocate(__n);
2485 __construct_at_end(__n, __x);
2486 }
2487}
2488
2489template <class _Allocator>
2490template <class _InputIterator>
2491vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2492 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2493 !__is_forward_iterator<_InputIterator>::value>::type*)
2494 : __begin_(0),
2495 __size_(0),
2496 __cap_alloc_(0)
2497{
2498#ifndef _LIBCPP_NO_EXCEPTIONS
2499 try
2500 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002501#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002502 for (; __first != __last; ++__first)
2503 push_back(*__first);
2504#ifndef _LIBCPP_NO_EXCEPTIONS
2505 }
2506 catch (...)
2507 {
2508 if (__begin_ != 0)
2509 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2510 __invalidate_all_iterators();
2511 throw;
2512 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002513#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002514}
2515
2516template <class _Allocator>
2517template <class _InputIterator>
2518vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2519 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2520 !__is_forward_iterator<_InputIterator>::value>::type*)
2521 : __begin_(0),
2522 __size_(0),
2523 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2524{
2525#ifndef _LIBCPP_NO_EXCEPTIONS
2526 try
2527 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002528#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002529 for (; __first != __last; ++__first)
2530 push_back(*__first);
2531#ifndef _LIBCPP_NO_EXCEPTIONS
2532 }
2533 catch (...)
2534 {
2535 if (__begin_ != 0)
2536 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2537 __invalidate_all_iterators();
2538 throw;
2539 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002540#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002541}
2542
2543template <class _Allocator>
2544template <class _ForwardIterator>
2545vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2546 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2547 : __begin_(0),
2548 __size_(0),
2549 __cap_alloc_(0)
2550{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002551 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002552 if (__n > 0)
2553 {
2554 allocate(__n);
2555 __construct_at_end(__first, __last);
2556 }
2557}
2558
2559template <class _Allocator>
2560template <class _ForwardIterator>
2561vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2562 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2563 : __begin_(0),
2564 __size_(0),
2565 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2566{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002567 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002568 if (__n > 0)
2569 {
2570 allocate(__n);
2571 __construct_at_end(__first, __last);
2572 }
2573}
2574
Howard Hinnante3e32912011-08-12 21:56:02 +00002575#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2576
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002577template <class _Allocator>
2578vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
2579 : __begin_(0),
2580 __size_(0),
2581 __cap_alloc_(0)
2582{
2583 size_type __n = static_cast<size_type>(__il.size());
2584 if (__n > 0)
2585 {
2586 allocate(__n);
2587 __construct_at_end(__il.begin(), __il.end());
2588 }
2589}
2590
2591template <class _Allocator>
2592vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
2593 : __begin_(0),
2594 __size_(0),
2595 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2596{
2597 size_type __n = static_cast<size_type>(__il.size());
2598 if (__n > 0)
2599 {
2600 allocate(__n);
2601 __construct_at_end(__il.begin(), __il.end());
2602 }
2603}
2604
Howard Hinnante3e32912011-08-12 21:56:02 +00002605#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2606
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002607template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002608vector<bool, _Allocator>::~vector()
2609{
2610 if (__begin_ != 0)
2611 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2612#ifdef _LIBCPP_DEBUG
2613 __invalidate_all_iterators();
2614#endif
2615}
2616
2617template <class _Allocator>
2618vector<bool, _Allocator>::vector(const vector& __v)
2619 : __begin_(0),
2620 __size_(0),
2621 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2622{
2623 if (__v.size() > 0)
2624 {
2625 allocate(__v.size());
2626 __construct_at_end(__v.begin(), __v.end());
2627 }
2628}
2629
2630template <class _Allocator>
2631vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
2632 : __begin_(0),
2633 __size_(0),
2634 __cap_alloc_(0, __a)
2635{
2636 if (__v.size() > 0)
2637 {
2638 allocate(__v.size());
2639 __construct_at_end(__v.begin(), __v.end());
2640 }
2641}
2642
2643template <class _Allocator>
2644vector<bool, _Allocator>&
2645vector<bool, _Allocator>::operator=(const vector& __v)
2646{
2647 if (this != &__v)
2648 {
2649 __copy_assign_alloc(__v);
2650 if (__v.__size_)
2651 {
2652 if (__v.__size_ > capacity())
2653 {
2654 deallocate();
2655 allocate(__v.__size_);
2656 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002657 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002658 }
2659 __size_ = __v.__size_;
2660 }
2661 return *this;
2662}
2663
Howard Hinnant73d21a42010-09-04 23:28:19 +00002664#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2665
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002666template <class _Allocator>
2667_LIBCPP_INLINE_VISIBILITY inline
2668vector<bool, _Allocator>::vector(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002669 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002670 : __begin_(__v.__begin_),
2671 __size_(__v.__size_),
2672 __cap_alloc_(__v.__cap_alloc_)
2673{
2674 __v.__begin_ = 0;
2675 __v.__size_ = 0;
2676 __v.__cap() = 0;
2677}
2678
2679template <class _Allocator>
2680vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
2681 : __begin_(0),
2682 __size_(0),
2683 __cap_alloc_(0, __a)
2684{
2685 if (__a == allocator_type(__v.__alloc()))
2686 {
2687 this->__begin_ = __v.__begin_;
2688 this->__size_ = __v.__size_;
2689 this->__cap() = __v.__cap();
2690 __v.__begin_ = nullptr;
2691 __v.__cap() = __v.__size_ = 0;
2692 }
2693 else if (__v.size() > 0)
2694 {
2695 allocate(__v.size());
2696 __construct_at_end(__v.begin(), __v.end());
2697 }
2698}
2699
2700template <class _Allocator>
2701_LIBCPP_INLINE_VISIBILITY inline
2702vector<bool, _Allocator>&
2703vector<bool, _Allocator>::operator=(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002704 _NOEXCEPT_(
2705 __alloc_traits::propagate_on_container_move_assignment::value &&
2706 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002707{
2708 __move_assign(__v, integral_constant<bool,
2709 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002710 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002711}
2712
2713template <class _Allocator>
2714void
2715vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2716{
2717 if (__alloc() != __c.__alloc())
2718 assign(__c.begin(), __c.end());
2719 else
2720 __move_assign(__c, true_type());
2721}
2722
2723template <class _Allocator>
2724void
2725vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002726 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002727{
2728 deallocate();
2729 this->__begin_ = __c.__begin_;
2730 this->__size_ = __c.__size_;
2731 this->__cap() = __c.__cap();
2732 __move_assign_alloc(__c);
2733 __c.__begin_ = nullptr;
2734 __c.__cap() = __c.__size_ = 0;
2735}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002736
2737#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738
2739template <class _Allocator>
2740void
2741vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2742{
2743 __size_ = 0;
2744 if (__n > 0)
2745 {
2746 size_type __c = capacity();
2747 if (__n <= __c)
2748 __size_ = __n;
2749 else
2750 {
2751 vector __v(__alloc());
2752 __v.reserve(__recommend(__n));
2753 __v.__size_ = __n;
2754 swap(__v);
2755 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002756 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002757 }
2758}
2759
2760template <class _Allocator>
2761template <class _InputIterator>
2762typename enable_if
2763<
2764 __is_input_iterator<_InputIterator>::value &&
2765 !__is_forward_iterator<_InputIterator>::value,
2766 void
2767>::type
2768vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2769{
2770 clear();
2771 for (; __first != __last; ++__first)
2772 push_back(*__first);
2773}
2774
2775template <class _Allocator>
2776template <class _ForwardIterator>
2777typename enable_if
2778<
2779 __is_forward_iterator<_ForwardIterator>::value,
2780 void
2781>::type
2782vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2783{
2784 clear();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002785 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002786 if (__n)
2787 {
2788 if (__n > capacity())
2789 {
2790 deallocate();
2791 allocate(__n);
2792 }
2793 __construct_at_end(__first, __last);
2794 }
2795}
2796
2797template <class _Allocator>
2798void
2799vector<bool, _Allocator>::reserve(size_type __n)
2800{
2801 if (__n > capacity())
2802 {
2803 vector __v(this->__alloc());
2804 __v.allocate(__n);
2805 __v.__construct_at_end(this->begin(), this->end());
2806 swap(__v);
2807 __invalidate_all_iterators();
2808 }
2809}
2810
2811template <class _Allocator>
2812void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002813vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002814{
2815 if (__external_cap_to_internal(size()) > __cap())
2816 {
2817#ifndef _LIBCPP_NO_EXCEPTIONS
2818 try
2819 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002820#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002821 vector(*this, allocator_type(__alloc())).swap(*this);
2822#ifndef _LIBCPP_NO_EXCEPTIONS
2823 }
2824 catch (...)
2825 {
2826 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002827#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002828 }
2829}
2830
2831template <class _Allocator>
2832typename vector<bool, _Allocator>::reference
2833vector<bool, _Allocator>::at(size_type __n)
2834{
2835 if (__n >= size())
2836 this->__throw_out_of_range();
2837 return (*this)[__n];
2838}
2839
2840template <class _Allocator>
2841typename vector<bool, _Allocator>::const_reference
2842vector<bool, _Allocator>::at(size_type __n) const
2843{
2844 if (__n >= size())
2845 this->__throw_out_of_range();
2846 return (*this)[__n];
2847}
2848
2849template <class _Allocator>
2850void
2851vector<bool, _Allocator>::push_back(const value_type& __x)
2852{
2853 if (this->__size_ == this->capacity())
2854 reserve(__recommend(this->__size_ + 1));
2855 ++this->__size_;
2856 back() = __x;
2857}
2858
2859template <class _Allocator>
2860typename vector<bool, _Allocator>::iterator
2861vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2862{
2863 iterator __r;
2864 if (size() < capacity())
2865 {
2866 const_iterator __old_end = end();
2867 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002868 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002869 __r = __const_iterator_cast(__position);
2870 }
2871 else
2872 {
2873 vector __v(__alloc());
2874 __v.reserve(__recommend(__size_ + 1));
2875 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002876 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2877 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002878 swap(__v);
2879 }
2880 *__r = __x;
2881 return __r;
2882}
2883
2884template <class _Allocator>
2885typename vector<bool, _Allocator>::iterator
2886vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
2887{
2888 iterator __r;
2889 size_type __c = capacity();
2890 if (__n <= __c && size() <= __c - __n)
2891 {
2892 const_iterator __old_end = end();
2893 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002894 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002895 __r = __const_iterator_cast(__position);
2896 }
2897 else
2898 {
2899 vector __v(__alloc());
2900 __v.reserve(__recommend(__size_ + __n));
2901 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002902 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2903 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002904 swap(__v);
2905 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002906 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002907 return __r;
2908}
2909
2910template <class _Allocator>
2911template <class _InputIterator>
2912typename enable_if
2913<
2914 __is_input_iterator <_InputIterator>::value &&
2915 !__is_forward_iterator<_InputIterator>::value,
2916 typename vector<bool, _Allocator>::iterator
2917>::type
2918vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
2919{
2920 difference_type __off = __position - begin();
2921 iterator __p = __const_iterator_cast(__position);
2922 iterator __old_end = end();
2923 for (; size() != capacity() && __first != __last; ++__first)
2924 {
2925 ++this->__size_;
2926 back() = *__first;
2927 }
2928 vector __v(__alloc());
2929 if (__first != __last)
2930 {
2931#ifndef _LIBCPP_NO_EXCEPTIONS
2932 try
2933 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002934#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002935 __v.assign(__first, __last);
2936 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
2937 difference_type __old_p = __p - begin();
2938 reserve(__recommend(size() + __v.size()));
2939 __p = begin() + __old_p;
2940 __old_end = begin() + __old_size;
2941#ifndef _LIBCPP_NO_EXCEPTIONS
2942 }
2943 catch (...)
2944 {
2945 erase(__old_end, end());
2946 throw;
2947 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002948#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002949 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002950 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002951 insert(__p, __v.begin(), __v.end());
2952 return begin() + __off;
2953}
2954
2955template <class _Allocator>
2956template <class _ForwardIterator>
2957typename enable_if
2958<
2959 __is_forward_iterator<_ForwardIterator>::value,
2960 typename vector<bool, _Allocator>::iterator
2961>::type
2962vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
2963{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002964 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002965 iterator __r;
2966 size_type __c = capacity();
2967 if (__n <= __c && size() <= __c - __n)
2968 {
2969 const_iterator __old_end = end();
2970 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002971 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002972 __r = __const_iterator_cast(__position);
2973 }
2974 else
2975 {
2976 vector __v(__alloc());
2977 __v.reserve(__recommend(__size_ + __n));
2978 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002979 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2980 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002981 swap(__v);
2982 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002983 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002984 return __r;
2985}
2986
2987template <class _Allocator>
2988_LIBCPP_INLINE_VISIBILITY inline
2989typename vector<bool, _Allocator>::iterator
2990vector<bool, _Allocator>::erase(const_iterator __position)
2991{
2992 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002993 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002994 --__size_;
2995 return __r;
2996}
2997
2998template <class _Allocator>
2999typename vector<bool, _Allocator>::iterator
3000vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3001{
3002 iterator __r = __const_iterator_cast(__first);
3003 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003004 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003005 __size_ -= __d;
3006 return __r;
3007}
3008
3009template <class _Allocator>
3010void
3011vector<bool, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003012 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3013 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003014{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003015 _VSTD::swap(this->__begin_, __x.__begin_);
3016 _VSTD::swap(this->__size_, __x.__size_);
3017 _VSTD::swap(this->__cap(), __x.__cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003018 __swap_alloc(this->__alloc(), __x.__alloc());
3019#ifdef _LIBCPP_DEBUG
3020 iterator::swap(this, &__x);
3021 const_iterator::swap(this, &__x);
Howard Hinnant324bb032010-08-22 00:02:43 +00003022#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003023}
3024
Howard Hinnant324bb032010-08-22 00:02:43 +00003025template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003026void
3027vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3028{
3029 size_type __cs = size();
3030 if (__cs < __sz)
3031 {
3032 iterator __r;
3033 size_type __c = capacity();
3034 size_type __n = __sz - __cs;
3035 if (__n <= __c && __cs <= __c - __n)
3036 {
3037 __r = end();
3038 __size_ += __n;
3039 }
3040 else
3041 {
3042 vector __v(__alloc());
3043 __v.reserve(__recommend(__size_ + __n));
3044 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003045 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003046 swap(__v);
3047 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003048 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003049 }
3050 else
3051 __size_ = __sz;
3052}
3053
Howard Hinnant324bb032010-08-22 00:02:43 +00003054template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003055void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003056vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003057{
3058 // do middle whole words
3059 size_type __n = __size_;
3060 __storage_pointer __p = __begin_;
3061 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3062 *__p = ~*__p;
3063 // do last partial word
3064 if (__n > 0)
3065 {
3066 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3067 __storage_type __b = *__p & __m;
3068 *__p &= ~__m;
3069 *__p |= ~__b & __m;
3070 }
3071}
3072
Howard Hinnant324bb032010-08-22 00:02:43 +00003073template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003074bool
3075vector<bool, _Allocator>::__invariants() const
3076{
3077 if (this->__begin_ == 0)
3078 {
3079 if (this->__size_ != 0 || this->__cap() != 0)
3080 return false;
3081 }
3082 else
3083 {
3084 if (this->__cap() == 0)
3085 return false;
3086 if (this->__size_ > this->capacity())
3087 return false;
3088 }
3089 return true;
3090}
3091
Howard Hinnant324bb032010-08-22 00:02:43 +00003092template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003093size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003094vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003095{
3096 size_t __h = 0;
3097 // do middle whole words
3098 size_type __n = __size_;
3099 __storage_pointer __p = __begin_;
3100 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3101 __h ^= *__p;
3102 // do last partial word
3103 if (__n > 0)
3104 {
3105 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3106 __h ^= *__p & __m;
3107 }
3108 return __h;
3109}
3110
3111template <class _Allocator>
Howard Hinnant83eade62013-03-06 23:30:19 +00003112struct _LIBCPP_TYPE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003113 : public unary_function<vector<bool, _Allocator>, size_t>
3114{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00003115 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003116 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003117 {return __vec.__hash_code();}
3118};
3119
3120template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003121_LIBCPP_INLINE_VISIBILITY inline
3122bool
3123operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3124{
3125 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003126 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003127}
3128
3129template <class _Tp, class _Allocator>
3130_LIBCPP_INLINE_VISIBILITY inline
3131bool
3132operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3133{
3134 return !(__x == __y);
3135}
3136
3137template <class _Tp, class _Allocator>
3138_LIBCPP_INLINE_VISIBILITY inline
3139bool
3140operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3141{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003142 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003143}
3144
3145template <class _Tp, class _Allocator>
3146_LIBCPP_INLINE_VISIBILITY inline
3147bool
3148operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3149{
3150 return __y < __x;
3151}
3152
3153template <class _Tp, class _Allocator>
3154_LIBCPP_INLINE_VISIBILITY inline
3155bool
3156operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3157{
3158 return !(__x < __y);
3159}
3160
3161template <class _Tp, class _Allocator>
3162_LIBCPP_INLINE_VISIBILITY inline
3163bool
3164operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3165{
3166 return !(__y < __x);
3167}
3168
3169template <class _Tp, class _Allocator>
3170_LIBCPP_INLINE_VISIBILITY inline
3171void
3172swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003173 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003174{
3175 __x.swap(__y);
3176}
3177
3178_LIBCPP_END_NAMESPACE_STD
3179
3180#endif // _LIBCPP_VECTOR