blob: 0758f75bf3b7163b02a49e5cc134abb18c0dc548 [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 Hinnant8b00e6c2013-08-02 00:26:35 +0000275#ifdef _LIBCPP_DEBUG2
276# include <__debug>
277#else
278# define _LIBCPP_ASSERT(x, m) ((void)0)
279#endif
280
Howard Hinnant08e17472011-10-17 20:05:10 +0000281#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000282#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000283#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000284
285_LIBCPP_BEGIN_NAMESPACE_STD
286
287template <bool>
288class __vector_base_common
289{
290protected:
291 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
292 void __throw_length_error() const;
293 void __throw_out_of_range() const;
294};
295
296template <bool __b>
297void
298__vector_base_common<__b>::__throw_length_error() const
299{
300#ifndef _LIBCPP_NO_EXCEPTIONS
301 throw length_error("vector");
302#else
303 assert(!"vector length_error");
304#endif
305}
306
307template <bool __b>
308void
309__vector_base_common<__b>::__throw_out_of_range() const
310{
311#ifndef _LIBCPP_NO_EXCEPTIONS
312 throw out_of_range("vector");
313#else
314 assert(!"vector out_of_range");
315#endif
316}
317
Howard Hinnante9df0a52013-08-01 18:17:34 +0000318#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000319#pragma warning( push )
320#pragma warning( disable: 4231 )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000321#endif // _LIBCPP_MSVC
Howard Hinnantff926772012-11-06 21:08:48 +0000322_LIBCPP_EXTERN_TEMPLATE(class __vector_base_common<true>)
Howard Hinnante9df0a52013-08-01 18:17:34 +0000323#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000324#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000325#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000326
327template <class _Tp, class _Allocator>
328class __vector_base
329 : protected __vector_base_common<true>
330{
331protected:
Howard Hinnant324bb032010-08-22 00:02:43 +0000332 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000333 typedef _Allocator allocator_type;
334 typedef allocator_traits<allocator_type> __alloc_traits;
335 typedef value_type& reference;
336 typedef const value_type& const_reference;
337 typedef typename __alloc_traits::size_type size_type;
338 typedef typename __alloc_traits::difference_type difference_type;
339 typedef typename __alloc_traits::pointer pointer;
340 typedef typename __alloc_traits::const_pointer const_pointer;
341 typedef pointer iterator;
342 typedef const_pointer const_iterator;
343
344 pointer __begin_;
345 pointer __end_;
346 __compressed_pair<pointer, allocator_type> __end_cap_;
347
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000348 _LIBCPP_INLINE_VISIBILITY
349 allocator_type& __alloc() _NOEXCEPT
350 {return __end_cap_.second();}
351 _LIBCPP_INLINE_VISIBILITY
352 const allocator_type& __alloc() const _NOEXCEPT
353 {return __end_cap_.second();}
354 _LIBCPP_INLINE_VISIBILITY
355 pointer& __end_cap() _NOEXCEPT
356 {return __end_cap_.first();}
357 _LIBCPP_INLINE_VISIBILITY
358 const pointer& __end_cap() const _NOEXCEPT
359 {return __end_cap_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000360
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000361 _LIBCPP_INLINE_VISIBILITY
362 __vector_base()
363 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000364 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000365 ~__vector_base();
366
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000367 _LIBCPP_INLINE_VISIBILITY
368 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
369 _LIBCPP_INLINE_VISIBILITY
370 size_type capacity() const _NOEXCEPT
371 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000372
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000373 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000374 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377 void __copy_assign_alloc(const __vector_base& __c)
378 {__copy_assign_alloc(__c, integral_constant<bool,
379 __alloc_traits::propagate_on_container_copy_assignment::value>());}
380
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000382 void __move_assign_alloc(__vector_base& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000383 _NOEXCEPT_(
384 !__alloc_traits::propagate_on_container_move_assignment::value ||
385 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000386 {__move_assign_alloc(__c, integral_constant<bool,
387 __alloc_traits::propagate_on_container_move_assignment::value>());}
388
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000390 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000391 _NOEXCEPT_(
392 !__alloc_traits::propagate_on_container_swap::value ||
393 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000394 {__swap_alloc(__x, __y, integral_constant<bool,
395 __alloc_traits::propagate_on_container_swap::value>());}
396private:
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000398 void __copy_assign_alloc(const __vector_base& __c, true_type)
399 {
400 if (__alloc() != __c.__alloc())
401 {
402 clear();
403 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
404 __begin_ = __end_ = __end_cap() = nullptr;
405 }
406 __alloc() = __c.__alloc();
407 }
408
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000410 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000411 {}
412
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000414 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000415 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000416 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000417 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000418 }
419
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000421 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000422 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000423 {}
424
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000426 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000427 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000428 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000429 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430 swap(__x, __y);
431 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000433 static void __swap_alloc(allocator_type&, allocator_type&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000434 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000435 {}
436};
437
438template <class _Tp, class _Allocator>
439_LIBCPP_INLINE_VISIBILITY inline
440void
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000441__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000442{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000443 while (__new_last != __end_)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000444 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000445}
446
447template <class _Tp, class _Allocator>
448_LIBCPP_INLINE_VISIBILITY inline
449__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000450 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000451 : __begin_(nullptr),
452 __end_(nullptr),
453 __end_cap_(nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000454{
455}
456
457template <class _Tp, class _Allocator>
458_LIBCPP_INLINE_VISIBILITY inline
459__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000460 : __begin_(nullptr),
461 __end_(nullptr),
462 __end_cap_(nullptr, __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000463{
464}
465
466template <class _Tp, class _Allocator>
467__vector_base<_Tp, _Allocator>::~__vector_base()
468{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000469 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000470 {
471 clear();
472 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
473 }
474}
475
476template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnant83eade62013-03-06 23:30:19 +0000477class _LIBCPP_TYPE_VIS vector
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000478 : private __vector_base<_Tp, _Allocator>
479{
480private:
481 typedef __vector_base<_Tp, _Allocator> __base;
Howard Hinnant324bb032010-08-22 00:02:43 +0000482public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000483 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +0000484 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000485 typedef _Allocator allocator_type;
486 typedef typename __base::__alloc_traits __alloc_traits;
487 typedef typename __base::reference reference;
488 typedef typename __base::const_reference const_reference;
489 typedef typename __base::size_type size_type;
490 typedef typename __base::difference_type difference_type;
491 typedef typename __base::pointer pointer;
492 typedef typename __base::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000493 typedef __wrap_iter<pointer> iterator;
494 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000495 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
496 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000497
Howard Hinnant02d5e182013-03-26 19:04:56 +0000498 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
499 "Allocator::value_type must be same type as value_type");
500
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000501 _LIBCPP_INLINE_VISIBILITY
502 vector()
503 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant7a563db2011-09-14 18:33:51 +0000504 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000505#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000506 __get_db()->__insert_c(this);
507#endif
508 }
509 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
510 : __base(__a)
511 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000512#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000513 __get_db()->__insert_c(this);
514#endif
515 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000516 explicit vector(size_type __n);
517 vector(size_type __n, const_reference __x);
518 vector(size_type __n, const_reference __x, const allocator_type& __a);
519 template <class _InputIterator>
520 vector(_InputIterator __first, _InputIterator __last,
521 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000522 !__is_forward_iterator<_InputIterator>::value &&
523 is_constructible<
524 value_type,
525 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000526 template <class _InputIterator>
527 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
528 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000529 !__is_forward_iterator<_InputIterator>::value &&
530 is_constructible<
531 value_type,
532 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000533 template <class _ForwardIterator>
534 vector(_ForwardIterator __first, _ForwardIterator __last,
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 Hinnantbc8d3f92010-05-11 19:42:16 +0000539 template <class _ForwardIterator>
540 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000541 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
542 is_constructible<
543 value_type,
544 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000545#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000547 vector(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000549 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000550#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantabe26282011-09-16 17:29:17 +0000551#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7a563db2011-09-14 18:33:51 +0000553 ~vector()
554 {
555 __get_db()->__erase_c(this);
556 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000557#endif
558
559 vector(const vector& __x);
560 vector(const vector& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000562 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000563#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000565 vector(vector&& __x)
566 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000568 vector(vector&& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000570 vector& operator=(vector&& __x)
571 _NOEXCEPT_(
572 __alloc_traits::propagate_on_container_move_assignment::value &&
573 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000574#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000575#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000577 vector& operator=(initializer_list<value_type> __il)
578 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000579#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000580
581 template <class _InputIterator>
582 typename enable_if
583 <
584 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000585 !__is_forward_iterator<_InputIterator>::value &&
586 is_constructible<
587 value_type,
588 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000589 void
590 >::type
591 assign(_InputIterator __first, _InputIterator __last);
592 template <class _ForwardIterator>
593 typename enable_if
594 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000595 __is_forward_iterator<_ForwardIterator>::value &&
596 is_constructible<
597 value_type,
598 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000599 void
600 >::type
601 assign(_ForwardIterator __first, _ForwardIterator __last);
602
603 void assign(size_type __n, const_reference __u);
Howard Hinnante3e32912011-08-12 21:56:02 +0000604#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000605 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000606 void assign(initializer_list<value_type> __il)
607 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000608#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000609
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000610 _LIBCPP_INLINE_VISIBILITY
611 allocator_type get_allocator() const _NOEXCEPT
612 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000613
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000614 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
615 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
616 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
617 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000618
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000619 _LIBCPP_INLINE_VISIBILITY
620 reverse_iterator rbegin() _NOEXCEPT
621 {return reverse_iterator(end());}
622 _LIBCPP_INLINE_VISIBILITY
623 const_reverse_iterator rbegin() const _NOEXCEPT
624 {return const_reverse_iterator(end());}
625 _LIBCPP_INLINE_VISIBILITY
626 reverse_iterator rend() _NOEXCEPT
627 {return reverse_iterator(begin());}
628 _LIBCPP_INLINE_VISIBILITY
629 const_reverse_iterator rend() const _NOEXCEPT
630 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000631
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000632 _LIBCPP_INLINE_VISIBILITY
633 const_iterator cbegin() const _NOEXCEPT
634 {return begin();}
635 _LIBCPP_INLINE_VISIBILITY
636 const_iterator cend() const _NOEXCEPT
637 {return end();}
638 _LIBCPP_INLINE_VISIBILITY
639 const_reverse_iterator crbegin() const _NOEXCEPT
640 {return rbegin();}
641 _LIBCPP_INLINE_VISIBILITY
642 const_reverse_iterator crend() const _NOEXCEPT
643 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000644
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000645 _LIBCPP_INLINE_VISIBILITY
646 size_type size() const _NOEXCEPT
647 {return static_cast<size_type>(this->__end_ - this->__begin_);}
648 _LIBCPP_INLINE_VISIBILITY
649 size_type capacity() const _NOEXCEPT
650 {return __base::capacity();}
651 _LIBCPP_INLINE_VISIBILITY
652 bool empty() const _NOEXCEPT
653 {return this->__begin_ == this->__end_;}
654 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000655 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000656 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000657
658 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
659 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
660 reference at(size_type __n);
661 const_reference at(size_type __n) const;
662
Howard Hinnant7a563db2011-09-14 18:33:51 +0000663 _LIBCPP_INLINE_VISIBILITY reference front()
664 {
665 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
666 return *this->__begin_;
667 }
668 _LIBCPP_INLINE_VISIBILITY const_reference front() const
669 {
670 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
671 return *this->__begin_;
672 }
673 _LIBCPP_INLINE_VISIBILITY reference back()
674 {
675 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
676 return *(this->__end_ - 1);
677 }
678 _LIBCPP_INLINE_VISIBILITY const_reference back() const
679 {
680 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
681 return *(this->__end_ - 1);
682 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000683
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000684 _LIBCPP_INLINE_VISIBILITY
685 value_type* data() _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000686 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000687 _LIBCPP_INLINE_VISIBILITY
688 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000689 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000690
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000691 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000692#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000693 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000694#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000695 template <class... _Args>
696 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000697#endif // _LIBCPP_HAS_NO_VARIADICS
698#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000699 void pop_back();
700
701 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000702#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000703 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000704#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000705 template <class... _Args>
706 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000707#endif // _LIBCPP_HAS_NO_VARIADICS
708#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000709 iterator insert(const_iterator __position, size_type __n, const_reference __x);
710 template <class _InputIterator>
711 typename enable_if
712 <
713 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000714 !__is_forward_iterator<_InputIterator>::value &&
715 is_constructible<
716 value_type,
717 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000718 iterator
719 >::type
720 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
721 template <class _ForwardIterator>
722 typename enable_if
723 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000724 __is_forward_iterator<_ForwardIterator>::value &&
725 is_constructible<
726 value_type,
727 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000728 iterator
729 >::type
730 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +0000731#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000733 iterator insert(const_iterator __position, initializer_list<value_type> __il)
734 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000735#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000736
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000737 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000738 iterator erase(const_iterator __first, const_iterator __last);
739
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000740 _LIBCPP_INLINE_VISIBILITY
741 void clear() _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000742 {
743 __base::clear();
744 __invalidate_all_iterators();
745 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000746
747 void resize(size_type __sz);
748 void resize(size_type __sz, const_reference __x);
749
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000750 void swap(vector&)
751 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
752 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000753
754 bool __invariants() const;
755
Howard Hinnantabe26282011-09-16 17:29:17 +0000756#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000757
758 bool __dereferenceable(const const_iterator* __i) const;
759 bool __decrementable(const const_iterator* __i) const;
760 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
761 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
762
Howard Hinnantabe26282011-09-16 17:29:17 +0000763#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000764
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000766 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000767 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000768 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000769 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31 +0000770 void __construct_at_end(size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000771 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000772 template <class _ForwardIterator>
773 typename enable_if
774 <
775 __is_forward_iterator<_ForwardIterator>::value,
776 void
777 >::type
778 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
779 void __move_construct_at_end(pointer __first, pointer __last);
780 void __append(size_type __n);
781 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000783 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000785 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000786 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
787 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
788 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000789 void __move_assign(vector& __c, true_type)
790 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000791 void __move_assign(vector& __c, false_type);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000792 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000793 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000794 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000795#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000796 __c_node* __c = __get_db()->__find_c_and_lock(this);
797 for (__i_node** __p = __c->end_; __p != __c->beg_; )
798 {
799 --__p;
800 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
801 if (__i->base() > __new_last)
802 {
803 (*__p)->__c_ = nullptr;
804 if (--__c->end_ != __p)
805 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
806 }
807 }
808 __get_db()->unlock();
809#endif
810 __base::__destruct_at_end(__new_last);
811 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000812 template <class _Up>
813 void
814#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
815 __push_back_slow_path(_Up&& __x);
816#else
817 __push_back_slow_path(_Up& __x);
818#endif
Howard Hinnant0438ea22012-02-26 15:30:12 +0000819#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
820 template <class... _Args>
821 void
822 __emplace_back_slow_path(_Args&&... __args);
823#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000824};
825
826template <class _Tp, class _Allocator>
827void
828vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
829{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000830 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000831 _VSTD::swap(this->__begin_, __v.__begin_);
832 _VSTD::swap(this->__end_, __v.__end_);
833 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000834 __v.__first_ = __v.__begin_;
835 __invalidate_all_iterators();
836}
837
838template <class _Tp, class _Allocator>
839typename vector<_Tp, _Allocator>::pointer
840vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
841{
842 pointer __r = __v.__begin_;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000843 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
844 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000845 _VSTD::swap(this->__begin_, __v.__begin_);
846 _VSTD::swap(this->__end_, __v.__end_);
847 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000848 __v.__first_ = __v.__begin_;
849 __invalidate_all_iterators();
850 return __r;
851}
852
853// Allocate space for __n objects
854// throws length_error if __n > max_size()
855// throws (probably bad_alloc) if memory run out
856// Precondition: __begin_ == __end_ == __end_cap() == 0
857// Precondition: __n > 0
858// Postcondition: capacity() == __n
859// Postcondition: size() == 0
860template <class _Tp, class _Allocator>
861void
862vector<_Tp, _Allocator>::allocate(size_type __n)
863{
864 if (__n > max_size())
865 this->__throw_length_error();
866 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
867 this->__end_cap() = this->__begin_ + __n;
868}
869
870template <class _Tp, class _Allocator>
871void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000872vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000873{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000874 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000875 {
876 clear();
877 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000878 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000879 }
880}
881
882template <class _Tp, class _Allocator>
883typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000884vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000885{
Sean Hunt110b8bf2011-07-29 23:31:58 +0000886 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 +0000887}
888
889// Precondition: __new_size > capacity()
890template <class _Tp, class _Allocator>
891_LIBCPP_INLINE_VISIBILITY inline
892typename vector<_Tp, _Allocator>::size_type
893vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
894{
895 const size_type __ms = max_size();
896 if (__new_size > __ms)
897 this->__throw_length_error();
898 const size_type __cap = capacity();
899 if (__cap >= __ms / 2)
900 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58 +0000901 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000902}
903
904// Default constructs __n objects starting at __end_
905// throws if construction throws
906// Precondition: __n > 0
907// Precondition: size() + __n <= capacity()
908// Postcondition: size() == size() + __n
909template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000910void
911vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
912{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000913 allocator_type& __a = this->__alloc();
914 do
915 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000916 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000917 ++this->__end_;
918 --__n;
919 } while (__n > 0);
920}
921
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000922// Copy constructs __n objects starting at __end_ from __x
923// throws if construction throws
924// Precondition: __n > 0
925// Precondition: size() + __n <= capacity()
926// Postcondition: size() == old size() + __n
927// Postcondition: [i] == __x for all i in [size() - __n, __n)
928template <class _Tp, class _Allocator>
929_LIBCPP_INLINE_VISIBILITY inline
930void
931vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
932{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000933 allocator_type& __a = this->__alloc();
934 do
935 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000936 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000937 ++this->__end_;
938 --__n;
939 } while (__n > 0);
940}
941
942template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000943template <class _ForwardIterator>
944typename enable_if
945<
946 __is_forward_iterator<_ForwardIterator>::value,
947 void
948>::type
949vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
950{
951 allocator_type& __a = this->__alloc();
952 for (; __first != __last; ++__first)
953 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000954 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000955 ++this->__end_;
956 }
957}
958
959template <class _Tp, class _Allocator>
960void
961vector<_Tp, _Allocator>::__move_construct_at_end(pointer __first, pointer __last)
962{
963 allocator_type& __a = this->__alloc();
964 for (; __first != __last; ++__first)
965 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000966 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
967 _VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000968 ++this->__end_;
969 }
970}
971
972// Default constructs __n objects starting at __end_
973// throws if construction throws
974// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000975// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000976template <class _Tp, class _Allocator>
977void
978vector<_Tp, _Allocator>::__append(size_type __n)
979{
980 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
981 this->__construct_at_end(__n);
982 else
983 {
984 allocator_type& __a = this->__alloc();
985 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
986 __v.__construct_at_end(__n);
987 __swap_out_circular_buffer(__v);
988 }
989}
990
991// Default constructs __n objects starting at __end_
992// throws if construction throws
993// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000994// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000995template <class _Tp, class _Allocator>
996void
997vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
998{
999 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1000 this->__construct_at_end(__n, __x);
1001 else
1002 {
1003 allocator_type& __a = this->__alloc();
1004 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1005 __v.__construct_at_end(__n, __x);
1006 __swap_out_circular_buffer(__v);
1007 }
1008}
1009
1010template <class _Tp, class _Allocator>
1011vector<_Tp, _Allocator>::vector(size_type __n)
1012{
Howard Hinnant0442b122011-09-16 18:41:29 +00001013#if _LIBCPP_DEBUG_LEVEL >= 2
1014 __get_db()->__insert_c(this);
1015#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001016 if (__n > 0)
1017 {
1018 allocate(__n);
1019 __construct_at_end(__n);
1020 }
1021}
1022
1023template <class _Tp, class _Allocator>
1024vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1025{
Howard Hinnant0442b122011-09-16 18:41:29 +00001026#if _LIBCPP_DEBUG_LEVEL >= 2
1027 __get_db()->__insert_c(this);
1028#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001029 if (__n > 0)
1030 {
1031 allocate(__n);
1032 __construct_at_end(__n, __x);
1033 }
1034}
1035
1036template <class _Tp, class _Allocator>
1037vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1038 : __base(__a)
1039{
Howard Hinnant0442b122011-09-16 18:41:29 +00001040#if _LIBCPP_DEBUG_LEVEL >= 2
1041 __get_db()->__insert_c(this);
1042#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001043 if (__n > 0)
1044 {
1045 allocate(__n);
1046 __construct_at_end(__n, __x);
1047 }
1048}
1049
1050template <class _Tp, class _Allocator>
1051template <class _InputIterator>
1052vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
1053 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001054 !__is_forward_iterator<_InputIterator>::value &&
1055 is_constructible<
1056 value_type,
1057 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001058{
Howard Hinnantabe26282011-09-16 17:29:17 +00001059#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001060 __get_db()->__insert_c(this);
1061#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001062 for (; __first != __last; ++__first)
1063 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001064}
1065
1066template <class _Tp, class _Allocator>
1067template <class _InputIterator>
1068vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1069 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001070 !__is_forward_iterator<_InputIterator>::value &&
1071 is_constructible<
1072 value_type,
1073 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074 : __base(__a)
1075{
Howard Hinnantabe26282011-09-16 17:29:17 +00001076#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001077 __get_db()->__insert_c(this);
1078#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001079 for (; __first != __last; ++__first)
1080 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001081}
1082
1083template <class _Tp, class _Allocator>
1084template <class _ForwardIterator>
1085vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001086 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1087 is_constructible<
1088 value_type,
1089 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001090{
Howard Hinnant0442b122011-09-16 18:41:29 +00001091#if _LIBCPP_DEBUG_LEVEL >= 2
1092 __get_db()->__insert_c(this);
1093#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001094 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001095 if (__n > 0)
1096 {
1097 allocate(__n);
1098 __construct_at_end(__first, __last);
1099 }
1100}
1101
1102template <class _Tp, class _Allocator>
1103template <class _ForwardIterator>
1104vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001105 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1106 is_constructible<
1107 value_type,
1108 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001109 : __base(__a)
1110{
Howard Hinnant0442b122011-09-16 18:41:29 +00001111#if _LIBCPP_DEBUG_LEVEL >= 2
1112 __get_db()->__insert_c(this);
1113#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001114 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001115 if (__n > 0)
1116 {
1117 allocate(__n);
1118 __construct_at_end(__first, __last);
1119 }
1120}
1121
1122template <class _Tp, class _Allocator>
1123vector<_Tp, _Allocator>::vector(const vector& __x)
1124 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1125{
Howard Hinnant0442b122011-09-16 18:41:29 +00001126#if _LIBCPP_DEBUG_LEVEL >= 2
1127 __get_db()->__insert_c(this);
1128#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001129 size_type __n = __x.size();
1130 if (__n > 0)
1131 {
1132 allocate(__n);
1133 __construct_at_end(__x.__begin_, __x.__end_);
1134 }
1135}
1136
1137template <class _Tp, class _Allocator>
1138vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1139 : __base(__a)
1140{
Howard Hinnant0442b122011-09-16 18:41:29 +00001141#if _LIBCPP_DEBUG_LEVEL >= 2
1142 __get_db()->__insert_c(this);
1143#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001144 size_type __n = __x.size();
1145 if (__n > 0)
1146 {
1147 allocate(__n);
1148 __construct_at_end(__x.__begin_, __x.__end_);
1149 }
1150}
1151
Howard Hinnant73d21a42010-09-04 23:28:19 +00001152#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001153
1154template <class _Tp, class _Allocator>
1155_LIBCPP_INLINE_VISIBILITY inline
1156vector<_Tp, _Allocator>::vector(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001157 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001158 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001159{
Howard Hinnantabe26282011-09-16 17:29:17 +00001160#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001161 __get_db()->__insert_c(this);
Howard Hinnante6125bd2011-09-19 16:34:29 +00001162 __get_db()->swap(this, &__x);
Howard Hinnant7a563db2011-09-14 18:33:51 +00001163#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001164 this->__begin_ = __x.__begin_;
1165 this->__end_ = __x.__end_;
1166 this->__end_cap() = __x.__end_cap();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001167 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001168}
1169
1170template <class _Tp, class _Allocator>
1171_LIBCPP_INLINE_VISIBILITY inline
1172vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1173 : __base(__a)
1174{
Howard Hinnant0442b122011-09-16 18:41:29 +00001175#if _LIBCPP_DEBUG_LEVEL >= 2
1176 __get_db()->__insert_c(this);
1177#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001178 if (__a == __x.__alloc())
1179 {
1180 this->__begin_ = __x.__begin_;
1181 this->__end_ = __x.__end_;
1182 this->__end_cap() = __x.__end_cap();
1183 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001184#if _LIBCPP_DEBUG_LEVEL >= 2
1185 __get_db()->swap(this, &__x);
1186#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001187 }
1188 else
1189 {
Howard Hinnant99968442011-11-29 18:15:50 +00001190 typedef move_iterator<iterator> _Ip;
1191 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001192 }
1193}
1194
Howard Hinnante3e32912011-08-12 21:56:02 +00001195#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1196
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001197template <class _Tp, class _Allocator>
1198_LIBCPP_INLINE_VISIBILITY inline
1199vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1200{
Howard Hinnant0442b122011-09-16 18:41:29 +00001201#if _LIBCPP_DEBUG_LEVEL >= 2
1202 __get_db()->__insert_c(this);
1203#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001204 if (__il.size() > 0)
1205 {
1206 allocate(__il.size());
1207 __construct_at_end(__il.begin(), __il.end());
1208 }
1209}
1210
1211template <class _Tp, class _Allocator>
1212_LIBCPP_INLINE_VISIBILITY inline
1213vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1214 : __base(__a)
1215{
Howard Hinnant0442b122011-09-16 18:41:29 +00001216#if _LIBCPP_DEBUG_LEVEL >= 2
1217 __get_db()->__insert_c(this);
1218#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001219 if (__il.size() > 0)
1220 {
1221 allocate(__il.size());
1222 __construct_at_end(__il.begin(), __il.end());
1223 }
1224}
1225
Howard Hinnante3e32912011-08-12 21:56:02 +00001226#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1227
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001228template <class _Tp, class _Allocator>
1229_LIBCPP_INLINE_VISIBILITY inline
1230vector<_Tp, _Allocator>&
1231vector<_Tp, _Allocator>::operator=(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001232 _NOEXCEPT_(
1233 __alloc_traits::propagate_on_container_move_assignment::value &&
1234 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001235{
1236 __move_assign(__x, integral_constant<bool,
1237 __alloc_traits::propagate_on_container_move_assignment::value>());
1238 return *this;
1239}
1240
1241template <class _Tp, class _Allocator>
1242void
1243vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1244{
1245 if (__base::__alloc() != __c.__alloc())
1246 {
Howard Hinnant99968442011-11-29 18:15:50 +00001247 typedef move_iterator<iterator> _Ip;
1248 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001249 }
1250 else
1251 __move_assign(__c, true_type());
1252}
1253
1254template <class _Tp, class _Allocator>
1255void
1256vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001257 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001258{
1259 deallocate();
1260 this->__begin_ = __c.__begin_;
1261 this->__end_ = __c.__end_;
1262 this->__end_cap() = __c.__end_cap();
1263 __base::__move_assign_alloc(__c);
1264 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001265#if _LIBCPP_DEBUG_LEVEL >= 2
1266 __get_db()->swap(this, &__c);
1267#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001268}
1269
Howard Hinnant73d21a42010-09-04 23:28:19 +00001270#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001271
1272template <class _Tp, class _Allocator>
1273_LIBCPP_INLINE_VISIBILITY inline
1274vector<_Tp, _Allocator>&
1275vector<_Tp, _Allocator>::operator=(const vector& __x)
1276{
1277 if (this != &__x)
1278 {
1279 __base::__copy_assign_alloc(__x);
1280 assign(__x.__begin_, __x.__end_);
1281 }
1282 return *this;
1283}
1284
1285template <class _Tp, class _Allocator>
1286template <class _InputIterator>
1287typename enable_if
1288<
1289 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001290 !__is_forward_iterator<_InputIterator>::value &&
1291 is_constructible<
1292 _Tp,
1293 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001294 void
1295>::type
1296vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1297{
1298 clear();
1299 for (; __first != __last; ++__first)
1300 push_back(*__first);
1301}
1302
1303template <class _Tp, class _Allocator>
1304template <class _ForwardIterator>
1305typename enable_if
1306<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001307 __is_forward_iterator<_ForwardIterator>::value &&
1308 is_constructible<
1309 _Tp,
1310 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001311 void
1312>::type
1313vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1314{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001315 typename iterator_traits<_ForwardIterator>::difference_type __new_size = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001316 if (static_cast<size_type>(__new_size) <= capacity())
1317 {
1318 _ForwardIterator __mid = __last;
1319 bool __growing = false;
1320 if (static_cast<size_type>(__new_size) > size())
1321 {
1322 __growing = true;
1323 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001324 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001325 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001326 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001327 if (__growing)
1328 __construct_at_end(__mid, __last);
1329 else
1330 this->__destruct_at_end(__m);
1331 }
1332 else
1333 {
1334 deallocate();
1335 allocate(__recommend(static_cast<size_type>(__new_size)));
1336 __construct_at_end(__first, __last);
1337 }
1338}
1339
1340template <class _Tp, class _Allocator>
1341void
1342vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1343{
1344 if (__n <= capacity())
1345 {
1346 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001347 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001348 if (__n > __s)
1349 __construct_at_end(__n - __s, __u);
1350 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001351 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001352 }
1353 else
1354 {
1355 deallocate();
1356 allocate(__recommend(static_cast<size_type>(__n)));
1357 __construct_at_end(__n, __u);
1358 }
1359}
1360
Howard Hinnant324bb032010-08-22 00:02:43 +00001361template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001362_LIBCPP_INLINE_VISIBILITY inline
1363typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001364vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001365{
Howard Hinnantabe26282011-09-16 17:29:17 +00001366#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001367 return iterator(this, __p);
1368#else
1369 return iterator(__p);
1370#endif
1371}
1372
Howard Hinnant324bb032010-08-22 00:02:43 +00001373template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001374_LIBCPP_INLINE_VISIBILITY inline
1375typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001376vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001377{
Howard Hinnantabe26282011-09-16 17:29:17 +00001378#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001379 return const_iterator(this, __p);
1380#else
1381 return const_iterator(__p);
1382#endif
1383}
1384
Howard Hinnant324bb032010-08-22 00:02:43 +00001385template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001386_LIBCPP_INLINE_VISIBILITY inline
1387typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001388vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001389{
1390 return __make_iter(this->__begin_);
1391}
1392
Howard Hinnant324bb032010-08-22 00:02:43 +00001393template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001394_LIBCPP_INLINE_VISIBILITY inline
1395typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001396vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001397{
1398 return __make_iter(this->__begin_);
1399}
1400
Howard Hinnant324bb032010-08-22 00:02:43 +00001401template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001402_LIBCPP_INLINE_VISIBILITY inline
1403typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001404vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001405{
1406 return __make_iter(this->__end_);
1407}
1408
Howard Hinnant324bb032010-08-22 00:02:43 +00001409template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001410_LIBCPP_INLINE_VISIBILITY inline
1411typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001412vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001413{
1414 return __make_iter(this->__end_);
1415}
1416
Howard Hinnant324bb032010-08-22 00:02:43 +00001417template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001418_LIBCPP_INLINE_VISIBILITY inline
1419typename vector<_Tp, _Allocator>::reference
1420vector<_Tp, _Allocator>::operator[](size_type __n)
1421{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001422 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001423 return this->__begin_[__n];
1424}
1425
Howard Hinnant324bb032010-08-22 00:02:43 +00001426template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001427_LIBCPP_INLINE_VISIBILITY inline
1428typename vector<_Tp, _Allocator>::const_reference
1429vector<_Tp, _Allocator>::operator[](size_type __n) const
1430{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001431 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001432 return this->__begin_[__n];
1433}
1434
Howard Hinnant324bb032010-08-22 00:02:43 +00001435template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001436typename vector<_Tp, _Allocator>::reference
1437vector<_Tp, _Allocator>::at(size_type __n)
1438{
1439 if (__n >= size())
1440 this->__throw_out_of_range();
1441 return this->__begin_[__n];
1442}
1443
Howard Hinnant324bb032010-08-22 00:02:43 +00001444template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001445typename vector<_Tp, _Allocator>::const_reference
1446vector<_Tp, _Allocator>::at(size_type __n) const
1447{
1448 if (__n >= size())
1449 this->__throw_out_of_range();
1450 return this->__begin_[__n];
1451}
1452
1453template <class _Tp, class _Allocator>
1454void
1455vector<_Tp, _Allocator>::reserve(size_type __n)
1456{
1457 if (__n > capacity())
1458 {
1459 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001460 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001461 __swap_out_circular_buffer(__v);
1462 }
1463}
1464
1465template <class _Tp, class _Allocator>
1466void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001467vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001468{
1469 if (capacity() > size())
1470 {
1471#ifndef _LIBCPP_NO_EXCEPTIONS
1472 try
1473 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001474#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001475 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001476 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001477 __swap_out_circular_buffer(__v);
1478#ifndef _LIBCPP_NO_EXCEPTIONS
1479 }
1480 catch (...)
1481 {
1482 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001483#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001484 }
1485}
1486
1487template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001488template <class _Up>
1489void
1490#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1491vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1492#else
1493vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1494#endif
1495{
1496 allocator_type& __a = this->__alloc();
1497 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1498 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnantf619e232013-01-11 20:36:59 +00001499 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1500 __v.__end_++;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001501 __swap_out_circular_buffer(__v);
1502}
1503
1504template <class _Tp, class _Allocator>
1505_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506void
1507vector<_Tp, _Allocator>::push_back(const_reference __x)
1508{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001509 if (this->__end_ != this->__end_cap())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001510 {
1511 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001512 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001513 ++this->__end_;
1514 }
1515 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001516 __push_back_slow_path(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001517}
1518
Howard Hinnant73d21a42010-09-04 23:28:19 +00001519#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001520
1521template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001522_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001523void
1524vector<_Tp, _Allocator>::push_back(value_type&& __x)
1525{
1526 if (this->__end_ < this->__end_cap())
1527 {
1528 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001529 _VSTD::__to_raw_pointer(this->__end_),
1530 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001531 ++this->__end_;
1532 }
1533 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001534 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001535}
1536
Howard Hinnant73d21a42010-09-04 23:28:19 +00001537#ifndef _LIBCPP_HAS_NO_VARIADICS
1538
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001539template <class _Tp, class _Allocator>
1540template <class... _Args>
1541void
Howard Hinnant0438ea22012-02-26 15:30:12 +00001542vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1543{
1544 allocator_type& __a = this->__alloc();
1545 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1546// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantf619e232013-01-11 20:36:59 +00001547 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1548 __v.__end_++;
Howard Hinnant0438ea22012-02-26 15:30:12 +00001549 __swap_out_circular_buffer(__v);
1550}
1551
1552template <class _Tp, class _Allocator>
1553template <class... _Args>
1554_LIBCPP_INLINE_VISIBILITY inline
1555void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001556vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1557{
1558 if (this->__end_ < this->__end_cap())
1559 {
1560 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001561 _VSTD::__to_raw_pointer(this->__end_),
1562 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001563 ++this->__end_;
1564 }
1565 else
Howard Hinnant0438ea22012-02-26 15:30:12 +00001566 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001567}
1568
Howard Hinnant73d21a42010-09-04 23:28:19 +00001569#endif // _LIBCPP_HAS_NO_VARIADICS
1570#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001571
1572template <class _Tp, class _Allocator>
1573_LIBCPP_INLINE_VISIBILITY inline
1574void
1575vector<_Tp, _Allocator>::pop_back()
1576{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001577 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001578 this->__destruct_at_end(this->__end_ - 1);
1579}
1580
1581template <class _Tp, class _Allocator>
1582_LIBCPP_INLINE_VISIBILITY inline
1583typename vector<_Tp, _Allocator>::iterator
1584vector<_Tp, _Allocator>::erase(const_iterator __position)
1585{
Howard Hinnantabe26282011-09-16 17:29:17 +00001586#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001587 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1588 "vector::erase(iterator) called with an iterator not"
1589 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001590#endif
Howard Hinnant782da332013-03-25 22:12:26 +00001591 _LIBCPP_ASSERT(__position != end(),
1592 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001593 difference_type __ps = __position - cbegin();
1594 pointer __p = this->__begin_ + __ps;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001595 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001596 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597 return __r;
1598}
1599
1600template <class _Tp, class _Allocator>
1601typename vector<_Tp, _Allocator>::iterator
1602vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1603{
Howard Hinnantabe26282011-09-16 17:29:17 +00001604#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001605 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1606 "vector::erase(iterator, iterator) called with an iterator not"
1607 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001608#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +00001609 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001610 pointer __p = this->__begin_ + (__first - begin());
1611 iterator __r = __make_iter(__p);
Howard Hinnantb4e67cf2013-04-18 15:02:57 +00001612 if (__first != __last)
1613 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001614 return __r;
1615}
1616
1617template <class _Tp, class _Allocator>
1618void
1619vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1620{
1621 pointer __old_last = this->__end_;
1622 difference_type __n = __old_last - __to;
1623 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1624 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001625 _VSTD::__to_raw_pointer(this->__end_),
1626 _VSTD::move(*__i));
1627 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001628}
1629
1630template <class _Tp, class _Allocator>
1631typename vector<_Tp, _Allocator>::iterator
1632vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1633{
Howard Hinnantabe26282011-09-16 17:29:17 +00001634#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001635 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1636 "vector::insert(iterator, x) called with an iterator not"
1637 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001638#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001639 pointer __p = this->__begin_ + (__position - begin());
1640 if (this->__end_ < this->__end_cap())
1641 {
1642 if (__p == this->__end_)
1643 {
1644 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001645 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001646 ++this->__end_;
1647 }
1648 else
1649 {
1650 __move_range(__p, this->__end_, __p + 1);
1651 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1652 if (__p <= __xr && __xr < this->__end_)
1653 ++__xr;
1654 *__p = *__xr;
1655 }
1656 }
1657 else
1658 {
1659 allocator_type& __a = this->__alloc();
1660 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1661 __v.push_back(__x);
1662 __p = __swap_out_circular_buffer(__v, __p);
1663 }
1664 return __make_iter(__p);
1665}
1666
Howard Hinnant73d21a42010-09-04 23:28:19 +00001667#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001668
1669template <class _Tp, class _Allocator>
1670typename vector<_Tp, _Allocator>::iterator
1671vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1672{
Howard Hinnantabe26282011-09-16 17:29:17 +00001673#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001674 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1675 "vector::insert(iterator, x) called with an iterator not"
1676 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001677#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001678 pointer __p = this->__begin_ + (__position - begin());
1679 if (this->__end_ < this->__end_cap())
1680 {
1681 if (__p == this->__end_)
1682 {
1683 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001684 _VSTD::__to_raw_pointer(this->__end_),
1685 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001686 ++this->__end_;
1687 }
1688 else
1689 {
1690 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001691 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001692 }
1693 }
1694 else
1695 {
1696 allocator_type& __a = this->__alloc();
1697 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001698 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001699 __p = __swap_out_circular_buffer(__v, __p);
1700 }
1701 return __make_iter(__p);
1702}
1703
Howard Hinnant73d21a42010-09-04 23:28:19 +00001704#ifndef _LIBCPP_HAS_NO_VARIADICS
1705
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001706template <class _Tp, class _Allocator>
1707template <class... _Args>
1708typename vector<_Tp, _Allocator>::iterator
1709vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1710{
Howard Hinnantabe26282011-09-16 17:29:17 +00001711#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001712 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1713 "vector::emplace(iterator, x) called with an iterator not"
1714 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001715#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001716 pointer __p = this->__begin_ + (__position - begin());
1717 if (this->__end_ < this->__end_cap())
1718 {
1719 if (__p == this->__end_)
1720 {
1721 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001722 _VSTD::__to_raw_pointer(this->__end_),
1723 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001724 ++this->__end_;
1725 }
1726 else
1727 {
Howard Hinnanta58402a2012-07-08 23:23:04 +00001728 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001729 __move_range(__p, this->__end_, __p + 1);
Howard Hinnanta58402a2012-07-08 23:23:04 +00001730 *__p = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001731 }
1732 }
1733 else
1734 {
1735 allocator_type& __a = this->__alloc();
1736 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001737 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001738 __p = __swap_out_circular_buffer(__v, __p);
1739 }
1740 return __make_iter(__p);
1741}
1742
Howard Hinnant73d21a42010-09-04 23:28:19 +00001743#endif // _LIBCPP_HAS_NO_VARIADICS
1744#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001745
1746template <class _Tp, class _Allocator>
1747typename vector<_Tp, _Allocator>::iterator
1748vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1749{
Howard Hinnantabe26282011-09-16 17:29:17 +00001750#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001751 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1752 "vector::insert(iterator, n, x) called with an iterator not"
1753 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001754#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001755 pointer __p = this->__begin_ + (__position - begin());
1756 if (__n > 0)
1757 {
1758 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1759 {
1760 size_type __old_n = __n;
1761 pointer __old_last = this->__end_;
1762 if (__n > static_cast<size_type>(this->__end_ - __p))
1763 {
1764 size_type __cx = __n - (this->__end_ - __p);
1765 __construct_at_end(__cx, __x);
1766 __n -= __cx;
1767 }
1768 if (__n > 0)
1769 {
1770 __move_range(__p, __old_last, __p + __old_n);
1771 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1772 if (__p <= __xr && __xr < this->__end_)
1773 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001774 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001775 }
1776 }
1777 else
1778 {
1779 allocator_type& __a = this->__alloc();
1780 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1781 __v.__construct_at_end(__n, __x);
1782 __p = __swap_out_circular_buffer(__v, __p);
1783 }
1784 }
1785 return __make_iter(__p);
1786}
1787
1788template <class _Tp, class _Allocator>
1789template <class _InputIterator>
1790typename enable_if
1791<
1792 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001793 !__is_forward_iterator<_InputIterator>::value &&
1794 is_constructible<
1795 _Tp,
1796 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001797 typename vector<_Tp, _Allocator>::iterator
1798>::type
1799vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1800{
Howard Hinnantabe26282011-09-16 17:29:17 +00001801#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001802 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1803 "vector::insert(iterator, range) called with an iterator not"
1804 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001805#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001806 difference_type __off = __position - begin();
1807 pointer __p = this->__begin_ + __off;
1808 allocator_type& __a = this->__alloc();
1809 pointer __old_last = this->__end_;
1810 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1811 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001812 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001813 *__first);
1814 ++this->__end_;
1815 }
1816 __split_buffer<value_type, allocator_type&> __v(__a);
1817 if (__first != __last)
1818 {
1819#ifndef _LIBCPP_NO_EXCEPTIONS
1820 try
1821 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001822#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001823 __v.__construct_at_end(__first, __last);
1824 difference_type __old_size = __old_last - this->__begin_;
1825 difference_type __old_p = __p - this->__begin_;
1826 reserve(__recommend(size() + __v.size()));
1827 __p = this->__begin_ + __old_p;
1828 __old_last = this->__begin_ + __old_size;
1829#ifndef _LIBCPP_NO_EXCEPTIONS
1830 }
1831 catch (...)
1832 {
1833 erase(__make_iter(__old_last), end());
1834 throw;
1835 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001836#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001838 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant0442b122011-09-16 18:41:29 +00001839 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1840 make_move_iterator(__v.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001841 return begin() + __off;
1842}
1843
1844template <class _Tp, class _Allocator>
1845template <class _ForwardIterator>
1846typename enable_if
1847<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001848 __is_forward_iterator<_ForwardIterator>::value &&
1849 is_constructible<
1850 _Tp,
1851 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001852 typename vector<_Tp, _Allocator>::iterator
1853>::type
1854vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1855{
Howard Hinnantabe26282011-09-16 17:29:17 +00001856#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001857 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1858 "vector::insert(iterator, range) called with an iterator not"
1859 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001860#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001861 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00001862 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001863 if (__n > 0)
1864 {
1865 if (__n <= this->__end_cap() - this->__end_)
1866 {
1867 size_type __old_n = __n;
1868 pointer __old_last = this->__end_;
1869 _ForwardIterator __m = __last;
1870 difference_type __dx = this->__end_ - __p;
1871 if (__n > __dx)
1872 {
1873 __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001874 _VSTD::advance(__m, this->__end_ - __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001875 __construct_at_end(__m, __last);
1876 __n = __dx;
1877 }
1878 if (__n > 0)
1879 {
1880 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001881 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001882 }
1883 }
1884 else
1885 {
1886 allocator_type& __a = this->__alloc();
1887 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1888 __v.__construct_at_end(__first, __last);
1889 __p = __swap_out_circular_buffer(__v, __p);
1890 }
1891 }
1892 return __make_iter(__p);
1893}
1894
1895template <class _Tp, class _Allocator>
1896void
1897vector<_Tp, _Allocator>::resize(size_type __sz)
1898{
1899 size_type __cs = size();
1900 if (__cs < __sz)
1901 this->__append(__sz - __cs);
1902 else if (__cs > __sz)
1903 this->__destruct_at_end(this->__begin_ + __sz);
1904}
1905
1906template <class _Tp, class _Allocator>
1907void
1908vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
1909{
1910 size_type __cs = size();
1911 if (__cs < __sz)
1912 this->__append(__sz - __cs, __x);
1913 else if (__cs > __sz)
1914 this->__destruct_at_end(this->__begin_ + __sz);
1915}
1916
1917template <class _Tp, class _Allocator>
1918void
1919vector<_Tp, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001920 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1921 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001922{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001923 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
1924 this->__alloc() == __x.__alloc(),
1925 "vector::swap: Either propagate_on_container_swap must be true"
1926 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +00001927 _VSTD::swap(this->__begin_, __x.__begin_);
1928 _VSTD::swap(this->__end_, __x.__end_);
1929 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001930 __base::__swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnantabe26282011-09-16 17:29:17 +00001931#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001932 __get_db()->swap(this, &__x);
Howard Hinnantabe26282011-09-16 17:29:17 +00001933#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001934}
1935
Howard Hinnant324bb032010-08-22 00:02:43 +00001936template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001937bool
1938vector<_Tp, _Allocator>::__invariants() const
1939{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001940 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001941 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001942 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001943 return false;
1944 }
1945 else
1946 {
1947 if (this->__begin_ > this->__end_)
1948 return false;
1949 if (this->__begin_ == this->__end_cap())
1950 return false;
1951 if (this->__end_ > this->__end_cap())
1952 return false;
1953 }
1954 return true;
1955}
1956
Howard Hinnantabe26282011-09-16 17:29:17 +00001957#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001958
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001959template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:51 +00001960bool
1961vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
1962{
1963 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
1964}
1965
1966template <class _Tp, class _Allocator>
1967bool
1968vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
1969{
1970 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
1971}
1972
1973template <class _Tp, class _Allocator>
1974bool
1975vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
1976{
1977 const_pointer __p = __i->base() + __n;
1978 return this->__begin_ <= __p && __p <= this->__end_;
1979}
1980
1981template <class _Tp, class _Allocator>
1982bool
1983vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
1984{
1985 const_pointer __p = __i->base() + __n;
1986 return this->__begin_ <= __p && __p < this->__end_;
1987}
1988
Howard Hinnantabe26282011-09-16 17:29:17 +00001989#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001990
1991template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001992_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001993void
1994vector<_Tp, _Allocator>::__invalidate_all_iterators()
1995{
Howard Hinnantabe26282011-09-16 17:29:17 +00001996#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001997 __get_db()->__invalidate_all(this);
Howard Hinnantabe26282011-09-16 17:29:17 +00001998#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001999}
2000
2001// vector<bool>
2002
2003template <class _Allocator> class vector<bool, _Allocator>;
2004
2005template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2006
2007template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002008struct __has_storage_type<vector<bool, _Allocator> >
2009{
2010 static const bool value = true;
2011};
2012
2013template <class _Allocator>
Howard Hinnant83eade62013-03-06 23:30:19 +00002014class _LIBCPP_TYPE_VIS vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002015 : private __vector_base_common<true>
2016{
2017public:
2018 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00002019 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002020 typedef _Allocator allocator_type;
2021 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002022 typedef typename __alloc_traits::size_type size_type;
2023 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantf867f632012-05-07 16:50:38 +00002024 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002025 typedef __bit_iterator<vector, false> pointer;
2026 typedef __bit_iterator<vector, true> const_pointer;
2027#ifdef _LIBCPP_DEBUG
2028 typedef __debug_iter<vector, pointer> iterator;
2029 typedef __debug_iter<vector, const_pointer> const_iterator;
2030
2031 friend class __debug_iter<vector, pointer>;
2032 friend class __debug_iter<vector, const_pointer>;
2033
2034 pair<iterator*, const_iterator*> __iterator_list_;
2035
2036 _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;}
2037 _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;}
Howard Hinnant324bb032010-08-22 00:02:43 +00002038#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002039 typedef pointer iterator;
2040 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +00002041#endif // _LIBCPP_DEBUG
Howard Hinnant0949eed2011-06-30 21:18:19 +00002042 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2043 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002044
2045private:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002046 typedef typename __alloc_traits::template
2047#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
2048 rebind_alloc<__storage_type>
2049#else
2050 rebind_alloc<__storage_type>::other
2051#endif
2052 __storage_allocator;
2053 typedef allocator_traits<__storage_allocator> __storage_traits;
2054 typedef typename __storage_traits::pointer __storage_pointer;
2055 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2056
2057 __storage_pointer __begin_;
2058 size_type __size_;
2059 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002060public:
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002061 typedef __bit_reference<vector> reference;
2062 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002063private:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002064 _LIBCPP_INLINE_VISIBILITY
2065 size_type& __cap() _NOEXCEPT
2066 {return __cap_alloc_.first();}
2067 _LIBCPP_INLINE_VISIBILITY
2068 const size_type& __cap() const _NOEXCEPT
2069 {return __cap_alloc_.first();}
2070 _LIBCPP_INLINE_VISIBILITY
2071 __storage_allocator& __alloc() _NOEXCEPT
2072 {return __cap_alloc_.second();}
2073 _LIBCPP_INLINE_VISIBILITY
2074 const __storage_allocator& __alloc() const _NOEXCEPT
2075 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002076
2077 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2078
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002079 _LIBCPP_INLINE_VISIBILITY
2080 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002081 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002082 _LIBCPP_INLINE_VISIBILITY
2083 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002084 {return (__n - 1) / __bits_per_word + 1;}
2085
2086public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002087 _LIBCPP_INLINE_VISIBILITY
2088 vector()
2089 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002090 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002091 ~vector();
2092 explicit vector(size_type __n);
2093 vector(size_type __n, const value_type& __v);
2094 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2095 template <class _InputIterator>
2096 vector(_InputIterator __first, _InputIterator __last,
2097 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2098 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2099 template <class _InputIterator>
2100 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2101 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2102 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2103 template <class _ForwardIterator>
2104 vector(_ForwardIterator __first, _ForwardIterator __last,
2105 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2106 template <class _ForwardIterator>
2107 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2108 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2109
2110 vector(const vector& __v);
2111 vector(const vector& __v, const allocator_type& __a);
2112 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00002113#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002114 vector(initializer_list<value_type> __il);
2115 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00002116#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002117
Howard Hinnant73d21a42010-09-04 23:28:19 +00002118#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002119 _LIBCPP_INLINE_VISIBILITY
2120 vector(vector&& __v)
2121 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002122 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002123 _LIBCPP_INLINE_VISIBILITY
2124 vector& operator=(vector&& __v)
2125 _NOEXCEPT_(
2126 __alloc_traits::propagate_on_container_move_assignment::value &&
2127 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002128#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +00002129#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002131 vector& operator=(initializer_list<value_type> __il)
2132 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00002133#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002134
2135 template <class _InputIterator>
2136 typename enable_if
2137 <
2138 __is_input_iterator<_InputIterator>::value &&
2139 !__is_forward_iterator<_InputIterator>::value,
2140 void
2141 >::type
2142 assign(_InputIterator __first, _InputIterator __last);
2143 template <class _ForwardIterator>
2144 typename enable_if
2145 <
2146 __is_forward_iterator<_ForwardIterator>::value,
2147 void
2148 >::type
2149 assign(_ForwardIterator __first, _ForwardIterator __last);
2150
2151 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +00002152#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002154 void assign(initializer_list<value_type> __il)
2155 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002156#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002157
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002158 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002159 {return allocator_type(this->__alloc());}
2160
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002161 size_type max_size() const _NOEXCEPT;
2162 _LIBCPP_INLINE_VISIBILITY
2163 size_type capacity() const _NOEXCEPT
2164 {return __internal_cap_to_external(__cap());}
2165 _LIBCPP_INLINE_VISIBILITY
2166 size_type size() const _NOEXCEPT
2167 {return __size_;}
2168 _LIBCPP_INLINE_VISIBILITY
2169 bool empty() const _NOEXCEPT
2170 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002171 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002172 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002173
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002174 _LIBCPP_INLINE_VISIBILITY
2175 iterator begin() _NOEXCEPT
2176 {return __make_iter(0);}
2177 _LIBCPP_INLINE_VISIBILITY
2178 const_iterator begin() const _NOEXCEPT
2179 {return __make_iter(0);}
2180 _LIBCPP_INLINE_VISIBILITY
2181 iterator end() _NOEXCEPT
2182 {return __make_iter(__size_);}
2183 _LIBCPP_INLINE_VISIBILITY
2184 const_iterator end() const _NOEXCEPT
2185 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002186
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002187 _LIBCPP_INLINE_VISIBILITY
2188 reverse_iterator rbegin() _NOEXCEPT
2189 {return reverse_iterator(end());}
2190 _LIBCPP_INLINE_VISIBILITY
2191 const_reverse_iterator rbegin() const _NOEXCEPT
2192 {return const_reverse_iterator(end());}
2193 _LIBCPP_INLINE_VISIBILITY
2194 reverse_iterator rend() _NOEXCEPT
2195 {return reverse_iterator(begin());}
2196 _LIBCPP_INLINE_VISIBILITY
2197 const_reverse_iterator rend() const _NOEXCEPT
2198 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002199
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002200 _LIBCPP_INLINE_VISIBILITY
2201 const_iterator cbegin() const _NOEXCEPT
2202 {return __make_iter(0);}
2203 _LIBCPP_INLINE_VISIBILITY
2204 const_iterator cend() const _NOEXCEPT
2205 {return __make_iter(__size_);}
2206 _LIBCPP_INLINE_VISIBILITY
2207 const_reverse_iterator crbegin() const _NOEXCEPT
2208 {return rbegin();}
2209 _LIBCPP_INLINE_VISIBILITY
2210 const_reverse_iterator crend() const _NOEXCEPT
2211 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002212
2213 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2214 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2215 reference at(size_type __n);
2216 const_reference at(size_type __n) const;
2217
2218 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2219 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2220 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2221 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2222
2223 void push_back(const value_type& __x);
2224 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2225
2226 iterator insert(const_iterator __position, const value_type& __x);
2227 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2228 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2229 template <class _InputIterator>
2230 typename enable_if
2231 <
2232 __is_input_iterator <_InputIterator>::value &&
2233 !__is_forward_iterator<_InputIterator>::value,
2234 iterator
2235 >::type
2236 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2237 template <class _ForwardIterator>
2238 typename enable_if
2239 <
2240 __is_forward_iterator<_ForwardIterator>::value,
2241 iterator
2242 >::type
2243 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00002244#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002246 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2247 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002248#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002249
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002250 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002251 iterator erase(const_iterator __first, const_iterator __last);
2252
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002253 _LIBCPP_INLINE_VISIBILITY
2254 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002255
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002256 void swap(vector&)
2257 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2258 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002259
2260 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002261 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002262
2263 bool __invariants() const;
2264
2265private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002266 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002267 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002268 void deallocate() _NOEXCEPT;
2269 _LIBCPP_INLINE_VISIBILITY
2270 static size_type __align(size_type __new_size) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002271 {return __new_size + (__bits_per_word-1) & ~(__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002272 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2273 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002274 template <class _ForwardIterator>
2275 typename enable_if
2276 <
2277 __is_forward_iterator<_ForwardIterator>::value,
2278 void
2279 >::type
2280 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2281 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002282 _LIBCPP_INLINE_VISIBILITY
2283 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002284 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002285 _LIBCPP_INLINE_VISIBILITY
2286 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002287 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
2288#ifdef _LIBCPP_DEBUG
2289 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_type __pos)
2290 {return iterator(this, pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2291 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_type __pos) const
2292 {return const_iterator(this, const_pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
2293 _LIBCPP_INLINE_VISIBILITY iterator __const_iterator_cast(const_iterator __p)
2294 {return iterator(this, pointer(const_cast<__storage_pointer>(__p.base().__seg_), __p.base().__ctz_));}
Howard Hinnant324bb032010-08-22 00:02:43 +00002295#else // _LIBCPP_DEBUG
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002296 _LIBCPP_INLINE_VISIBILITY
2297 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002298 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002299 _LIBCPP_INLINE_VISIBILITY
2300 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002301 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002302 _LIBCPP_INLINE_VISIBILITY
2303 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002304 {return begin() + (__p - cbegin());}
Howard Hinnant324bb032010-08-22 00:02:43 +00002305#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002306
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002308 void __copy_assign_alloc(const vector& __v)
2309 {__copy_assign_alloc(__v, integral_constant<bool,
2310 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002311 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002312 void __copy_assign_alloc(const vector& __c, true_type)
2313 {
2314 if (__alloc() != __c.__alloc())
2315 deallocate();
2316 __alloc() = __c.__alloc();
2317 }
2318
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002320 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002321 {}
2322
2323 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002324 void __move_assign(vector& __c, true_type)
2325 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002326 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002327 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002328 _NOEXCEPT_(
2329 !__storage_traits::propagate_on_container_move_assignment::value ||
2330 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002331 {__move_assign_alloc(__c, integral_constant<bool,
2332 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00002334 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002335 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002336 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002337 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002338 }
2339
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002341 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002342 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002343 {}
2344
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002345 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002346 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002347 _NOEXCEPT_(
2348 !__storage_traits::propagate_on_container_swap::value ||
2349 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002350 {__swap_alloc(__x, __y, integral_constant<bool,
2351 __storage_traits::propagate_on_container_swap::value>());}
2352
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002353 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002354 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002355 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002356 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002357 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002358 swap(__x, __y);
2359 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002361 static void __swap_alloc(__storage_allocator&, __storage_allocator&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002362 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002363 {}
2364
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002365 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002366
2367 friend class __bit_reference<vector>;
2368 friend class __bit_const_reference<vector>;
2369 friend class __bit_iterator<vector, false>;
2370 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:18 +00002371 friend struct __bit_array<vector>;
Howard Hinnant83eade62013-03-06 23:30:19 +00002372 friend struct _LIBCPP_TYPE_VIS hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002373};
2374
2375template <class _Allocator>
2376#ifndef _LIBCPP_DEBUG
2377_LIBCPP_INLINE_VISIBILITY inline
2378#endif
2379void
2380vector<bool, _Allocator>::__invalidate_all_iterators()
2381{
2382#ifdef _LIBCPP_DEBUG
2383 iterator::__remove_all(this);
2384 const_iterator::__remove_all(this);
Howard Hinnant324bb032010-08-22 00:02:43 +00002385#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002386}
2387
2388// Allocate space for __n objects
2389// throws length_error if __n > max_size()
2390// throws (probably bad_alloc) if memory run out
2391// Precondition: __begin_ == __end_ == __cap() == 0
2392// Precondition: __n > 0
2393// Postcondition: capacity() == __n
2394// Postcondition: size() == 0
2395template <class _Allocator>
2396void
2397vector<bool, _Allocator>::allocate(size_type __n)
2398{
2399 if (__n > max_size())
2400 this->__throw_length_error();
2401 __n = __external_cap_to_internal(__n);
2402 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2403 this->__size_ = 0;
2404 this->__cap() = __n;
2405}
2406
2407template <class _Allocator>
2408void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002409vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002410{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002411 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002412 {
2413 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2414 __invalidate_all_iterators();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002415 this->__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002416 this->__size_ = this->__cap() = 0;
2417 }
2418}
2419
2420template <class _Allocator>
2421typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002422vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002423{
2424 size_type __amax = __storage_traits::max_size(__alloc());
2425 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2426 if (__nmax / __bits_per_word <= __amax)
2427 return __nmax;
2428 return __internal_cap_to_external(__amax);
2429}
2430
2431// Precondition: __new_size > capacity()
2432template <class _Allocator>
2433_LIBCPP_INLINE_VISIBILITY inline
2434typename vector<bool, _Allocator>::size_type
2435vector<bool, _Allocator>::__recommend(size_type __new_size) const
2436{
2437 const size_type __ms = max_size();
2438 if (__new_size > __ms)
2439 this->__throw_length_error();
2440 const size_type __cap = capacity();
2441 if (__cap >= __ms / 2)
2442 return __ms;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002443 return _VSTD::max(2*__cap, __align(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002444}
2445
2446// Default constructs __n objects starting at __end_
2447// Precondition: __n > 0
2448// Precondition: size() + __n <= capacity()
2449// Postcondition: size() == size() + __n
2450template <class _Allocator>
2451_LIBCPP_INLINE_VISIBILITY inline
2452void
2453vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2454{
2455 size_type __old_size = this->__size_;
2456 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002457 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002458}
2459
2460template <class _Allocator>
2461template <class _ForwardIterator>
2462typename enable_if
2463<
2464 __is_forward_iterator<_ForwardIterator>::value,
2465 void
2466>::type
2467vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2468{
2469 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002470 this->__size_ += _VSTD::distance(__first, __last);
2471 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002472}
2473
2474template <class _Allocator>
2475_LIBCPP_INLINE_VISIBILITY inline
2476vector<bool, _Allocator>::vector()
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002477 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002478 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002479 __size_(0),
2480 __cap_alloc_(0)
2481{
2482}
2483
2484template <class _Allocator>
2485_LIBCPP_INLINE_VISIBILITY inline
2486vector<bool, _Allocator>::vector(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002487 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002488 __size_(0),
2489 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2490{
2491}
2492
2493template <class _Allocator>
2494vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002495 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002496 __size_(0),
2497 __cap_alloc_(0)
2498{
2499 if (__n > 0)
2500 {
2501 allocate(__n);
2502 __construct_at_end(__n, false);
2503 }
2504}
2505
2506template <class _Allocator>
2507vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002508 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002509 __size_(0),
2510 __cap_alloc_(0)
2511{
2512 if (__n > 0)
2513 {
2514 allocate(__n);
2515 __construct_at_end(__n, __x);
2516 }
2517}
2518
2519template <class _Allocator>
2520vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002521 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002522 __size_(0),
2523 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2524{
2525 if (__n > 0)
2526 {
2527 allocate(__n);
2528 __construct_at_end(__n, __x);
2529 }
2530}
2531
2532template <class _Allocator>
2533template <class _InputIterator>
2534vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2535 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2536 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002537 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002538 __size_(0),
2539 __cap_alloc_(0)
2540{
2541#ifndef _LIBCPP_NO_EXCEPTIONS
2542 try
2543 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002544#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002545 for (; __first != __last; ++__first)
2546 push_back(*__first);
2547#ifndef _LIBCPP_NO_EXCEPTIONS
2548 }
2549 catch (...)
2550 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002551 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002552 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2553 __invalidate_all_iterators();
2554 throw;
2555 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002556#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002557}
2558
2559template <class _Allocator>
2560template <class _InputIterator>
2561vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2562 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2563 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002564 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002565 __size_(0),
2566 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2567{
2568#ifndef _LIBCPP_NO_EXCEPTIONS
2569 try
2570 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002571#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002572 for (; __first != __last; ++__first)
2573 push_back(*__first);
2574#ifndef _LIBCPP_NO_EXCEPTIONS
2575 }
2576 catch (...)
2577 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002578 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002579 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2580 __invalidate_all_iterators();
2581 throw;
2582 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002583#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002584}
2585
2586template <class _Allocator>
2587template <class _ForwardIterator>
2588vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2589 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002590 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002591 __size_(0),
2592 __cap_alloc_(0)
2593{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002594 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002595 if (__n > 0)
2596 {
2597 allocate(__n);
2598 __construct_at_end(__first, __last);
2599 }
2600}
2601
2602template <class _Allocator>
2603template <class _ForwardIterator>
2604vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2605 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002606 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002607 __size_(0),
2608 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2609{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002610 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002611 if (__n > 0)
2612 {
2613 allocate(__n);
2614 __construct_at_end(__first, __last);
2615 }
2616}
2617
Howard Hinnante3e32912011-08-12 21:56:02 +00002618#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2619
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002620template <class _Allocator>
2621vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002622 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002623 __size_(0),
2624 __cap_alloc_(0)
2625{
2626 size_type __n = static_cast<size_type>(__il.size());
2627 if (__n > 0)
2628 {
2629 allocate(__n);
2630 __construct_at_end(__il.begin(), __il.end());
2631 }
2632}
2633
2634template <class _Allocator>
2635vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002636 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002637 __size_(0),
2638 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2639{
2640 size_type __n = static_cast<size_type>(__il.size());
2641 if (__n > 0)
2642 {
2643 allocate(__n);
2644 __construct_at_end(__il.begin(), __il.end());
2645 }
2646}
2647
Howard Hinnante3e32912011-08-12 21:56:02 +00002648#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2649
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002650template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002651vector<bool, _Allocator>::~vector()
2652{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002653 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002654 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2655#ifdef _LIBCPP_DEBUG
2656 __invalidate_all_iterators();
2657#endif
2658}
2659
2660template <class _Allocator>
2661vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002662 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002663 __size_(0),
2664 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2665{
2666 if (__v.size() > 0)
2667 {
2668 allocate(__v.size());
2669 __construct_at_end(__v.begin(), __v.end());
2670 }
2671}
2672
2673template <class _Allocator>
2674vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002675 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002676 __size_(0),
2677 __cap_alloc_(0, __a)
2678{
2679 if (__v.size() > 0)
2680 {
2681 allocate(__v.size());
2682 __construct_at_end(__v.begin(), __v.end());
2683 }
2684}
2685
2686template <class _Allocator>
2687vector<bool, _Allocator>&
2688vector<bool, _Allocator>::operator=(const vector& __v)
2689{
2690 if (this != &__v)
2691 {
2692 __copy_assign_alloc(__v);
2693 if (__v.__size_)
2694 {
2695 if (__v.__size_ > capacity())
2696 {
2697 deallocate();
2698 allocate(__v.__size_);
2699 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002700 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002701 }
2702 __size_ = __v.__size_;
2703 }
2704 return *this;
2705}
2706
Howard Hinnant73d21a42010-09-04 23:28:19 +00002707#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2708
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002709template <class _Allocator>
2710_LIBCPP_INLINE_VISIBILITY inline
2711vector<bool, _Allocator>::vector(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002712 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002713 : __begin_(__v.__begin_),
2714 __size_(__v.__size_),
2715 __cap_alloc_(__v.__cap_alloc_)
2716{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002717 __v.__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002718 __v.__size_ = 0;
2719 __v.__cap() = 0;
2720}
2721
2722template <class _Allocator>
2723vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002724 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002725 __size_(0),
2726 __cap_alloc_(0, __a)
2727{
2728 if (__a == allocator_type(__v.__alloc()))
2729 {
2730 this->__begin_ = __v.__begin_;
2731 this->__size_ = __v.__size_;
2732 this->__cap() = __v.__cap();
2733 __v.__begin_ = nullptr;
2734 __v.__cap() = __v.__size_ = 0;
2735 }
2736 else if (__v.size() > 0)
2737 {
2738 allocate(__v.size());
2739 __construct_at_end(__v.begin(), __v.end());
2740 }
2741}
2742
2743template <class _Allocator>
2744_LIBCPP_INLINE_VISIBILITY inline
2745vector<bool, _Allocator>&
2746vector<bool, _Allocator>::operator=(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002747 _NOEXCEPT_(
2748 __alloc_traits::propagate_on_container_move_assignment::value &&
2749 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002750{
2751 __move_assign(__v, integral_constant<bool,
2752 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002753 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002754}
2755
2756template <class _Allocator>
2757void
2758vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2759{
2760 if (__alloc() != __c.__alloc())
2761 assign(__c.begin(), __c.end());
2762 else
2763 __move_assign(__c, true_type());
2764}
2765
2766template <class _Allocator>
2767void
2768vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002769 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002770{
2771 deallocate();
2772 this->__begin_ = __c.__begin_;
2773 this->__size_ = __c.__size_;
2774 this->__cap() = __c.__cap();
2775 __move_assign_alloc(__c);
2776 __c.__begin_ = nullptr;
2777 __c.__cap() = __c.__size_ = 0;
2778}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002779
2780#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002781
2782template <class _Allocator>
2783void
2784vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2785{
2786 __size_ = 0;
2787 if (__n > 0)
2788 {
2789 size_type __c = capacity();
2790 if (__n <= __c)
2791 __size_ = __n;
2792 else
2793 {
2794 vector __v(__alloc());
2795 __v.reserve(__recommend(__n));
2796 __v.__size_ = __n;
2797 swap(__v);
2798 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002799 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002800 }
2801}
2802
2803template <class _Allocator>
2804template <class _InputIterator>
2805typename enable_if
2806<
2807 __is_input_iterator<_InputIterator>::value &&
2808 !__is_forward_iterator<_InputIterator>::value,
2809 void
2810>::type
2811vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2812{
2813 clear();
2814 for (; __first != __last; ++__first)
2815 push_back(*__first);
2816}
2817
2818template <class _Allocator>
2819template <class _ForwardIterator>
2820typename enable_if
2821<
2822 __is_forward_iterator<_ForwardIterator>::value,
2823 void
2824>::type
2825vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2826{
2827 clear();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002828 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002829 if (__n)
2830 {
2831 if (__n > capacity())
2832 {
2833 deallocate();
2834 allocate(__n);
2835 }
2836 __construct_at_end(__first, __last);
2837 }
2838}
2839
2840template <class _Allocator>
2841void
2842vector<bool, _Allocator>::reserve(size_type __n)
2843{
2844 if (__n > capacity())
2845 {
2846 vector __v(this->__alloc());
2847 __v.allocate(__n);
2848 __v.__construct_at_end(this->begin(), this->end());
2849 swap(__v);
2850 __invalidate_all_iterators();
2851 }
2852}
2853
2854template <class _Allocator>
2855void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002856vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002857{
2858 if (__external_cap_to_internal(size()) > __cap())
2859 {
2860#ifndef _LIBCPP_NO_EXCEPTIONS
2861 try
2862 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002863#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002864 vector(*this, allocator_type(__alloc())).swap(*this);
2865#ifndef _LIBCPP_NO_EXCEPTIONS
2866 }
2867 catch (...)
2868 {
2869 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002870#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002871 }
2872}
2873
2874template <class _Allocator>
2875typename vector<bool, _Allocator>::reference
2876vector<bool, _Allocator>::at(size_type __n)
2877{
2878 if (__n >= size())
2879 this->__throw_out_of_range();
2880 return (*this)[__n];
2881}
2882
2883template <class _Allocator>
2884typename vector<bool, _Allocator>::const_reference
2885vector<bool, _Allocator>::at(size_type __n) const
2886{
2887 if (__n >= size())
2888 this->__throw_out_of_range();
2889 return (*this)[__n];
2890}
2891
2892template <class _Allocator>
2893void
2894vector<bool, _Allocator>::push_back(const value_type& __x)
2895{
2896 if (this->__size_ == this->capacity())
2897 reserve(__recommend(this->__size_ + 1));
2898 ++this->__size_;
2899 back() = __x;
2900}
2901
2902template <class _Allocator>
2903typename vector<bool, _Allocator>::iterator
2904vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2905{
2906 iterator __r;
2907 if (size() < capacity())
2908 {
2909 const_iterator __old_end = end();
2910 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002911 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002912 __r = __const_iterator_cast(__position);
2913 }
2914 else
2915 {
2916 vector __v(__alloc());
2917 __v.reserve(__recommend(__size_ + 1));
2918 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002919 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2920 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002921 swap(__v);
2922 }
2923 *__r = __x;
2924 return __r;
2925}
2926
2927template <class _Allocator>
2928typename vector<bool, _Allocator>::iterator
2929vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
2930{
2931 iterator __r;
2932 size_type __c = capacity();
2933 if (__n <= __c && size() <= __c - __n)
2934 {
2935 const_iterator __old_end = end();
2936 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002937 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002938 __r = __const_iterator_cast(__position);
2939 }
2940 else
2941 {
2942 vector __v(__alloc());
2943 __v.reserve(__recommend(__size_ + __n));
2944 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002945 __r = _VSTD::copy(cbegin(), __position, __v.begin());
2946 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002947 swap(__v);
2948 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002949 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002950 return __r;
2951}
2952
2953template <class _Allocator>
2954template <class _InputIterator>
2955typename enable_if
2956<
2957 __is_input_iterator <_InputIterator>::value &&
2958 !__is_forward_iterator<_InputIterator>::value,
2959 typename vector<bool, _Allocator>::iterator
2960>::type
2961vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
2962{
2963 difference_type __off = __position - begin();
2964 iterator __p = __const_iterator_cast(__position);
2965 iterator __old_end = end();
2966 for (; size() != capacity() && __first != __last; ++__first)
2967 {
2968 ++this->__size_;
2969 back() = *__first;
2970 }
2971 vector __v(__alloc());
2972 if (__first != __last)
2973 {
2974#ifndef _LIBCPP_NO_EXCEPTIONS
2975 try
2976 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002977#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002978 __v.assign(__first, __last);
2979 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
2980 difference_type __old_p = __p - begin();
2981 reserve(__recommend(size() + __v.size()));
2982 __p = begin() + __old_p;
2983 __old_end = begin() + __old_size;
2984#ifndef _LIBCPP_NO_EXCEPTIONS
2985 }
2986 catch (...)
2987 {
2988 erase(__old_end, end());
2989 throw;
2990 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002991#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002992 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002993 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002994 insert(__p, __v.begin(), __v.end());
2995 return begin() + __off;
2996}
2997
2998template <class _Allocator>
2999template <class _ForwardIterator>
3000typename enable_if
3001<
3002 __is_forward_iterator<_ForwardIterator>::value,
3003 typename vector<bool, _Allocator>::iterator
3004>::type
3005vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3006{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003007 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003008 iterator __r;
3009 size_type __c = capacity();
3010 if (__n <= __c && size() <= __c - __n)
3011 {
3012 const_iterator __old_end = end();
3013 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003014 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003015 __r = __const_iterator_cast(__position);
3016 }
3017 else
3018 {
3019 vector __v(__alloc());
3020 __v.reserve(__recommend(__size_ + __n));
3021 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003022 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3023 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003024 swap(__v);
3025 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003026 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003027 return __r;
3028}
3029
3030template <class _Allocator>
3031_LIBCPP_INLINE_VISIBILITY inline
3032typename vector<bool, _Allocator>::iterator
3033vector<bool, _Allocator>::erase(const_iterator __position)
3034{
3035 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003036 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003037 --__size_;
3038 return __r;
3039}
3040
3041template <class _Allocator>
3042typename vector<bool, _Allocator>::iterator
3043vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3044{
3045 iterator __r = __const_iterator_cast(__first);
3046 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003047 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003048 __size_ -= __d;
3049 return __r;
3050}
3051
3052template <class _Allocator>
3053void
3054vector<bool, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003055 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3056 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003057{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003058 _VSTD::swap(this->__begin_, __x.__begin_);
3059 _VSTD::swap(this->__size_, __x.__size_);
3060 _VSTD::swap(this->__cap(), __x.__cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003061 __swap_alloc(this->__alloc(), __x.__alloc());
3062#ifdef _LIBCPP_DEBUG
3063 iterator::swap(this, &__x);
3064 const_iterator::swap(this, &__x);
Howard Hinnant324bb032010-08-22 00:02:43 +00003065#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003066}
3067
Howard Hinnant324bb032010-08-22 00:02:43 +00003068template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003069void
3070vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3071{
3072 size_type __cs = size();
3073 if (__cs < __sz)
3074 {
3075 iterator __r;
3076 size_type __c = capacity();
3077 size_type __n = __sz - __cs;
3078 if (__n <= __c && __cs <= __c - __n)
3079 {
3080 __r = end();
3081 __size_ += __n;
3082 }
3083 else
3084 {
3085 vector __v(__alloc());
3086 __v.reserve(__recommend(__size_ + __n));
3087 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003088 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003089 swap(__v);
3090 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003091 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003092 }
3093 else
3094 __size_ = __sz;
3095}
3096
Howard Hinnant324bb032010-08-22 00:02:43 +00003097template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003098void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003099vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003100{
3101 // do middle whole words
3102 size_type __n = __size_;
3103 __storage_pointer __p = __begin_;
3104 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3105 *__p = ~*__p;
3106 // do last partial word
3107 if (__n > 0)
3108 {
3109 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3110 __storage_type __b = *__p & __m;
3111 *__p &= ~__m;
3112 *__p |= ~__b & __m;
3113 }
3114}
3115
Howard Hinnant324bb032010-08-22 00:02:43 +00003116template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003117bool
3118vector<bool, _Allocator>::__invariants() const
3119{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00003120 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003121 {
3122 if (this->__size_ != 0 || this->__cap() != 0)
3123 return false;
3124 }
3125 else
3126 {
3127 if (this->__cap() == 0)
3128 return false;
3129 if (this->__size_ > this->capacity())
3130 return false;
3131 }
3132 return true;
3133}
3134
Howard Hinnant324bb032010-08-22 00:02:43 +00003135template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003136size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003137vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003138{
3139 size_t __h = 0;
3140 // do middle whole words
3141 size_type __n = __size_;
3142 __storage_pointer __p = __begin_;
3143 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3144 __h ^= *__p;
3145 // do last partial word
3146 if (__n > 0)
3147 {
3148 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3149 __h ^= *__p & __m;
3150 }
3151 return __h;
3152}
3153
3154template <class _Allocator>
Howard Hinnant83eade62013-03-06 23:30:19 +00003155struct _LIBCPP_TYPE_VIS hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003156 : public unary_function<vector<bool, _Allocator>, size_t>
3157{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00003158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003159 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003160 {return __vec.__hash_code();}
3161};
3162
3163template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003164_LIBCPP_INLINE_VISIBILITY inline
3165bool
3166operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3167{
3168 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003169 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003170}
3171
3172template <class _Tp, class _Allocator>
3173_LIBCPP_INLINE_VISIBILITY inline
3174bool
3175operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3176{
3177 return !(__x == __y);
3178}
3179
3180template <class _Tp, class _Allocator>
3181_LIBCPP_INLINE_VISIBILITY inline
3182bool
3183operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3184{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003185 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003186}
3187
3188template <class _Tp, class _Allocator>
3189_LIBCPP_INLINE_VISIBILITY inline
3190bool
3191operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3192{
3193 return __y < __x;
3194}
3195
3196template <class _Tp, class _Allocator>
3197_LIBCPP_INLINE_VISIBILITY inline
3198bool
3199operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3200{
3201 return !(__x < __y);
3202}
3203
3204template <class _Tp, class _Allocator>
3205_LIBCPP_INLINE_VISIBILITY inline
3206bool
3207operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3208{
3209 return !(__y < __x);
3210}
3211
3212template <class _Tp, class _Allocator>
3213_LIBCPP_INLINE_VISIBILITY inline
3214void
3215swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003216 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003217{
3218 __x.swap(__y);
3219}
3220
3221_LIBCPP_END_NAMESPACE_STD
3222
3223#endif // _LIBCPP_VECTOR