blob: 373e7c13d886a36a1ea86a1a7b3c6149e3a36b22 [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 Hinnante9df0a52013-08-01 18:17:34 +0000312#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000313#pragma warning( push )
314#pragma warning( disable: 4231 )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000315#endif // _LIBCPP_MSVC
Howard Hinnantff926772012-11-06 21:08:48 +0000316_LIBCPP_EXTERN_TEMPLATE(class __vector_base_common<true>)
Howard Hinnante9df0a52013-08-01 18:17:34 +0000317#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000318#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000319#endif // _LIBCPP_MSVC
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
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000368 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000369
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371 void __copy_assign_alloc(const __vector_base& __c)
372 {__copy_assign_alloc(__c, integral_constant<bool,
373 __alloc_traits::propagate_on_container_copy_assignment::value>());}
374
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000376 void __move_assign_alloc(__vector_base& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000377 _NOEXCEPT_(
378 !__alloc_traits::propagate_on_container_move_assignment::value ||
379 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000380 {__move_assign_alloc(__c, integral_constant<bool,
381 __alloc_traits::propagate_on_container_move_assignment::value>());}
382
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000383 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000385 _NOEXCEPT_(
386 !__alloc_traits::propagate_on_container_swap::value ||
387 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000388 {__swap_alloc(__x, __y, integral_constant<bool,
389 __alloc_traits::propagate_on_container_swap::value>());}
390private:
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392 void __copy_assign_alloc(const __vector_base& __c, true_type)
393 {
394 if (__alloc() != __c.__alloc())
395 {
396 clear();
397 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
398 __begin_ = __end_ = __end_cap() = nullptr;
399 }
400 __alloc() = __c.__alloc();
401 }
402
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000404 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000405 {}
406
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000408 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000409 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000410 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000411 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000412 }
413
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000414 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000415 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000416 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000417 {}
418
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000420 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000421 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000422 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000423 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000424 swap(__x, __y);
425 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000427 static void __swap_alloc(allocator_type&, allocator_type&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000428 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000429 {}
430};
431
432template <class _Tp, class _Allocator>
433_LIBCPP_INLINE_VISIBILITY inline
434void
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000435__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000436{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000437 while (__new_last != __end_)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000438 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000439}
440
441template <class _Tp, class _Allocator>
442_LIBCPP_INLINE_VISIBILITY inline
443__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000444 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000445 : __begin_(nullptr),
446 __end_(nullptr),
447 __end_cap_(nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000448{
449}
450
451template <class _Tp, class _Allocator>
452_LIBCPP_INLINE_VISIBILITY inline
453__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000454 : __begin_(nullptr),
455 __end_(nullptr),
456 __end_cap_(nullptr, __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000457{
458}
459
460template <class _Tp, class _Allocator>
461__vector_base<_Tp, _Allocator>::~__vector_base()
462{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000463 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000464 {
465 clear();
466 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
467 }
468}
469
470template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnant83eade62013-03-06 23:30:19 +0000471class _LIBCPP_TYPE_VIS vector
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000472 : private __vector_base<_Tp, _Allocator>
473{
474private:
475 typedef __vector_base<_Tp, _Allocator> __base;
Howard Hinnant324bb032010-08-22 00:02:43 +0000476public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000477 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +0000478 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000479 typedef _Allocator allocator_type;
480 typedef typename __base::__alloc_traits __alloc_traits;
481 typedef typename __base::reference reference;
482 typedef typename __base::const_reference const_reference;
483 typedef typename __base::size_type size_type;
484 typedef typename __base::difference_type difference_type;
485 typedef typename __base::pointer pointer;
486 typedef typename __base::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000487 typedef __wrap_iter<pointer> iterator;
488 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000489 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
490 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000491
Howard Hinnant02d5e182013-03-26 19:04:56 +0000492 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
493 "Allocator::value_type must be same type as value_type");
494
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000495 _LIBCPP_INLINE_VISIBILITY
496 vector()
497 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant7a563db2011-09-14 18:33:51 +0000498 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000499#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000500 __get_db()->__insert_c(this);
501#endif
502 }
503 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
504 : __base(__a)
505 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000506#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000507 __get_db()->__insert_c(this);
508#endif
509 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000510 explicit vector(size_type __n);
511 vector(size_type __n, const_reference __x);
512 vector(size_type __n, const_reference __x, const allocator_type& __a);
513 template <class _InputIterator>
514 vector(_InputIterator __first, _InputIterator __last,
515 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000516 !__is_forward_iterator<_InputIterator>::value &&
517 is_constructible<
518 value_type,
519 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520 template <class _InputIterator>
521 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
522 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000523 !__is_forward_iterator<_InputIterator>::value &&
524 is_constructible<
525 value_type,
526 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000527 template <class _ForwardIterator>
528 vector(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000529 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
530 is_constructible<
531 value_type,
532 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000533 template <class _ForwardIterator>
534 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000535 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
536 is_constructible<
537 value_type,
538 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000539#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000540 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000541 vector(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000543 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000544#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantabe26282011-09-16 17:29:17 +0000545#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7a563db2011-09-14 18:33:51 +0000547 ~vector()
548 {
549 __get_db()->__erase_c(this);
550 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000551#endif
552
553 vector(const vector& __x);
554 vector(const vector& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000556 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000557#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000559 vector(vector&& __x)
560 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000562 vector(vector&& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000564 vector& operator=(vector&& __x)
565 _NOEXCEPT_(
566 __alloc_traits::propagate_on_container_move_assignment::value &&
567 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000568#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000569#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000571 vector& operator=(initializer_list<value_type> __il)
572 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000573#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000574
575 template <class _InputIterator>
576 typename enable_if
577 <
578 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000579 !__is_forward_iterator<_InputIterator>::value &&
580 is_constructible<
581 value_type,
582 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000583 void
584 >::type
585 assign(_InputIterator __first, _InputIterator __last);
586 template <class _ForwardIterator>
587 typename enable_if
588 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000589 __is_forward_iterator<_ForwardIterator>::value &&
590 is_constructible<
591 value_type,
592 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000593 void
594 >::type
595 assign(_ForwardIterator __first, _ForwardIterator __last);
596
597 void assign(size_type __n, const_reference __u);
Howard Hinnante3e32912011-08-12 21:56:02 +0000598#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000600 void assign(initializer_list<value_type> __il)
601 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000602#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000603
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000604 _LIBCPP_INLINE_VISIBILITY
605 allocator_type get_allocator() const _NOEXCEPT
606 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000607
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000608 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
609 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
610 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
611 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000612
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000613 _LIBCPP_INLINE_VISIBILITY
614 reverse_iterator rbegin() _NOEXCEPT
615 {return reverse_iterator(end());}
616 _LIBCPP_INLINE_VISIBILITY
617 const_reverse_iterator rbegin() const _NOEXCEPT
618 {return const_reverse_iterator(end());}
619 _LIBCPP_INLINE_VISIBILITY
620 reverse_iterator rend() _NOEXCEPT
621 {return reverse_iterator(begin());}
622 _LIBCPP_INLINE_VISIBILITY
623 const_reverse_iterator rend() const _NOEXCEPT
624 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000625
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000626 _LIBCPP_INLINE_VISIBILITY
627 const_iterator cbegin() const _NOEXCEPT
628 {return begin();}
629 _LIBCPP_INLINE_VISIBILITY
630 const_iterator cend() const _NOEXCEPT
631 {return end();}
632 _LIBCPP_INLINE_VISIBILITY
633 const_reverse_iterator crbegin() const _NOEXCEPT
634 {return rbegin();}
635 _LIBCPP_INLINE_VISIBILITY
636 const_reverse_iterator crend() const _NOEXCEPT
637 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000638
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000639 _LIBCPP_INLINE_VISIBILITY
640 size_type size() const _NOEXCEPT
641 {return static_cast<size_type>(this->__end_ - this->__begin_);}
642 _LIBCPP_INLINE_VISIBILITY
643 size_type capacity() const _NOEXCEPT
644 {return __base::capacity();}
645 _LIBCPP_INLINE_VISIBILITY
646 bool empty() const _NOEXCEPT
647 {return this->__begin_ == this->__end_;}
648 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000649 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000650 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000651
652 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
653 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
654 reference at(size_type __n);
655 const_reference at(size_type __n) const;
656
Howard Hinnant7a563db2011-09-14 18:33:51 +0000657 _LIBCPP_INLINE_VISIBILITY reference front()
658 {
659 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
660 return *this->__begin_;
661 }
662 _LIBCPP_INLINE_VISIBILITY const_reference front() const
663 {
664 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
665 return *this->__begin_;
666 }
667 _LIBCPP_INLINE_VISIBILITY reference back()
668 {
669 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
670 return *(this->__end_ - 1);
671 }
672 _LIBCPP_INLINE_VISIBILITY const_reference back() const
673 {
674 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
675 return *(this->__end_ - 1);
676 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000677
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000678 _LIBCPP_INLINE_VISIBILITY
679 value_type* data() _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000680 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000681 _LIBCPP_INLINE_VISIBILITY
682 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000683 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000684
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000685 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000686#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000687 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000688#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000689 template <class... _Args>
690 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000691#endif // _LIBCPP_HAS_NO_VARIADICS
692#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000693 void pop_back();
694
695 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000696#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000697 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000698#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000699 template <class... _Args>
700 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000701#endif // _LIBCPP_HAS_NO_VARIADICS
702#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000703 iterator insert(const_iterator __position, size_type __n, const_reference __x);
704 template <class _InputIterator>
705 typename enable_if
706 <
707 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000708 !__is_forward_iterator<_InputIterator>::value &&
709 is_constructible<
710 value_type,
711 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000712 iterator
713 >::type
714 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
715 template <class _ForwardIterator>
716 typename enable_if
717 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000718 __is_forward_iterator<_ForwardIterator>::value &&
719 is_constructible<
720 value_type,
721 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000722 iterator
723 >::type
724 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +0000725#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000727 iterator insert(const_iterator __position, initializer_list<value_type> __il)
728 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000729#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000730
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000731 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000732 iterator erase(const_iterator __first, const_iterator __last);
733
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000734 _LIBCPP_INLINE_VISIBILITY
735 void clear() _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000736 {
737 __base::clear();
738 __invalidate_all_iterators();
739 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000740
741 void resize(size_type __sz);
742 void resize(size_type __sz, const_reference __x);
743
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000744 void swap(vector&)
745 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
746 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000747
748 bool __invariants() const;
749
Howard Hinnantabe26282011-09-16 17:29:17 +0000750#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000751
752 bool __dereferenceable(const const_iterator* __i) const;
753 bool __decrementable(const const_iterator* __i) const;
754 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
755 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
756
Howard Hinnantabe26282011-09-16 17:29:17 +0000757#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000758
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000759private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000760 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000761 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000762 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000763 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31 +0000764 void __construct_at_end(size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000766 template <class _ForwardIterator>
767 typename enable_if
768 <
769 __is_forward_iterator<_ForwardIterator>::value,
770 void
771 >::type
772 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
773 void __move_construct_at_end(pointer __first, pointer __last);
774 void __append(size_type __n);
775 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000776 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000777 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000779 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000780 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
781 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
782 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000783 void __move_assign(vector& __c, true_type)
784 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000785 void __move_assign(vector& __c, false_type);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000787 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000788 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000789#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000790 __c_node* __c = __get_db()->__find_c_and_lock(this);
791 for (__i_node** __p = __c->end_; __p != __c->beg_; )
792 {
793 --__p;
794 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
795 if (__i->base() > __new_last)
796 {
797 (*__p)->__c_ = nullptr;
798 if (--__c->end_ != __p)
799 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
800 }
801 }
802 __get_db()->unlock();
803#endif
804 __base::__destruct_at_end(__new_last);
805 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000806 template <class _Up>
807 void
808#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
809 __push_back_slow_path(_Up&& __x);
810#else
811 __push_back_slow_path(_Up& __x);
812#endif
Howard Hinnant0438ea22012-02-26 15:30:12 +0000813#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
814 template <class... _Args>
815 void
816 __emplace_back_slow_path(_Args&&... __args);
817#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000818};
819
820template <class _Tp, class _Allocator>
821void
822vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
823{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000824 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000825 _VSTD::swap(this->__begin_, __v.__begin_);
826 _VSTD::swap(this->__end_, __v.__end_);
827 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000828 __v.__first_ = __v.__begin_;
829 __invalidate_all_iterators();
830}
831
832template <class _Tp, class _Allocator>
833typename vector<_Tp, _Allocator>::pointer
834vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
835{
836 pointer __r = __v.__begin_;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000837 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
838 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000839 _VSTD::swap(this->__begin_, __v.__begin_);
840 _VSTD::swap(this->__end_, __v.__end_);
841 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000842 __v.__first_ = __v.__begin_;
843 __invalidate_all_iterators();
844 return __r;
845}
846
847// Allocate space for __n objects
848// throws length_error if __n > max_size()
849// throws (probably bad_alloc) if memory run out
850// Precondition: __begin_ == __end_ == __end_cap() == 0
851// Precondition: __n > 0
852// Postcondition: capacity() == __n
853// Postcondition: size() == 0
854template <class _Tp, class _Allocator>
855void
856vector<_Tp, _Allocator>::allocate(size_type __n)
857{
858 if (__n > max_size())
859 this->__throw_length_error();
860 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
861 this->__end_cap() = this->__begin_ + __n;
862}
863
864template <class _Tp, class _Allocator>
865void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000866vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000867{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000868 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000869 {
870 clear();
871 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000872 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000873 }
874}
875
876template <class _Tp, class _Allocator>
877typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000878vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000879{
Sean Hunt110b8bf2011-07-29 23:31:58 +0000880 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 +0000881}
882
883// Precondition: __new_size > capacity()
884template <class _Tp, class _Allocator>
885_LIBCPP_INLINE_VISIBILITY inline
886typename vector<_Tp, _Allocator>::size_type
887vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
888{
889 const size_type __ms = max_size();
890 if (__new_size > __ms)
891 this->__throw_length_error();
892 const size_type __cap = capacity();
893 if (__cap >= __ms / 2)
894 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58 +0000895 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000896}
897
898// Default constructs __n objects starting at __end_
899// throws if construction throws
900// Precondition: __n > 0
901// Precondition: size() + __n <= capacity()
902// Postcondition: size() == size() + __n
903template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000904void
905vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
906{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000907 allocator_type& __a = this->__alloc();
908 do
909 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000910 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000911 ++this->__end_;
912 --__n;
913 } while (__n > 0);
914}
915
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000916// Copy constructs __n objects starting at __end_ from __x
917// throws if construction throws
918// Precondition: __n > 0
919// Precondition: size() + __n <= capacity()
920// Postcondition: size() == old size() + __n
921// Postcondition: [i] == __x for all i in [size() - __n, __n)
922template <class _Tp, class _Allocator>
923_LIBCPP_INLINE_VISIBILITY inline
924void
925vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
926{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000927 allocator_type& __a = this->__alloc();
928 do
929 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000930 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000931 ++this->__end_;
932 --__n;
933 } while (__n > 0);
934}
935
936template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000937template <class _ForwardIterator>
938typename enable_if
939<
940 __is_forward_iterator<_ForwardIterator>::value,
941 void
942>::type
943vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
944{
945 allocator_type& __a = this->__alloc();
946 for (; __first != __last; ++__first)
947 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000948 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000949 ++this->__end_;
950 }
951}
952
953template <class _Tp, class _Allocator>
954void
955vector<_Tp, _Allocator>::__move_construct_at_end(pointer __first, pointer __last)
956{
957 allocator_type& __a = this->__alloc();
958 for (; __first != __last; ++__first)
959 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000960 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
961 _VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000962 ++this->__end_;
963 }
964}
965
966// Default constructs __n objects starting at __end_
967// throws if construction throws
968// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000969// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000970template <class _Tp, class _Allocator>
971void
972vector<_Tp, _Allocator>::__append(size_type __n)
973{
974 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
975 this->__construct_at_end(__n);
976 else
977 {
978 allocator_type& __a = this->__alloc();
979 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
980 __v.__construct_at_end(__n);
981 __swap_out_circular_buffer(__v);
982 }
983}
984
985// Default constructs __n objects starting at __end_
986// throws if construction throws
987// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000988// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000989template <class _Tp, class _Allocator>
990void
991vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
992{
993 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
994 this->__construct_at_end(__n, __x);
995 else
996 {
997 allocator_type& __a = this->__alloc();
998 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
999 __v.__construct_at_end(__n, __x);
1000 __swap_out_circular_buffer(__v);
1001 }
1002}
1003
1004template <class _Tp, class _Allocator>
1005vector<_Tp, _Allocator>::vector(size_type __n)
1006{
Howard Hinnant0442b122011-09-16 18:41:29 +00001007#if _LIBCPP_DEBUG_LEVEL >= 2
1008 __get_db()->__insert_c(this);
1009#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001010 if (__n > 0)
1011 {
1012 allocate(__n);
1013 __construct_at_end(__n);
1014 }
1015}
1016
1017template <class _Tp, class _Allocator>
1018vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1019{
Howard Hinnant0442b122011-09-16 18:41:29 +00001020#if _LIBCPP_DEBUG_LEVEL >= 2
1021 __get_db()->__insert_c(this);
1022#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001023 if (__n > 0)
1024 {
1025 allocate(__n);
1026 __construct_at_end(__n, __x);
1027 }
1028}
1029
1030template <class _Tp, class _Allocator>
1031vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1032 : __base(__a)
1033{
Howard Hinnant0442b122011-09-16 18:41:29 +00001034#if _LIBCPP_DEBUG_LEVEL >= 2
1035 __get_db()->__insert_c(this);
1036#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001037 if (__n > 0)
1038 {
1039 allocate(__n);
1040 __construct_at_end(__n, __x);
1041 }
1042}
1043
1044template <class _Tp, class _Allocator>
1045template <class _InputIterator>
1046vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
1047 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001048 !__is_forward_iterator<_InputIterator>::value &&
1049 is_constructible<
1050 value_type,
1051 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001052{
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 _InputIterator>
1062vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1063 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001064 !__is_forward_iterator<_InputIterator>::value &&
1065 is_constructible<
1066 value_type,
1067 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001068 : __base(__a)
1069{
Howard Hinnantabe26282011-09-16 17:29:17 +00001070#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001071 __get_db()->__insert_c(this);
1072#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001073 for (; __first != __last; ++__first)
1074 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001075}
1076
1077template <class _Tp, class _Allocator>
1078template <class _ForwardIterator>
1079vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001080 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1081 is_constructible<
1082 value_type,
1083 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001084{
Howard Hinnant0442b122011-09-16 18:41:29 +00001085#if _LIBCPP_DEBUG_LEVEL >= 2
1086 __get_db()->__insert_c(this);
1087#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001088 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001089 if (__n > 0)
1090 {
1091 allocate(__n);
1092 __construct_at_end(__first, __last);
1093 }
1094}
1095
1096template <class _Tp, class _Allocator>
1097template <class _ForwardIterator>
1098vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001099 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1100 is_constructible<
1101 value_type,
1102 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001103 : __base(__a)
1104{
Howard Hinnant0442b122011-09-16 18:41:29 +00001105#if _LIBCPP_DEBUG_LEVEL >= 2
1106 __get_db()->__insert_c(this);
1107#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001108 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001109 if (__n > 0)
1110 {
1111 allocate(__n);
1112 __construct_at_end(__first, __last);
1113 }
1114}
1115
1116template <class _Tp, class _Allocator>
1117vector<_Tp, _Allocator>::vector(const vector& __x)
1118 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1119{
Howard Hinnant0442b122011-09-16 18:41:29 +00001120#if _LIBCPP_DEBUG_LEVEL >= 2
1121 __get_db()->__insert_c(this);
1122#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001123 size_type __n = __x.size();
1124 if (__n > 0)
1125 {
1126 allocate(__n);
1127 __construct_at_end(__x.__begin_, __x.__end_);
1128 }
1129}
1130
1131template <class _Tp, class _Allocator>
1132vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1133 : __base(__a)
1134{
Howard Hinnant0442b122011-09-16 18:41:29 +00001135#if _LIBCPP_DEBUG_LEVEL >= 2
1136 __get_db()->__insert_c(this);
1137#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001138 size_type __n = __x.size();
1139 if (__n > 0)
1140 {
1141 allocate(__n);
1142 __construct_at_end(__x.__begin_, __x.__end_);
1143 }
1144}
1145
Howard Hinnant73d21a42010-09-04 23:28:19 +00001146#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001147
1148template <class _Tp, class _Allocator>
1149_LIBCPP_INLINE_VISIBILITY inline
1150vector<_Tp, _Allocator>::vector(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001151 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001152 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001153{
Howard Hinnantabe26282011-09-16 17:29:17 +00001154#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001155 __get_db()->__insert_c(this);
Howard Hinnante6125bd2011-09-19 16:34:29 +00001156 __get_db()->swap(this, &__x);
Howard Hinnant7a563db2011-09-14 18:33:51 +00001157#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001158 this->__begin_ = __x.__begin_;
1159 this->__end_ = __x.__end_;
1160 this->__end_cap() = __x.__end_cap();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001161 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001162}
1163
1164template <class _Tp, class _Allocator>
1165_LIBCPP_INLINE_VISIBILITY inline
1166vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1167 : __base(__a)
1168{
Howard Hinnant0442b122011-09-16 18:41:29 +00001169#if _LIBCPP_DEBUG_LEVEL >= 2
1170 __get_db()->__insert_c(this);
1171#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001172 if (__a == __x.__alloc())
1173 {
1174 this->__begin_ = __x.__begin_;
1175 this->__end_ = __x.__end_;
1176 this->__end_cap() = __x.__end_cap();
1177 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001178#if _LIBCPP_DEBUG_LEVEL >= 2
1179 __get_db()->swap(this, &__x);
1180#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001181 }
1182 else
1183 {
Howard Hinnant99968442011-11-29 18:15:50 +00001184 typedef move_iterator<iterator> _Ip;
1185 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001186 }
1187}
1188
Howard Hinnante3e32912011-08-12 21:56:02 +00001189#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1190
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001191template <class _Tp, class _Allocator>
1192_LIBCPP_INLINE_VISIBILITY inline
1193vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1194{
Howard Hinnant0442b122011-09-16 18:41:29 +00001195#if _LIBCPP_DEBUG_LEVEL >= 2
1196 __get_db()->__insert_c(this);
1197#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001198 if (__il.size() > 0)
1199 {
1200 allocate(__il.size());
1201 __construct_at_end(__il.begin(), __il.end());
1202 }
1203}
1204
1205template <class _Tp, class _Allocator>
1206_LIBCPP_INLINE_VISIBILITY inline
1207vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1208 : __base(__a)
1209{
Howard Hinnant0442b122011-09-16 18:41:29 +00001210#if _LIBCPP_DEBUG_LEVEL >= 2
1211 __get_db()->__insert_c(this);
1212#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001213 if (__il.size() > 0)
1214 {
1215 allocate(__il.size());
1216 __construct_at_end(__il.begin(), __il.end());
1217 }
1218}
1219
Howard Hinnante3e32912011-08-12 21:56:02 +00001220#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1221
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001222template <class _Tp, class _Allocator>
1223_LIBCPP_INLINE_VISIBILITY inline
1224vector<_Tp, _Allocator>&
1225vector<_Tp, _Allocator>::operator=(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001226 _NOEXCEPT_(
1227 __alloc_traits::propagate_on_container_move_assignment::value &&
1228 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001229{
1230 __move_assign(__x, integral_constant<bool,
1231 __alloc_traits::propagate_on_container_move_assignment::value>());
1232 return *this;
1233}
1234
1235template <class _Tp, class _Allocator>
1236void
1237vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1238{
1239 if (__base::__alloc() != __c.__alloc())
1240 {
Howard Hinnant99968442011-11-29 18:15:50 +00001241 typedef move_iterator<iterator> _Ip;
1242 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001243 }
1244 else
1245 __move_assign(__c, true_type());
1246}
1247
1248template <class _Tp, class _Allocator>
1249void
1250vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001251 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001252{
1253 deallocate();
1254 this->__begin_ = __c.__begin_;
1255 this->__end_ = __c.__end_;
1256 this->__end_cap() = __c.__end_cap();
1257 __base::__move_assign_alloc(__c);
1258 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001259#if _LIBCPP_DEBUG_LEVEL >= 2
1260 __get_db()->swap(this, &__c);
1261#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001262}
1263
Howard Hinnant73d21a42010-09-04 23:28:19 +00001264#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001265
1266template <class _Tp, class _Allocator>
1267_LIBCPP_INLINE_VISIBILITY inline
1268vector<_Tp, _Allocator>&
1269vector<_Tp, _Allocator>::operator=(const vector& __x)
1270{
1271 if (this != &__x)
1272 {
1273 __base::__copy_assign_alloc(__x);
1274 assign(__x.__begin_, __x.__end_);
1275 }
1276 return *this;
1277}
1278
1279template <class _Tp, class _Allocator>
1280template <class _InputIterator>
1281typename enable_if
1282<
1283 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001284 !__is_forward_iterator<_InputIterator>::value &&
1285 is_constructible<
1286 _Tp,
1287 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001288 void
1289>::type
1290vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1291{
1292 clear();
1293 for (; __first != __last; ++__first)
1294 push_back(*__first);
1295}
1296
1297template <class _Tp, class _Allocator>
1298template <class _ForwardIterator>
1299typename enable_if
1300<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001301 __is_forward_iterator<_ForwardIterator>::value &&
1302 is_constructible<
1303 _Tp,
1304 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001305 void
1306>::type
1307vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1308{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001309 typename iterator_traits<_ForwardIterator>::difference_type __new_size = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001310 if (static_cast<size_type>(__new_size) <= capacity())
1311 {
1312 _ForwardIterator __mid = __last;
1313 bool __growing = false;
1314 if (static_cast<size_type>(__new_size) > size())
1315 {
1316 __growing = true;
1317 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001318 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001319 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001320 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001321 if (__growing)
1322 __construct_at_end(__mid, __last);
1323 else
1324 this->__destruct_at_end(__m);
1325 }
1326 else
1327 {
1328 deallocate();
1329 allocate(__recommend(static_cast<size_type>(__new_size)));
1330 __construct_at_end(__first, __last);
1331 }
1332}
1333
1334template <class _Tp, class _Allocator>
1335void
1336vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1337{
1338 if (__n <= capacity())
1339 {
1340 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001341 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001342 if (__n > __s)
1343 __construct_at_end(__n - __s, __u);
1344 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001345 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001346 }
1347 else
1348 {
1349 deallocate();
1350 allocate(__recommend(static_cast<size_type>(__n)));
1351 __construct_at_end(__n, __u);
1352 }
1353}
1354
Howard Hinnant324bb032010-08-22 00:02:43 +00001355template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001356_LIBCPP_INLINE_VISIBILITY inline
1357typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001358vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001359{
Howard Hinnantabe26282011-09-16 17:29:17 +00001360#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001361 return iterator(this, __p);
1362#else
1363 return iterator(__p);
1364#endif
1365}
1366
Howard Hinnant324bb032010-08-22 00:02:43 +00001367template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001368_LIBCPP_INLINE_VISIBILITY inline
1369typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001370vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001371{
Howard Hinnantabe26282011-09-16 17:29:17 +00001372#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001373 return const_iterator(this, __p);
1374#else
1375 return const_iterator(__p);
1376#endif
1377}
1378
Howard Hinnant324bb032010-08-22 00:02:43 +00001379template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001380_LIBCPP_INLINE_VISIBILITY inline
1381typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001382vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001383{
1384 return __make_iter(this->__begin_);
1385}
1386
Howard Hinnant324bb032010-08-22 00:02:43 +00001387template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001388_LIBCPP_INLINE_VISIBILITY inline
1389typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001390vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001391{
1392 return __make_iter(this->__begin_);
1393}
1394
Howard Hinnant324bb032010-08-22 00:02:43 +00001395template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001396_LIBCPP_INLINE_VISIBILITY inline
1397typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001398vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001399{
1400 return __make_iter(this->__end_);
1401}
1402
Howard Hinnant324bb032010-08-22 00:02:43 +00001403template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001404_LIBCPP_INLINE_VISIBILITY inline
1405typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001406vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001407{
1408 return __make_iter(this->__end_);
1409}
1410
Howard Hinnant324bb032010-08-22 00:02:43 +00001411template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001412_LIBCPP_INLINE_VISIBILITY inline
1413typename vector<_Tp, _Allocator>::reference
1414vector<_Tp, _Allocator>::operator[](size_type __n)
1415{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001416 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001417 return this->__begin_[__n];
1418}
1419
Howard Hinnant324bb032010-08-22 00:02:43 +00001420template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001421_LIBCPP_INLINE_VISIBILITY inline
1422typename vector<_Tp, _Allocator>::const_reference
1423vector<_Tp, _Allocator>::operator[](size_type __n) const
1424{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001425 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001426 return this->__begin_[__n];
1427}
1428
Howard Hinnant324bb032010-08-22 00:02:43 +00001429template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001430typename vector<_Tp, _Allocator>::reference
1431vector<_Tp, _Allocator>::at(size_type __n)
1432{
1433 if (__n >= size())
1434 this->__throw_out_of_range();
1435 return this->__begin_[__n];
1436}
1437
Howard Hinnant324bb032010-08-22 00:02:43 +00001438template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001439typename vector<_Tp, _Allocator>::const_reference
1440vector<_Tp, _Allocator>::at(size_type __n) const
1441{
1442 if (__n >= size())
1443 this->__throw_out_of_range();
1444 return this->__begin_[__n];
1445}
1446
1447template <class _Tp, class _Allocator>
1448void
1449vector<_Tp, _Allocator>::reserve(size_type __n)
1450{
1451 if (__n > capacity())
1452 {
1453 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001454 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001455 __swap_out_circular_buffer(__v);
1456 }
1457}
1458
1459template <class _Tp, class _Allocator>
1460void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001461vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001462{
1463 if (capacity() > size())
1464 {
1465#ifndef _LIBCPP_NO_EXCEPTIONS
1466 try
1467 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001468#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001470 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001471 __swap_out_circular_buffer(__v);
1472#ifndef _LIBCPP_NO_EXCEPTIONS
1473 }
1474 catch (...)
1475 {
1476 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001477#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478 }
1479}
1480
1481template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001482template <class _Up>
1483void
1484#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1485vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1486#else
1487vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1488#endif
1489{
1490 allocator_type& __a = this->__alloc();
1491 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1492 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnantf619e232013-01-11 20:36:59 +00001493 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1494 __v.__end_++;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001495 __swap_out_circular_buffer(__v);
1496}
1497
1498template <class _Tp, class _Allocator>
1499_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001500void
1501vector<_Tp, _Allocator>::push_back(const_reference __x)
1502{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001503 if (this->__end_ != this->__end_cap())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504 {
1505 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001506 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001507 ++this->__end_;
1508 }
1509 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001510 __push_back_slow_path(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001511}
1512
Howard Hinnant73d21a42010-09-04 23:28:19 +00001513#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001514
1515template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001516_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001517void
1518vector<_Tp, _Allocator>::push_back(value_type&& __x)
1519{
1520 if (this->__end_ < this->__end_cap())
1521 {
1522 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001523 _VSTD::__to_raw_pointer(this->__end_),
1524 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001525 ++this->__end_;
1526 }
1527 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001528 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001529}
1530
Howard Hinnant73d21a42010-09-04 23:28:19 +00001531#ifndef _LIBCPP_HAS_NO_VARIADICS
1532
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001533template <class _Tp, class _Allocator>
1534template <class... _Args>
1535void
Howard Hinnant0438ea22012-02-26 15:30:12 +00001536vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1537{
1538 allocator_type& __a = this->__alloc();
1539 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1540// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantf619e232013-01-11 20:36:59 +00001541 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1542 __v.__end_++;
Howard Hinnant0438ea22012-02-26 15:30:12 +00001543 __swap_out_circular_buffer(__v);
1544}
1545
1546template <class _Tp, class _Allocator>
1547template <class... _Args>
1548_LIBCPP_INLINE_VISIBILITY inline
1549void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001550vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1551{
1552 if (this->__end_ < this->__end_cap())
1553 {
1554 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001555 _VSTD::__to_raw_pointer(this->__end_),
1556 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001557 ++this->__end_;
1558 }
1559 else
Howard Hinnant0438ea22012-02-26 15:30:12 +00001560 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001561}
1562
Howard Hinnant73d21a42010-09-04 23:28:19 +00001563#endif // _LIBCPP_HAS_NO_VARIADICS
1564#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001565
1566template <class _Tp, class _Allocator>
1567_LIBCPP_INLINE_VISIBILITY inline
1568void
1569vector<_Tp, _Allocator>::pop_back()
1570{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001571 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001572 this->__destruct_at_end(this->__end_ - 1);
1573}
1574
1575template <class _Tp, class _Allocator>
1576_LIBCPP_INLINE_VISIBILITY inline
1577typename vector<_Tp, _Allocator>::iterator
1578vector<_Tp, _Allocator>::erase(const_iterator __position)
1579{
Howard Hinnantabe26282011-09-16 17:29:17 +00001580#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001581 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1582 "vector::erase(iterator) called with an iterator not"
1583 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001584#endif
Howard Hinnant782da332013-03-25 22:12:26 +00001585 _LIBCPP_ASSERT(__position != end(),
1586 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001587 difference_type __ps = __position - cbegin();
1588 pointer __p = this->__begin_ + __ps;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001589 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001590 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001591 return __r;
1592}
1593
1594template <class _Tp, class _Allocator>
1595typename vector<_Tp, _Allocator>::iterator
1596vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1597{
Howard Hinnantabe26282011-09-16 17:29:17 +00001598#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001599 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1600 "vector::erase(iterator, iterator) called with an iterator not"
1601 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001602#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +00001603 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604 pointer __p = this->__begin_ + (__first - begin());
1605 iterator __r = __make_iter(__p);
Howard Hinnantb4e67cf2013-04-18 15:02:57 +00001606 if (__first != __last)
1607 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001608 return __r;
1609}
1610
1611template <class _Tp, class _Allocator>
1612void
1613vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1614{
1615 pointer __old_last = this->__end_;
1616 difference_type __n = __old_last - __to;
1617 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1618 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001619 _VSTD::__to_raw_pointer(this->__end_),
1620 _VSTD::move(*__i));
1621 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001622}
1623
1624template <class _Tp, class _Allocator>
1625typename vector<_Tp, _Allocator>::iterator
1626vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1627{
Howard Hinnantabe26282011-09-16 17:29:17 +00001628#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001629 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1630 "vector::insert(iterator, x) called with an iterator not"
1631 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001632#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001633 pointer __p = this->__begin_ + (__position - begin());
1634 if (this->__end_ < this->__end_cap())
1635 {
1636 if (__p == this->__end_)
1637 {
1638 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001639 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640 ++this->__end_;
1641 }
1642 else
1643 {
1644 __move_range(__p, this->__end_, __p + 1);
1645 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1646 if (__p <= __xr && __xr < this->__end_)
1647 ++__xr;
1648 *__p = *__xr;
1649 }
1650 }
1651 else
1652 {
1653 allocator_type& __a = this->__alloc();
1654 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1655 __v.push_back(__x);
1656 __p = __swap_out_circular_buffer(__v, __p);
1657 }
1658 return __make_iter(__p);
1659}
1660
Howard Hinnant73d21a42010-09-04 23:28:19 +00001661#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001662
1663template <class _Tp, class _Allocator>
1664typename vector<_Tp, _Allocator>::iterator
1665vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1666{
Howard Hinnantabe26282011-09-16 17:29:17 +00001667#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001668 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1669 "vector::insert(iterator, x) called with an iterator not"
1670 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001671#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001672 pointer __p = this->__begin_ + (__position - begin());
1673 if (this->__end_ < this->__end_cap())
1674 {
1675 if (__p == this->__end_)
1676 {
1677 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001678 _VSTD::__to_raw_pointer(this->__end_),
1679 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001680 ++this->__end_;
1681 }
1682 else
1683 {
1684 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001685 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001686 }
1687 }
1688 else
1689 {
1690 allocator_type& __a = this->__alloc();
1691 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001692 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001693 __p = __swap_out_circular_buffer(__v, __p);
1694 }
1695 return __make_iter(__p);
1696}
1697
Howard Hinnant73d21a42010-09-04 23:28:19 +00001698#ifndef _LIBCPP_HAS_NO_VARIADICS
1699
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001700template <class _Tp, class _Allocator>
1701template <class... _Args>
1702typename vector<_Tp, _Allocator>::iterator
1703vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1704{
Howard Hinnantabe26282011-09-16 17:29:17 +00001705#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001706 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1707 "vector::emplace(iterator, x) called with an iterator not"
1708 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001709#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001710 pointer __p = this->__begin_ + (__position - begin());
1711 if (this->__end_ < this->__end_cap())
1712 {
1713 if (__p == this->__end_)
1714 {
1715 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001716 _VSTD::__to_raw_pointer(this->__end_),
1717 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001718 ++this->__end_;
1719 }
1720 else
1721 {
Howard Hinnanta58402a2012-07-08 23:23:04 +00001722 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001723 __move_range(__p, this->__end_, __p + 1);
Howard Hinnanta58402a2012-07-08 23:23:04 +00001724 *__p = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001725 }
1726 }
1727 else
1728 {
1729 allocator_type& __a = this->__alloc();
1730 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001731 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001732 __p = __swap_out_circular_buffer(__v, __p);
1733 }
1734 return __make_iter(__p);
1735}
1736
Howard Hinnant73d21a42010-09-04 23:28:19 +00001737#endif // _LIBCPP_HAS_NO_VARIADICS
1738#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001739
1740template <class _Tp, class _Allocator>
1741typename vector<_Tp, _Allocator>::iterator
1742vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1743{
Howard Hinnantabe26282011-09-16 17:29:17 +00001744#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001745 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1746 "vector::insert(iterator, n, x) called with an iterator not"
1747 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001748#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001749 pointer __p = this->__begin_ + (__position - begin());
1750 if (__n > 0)
1751 {
1752 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1753 {
1754 size_type __old_n = __n;
1755 pointer __old_last = this->__end_;
1756 if (__n > static_cast<size_type>(this->__end_ - __p))
1757 {
1758 size_type __cx = __n - (this->__end_ - __p);
1759 __construct_at_end(__cx, __x);
1760 __n -= __cx;
1761 }
1762 if (__n > 0)
1763 {
1764 __move_range(__p, __old_last, __p + __old_n);
1765 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1766 if (__p <= __xr && __xr < this->__end_)
1767 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001768 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001769 }
1770 }
1771 else
1772 {
1773 allocator_type& __a = this->__alloc();
1774 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1775 __v.__construct_at_end(__n, __x);
1776 __p = __swap_out_circular_buffer(__v, __p);
1777 }
1778 }
1779 return __make_iter(__p);
1780}
1781
1782template <class _Tp, class _Allocator>
1783template <class _InputIterator>
1784typename enable_if
1785<
1786 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001787 !__is_forward_iterator<_InputIterator>::value &&
1788 is_constructible<
1789 _Tp,
1790 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001791 typename vector<_Tp, _Allocator>::iterator
1792>::type
1793vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1794{
Howard Hinnantabe26282011-09-16 17:29:17 +00001795#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001796 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1797 "vector::insert(iterator, range) called with an iterator not"
1798 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001799#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001800 difference_type __off = __position - begin();
1801 pointer __p = this->__begin_ + __off;
1802 allocator_type& __a = this->__alloc();
1803 pointer __old_last = this->__end_;
1804 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1805 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001806 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001807 *__first);
1808 ++this->__end_;
1809 }
1810 __split_buffer<value_type, allocator_type&> __v(__a);
1811 if (__first != __last)
1812 {
1813#ifndef _LIBCPP_NO_EXCEPTIONS
1814 try
1815 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001816#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817 __v.__construct_at_end(__first, __last);
1818 difference_type __old_size = __old_last - this->__begin_;
1819 difference_type __old_p = __p - this->__begin_;
1820 reserve(__recommend(size() + __v.size()));
1821 __p = this->__begin_ + __old_p;
1822 __old_last = this->__begin_ + __old_size;
1823#ifndef _LIBCPP_NO_EXCEPTIONS
1824 }
1825 catch (...)
1826 {
1827 erase(__make_iter(__old_last), end());
1828 throw;
1829 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001830#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001831 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001832 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant0442b122011-09-16 18:41:29 +00001833 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1834 make_move_iterator(__v.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001835 return begin() + __off;
1836}
1837
1838template <class _Tp, class _Allocator>
1839template <class _ForwardIterator>
1840typename enable_if
1841<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001842 __is_forward_iterator<_ForwardIterator>::value &&
1843 is_constructible<
1844 _Tp,
1845 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001846 typename vector<_Tp, _Allocator>::iterator
1847>::type
1848vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1849{
Howard Hinnantabe26282011-09-16 17:29:17 +00001850#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001851 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1852 "vector::insert(iterator, range) called with an iterator not"
1853 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001854#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001855 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00001856 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001857 if (__n > 0)
1858 {
1859 if (__n <= this->__end_cap() - this->__end_)
1860 {
1861 size_type __old_n = __n;
1862 pointer __old_last = this->__end_;
1863 _ForwardIterator __m = __last;
1864 difference_type __dx = this->__end_ - __p;
1865 if (__n > __dx)
1866 {
1867 __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001868 _VSTD::advance(__m, this->__end_ - __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001869 __construct_at_end(__m, __last);
1870 __n = __dx;
1871 }
1872 if (__n > 0)
1873 {
1874 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001875 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001876 }
1877 }
1878 else
1879 {
1880 allocator_type& __a = this->__alloc();
1881 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1882 __v.__construct_at_end(__first, __last);
1883 __p = __swap_out_circular_buffer(__v, __p);
1884 }
1885 }
1886 return __make_iter(__p);
1887}
1888
1889template <class _Tp, class _Allocator>
1890void
1891vector<_Tp, _Allocator>::resize(size_type __sz)
1892{
1893 size_type __cs = size();
1894 if (__cs < __sz)
1895 this->__append(__sz - __cs);
1896 else if (__cs > __sz)
1897 this->__destruct_at_end(this->__begin_ + __sz);
1898}
1899
1900template <class _Tp, class _Allocator>
1901void
1902vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
1903{
1904 size_type __cs = size();
1905 if (__cs < __sz)
1906 this->__append(__sz - __cs, __x);
1907 else if (__cs > __sz)
1908 this->__destruct_at_end(this->__begin_ + __sz);
1909}
1910
1911template <class _Tp, class _Allocator>
1912void
1913vector<_Tp, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001914 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1915 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001916{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001917 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
1918 this->__alloc() == __x.__alloc(),
1919 "vector::swap: Either propagate_on_container_swap must be true"
1920 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +00001921 _VSTD::swap(this->__begin_, __x.__begin_);
1922 _VSTD::swap(this->__end_, __x.__end_);
1923 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001924 __base::__swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnantabe26282011-09-16 17:29:17 +00001925#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001926 __get_db()->swap(this, &__x);
Howard Hinnantabe26282011-09-16 17:29:17 +00001927#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001928}
1929
Howard Hinnant324bb032010-08-22 00:02:43 +00001930template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001931bool
1932vector<_Tp, _Allocator>::__invariants() const
1933{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001934 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001935 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001936 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001937 return false;
1938 }
1939 else
1940 {
1941 if (this->__begin_ > this->__end_)
1942 return false;
1943 if (this->__begin_ == this->__end_cap())
1944 return false;
1945 if (this->__end_ > this->__end_cap())
1946 return false;
1947 }
1948 return true;
1949}
1950
Howard Hinnantabe26282011-09-16 17:29:17 +00001951#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001952
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:51 +00001954bool
1955vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
1956{
1957 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
1958}
1959
1960template <class _Tp, class _Allocator>
1961bool
1962vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
1963{
1964 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
1965}
1966
1967template <class _Tp, class _Allocator>
1968bool
1969vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
1970{
1971 const_pointer __p = __i->base() + __n;
1972 return this->__begin_ <= __p && __p <= this->__end_;
1973}
1974
1975template <class _Tp, class _Allocator>
1976bool
1977vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
1978{
1979 const_pointer __p = __i->base() + __n;
1980 return this->__begin_ <= __p && __p < this->__end_;
1981}
1982
Howard Hinnantabe26282011-09-16 17:29:17 +00001983#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001984
1985template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001986_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001987void
1988vector<_Tp, _Allocator>::__invalidate_all_iterators()
1989{
Howard Hinnantabe26282011-09-16 17:29:17 +00001990#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001991 __get_db()->__invalidate_all(this);
Howard Hinnantabe26282011-09-16 17:29:17 +00001992#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001993}
1994
1995// vector<bool>
1996
1997template <class _Allocator> class vector<bool, _Allocator>;
1998
1999template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2000
2001template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002002struct __has_storage_type<vector<bool, _Allocator> >
2003{
2004 static const bool value = true;
2005};
2006
2007template <class _Allocator>
Howard Hinnant83eade62013-03-06 23:30:19 +00002008class _LIBCPP_TYPE_VIS vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002009 : private __vector_base_common<true>
2010{
2011public:
2012 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00002013 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002014 typedef _Allocator allocator_type;
2015 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002016 typedef typename __alloc_traits::size_type size_type;
2017 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantf867f632012-05-07 16:50:38 +00002018 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002019 typedef __bit_iterator<vector, false> pointer;
2020 typedef __bit_iterator<vector, true> const_pointer;
2021#ifdef _LIBCPP_DEBUG
2022 typedef __debug_iter<vector, pointer> iterator;
2023 typedef __debug_iter<vector, const_pointer> const_iterator;
2024
2025 friend class __debug_iter<vector, pointer>;
2026 friend class __debug_iter<vector, const_pointer>;
2027
2028 pair<iterator*, const_iterator*> __iterator_list_;
2029
2030 _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;}
2031 _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;}
Howard Hinnant324bb032010-08-22 00:02:43 +00002032#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002033 typedef pointer iterator;
2034 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +00002035#endif // _LIBCPP_DEBUG
Howard Hinnant0949eed2011-06-30 21:18:19 +00002036 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2037 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002038
2039private:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002040 typedef typename __alloc_traits::template
2041#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
2042 rebind_alloc<__storage_type>
2043#else
2044 rebind_alloc<__storage_type>::other
2045#endif
2046 __storage_allocator;
2047 typedef allocator_traits<__storage_allocator> __storage_traits;
2048 typedef typename __storage_traits::pointer __storage_pointer;
2049 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2050
2051 __storage_pointer __begin_;
2052 size_type __size_;
2053 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002054public:
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002055 typedef __bit_reference<vector> reference;
2056 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002057private:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002058 _LIBCPP_INLINE_VISIBILITY
2059 size_type& __cap() _NOEXCEPT
2060 {return __cap_alloc_.first();}
2061 _LIBCPP_INLINE_VISIBILITY
2062 const size_type& __cap() const _NOEXCEPT
2063 {return __cap_alloc_.first();}
2064 _LIBCPP_INLINE_VISIBILITY
2065 __storage_allocator& __alloc() _NOEXCEPT
2066 {return __cap_alloc_.second();}
2067 _LIBCPP_INLINE_VISIBILITY
2068 const __storage_allocator& __alloc() const _NOEXCEPT
2069 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070
2071 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2072
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002073 _LIBCPP_INLINE_VISIBILITY
2074 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002075 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002076 _LIBCPP_INLINE_VISIBILITY
2077 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002078 {return (__n - 1) / __bits_per_word + 1;}
2079
2080public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002081 _LIBCPP_INLINE_VISIBILITY
2082 vector()
2083 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002084 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002085 ~vector();
2086 explicit vector(size_type __n);
2087 vector(size_type __n, const value_type& __v);
2088 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2089 template <class _InputIterator>
2090 vector(_InputIterator __first, _InputIterator __last,
2091 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2092 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2093 template <class _InputIterator>
2094 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2095 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2096 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2097 template <class _ForwardIterator>
2098 vector(_ForwardIterator __first, _ForwardIterator __last,
2099 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2100 template <class _ForwardIterator>
2101 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2102 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2103
2104 vector(const vector& __v);
2105 vector(const vector& __v, const allocator_type& __a);
2106 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00002107#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002108 vector(initializer_list<value_type> __il);
2109 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00002110#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002111
Howard Hinnant73d21a42010-09-04 23:28:19 +00002112#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002113 _LIBCPP_INLINE_VISIBILITY
2114 vector(vector&& __v)
2115 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002116 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002117 _LIBCPP_INLINE_VISIBILITY
2118 vector& operator=(vector&& __v)
2119 _NOEXCEPT_(
2120 __alloc_traits::propagate_on_container_move_assignment::value &&
2121 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002122#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +00002123#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002125 vector& operator=(initializer_list<value_type> __il)
2126 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00002127#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002128
2129 template <class _InputIterator>
2130 typename enable_if
2131 <
2132 __is_input_iterator<_InputIterator>::value &&
2133 !__is_forward_iterator<_InputIterator>::value,
2134 void
2135 >::type
2136 assign(_InputIterator __first, _InputIterator __last);
2137 template <class _ForwardIterator>
2138 typename enable_if
2139 <
2140 __is_forward_iterator<_ForwardIterator>::value,
2141 void
2142 >::type
2143 assign(_ForwardIterator __first, _ForwardIterator __last);
2144
2145 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +00002146#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002147 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002148 void assign(initializer_list<value_type> __il)
2149 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002150#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002151
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002152 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002153 {return allocator_type(this->__alloc());}
2154
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002155 size_type max_size() const _NOEXCEPT;
2156 _LIBCPP_INLINE_VISIBILITY
2157 size_type capacity() const _NOEXCEPT
2158 {return __internal_cap_to_external(__cap());}
2159 _LIBCPP_INLINE_VISIBILITY
2160 size_type size() const _NOEXCEPT
2161 {return __size_;}
2162 _LIBCPP_INLINE_VISIBILITY
2163 bool empty() const _NOEXCEPT
2164 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002165 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002166 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002167
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002168 _LIBCPP_INLINE_VISIBILITY
2169 iterator begin() _NOEXCEPT
2170 {return __make_iter(0);}
2171 _LIBCPP_INLINE_VISIBILITY
2172 const_iterator begin() const _NOEXCEPT
2173 {return __make_iter(0);}
2174 _LIBCPP_INLINE_VISIBILITY
2175 iterator end() _NOEXCEPT
2176 {return __make_iter(__size_);}
2177 _LIBCPP_INLINE_VISIBILITY
2178 const_iterator end() const _NOEXCEPT
2179 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002180
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002181 _LIBCPP_INLINE_VISIBILITY
2182 reverse_iterator rbegin() _NOEXCEPT
2183 {return reverse_iterator(end());}
2184 _LIBCPP_INLINE_VISIBILITY
2185 const_reverse_iterator rbegin() const _NOEXCEPT
2186 {return const_reverse_iterator(end());}
2187 _LIBCPP_INLINE_VISIBILITY
2188 reverse_iterator rend() _NOEXCEPT
2189 {return reverse_iterator(begin());}
2190 _LIBCPP_INLINE_VISIBILITY
2191 const_reverse_iterator rend() const _NOEXCEPT
2192 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002193
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002194 _LIBCPP_INLINE_VISIBILITY
2195 const_iterator cbegin() const _NOEXCEPT
2196 {return __make_iter(0);}
2197 _LIBCPP_INLINE_VISIBILITY
2198 const_iterator cend() const _NOEXCEPT
2199 {return __make_iter(__size_);}
2200 _LIBCPP_INLINE_VISIBILITY
2201 const_reverse_iterator crbegin() const _NOEXCEPT
2202 {return rbegin();}
2203 _LIBCPP_INLINE_VISIBILITY
2204 const_reverse_iterator crend() const _NOEXCEPT
2205 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002206
2207 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2208 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2209 reference at(size_type __n);
2210 const_reference at(size_type __n) const;
2211
2212 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2213 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2214 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2215 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2216
2217 void push_back(const value_type& __x);
2218 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2219
2220 iterator insert(const_iterator __position, const value_type& __x);
2221 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2222 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2223 template <class _InputIterator>
2224 typename enable_if
2225 <
2226 __is_input_iterator <_InputIterator>::value &&
2227 !__is_forward_iterator<_InputIterator>::value,
2228 iterator
2229 >::type
2230 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2231 template <class _ForwardIterator>
2232 typename enable_if
2233 <
2234 __is_forward_iterator<_ForwardIterator>::value,
2235 iterator
2236 >::type
2237 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00002238#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002240 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2241 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002242#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002243
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002244 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002245 iterator erase(const_iterator __first, const_iterator __last);
2246
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002247 _LIBCPP_INLINE_VISIBILITY
2248 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002249
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002250 void swap(vector&)
2251 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2252 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002253
2254 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002255 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002256
2257 bool __invariants() const;
2258
2259private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002260 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002261 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002262 void deallocate() _NOEXCEPT;
2263 _LIBCPP_INLINE_VISIBILITY
2264 static size_type __align(size_type __new_size) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002265 {return __new_size + (__bits_per_word-1) & ~(__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002266 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2267 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002268 template <class _ForwardIterator>
2269 typename enable_if
2270 <
2271 __is_forward_iterator<_ForwardIterator>::value,
2272 void
2273 >::type
2274 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2275 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002276 _LIBCPP_INLINE_VISIBILITY
2277 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002278 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002279 _LIBCPP_INLINE_VISIBILITY
2280 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002281 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
2282#ifdef _LIBCPP_DEBUG
2283 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_type __pos)
2284 {return iterator(this, pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2285 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_type __pos) const
2286 {return const_iterator(this, const_pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2287 _LIBCPP_INLINE_VISIBILITY iterator __const_iterator_cast(const_iterator __p)
2288 {return iterator(this, pointer(const_cast<__storage_pointer>(__p.base().__seg_), __p.base().__ctz_));}
Howard Hinnant324bb032010-08-22 00:02:43 +00002289#else // _LIBCPP_DEBUG
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002290 _LIBCPP_INLINE_VISIBILITY
2291 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002292 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002293 _LIBCPP_INLINE_VISIBILITY
2294 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002295 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002296 _LIBCPP_INLINE_VISIBILITY
2297 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002298 {return begin() + (__p - cbegin());}
Howard Hinnant324bb032010-08-22 00:02:43 +00002299#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002300
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002302 void __copy_assign_alloc(const vector& __v)
2303 {__copy_assign_alloc(__v, integral_constant<bool,
2304 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002306 void __copy_assign_alloc(const vector& __c, true_type)
2307 {
2308 if (__alloc() != __c.__alloc())
2309 deallocate();
2310 __alloc() = __c.__alloc();
2311 }
2312
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002313 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002314 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002315 {}
2316
2317 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002318 void __move_assign(vector& __c, true_type)
2319 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002321 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002322 _NOEXCEPT_(
2323 !__storage_traits::propagate_on_container_move_assignment::value ||
2324 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002325 {__move_assign_alloc(__c, integral_constant<bool,
2326 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00002328 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002329 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002330 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002331 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002332 }
2333
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002335 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002336 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002337 {}
2338
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002340 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002341 _NOEXCEPT_(
2342 !__storage_traits::propagate_on_container_swap::value ||
2343 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002344 {__swap_alloc(__x, __y, integral_constant<bool,
2345 __storage_traits::propagate_on_container_swap::value>());}
2346
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002347 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002348 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002349 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002350 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002351 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002352 swap(__x, __y);
2353 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002354 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002355 static void __swap_alloc(__storage_allocator&, __storage_allocator&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002356 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002357 {}
2358
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002359 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002360
2361 friend class __bit_reference<vector>;
2362 friend class __bit_const_reference<vector>;
2363 friend class __bit_iterator<vector, false>;
2364 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:18 +00002365 friend struct __bit_array<vector>;
Howard Hinnant83eade62013-03-06 23:30:19 +00002366 friend struct _LIBCPP_TYPE_VIS hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002367};
2368
2369template <class _Allocator>
2370#ifndef _LIBCPP_DEBUG
2371_LIBCPP_INLINE_VISIBILITY inline
2372#endif
2373void
2374vector<bool, _Allocator>::__invalidate_all_iterators()
2375{
2376#ifdef _LIBCPP_DEBUG
2377 iterator::__remove_all(this);
2378 const_iterator::__remove_all(this);
Howard Hinnant324bb032010-08-22 00:02:43 +00002379#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002380}
2381
2382// Allocate space for __n objects
2383// throws length_error if __n > max_size()
2384// throws (probably bad_alloc) if memory run out
2385// Precondition: __begin_ == __end_ == __cap() == 0
2386// Precondition: __n > 0
2387// Postcondition: capacity() == __n
2388// Postcondition: size() == 0
2389template <class _Allocator>
2390void
2391vector<bool, _Allocator>::allocate(size_type __n)
2392{
2393 if (__n > max_size())
2394 this->__throw_length_error();
2395 __n = __external_cap_to_internal(__n);
2396 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2397 this->__size_ = 0;
2398 this->__cap() = __n;
2399}
2400
2401template <class _Allocator>
2402void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002403vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002404{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002405 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002406 {
2407 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2408 __invalidate_all_iterators();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002409 this->__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002410 this->__size_ = this->__cap() = 0;
2411 }
2412}
2413
2414template <class _Allocator>
2415typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002416vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002417{
2418 size_type __amax = __storage_traits::max_size(__alloc());
2419 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2420 if (__nmax / __bits_per_word <= __amax)
2421 return __nmax;
2422 return __internal_cap_to_external(__amax);
2423}
2424
2425// Precondition: __new_size > capacity()
2426template <class _Allocator>
2427_LIBCPP_INLINE_VISIBILITY inline
2428typename vector<bool, _Allocator>::size_type
2429vector<bool, _Allocator>::__recommend(size_type __new_size) const
2430{
2431 const size_type __ms = max_size();
2432 if (__new_size > __ms)
2433 this->__throw_length_error();
2434 const size_type __cap = capacity();
2435 if (__cap >= __ms / 2)
2436 return __ms;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002437 return _VSTD::max(2*__cap, __align(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002438}
2439
2440// Default constructs __n objects starting at __end_
2441// Precondition: __n > 0
2442// Precondition: size() + __n <= capacity()
2443// Postcondition: size() == size() + __n
2444template <class _Allocator>
2445_LIBCPP_INLINE_VISIBILITY inline
2446void
2447vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2448{
2449 size_type __old_size = this->__size_;
2450 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002451 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002452}
2453
2454template <class _Allocator>
2455template <class _ForwardIterator>
2456typename enable_if
2457<
2458 __is_forward_iterator<_ForwardIterator>::value,
2459 void
2460>::type
2461vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2462{
2463 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002464 this->__size_ += _VSTD::distance(__first, __last);
2465 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002466}
2467
2468template <class _Allocator>
2469_LIBCPP_INLINE_VISIBILITY inline
2470vector<bool, _Allocator>::vector()
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002471 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002472 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002473 __size_(0),
2474 __cap_alloc_(0)
2475{
2476}
2477
2478template <class _Allocator>
2479_LIBCPP_INLINE_VISIBILITY inline
2480vector<bool, _Allocator>::vector(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002481 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002482 __size_(0),
2483 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2484{
2485}
2486
2487template <class _Allocator>
2488vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002489 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002490 __size_(0),
2491 __cap_alloc_(0)
2492{
2493 if (__n > 0)
2494 {
2495 allocate(__n);
2496 __construct_at_end(__n, false);
2497 }
2498}
2499
2500template <class _Allocator>
2501vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002502 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002503 __size_(0),
2504 __cap_alloc_(0)
2505{
2506 if (__n > 0)
2507 {
2508 allocate(__n);
2509 __construct_at_end(__n, __x);
2510 }
2511}
2512
2513template <class _Allocator>
2514vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002515 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002516 __size_(0),
2517 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2518{
2519 if (__n > 0)
2520 {
2521 allocate(__n);
2522 __construct_at_end(__n, __x);
2523 }
2524}
2525
2526template <class _Allocator>
2527template <class _InputIterator>
2528vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2529 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2530 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002531 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002532 __size_(0),
2533 __cap_alloc_(0)
2534{
2535#ifndef _LIBCPP_NO_EXCEPTIONS
2536 try
2537 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002538#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002539 for (; __first != __last; ++__first)
2540 push_back(*__first);
2541#ifndef _LIBCPP_NO_EXCEPTIONS
2542 }
2543 catch (...)
2544 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002545 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002546 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2547 __invalidate_all_iterators();
2548 throw;
2549 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002550#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002551}
2552
2553template <class _Allocator>
2554template <class _InputIterator>
2555vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2556 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2557 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002558 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002559 __size_(0),
2560 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2561{
2562#ifndef _LIBCPP_NO_EXCEPTIONS
2563 try
2564 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002565#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002566 for (; __first != __last; ++__first)
2567 push_back(*__first);
2568#ifndef _LIBCPP_NO_EXCEPTIONS
2569 }
2570 catch (...)
2571 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002572 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002573 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2574 __invalidate_all_iterators();
2575 throw;
2576 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002577#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002578}
2579
2580template <class _Allocator>
2581template <class _ForwardIterator>
2582vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2583 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002584 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002585 __size_(0),
2586 __cap_alloc_(0)
2587{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002588 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002589 if (__n > 0)
2590 {
2591 allocate(__n);
2592 __construct_at_end(__first, __last);
2593 }
2594}
2595
2596template <class _Allocator>
2597template <class _ForwardIterator>
2598vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2599 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002600 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002601 __size_(0),
2602 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2603{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002604 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002605 if (__n > 0)
2606 {
2607 allocate(__n);
2608 __construct_at_end(__first, __last);
2609 }
2610}
2611
Howard Hinnante3e32912011-08-12 21:56:02 +00002612#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2613
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002614template <class _Allocator>
2615vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002616 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002617 __size_(0),
2618 __cap_alloc_(0)
2619{
2620 size_type __n = static_cast<size_type>(__il.size());
2621 if (__n > 0)
2622 {
2623 allocate(__n);
2624 __construct_at_end(__il.begin(), __il.end());
2625 }
2626}
2627
2628template <class _Allocator>
2629vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002630 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002631 __size_(0),
2632 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2633{
2634 size_type __n = static_cast<size_type>(__il.size());
2635 if (__n > 0)
2636 {
2637 allocate(__n);
2638 __construct_at_end(__il.begin(), __il.end());
2639 }
2640}
2641
Howard Hinnante3e32912011-08-12 21:56:02 +00002642#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2643
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002644template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002645vector<bool, _Allocator>::~vector()
2646{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002647 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002648 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2649#ifdef _LIBCPP_DEBUG
2650 __invalidate_all_iterators();
2651#endif
2652}
2653
2654template <class _Allocator>
2655vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002656 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002657 __size_(0),
2658 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2659{
2660 if (__v.size() > 0)
2661 {
2662 allocate(__v.size());
2663 __construct_at_end(__v.begin(), __v.end());
2664 }
2665}
2666
2667template <class _Allocator>
2668vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002669 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002670 __size_(0),
2671 __cap_alloc_(0, __a)
2672{
2673 if (__v.size() > 0)
2674 {
2675 allocate(__v.size());
2676 __construct_at_end(__v.begin(), __v.end());
2677 }
2678}
2679
2680template <class _Allocator>
2681vector<bool, _Allocator>&
2682vector<bool, _Allocator>::operator=(const vector& __v)
2683{
2684 if (this != &__v)
2685 {
2686 __copy_assign_alloc(__v);
2687 if (__v.__size_)
2688 {
2689 if (__v.__size_ > capacity())
2690 {
2691 deallocate();
2692 allocate(__v.__size_);
2693 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002694 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002695 }
2696 __size_ = __v.__size_;
2697 }
2698 return *this;
2699}
2700
Howard Hinnant73d21a42010-09-04 23:28:19 +00002701#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2702
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002703template <class _Allocator>
2704_LIBCPP_INLINE_VISIBILITY inline
2705vector<bool, _Allocator>::vector(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002706 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002707 : __begin_(__v.__begin_),
2708 __size_(__v.__size_),
2709 __cap_alloc_(__v.__cap_alloc_)
2710{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002711 __v.__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002712 __v.__size_ = 0;
2713 __v.__cap() = 0;
2714}
2715
2716template <class _Allocator>
2717vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002718 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002719 __size_(0),
2720 __cap_alloc_(0, __a)
2721{
2722 if (__a == allocator_type(__v.__alloc()))
2723 {
2724 this->__begin_ = __v.__begin_;
2725 this->__size_ = __v.__size_;
2726 this->__cap() = __v.__cap();
2727 __v.__begin_ = nullptr;
2728 __v.__cap() = __v.__size_ = 0;
2729 }
2730 else if (__v.size() > 0)
2731 {
2732 allocate(__v.size());
2733 __construct_at_end(__v.begin(), __v.end());
2734 }
2735}
2736
2737template <class _Allocator>
2738_LIBCPP_INLINE_VISIBILITY inline
2739vector<bool, _Allocator>&
2740vector<bool, _Allocator>::operator=(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002741 _NOEXCEPT_(
2742 __alloc_traits::propagate_on_container_move_assignment::value &&
2743 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002744{
2745 __move_assign(__v, integral_constant<bool,
2746 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002747 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002748}
2749
2750template <class _Allocator>
2751void
2752vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2753{
2754 if (__alloc() != __c.__alloc())
2755 assign(__c.begin(), __c.end());
2756 else
2757 __move_assign(__c, true_type());
2758}
2759
2760template <class _Allocator>
2761void
2762vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002763 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002764{
2765 deallocate();
2766 this->__begin_ = __c.__begin_;
2767 this->__size_ = __c.__size_;
2768 this->__cap() = __c.__cap();
2769 __move_assign_alloc(__c);
2770 __c.__begin_ = nullptr;
2771 __c.__cap() = __c.__size_ = 0;
2772}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002773
2774#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002775
2776template <class _Allocator>
2777void
2778vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2779{
2780 __size_ = 0;
2781 if (__n > 0)
2782 {
2783 size_type __c = capacity();
2784 if (__n <= __c)
2785 __size_ = __n;
2786 else
2787 {
2788 vector __v(__alloc());
2789 __v.reserve(__recommend(__n));
2790 __v.__size_ = __n;
2791 swap(__v);
2792 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002793 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002794 }
2795}
2796
2797template <class _Allocator>
2798template <class _InputIterator>
2799typename enable_if
2800<
2801 __is_input_iterator<_InputIterator>::value &&
2802 !__is_forward_iterator<_InputIterator>::value,
2803 void
2804>::type
2805vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2806{
2807 clear();
2808 for (; __first != __last; ++__first)
2809 push_back(*__first);
2810}
2811
2812template <class _Allocator>
2813template <class _ForwardIterator>
2814typename enable_if
2815<
2816 __is_forward_iterator<_ForwardIterator>::value,
2817 void
2818>::type
2819vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2820{
2821 clear();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002822 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002823 if (__n)
2824 {
2825 if (__n > capacity())
2826 {
2827 deallocate();
2828 allocate(__n);
2829 }
2830 __construct_at_end(__first, __last);
2831 }
2832}
2833
2834template <class _Allocator>
2835void
2836vector<bool, _Allocator>::reserve(size_type __n)
2837{
2838 if (__n > capacity())
2839 {
2840 vector __v(this->__alloc());
2841 __v.allocate(__n);
2842 __v.__construct_at_end(this->begin(), this->end());
2843 swap(__v);
2844 __invalidate_all_iterators();
2845 }
2846}
2847
2848template <class _Allocator>
2849void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002850vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002851{
2852 if (__external_cap_to_internal(size()) > __cap())
2853 {
2854#ifndef _LIBCPP_NO_EXCEPTIONS
2855 try
2856 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002857#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002858 vector(*this, allocator_type(__alloc())).swap(*this);
2859#ifndef _LIBCPP_NO_EXCEPTIONS
2860 }
2861 catch (...)
2862 {
2863 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002864#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002865 }
2866}
2867
2868template <class _Allocator>
2869typename vector<bool, _Allocator>::reference
2870vector<bool, _Allocator>::at(size_type __n)
2871{
2872 if (__n >= size())
2873 this->__throw_out_of_range();
2874 return (*this)[__n];
2875}
2876
2877template <class _Allocator>
2878typename vector<bool, _Allocator>::const_reference
2879vector<bool, _Allocator>::at(size_type __n) const
2880{
2881 if (__n >= size())
2882 this->__throw_out_of_range();
2883 return (*this)[__n];
2884}
2885
2886template <class _Allocator>
2887void
2888vector<bool, _Allocator>::push_back(const value_type& __x)
2889{
2890 if (this->__size_ == this->capacity())
2891 reserve(__recommend(this->__size_ + 1));
2892 ++this->__size_;
2893 back() = __x;
2894}
2895
2896template <class _Allocator>
2897typename vector<bool, _Allocator>::iterator
2898vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2899{
2900 iterator __r;
2901 if (size() < capacity())
2902 {
2903 const_iterator __old_end = end();
2904 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002905 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002906 __r = __const_iterator_cast(__position);
2907 }
2908 else
2909 {
2910 vector __v(__alloc());
2911 __v.reserve(__recommend(__size_ + 1));
2912 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002913 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2914 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002915 swap(__v);
2916 }
2917 *__r = __x;
2918 return __r;
2919}
2920
2921template <class _Allocator>
2922typename vector<bool, _Allocator>::iterator
2923vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
2924{
2925 iterator __r;
2926 size_type __c = capacity();
2927 if (__n <= __c && size() <= __c - __n)
2928 {
2929 const_iterator __old_end = end();
2930 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002931 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002932 __r = __const_iterator_cast(__position);
2933 }
2934 else
2935 {
2936 vector __v(__alloc());
2937 __v.reserve(__recommend(__size_ + __n));
2938 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002939 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2940 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002941 swap(__v);
2942 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002943 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002944 return __r;
2945}
2946
2947template <class _Allocator>
2948template <class _InputIterator>
2949typename enable_if
2950<
2951 __is_input_iterator <_InputIterator>::value &&
2952 !__is_forward_iterator<_InputIterator>::value,
2953 typename vector<bool, _Allocator>::iterator
2954>::type
2955vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
2956{
2957 difference_type __off = __position - begin();
2958 iterator __p = __const_iterator_cast(__position);
2959 iterator __old_end = end();
2960 for (; size() != capacity() && __first != __last; ++__first)
2961 {
2962 ++this->__size_;
2963 back() = *__first;
2964 }
2965 vector __v(__alloc());
2966 if (__first != __last)
2967 {
2968#ifndef _LIBCPP_NO_EXCEPTIONS
2969 try
2970 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002971#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002972 __v.assign(__first, __last);
2973 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
2974 difference_type __old_p = __p - begin();
2975 reserve(__recommend(size() + __v.size()));
2976 __p = begin() + __old_p;
2977 __old_end = begin() + __old_size;
2978#ifndef _LIBCPP_NO_EXCEPTIONS
2979 }
2980 catch (...)
2981 {
2982 erase(__old_end, end());
2983 throw;
2984 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002985#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002986 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002987 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002988 insert(__p, __v.begin(), __v.end());
2989 return begin() + __off;
2990}
2991
2992template <class _Allocator>
2993template <class _ForwardIterator>
2994typename enable_if
2995<
2996 __is_forward_iterator<_ForwardIterator>::value,
2997 typename vector<bool, _Allocator>::iterator
2998>::type
2999vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3000{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003001 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003002 iterator __r;
3003 size_type __c = capacity();
3004 if (__n <= __c && size() <= __c - __n)
3005 {
3006 const_iterator __old_end = end();
3007 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003008 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003009 __r = __const_iterator_cast(__position);
3010 }
3011 else
3012 {
3013 vector __v(__alloc());
3014 __v.reserve(__recommend(__size_ + __n));
3015 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003016 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3017 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003018 swap(__v);
3019 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003020 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003021 return __r;
3022}
3023
3024template <class _Allocator>
3025_LIBCPP_INLINE_VISIBILITY inline
3026typename vector<bool, _Allocator>::iterator
3027vector<bool, _Allocator>::erase(const_iterator __position)
3028{
3029 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003030 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003031 --__size_;
3032 return __r;
3033}
3034
3035template <class _Allocator>
3036typename vector<bool, _Allocator>::iterator
3037vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3038{
3039 iterator __r = __const_iterator_cast(__first);
3040 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003041 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003042 __size_ -= __d;
3043 return __r;
3044}
3045
3046template <class _Allocator>
3047void
3048vector<bool, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003049 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3050 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003051{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003052 _VSTD::swap(this->__begin_, __x.__begin_);
3053 _VSTD::swap(this->__size_, __x.__size_);
3054 _VSTD::swap(this->__cap(), __x.__cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003055 __swap_alloc(this->__alloc(), __x.__alloc());
3056#ifdef _LIBCPP_DEBUG
3057 iterator::swap(this, &__x);
3058 const_iterator::swap(this, &__x);
Howard Hinnant324bb032010-08-22 00:02:43 +00003059#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003060}
3061
Howard Hinnant324bb032010-08-22 00:02:43 +00003062template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003063void
3064vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3065{
3066 size_type __cs = size();
3067 if (__cs < __sz)
3068 {
3069 iterator __r;
3070 size_type __c = capacity();
3071 size_type __n = __sz - __cs;
3072 if (__n <= __c && __cs <= __c - __n)
3073 {
3074 __r = end();
3075 __size_ += __n;
3076 }
3077 else
3078 {
3079 vector __v(__alloc());
3080 __v.reserve(__recommend(__size_ + __n));
3081 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003082 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003083 swap(__v);
3084 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003085 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003086 }
3087 else
3088 __size_ = __sz;
3089}
3090
Howard Hinnant324bb032010-08-22 00:02:43 +00003091template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003092void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003093vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003094{
3095 // do middle whole words
3096 size_type __n = __size_;
3097 __storage_pointer __p = __begin_;
3098 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3099 *__p = ~*__p;
3100 // do last partial word
3101 if (__n > 0)
3102 {
3103 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3104 __storage_type __b = *__p & __m;
3105 *__p &= ~__m;
3106 *__p |= ~__b & __m;
3107 }
3108}
3109
Howard Hinnant324bb032010-08-22 00:02:43 +00003110template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003111bool
3112vector<bool, _Allocator>::__invariants() const
3113{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00003114 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003115 {
3116 if (this->__size_ != 0 || this->__cap() != 0)
3117 return false;
3118 }
3119 else
3120 {
3121 if (this->__cap() == 0)
3122 return false;
3123 if (this->__size_ > this->capacity())
3124 return false;
3125 }
3126 return true;
3127}
3128
Howard Hinnant324bb032010-08-22 00:02:43 +00003129template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003130size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003131vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003132{
3133 size_t __h = 0;
3134 // do middle whole words
3135 size_type __n = __size_;
3136 __storage_pointer __p = __begin_;
3137 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3138 __h ^= *__p;
3139 // do last partial word
3140 if (__n > 0)
3141 {
3142 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3143 __h ^= *__p & __m;
3144 }
3145 return __h;
3146}
3147
3148template <class _Allocator>
Howard Hinnant83eade62013-03-06 23:30:19 +00003149struct _LIBCPP_TYPE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003150 : public unary_function<vector<bool, _Allocator>, size_t>
3151{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00003152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003153 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003154 {return __vec.__hash_code();}
3155};
3156
3157template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003158_LIBCPP_INLINE_VISIBILITY inline
3159bool
3160operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3161{
3162 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003163 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003164}
3165
3166template <class _Tp, class _Allocator>
3167_LIBCPP_INLINE_VISIBILITY inline
3168bool
3169operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3170{
3171 return !(__x == __y);
3172}
3173
3174template <class _Tp, class _Allocator>
3175_LIBCPP_INLINE_VISIBILITY inline
3176bool
3177operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3178{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003179 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003180}
3181
3182template <class _Tp, class _Allocator>
3183_LIBCPP_INLINE_VISIBILITY inline
3184bool
3185operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3186{
3187 return __y < __x;
3188}
3189
3190template <class _Tp, class _Allocator>
3191_LIBCPP_INLINE_VISIBILITY inline
3192bool
3193operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3194{
3195 return !(__x < __y);
3196}
3197
3198template <class _Tp, class _Allocator>
3199_LIBCPP_INLINE_VISIBILITY inline
3200bool
3201operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3202{
3203 return !(__y < __x);
3204}
3205
3206template <class _Tp, class _Allocator>
3207_LIBCPP_INLINE_VISIBILITY inline
3208void
3209swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003210 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003211{
3212 __x.swap(__y);
3213}
3214
3215_LIBCPP_END_NAMESPACE_STD
3216
3217#endif // _LIBCPP_VECTOR