blob: ec413fa6e8ebc8022297d7f1980de81b9e5e11fd [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);
Marshall Clowa49a2c92013-09-14 00:47:59 +000041 explicit vector(size_type n, const allocator_type&); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
43 template <class InputIterator>
44 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
45 vector(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +000046 vector(vector&& x)
47 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000048 vector(initializer_list<value_type> il);
49 vector(initializer_list<value_type> il, const allocator_type& a);
50 ~vector();
51 vector& operator=(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +000052 vector& operator=(vector&& x)
53 noexcept(
54 allocator_type::propagate_on_container_move_assignment::value &&
55 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056 vector& operator=(initializer_list<value_type> il);
57 template <class InputIterator>
58 void assign(InputIterator first, InputIterator last);
59 void assign(size_type n, const value_type& u);
60 void assign(initializer_list<value_type> il);
61
Howard Hinnantd1d27a42011-06-03 19:40:40 +000062 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063
Howard Hinnantd1d27a42011-06-03 19:40:40 +000064 iterator begin() noexcept;
65 const_iterator begin() const noexcept;
66 iterator end() noexcept;
67 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000068
Howard Hinnantd1d27a42011-06-03 19:40:40 +000069 reverse_iterator rbegin() noexcept;
70 const_reverse_iterator rbegin() const noexcept;
71 reverse_iterator rend() noexcept;
72 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000073
Howard Hinnantd1d27a42011-06-03 19:40:40 +000074 const_iterator cbegin() const noexcept;
75 const_iterator cend() const noexcept;
76 const_reverse_iterator crbegin() const noexcept;
77 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000078
Howard Hinnantd1d27a42011-06-03 19:40:40 +000079 size_type size() const noexcept;
80 size_type max_size() const noexcept;
81 size_type capacity() const noexcept;
82 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +000084 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000085
86 reference operator[](size_type n);
87 const_reference operator[](size_type n) const;
88 reference at(size_type n);
89 const_reference at(size_type n) const;
90
91 reference front();
92 const_reference front() const;
93 reference back();
94 const_reference back() const;
95
Howard Hinnantd1d27a42011-06-03 19:40:40 +000096 value_type* data() noexcept;
97 const value_type* data() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098
99 void push_back(const value_type& x);
100 void push_back(value_type&& x);
101 template <class... Args>
102 void emplace_back(Args&&... args);
103 void pop_back();
104
105 template <class... Args> iterator emplace(const_iterator position, Args&&... args);
106 iterator insert(const_iterator position, const value_type& x);
107 iterator insert(const_iterator position, value_type&& x);
108 iterator insert(const_iterator position, size_type n, const value_type& x);
109 template <class InputIterator>
110 iterator insert(const_iterator position, InputIterator first, InputIterator last);
111 iterator insert(const_iterator position, initializer_list<value_type> il);
112
113 iterator erase(const_iterator position);
114 iterator erase(const_iterator first, const_iterator last);
115
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000116 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000117
118 void resize(size_type sz);
119 void resize(size_type sz, const value_type& c);
120
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000121 void swap(vector&)
122 noexcept(!allocator_type::propagate_on_container_swap::value ||
123 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000124
125 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000126};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000127
Howard Hinnant324bb032010-08-22 00:02:43 +0000128template <class Allocator = allocator<T> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129class vector<bool, Allocator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000130{
131public:
132 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000133 typedef Allocator allocator_type;
134 typedef implementation-defined iterator;
135 typedef implementation-defined const_iterator;
136 typedef typename allocator_type::size_type size_type;
137 typedef typename allocator_type::difference_type difference_type;
138 typedef iterator pointer;
139 typedef const_iterator const_pointer;
140 typedef std::reverse_iterator<iterator> reverse_iterator;
141 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
142
143 class reference
144 {
145 public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000146 reference(const reference&) noexcept;
147 operator bool() const noexcept;
148 reference& operator=(const bool x) noexcept;
149 reference& operator=(const reference& x) noexcept;
150 iterator operator&() const noexcept;
151 void flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152 };
153
154 class const_reference
155 {
156 public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000157 const_reference(const reference&) noexcept;
158 operator bool() const noexcept;
159 const_iterator operator&() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160 };
161
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000162 vector()
163 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant9cbee432011-09-02 20:42:31 +0000164 explicit vector(const allocator_type&);
Marshall Clowa49a2c92013-09-14 00:47:59 +0000165 explicit vector(size_type n, const allocator_type& a = allocator_type()); // C++14
166 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000167 template <class InputIterator>
168 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
169 vector(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000170 vector(vector&& x)
171 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000172 vector(initializer_list<value_type> il);
173 vector(initializer_list<value_type> il, const allocator_type& a);
174 ~vector();
175 vector& operator=(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000176 vector& operator=(vector&& x)
177 noexcept(
178 allocator_type::propagate_on_container_move_assignment::value &&
179 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000180 vector& operator=(initializer_list<value_type> il);
181 template <class InputIterator>
182 void assign(InputIterator first, InputIterator last);
183 void assign(size_type n, const value_type& u);
184 void assign(initializer_list<value_type> il);
185
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000186 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000188 iterator begin() noexcept;
189 const_iterator begin() const noexcept;
190 iterator end() noexcept;
191 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000192
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000193 reverse_iterator rbegin() noexcept;
194 const_reverse_iterator rbegin() const noexcept;
195 reverse_iterator rend() noexcept;
196 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000197
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000198 const_iterator cbegin() const noexcept;
199 const_iterator cend() const noexcept;
200 const_reverse_iterator crbegin() const noexcept;
201 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000203 size_type size() const noexcept;
204 size_type max_size() const noexcept;
205 size_type capacity() const noexcept;
206 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000207 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000208 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000209
210 reference operator[](size_type n);
211 const_reference operator[](size_type n) const;
212 reference at(size_type n);
213 const_reference at(size_type n) const;
214
215 reference front();
216 const_reference front() const;
217 reference back();
218 const_reference back() const;
219
220 void push_back(const value_type& x);
Marshall Clow198a2a52013-08-13 23:54:12 +0000221 template <class... Args> void emplace_back(Args&&... args); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000222 void pop_back();
223
Marshall Clow198a2a52013-08-13 23:54:12 +0000224 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000225 iterator insert(const_iterator position, const value_type& x);
226 iterator insert(const_iterator position, size_type n, const value_type& x);
227 template <class InputIterator>
228 iterator insert(const_iterator position, InputIterator first, InputIterator last);
229 iterator insert(const_iterator position, initializer_list<value_type> il);
230
231 iterator erase(const_iterator position);
232 iterator erase(const_iterator first, const_iterator last);
233
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000234 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235
236 void resize(size_type sz);
237 void resize(size_type sz, value_type x);
238
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000239 void swap(vector&)
240 noexcept(!allocator_type::propagate_on_container_swap::value ||
241 __is_nothrow_swappable<allocator_type>::value);
242 void flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243
244 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000245};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000246
247template <class Allocator> struct hash<std::vector<bool, Allocator>>;
248
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);
251template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
252template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
253template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
254template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
255
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000256template <class T, class Allocator>
257void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
258 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000259
260} // std
261
262*/
263
264#include <__config>
265#include <__bit_reference>
266#include <type_traits>
267#include <climits>
268#include <limits>
269#include <initializer_list>
270#include <memory>
271#include <stdexcept>
272#include <algorithm>
273#include <cstring>
274#include <__split_buffer>
275#include <__functional_base>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000276
Howard Hinnant66c6f972011-11-29 16:45:27 +0000277#include <__undef_min_max>
278
Eric Fiselierb9536102014-08-10 23:53:08 +0000279#include <__debug>
Howard Hinnant8b00e6c2013-08-02 00:26:35 +0000280
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 Hinnant0f678bd2013-08-12 18:38:34 +0000322_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS __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>
Howard Hinnant1e564242013-10-04 22:09:00 +0000439inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000440void
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>
Howard Hinnant1e564242013-10-04 22:09:00 +0000448inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000449__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>
Howard Hinnant1e564242013-10-04 22:09:00 +0000458inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000459__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 Hinnant0f678bd2013-08-12 18:38:34 +0000477class _LIBCPP_TYPE_VIS_ONLY vector
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000478 : private __vector_base<_Tp, _Allocator>
479{
480private:
481 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000482 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant324bb032010-08-22 00:02:43 +0000483public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000484 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +0000485 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000486 typedef _Allocator allocator_type;
487 typedef typename __base::__alloc_traits __alloc_traits;
488 typedef typename __base::reference reference;
489 typedef typename __base::const_reference const_reference;
490 typedef typename __base::size_type size_type;
491 typedef typename __base::difference_type difference_type;
492 typedef typename __base::pointer pointer;
493 typedef typename __base::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000494 typedef __wrap_iter<pointer> iterator;
495 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000496 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
497 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000498
Howard Hinnant02d5e182013-03-26 19:04:56 +0000499 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
500 "Allocator::value_type must be same type as value_type");
501
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000502 _LIBCPP_INLINE_VISIBILITY
503 vector()
Marshall Clow127db912015-06-04 00:10:20 +0000504#if _LIBCPP_STD_VER <= 14
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000505 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Marshall Clow127db912015-06-04 00:10:20 +0000506#else
507 _NOEXCEPT
508#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +0000509 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000510#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000511 __get_db()->__insert_c(this);
512#endif
513 }
514 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow127db912015-06-04 00:10:20 +0000515#if _LIBCPP_STD_VER <= 14
516 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
517#else
518 _NOEXCEPT
519#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +0000520 : __base(__a)
521 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000522#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000523 __get_db()->__insert_c(this);
524#endif
525 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000526 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59 +0000527#if _LIBCPP_STD_VER > 11
528 explicit vector(size_type __n, const allocator_type& __a);
529#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000530 vector(size_type __n, const_reference __x);
531 vector(size_type __n, const_reference __x, const allocator_type& __a);
532 template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +0000533 vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000534 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000535 !__is_forward_iterator<_InputIterator>::value &&
536 is_constructible<
537 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +0000538 typename iterator_traits<_InputIterator>::reference>::value,
539 _InputIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000540 template <class _InputIterator>
541 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
542 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000543 !__is_forward_iterator<_InputIterator>::value &&
544 is_constructible<
545 value_type,
546 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000547 template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +0000548 vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000549 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
550 is_constructible<
551 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +0000552 typename iterator_traits<_ForwardIterator>::reference>::value,
553 _ForwardIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000554 template <class _ForwardIterator>
555 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000556 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
557 is_constructible<
558 value_type,
559 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000560#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000562 vector(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000564 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000565#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantabe26282011-09-16 17:29:17 +0000566#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7a563db2011-09-14 18:33:51 +0000568 ~vector()
569 {
570 __get_db()->__erase_c(this);
571 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572#endif
573
574 vector(const vector& __x);
575 vector(const vector& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000577 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000578#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000580 vector(vector&& __x)
581 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000583 vector(vector&& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000584 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000585 vector& operator=(vector&& __x)
586 _NOEXCEPT_(
587 __alloc_traits::propagate_on_container_move_assignment::value &&
588 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000589#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000590#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000591 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000592 vector& operator=(initializer_list<value_type> __il)
593 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000594#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000595
596 template <class _InputIterator>
597 typename enable_if
598 <
599 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000600 !__is_forward_iterator<_InputIterator>::value &&
601 is_constructible<
602 value_type,
603 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000604 void
605 >::type
606 assign(_InputIterator __first, _InputIterator __last);
607 template <class _ForwardIterator>
608 typename enable_if
609 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000610 __is_forward_iterator<_ForwardIterator>::value &&
611 is_constructible<
612 value_type,
613 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000614 void
615 >::type
616 assign(_ForwardIterator __first, _ForwardIterator __last);
617
618 void assign(size_type __n, const_reference __u);
Howard Hinnante3e32912011-08-12 21:56:02 +0000619#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000620 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000621 void assign(initializer_list<value_type> __il)
622 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000623#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000624
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000625 _LIBCPP_INLINE_VISIBILITY
626 allocator_type get_allocator() const _NOEXCEPT
627 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000628
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000629 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
630 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
631 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
632 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000633
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000634 _LIBCPP_INLINE_VISIBILITY
635 reverse_iterator rbegin() _NOEXCEPT
636 {return reverse_iterator(end());}
637 _LIBCPP_INLINE_VISIBILITY
638 const_reverse_iterator rbegin() const _NOEXCEPT
639 {return const_reverse_iterator(end());}
640 _LIBCPP_INLINE_VISIBILITY
641 reverse_iterator rend() _NOEXCEPT
642 {return reverse_iterator(begin());}
643 _LIBCPP_INLINE_VISIBILITY
644 const_reverse_iterator rend() const _NOEXCEPT
645 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000646
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000647 _LIBCPP_INLINE_VISIBILITY
648 const_iterator cbegin() const _NOEXCEPT
649 {return begin();}
650 _LIBCPP_INLINE_VISIBILITY
651 const_iterator cend() const _NOEXCEPT
652 {return end();}
653 _LIBCPP_INLINE_VISIBILITY
654 const_reverse_iterator crbegin() const _NOEXCEPT
655 {return rbegin();}
656 _LIBCPP_INLINE_VISIBILITY
657 const_reverse_iterator crend() const _NOEXCEPT
658 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000659
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000660 _LIBCPP_INLINE_VISIBILITY
661 size_type size() const _NOEXCEPT
662 {return static_cast<size_type>(this->__end_ - this->__begin_);}
663 _LIBCPP_INLINE_VISIBILITY
664 size_type capacity() const _NOEXCEPT
665 {return __base::capacity();}
666 _LIBCPP_INLINE_VISIBILITY
667 bool empty() const _NOEXCEPT
668 {return this->__begin_ == this->__end_;}
669 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000670 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000671 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000672
673 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
674 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
675 reference at(size_type __n);
676 const_reference at(size_type __n) const;
677
Howard Hinnant7a563db2011-09-14 18:33:51 +0000678 _LIBCPP_INLINE_VISIBILITY reference front()
679 {
680 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
681 return *this->__begin_;
682 }
683 _LIBCPP_INLINE_VISIBILITY const_reference front() const
684 {
685 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
686 return *this->__begin_;
687 }
688 _LIBCPP_INLINE_VISIBILITY reference back()
689 {
690 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
691 return *(this->__end_ - 1);
692 }
693 _LIBCPP_INLINE_VISIBILITY const_reference back() const
694 {
695 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
696 return *(this->__end_ - 1);
697 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000698
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000699 _LIBCPP_INLINE_VISIBILITY
700 value_type* data() _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000701 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000702 _LIBCPP_INLINE_VISIBILITY
703 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000704 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000705
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000706 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000707#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000708 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000709#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000710 template <class... _Args>
711 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000712#endif // _LIBCPP_HAS_NO_VARIADICS
713#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000714 void pop_back();
715
716 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000717#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000718 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000719#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000720 template <class... _Args>
721 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000722#endif // _LIBCPP_HAS_NO_VARIADICS
723#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000724 iterator insert(const_iterator __position, size_type __n, const_reference __x);
725 template <class _InputIterator>
726 typename enable_if
727 <
728 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000729 !__is_forward_iterator<_InputIterator>::value &&
730 is_constructible<
731 value_type,
732 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000733 iterator
734 >::type
735 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
736 template <class _ForwardIterator>
737 typename enable_if
738 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000739 __is_forward_iterator<_ForwardIterator>::value &&
740 is_constructible<
741 value_type,
742 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000743 iterator
744 >::type
745 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +0000746#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000747 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000748 iterator insert(const_iterator __position, initializer_list<value_type> __il)
749 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000750#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000751
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000752 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000753 iterator erase(const_iterator __first, const_iterator __last);
754
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000755 _LIBCPP_INLINE_VISIBILITY
756 void clear() _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000757 {
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000758 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000759 __base::clear();
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000760 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000761 __invalidate_all_iterators();
762 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000763
764 void resize(size_type __sz);
765 void resize(size_type __sz, const_reference __x);
766
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000767 void swap(vector&)
768 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
769 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000770
771 bool __invariants() const;
772
Howard Hinnantabe26282011-09-16 17:29:17 +0000773#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000774
775 bool __dereferenceable(const const_iterator* __i) const;
776 bool __decrementable(const const_iterator* __i) const;
777 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
778 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
779
Howard Hinnantabe26282011-09-16 17:29:17 +0000780#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000781
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000782private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000783 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000784 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000785 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000786 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31 +0000787 void __construct_at_end(size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000788 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000789 template <class _ForwardIterator>
790 typename enable_if
791 <
792 __is_forward_iterator<_ForwardIterator>::value,
793 void
794 >::type
Eric Fiselier088ed9f2015-03-31 16:54:19 +0000795 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000796 void __append(size_type __n);
797 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000798 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000799 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000800 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000801 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000802 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
803 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
804 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000805 void __move_assign(vector& __c, true_type)
806 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000807 void __move_assign(vector& __c, false_type);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000809 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000810 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000811#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000812 __c_node* __c = __get_db()->__find_c_and_lock(this);
813 for (__i_node** __p = __c->end_; __p != __c->beg_; )
814 {
815 --__p;
816 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
817 if (__i->base() > __new_last)
818 {
819 (*__p)->__c_ = nullptr;
820 if (--__c->end_ != __p)
821 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
822 }
823 }
824 __get_db()->unlock();
825#endif
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000826 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000827 __base::__destruct_at_end(__new_last);
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000828 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000829 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000830 template <class _Up>
831 void
832#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
833 __push_back_slow_path(_Up&& __x);
834#else
835 __push_back_slow_path(_Up& __x);
836#endif
Howard Hinnant0438ea22012-02-26 15:30:12 +0000837#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
838 template <class... _Args>
839 void
840 __emplace_back_slow_path(_Args&&... __args);
841#endif
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000842 // The following functions are no-ops outside of AddressSanitizer mode.
843 // We call annotatations only for the default Allocator because other allocators
844 // may not meet the AddressSanitizer alignment constraints.
845 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
846 void __annotate_contiguous_container
Kostya Serebryany497f9122014-09-02 23:43:38 +0000847 (const void *__beg, const void *__end, const void *__old_mid, const void *__new_mid) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000848 {
849#ifndef _LIBCPP_HAS_NO_ASAN
850 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
851 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
852#endif
853 }
854
Kostya Serebryany497f9122014-09-02 23:43:38 +0000855 void __annotate_new(size_type __current_size) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000856 {
857 __annotate_contiguous_container(data(), data() + capacity(),
858 data() + capacity(), data() + __current_size);
859 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000860 void __annotate_delete() const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000861 {
862 __annotate_contiguous_container(data(), data() + capacity(),
863 data() + size(), data() + capacity());
864 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000865 void __annotate_increase(size_type __n) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000866 {
867 __annotate_contiguous_container(data(), data() + capacity(),
868 data() + size(), data() + size() + __n);
869 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000870 void __annotate_shrink(size_type __old_size) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000871 {
872 __annotate_contiguous_container(data(), data() + capacity(),
873 data() + __old_size, data() + size());
874 }
Marshall Clow26f472d2014-09-03 21:37:43 +0000875#ifndef _LIBCPP_HAS_NO_ASAN
Kostya Serebryany497f9122014-09-02 23:43:38 +0000876 // The annotation for size increase should happen before the actual increase,
877 // but if an exception is thrown after that the annotation has to be undone.
878 struct __RAII_IncreaseAnnotator {
879 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000880 : __commit(false), __v(__v), __old_size(__v.size() + __n) {
Kostya Serebryany497f9122014-09-02 23:43:38 +0000881 __v.__annotate_increase(__n);
882 }
883 void __done() { __commit = true; }
884 ~__RAII_IncreaseAnnotator() {
885 if (__commit) return;
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000886 __v.__annotate_shrink(__old_size);
Kostya Serebryany497f9122014-09-02 23:43:38 +0000887 }
888 bool __commit;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000889 const vector &__v;
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000890 size_type __old_size;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000891 };
Marshall Clow26f472d2014-09-03 21:37:43 +0000892#else
893 struct __RAII_IncreaseAnnotator {
894 inline __RAII_IncreaseAnnotator(const vector &, size_type __n = 1) {}
895 inline void __done() {}
896 };
897#endif
898
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000899};
900
901template <class _Tp, class _Allocator>
902void
903vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
904{
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000905 __annotate_delete();
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000906 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000907 _VSTD::swap(this->__begin_, __v.__begin_);
908 _VSTD::swap(this->__end_, __v.__end_);
909 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000910 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000911 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000912 __invalidate_all_iterators();
913}
914
915template <class _Tp, class _Allocator>
916typename vector<_Tp, _Allocator>::pointer
917vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
918{
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000919 __annotate_delete();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000920 pointer __r = __v.__begin_;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000921 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
922 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000923 _VSTD::swap(this->__begin_, __v.__begin_);
924 _VSTD::swap(this->__end_, __v.__end_);
925 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000926 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000927 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000928 __invalidate_all_iterators();
929 return __r;
930}
931
932// Allocate space for __n objects
933// throws length_error if __n > max_size()
934// throws (probably bad_alloc) if memory run out
935// Precondition: __begin_ == __end_ == __end_cap() == 0
936// Precondition: __n > 0
937// Postcondition: capacity() == __n
938// Postcondition: size() == 0
939template <class _Tp, class _Allocator>
940void
941vector<_Tp, _Allocator>::allocate(size_type __n)
942{
943 if (__n > max_size())
944 this->__throw_length_error();
945 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
946 this->__end_cap() = this->__begin_ + __n;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000947 __annotate_new(0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000948}
949
950template <class _Tp, class _Allocator>
951void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000952vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000953{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000954 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000955 {
956 clear();
957 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000958 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000959 }
960}
961
962template <class _Tp, class _Allocator>
963typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000964vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000965{
Sean Hunt110b8bf2011-07-29 23:31:58 +0000966 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 +0000967}
968
969// Precondition: __new_size > capacity()
970template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000971inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000972typename vector<_Tp, _Allocator>::size_type
973vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
974{
975 const size_type __ms = max_size();
976 if (__new_size > __ms)
977 this->__throw_length_error();
978 const size_type __cap = capacity();
979 if (__cap >= __ms / 2)
980 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58 +0000981 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000982}
983
984// Default constructs __n objects starting at __end_
985// throws if construction throws
986// Precondition: __n > 0
987// Precondition: size() + __n <= capacity()
988// Postcondition: size() == size() + __n
989template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990void
991vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
992{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000993 allocator_type& __a = this->__alloc();
994 do
995 {
Kostya Serebryany497f9122014-09-02 23:43:38 +0000996 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000997 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000998 ++this->__end_;
999 --__n;
Kostya Serebryany497f9122014-09-02 23:43:38 +00001000 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001001 } while (__n > 0);
1002}
1003
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001004// Copy constructs __n objects starting at __end_ from __x
1005// throws if construction throws
1006// Precondition: __n > 0
1007// Precondition: size() + __n <= capacity()
1008// Postcondition: size() == old size() + __n
1009// Postcondition: [i] == __x for all i in [size() - __n, __n)
1010template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001011inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001012void
1013vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1014{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001015 allocator_type& __a = this->__alloc();
1016 do
1017 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001018 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001019 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001020 ++this->__end_;
1021 --__n;
Kostya Serebryany497f9122014-09-02 23:43:38 +00001022 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001023 } while (__n > 0);
1024}
1025
1026template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001027template <class _ForwardIterator>
1028typename enable_if
1029<
1030 __is_forward_iterator<_ForwardIterator>::value,
1031 void
1032>::type
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001033vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001034{
1035 allocator_type& __a = this->__alloc();
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001036 __RAII_IncreaseAnnotator __annotator(*this, __n);
1037 __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
1038 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001039}
1040
1041// Default constructs __n objects starting at __end_
1042// throws if construction throws
1043// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001044// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001045template <class _Tp, class _Allocator>
1046void
1047vector<_Tp, _Allocator>::__append(size_type __n)
1048{
1049 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1050 this->__construct_at_end(__n);
1051 else
1052 {
1053 allocator_type& __a = this->__alloc();
1054 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1055 __v.__construct_at_end(__n);
1056 __swap_out_circular_buffer(__v);
1057 }
1058}
1059
1060// Default constructs __n objects starting at __end_
1061// throws if construction throws
1062// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001063// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001064template <class _Tp, class _Allocator>
1065void
1066vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1067{
1068 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1069 this->__construct_at_end(__n, __x);
1070 else
1071 {
1072 allocator_type& __a = this->__alloc();
1073 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1074 __v.__construct_at_end(__n, __x);
1075 __swap_out_circular_buffer(__v);
1076 }
1077}
1078
1079template <class _Tp, class _Allocator>
1080vector<_Tp, _Allocator>::vector(size_type __n)
1081{
Howard Hinnant0442b122011-09-16 18:41:29 +00001082#if _LIBCPP_DEBUG_LEVEL >= 2
1083 __get_db()->__insert_c(this);
1084#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001085 if (__n > 0)
1086 {
1087 allocate(__n);
1088 __construct_at_end(__n);
1089 }
1090}
1091
Marshall Clowa49a2c92013-09-14 00:47:59 +00001092#if _LIBCPP_STD_VER > 11
1093template <class _Tp, class _Allocator>
1094vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1095 : __base(__a)
1096{
1097#if _LIBCPP_DEBUG_LEVEL >= 2
1098 __get_db()->__insert_c(this);
1099#endif
1100 if (__n > 0)
1101 {
1102 allocate(__n);
1103 __construct_at_end(__n);
1104 }
1105}
1106#endif
1107
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001108template <class _Tp, class _Allocator>
1109vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1110{
Howard Hinnant0442b122011-09-16 18:41:29 +00001111#if _LIBCPP_DEBUG_LEVEL >= 2
1112 __get_db()->__insert_c(this);
1113#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001114 if (__n > 0)
1115 {
1116 allocate(__n);
1117 __construct_at_end(__n, __x);
1118 }
1119}
1120
1121template <class _Tp, class _Allocator>
1122vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1123 : __base(__a)
1124{
Howard Hinnant0442b122011-09-16 18:41:29 +00001125#if _LIBCPP_DEBUG_LEVEL >= 2
1126 __get_db()->__insert_c(this);
1127#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001128 if (__n > 0)
1129 {
1130 allocate(__n);
1131 __construct_at_end(__n, __x);
1132 }
1133}
1134
1135template <class _Tp, class _Allocator>
1136template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +00001137vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001138 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001139 !__is_forward_iterator<_InputIterator>::value &&
1140 is_constructible<
1141 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +00001142 typename iterator_traits<_InputIterator>::reference>::value,
1143 _InputIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001144{
Howard Hinnantabe26282011-09-16 17:29:17 +00001145#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001146 __get_db()->__insert_c(this);
1147#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001148 for (; __first != __last; ++__first)
1149 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001150}
1151
1152template <class _Tp, class _Allocator>
1153template <class _InputIterator>
1154vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1155 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001156 !__is_forward_iterator<_InputIterator>::value &&
1157 is_constructible<
1158 value_type,
1159 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001160 : __base(__a)
1161{
Howard Hinnantabe26282011-09-16 17:29:17 +00001162#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001163 __get_db()->__insert_c(this);
1164#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001165 for (; __first != __last; ++__first)
1166 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001167}
1168
1169template <class _Tp, class _Allocator>
1170template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +00001171vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001172 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1173 is_constructible<
1174 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +00001175 typename iterator_traits<_ForwardIterator>::reference>::value,
1176 _ForwardIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001177{
Howard Hinnant0442b122011-09-16 18:41:29 +00001178#if _LIBCPP_DEBUG_LEVEL >= 2
1179 __get_db()->__insert_c(this);
1180#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001181 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001182 if (__n > 0)
1183 {
1184 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001185 __construct_at_end(__first, __last, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001186 }
1187}
1188
1189template <class _Tp, class _Allocator>
1190template <class _ForwardIterator>
1191vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001192 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1193 is_constructible<
1194 value_type,
1195 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001196 : __base(__a)
1197{
Howard Hinnant0442b122011-09-16 18:41:29 +00001198#if _LIBCPP_DEBUG_LEVEL >= 2
1199 __get_db()->__insert_c(this);
1200#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001201 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001202 if (__n > 0)
1203 {
1204 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001205 __construct_at_end(__first, __last, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001206 }
1207}
1208
1209template <class _Tp, class _Allocator>
1210vector<_Tp, _Allocator>::vector(const vector& __x)
1211 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1212{
Howard Hinnant0442b122011-09-16 18:41:29 +00001213#if _LIBCPP_DEBUG_LEVEL >= 2
1214 __get_db()->__insert_c(this);
1215#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001216 size_type __n = __x.size();
1217 if (__n > 0)
1218 {
1219 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001220 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001221 }
1222}
1223
1224template <class _Tp, class _Allocator>
1225vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1226 : __base(__a)
1227{
Howard Hinnant0442b122011-09-16 18:41:29 +00001228#if _LIBCPP_DEBUG_LEVEL >= 2
1229 __get_db()->__insert_c(this);
1230#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001231 size_type __n = __x.size();
1232 if (__n > 0)
1233 {
1234 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001235 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001236 }
1237}
1238
Howard Hinnant73d21a42010-09-04 23:28:19 +00001239#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001240
1241template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001242inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001243vector<_Tp, _Allocator>::vector(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001244 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001245 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001246{
Howard Hinnantabe26282011-09-16 17:29:17 +00001247#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001248 __get_db()->__insert_c(this);
Howard Hinnante6125bd2011-09-19 16:34:29 +00001249 __get_db()->swap(this, &__x);
Howard Hinnant7a563db2011-09-14 18:33:51 +00001250#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001251 this->__begin_ = __x.__begin_;
1252 this->__end_ = __x.__end_;
1253 this->__end_cap() = __x.__end_cap();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001254 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001255}
1256
1257template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001258inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001259vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1260 : __base(__a)
1261{
Howard Hinnant0442b122011-09-16 18:41:29 +00001262#if _LIBCPP_DEBUG_LEVEL >= 2
1263 __get_db()->__insert_c(this);
1264#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001265 if (__a == __x.__alloc())
1266 {
1267 this->__begin_ = __x.__begin_;
1268 this->__end_ = __x.__end_;
1269 this->__end_cap() = __x.__end_cap();
1270 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001271#if _LIBCPP_DEBUG_LEVEL >= 2
1272 __get_db()->swap(this, &__x);
1273#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001274 }
1275 else
1276 {
Howard Hinnant99968442011-11-29 18:15:50 +00001277 typedef move_iterator<iterator> _Ip;
1278 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001279 }
1280}
1281
Howard Hinnante3e32912011-08-12 21:56:02 +00001282#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1283
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001284template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001285inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001286vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1287{
Howard Hinnant0442b122011-09-16 18:41:29 +00001288#if _LIBCPP_DEBUG_LEVEL >= 2
1289 __get_db()->__insert_c(this);
1290#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001291 if (__il.size() > 0)
1292 {
1293 allocate(__il.size());
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001294 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001295 }
1296}
1297
1298template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001299inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001300vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1301 : __base(__a)
1302{
Howard Hinnant0442b122011-09-16 18:41:29 +00001303#if _LIBCPP_DEBUG_LEVEL >= 2
1304 __get_db()->__insert_c(this);
1305#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001306 if (__il.size() > 0)
1307 {
1308 allocate(__il.size());
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001309 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001310 }
1311}
1312
Howard Hinnante3e32912011-08-12 21:56:02 +00001313#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1314
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001315template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001316inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001317vector<_Tp, _Allocator>&
1318vector<_Tp, _Allocator>::operator=(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001319 _NOEXCEPT_(
1320 __alloc_traits::propagate_on_container_move_assignment::value &&
1321 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001322{
1323 __move_assign(__x, integral_constant<bool,
1324 __alloc_traits::propagate_on_container_move_assignment::value>());
1325 return *this;
1326}
1327
1328template <class _Tp, class _Allocator>
1329void
1330vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1331{
1332 if (__base::__alloc() != __c.__alloc())
1333 {
Howard Hinnant99968442011-11-29 18:15:50 +00001334 typedef move_iterator<iterator> _Ip;
1335 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001336 }
1337 else
1338 __move_assign(__c, true_type());
1339}
1340
1341template <class _Tp, class _Allocator>
1342void
1343vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001344 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001345{
1346 deallocate();
Marshall Clow3c2eac62014-07-21 15:11:13 +00001347 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001348 this->__begin_ = __c.__begin_;
1349 this->__end_ = __c.__end_;
1350 this->__end_cap() = __c.__end_cap();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001351 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001352#if _LIBCPP_DEBUG_LEVEL >= 2
1353 __get_db()->swap(this, &__c);
1354#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001355}
1356
Howard Hinnant73d21a42010-09-04 23:28:19 +00001357#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001358
1359template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001360inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001361vector<_Tp, _Allocator>&
1362vector<_Tp, _Allocator>::operator=(const vector& __x)
1363{
1364 if (this != &__x)
1365 {
1366 __base::__copy_assign_alloc(__x);
1367 assign(__x.__begin_, __x.__end_);
1368 }
1369 return *this;
1370}
1371
1372template <class _Tp, class _Allocator>
1373template <class _InputIterator>
1374typename enable_if
1375<
1376 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001377 !__is_forward_iterator<_InputIterator>::value &&
1378 is_constructible<
1379 _Tp,
1380 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001381 void
1382>::type
1383vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1384{
1385 clear();
1386 for (; __first != __last; ++__first)
1387 push_back(*__first);
1388}
1389
1390template <class _Tp, class _Allocator>
1391template <class _ForwardIterator>
1392typename enable_if
1393<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001394 __is_forward_iterator<_ForwardIterator>::value &&
1395 is_constructible<
1396 _Tp,
1397 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001398 void
1399>::type
1400vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1401{
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001402 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1403 if (__new_size <= capacity())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001404 {
1405 _ForwardIterator __mid = __last;
1406 bool __growing = false;
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001407 if (__new_size > size())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001408 {
1409 __growing = true;
1410 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001411 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001412 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001413 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001414 if (__growing)
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001415 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001416 else
1417 this->__destruct_at_end(__m);
1418 }
1419 else
1420 {
1421 deallocate();
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001422 allocate(__recommend(__new_size));
1423 __construct_at_end(__first, __last, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001424 }
1425}
1426
1427template <class _Tp, class _Allocator>
1428void
1429vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1430{
1431 if (__n <= capacity())
1432 {
1433 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001434 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001435 if (__n > __s)
1436 __construct_at_end(__n - __s, __u);
1437 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001438 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001439 }
1440 else
1441 {
1442 deallocate();
1443 allocate(__recommend(static_cast<size_type>(__n)));
1444 __construct_at_end(__n, __u);
1445 }
1446}
1447
Howard Hinnant324bb032010-08-22 00:02:43 +00001448template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001449inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001450typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001451vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001452{
Howard Hinnantabe26282011-09-16 17:29:17 +00001453#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001454 return iterator(this, __p);
1455#else
1456 return iterator(__p);
1457#endif
1458}
1459
Howard Hinnant324bb032010-08-22 00:02:43 +00001460template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001461inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001462typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001463vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001464{
Howard Hinnantabe26282011-09-16 17:29:17 +00001465#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001466 return const_iterator(this, __p);
1467#else
1468 return const_iterator(__p);
1469#endif
1470}
1471
Howard Hinnant324bb032010-08-22 00:02:43 +00001472template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001473inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001474typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001475vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001476{
1477 return __make_iter(this->__begin_);
1478}
1479
Howard Hinnant324bb032010-08-22 00:02:43 +00001480template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001481inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001482typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001483vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001484{
1485 return __make_iter(this->__begin_);
1486}
1487
Howard Hinnant324bb032010-08-22 00:02:43 +00001488template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001489inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001490typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001491vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001492{
1493 return __make_iter(this->__end_);
1494}
1495
Howard Hinnant324bb032010-08-22 00:02:43 +00001496template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001497inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001498typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001499vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001500{
1501 return __make_iter(this->__end_);
1502}
1503
Howard Hinnant324bb032010-08-22 00:02:43 +00001504template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001505inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506typename vector<_Tp, _Allocator>::reference
1507vector<_Tp, _Allocator>::operator[](size_type __n)
1508{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001509 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001510 return this->__begin_[__n];
1511}
1512
Howard Hinnant324bb032010-08-22 00:02:43 +00001513template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001514inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001515typename vector<_Tp, _Allocator>::const_reference
1516vector<_Tp, _Allocator>::operator[](size_type __n) const
1517{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001518 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001519 return this->__begin_[__n];
1520}
1521
Howard Hinnant324bb032010-08-22 00:02:43 +00001522template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001523typename vector<_Tp, _Allocator>::reference
1524vector<_Tp, _Allocator>::at(size_type __n)
1525{
1526 if (__n >= size())
1527 this->__throw_out_of_range();
1528 return this->__begin_[__n];
1529}
1530
Howard Hinnant324bb032010-08-22 00:02:43 +00001531template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001532typename vector<_Tp, _Allocator>::const_reference
1533vector<_Tp, _Allocator>::at(size_type __n) const
1534{
1535 if (__n >= size())
1536 this->__throw_out_of_range();
1537 return this->__begin_[__n];
1538}
1539
1540template <class _Tp, class _Allocator>
1541void
1542vector<_Tp, _Allocator>::reserve(size_type __n)
1543{
1544 if (__n > capacity())
1545 {
1546 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001547 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001548 __swap_out_circular_buffer(__v);
1549 }
1550}
1551
1552template <class _Tp, class _Allocator>
1553void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001554vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001555{
1556 if (capacity() > size())
1557 {
1558#ifndef _LIBCPP_NO_EXCEPTIONS
1559 try
1560 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001561#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001562 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001563 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001564 __swap_out_circular_buffer(__v);
1565#ifndef _LIBCPP_NO_EXCEPTIONS
1566 }
1567 catch (...)
1568 {
1569 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001570#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001571 }
1572}
1573
1574template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001575template <class _Up>
1576void
1577#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1578vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1579#else
1580vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1581#endif
1582{
1583 allocator_type& __a = this->__alloc();
1584 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1585 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnantf619e232013-01-11 20:36:59 +00001586 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1587 __v.__end_++;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001588 __swap_out_circular_buffer(__v);
1589}
1590
1591template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001592inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001593void
1594vector<_Tp, _Allocator>::push_back(const_reference __x)
1595{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001596 if (this->__end_ != this->__end_cap())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001598 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001599 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001600 _VSTD::__to_raw_pointer(this->__end_), __x);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001601 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001602 ++this->__end_;
1603 }
1604 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001605 __push_back_slow_path(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001606}
1607
Howard Hinnant73d21a42010-09-04 23:28:19 +00001608#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001609
1610template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001611inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001612void
1613vector<_Tp, _Allocator>::push_back(value_type&& __x)
1614{
1615 if (this->__end_ < this->__end_cap())
1616 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001617 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001619 _VSTD::__to_raw_pointer(this->__end_),
1620 _VSTD::move(__x));
Kostya Serebryany497f9122014-09-02 23:43:38 +00001621 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001622 ++this->__end_;
1623 }
1624 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001625 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001626}
1627
Howard Hinnant73d21a42010-09-04 23:28:19 +00001628#ifndef _LIBCPP_HAS_NO_VARIADICS
1629
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001630template <class _Tp, class _Allocator>
1631template <class... _Args>
1632void
Howard Hinnant0438ea22012-02-26 15:30:12 +00001633vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1634{
1635 allocator_type& __a = this->__alloc();
1636 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1637// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantf619e232013-01-11 20:36:59 +00001638 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1639 __v.__end_++;
Howard Hinnant0438ea22012-02-26 15:30:12 +00001640 __swap_out_circular_buffer(__v);
1641}
1642
1643template <class _Tp, class _Allocator>
1644template <class... _Args>
Howard Hinnant1e564242013-10-04 22:09:00 +00001645inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0438ea22012-02-26 15:30:12 +00001646void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001647vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1648{
1649 if (this->__end_ < this->__end_cap())
1650 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001651 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001652 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001653 _VSTD::__to_raw_pointer(this->__end_),
1654 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001655 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001656 ++this->__end_;
1657 }
1658 else
Howard Hinnant0438ea22012-02-26 15:30:12 +00001659 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001660}
1661
Howard Hinnant73d21a42010-09-04 23:28:19 +00001662#endif // _LIBCPP_HAS_NO_VARIADICS
1663#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001664
1665template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001666inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001667void
1668vector<_Tp, _Allocator>::pop_back()
1669{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001670 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001671 this->__destruct_at_end(this->__end_ - 1);
1672}
1673
1674template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001675inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676typename vector<_Tp, _Allocator>::iterator
1677vector<_Tp, _Allocator>::erase(const_iterator __position)
1678{
Howard Hinnantabe26282011-09-16 17:29:17 +00001679#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001680 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1681 "vector::erase(iterator) called with an iterator not"
1682 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001683#endif
Howard Hinnant782da332013-03-25 22:12:26 +00001684 _LIBCPP_ASSERT(__position != end(),
1685 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001686 difference_type __ps = __position - cbegin();
1687 pointer __p = this->__begin_ + __ps;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001688 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001689 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001690 return __r;
1691}
1692
1693template <class _Tp, class _Allocator>
1694typename vector<_Tp, _Allocator>::iterator
1695vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1696{
Howard Hinnantabe26282011-09-16 17:29:17 +00001697#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001698 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1699 "vector::erase(iterator, iterator) called with an iterator not"
1700 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001701#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +00001702 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001703 pointer __p = this->__begin_ + (__first - begin());
1704 iterator __r = __make_iter(__p);
Howard Hinnantb4e67cf2013-04-18 15:02:57 +00001705 if (__first != __last)
1706 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001707 return __r;
1708}
1709
1710template <class _Tp, class _Allocator>
1711void
1712vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1713{
1714 pointer __old_last = this->__end_;
1715 difference_type __n = __old_last - __to;
1716 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1717 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001718 _VSTD::__to_raw_pointer(this->__end_),
1719 _VSTD::move(*__i));
1720 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001721}
1722
1723template <class _Tp, class _Allocator>
1724typename vector<_Tp, _Allocator>::iterator
1725vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1726{
Howard Hinnantabe26282011-09-16 17:29:17 +00001727#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001728 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1729 "vector::insert(iterator, x) called with an iterator not"
1730 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001731#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001732 pointer __p = this->__begin_ + (__position - begin());
1733 if (this->__end_ < this->__end_cap())
1734 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001735 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001736 if (__p == this->__end_)
1737 {
1738 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001739 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001740 ++this->__end_;
1741 }
1742 else
1743 {
1744 __move_range(__p, this->__end_, __p + 1);
1745 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1746 if (__p <= __xr && __xr < this->__end_)
1747 ++__xr;
1748 *__p = *__xr;
1749 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001750 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001751 }
1752 else
1753 {
1754 allocator_type& __a = this->__alloc();
1755 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1756 __v.push_back(__x);
1757 __p = __swap_out_circular_buffer(__v, __p);
1758 }
1759 return __make_iter(__p);
1760}
1761
Howard Hinnant73d21a42010-09-04 23:28:19 +00001762#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001763
1764template <class _Tp, class _Allocator>
1765typename vector<_Tp, _Allocator>::iterator
1766vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1767{
Howard Hinnantabe26282011-09-16 17:29:17 +00001768#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001769 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1770 "vector::insert(iterator, x) called with an iterator not"
1771 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001772#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001773 pointer __p = this->__begin_ + (__position - begin());
1774 if (this->__end_ < this->__end_cap())
1775 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001776 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001777 if (__p == this->__end_)
1778 {
1779 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001780 _VSTD::__to_raw_pointer(this->__end_),
1781 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001782 ++this->__end_;
1783 }
1784 else
1785 {
1786 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001787 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001788 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001789 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001790 }
1791 else
1792 {
1793 allocator_type& __a = this->__alloc();
1794 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001795 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001796 __p = __swap_out_circular_buffer(__v, __p);
1797 }
1798 return __make_iter(__p);
1799}
1800
Howard Hinnant73d21a42010-09-04 23:28:19 +00001801#ifndef _LIBCPP_HAS_NO_VARIADICS
1802
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001803template <class _Tp, class _Allocator>
1804template <class... _Args>
1805typename vector<_Tp, _Allocator>::iterator
1806vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1807{
Howard Hinnantabe26282011-09-16 17:29:17 +00001808#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001809 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1810 "vector::emplace(iterator, x) called with an iterator not"
1811 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001812#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001813 pointer __p = this->__begin_ + (__position - begin());
1814 if (this->__end_ < this->__end_cap())
1815 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001816 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817 if (__p == this->__end_)
1818 {
1819 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001820 _VSTD::__to_raw_pointer(this->__end_),
1821 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001822 ++this->__end_;
1823 }
1824 else
1825 {
Howard Hinnanta58402a2012-07-08 23:23:04 +00001826 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001827 __move_range(__p, this->__end_, __p + 1);
Howard Hinnanta58402a2012-07-08 23:23:04 +00001828 *__p = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001830 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001831 }
1832 else
1833 {
1834 allocator_type& __a = this->__alloc();
1835 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001836 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837 __p = __swap_out_circular_buffer(__v, __p);
1838 }
1839 return __make_iter(__p);
1840}
1841
Howard Hinnant73d21a42010-09-04 23:28:19 +00001842#endif // _LIBCPP_HAS_NO_VARIADICS
1843#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001844
1845template <class _Tp, class _Allocator>
1846typename vector<_Tp, _Allocator>::iterator
1847vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1848{
Howard Hinnantabe26282011-09-16 17:29:17 +00001849#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001850 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1851 "vector::insert(iterator, n, x) called with an iterator not"
1852 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001853#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001854 pointer __p = this->__begin_ + (__position - begin());
1855 if (__n > 0)
1856 {
1857 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1858 {
1859 size_type __old_n = __n;
1860 pointer __old_last = this->__end_;
1861 if (__n > static_cast<size_type>(this->__end_ - __p))
1862 {
1863 size_type __cx = __n - (this->__end_ - __p);
1864 __construct_at_end(__cx, __x);
1865 __n -= __cx;
1866 }
1867 if (__n > 0)
1868 {
Eric Fiselierd7590952014-11-14 18:28:36 +00001869 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001870 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001871 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001872 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1873 if (__p <= __xr && __xr < this->__end_)
1874 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001875 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001876 }
1877 }
1878 else
1879 {
1880 allocator_type& __a = this->__alloc();
1881 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1882 __v.__construct_at_end(__n, __x);
1883 __p = __swap_out_circular_buffer(__v, __p);
1884 }
1885 }
1886 return __make_iter(__p);
1887}
1888
1889template <class _Tp, class _Allocator>
1890template <class _InputIterator>
1891typename enable_if
1892<
1893 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001894 !__is_forward_iterator<_InputIterator>::value &&
1895 is_constructible<
1896 _Tp,
1897 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001898 typename vector<_Tp, _Allocator>::iterator
1899>::type
1900vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1901{
Howard Hinnantabe26282011-09-16 17:29:17 +00001902#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001903 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1904 "vector::insert(iterator, range) called with an iterator not"
1905 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001906#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001907 difference_type __off = __position - begin();
1908 pointer __p = this->__begin_ + __off;
1909 allocator_type& __a = this->__alloc();
1910 pointer __old_last = this->__end_;
1911 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1912 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001913 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001914 *__first);
1915 ++this->__end_;
1916 }
1917 __split_buffer<value_type, allocator_type&> __v(__a);
1918 if (__first != __last)
1919 {
1920#ifndef _LIBCPP_NO_EXCEPTIONS
1921 try
1922 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001923#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001924 __v.__construct_at_end(__first, __last);
1925 difference_type __old_size = __old_last - this->__begin_;
1926 difference_type __old_p = __p - this->__begin_;
1927 reserve(__recommend(size() + __v.size()));
1928 __p = this->__begin_ + __old_p;
1929 __old_last = this->__begin_ + __old_size;
1930#ifndef _LIBCPP_NO_EXCEPTIONS
1931 }
1932 catch (...)
1933 {
1934 erase(__make_iter(__old_last), end());
1935 throw;
1936 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001937#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001938 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001939 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant0442b122011-09-16 18:41:29 +00001940 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1941 make_move_iterator(__v.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001942 return begin() + __off;
1943}
1944
1945template <class _Tp, class _Allocator>
1946template <class _ForwardIterator>
1947typename enable_if
1948<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001949 __is_forward_iterator<_ForwardIterator>::value &&
1950 is_constructible<
1951 _Tp,
1952 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953 typename vector<_Tp, _Allocator>::iterator
1954>::type
1955vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1956{
Howard Hinnantabe26282011-09-16 17:29:17 +00001957#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001958 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1959 "vector::insert(iterator, range) called with an iterator not"
1960 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001961#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001962 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00001963 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001964 if (__n > 0)
1965 {
1966 if (__n <= this->__end_cap() - this->__end_)
1967 {
1968 size_type __old_n = __n;
1969 pointer __old_last = this->__end_;
1970 _ForwardIterator __m = __last;
1971 difference_type __dx = this->__end_ - __p;
1972 if (__n > __dx)
1973 {
1974 __m = __first;
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001975 difference_type __diff = this->__end_ - __p;
1976 _VSTD::advance(__m, __diff);
1977 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001978 __n = __dx;
1979 }
1980 if (__n > 0)
1981 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001982 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001983 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001984 __annotator.__done();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001985 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001986 }
1987 }
1988 else
1989 {
1990 allocator_type& __a = this->__alloc();
1991 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1992 __v.__construct_at_end(__first, __last);
1993 __p = __swap_out_circular_buffer(__v, __p);
1994 }
1995 }
1996 return __make_iter(__p);
1997}
1998
1999template <class _Tp, class _Allocator>
2000void
2001vector<_Tp, _Allocator>::resize(size_type __sz)
2002{
2003 size_type __cs = size();
2004 if (__cs < __sz)
2005 this->__append(__sz - __cs);
2006 else if (__cs > __sz)
2007 this->__destruct_at_end(this->__begin_ + __sz);
2008}
2009
2010template <class _Tp, class _Allocator>
2011void
2012vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2013{
2014 size_type __cs = size();
2015 if (__cs < __sz)
2016 this->__append(__sz - __cs, __x);
2017 else if (__cs > __sz)
2018 this->__destruct_at_end(this->__begin_ + __sz);
2019}
2020
2021template <class _Tp, class _Allocator>
2022void
2023vector<_Tp, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002024 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2025 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002026{
Howard Hinnant7a563db2011-09-14 18:33:51 +00002027 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2028 this->__alloc() == __x.__alloc(),
2029 "vector::swap: Either propagate_on_container_swap must be true"
2030 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +00002031 _VSTD::swap(this->__begin_, __x.__begin_);
2032 _VSTD::swap(this->__end_, __x.__end_);
2033 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002034 __base::__swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnantabe26282011-09-16 17:29:17 +00002035#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002036 __get_db()->swap(this, &__x);
Howard Hinnantabe26282011-09-16 17:29:17 +00002037#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002038}
2039
Howard Hinnant324bb032010-08-22 00:02:43 +00002040template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002041bool
2042vector<_Tp, _Allocator>::__invariants() const
2043{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002044 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002045 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002046 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002047 return false;
2048 }
2049 else
2050 {
2051 if (this->__begin_ > this->__end_)
2052 return false;
2053 if (this->__begin_ == this->__end_cap())
2054 return false;
2055 if (this->__end_ > this->__end_cap())
2056 return false;
2057 }
2058 return true;
2059}
2060
Howard Hinnantabe26282011-09-16 17:29:17 +00002061#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002062
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002063template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:51 +00002064bool
2065vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2066{
2067 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2068}
2069
2070template <class _Tp, class _Allocator>
2071bool
2072vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2073{
2074 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2075}
2076
2077template <class _Tp, class _Allocator>
2078bool
2079vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2080{
2081 const_pointer __p = __i->base() + __n;
2082 return this->__begin_ <= __p && __p <= this->__end_;
2083}
2084
2085template <class _Tp, class _Allocator>
2086bool
2087vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2088{
2089 const_pointer __p = __i->base() + __n;
2090 return this->__begin_ <= __p && __p < this->__end_;
2091}
2092
Howard Hinnantabe26282011-09-16 17:29:17 +00002093#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002094
2095template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002096inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002097void
2098vector<_Tp, _Allocator>::__invalidate_all_iterators()
2099{
Howard Hinnantabe26282011-09-16 17:29:17 +00002100#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002101 __get_db()->__invalidate_all(this);
Howard Hinnantabe26282011-09-16 17:29:17 +00002102#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002103}
2104
2105// vector<bool>
2106
2107template <class _Allocator> class vector<bool, _Allocator>;
2108
2109template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2110
2111template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002112struct __has_storage_type<vector<bool, _Allocator> >
2113{
2114 static const bool value = true;
2115};
2116
2117template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002118class _LIBCPP_TYPE_VIS_ONLY vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002119 : private __vector_base_common<true>
2120{
2121public:
2122 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00002123 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002124 typedef _Allocator allocator_type;
2125 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002126 typedef typename __alloc_traits::size_type size_type;
2127 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantf867f632012-05-07 16:50:38 +00002128 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002129 typedef __bit_iterator<vector, false> pointer;
2130 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002131 typedef pointer iterator;
2132 typedef const_pointer const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002133 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2134 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002135
2136private:
Marshall Clow66302c62015-04-07 05:21:38 +00002137 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002138 typedef allocator_traits<__storage_allocator> __storage_traits;
2139 typedef typename __storage_traits::pointer __storage_pointer;
2140 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2141
2142 __storage_pointer __begin_;
2143 size_type __size_;
2144 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002145public:
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002146 typedef __bit_reference<vector> reference;
2147 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002148private:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002149 _LIBCPP_INLINE_VISIBILITY
2150 size_type& __cap() _NOEXCEPT
2151 {return __cap_alloc_.first();}
2152 _LIBCPP_INLINE_VISIBILITY
2153 const size_type& __cap() const _NOEXCEPT
2154 {return __cap_alloc_.first();}
2155 _LIBCPP_INLINE_VISIBILITY
2156 __storage_allocator& __alloc() _NOEXCEPT
2157 {return __cap_alloc_.second();}
2158 _LIBCPP_INLINE_VISIBILITY
2159 const __storage_allocator& __alloc() const _NOEXCEPT
2160 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002161
2162 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2163
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002164 _LIBCPP_INLINE_VISIBILITY
2165 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002166 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002167 _LIBCPP_INLINE_VISIBILITY
2168 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002169 {return (__n - 1) / __bits_per_word + 1;}
2170
2171public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002172 _LIBCPP_INLINE_VISIBILITY
2173 vector()
Marshall Clow127db912015-06-04 00:10:20 +00002174#if _LIBCPP_STD_VER <= 14
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002175 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow127db912015-06-04 00:10:20 +00002176#else
2177 _NOEXCEPT;
2178#endif
2179
2180 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2181#if _LIBCPP_STD_VER <= 14
2182 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2183#else
2184 _NOEXCEPT;
2185#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002186 ~vector();
2187 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59 +00002188#if _LIBCPP_STD_VER > 11
2189 explicit vector(size_type __n, const allocator_type& __a);
2190#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002191 vector(size_type __n, const value_type& __v);
2192 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2193 template <class _InputIterator>
2194 vector(_InputIterator __first, _InputIterator __last,
2195 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2196 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2197 template <class _InputIterator>
2198 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2199 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2200 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2201 template <class _ForwardIterator>
2202 vector(_ForwardIterator __first, _ForwardIterator __last,
2203 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2204 template <class _ForwardIterator>
2205 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2206 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2207
2208 vector(const vector& __v);
2209 vector(const vector& __v, const allocator_type& __a);
2210 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00002211#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002212 vector(initializer_list<value_type> __il);
2213 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00002214#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002215
Howard Hinnant73d21a42010-09-04 23:28:19 +00002216#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002217 _LIBCPP_INLINE_VISIBILITY
2218 vector(vector&& __v)
2219 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002220 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002221 _LIBCPP_INLINE_VISIBILITY
2222 vector& operator=(vector&& __v)
2223 _NOEXCEPT_(
2224 __alloc_traits::propagate_on_container_move_assignment::value &&
2225 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002226#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +00002227#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002228 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002229 vector& operator=(initializer_list<value_type> __il)
2230 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00002231#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002232
2233 template <class _InputIterator>
2234 typename enable_if
2235 <
2236 __is_input_iterator<_InputIterator>::value &&
2237 !__is_forward_iterator<_InputIterator>::value,
2238 void
2239 >::type
2240 assign(_InputIterator __first, _InputIterator __last);
2241 template <class _ForwardIterator>
2242 typename enable_if
2243 <
2244 __is_forward_iterator<_ForwardIterator>::value,
2245 void
2246 >::type
2247 assign(_ForwardIterator __first, _ForwardIterator __last);
2248
2249 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +00002250#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002252 void assign(initializer_list<value_type> __il)
2253 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002254#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002255
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002256 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002257 {return allocator_type(this->__alloc());}
2258
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002259 size_type max_size() const _NOEXCEPT;
2260 _LIBCPP_INLINE_VISIBILITY
2261 size_type capacity() const _NOEXCEPT
2262 {return __internal_cap_to_external(__cap());}
2263 _LIBCPP_INLINE_VISIBILITY
2264 size_type size() const _NOEXCEPT
2265 {return __size_;}
2266 _LIBCPP_INLINE_VISIBILITY
2267 bool empty() const _NOEXCEPT
2268 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002269 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002270 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002271
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002272 _LIBCPP_INLINE_VISIBILITY
2273 iterator begin() _NOEXCEPT
2274 {return __make_iter(0);}
2275 _LIBCPP_INLINE_VISIBILITY
2276 const_iterator begin() const _NOEXCEPT
2277 {return __make_iter(0);}
2278 _LIBCPP_INLINE_VISIBILITY
2279 iterator end() _NOEXCEPT
2280 {return __make_iter(__size_);}
2281 _LIBCPP_INLINE_VISIBILITY
2282 const_iterator end() const _NOEXCEPT
2283 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002284
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002285 _LIBCPP_INLINE_VISIBILITY
2286 reverse_iterator rbegin() _NOEXCEPT
2287 {return reverse_iterator(end());}
2288 _LIBCPP_INLINE_VISIBILITY
2289 const_reverse_iterator rbegin() const _NOEXCEPT
2290 {return const_reverse_iterator(end());}
2291 _LIBCPP_INLINE_VISIBILITY
2292 reverse_iterator rend() _NOEXCEPT
2293 {return reverse_iterator(begin());}
2294 _LIBCPP_INLINE_VISIBILITY
2295 const_reverse_iterator rend() const _NOEXCEPT
2296 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002297
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002298 _LIBCPP_INLINE_VISIBILITY
2299 const_iterator cbegin() const _NOEXCEPT
2300 {return __make_iter(0);}
2301 _LIBCPP_INLINE_VISIBILITY
2302 const_iterator cend() const _NOEXCEPT
2303 {return __make_iter(__size_);}
2304 _LIBCPP_INLINE_VISIBILITY
2305 const_reverse_iterator crbegin() const _NOEXCEPT
2306 {return rbegin();}
2307 _LIBCPP_INLINE_VISIBILITY
2308 const_reverse_iterator crend() const _NOEXCEPT
2309 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002310
2311 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2312 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2313 reference at(size_type __n);
2314 const_reference at(size_type __n) const;
2315
2316 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2317 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2318 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2319 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2320
2321 void push_back(const value_type& __x);
Marshall Clow198a2a52013-08-13 23:54:12 +00002322#if _LIBCPP_STD_VER > 11
2323 template <class... _Args>
2324 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2325 { push_back ( value_type ( _VSTD::forward<_Args>(__args)... )); }
2326#endif
2327
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002328 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2329
Marshall Clow198a2a52013-08-13 23:54:12 +00002330#if _LIBCPP_STD_VER > 11
2331 template <class... _Args>
2332 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2333 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2334#endif
2335
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002336 iterator insert(const_iterator __position, const value_type& __x);
2337 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2338 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2339 template <class _InputIterator>
2340 typename enable_if
2341 <
2342 __is_input_iterator <_InputIterator>::value &&
2343 !__is_forward_iterator<_InputIterator>::value,
2344 iterator
2345 >::type
2346 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2347 template <class _ForwardIterator>
2348 typename enable_if
2349 <
2350 __is_forward_iterator<_ForwardIterator>::value,
2351 iterator
2352 >::type
2353 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00002354#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002356 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2357 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002358#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002359
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002360 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002361 iterator erase(const_iterator __first, const_iterator __last);
2362
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002363 _LIBCPP_INLINE_VISIBILITY
2364 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002365
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002366 void swap(vector&)
2367 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2368 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002369
2370 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002371 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002372
2373 bool __invariants() const;
2374
2375private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002376 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002377 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002378 void deallocate() _NOEXCEPT;
2379 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00002380 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow0d1965d2014-07-28 15:02:42 +00002381 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002382 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2383 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002384 template <class _ForwardIterator>
2385 typename enable_if
2386 <
2387 __is_forward_iterator<_ForwardIterator>::value,
2388 void
2389 >::type
2390 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2391 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002392 _LIBCPP_INLINE_VISIBILITY
2393 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002394 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002395 _LIBCPP_INLINE_VISIBILITY
2396 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002397 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002398 _LIBCPP_INLINE_VISIBILITY
2399 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002400 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002401 _LIBCPP_INLINE_VISIBILITY
2402 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002403 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002404 _LIBCPP_INLINE_VISIBILITY
2405 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002406 {return begin() + (__p - cbegin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002407
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002409 void __copy_assign_alloc(const vector& __v)
2410 {__copy_assign_alloc(__v, integral_constant<bool,
2411 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002412 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002413 void __copy_assign_alloc(const vector& __c, true_type)
2414 {
2415 if (__alloc() != __c.__alloc())
2416 deallocate();
2417 __alloc() = __c.__alloc();
2418 }
2419
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002421 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002422 {}
2423
2424 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002425 void __move_assign(vector& __c, true_type)
2426 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002428 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002429 _NOEXCEPT_(
2430 !__storage_traits::propagate_on_container_move_assignment::value ||
2431 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002432 {__move_assign_alloc(__c, integral_constant<bool,
2433 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00002435 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002436 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002437 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002438 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002439 }
2440
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002442 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002443 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002444 {}
2445
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002447 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002448 _NOEXCEPT_(
2449 !__storage_traits::propagate_on_container_swap::value ||
2450 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002451 {__swap_alloc(__x, __y, integral_constant<bool,
2452 __storage_traits::propagate_on_container_swap::value>());}
2453
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002454 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002455 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002456 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002457 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002458 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002459 swap(__x, __y);
2460 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002462 static void __swap_alloc(__storage_allocator&, __storage_allocator&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002463 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002464 {}
2465
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002466 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002467
2468 friend class __bit_reference<vector>;
2469 friend class __bit_const_reference<vector>;
2470 friend class __bit_iterator<vector, false>;
2471 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:18 +00002472 friend struct __bit_array<vector>;
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002473 friend struct _LIBCPP_TYPE_VIS_ONLY hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002474};
2475
2476template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002477inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002478void
2479vector<bool, _Allocator>::__invalidate_all_iterators()
2480{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002481}
2482
2483// Allocate space for __n objects
2484// throws length_error if __n > max_size()
2485// throws (probably bad_alloc) if memory run out
2486// Precondition: __begin_ == __end_ == __cap() == 0
2487// Precondition: __n > 0
2488// Postcondition: capacity() == __n
2489// Postcondition: size() == 0
2490template <class _Allocator>
2491void
2492vector<bool, _Allocator>::allocate(size_type __n)
2493{
2494 if (__n > max_size())
2495 this->__throw_length_error();
2496 __n = __external_cap_to_internal(__n);
2497 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2498 this->__size_ = 0;
2499 this->__cap() = __n;
2500}
2501
2502template <class _Allocator>
2503void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002504vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002505{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002506 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002507 {
2508 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2509 __invalidate_all_iterators();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002510 this->__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002511 this->__size_ = this->__cap() = 0;
2512 }
2513}
2514
2515template <class _Allocator>
2516typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002517vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002518{
2519 size_type __amax = __storage_traits::max_size(__alloc());
2520 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2521 if (__nmax / __bits_per_word <= __amax)
2522 return __nmax;
2523 return __internal_cap_to_external(__amax);
2524}
2525
2526// Precondition: __new_size > capacity()
2527template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002528inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002529typename vector<bool, _Allocator>::size_type
2530vector<bool, _Allocator>::__recommend(size_type __new_size) const
2531{
2532 const size_type __ms = max_size();
2533 if (__new_size > __ms)
2534 this->__throw_length_error();
2535 const size_type __cap = capacity();
2536 if (__cap >= __ms / 2)
2537 return __ms;
Howard Hinnant7f764502013-08-14 18:00:20 +00002538 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002539}
2540
2541// Default constructs __n objects starting at __end_
2542// Precondition: __n > 0
2543// Precondition: size() + __n <= capacity()
2544// Postcondition: size() == size() + __n
2545template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002546inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002547void
2548vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2549{
2550 size_type __old_size = this->__size_;
2551 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002552 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002553}
2554
2555template <class _Allocator>
2556template <class _ForwardIterator>
2557typename enable_if
2558<
2559 __is_forward_iterator<_ForwardIterator>::value,
2560 void
2561>::type
2562vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2563{
2564 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002565 this->__size_ += _VSTD::distance(__first, __last);
2566 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002567}
2568
2569template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002570inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002571vector<bool, _Allocator>::vector()
Marshall Clow127db912015-06-04 00:10:20 +00002572#if _LIBCPP_STD_VER <= 14
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002573 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Marshall Clow127db912015-06-04 00:10:20 +00002574#else
2575 _NOEXCEPT
2576#endif
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002577 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002578 __size_(0),
2579 __cap_alloc_(0)
2580{
2581}
2582
2583template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002584inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002585vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow127db912015-06-04 00:10:20 +00002586#if _LIBCPP_STD_VER <= 14
2587 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2588#else
2589 _NOEXCEPT
2590#endif
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002591 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002592 __size_(0),
2593 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2594{
2595}
2596
2597template <class _Allocator>
2598vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002599 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002600 __size_(0),
2601 __cap_alloc_(0)
2602{
2603 if (__n > 0)
2604 {
2605 allocate(__n);
2606 __construct_at_end(__n, false);
2607 }
2608}
2609
Marshall Clowa49a2c92013-09-14 00:47:59 +00002610#if _LIBCPP_STD_VER > 11
2611template <class _Allocator>
2612vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2613 : __begin_(nullptr),
2614 __size_(0),
2615 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2616{
2617 if (__n > 0)
2618 {
2619 allocate(__n);
2620 __construct_at_end(__n, false);
2621 }
2622}
2623#endif
2624
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002625template <class _Allocator>
2626vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002627 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002628 __size_(0),
2629 __cap_alloc_(0)
2630{
2631 if (__n > 0)
2632 {
2633 allocate(__n);
2634 __construct_at_end(__n, __x);
2635 }
2636}
2637
2638template <class _Allocator>
2639vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002640 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002641 __size_(0),
2642 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2643{
2644 if (__n > 0)
2645 {
2646 allocate(__n);
2647 __construct_at_end(__n, __x);
2648 }
2649}
2650
2651template <class _Allocator>
2652template <class _InputIterator>
2653vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2654 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2655 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002656 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002657 __size_(0),
2658 __cap_alloc_(0)
2659{
2660#ifndef _LIBCPP_NO_EXCEPTIONS
2661 try
2662 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002663#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002664 for (; __first != __last; ++__first)
2665 push_back(*__first);
2666#ifndef _LIBCPP_NO_EXCEPTIONS
2667 }
2668 catch (...)
2669 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002670 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002671 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2672 __invalidate_all_iterators();
2673 throw;
2674 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002675#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002676}
2677
2678template <class _Allocator>
2679template <class _InputIterator>
2680vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2681 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2682 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002683 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002684 __size_(0),
2685 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2686{
2687#ifndef _LIBCPP_NO_EXCEPTIONS
2688 try
2689 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002690#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002691 for (; __first != __last; ++__first)
2692 push_back(*__first);
2693#ifndef _LIBCPP_NO_EXCEPTIONS
2694 }
2695 catch (...)
2696 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002697 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002698 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2699 __invalidate_all_iterators();
2700 throw;
2701 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002702#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002703}
2704
2705template <class _Allocator>
2706template <class _ForwardIterator>
2707vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2708 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002709 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002710 __size_(0),
2711 __cap_alloc_(0)
2712{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002713 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002714 if (__n > 0)
2715 {
2716 allocate(__n);
2717 __construct_at_end(__first, __last);
2718 }
2719}
2720
2721template <class _Allocator>
2722template <class _ForwardIterator>
2723vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2724 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002725 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002726 __size_(0),
2727 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2728{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002729 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002730 if (__n > 0)
2731 {
2732 allocate(__n);
2733 __construct_at_end(__first, __last);
2734 }
2735}
2736
Howard Hinnante3e32912011-08-12 21:56:02 +00002737#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2738
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002739template <class _Allocator>
2740vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002741 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002742 __size_(0),
2743 __cap_alloc_(0)
2744{
2745 size_type __n = static_cast<size_type>(__il.size());
2746 if (__n > 0)
2747 {
2748 allocate(__n);
2749 __construct_at_end(__il.begin(), __il.end());
2750 }
2751}
2752
2753template <class _Allocator>
2754vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002755 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002756 __size_(0),
2757 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2758{
2759 size_type __n = static_cast<size_type>(__il.size());
2760 if (__n > 0)
2761 {
2762 allocate(__n);
2763 __construct_at_end(__il.begin(), __il.end());
2764 }
2765}
2766
Howard Hinnante3e32912011-08-12 21:56:02 +00002767#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2768
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002769template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002770vector<bool, _Allocator>::~vector()
2771{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002772 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002773 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002774 __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002775}
2776
2777template <class _Allocator>
2778vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002779 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002780 __size_(0),
2781 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2782{
2783 if (__v.size() > 0)
2784 {
2785 allocate(__v.size());
2786 __construct_at_end(__v.begin(), __v.end());
2787 }
2788}
2789
2790template <class _Allocator>
2791vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002792 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002793 __size_(0),
2794 __cap_alloc_(0, __a)
2795{
2796 if (__v.size() > 0)
2797 {
2798 allocate(__v.size());
2799 __construct_at_end(__v.begin(), __v.end());
2800 }
2801}
2802
2803template <class _Allocator>
2804vector<bool, _Allocator>&
2805vector<bool, _Allocator>::operator=(const vector& __v)
2806{
2807 if (this != &__v)
2808 {
2809 __copy_assign_alloc(__v);
2810 if (__v.__size_)
2811 {
2812 if (__v.__size_ > capacity())
2813 {
2814 deallocate();
2815 allocate(__v.__size_);
2816 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002817 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002818 }
2819 __size_ = __v.__size_;
2820 }
2821 return *this;
2822}
2823
Howard Hinnant73d21a42010-09-04 23:28:19 +00002824#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2825
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002826template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002827inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002828vector<bool, _Allocator>::vector(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002829 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002830 : __begin_(__v.__begin_),
2831 __size_(__v.__size_),
2832 __cap_alloc_(__v.__cap_alloc_)
2833{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002834 __v.__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002835 __v.__size_ = 0;
2836 __v.__cap() = 0;
2837}
2838
2839template <class _Allocator>
2840vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002841 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002842 __size_(0),
2843 __cap_alloc_(0, __a)
2844{
2845 if (__a == allocator_type(__v.__alloc()))
2846 {
2847 this->__begin_ = __v.__begin_;
2848 this->__size_ = __v.__size_;
2849 this->__cap() = __v.__cap();
2850 __v.__begin_ = nullptr;
2851 __v.__cap() = __v.__size_ = 0;
2852 }
2853 else if (__v.size() > 0)
2854 {
2855 allocate(__v.size());
2856 __construct_at_end(__v.begin(), __v.end());
2857 }
2858}
2859
2860template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002861inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002862vector<bool, _Allocator>&
2863vector<bool, _Allocator>::operator=(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002864 _NOEXCEPT_(
2865 __alloc_traits::propagate_on_container_move_assignment::value &&
2866 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002867{
2868 __move_assign(__v, integral_constant<bool,
2869 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002870 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002871}
2872
2873template <class _Allocator>
2874void
2875vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2876{
2877 if (__alloc() != __c.__alloc())
2878 assign(__c.begin(), __c.end());
2879 else
2880 __move_assign(__c, true_type());
2881}
2882
2883template <class _Allocator>
2884void
2885vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002886 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002887{
2888 deallocate();
Marshall Clowdb5e54d2014-07-21 15:15:15 +00002889 __move_assign_alloc(__c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002890 this->__begin_ = __c.__begin_;
2891 this->__size_ = __c.__size_;
2892 this->__cap() = __c.__cap();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002893 __c.__begin_ = nullptr;
2894 __c.__cap() = __c.__size_ = 0;
2895}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002896
2897#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002898
2899template <class _Allocator>
2900void
2901vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2902{
2903 __size_ = 0;
2904 if (__n > 0)
2905 {
2906 size_type __c = capacity();
2907 if (__n <= __c)
2908 __size_ = __n;
2909 else
2910 {
2911 vector __v(__alloc());
2912 __v.reserve(__recommend(__n));
2913 __v.__size_ = __n;
2914 swap(__v);
2915 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002916 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002917 }
2918}
2919
2920template <class _Allocator>
2921template <class _InputIterator>
2922typename enable_if
2923<
2924 __is_input_iterator<_InputIterator>::value &&
2925 !__is_forward_iterator<_InputIterator>::value,
2926 void
2927>::type
2928vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2929{
2930 clear();
2931 for (; __first != __last; ++__first)
2932 push_back(*__first);
2933}
2934
2935template <class _Allocator>
2936template <class _ForwardIterator>
2937typename enable_if
2938<
2939 __is_forward_iterator<_ForwardIterator>::value,
2940 void
2941>::type
2942vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2943{
2944 clear();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002945 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002946 if (__n)
2947 {
2948 if (__n > capacity())
2949 {
2950 deallocate();
2951 allocate(__n);
2952 }
2953 __construct_at_end(__first, __last);
2954 }
2955}
2956
2957template <class _Allocator>
2958void
2959vector<bool, _Allocator>::reserve(size_type __n)
2960{
2961 if (__n > capacity())
2962 {
2963 vector __v(this->__alloc());
2964 __v.allocate(__n);
2965 __v.__construct_at_end(this->begin(), this->end());
2966 swap(__v);
2967 __invalidate_all_iterators();
2968 }
2969}
2970
2971template <class _Allocator>
2972void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002973vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002974{
2975 if (__external_cap_to_internal(size()) > __cap())
2976 {
2977#ifndef _LIBCPP_NO_EXCEPTIONS
2978 try
2979 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002980#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002981 vector(*this, allocator_type(__alloc())).swap(*this);
2982#ifndef _LIBCPP_NO_EXCEPTIONS
2983 }
2984 catch (...)
2985 {
2986 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002987#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002988 }
2989}
2990
2991template <class _Allocator>
2992typename vector<bool, _Allocator>::reference
2993vector<bool, _Allocator>::at(size_type __n)
2994{
2995 if (__n >= size())
2996 this->__throw_out_of_range();
2997 return (*this)[__n];
2998}
2999
3000template <class _Allocator>
3001typename vector<bool, _Allocator>::const_reference
3002vector<bool, _Allocator>::at(size_type __n) const
3003{
3004 if (__n >= size())
3005 this->__throw_out_of_range();
3006 return (*this)[__n];
3007}
3008
3009template <class _Allocator>
3010void
3011vector<bool, _Allocator>::push_back(const value_type& __x)
3012{
3013 if (this->__size_ == this->capacity())
3014 reserve(__recommend(this->__size_ + 1));
3015 ++this->__size_;
3016 back() = __x;
3017}
3018
3019template <class _Allocator>
3020typename vector<bool, _Allocator>::iterator
3021vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3022{
3023 iterator __r;
3024 if (size() < capacity())
3025 {
3026 const_iterator __old_end = end();
3027 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003028 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003029 __r = __const_iterator_cast(__position);
3030 }
3031 else
3032 {
3033 vector __v(__alloc());
3034 __v.reserve(__recommend(__size_ + 1));
3035 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003036 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3037 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003038 swap(__v);
3039 }
3040 *__r = __x;
3041 return __r;
3042}
3043
3044template <class _Allocator>
3045typename vector<bool, _Allocator>::iterator
3046vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3047{
3048 iterator __r;
3049 size_type __c = capacity();
3050 if (__n <= __c && size() <= __c - __n)
3051 {
3052 const_iterator __old_end = end();
3053 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003054 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003055 __r = __const_iterator_cast(__position);
3056 }
3057 else
3058 {
3059 vector __v(__alloc());
3060 __v.reserve(__recommend(__size_ + __n));
3061 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003062 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3063 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003064 swap(__v);
3065 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003066 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003067 return __r;
3068}
3069
3070template <class _Allocator>
3071template <class _InputIterator>
3072typename enable_if
3073<
3074 __is_input_iterator <_InputIterator>::value &&
3075 !__is_forward_iterator<_InputIterator>::value,
3076 typename vector<bool, _Allocator>::iterator
3077>::type
3078vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3079{
3080 difference_type __off = __position - begin();
3081 iterator __p = __const_iterator_cast(__position);
3082 iterator __old_end = end();
3083 for (; size() != capacity() && __first != __last; ++__first)
3084 {
3085 ++this->__size_;
3086 back() = *__first;
3087 }
3088 vector __v(__alloc());
3089 if (__first != __last)
3090 {
3091#ifndef _LIBCPP_NO_EXCEPTIONS
3092 try
3093 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003094#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003095 __v.assign(__first, __last);
3096 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3097 difference_type __old_p = __p - begin();
3098 reserve(__recommend(size() + __v.size()));
3099 __p = begin() + __old_p;
3100 __old_end = begin() + __old_size;
3101#ifndef _LIBCPP_NO_EXCEPTIONS
3102 }
3103 catch (...)
3104 {
3105 erase(__old_end, end());
3106 throw;
3107 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003108#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003109 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003110 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003111 insert(__p, __v.begin(), __v.end());
3112 return begin() + __off;
3113}
3114
3115template <class _Allocator>
3116template <class _ForwardIterator>
3117typename enable_if
3118<
3119 __is_forward_iterator<_ForwardIterator>::value,
3120 typename vector<bool, _Allocator>::iterator
3121>::type
3122vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3123{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003124 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003125 iterator __r;
3126 size_type __c = capacity();
3127 if (__n <= __c && size() <= __c - __n)
3128 {
3129 const_iterator __old_end = end();
3130 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003131 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003132 __r = __const_iterator_cast(__position);
3133 }
3134 else
3135 {
3136 vector __v(__alloc());
3137 __v.reserve(__recommend(__size_ + __n));
3138 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003139 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3140 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003141 swap(__v);
3142 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003143 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003144 return __r;
3145}
3146
3147template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003148inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003149typename vector<bool, _Allocator>::iterator
3150vector<bool, _Allocator>::erase(const_iterator __position)
3151{
3152 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003153 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003154 --__size_;
3155 return __r;
3156}
3157
3158template <class _Allocator>
3159typename vector<bool, _Allocator>::iterator
3160vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3161{
3162 iterator __r = __const_iterator_cast(__first);
3163 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003164 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003165 __size_ -= __d;
3166 return __r;
3167}
3168
3169template <class _Allocator>
3170void
3171vector<bool, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003172 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3173 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003174{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003175 _VSTD::swap(this->__begin_, __x.__begin_);
3176 _VSTD::swap(this->__size_, __x.__size_);
3177 _VSTD::swap(this->__cap(), __x.__cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003178 __swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003179}
3180
Howard Hinnant324bb032010-08-22 00:02:43 +00003181template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003182void
3183vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3184{
3185 size_type __cs = size();
3186 if (__cs < __sz)
3187 {
3188 iterator __r;
3189 size_type __c = capacity();
3190 size_type __n = __sz - __cs;
3191 if (__n <= __c && __cs <= __c - __n)
3192 {
3193 __r = end();
3194 __size_ += __n;
3195 }
3196 else
3197 {
3198 vector __v(__alloc());
3199 __v.reserve(__recommend(__size_ + __n));
3200 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003201 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003202 swap(__v);
3203 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003204 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003205 }
3206 else
3207 __size_ = __sz;
3208}
3209
Howard Hinnant324bb032010-08-22 00:02:43 +00003210template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003211void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003212vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003213{
3214 // do middle whole words
3215 size_type __n = __size_;
3216 __storage_pointer __p = __begin_;
3217 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3218 *__p = ~*__p;
3219 // do last partial word
3220 if (__n > 0)
3221 {
3222 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3223 __storage_type __b = *__p & __m;
3224 *__p &= ~__m;
3225 *__p |= ~__b & __m;
3226 }
3227}
3228
Howard Hinnant324bb032010-08-22 00:02:43 +00003229template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003230bool
3231vector<bool, _Allocator>::__invariants() const
3232{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00003233 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003234 {
3235 if (this->__size_ != 0 || this->__cap() != 0)
3236 return false;
3237 }
3238 else
3239 {
3240 if (this->__cap() == 0)
3241 return false;
3242 if (this->__size_ > this->capacity())
3243 return false;
3244 }
3245 return true;
3246}
3247
Howard Hinnant324bb032010-08-22 00:02:43 +00003248template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003249size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003250vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003251{
3252 size_t __h = 0;
3253 // do middle whole words
3254 size_type __n = __size_;
3255 __storage_pointer __p = __begin_;
3256 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3257 __h ^= *__p;
3258 // do last partial word
3259 if (__n > 0)
3260 {
3261 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3262 __h ^= *__p & __m;
3263 }
3264 return __h;
3265}
3266
3267template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003268struct _LIBCPP_TYPE_VIS_ONLY hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003269 : public unary_function<vector<bool, _Allocator>, size_t>
3270{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00003271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003272 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003273 {return __vec.__hash_code();}
3274};
3275
3276template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003277inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003278bool
3279operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3280{
3281 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003282 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003283}
3284
3285template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003286inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003287bool
3288operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3289{
3290 return !(__x == __y);
3291}
3292
3293template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003294inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003295bool
3296operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3297{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003298 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003299}
3300
3301template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003302inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003303bool
3304operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3305{
3306 return __y < __x;
3307}
3308
3309template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003310inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003311bool
3312operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3313{
3314 return !(__x < __y);
3315}
3316
3317template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003318inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003319bool
3320operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3321{
3322 return !(__y < __x);
3323}
3324
3325template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003326inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003327void
3328swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003329 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003330{
3331 __x.swap(__y);
3332}
3333
3334_LIBCPP_END_NAMESPACE_STD
3335
3336#endif // _LIBCPP_VECTOR