blob: 16d48ae58d14107f7e4d5f9fe3613ee111fc6d8b [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(
Marshall Clowaf961ed2015-08-18 18:57:00 +000054 allocator_type::propagate_on_container_move_assignment::value ||
55 allocator_type::is_always_equal::value); // C++17
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>
Eric Fiselier3816ef92016-07-21 03:20:17 +0000102 reference emplace_back(Args&&... args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000103 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&)
Marshall Clow7d914d12015-07-13 20:04:56 +0000122 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
123 allocator_traits<allocator_type>::is_always_equal::value); // C++17
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(
Marshall Clowaf961ed2015-08-18 18:57:00 +0000178 allocator_type::propagate_on_container_move_assignment::value ||
179 allocator_type::is_always_equal::value); // C++17
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);
Eric Fiselier3816ef92016-07-21 03:20:17 +0000221 template <class... Args> reference 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&)
Marshall Clow7d914d12015-07-13 20:04:56 +0000240 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
241 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000242 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>
Eric Fiselierb3792282016-02-20 00:19:45 +0000265#include <iosfwd> // for forward declaration of vector
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266#include <__bit_reference>
267#include <type_traits>
268#include <climits>
269#include <limits>
270#include <initializer_list>
271#include <memory>
272#include <stdexcept>
273#include <algorithm>
274#include <cstring>
275#include <__split_buffer>
276#include <__functional_base>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000277
Howard Hinnant66c6f972011-11-29 16:45:27 +0000278#include <__undef_min_max>
279
Eric Fiselierb9536102014-08-10 23:53:08 +0000280#include <__debug>
Howard Hinnant8b00e6c2013-08-02 00:26:35 +0000281
Howard Hinnant08e17472011-10-17 20:05:10 +0000282#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000283#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000284#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000285
286_LIBCPP_BEGIN_NAMESPACE_STD
287
288template <bool>
289class __vector_base_common
290{
291protected:
292 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
293 void __throw_length_error() const;
294 void __throw_out_of_range() const;
295};
296
297template <bool __b>
298void
299__vector_base_common<__b>::__throw_length_error() const
300{
301#ifndef _LIBCPP_NO_EXCEPTIONS
302 throw length_error("vector");
303#else
304 assert(!"vector length_error");
305#endif
306}
307
308template <bool __b>
309void
310__vector_base_common<__b>::__throw_out_of_range() const
311{
312#ifndef _LIBCPP_NO_EXCEPTIONS
313 throw out_of_range("vector");
314#else
315 assert(!"vector out_of_range");
316#endif
317}
318
Howard Hinnante9df0a52013-08-01 18:17:34 +0000319#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000320#pragma warning( push )
321#pragma warning( disable: 4231 )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000322#endif // _LIBCPP_MSVC
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000323_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS __vector_base_common<true>)
Howard Hinnante9df0a52013-08-01 18:17:34 +0000324#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000325#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000326#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000327
328template <class _Tp, class _Allocator>
329class __vector_base
330 : protected __vector_base_common<true>
331{
332protected:
Howard Hinnant324bb032010-08-22 00:02:43 +0000333 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000334 typedef _Allocator allocator_type;
335 typedef allocator_traits<allocator_type> __alloc_traits;
336 typedef value_type& reference;
337 typedef const value_type& const_reference;
338 typedef typename __alloc_traits::size_type size_type;
339 typedef typename __alloc_traits::difference_type difference_type;
340 typedef typename __alloc_traits::pointer pointer;
341 typedef typename __alloc_traits::const_pointer const_pointer;
342 typedef pointer iterator;
343 typedef const_pointer const_iterator;
344
345 pointer __begin_;
346 pointer __end_;
347 __compressed_pair<pointer, allocator_type> __end_cap_;
348
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000349 _LIBCPP_INLINE_VISIBILITY
350 allocator_type& __alloc() _NOEXCEPT
351 {return __end_cap_.second();}
352 _LIBCPP_INLINE_VISIBILITY
353 const allocator_type& __alloc() const _NOEXCEPT
354 {return __end_cap_.second();}
355 _LIBCPP_INLINE_VISIBILITY
356 pointer& __end_cap() _NOEXCEPT
357 {return __end_cap_.first();}
358 _LIBCPP_INLINE_VISIBILITY
359 const pointer& __end_cap() const _NOEXCEPT
360 {return __end_cap_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000361
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000362 _LIBCPP_INLINE_VISIBILITY
363 __vector_base()
364 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000365 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000366 ~__vector_base();
367
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000368 _LIBCPP_INLINE_VISIBILITY
369 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
370 _LIBCPP_INLINE_VISIBILITY
371 size_type capacity() const _NOEXCEPT
372 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000373
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000375 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000376
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000378 void __copy_assign_alloc(const __vector_base& __c)
379 {__copy_assign_alloc(__c, integral_constant<bool,
380 __alloc_traits::propagate_on_container_copy_assignment::value>());}
381
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000383 void __move_assign_alloc(__vector_base& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000384 _NOEXCEPT_(
385 !__alloc_traits::propagate_on_container_move_assignment::value ||
386 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387 {__move_assign_alloc(__c, integral_constant<bool,
388 __alloc_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000389private:
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000391 void __copy_assign_alloc(const __vector_base& __c, true_type)
392 {
393 if (__alloc() != __c.__alloc())
394 {
395 clear();
396 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
397 __begin_ = __end_ = __end_cap() = nullptr;
398 }
399 __alloc() = __c.__alloc();
400 }
401
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000402 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000403 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000404 {}
405
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000407 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000408 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000410 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000411 }
412
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000414 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000415 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000416 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000417};
418
419template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000420inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000421void
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000422__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000423{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000424 while (__new_last != __end_)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000425 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000426}
427
428template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000429inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000431 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000432 : __begin_(nullptr),
433 __end_(nullptr),
434 __end_cap_(nullptr)
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 +0000440__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000441 : __begin_(nullptr),
442 __end_(nullptr),
443 __end_cap_(nullptr, __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000444{
445}
446
447template <class _Tp, class _Allocator>
448__vector_base<_Tp, _Allocator>::~__vector_base()
449{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000450 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000451 {
452 clear();
453 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
454 }
455}
456
Eric Fiselierb3792282016-02-20 00:19:45 +0000457template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000458class _LIBCPP_TYPE_VIS_ONLY vector
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000459 : private __vector_base<_Tp, _Allocator>
460{
461private:
462 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000463 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant324bb032010-08-22 00:02:43 +0000464public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +0000466 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000467 typedef _Allocator allocator_type;
468 typedef typename __base::__alloc_traits __alloc_traits;
469 typedef typename __base::reference reference;
470 typedef typename __base::const_reference const_reference;
471 typedef typename __base::size_type size_type;
472 typedef typename __base::difference_type difference_type;
473 typedef typename __base::pointer pointer;
474 typedef typename __base::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000475 typedef __wrap_iter<pointer> iterator;
476 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000477 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
478 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000479
Howard Hinnant02d5e182013-03-26 19:04:56 +0000480 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
481 "Allocator::value_type must be same type as value_type");
482
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000483 _LIBCPP_INLINE_VISIBILITY
Marshall Clowc912c0c2015-06-04 02:05:41 +0000484 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant7a563db2011-09-14 18:33:51 +0000485 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000486#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000487 __get_db()->__insert_c(this);
488#endif
489 }
490 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow127db912015-06-04 00:10:20 +0000491#if _LIBCPP_STD_VER <= 14
492 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
493#else
494 _NOEXCEPT
495#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +0000496 : __base(__a)
497 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000498#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000499 __get_db()->__insert_c(this);
500#endif
501 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000502 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59 +0000503#if _LIBCPP_STD_VER > 11
504 explicit vector(size_type __n, const allocator_type& __a);
505#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000506 vector(size_type __n, const_reference __x);
507 vector(size_type __n, const_reference __x, const allocator_type& __a);
508 template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +0000509 vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000510 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000511 !__is_forward_iterator<_InputIterator>::value &&
512 is_constructible<
513 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +0000514 typename iterator_traits<_InputIterator>::reference>::value,
515 _InputIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000516 template <class _InputIterator>
517 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
518 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000519 !__is_forward_iterator<_InputIterator>::value &&
520 is_constructible<
521 value_type,
522 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000523 template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +0000524 vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000525 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
526 is_constructible<
527 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +0000528 typename iterator_traits<_ForwardIterator>::reference>::value,
529 _ForwardIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000530 template <class _ForwardIterator>
531 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000532 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
533 is_constructible<
534 value_type,
535 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000536#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000538 vector(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000540 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000541#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantabe26282011-09-16 17:29:17 +0000542#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7a563db2011-09-14 18:33:51 +0000544 ~vector()
545 {
546 __get_db()->__erase_c(this);
547 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000548#endif
549
550 vector(const vector& __x);
551 vector(const vector& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000553 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000554#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000556 vector(vector&& __x)
Marshall Clow119ed472015-07-14 14:46:32 +0000557#if _LIBCPP_STD_VER > 14
558 _NOEXCEPT;
559#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000560 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow119ed472015-07-14 14:46:32 +0000561#endif
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000563 vector(vector&& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000565 vector& operator=(vector&& __x)
Marshall Clowaf961ed2015-08-18 18:57:00 +0000566 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnant73d21a42010-09-04 23:28:19 +0000567#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000568#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000570 vector& operator=(initializer_list<value_type> __il)
571 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000572#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000573
574 template <class _InputIterator>
575 typename enable_if
576 <
577 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000578 !__is_forward_iterator<_InputIterator>::value &&
579 is_constructible<
580 value_type,
581 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000582 void
583 >::type
584 assign(_InputIterator __first, _InputIterator __last);
585 template <class _ForwardIterator>
586 typename enable_if
587 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000588 __is_forward_iterator<_ForwardIterator>::value &&
589 is_constructible<
590 value_type,
591 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000592 void
593 >::type
594 assign(_ForwardIterator __first, _ForwardIterator __last);
595
596 void assign(size_type __n, const_reference __u);
Howard Hinnante3e32912011-08-12 21:56:02 +0000597#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000598 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000599 void assign(initializer_list<value_type> __il)
600 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000601#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000602
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000603 _LIBCPP_INLINE_VISIBILITY
604 allocator_type get_allocator() const _NOEXCEPT
605 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000606
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000607 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
608 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
609 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
610 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000611
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000612 _LIBCPP_INLINE_VISIBILITY
613 reverse_iterator rbegin() _NOEXCEPT
614 {return reverse_iterator(end());}
615 _LIBCPP_INLINE_VISIBILITY
616 const_reverse_iterator rbegin() const _NOEXCEPT
617 {return const_reverse_iterator(end());}
618 _LIBCPP_INLINE_VISIBILITY
619 reverse_iterator rend() _NOEXCEPT
620 {return reverse_iterator(begin());}
621 _LIBCPP_INLINE_VISIBILITY
622 const_reverse_iterator rend() const _NOEXCEPT
623 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000624
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000625 _LIBCPP_INLINE_VISIBILITY
626 const_iterator cbegin() const _NOEXCEPT
627 {return begin();}
628 _LIBCPP_INLINE_VISIBILITY
629 const_iterator cend() const _NOEXCEPT
630 {return end();}
631 _LIBCPP_INLINE_VISIBILITY
632 const_reverse_iterator crbegin() const _NOEXCEPT
633 {return rbegin();}
634 _LIBCPP_INLINE_VISIBILITY
635 const_reverse_iterator crend() const _NOEXCEPT
636 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000637
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000638 _LIBCPP_INLINE_VISIBILITY
639 size_type size() const _NOEXCEPT
640 {return static_cast<size_type>(this->__end_ - this->__begin_);}
641 _LIBCPP_INLINE_VISIBILITY
642 size_type capacity() const _NOEXCEPT
643 {return __base::capacity();}
644 _LIBCPP_INLINE_VISIBILITY
645 bool empty() const _NOEXCEPT
646 {return this->__begin_ == this->__end_;}
647 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000649 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000650
651 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
652 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
653 reference at(size_type __n);
654 const_reference at(size_type __n) const;
655
Howard Hinnant7a563db2011-09-14 18:33:51 +0000656 _LIBCPP_INLINE_VISIBILITY reference front()
657 {
658 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
659 return *this->__begin_;
660 }
661 _LIBCPP_INLINE_VISIBILITY const_reference front() const
662 {
663 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
664 return *this->__begin_;
665 }
666 _LIBCPP_INLINE_VISIBILITY reference back()
667 {
668 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
669 return *(this->__end_ - 1);
670 }
671 _LIBCPP_INLINE_VISIBILITY const_reference back() const
672 {
673 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
674 return *(this->__end_ - 1);
675 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000676
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000677 _LIBCPP_INLINE_VISIBILITY
678 value_type* data() _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000679 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000680 _LIBCPP_INLINE_VISIBILITY
681 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000682 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000683
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000684 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000685#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000686 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000687#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000688 template <class... _Args>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000689 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier3816ef92016-07-21 03:20:17 +0000690 reference emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000691#endif // _LIBCPP_HAS_NO_VARIADICS
692#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000693 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000694 void pop_back();
695
696 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000697#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000698 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000699#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000700 template <class... _Args>
701 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000702#endif // _LIBCPP_HAS_NO_VARIADICS
703#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000704 iterator insert(const_iterator __position, size_type __n, const_reference __x);
705 template <class _InputIterator>
706 typename enable_if
707 <
708 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000709 !__is_forward_iterator<_InputIterator>::value &&
710 is_constructible<
711 value_type,
712 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000713 iterator
714 >::type
715 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
716 template <class _ForwardIterator>
717 typename enable_if
718 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000719 __is_forward_iterator<_ForwardIterator>::value &&
720 is_constructible<
721 value_type,
722 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723 iterator
724 >::type
725 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +0000726#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000728 iterator insert(const_iterator __position, initializer_list<value_type> __il)
729 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000730#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000731
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000732 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000733 iterator erase(const_iterator __first, const_iterator __last);
734
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000735 _LIBCPP_INLINE_VISIBILITY
736 void clear() _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000737 {
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000738 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000739 __base::clear();
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000740 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000741 __invalidate_all_iterators();
742 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000743
744 void resize(size_type __sz);
745 void resize(size_type __sz, const_reference __x);
746
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000747 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +0000748#if _LIBCPP_STD_VER >= 14
749 _NOEXCEPT;
750#else
751 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
752 __is_nothrow_swappable<allocator_type>::value);
753#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000754
755 bool __invariants() const;
756
Howard Hinnantabe26282011-09-16 17:29:17 +0000757#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000758
759 bool __dereferenceable(const const_iterator* __i) const;
760 bool __decrementable(const const_iterator* __i) const;
761 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
762 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
763
Howard Hinnantabe26282011-09-16 17:29:17 +0000764#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000765
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000766private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000767 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000768 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000769 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000770 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31 +0000771 void __construct_at_end(size_type __n);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000773 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000774 template <class _ForwardIterator>
775 typename enable_if
776 <
777 __is_forward_iterator<_ForwardIterator>::value,
778 void
779 >::type
Eric Fiselier088ed9f2015-03-31 16:54:19 +0000780 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000781 void __append(size_type __n);
782 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000784 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000786 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000787 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
788 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
789 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000790 void __move_assign(vector& __c, true_type)
791 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clowaf961ed2015-08-18 18:57:00 +0000792 void __move_assign(vector& __c, false_type)
793 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000795 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000796 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000797#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000798 __c_node* __c = __get_db()->__find_c_and_lock(this);
799 for (__i_node** __p = __c->end_; __p != __c->beg_; )
800 {
801 --__p;
802 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
803 if (__i->base() > __new_last)
804 {
805 (*__p)->__c_ = nullptr;
806 if (--__c->end_ != __p)
807 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
808 }
809 }
810 __get_db()->unlock();
811#endif
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000812 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000813 __base::__destruct_at_end(__new_last);
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000814 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000815 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000816 template <class _Up>
817 void
818#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
819 __push_back_slow_path(_Up&& __x);
820#else
821 __push_back_slow_path(_Up& __x);
822#endif
Howard Hinnant0438ea22012-02-26 15:30:12 +0000823#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
824 template <class... _Args>
825 void
826 __emplace_back_slow_path(_Args&&... __args);
827#endif
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000828 // The following functions are no-ops outside of AddressSanitizer mode.
829 // We call annotatations only for the default Allocator because other allocators
830 // may not meet the AddressSanitizer alignment constraints.
831 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
832 void __annotate_contiguous_container
Kostya Serebryany497f9122014-09-02 23:43:38 +0000833 (const void *__beg, const void *__end, const void *__old_mid, const void *__new_mid) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000834 {
835#ifndef _LIBCPP_HAS_NO_ASAN
836 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
837 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
838#endif
839 }
840
Kostya Serebryany497f9122014-09-02 23:43:38 +0000841 void __annotate_new(size_type __current_size) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000842 {
843 __annotate_contiguous_container(data(), data() + capacity(),
844 data() + capacity(), data() + __current_size);
845 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000846 void __annotate_delete() const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000847 {
848 __annotate_contiguous_container(data(), data() + capacity(),
849 data() + size(), data() + capacity());
850 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000851 void __annotate_increase(size_type __n) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000852 {
853 __annotate_contiguous_container(data(), data() + capacity(),
854 data() + size(), data() + size() + __n);
855 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000856 void __annotate_shrink(size_type __old_size) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000857 {
858 __annotate_contiguous_container(data(), data() + capacity(),
859 data() + __old_size, data() + size());
860 }
Marshall Clow26f472d2014-09-03 21:37:43 +0000861#ifndef _LIBCPP_HAS_NO_ASAN
Kostya Serebryany497f9122014-09-02 23:43:38 +0000862 // The annotation for size increase should happen before the actual increase,
863 // but if an exception is thrown after that the annotation has to be undone.
864 struct __RAII_IncreaseAnnotator {
865 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000866 : __commit(false), __v(__v), __old_size(__v.size() + __n) {
Kostya Serebryany497f9122014-09-02 23:43:38 +0000867 __v.__annotate_increase(__n);
868 }
869 void __done() { __commit = true; }
870 ~__RAII_IncreaseAnnotator() {
871 if (__commit) return;
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000872 __v.__annotate_shrink(__old_size);
Kostya Serebryany497f9122014-09-02 23:43:38 +0000873 }
874 bool __commit;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000875 const vector &__v;
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000876 size_type __old_size;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000877 };
Marshall Clow26f472d2014-09-03 21:37:43 +0000878#else
879 struct __RAII_IncreaseAnnotator {
880 inline __RAII_IncreaseAnnotator(const vector &, size_type __n = 1) {}
881 inline void __done() {}
882 };
883#endif
884
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000885};
886
887template <class _Tp, class _Allocator>
888void
889vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
890{
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000891 __annotate_delete();
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000892 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000893 _VSTD::swap(this->__begin_, __v.__begin_);
894 _VSTD::swap(this->__end_, __v.__end_);
895 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000896 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000897 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000898 __invalidate_all_iterators();
899}
900
901template <class _Tp, class _Allocator>
902typename vector<_Tp, _Allocator>::pointer
903vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
904{
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000905 __annotate_delete();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000906 pointer __r = __v.__begin_;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000907 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
908 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000909 _VSTD::swap(this->__begin_, __v.__begin_);
910 _VSTD::swap(this->__end_, __v.__end_);
911 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000912 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000913 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000914 __invalidate_all_iterators();
915 return __r;
916}
917
918// Allocate space for __n objects
919// throws length_error if __n > max_size()
920// throws (probably bad_alloc) if memory run out
921// Precondition: __begin_ == __end_ == __end_cap() == 0
922// Precondition: __n > 0
923// Postcondition: capacity() == __n
924// Postcondition: size() == 0
925template <class _Tp, class _Allocator>
926void
927vector<_Tp, _Allocator>::allocate(size_type __n)
928{
929 if (__n > max_size())
930 this->__throw_length_error();
931 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
932 this->__end_cap() = this->__begin_ + __n;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000933 __annotate_new(0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000934}
935
936template <class _Tp, class _Allocator>
937void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000938vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000939{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000940 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000941 {
942 clear();
943 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000944 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000945 }
946}
947
948template <class _Tp, class _Allocator>
949typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000950vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000951{
Sean Hunt110b8bf2011-07-29 23:31:58 +0000952 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 +0000953}
954
955// Precondition: __new_size > capacity()
956template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000957inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000958typename vector<_Tp, _Allocator>::size_type
959vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
960{
961 const size_type __ms = max_size();
962 if (__new_size > __ms)
963 this->__throw_length_error();
964 const size_type __cap = capacity();
965 if (__cap >= __ms / 2)
966 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58 +0000967 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000968}
969
970// Default constructs __n objects starting at __end_
971// throws if construction throws
972// Precondition: __n > 0
973// Precondition: size() + __n <= capacity()
974// Postcondition: size() == size() + __n
975template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000976void
977vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
978{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000979 allocator_type& __a = this->__alloc();
980 do
981 {
Kostya Serebryany497f9122014-09-02 23:43:38 +0000982 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000983 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000984 ++this->__end_;
985 --__n;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000986 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000987 } while (__n > 0);
988}
989
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990// Copy constructs __n objects starting at __end_ from __x
991// throws if construction throws
992// Precondition: __n > 0
993// Precondition: size() + __n <= capacity()
994// Postcondition: size() == old size() + __n
995// Postcondition: [i] == __x for all i in [size() - __n, __n)
996template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000997inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000998void
999vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1000{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001001 allocator_type& __a = this->__alloc();
1002 do
1003 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001004 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001005 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001006 ++this->__end_;
1007 --__n;
Kostya Serebryany497f9122014-09-02 23:43:38 +00001008 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001009 } while (__n > 0);
1010}
1011
1012template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013template <class _ForwardIterator>
1014typename enable_if
1015<
1016 __is_forward_iterator<_ForwardIterator>::value,
1017 void
1018>::type
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001019vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001020{
1021 allocator_type& __a = this->__alloc();
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001022 __RAII_IncreaseAnnotator __annotator(*this, __n);
1023 __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
1024 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001025}
1026
1027// Default constructs __n objects starting at __end_
1028// throws if construction throws
1029// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001030// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001031template <class _Tp, class _Allocator>
1032void
1033vector<_Tp, _Allocator>::__append(size_type __n)
1034{
1035 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1036 this->__construct_at_end(__n);
1037 else
1038 {
1039 allocator_type& __a = this->__alloc();
1040 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1041 __v.__construct_at_end(__n);
1042 __swap_out_circular_buffer(__v);
1043 }
1044}
1045
1046// Default constructs __n objects starting at __end_
1047// throws if construction throws
1048// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001049// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050template <class _Tp, class _Allocator>
1051void
1052vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1053{
1054 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1055 this->__construct_at_end(__n, __x);
1056 else
1057 {
1058 allocator_type& __a = this->__alloc();
1059 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1060 __v.__construct_at_end(__n, __x);
1061 __swap_out_circular_buffer(__v);
1062 }
1063}
1064
1065template <class _Tp, class _Allocator>
1066vector<_Tp, _Allocator>::vector(size_type __n)
1067{
Howard Hinnant0442b122011-09-16 18:41:29 +00001068#if _LIBCPP_DEBUG_LEVEL >= 2
1069 __get_db()->__insert_c(this);
1070#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001071 if (__n > 0)
1072 {
1073 allocate(__n);
1074 __construct_at_end(__n);
1075 }
1076}
1077
Marshall Clowa49a2c92013-09-14 00:47:59 +00001078#if _LIBCPP_STD_VER > 11
1079template <class _Tp, class _Allocator>
1080vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1081 : __base(__a)
1082{
1083#if _LIBCPP_DEBUG_LEVEL >= 2
1084 __get_db()->__insert_c(this);
1085#endif
1086 if (__n > 0)
1087 {
1088 allocate(__n);
1089 __construct_at_end(__n);
1090 }
1091}
1092#endif
1093
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001094template <class _Tp, class _Allocator>
1095vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1096{
Howard Hinnant0442b122011-09-16 18:41:29 +00001097#if _LIBCPP_DEBUG_LEVEL >= 2
1098 __get_db()->__insert_c(this);
1099#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001100 if (__n > 0)
1101 {
1102 allocate(__n);
1103 __construct_at_end(__n, __x);
1104 }
1105}
1106
1107template <class _Tp, class _Allocator>
1108vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1109 : __base(__a)
1110{
Howard Hinnant0442b122011-09-16 18:41:29 +00001111#if _LIBCPP_DEBUG_LEVEL >= 2
1112 __get_db()->__insert_c(this);
1113#endif
Howard 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>
1122template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +00001123vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001124 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001125 !__is_forward_iterator<_InputIterator>::value &&
1126 is_constructible<
1127 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +00001128 typename iterator_traits<_InputIterator>::reference>::value,
1129 _InputIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001130{
Howard Hinnantabe26282011-09-16 17:29:17 +00001131#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001132 __get_db()->__insert_c(this);
1133#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001134 for (; __first != __last; ++__first)
1135 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001136}
1137
1138template <class _Tp, class _Allocator>
1139template <class _InputIterator>
1140vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1141 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001142 !__is_forward_iterator<_InputIterator>::value &&
1143 is_constructible<
1144 value_type,
1145 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001146 : __base(__a)
1147{
Howard Hinnantabe26282011-09-16 17:29:17 +00001148#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001149 __get_db()->__insert_c(this);
1150#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001151 for (; __first != __last; ++__first)
1152 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001153}
1154
1155template <class _Tp, class _Allocator>
1156template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +00001157vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001158 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1159 is_constructible<
1160 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +00001161 typename iterator_traits<_ForwardIterator>::reference>::value,
1162 _ForwardIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001163{
Howard Hinnant0442b122011-09-16 18:41:29 +00001164#if _LIBCPP_DEBUG_LEVEL >= 2
1165 __get_db()->__insert_c(this);
1166#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001167 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001168 if (__n > 0)
1169 {
1170 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001171 __construct_at_end(__first, __last, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001172 }
1173}
1174
1175template <class _Tp, class _Allocator>
1176template <class _ForwardIterator>
1177vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001178 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1179 is_constructible<
1180 value_type,
1181 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001182 : __base(__a)
1183{
Howard Hinnant0442b122011-09-16 18:41:29 +00001184#if _LIBCPP_DEBUG_LEVEL >= 2
1185 __get_db()->__insert_c(this);
1186#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001187 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001188 if (__n > 0)
1189 {
1190 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001191 __construct_at_end(__first, __last, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001192 }
1193}
1194
1195template <class _Tp, class _Allocator>
1196vector<_Tp, _Allocator>::vector(const vector& __x)
1197 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1198{
Howard Hinnant0442b122011-09-16 18:41:29 +00001199#if _LIBCPP_DEBUG_LEVEL >= 2
1200 __get_db()->__insert_c(this);
1201#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001202 size_type __n = __x.size();
1203 if (__n > 0)
1204 {
1205 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001206 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001207 }
1208}
1209
1210template <class _Tp, class _Allocator>
1211vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1212 : __base(__a)
1213{
Howard Hinnant0442b122011-09-16 18:41:29 +00001214#if _LIBCPP_DEBUG_LEVEL >= 2
1215 __get_db()->__insert_c(this);
1216#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001217 size_type __n = __x.size();
1218 if (__n > 0)
1219 {
1220 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001221 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001222 }
1223}
1224
Howard Hinnant73d21a42010-09-04 23:28:19 +00001225#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001226
1227template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001228inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001229vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clow119ed472015-07-14 14:46:32 +00001230#if _LIBCPP_STD_VER > 14
1231 _NOEXCEPT
1232#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001233 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow119ed472015-07-14 14:46:32 +00001234#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001235 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001236{
Howard Hinnantabe26282011-09-16 17:29:17 +00001237#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001238 __get_db()->__insert_c(this);
Howard Hinnante6125bd2011-09-19 16:34:29 +00001239 __get_db()->swap(this, &__x);
Howard Hinnant7a563db2011-09-14 18:33:51 +00001240#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001241 this->__begin_ = __x.__begin_;
1242 this->__end_ = __x.__end_;
1243 this->__end_cap() = __x.__end_cap();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001244 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245}
1246
1247template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001248inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001249vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1250 : __base(__a)
1251{
Howard Hinnant0442b122011-09-16 18:41:29 +00001252#if _LIBCPP_DEBUG_LEVEL >= 2
1253 __get_db()->__insert_c(this);
1254#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001255 if (__a == __x.__alloc())
1256 {
1257 this->__begin_ = __x.__begin_;
1258 this->__end_ = __x.__end_;
1259 this->__end_cap() = __x.__end_cap();
1260 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001261#if _LIBCPP_DEBUG_LEVEL >= 2
1262 __get_db()->swap(this, &__x);
1263#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001264 }
1265 else
1266 {
Howard Hinnant99968442011-11-29 18:15:50 +00001267 typedef move_iterator<iterator> _Ip;
1268 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001269 }
1270}
1271
Howard Hinnante3e32912011-08-12 21:56:02 +00001272#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1273
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001274template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001275inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001276vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1277{
Howard Hinnant0442b122011-09-16 18:41:29 +00001278#if _LIBCPP_DEBUG_LEVEL >= 2
1279 __get_db()->__insert_c(this);
1280#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001281 if (__il.size() > 0)
1282 {
1283 allocate(__il.size());
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001284 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001285 }
1286}
1287
1288template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001289inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001290vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1291 : __base(__a)
1292{
Howard Hinnant0442b122011-09-16 18:41:29 +00001293#if _LIBCPP_DEBUG_LEVEL >= 2
1294 __get_db()->__insert_c(this);
1295#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001296 if (__il.size() > 0)
1297 {
1298 allocate(__il.size());
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001299 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001300 }
1301}
1302
Howard Hinnante3e32912011-08-12 21:56:02 +00001303#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1304
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001305template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001306inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001307vector<_Tp, _Allocator>&
1308vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001309 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001310{
1311 __move_assign(__x, integral_constant<bool,
1312 __alloc_traits::propagate_on_container_move_assignment::value>());
1313 return *this;
1314}
1315
1316template <class _Tp, class _Allocator>
1317void
1318vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001319 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001320{
1321 if (__base::__alloc() != __c.__alloc())
1322 {
Howard Hinnant99968442011-11-29 18:15:50 +00001323 typedef move_iterator<iterator> _Ip;
1324 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001325 }
1326 else
1327 __move_assign(__c, true_type());
1328}
1329
1330template <class _Tp, class _Allocator>
1331void
1332vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001333 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001334{
1335 deallocate();
Marshall Clow3c2eac62014-07-21 15:11:13 +00001336 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001337 this->__begin_ = __c.__begin_;
1338 this->__end_ = __c.__end_;
1339 this->__end_cap() = __c.__end_cap();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001340 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001341#if _LIBCPP_DEBUG_LEVEL >= 2
1342 __get_db()->swap(this, &__c);
1343#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001344}
1345
Howard Hinnant73d21a42010-09-04 23:28:19 +00001346#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001347
1348template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001349inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001350vector<_Tp, _Allocator>&
1351vector<_Tp, _Allocator>::operator=(const vector& __x)
1352{
1353 if (this != &__x)
1354 {
1355 __base::__copy_assign_alloc(__x);
1356 assign(__x.__begin_, __x.__end_);
1357 }
1358 return *this;
1359}
1360
1361template <class _Tp, class _Allocator>
1362template <class _InputIterator>
1363typename enable_if
1364<
1365 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001366 !__is_forward_iterator<_InputIterator>::value &&
1367 is_constructible<
1368 _Tp,
1369 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001370 void
1371>::type
1372vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1373{
1374 clear();
1375 for (; __first != __last; ++__first)
1376 push_back(*__first);
1377}
1378
1379template <class _Tp, class _Allocator>
1380template <class _ForwardIterator>
1381typename enable_if
1382<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001383 __is_forward_iterator<_ForwardIterator>::value &&
1384 is_constructible<
1385 _Tp,
1386 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001387 void
1388>::type
1389vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1390{
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001391 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1392 if (__new_size <= capacity())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001393 {
1394 _ForwardIterator __mid = __last;
1395 bool __growing = false;
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001396 if (__new_size > size())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001397 {
1398 __growing = true;
1399 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001400 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001401 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001402 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001403 if (__growing)
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001404 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001405 else
1406 this->__destruct_at_end(__m);
1407 }
1408 else
1409 {
1410 deallocate();
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001411 allocate(__recommend(__new_size));
1412 __construct_at_end(__first, __last, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001413 }
1414}
1415
1416template <class _Tp, class _Allocator>
1417void
1418vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1419{
1420 if (__n <= capacity())
1421 {
1422 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001423 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001424 if (__n > __s)
1425 __construct_at_end(__n - __s, __u);
1426 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001427 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001428 }
1429 else
1430 {
1431 deallocate();
1432 allocate(__recommend(static_cast<size_type>(__n)));
1433 __construct_at_end(__n, __u);
1434 }
1435}
1436
Howard Hinnant324bb032010-08-22 00:02:43 +00001437template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001438inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001439typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001440vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441{
Howard Hinnantabe26282011-09-16 17:29:17 +00001442#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001443 return iterator(this, __p);
1444#else
1445 return iterator(__p);
1446#endif
1447}
1448
Howard Hinnant324bb032010-08-22 00:02:43 +00001449template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001450inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001451typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001452vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001453{
Howard Hinnantabe26282011-09-16 17:29:17 +00001454#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001455 return const_iterator(this, __p);
1456#else
1457 return const_iterator(__p);
1458#endif
1459}
1460
Howard Hinnant324bb032010-08-22 00:02:43 +00001461template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001462inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001464vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001465{
1466 return __make_iter(this->__begin_);
1467}
1468
Howard Hinnant324bb032010-08-22 00:02:43 +00001469template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001470inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001471typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001472vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001473{
1474 return __make_iter(this->__begin_);
1475}
1476
Howard Hinnant324bb032010-08-22 00:02:43 +00001477template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001478inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001479typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001480vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001481{
1482 return __make_iter(this->__end_);
1483}
1484
Howard Hinnant324bb032010-08-22 00:02:43 +00001485template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001486inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001487typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001488vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001489{
1490 return __make_iter(this->__end_);
1491}
1492
Howard Hinnant324bb032010-08-22 00:02:43 +00001493template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001494inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001495typename vector<_Tp, _Allocator>::reference
1496vector<_Tp, _Allocator>::operator[](size_type __n)
1497{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001498 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001499 return this->__begin_[__n];
1500}
1501
Howard Hinnant324bb032010-08-22 00:02:43 +00001502template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001503inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504typename vector<_Tp, _Allocator>::const_reference
1505vector<_Tp, _Allocator>::operator[](size_type __n) const
1506{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001507 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001508 return this->__begin_[__n];
1509}
1510
Howard Hinnant324bb032010-08-22 00:02:43 +00001511template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001512typename vector<_Tp, _Allocator>::reference
1513vector<_Tp, _Allocator>::at(size_type __n)
1514{
1515 if (__n >= size())
1516 this->__throw_out_of_range();
1517 return this->__begin_[__n];
1518}
1519
Howard Hinnant324bb032010-08-22 00:02:43 +00001520template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001521typename vector<_Tp, _Allocator>::const_reference
1522vector<_Tp, _Allocator>::at(size_type __n) const
1523{
1524 if (__n >= size())
1525 this->__throw_out_of_range();
1526 return this->__begin_[__n];
1527}
1528
1529template <class _Tp, class _Allocator>
1530void
1531vector<_Tp, _Allocator>::reserve(size_type __n)
1532{
1533 if (__n > capacity())
1534 {
1535 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001536 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001537 __swap_out_circular_buffer(__v);
1538 }
1539}
1540
1541template <class _Tp, class _Allocator>
1542void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001543vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001544{
1545 if (capacity() > size())
1546 {
1547#ifndef _LIBCPP_NO_EXCEPTIONS
1548 try
1549 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001550#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001551 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001552 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001553 __swap_out_circular_buffer(__v);
1554#ifndef _LIBCPP_NO_EXCEPTIONS
1555 }
1556 catch (...)
1557 {
1558 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001559#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001560 }
1561}
1562
1563template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001564template <class _Up>
1565void
1566#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1567vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1568#else
1569vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1570#endif
1571{
1572 allocator_type& __a = this->__alloc();
1573 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1574 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnantf619e232013-01-11 20:36:59 +00001575 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1576 __v.__end_++;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001577 __swap_out_circular_buffer(__v);
1578}
1579
1580template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001581inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001582void
1583vector<_Tp, _Allocator>::push_back(const_reference __x)
1584{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001585 if (this->__end_ != this->__end_cap())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001586 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001587 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001588 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001589 _VSTD::__to_raw_pointer(this->__end_), __x);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001590 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001591 ++this->__end_;
1592 }
1593 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001594 __push_back_slow_path(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001595}
1596
Howard Hinnant73d21a42010-09-04 23:28:19 +00001597#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001598
1599template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001600inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001601void
1602vector<_Tp, _Allocator>::push_back(value_type&& __x)
1603{
1604 if (this->__end_ < this->__end_cap())
1605 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001606 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001607 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001608 _VSTD::__to_raw_pointer(this->__end_),
1609 _VSTD::move(__x));
Kostya Serebryany497f9122014-09-02 23:43:38 +00001610 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611 ++this->__end_;
1612 }
1613 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001614 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001615}
1616
Howard Hinnant73d21a42010-09-04 23:28:19 +00001617#ifndef _LIBCPP_HAS_NO_VARIADICS
1618
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001619template <class _Tp, class _Allocator>
1620template <class... _Args>
1621void
Howard Hinnant0438ea22012-02-26 15:30:12 +00001622vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1623{
1624 allocator_type& __a = this->__alloc();
1625 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1626// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantf619e232013-01-11 20:36:59 +00001627 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1628 __v.__end_++;
Howard Hinnant0438ea22012-02-26 15:30:12 +00001629 __swap_out_circular_buffer(__v);
1630}
1631
1632template <class _Tp, class _Allocator>
1633template <class... _Args>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001634inline
Eric Fiselier3816ef92016-07-21 03:20:17 +00001635typename vector<_Tp, _Allocator>::reference
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001636vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1637{
1638 if (this->__end_ < this->__end_cap())
1639 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001640 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001641 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001642 _VSTD::__to_raw_pointer(this->__end_),
1643 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001644 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001645 ++this->__end_;
1646 }
1647 else
Howard Hinnant0438ea22012-02-26 15:30:12 +00001648 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Eric Fiselier3816ef92016-07-21 03:20:17 +00001649 return this->back();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001650}
1651
Howard Hinnant73d21a42010-09-04 23:28:19 +00001652#endif // _LIBCPP_HAS_NO_VARIADICS
1653#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001654
1655template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001656inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001657void
1658vector<_Tp, _Allocator>::pop_back()
1659{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001660 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001661 this->__destruct_at_end(this->__end_ - 1);
1662}
1663
1664template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001665inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001666typename vector<_Tp, _Allocator>::iterator
1667vector<_Tp, _Allocator>::erase(const_iterator __position)
1668{
Howard Hinnantabe26282011-09-16 17:29:17 +00001669#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001670 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1671 "vector::erase(iterator) called with an iterator not"
1672 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001673#endif
Howard Hinnant782da332013-03-25 22:12:26 +00001674 _LIBCPP_ASSERT(__position != end(),
1675 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001676 difference_type __ps = __position - cbegin();
1677 pointer __p = this->__begin_ + __ps;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001678 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001679 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001680 return __r;
1681}
1682
1683template <class _Tp, class _Allocator>
1684typename vector<_Tp, _Allocator>::iterator
1685vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1686{
Howard Hinnantabe26282011-09-16 17:29:17 +00001687#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001688 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1689 "vector::erase(iterator, iterator) called with an iterator not"
1690 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001691#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +00001692 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001693 pointer __p = this->__begin_ + (__first - begin());
1694 iterator __r = __make_iter(__p);
Howard Hinnantb4e67cf2013-04-18 15:02:57 +00001695 if (__first != __last)
1696 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001697 return __r;
1698}
1699
1700template <class _Tp, class _Allocator>
1701void
1702vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1703{
1704 pointer __old_last = this->__end_;
1705 difference_type __n = __old_last - __to;
1706 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1707 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001708 _VSTD::__to_raw_pointer(this->__end_),
1709 _VSTD::move(*__i));
1710 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001711}
1712
1713template <class _Tp, class _Allocator>
1714typename vector<_Tp, _Allocator>::iterator
1715vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1716{
Howard Hinnantabe26282011-09-16 17:29:17 +00001717#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001718 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1719 "vector::insert(iterator, x) called with an iterator not"
1720 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001721#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001722 pointer __p = this->__begin_ + (__position - begin());
1723 if (this->__end_ < this->__end_cap())
1724 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001725 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001726 if (__p == this->__end_)
1727 {
1728 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001729 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730 ++this->__end_;
1731 }
1732 else
1733 {
1734 __move_range(__p, this->__end_, __p + 1);
1735 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1736 if (__p <= __xr && __xr < this->__end_)
1737 ++__xr;
1738 *__p = *__xr;
1739 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001740 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001741 }
1742 else
1743 {
1744 allocator_type& __a = this->__alloc();
1745 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1746 __v.push_back(__x);
1747 __p = __swap_out_circular_buffer(__v, __p);
1748 }
1749 return __make_iter(__p);
1750}
1751
Howard Hinnant73d21a42010-09-04 23:28:19 +00001752#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001753
1754template <class _Tp, class _Allocator>
1755typename vector<_Tp, _Allocator>::iterator
1756vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1757{
Howard Hinnantabe26282011-09-16 17:29:17 +00001758#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001759 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1760 "vector::insert(iterator, x) called with an iterator not"
1761 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001762#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001763 pointer __p = this->__begin_ + (__position - begin());
1764 if (this->__end_ < this->__end_cap())
1765 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001766 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001767 if (__p == this->__end_)
1768 {
1769 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001770 _VSTD::__to_raw_pointer(this->__end_),
1771 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001772 ++this->__end_;
1773 }
1774 else
1775 {
1776 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001777 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001778 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001779 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001780 }
1781 else
1782 {
1783 allocator_type& __a = this->__alloc();
1784 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001785 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001786 __p = __swap_out_circular_buffer(__v, __p);
1787 }
1788 return __make_iter(__p);
1789}
1790
Howard Hinnant73d21a42010-09-04 23:28:19 +00001791#ifndef _LIBCPP_HAS_NO_VARIADICS
1792
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001793template <class _Tp, class _Allocator>
1794template <class... _Args>
1795typename vector<_Tp, _Allocator>::iterator
1796vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1797{
Howard Hinnantabe26282011-09-16 17:29:17 +00001798#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001799 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1800 "vector::emplace(iterator, x) called with an iterator not"
1801 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001802#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001803 pointer __p = this->__begin_ + (__position - begin());
1804 if (this->__end_ < this->__end_cap())
1805 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001806 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001807 if (__p == this->__end_)
1808 {
1809 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001810 _VSTD::__to_raw_pointer(this->__end_),
1811 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001812 ++this->__end_;
1813 }
1814 else
1815 {
Marshall Clow51d7e8e2016-07-11 21:38:08 +00001816 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817 __move_range(__p, this->__end_, __p + 1);
Marshall Clow51d7e8e2016-07-11 21:38:08 +00001818 *__p = _VSTD::move(__tmp.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001819 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001820 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001821 }
1822 else
1823 {
1824 allocator_type& __a = this->__alloc();
1825 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001826 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001827 __p = __swap_out_circular_buffer(__v, __p);
1828 }
1829 return __make_iter(__p);
1830}
1831
Howard Hinnant73d21a42010-09-04 23:28:19 +00001832#endif // _LIBCPP_HAS_NO_VARIADICS
1833#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001834
1835template <class _Tp, class _Allocator>
1836typename vector<_Tp, _Allocator>::iterator
1837vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1838{
Howard Hinnantabe26282011-09-16 17:29:17 +00001839#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001840 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1841 "vector::insert(iterator, n, x) called with an iterator not"
1842 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001843#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001844 pointer __p = this->__begin_ + (__position - begin());
1845 if (__n > 0)
1846 {
1847 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1848 {
1849 size_type __old_n = __n;
1850 pointer __old_last = this->__end_;
1851 if (__n > static_cast<size_type>(this->__end_ - __p))
1852 {
1853 size_type __cx = __n - (this->__end_ - __p);
1854 __construct_at_end(__cx, __x);
1855 __n -= __cx;
1856 }
1857 if (__n > 0)
1858 {
Eric Fiselierd7590952014-11-14 18:28:36 +00001859 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001860 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001861 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001862 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1863 if (__p <= __xr && __xr < this->__end_)
1864 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001865 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001866 }
1867 }
1868 else
1869 {
1870 allocator_type& __a = this->__alloc();
1871 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1872 __v.__construct_at_end(__n, __x);
1873 __p = __swap_out_circular_buffer(__v, __p);
1874 }
1875 }
1876 return __make_iter(__p);
1877}
1878
1879template <class _Tp, class _Allocator>
1880template <class _InputIterator>
1881typename enable_if
1882<
1883 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001884 !__is_forward_iterator<_InputIterator>::value &&
1885 is_constructible<
1886 _Tp,
1887 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001888 typename vector<_Tp, _Allocator>::iterator
1889>::type
1890vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1891{
Howard Hinnantabe26282011-09-16 17:29:17 +00001892#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001893 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1894 "vector::insert(iterator, range) called with an iterator not"
1895 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001896#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001897 difference_type __off = __position - begin();
1898 pointer __p = this->__begin_ + __off;
1899 allocator_type& __a = this->__alloc();
1900 pointer __old_last = this->__end_;
1901 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1902 {
Eric Fiselier7d439a42015-07-18 18:22:12 +00001903 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001904 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001905 *__first);
1906 ++this->__end_;
Eric Fiselier7d439a42015-07-18 18:22:12 +00001907 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001908 }
1909 __split_buffer<value_type, allocator_type&> __v(__a);
1910 if (__first != __last)
1911 {
1912#ifndef _LIBCPP_NO_EXCEPTIONS
1913 try
1914 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001915#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001916 __v.__construct_at_end(__first, __last);
1917 difference_type __old_size = __old_last - this->__begin_;
1918 difference_type __old_p = __p - this->__begin_;
1919 reserve(__recommend(size() + __v.size()));
1920 __p = this->__begin_ + __old_p;
1921 __old_last = this->__begin_ + __old_size;
1922#ifndef _LIBCPP_NO_EXCEPTIONS
1923 }
1924 catch (...)
1925 {
1926 erase(__make_iter(__old_last), end());
1927 throw;
1928 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001929#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001930 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001931 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant0442b122011-09-16 18:41:29 +00001932 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1933 make_move_iterator(__v.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001934 return begin() + __off;
1935}
1936
1937template <class _Tp, class _Allocator>
1938template <class _ForwardIterator>
1939typename enable_if
1940<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001941 __is_forward_iterator<_ForwardIterator>::value &&
1942 is_constructible<
1943 _Tp,
1944 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001945 typename vector<_Tp, _Allocator>::iterator
1946>::type
1947vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1948{
Howard Hinnantabe26282011-09-16 17:29:17 +00001949#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001950 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1951 "vector::insert(iterator, range) called with an iterator not"
1952 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001953#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001954 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00001955 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001956 if (__n > 0)
1957 {
1958 if (__n <= this->__end_cap() - this->__end_)
1959 {
1960 size_type __old_n = __n;
1961 pointer __old_last = this->__end_;
1962 _ForwardIterator __m = __last;
1963 difference_type __dx = this->__end_ - __p;
1964 if (__n > __dx)
1965 {
1966 __m = __first;
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001967 difference_type __diff = this->__end_ - __p;
1968 _VSTD::advance(__m, __diff);
1969 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001970 __n = __dx;
1971 }
1972 if (__n > 0)
1973 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001974 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001975 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001976 __annotator.__done();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001977 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001978 }
1979 }
1980 else
1981 {
1982 allocator_type& __a = this->__alloc();
1983 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1984 __v.__construct_at_end(__first, __last);
1985 __p = __swap_out_circular_buffer(__v, __p);
1986 }
1987 }
1988 return __make_iter(__p);
1989}
1990
1991template <class _Tp, class _Allocator>
1992void
1993vector<_Tp, _Allocator>::resize(size_type __sz)
1994{
1995 size_type __cs = size();
1996 if (__cs < __sz)
1997 this->__append(__sz - __cs);
1998 else if (__cs > __sz)
1999 this->__destruct_at_end(this->__begin_ + __sz);
2000}
2001
2002template <class _Tp, class _Allocator>
2003void
2004vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2005{
2006 size_type __cs = size();
2007 if (__cs < __sz)
2008 this->__append(__sz - __cs, __x);
2009 else if (__cs > __sz)
2010 this->__destruct_at_end(this->__begin_ + __sz);
2011}
2012
2013template <class _Tp, class _Allocator>
2014void
2015vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow7d914d12015-07-13 20:04:56 +00002016#if _LIBCPP_STD_VER >= 14
2017 _NOEXCEPT
2018#else
2019 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2020 __is_nothrow_swappable<allocator_type>::value)
2021#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002022{
Howard Hinnant7a563db2011-09-14 18:33:51 +00002023 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2024 this->__alloc() == __x.__alloc(),
2025 "vector::swap: Either propagate_on_container_swap must be true"
2026 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +00002027 _VSTD::swap(this->__begin_, __x.__begin_);
2028 _VSTD::swap(this->__end_, __x.__end_);
2029 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Marshall Clow7d914d12015-07-13 20:04:56 +00002030 __swap_allocator(this->__alloc(), __x.__alloc(),
2031 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantabe26282011-09-16 17:29:17 +00002032#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002033 __get_db()->swap(this, &__x);
Howard Hinnantabe26282011-09-16 17:29:17 +00002034#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035}
2036
Howard Hinnant324bb032010-08-22 00:02:43 +00002037template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002038bool
2039vector<_Tp, _Allocator>::__invariants() const
2040{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002041 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002042 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002043 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002044 return false;
2045 }
2046 else
2047 {
2048 if (this->__begin_ > this->__end_)
2049 return false;
2050 if (this->__begin_ == this->__end_cap())
2051 return false;
2052 if (this->__end_ > this->__end_cap())
2053 return false;
2054 }
2055 return true;
2056}
2057
Howard Hinnantabe26282011-09-16 17:29:17 +00002058#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002059
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002060template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:51 +00002061bool
2062vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2063{
2064 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2065}
2066
2067template <class _Tp, class _Allocator>
2068bool
2069vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2070{
2071 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2072}
2073
2074template <class _Tp, class _Allocator>
2075bool
2076vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2077{
2078 const_pointer __p = __i->base() + __n;
2079 return this->__begin_ <= __p && __p <= this->__end_;
2080}
2081
2082template <class _Tp, class _Allocator>
2083bool
2084vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2085{
2086 const_pointer __p = __i->base() + __n;
2087 return this->__begin_ <= __p && __p < this->__end_;
2088}
2089
Howard Hinnantabe26282011-09-16 17:29:17 +00002090#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002091
2092template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002093inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002094void
2095vector<_Tp, _Allocator>::__invalidate_all_iterators()
2096{
Howard Hinnantabe26282011-09-16 17:29:17 +00002097#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002098 __get_db()->__invalidate_all(this);
Howard Hinnantabe26282011-09-16 17:29:17 +00002099#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002100}
2101
2102// vector<bool>
2103
2104template <class _Allocator> class vector<bool, _Allocator>;
2105
2106template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2107
2108template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002109struct __has_storage_type<vector<bool, _Allocator> >
2110{
2111 static const bool value = true;
2112};
2113
2114template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002115class _LIBCPP_TYPE_VIS_ONLY vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002116 : private __vector_base_common<true>
2117{
2118public:
2119 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00002120 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002121 typedef _Allocator allocator_type;
2122 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002123 typedef typename __alloc_traits::size_type size_type;
2124 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantf867f632012-05-07 16:50:38 +00002125 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002126 typedef __bit_iterator<vector, false> pointer;
2127 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002128 typedef pointer iterator;
2129 typedef const_pointer const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002130 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2131 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002132
2133private:
Marshall Clow66302c62015-04-07 05:21:38 +00002134 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002135 typedef allocator_traits<__storage_allocator> __storage_traits;
2136 typedef typename __storage_traits::pointer __storage_pointer;
2137 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2138
2139 __storage_pointer __begin_;
2140 size_type __size_;
2141 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002142public:
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002143 typedef __bit_reference<vector> reference;
2144 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002145private:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002146 _LIBCPP_INLINE_VISIBILITY
2147 size_type& __cap() _NOEXCEPT
2148 {return __cap_alloc_.first();}
2149 _LIBCPP_INLINE_VISIBILITY
2150 const size_type& __cap() const _NOEXCEPT
2151 {return __cap_alloc_.first();}
2152 _LIBCPP_INLINE_VISIBILITY
2153 __storage_allocator& __alloc() _NOEXCEPT
2154 {return __cap_alloc_.second();}
2155 _LIBCPP_INLINE_VISIBILITY
2156 const __storage_allocator& __alloc() const _NOEXCEPT
2157 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002158
2159 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2160
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002161 _LIBCPP_INLINE_VISIBILITY
2162 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002163 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002164 _LIBCPP_INLINE_VISIBILITY
2165 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002166 {return (__n - 1) / __bits_per_word + 1;}
2167
2168public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002169 _LIBCPP_INLINE_VISIBILITY
Marshall Clowc912c0c2015-06-04 02:05:41 +00002170 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow127db912015-06-04 00:10:20 +00002171
2172 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2173#if _LIBCPP_STD_VER <= 14
2174 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2175#else
2176 _NOEXCEPT;
2177#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002178 ~vector();
2179 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59 +00002180#if _LIBCPP_STD_VER > 11
2181 explicit vector(size_type __n, const allocator_type& __a);
2182#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002183 vector(size_type __n, const value_type& __v);
2184 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2185 template <class _InputIterator>
2186 vector(_InputIterator __first, _InputIterator __last,
2187 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2188 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2189 template <class _InputIterator>
2190 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2191 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2192 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2193 template <class _ForwardIterator>
2194 vector(_ForwardIterator __first, _ForwardIterator __last,
2195 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2196 template <class _ForwardIterator>
2197 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2198 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2199
2200 vector(const vector& __v);
2201 vector(const vector& __v, const allocator_type& __a);
2202 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00002203#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002204 vector(initializer_list<value_type> __il);
2205 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00002206#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002207
Howard Hinnant73d21a42010-09-04 23:28:19 +00002208#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002209 _LIBCPP_INLINE_VISIBILITY
2210 vector(vector&& __v)
Marshall Clow119ed472015-07-14 14:46:32 +00002211#if _LIBCPP_STD_VER > 14
2212 _NOEXCEPT;
2213#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002214 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow119ed472015-07-14 14:46:32 +00002215#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002216 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002217 _LIBCPP_INLINE_VISIBILITY
2218 vector& operator=(vector&& __v)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002219 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnant73d21a42010-09-04 23:28:19 +00002220#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +00002221#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002222 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002223 vector& operator=(initializer_list<value_type> __il)
2224 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00002225#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002226
2227 template <class _InputIterator>
2228 typename enable_if
2229 <
2230 __is_input_iterator<_InputIterator>::value &&
2231 !__is_forward_iterator<_InputIterator>::value,
2232 void
2233 >::type
2234 assign(_InputIterator __first, _InputIterator __last);
2235 template <class _ForwardIterator>
2236 typename enable_if
2237 <
2238 __is_forward_iterator<_ForwardIterator>::value,
2239 void
2240 >::type
2241 assign(_ForwardIterator __first, _ForwardIterator __last);
2242
2243 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +00002244#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002246 void assign(initializer_list<value_type> __il)
2247 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002248#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002249
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002250 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002251 {return allocator_type(this->__alloc());}
2252
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002253 size_type max_size() const _NOEXCEPT;
2254 _LIBCPP_INLINE_VISIBILITY
2255 size_type capacity() const _NOEXCEPT
2256 {return __internal_cap_to_external(__cap());}
2257 _LIBCPP_INLINE_VISIBILITY
2258 size_type size() const _NOEXCEPT
2259 {return __size_;}
2260 _LIBCPP_INLINE_VISIBILITY
2261 bool empty() const _NOEXCEPT
2262 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002263 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002264 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002265
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002266 _LIBCPP_INLINE_VISIBILITY
2267 iterator begin() _NOEXCEPT
2268 {return __make_iter(0);}
2269 _LIBCPP_INLINE_VISIBILITY
2270 const_iterator begin() const _NOEXCEPT
2271 {return __make_iter(0);}
2272 _LIBCPP_INLINE_VISIBILITY
2273 iterator end() _NOEXCEPT
2274 {return __make_iter(__size_);}
2275 _LIBCPP_INLINE_VISIBILITY
2276 const_iterator end() const _NOEXCEPT
2277 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002278
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002279 _LIBCPP_INLINE_VISIBILITY
2280 reverse_iterator rbegin() _NOEXCEPT
2281 {return reverse_iterator(end());}
2282 _LIBCPP_INLINE_VISIBILITY
2283 const_reverse_iterator rbegin() const _NOEXCEPT
2284 {return const_reverse_iterator(end());}
2285 _LIBCPP_INLINE_VISIBILITY
2286 reverse_iterator rend() _NOEXCEPT
2287 {return reverse_iterator(begin());}
2288 _LIBCPP_INLINE_VISIBILITY
2289 const_reverse_iterator rend() const _NOEXCEPT
2290 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002291
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002292 _LIBCPP_INLINE_VISIBILITY
2293 const_iterator cbegin() const _NOEXCEPT
2294 {return __make_iter(0);}
2295 _LIBCPP_INLINE_VISIBILITY
2296 const_iterator cend() const _NOEXCEPT
2297 {return __make_iter(__size_);}
2298 _LIBCPP_INLINE_VISIBILITY
2299 const_reverse_iterator crbegin() const _NOEXCEPT
2300 {return rbegin();}
2301 _LIBCPP_INLINE_VISIBILITY
2302 const_reverse_iterator crend() const _NOEXCEPT
2303 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002304
2305 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2306 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2307 reference at(size_type __n);
2308 const_reference at(size_type __n) const;
2309
2310 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2311 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2312 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2313 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2314
2315 void push_back(const value_type& __x);
Marshall Clow198a2a52013-08-13 23:54:12 +00002316#if _LIBCPP_STD_VER > 11
2317 template <class... _Args>
Eric Fiselier3816ef92016-07-21 03:20:17 +00002318 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args) {
2319 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
2320 return this->back();
2321 }
Marshall Clow198a2a52013-08-13 23:54:12 +00002322#endif
2323
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002324 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2325
Marshall Clow198a2a52013-08-13 23:54:12 +00002326#if _LIBCPP_STD_VER > 11
2327 template <class... _Args>
2328 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2329 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2330#endif
2331
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002332 iterator insert(const_iterator __position, const value_type& __x);
2333 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2334 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2335 template <class _InputIterator>
2336 typename enable_if
2337 <
2338 __is_input_iterator <_InputIterator>::value &&
2339 !__is_forward_iterator<_InputIterator>::value,
2340 iterator
2341 >::type
2342 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2343 template <class _ForwardIterator>
2344 typename enable_if
2345 <
2346 __is_forward_iterator<_ForwardIterator>::value,
2347 iterator
2348 >::type
2349 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00002350#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002351 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002352 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2353 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002354#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002355
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002356 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002357 iterator erase(const_iterator __first, const_iterator __last);
2358
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002359 _LIBCPP_INLINE_VISIBILITY
2360 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002361
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002362 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +00002363#if _LIBCPP_STD_VER >= 14
2364 _NOEXCEPT;
2365#else
2366 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2367 __is_nothrow_swappable<allocator_type>::value);
2368#endif
Marshall Clow005c60b2016-04-07 14:20:31 +00002369 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002370
2371 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002372 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002373
2374 bool __invariants() const;
2375
2376private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002377 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002378 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002379 void deallocate() _NOEXCEPT;
2380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00002381 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow0d1965d2014-07-28 15:02:42 +00002382 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002383 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2384 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002385 template <class _ForwardIterator>
2386 typename enable_if
2387 <
2388 __is_forward_iterator<_ForwardIterator>::value,
2389 void
2390 >::type
2391 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2392 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002393 _LIBCPP_INLINE_VISIBILITY
2394 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002395 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002396 _LIBCPP_INLINE_VISIBILITY
2397 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002398 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002399 _LIBCPP_INLINE_VISIBILITY
2400 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002401 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002402 _LIBCPP_INLINE_VISIBILITY
2403 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002404 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002405 _LIBCPP_INLINE_VISIBILITY
2406 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002407 {return begin() + (__p - cbegin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002408
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002410 void __copy_assign_alloc(const vector& __v)
2411 {__copy_assign_alloc(__v, integral_constant<bool,
2412 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002414 void __copy_assign_alloc(const vector& __c, true_type)
2415 {
2416 if (__alloc() != __c.__alloc())
2417 deallocate();
2418 __alloc() = __c.__alloc();
2419 }
2420
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002421 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002422 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002423 {}
2424
2425 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002426 void __move_assign(vector& __c, true_type)
2427 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002429 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002430 _NOEXCEPT_(
2431 !__storage_traits::propagate_on_container_move_assignment::value ||
2432 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002433 {__move_assign_alloc(__c, integral_constant<bool,
2434 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00002436 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002437 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002438 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002439 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002440 }
2441
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002442 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002443 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002444 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002445 {}
2446
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002447 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002448
2449 friend class __bit_reference<vector>;
2450 friend class __bit_const_reference<vector>;
2451 friend class __bit_iterator<vector, false>;
2452 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:18 +00002453 friend struct __bit_array<vector>;
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002454 friend struct _LIBCPP_TYPE_VIS_ONLY hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002455};
2456
2457template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002458inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002459void
2460vector<bool, _Allocator>::__invalidate_all_iterators()
2461{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002462}
2463
2464// Allocate space for __n objects
2465// throws length_error if __n > max_size()
2466// throws (probably bad_alloc) if memory run out
2467// Precondition: __begin_ == __end_ == __cap() == 0
2468// Precondition: __n > 0
2469// Postcondition: capacity() == __n
2470// Postcondition: size() == 0
2471template <class _Allocator>
2472void
2473vector<bool, _Allocator>::allocate(size_type __n)
2474{
2475 if (__n > max_size())
2476 this->__throw_length_error();
2477 __n = __external_cap_to_internal(__n);
2478 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2479 this->__size_ = 0;
2480 this->__cap() = __n;
2481}
2482
2483template <class _Allocator>
2484void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002485vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002486{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002487 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002488 {
2489 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2490 __invalidate_all_iterators();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002491 this->__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002492 this->__size_ = this->__cap() = 0;
2493 }
2494}
2495
2496template <class _Allocator>
2497typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002498vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002499{
2500 size_type __amax = __storage_traits::max_size(__alloc());
2501 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2502 if (__nmax / __bits_per_word <= __amax)
2503 return __nmax;
2504 return __internal_cap_to_external(__amax);
2505}
2506
2507// Precondition: __new_size > capacity()
2508template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002509inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002510typename vector<bool, _Allocator>::size_type
2511vector<bool, _Allocator>::__recommend(size_type __new_size) const
2512{
2513 const size_type __ms = max_size();
2514 if (__new_size > __ms)
2515 this->__throw_length_error();
2516 const size_type __cap = capacity();
2517 if (__cap >= __ms / 2)
2518 return __ms;
Howard Hinnant7f764502013-08-14 18:00:20 +00002519 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002520}
2521
2522// Default constructs __n objects starting at __end_
2523// Precondition: __n > 0
2524// Precondition: size() + __n <= capacity()
2525// Postcondition: size() == size() + __n
2526template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002527inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002528void
2529vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2530{
2531 size_type __old_size = this->__size_;
2532 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002533 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002534}
2535
2536template <class _Allocator>
2537template <class _ForwardIterator>
2538typename enable_if
2539<
2540 __is_forward_iterator<_ForwardIterator>::value,
2541 void
2542>::type
2543vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2544{
2545 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002546 this->__size_ += _VSTD::distance(__first, __last);
2547 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002548}
2549
2550template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002551inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002552vector<bool, _Allocator>::vector()
Marshall Clowc912c0c2015-06-04 02:05:41 +00002553 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002554 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002555 __size_(0),
2556 __cap_alloc_(0)
2557{
2558}
2559
2560template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002561inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002562vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow127db912015-06-04 00:10:20 +00002563#if _LIBCPP_STD_VER <= 14
2564 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2565#else
2566 _NOEXCEPT
2567#endif
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002568 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002569 __size_(0),
2570 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2571{
2572}
2573
2574template <class _Allocator>
2575vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002576 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002577 __size_(0),
2578 __cap_alloc_(0)
2579{
2580 if (__n > 0)
2581 {
2582 allocate(__n);
2583 __construct_at_end(__n, false);
2584 }
2585}
2586
Marshall Clowa49a2c92013-09-14 00:47:59 +00002587#if _LIBCPP_STD_VER > 11
2588template <class _Allocator>
2589vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2590 : __begin_(nullptr),
2591 __size_(0),
2592 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2593{
2594 if (__n > 0)
2595 {
2596 allocate(__n);
2597 __construct_at_end(__n, false);
2598 }
2599}
2600#endif
2601
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002602template <class _Allocator>
2603vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002604 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002605 __size_(0),
2606 __cap_alloc_(0)
2607{
2608 if (__n > 0)
2609 {
2610 allocate(__n);
2611 __construct_at_end(__n, __x);
2612 }
2613}
2614
2615template <class _Allocator>
2616vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002617 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002618 __size_(0),
2619 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2620{
2621 if (__n > 0)
2622 {
2623 allocate(__n);
2624 __construct_at_end(__n, __x);
2625 }
2626}
2627
2628template <class _Allocator>
2629template <class _InputIterator>
2630vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2631 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2632 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002633 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002634 __size_(0),
2635 __cap_alloc_(0)
2636{
2637#ifndef _LIBCPP_NO_EXCEPTIONS
2638 try
2639 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002640#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002641 for (; __first != __last; ++__first)
2642 push_back(*__first);
2643#ifndef _LIBCPP_NO_EXCEPTIONS
2644 }
2645 catch (...)
2646 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002647 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002648 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2649 __invalidate_all_iterators();
2650 throw;
2651 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002652#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002653}
2654
2655template <class _Allocator>
2656template <class _InputIterator>
2657vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2658 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2659 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002660 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002661 __size_(0),
2662 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2663{
2664#ifndef _LIBCPP_NO_EXCEPTIONS
2665 try
2666 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002667#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002668 for (; __first != __last; ++__first)
2669 push_back(*__first);
2670#ifndef _LIBCPP_NO_EXCEPTIONS
2671 }
2672 catch (...)
2673 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002674 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002675 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2676 __invalidate_all_iterators();
2677 throw;
2678 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002679#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002680}
2681
2682template <class _Allocator>
2683template <class _ForwardIterator>
2684vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2685 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002686 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002687 __size_(0),
2688 __cap_alloc_(0)
2689{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002690 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002691 if (__n > 0)
2692 {
2693 allocate(__n);
2694 __construct_at_end(__first, __last);
2695 }
2696}
2697
2698template <class _Allocator>
2699template <class _ForwardIterator>
2700vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2701 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002702 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002703 __size_(0),
2704 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2705{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002706 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002707 if (__n > 0)
2708 {
2709 allocate(__n);
2710 __construct_at_end(__first, __last);
2711 }
2712}
2713
Howard Hinnante3e32912011-08-12 21:56:02 +00002714#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2715
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002716template <class _Allocator>
2717vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002718 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002719 __size_(0),
2720 __cap_alloc_(0)
2721{
2722 size_type __n = static_cast<size_type>(__il.size());
2723 if (__n > 0)
2724 {
2725 allocate(__n);
2726 __construct_at_end(__il.begin(), __il.end());
2727 }
2728}
2729
2730template <class _Allocator>
2731vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002732 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002733 __size_(0),
2734 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2735{
2736 size_type __n = static_cast<size_type>(__il.size());
2737 if (__n > 0)
2738 {
2739 allocate(__n);
2740 __construct_at_end(__il.begin(), __il.end());
2741 }
2742}
2743
Howard Hinnante3e32912011-08-12 21:56:02 +00002744#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2745
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002746template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002747vector<bool, _Allocator>::~vector()
2748{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002749 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002750 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002751 __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002752}
2753
2754template <class _Allocator>
2755vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002756 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002757 __size_(0),
2758 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2759{
2760 if (__v.size() > 0)
2761 {
2762 allocate(__v.size());
2763 __construct_at_end(__v.begin(), __v.end());
2764 }
2765}
2766
2767template <class _Allocator>
2768vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002769 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002770 __size_(0),
2771 __cap_alloc_(0, __a)
2772{
2773 if (__v.size() > 0)
2774 {
2775 allocate(__v.size());
2776 __construct_at_end(__v.begin(), __v.end());
2777 }
2778}
2779
2780template <class _Allocator>
2781vector<bool, _Allocator>&
2782vector<bool, _Allocator>::operator=(const vector& __v)
2783{
2784 if (this != &__v)
2785 {
2786 __copy_assign_alloc(__v);
2787 if (__v.__size_)
2788 {
2789 if (__v.__size_ > capacity())
2790 {
2791 deallocate();
2792 allocate(__v.__size_);
2793 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002794 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002795 }
2796 __size_ = __v.__size_;
2797 }
2798 return *this;
2799}
2800
Howard Hinnant73d21a42010-09-04 23:28:19 +00002801#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2802
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002803template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002804inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002805vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clow119ed472015-07-14 14:46:32 +00002806#if _LIBCPP_STD_VER > 14
2807 _NOEXCEPT
2808#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002809 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow119ed472015-07-14 14:46:32 +00002810#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002811 : __begin_(__v.__begin_),
2812 __size_(__v.__size_),
2813 __cap_alloc_(__v.__cap_alloc_)
2814{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002815 __v.__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002816 __v.__size_ = 0;
2817 __v.__cap() = 0;
2818}
2819
2820template <class _Allocator>
2821vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002822 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002823 __size_(0),
2824 __cap_alloc_(0, __a)
2825{
2826 if (__a == allocator_type(__v.__alloc()))
2827 {
2828 this->__begin_ = __v.__begin_;
2829 this->__size_ = __v.__size_;
2830 this->__cap() = __v.__cap();
2831 __v.__begin_ = nullptr;
2832 __v.__cap() = __v.__size_ = 0;
2833 }
2834 else if (__v.size() > 0)
2835 {
2836 allocate(__v.size());
2837 __construct_at_end(__v.begin(), __v.end());
2838 }
2839}
2840
2841template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002842inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002843vector<bool, _Allocator>&
2844vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002845 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002846{
2847 __move_assign(__v, integral_constant<bool,
2848 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002849 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002850}
2851
2852template <class _Allocator>
2853void
2854vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2855{
2856 if (__alloc() != __c.__alloc())
2857 assign(__c.begin(), __c.end());
2858 else
2859 __move_assign(__c, true_type());
2860}
2861
2862template <class _Allocator>
2863void
2864vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002865 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002866{
2867 deallocate();
Marshall Clowdb5e54d2014-07-21 15:15:15 +00002868 __move_assign_alloc(__c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002869 this->__begin_ = __c.__begin_;
2870 this->__size_ = __c.__size_;
2871 this->__cap() = __c.__cap();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002872 __c.__begin_ = nullptr;
2873 __c.__cap() = __c.__size_ = 0;
2874}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002875
2876#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002877
2878template <class _Allocator>
2879void
2880vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2881{
2882 __size_ = 0;
2883 if (__n > 0)
2884 {
2885 size_type __c = capacity();
2886 if (__n <= __c)
2887 __size_ = __n;
2888 else
2889 {
2890 vector __v(__alloc());
2891 __v.reserve(__recommend(__n));
2892 __v.__size_ = __n;
2893 swap(__v);
2894 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002895 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002896 }
2897}
2898
2899template <class _Allocator>
2900template <class _InputIterator>
2901typename enable_if
2902<
2903 __is_input_iterator<_InputIterator>::value &&
2904 !__is_forward_iterator<_InputIterator>::value,
2905 void
2906>::type
2907vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2908{
2909 clear();
2910 for (; __first != __last; ++__first)
2911 push_back(*__first);
2912}
2913
2914template <class _Allocator>
2915template <class _ForwardIterator>
2916typename enable_if
2917<
2918 __is_forward_iterator<_ForwardIterator>::value,
2919 void
2920>::type
2921vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2922{
2923 clear();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002924 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002925 if (__n)
2926 {
2927 if (__n > capacity())
2928 {
2929 deallocate();
2930 allocate(__n);
2931 }
2932 __construct_at_end(__first, __last);
2933 }
2934}
2935
2936template <class _Allocator>
2937void
2938vector<bool, _Allocator>::reserve(size_type __n)
2939{
2940 if (__n > capacity())
2941 {
2942 vector __v(this->__alloc());
2943 __v.allocate(__n);
2944 __v.__construct_at_end(this->begin(), this->end());
2945 swap(__v);
2946 __invalidate_all_iterators();
2947 }
2948}
2949
2950template <class _Allocator>
2951void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002952vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002953{
2954 if (__external_cap_to_internal(size()) > __cap())
2955 {
2956#ifndef _LIBCPP_NO_EXCEPTIONS
2957 try
2958 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002959#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002960 vector(*this, allocator_type(__alloc())).swap(*this);
2961#ifndef _LIBCPP_NO_EXCEPTIONS
2962 }
2963 catch (...)
2964 {
2965 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002966#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002967 }
2968}
2969
2970template <class _Allocator>
2971typename vector<bool, _Allocator>::reference
2972vector<bool, _Allocator>::at(size_type __n)
2973{
2974 if (__n >= size())
2975 this->__throw_out_of_range();
2976 return (*this)[__n];
2977}
2978
2979template <class _Allocator>
2980typename vector<bool, _Allocator>::const_reference
2981vector<bool, _Allocator>::at(size_type __n) const
2982{
2983 if (__n >= size())
2984 this->__throw_out_of_range();
2985 return (*this)[__n];
2986}
2987
2988template <class _Allocator>
2989void
2990vector<bool, _Allocator>::push_back(const value_type& __x)
2991{
2992 if (this->__size_ == this->capacity())
2993 reserve(__recommend(this->__size_ + 1));
2994 ++this->__size_;
2995 back() = __x;
2996}
2997
2998template <class _Allocator>
2999typename vector<bool, _Allocator>::iterator
3000vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3001{
3002 iterator __r;
3003 if (size() < capacity())
3004 {
3005 const_iterator __old_end = end();
3006 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003007 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003008 __r = __const_iterator_cast(__position);
3009 }
3010 else
3011 {
3012 vector __v(__alloc());
3013 __v.reserve(__recommend(__size_ + 1));
3014 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003015 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3016 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003017 swap(__v);
3018 }
3019 *__r = __x;
3020 return __r;
3021}
3022
3023template <class _Allocator>
3024typename vector<bool, _Allocator>::iterator
3025vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3026{
3027 iterator __r;
3028 size_type __c = capacity();
3029 if (__n <= __c && size() <= __c - __n)
3030 {
3031 const_iterator __old_end = end();
3032 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003033 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003034 __r = __const_iterator_cast(__position);
3035 }
3036 else
3037 {
3038 vector __v(__alloc());
3039 __v.reserve(__recommend(__size_ + __n));
3040 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003041 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3042 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003043 swap(__v);
3044 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003045 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003046 return __r;
3047}
3048
3049template <class _Allocator>
3050template <class _InputIterator>
3051typename enable_if
3052<
3053 __is_input_iterator <_InputIterator>::value &&
3054 !__is_forward_iterator<_InputIterator>::value,
3055 typename vector<bool, _Allocator>::iterator
3056>::type
3057vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3058{
3059 difference_type __off = __position - begin();
3060 iterator __p = __const_iterator_cast(__position);
3061 iterator __old_end = end();
3062 for (; size() != capacity() && __first != __last; ++__first)
3063 {
3064 ++this->__size_;
3065 back() = *__first;
3066 }
3067 vector __v(__alloc());
3068 if (__first != __last)
3069 {
3070#ifndef _LIBCPP_NO_EXCEPTIONS
3071 try
3072 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003073#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003074 __v.assign(__first, __last);
3075 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3076 difference_type __old_p = __p - begin();
3077 reserve(__recommend(size() + __v.size()));
3078 __p = begin() + __old_p;
3079 __old_end = begin() + __old_size;
3080#ifndef _LIBCPP_NO_EXCEPTIONS
3081 }
3082 catch (...)
3083 {
3084 erase(__old_end, end());
3085 throw;
3086 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003087#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003088 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003089 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003090 insert(__p, __v.begin(), __v.end());
3091 return begin() + __off;
3092}
3093
3094template <class _Allocator>
3095template <class _ForwardIterator>
3096typename enable_if
3097<
3098 __is_forward_iterator<_ForwardIterator>::value,
3099 typename vector<bool, _Allocator>::iterator
3100>::type
3101vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3102{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003103 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003104 iterator __r;
3105 size_type __c = capacity();
3106 if (__n <= __c && size() <= __c - __n)
3107 {
3108 const_iterator __old_end = end();
3109 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003110 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003111 __r = __const_iterator_cast(__position);
3112 }
3113 else
3114 {
3115 vector __v(__alloc());
3116 __v.reserve(__recommend(__size_ + __n));
3117 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003118 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3119 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003120 swap(__v);
3121 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003122 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003123 return __r;
3124}
3125
3126template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003127inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003128typename vector<bool, _Allocator>::iterator
3129vector<bool, _Allocator>::erase(const_iterator __position)
3130{
3131 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003132 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003133 --__size_;
3134 return __r;
3135}
3136
3137template <class _Allocator>
3138typename vector<bool, _Allocator>::iterator
3139vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3140{
3141 iterator __r = __const_iterator_cast(__first);
3142 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003143 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003144 __size_ -= __d;
3145 return __r;
3146}
3147
3148template <class _Allocator>
3149void
3150vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow7d914d12015-07-13 20:04:56 +00003151#if _LIBCPP_STD_VER >= 14
3152 _NOEXCEPT
3153#else
3154 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3155 __is_nothrow_swappable<allocator_type>::value)
3156#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003157{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003158 _VSTD::swap(this->__begin_, __x.__begin_);
3159 _VSTD::swap(this->__size_, __x.__size_);
3160 _VSTD::swap(this->__cap(), __x.__cap());
Marshall Clow7d914d12015-07-13 20:04:56 +00003161 __swap_allocator(this->__alloc(), __x.__alloc(),
3162 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003163}
3164
Howard Hinnant324bb032010-08-22 00:02:43 +00003165template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003166void
3167vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3168{
3169 size_type __cs = size();
3170 if (__cs < __sz)
3171 {
3172 iterator __r;
3173 size_type __c = capacity();
3174 size_type __n = __sz - __cs;
3175 if (__n <= __c && __cs <= __c - __n)
3176 {
3177 __r = end();
3178 __size_ += __n;
3179 }
3180 else
3181 {
3182 vector __v(__alloc());
3183 __v.reserve(__recommend(__size_ + __n));
3184 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003185 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003186 swap(__v);
3187 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003188 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003189 }
3190 else
3191 __size_ = __sz;
3192}
3193
Howard Hinnant324bb032010-08-22 00:02:43 +00003194template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003195void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003196vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003197{
3198 // do middle whole words
3199 size_type __n = __size_;
3200 __storage_pointer __p = __begin_;
3201 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3202 *__p = ~*__p;
3203 // do last partial word
3204 if (__n > 0)
3205 {
3206 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3207 __storage_type __b = *__p & __m;
3208 *__p &= ~__m;
3209 *__p |= ~__b & __m;
3210 }
3211}
3212
Howard Hinnant324bb032010-08-22 00:02:43 +00003213template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003214bool
3215vector<bool, _Allocator>::__invariants() const
3216{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00003217 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003218 {
3219 if (this->__size_ != 0 || this->__cap() != 0)
3220 return false;
3221 }
3222 else
3223 {
3224 if (this->__cap() == 0)
3225 return false;
3226 if (this->__size_ > this->capacity())
3227 return false;
3228 }
3229 return true;
3230}
3231
Howard Hinnant324bb032010-08-22 00:02:43 +00003232template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003233size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003234vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003235{
3236 size_t __h = 0;
3237 // do middle whole words
3238 size_type __n = __size_;
3239 __storage_pointer __p = __begin_;
3240 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3241 __h ^= *__p;
3242 // do last partial word
3243 if (__n > 0)
3244 {
3245 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3246 __h ^= *__p & __m;
3247 }
3248 return __h;
3249}
3250
3251template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003252struct _LIBCPP_TYPE_VIS_ONLY hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003253 : public unary_function<vector<bool, _Allocator>, size_t>
3254{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00003255 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003256 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003257 {return __vec.__hash_code();}
3258};
3259
3260template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003261inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003262bool
3263operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3264{
3265 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003266 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003267}
3268
3269template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003270inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003271bool
3272operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3273{
3274 return !(__x == __y);
3275}
3276
3277template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003278inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003279bool
3280operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3281{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003282 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
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 __y < __x;
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{
3298 return !(__x < __y);
3299}
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 +00003311void
3312swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003313 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003314{
3315 __x.swap(__y);
3316}
3317
3318_LIBCPP_END_NAMESPACE_STD
3319
3320#endif // _LIBCPP_VECTOR