blob: 3dae3b5df53c4997e48559f19d3bdfbbf9f1bf7a [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------------ vector --------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-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 Hinnantc51e1022010-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 Hinnant3b6579a2010-08-22 00:02:43 +000020template <class T, class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000021class vector
Howard Hinnant3b6579a2010-08-22 00:02:43 +000022{
23public:
24 typedef T value_type;
Howard Hinnantc51e1022010-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 Hinnant1c936782011-06-03 19:40:40 +000037 vector()
38 noexcept(is_nothrow_default_constructible<allocator_type>::value);
39 explicit vector(const allocator_type&);
Howard Hinnantc51e1022010-05-11 19:42:16 +000040 explicit vector(size_type n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +000041 explicit vector(size_type n, const allocator_type&); // C++14
Howard Hinnantc51e1022010-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 Hinnant1c936782011-06-03 19:40:40 +000046 vector(vector&& x)
47 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-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 Hinnant1c936782011-06-03 19:40:40 +000052 vector& operator=(vector&& x)
53 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +000054 allocator_type::propagate_on_container_move_assignment::value ||
55 allocator_type::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-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 Hinnant1c936782011-06-03 19:40:40 +000062 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000063
Howard Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +000068
Howard Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +000073
Howard Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +000078
Howard Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +000083 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +000084 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-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 Hinnant1c936782011-06-03 19:40:40 +000096 value_type* data() noexcept;
97 const value_type* data() const noexcept;
Howard Hinnantc51e1022010-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 Fiselier34ba5b92016-07-21 03:20:17 +0000102 reference emplace_back(Args&&... args);
Howard Hinnantc51e1022010-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 Hinnant1c936782011-06-03 19:40:40 +0000116 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000117
118 void resize(size_type sz);
119 void resize(size_type sz, const value_type& c);
120
Howard Hinnant1c936782011-06-03 19:40:40 +0000121 void swap(vector&)
Marshall Clow8982dcd2015-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 Hinnantc51e1022010-05-11 19:42:16 +0000124
125 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000126};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000127
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000128template <class Allocator = allocator<T> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000129class vector<bool, Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000130{
131public:
132 typedef bool value_type;
Howard Hinnantc51e1022010-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 Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +0000152 };
153
154 class const_reference
155 {
156 public:
Howard Hinnant1c936782011-06-03 19:40:40 +0000157 const_reference(const reference&) noexcept;
158 operator bool() const noexcept;
159 const_iterator operator&() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160 };
161
Howard Hinnant1c936782011-06-03 19:40:40 +0000162 vector()
163 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantc2734962011-09-02 20:42:31 +0000164 explicit vector(const allocator_type&);
Marshall Clowd3cbeaa2013-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 Hinnantc51e1022010-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 Hinnant1c936782011-06-03 19:40:40 +0000170 vector(vector&& x)
171 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-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 Hinnant1c936782011-06-03 19:40:40 +0000176 vector& operator=(vector&& x)
177 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000178 allocator_type::propagate_on_container_move_assignment::value ||
179 allocator_type::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-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 Hinnant1c936782011-06-03 19:40:40 +0000186 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187
Howard Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +0000192
Howard Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +0000197
Howard Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +0000202
Howard Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +0000207 void reserve(size_type n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000208 void shrink_to_fit() noexcept;
Howard Hinnantc51e1022010-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 Fiselier34ba5b92016-07-21 03:20:17 +0000221 template <class... Args> reference emplace_back(Args&&... args); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222 void pop_back();
223
Marshall Clowc46bb8e2013-08-13 23:54:12 +0000224 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
Howard Hinnantc51e1022010-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 Hinnant1c936782011-06-03 19:40:40 +0000234 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000235
236 void resize(size_type sz);
237 void resize(size_type sz, value_type x);
238
Howard Hinnant1c936782011-06-03 19:40:40 +0000239 void swap(vector&)
Marshall Clow8982dcd2015-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 Hinnant1c936782011-06-03 19:40:40 +0000242 void flip() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000243
244 bool __invariants() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000245};
Howard Hinnantc51e1022010-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 Hinnant1c936782011-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 Hinnantc51e1022010-05-11 19:42:16 +0000259
260} // std
261
262*/
263
264#include <__config>
Eric Fiselier876c6862016-02-20 00:19:45 +0000265#include <iosfwd> // for forward declaration of vector
Howard Hinnantc51e1022010-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 Hinnantc51e1022010-05-11 19:42:16 +0000277
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000278#include <__undef_min_max>
279
Eric Fiselier14b6de92014-08-10 23:53:08 +0000280#include <__debug>
Howard Hinnante6ff0b62013-08-02 00:26:35 +0000281
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000282#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000284#endif
Howard Hinnantc51e1022010-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() {}
Marshall Clow8fea1612016-08-25 15:09:01 +0000293 _LIBCPP_NORETURN void __throw_length_error() const;
294 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000295};
296
297template <bool __b>
298void
299__vector_base_common<__b>::__throw_length_error() const
300{
Marshall Clow8fea1612016-08-25 15:09:01 +0000301 _VSTD::__throw_length_error("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000302}
303
304template <bool __b>
305void
306__vector_base_common<__b>::__throw_out_of_range() const
307{
Marshall Clow8fea1612016-08-25 15:09:01 +0000308 _VSTD::__throw_out_of_range("vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000309}
310
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000311#ifdef _LIBCPP_MSVC
Howard Hinnantbf074022011-10-22 20:59:45 +0000312#pragma warning( push )
313#pragma warning( disable: 4231 )
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000314#endif // _LIBCPP_MSVC
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000315_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __vector_base_common<true>)
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000316#ifdef _LIBCPP_MSVC
Howard Hinnantbf074022011-10-22 20:59:45 +0000317#pragma warning( pop )
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000318#endif // _LIBCPP_MSVC
Howard Hinnantc51e1022010-05-11 19:42:16 +0000319
320template <class _Tp, class _Allocator>
321class __vector_base
322 : protected __vector_base_common<true>
323{
324protected:
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000325 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000326 typedef _Allocator allocator_type;
327 typedef allocator_traits<allocator_type> __alloc_traits;
328 typedef value_type& reference;
329 typedef const value_type& const_reference;
330 typedef typename __alloc_traits::size_type size_type;
331 typedef typename __alloc_traits::difference_type difference_type;
332 typedef typename __alloc_traits::pointer pointer;
333 typedef typename __alloc_traits::const_pointer const_pointer;
334 typedef pointer iterator;
335 typedef const_pointer const_iterator;
336
337 pointer __begin_;
338 pointer __end_;
339 __compressed_pair<pointer, allocator_type> __end_cap_;
340
Howard Hinnant1c936782011-06-03 19:40:40 +0000341 _LIBCPP_INLINE_VISIBILITY
342 allocator_type& __alloc() _NOEXCEPT
343 {return __end_cap_.second();}
344 _LIBCPP_INLINE_VISIBILITY
345 const allocator_type& __alloc() const _NOEXCEPT
346 {return __end_cap_.second();}
347 _LIBCPP_INLINE_VISIBILITY
348 pointer& __end_cap() _NOEXCEPT
349 {return __end_cap_.first();}
350 _LIBCPP_INLINE_VISIBILITY
351 const pointer& __end_cap() const _NOEXCEPT
352 {return __end_cap_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353
Howard Hinnant1c936782011-06-03 19:40:40 +0000354 _LIBCPP_INLINE_VISIBILITY
355 __vector_base()
356 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantcf823322010-12-17 14:46:43 +0000357 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000358 ~__vector_base();
359
Howard Hinnant1c936782011-06-03 19:40:40 +0000360 _LIBCPP_INLINE_VISIBILITY
361 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
362 _LIBCPP_INLINE_VISIBILITY
363 size_type capacity() const _NOEXCEPT
364 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000365
Howard Hinnant1c936782011-06-03 19:40:40 +0000366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000367 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000368
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370 void __copy_assign_alloc(const __vector_base& __c)
371 {__copy_assign_alloc(__c, integral_constant<bool,
372 __alloc_traits::propagate_on_container_copy_assignment::value>());}
373
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000375 void __move_assign_alloc(__vector_base& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +0000376 _NOEXCEPT_(
377 !__alloc_traits::propagate_on_container_move_assignment::value ||
378 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379 {__move_assign_alloc(__c, integral_constant<bool,
380 __alloc_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381private:
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383 void __copy_assign_alloc(const __vector_base& __c, true_type)
384 {
385 if (__alloc() != __c.__alloc())
386 {
387 clear();
388 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
389 __begin_ = __end_ = __end_cap() = nullptr;
390 }
391 __alloc() = __c.__alloc();
392 }
393
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000395 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396 {}
397
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +0000399 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000400 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000402 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403 }
404
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +0000406 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +0000407 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000408 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409};
410
411template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000412inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413void
Howard Hinnant76053d72013-06-27 19:35:32 +0000414__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000416 while (__new_last != __end_)
Howard Hinnant76053d72013-06-27 19:35:32 +0000417 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__end_));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000418}
419
420template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000421inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000422__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnant1c936782011-06-03 19:40:40 +0000423 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +0000424 : __begin_(nullptr),
425 __end_(nullptr),
426 __end_cap_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000427{
428}
429
430template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000431inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000432__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +0000433 : __begin_(nullptr),
434 __end_(nullptr),
435 __end_cap_(nullptr, __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000436{
437}
438
439template <class _Tp, class _Allocator>
440__vector_base<_Tp, _Allocator>::~__vector_base()
441{
Howard Hinnant76053d72013-06-27 19:35:32 +0000442 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000443 {
444 clear();
445 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
446 }
447}
448
Eric Fiselier876c6862016-02-20 00:19:45 +0000449template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000450class _LIBCPP_TYPE_VIS_ONLY vector
Howard Hinnantc51e1022010-05-11 19:42:16 +0000451 : private __vector_base<_Tp, _Allocator>
452{
453private:
454 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000455 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000456public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000457 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000458 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459 typedef _Allocator allocator_type;
460 typedef typename __base::__alloc_traits __alloc_traits;
461 typedef typename __base::reference reference;
462 typedef typename __base::const_reference const_reference;
463 typedef typename __base::size_type size_type;
464 typedef typename __base::difference_type difference_type;
465 typedef typename __base::pointer pointer;
466 typedef typename __base::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000467 typedef __wrap_iter<pointer> iterator;
468 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000469 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
470 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000471
Howard Hinnanta416ff02013-03-26 19:04:56 +0000472 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
473 "Allocator::value_type must be same type as value_type");
474
Howard Hinnant1c936782011-06-03 19:40:40 +0000475 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +0000476 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000477 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000478#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000479 __get_db()->__insert_c(this);
480#endif
481 }
482 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +0000483#if _LIBCPP_STD_VER <= 14
484 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
485#else
486 _NOEXCEPT
487#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000488 : __base(__a)
489 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000490#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000491 __get_db()->__insert_c(this);
492#endif
493 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000494 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +0000495#if _LIBCPP_STD_VER > 11
496 explicit vector(size_type __n, const allocator_type& __a);
497#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000498 vector(size_type __n, const_reference __x);
499 vector(size_type __n, const_reference __x, const allocator_type& __a);
500 template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000501 vector(_InputIterator __first,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000502 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000503 !__is_forward_iterator<_InputIterator>::value &&
504 is_constructible<
505 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000506 typename iterator_traits<_InputIterator>::reference>::value,
507 _InputIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000508 template <class _InputIterator>
509 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
510 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000511 !__is_forward_iterator<_InputIterator>::value &&
512 is_constructible<
513 value_type,
514 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000515 template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +0000516 vector(_ForwardIterator __first,
Howard Hinnant88010252013-03-28 17:44:32 +0000517 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
518 is_constructible<
519 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +0000520 typename iterator_traits<_ForwardIterator>::reference>::value,
521 _ForwardIterator>::type __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000522 template <class _ForwardIterator>
523 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant88010252013-03-28 17:44:32 +0000524 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
525 is_constructible<
526 value_type,
527 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Howard Hinnant33711792011-08-12 21:56:02 +0000528#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantcf823322010-12-17 14:46:43 +0000529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000530 vector(initializer_list<value_type> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnant33711792011-08-12 21:56:02 +0000533#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000534#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27e0e772011-09-14 18:33:51 +0000536 ~vector()
537 {
538 __get_db()->__erase_c(this);
539 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000540#endif
541
542 vector(const vector& __x);
543 vector(const vector& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000545 vector& operator=(const vector& __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000546#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantcf823322010-12-17 14:46:43 +0000547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000548 vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +0000549#if _LIBCPP_STD_VER > 14
550 _NOEXCEPT;
551#else
Howard Hinnant1c936782011-06-03 19:40:40 +0000552 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +0000553#endif
Howard Hinnantcf823322010-12-17 14:46:43 +0000554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000555 vector(vector&& __x, const allocator_type& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000557 vector& operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000558 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnant74279a52010-09-04 23:28:19 +0000559#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant33711792011-08-12 21:56:02 +0000560#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562 vector& operator=(initializer_list<value_type> __il)
563 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnant33711792011-08-12 21:56:02 +0000564#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565
566 template <class _InputIterator>
567 typename enable_if
568 <
569 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000570 !__is_forward_iterator<_InputIterator>::value &&
571 is_constructible<
572 value_type,
573 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574 void
575 >::type
576 assign(_InputIterator __first, _InputIterator __last);
577 template <class _ForwardIterator>
578 typename enable_if
579 <
Howard Hinnant88010252013-03-28 17:44:32 +0000580 __is_forward_iterator<_ForwardIterator>::value &&
581 is_constructible<
582 value_type,
583 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584 void
585 >::type
586 assign(_ForwardIterator __first, _ForwardIterator __last);
587
588 void assign(size_type __n, const_reference __u);
Howard Hinnant33711792011-08-12 21:56:02 +0000589#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591 void assign(initializer_list<value_type> __il)
592 {assign(__il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +0000593#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000594
Howard Hinnant1c936782011-06-03 19:40:40 +0000595 _LIBCPP_INLINE_VISIBILITY
596 allocator_type get_allocator() const _NOEXCEPT
597 {return this->__alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000598
Howard Hinnant1c936782011-06-03 19:40:40 +0000599 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
600 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
601 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
602 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000603
Howard Hinnant1c936782011-06-03 19:40:40 +0000604 _LIBCPP_INLINE_VISIBILITY
605 reverse_iterator rbegin() _NOEXCEPT
606 {return reverse_iterator(end());}
607 _LIBCPP_INLINE_VISIBILITY
608 const_reverse_iterator rbegin() const _NOEXCEPT
609 {return const_reverse_iterator(end());}
610 _LIBCPP_INLINE_VISIBILITY
611 reverse_iterator rend() _NOEXCEPT
612 {return reverse_iterator(begin());}
613 _LIBCPP_INLINE_VISIBILITY
614 const_reverse_iterator rend() const _NOEXCEPT
615 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000616
Howard Hinnant1c936782011-06-03 19:40:40 +0000617 _LIBCPP_INLINE_VISIBILITY
618 const_iterator cbegin() const _NOEXCEPT
619 {return begin();}
620 _LIBCPP_INLINE_VISIBILITY
621 const_iterator cend() const _NOEXCEPT
622 {return end();}
623 _LIBCPP_INLINE_VISIBILITY
624 const_reverse_iterator crbegin() const _NOEXCEPT
625 {return rbegin();}
626 _LIBCPP_INLINE_VISIBILITY
627 const_reverse_iterator crend() const _NOEXCEPT
628 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000629
Howard Hinnant1c936782011-06-03 19:40:40 +0000630 _LIBCPP_INLINE_VISIBILITY
631 size_type size() const _NOEXCEPT
632 {return static_cast<size_type>(this->__end_ - this->__begin_);}
633 _LIBCPP_INLINE_VISIBILITY
634 size_type capacity() const _NOEXCEPT
635 {return __base::capacity();}
636 _LIBCPP_INLINE_VISIBILITY
637 bool empty() const _NOEXCEPT
638 {return this->__begin_ == this->__end_;}
639 size_type max_size() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000640 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000641 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000642
643 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
644 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
645 reference at(size_type __n);
646 const_reference at(size_type __n) const;
647
Howard Hinnant27e0e772011-09-14 18:33:51 +0000648 _LIBCPP_INLINE_VISIBILITY reference front()
649 {
650 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
651 return *this->__begin_;
652 }
653 _LIBCPP_INLINE_VISIBILITY const_reference front() const
654 {
655 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
656 return *this->__begin_;
657 }
658 _LIBCPP_INLINE_VISIBILITY reference back()
659 {
660 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
661 return *(this->__end_ - 1);
662 }
663 _LIBCPP_INLINE_VISIBILITY const_reference back() const
664 {
665 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
666 return *(this->__end_ - 1);
667 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000668
Howard Hinnant1c936782011-06-03 19:40:40 +0000669 _LIBCPP_INLINE_VISIBILITY
670 value_type* data() _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000671 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnant1c936782011-06-03 19:40:40 +0000672 _LIBCPP_INLINE_VISIBILITY
673 const value_type* data() const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000674 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675
Howard Hinnantcf823322010-12-17 14:46:43 +0000676 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000677#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000678 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000679#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680 template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000681 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000682 reference emplace_back(_Args&&... __args);
Howard Hinnant74279a52010-09-04 23:28:19 +0000683#endif // _LIBCPP_HAS_NO_VARIADICS
684#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686 void pop_back();
687
688 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000689#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000690 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant74279a52010-09-04 23:28:19 +0000691#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692 template <class... _Args>
693 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant74279a52010-09-04 23:28:19 +0000694#endif // _LIBCPP_HAS_NO_VARIADICS
695#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696 iterator insert(const_iterator __position, size_type __n, const_reference __x);
697 template <class _InputIterator>
698 typename enable_if
699 <
700 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +0000701 !__is_forward_iterator<_InputIterator>::value &&
702 is_constructible<
703 value_type,
704 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000705 iterator
706 >::type
707 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
708 template <class _ForwardIterator>
709 typename enable_if
710 <
Howard Hinnant88010252013-03-28 17:44:32 +0000711 __is_forward_iterator<_ForwardIterator>::value &&
712 is_constructible<
713 value_type,
714 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000715 iterator
716 >::type
717 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant33711792011-08-12 21:56:02 +0000718#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000720 iterator insert(const_iterator __position, initializer_list<value_type> __il)
721 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +0000722#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000723
Howard Hinnantcf823322010-12-17 14:46:43 +0000724 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000725 iterator erase(const_iterator __first, const_iterator __last);
726
Howard Hinnant1c936782011-06-03 19:40:40 +0000727 _LIBCPP_INLINE_VISIBILITY
728 void clear() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000729 {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000730 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000731 __base::clear();
Marshall Clow2cd9d372014-05-08 14:14:06 +0000732 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000733 __invalidate_all_iterators();
734 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000735
736 void resize(size_type __sz);
737 void resize(size_type __sz, const_reference __x);
738
Howard Hinnant1c936782011-06-03 19:40:40 +0000739 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000740#if _LIBCPP_STD_VER >= 14
741 _NOEXCEPT;
742#else
743 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
744 __is_nothrow_swappable<allocator_type>::value);
745#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746
747 bool __invariants() const;
748
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000749#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000750
751 bool __dereferenceable(const const_iterator* __i) const;
752 bool __decrementable(const const_iterator* __i) const;
753 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
754 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
755
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000756#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000757
Howard Hinnantc51e1022010-05-11 19:42:16 +0000758private:
Howard Hinnantcf823322010-12-17 14:46:43 +0000759 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +0000761 void deallocate() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000762 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnanta5589382011-01-04 19:53:31 +0000763 void __construct_at_end(size_type __n);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000764 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000766 template <class _ForwardIterator>
767 typename enable_if
768 <
769 __is_forward_iterator<_ForwardIterator>::value,
770 void
771 >::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +0000772 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000773 void __append(size_type __n);
774 void __append(size_type __n, const_reference __x);
Howard Hinnantcf823322010-12-17 14:46:43 +0000775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000776 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +0000778 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000779 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
780 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
781 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnant1c936782011-06-03 19:40:40 +0000782 void __move_assign(vector& __c, true_type)
783 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000784 void __move_assign(vector& __c, false_type)
785 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76053d72013-06-27 19:35:32 +0000787 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +0000788 {
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000789#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +0000790 __c_node* __c = __get_db()->__find_c_and_lock(this);
791 for (__i_node** __p = __c->end_; __p != __c->beg_; )
792 {
793 --__p;
794 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
795 if (__i->base() > __new_last)
796 {
797 (*__p)->__c_ = nullptr;
798 if (--__c->end_ != __p)
799 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
800 }
801 }
802 __get_db()->unlock();
803#endif
Marshall Clow2cd9d372014-05-08 14:14:06 +0000804 size_type __old_size = size();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000805 __base::__destruct_at_end(__new_last);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000806 __annotate_shrink(__old_size);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000807 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000808 template <class _Up>
809 void
810#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
811 __push_back_slow_path(_Up&& __x);
812#else
813 __push_back_slow_path(_Up& __x);
814#endif
Howard Hinnantb6c49562012-02-26 15:30:12 +0000815#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
816 template <class... _Args>
817 void
818 __emplace_back_slow_path(_Args&&... __args);
819#endif
Marshall Clow2cd9d372014-05-08 14:14:06 +0000820 // The following functions are no-ops outside of AddressSanitizer mode.
821 // We call annotatations only for the default Allocator because other allocators
822 // may not meet the AddressSanitizer alignment constraints.
823 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
Marshall Clow2cd9d372014-05-08 14:14:06 +0000824#ifndef _LIBCPP_HAS_NO_ASAN
Eric Fiselier6003c772016-12-23 23:37:52 +0000825 void __annotate_contiguous_container(const void *__beg, const void *__end,
826 const void *__old_mid,
827 const void *__new_mid) const
828 {
829
Marshall Clow2cd9d372014-05-08 14:14:06 +0000830 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
831 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
Marshall Clow2cd9d372014-05-08 14:14:06 +0000832 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000833#else
834 _LIBCPP_INLINE_VISIBILITY
835 void __annotate_contiguous_container(const void*, const void*, const void*,
836 const void*) const {}
837#endif
838 _LIBCPP_INLINE_VISIBILITY
839 void __annotate_new(size_type __current_size) const {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000840 __annotate_contiguous_container(data(), data() + capacity(),
841 data() + capacity(), data() + __current_size);
842 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000843
844 _LIBCPP_INLINE_VISIBILITY
845 void __annotate_delete() const {
Marshall Clow2cd9d372014-05-08 14:14:06 +0000846 __annotate_contiguous_container(data(), data() + capacity(),
847 data() + size(), data() + capacity());
848 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000849
850 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany4963c252014-09-02 23:43:38 +0000851 void __annotate_increase(size_type __n) const
Marshall Clow2cd9d372014-05-08 14:14:06 +0000852 {
853 __annotate_contiguous_container(data(), data() + capacity(),
854 data() + size(), data() + size() + __n);
855 }
Eric Fiselier6003c772016-12-23 23:37:52 +0000856
857 _LIBCPP_INLINE_VISIBILITY
Kostya Serebryany4963c252014-09-02 23:43:38 +0000858 void __annotate_shrink(size_type __old_size) const
Marshall Clow2cd9d372014-05-08 14:14:06 +0000859 {
860 __annotate_contiguous_container(data(), data() + capacity(),
861 data() + __old_size, data() + size());
862 }
Marshall Clowc78ffa52014-09-03 21:37:43 +0000863#ifndef _LIBCPP_HAS_NO_ASAN
Kostya Serebryany4963c252014-09-02 23:43:38 +0000864 // The annotation for size increase should happen before the actual increase,
865 // but if an exception is thrown after that the annotation has to be undone.
866 struct __RAII_IncreaseAnnotator {
867 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000868 : __commit(false), __v(__v), __old_size(__v.size() + __n) {
Kostya Serebryany4963c252014-09-02 23:43:38 +0000869 __v.__annotate_increase(__n);
870 }
871 void __done() { __commit = true; }
872 ~__RAII_IncreaseAnnotator() {
873 if (__commit) return;
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000874 __v.__annotate_shrink(__old_size);
Kostya Serebryany4963c252014-09-02 23:43:38 +0000875 }
876 bool __commit;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000877 const vector &__v;
Eric Fiseliera7c043e2015-03-10 00:25:20 +0000878 size_type __old_size;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000879 };
Marshall Clowc78ffa52014-09-03 21:37:43 +0000880#else
881 struct __RAII_IncreaseAnnotator {
Eric Fiselier6003c772016-12-23 23:37:52 +0000882 _LIBCPP_INLINE_VISIBILITY
883 __RAII_IncreaseAnnotator(const vector &, size_type = 1) {}
884 _LIBCPP_INLINE_VISIBILITY void __done() {}
Marshall Clowc78ffa52014-09-03 21:37:43 +0000885 };
886#endif
887
Howard Hinnantc51e1022010-05-11 19:42:16 +0000888};
889
890template <class _Tp, class _Allocator>
891void
892vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
893{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000894 __annotate_delete();
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000895 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000896 _VSTD::swap(this->__begin_, __v.__begin_);
897 _VSTD::swap(this->__end_, __v.__end_);
898 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000899 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000900 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901 __invalidate_all_iterators();
902}
903
904template <class _Tp, class _Allocator>
905typename vector<_Tp, _Allocator>::pointer
906vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
907{
Marshall Clow2cd9d372014-05-08 14:14:06 +0000908 __annotate_delete();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000909 pointer __r = __v.__begin_;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000910 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
911 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000912 _VSTD::swap(this->__begin_, __v.__begin_);
913 _VSTD::swap(this->__end_, __v.__end_);
914 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000915 __v.__first_ = __v.__begin_;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000916 __annotate_new(size());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000917 __invalidate_all_iterators();
918 return __r;
919}
920
921// Allocate space for __n objects
922// throws length_error if __n > max_size()
923// throws (probably bad_alloc) if memory run out
924// Precondition: __begin_ == __end_ == __end_cap() == 0
925// Precondition: __n > 0
926// Postcondition: capacity() == __n
927// Postcondition: size() == 0
928template <class _Tp, class _Allocator>
929void
930vector<_Tp, _Allocator>::allocate(size_type __n)
931{
932 if (__n > max_size())
933 this->__throw_length_error();
934 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
935 this->__end_cap() = this->__begin_ + __n;
Marshall Clow2cd9d372014-05-08 14:14:06 +0000936 __annotate_new(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937}
938
939template <class _Tp, class _Allocator>
940void
Howard Hinnant1c936782011-06-03 19:40:40 +0000941vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942{
Howard Hinnant76053d72013-06-27 19:35:32 +0000943 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000944 {
945 clear();
946 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant76053d72013-06-27 19:35:32 +0000947 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000948 }
949}
950
951template <class _Tp, class _Allocator>
952typename vector<_Tp, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +0000953vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000954{
Eric Fiselierb5d9f442016-11-23 01:18:56 +0000955 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
956 numeric_limits<difference_type>::max());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000957}
958
959// Precondition: __new_size > capacity()
960template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000961inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962typename vector<_Tp, _Allocator>::size_type
963vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
964{
965 const size_type __ms = max_size();
966 if (__new_size > __ms)
967 this->__throw_length_error();
968 const size_type __cap = capacity();
969 if (__cap >= __ms / 2)
970 return __ms;
Alexis Hunt991d29b2011-07-29 23:31:58 +0000971 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972}
973
974// Default constructs __n objects starting at __end_
975// throws if construction throws
976// Precondition: __n > 0
977// Precondition: size() + __n <= capacity()
978// Postcondition: size() == size() + __n
979template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980void
981vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
982{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 allocator_type& __a = this->__alloc();
984 do
985 {
Kostya Serebryany4963c252014-09-02 23:43:38 +0000986 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000987 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988 ++this->__end_;
989 --__n;
Kostya Serebryany4963c252014-09-02 23:43:38 +0000990 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991 } while (__n > 0);
992}
993
Howard Hinnantc51e1022010-05-11 19:42:16 +0000994// Copy constructs __n objects starting at __end_ from __x
995// throws if construction throws
996// Precondition: __n > 0
997// Precondition: size() + __n <= capacity()
998// Postcondition: size() == old size() + __n
999// Postcondition: [i] == __x for all i in [size() - __n, __n)
1000template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001001inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001002void
1003vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1004{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005 allocator_type& __a = this->__alloc();
1006 do
1007 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001008 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001009 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010 ++this->__end_;
1011 --__n;
Kostya Serebryany4963c252014-09-02 23:43:38 +00001012 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013 } while (__n > 0);
1014}
1015
1016template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017template <class _ForwardIterator>
1018typename enable_if
1019<
1020 __is_forward_iterator<_ForwardIterator>::value,
1021 void
1022>::type
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001023vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001024{
1025 allocator_type& __a = this->__alloc();
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001026 __RAII_IncreaseAnnotator __annotator(*this, __n);
1027 __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
1028 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029}
1030
1031// Default constructs __n objects starting at __end_
1032// throws if construction throws
1033// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001034// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035template <class _Tp, class _Allocator>
1036void
1037vector<_Tp, _Allocator>::__append(size_type __n)
1038{
1039 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1040 this->__construct_at_end(__n);
1041 else
1042 {
1043 allocator_type& __a = this->__alloc();
1044 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1045 __v.__construct_at_end(__n);
1046 __swap_out_circular_buffer(__v);
1047 }
1048}
1049
1050// Default constructs __n objects starting at __end_
1051// throws if construction throws
1052// Postcondition: size() == size() + __n
Howard Hinnant1c936782011-06-03 19:40:40 +00001053// Exception safety: strong.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054template <class _Tp, class _Allocator>
1055void
1056vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1057{
1058 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1059 this->__construct_at_end(__n, __x);
1060 else
1061 {
1062 allocator_type& __a = this->__alloc();
1063 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1064 __v.__construct_at_end(__n, __x);
1065 __swap_out_circular_buffer(__v);
1066 }
1067}
1068
1069template <class _Tp, class _Allocator>
1070vector<_Tp, _Allocator>::vector(size_type __n)
1071{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001072#if _LIBCPP_DEBUG_LEVEL >= 2
1073 __get_db()->__insert_c(this);
1074#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075 if (__n > 0)
1076 {
1077 allocate(__n);
1078 __construct_at_end(__n);
1079 }
1080}
1081
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00001082#if _LIBCPP_STD_VER > 11
1083template <class _Tp, class _Allocator>
1084vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1085 : __base(__a)
1086{
1087#if _LIBCPP_DEBUG_LEVEL >= 2
1088 __get_db()->__insert_c(this);
1089#endif
1090 if (__n > 0)
1091 {
1092 allocate(__n);
1093 __construct_at_end(__n);
1094 }
1095}
1096#endif
1097
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098template <class _Tp, class _Allocator>
1099vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1100{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001101#if _LIBCPP_DEBUG_LEVEL >= 2
1102 __get_db()->__insert_c(this);
1103#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104 if (__n > 0)
1105 {
1106 allocate(__n);
1107 __construct_at_end(__n, __x);
1108 }
1109}
1110
1111template <class _Tp, class _Allocator>
1112vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1113 : __base(__a)
1114{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001115#if _LIBCPP_DEBUG_LEVEL >= 2
1116 __get_db()->__insert_c(this);
1117#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118 if (__n > 0)
1119 {
1120 allocate(__n);
1121 __construct_at_end(__n, __x);
1122 }
1123}
1124
1125template <class _Tp, class _Allocator>
1126template <class _InputIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001127vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001129 !__is_forward_iterator<_InputIterator>::value &&
1130 is_constructible<
1131 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001132 typename iterator_traits<_InputIterator>::reference>::value,
1133 _InputIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001135#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001136 __get_db()->__insert_c(this);
1137#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001138 for (; __first != __last; ++__first)
1139 push_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140}
1141
1142template <class _Tp, class _Allocator>
1143template <class _InputIterator>
1144vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1145 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001146 !__is_forward_iterator<_InputIterator>::value &&
1147 is_constructible<
1148 value_type,
1149 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150 : __base(__a)
1151{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001152#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001153 __get_db()->__insert_c(this);
1154#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001155 for (; __first != __last; ++__first)
1156 push_back(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157}
1158
1159template <class _Tp, class _Allocator>
1160template <class _ForwardIterator>
Howard Hinnant66fce262013-09-21 21:13:54 +00001161vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant88010252013-03-28 17:44:32 +00001162 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1163 is_constructible<
1164 value_type,
Howard Hinnant66fce262013-09-21 21:13:54 +00001165 typename iterator_traits<_ForwardIterator>::reference>::value,
1166 _ForwardIterator>::type __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001168#if _LIBCPP_DEBUG_LEVEL >= 2
1169 __get_db()->__insert_c(this);
1170#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001171 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172 if (__n > 0)
1173 {
1174 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001175 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176 }
1177}
1178
1179template <class _Tp, class _Allocator>
1180template <class _ForwardIterator>
1181vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant88010252013-03-28 17:44:32 +00001182 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1183 is_constructible<
1184 value_type,
1185 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186 : __base(__a)
1187{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001188#if _LIBCPP_DEBUG_LEVEL >= 2
1189 __get_db()->__insert_c(this);
1190#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001191 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192 if (__n > 0)
1193 {
1194 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001195 __construct_at_end(__first, __last, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196 }
1197}
1198
1199template <class _Tp, class _Allocator>
1200vector<_Tp, _Allocator>::vector(const vector& __x)
1201 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1202{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001203#if _LIBCPP_DEBUG_LEVEL >= 2
1204 __get_db()->__insert_c(this);
1205#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206 size_type __n = __x.size();
1207 if (__n > 0)
1208 {
1209 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001210 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211 }
1212}
1213
1214template <class _Tp, class _Allocator>
1215vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1216 : __base(__a)
1217{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001218#if _LIBCPP_DEBUG_LEVEL >= 2
1219 __get_db()->__insert_c(this);
1220#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221 size_type __n = __x.size();
1222 if (__n > 0)
1223 {
1224 allocate(__n);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001225 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226 }
1227}
1228
Howard Hinnant74279a52010-09-04 23:28:19 +00001229#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001230
1231template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001232inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clowe5108202015-07-14 14:46:32 +00001234#if _LIBCPP_STD_VER > 14
1235 _NOEXCEPT
1236#else
Howard Hinnant1c936782011-06-03 19:40:40 +00001237 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00001238#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001239 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001240{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001241#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001242 __get_db()->__insert_c(this);
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001243 __get_db()->swap(this, &__x);
Howard Hinnant27e0e772011-09-14 18:33:51 +00001244#endif
Howard Hinnant9cd22302011-09-16 18:41:29 +00001245 this->__begin_ = __x.__begin_;
1246 this->__end_ = __x.__end_;
1247 this->__end_cap() = __x.__end_cap();
Howard Hinnant76053d72013-06-27 19:35:32 +00001248 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249}
1250
1251template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001252inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1254 : __base(__a)
1255{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001256#if _LIBCPP_DEBUG_LEVEL >= 2
1257 __get_db()->__insert_c(this);
1258#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001259 if (__a == __x.__alloc())
1260 {
1261 this->__begin_ = __x.__begin_;
1262 this->__end_ = __x.__end_;
1263 this->__end_cap() = __x.__end_cap();
1264 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001265#if _LIBCPP_DEBUG_LEVEL >= 2
1266 __get_db()->swap(this, &__x);
1267#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001268 }
1269 else
1270 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001271 typedef move_iterator<iterator> _Ip;
1272 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001273 }
1274}
1275
Howard Hinnant33711792011-08-12 21:56:02 +00001276#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1277
Howard Hinnantc51e1022010-05-11 19:42:16 +00001278template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001279inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001280vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1281{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001282#if _LIBCPP_DEBUG_LEVEL >= 2
1283 __get_db()->__insert_c(this);
1284#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285 if (__il.size() > 0)
1286 {
1287 allocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001288 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289 }
1290}
1291
1292template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001293inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001294vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1295 : __base(__a)
1296{
Howard Hinnant9cd22302011-09-16 18:41:29 +00001297#if _LIBCPP_DEBUG_LEVEL >= 2
1298 __get_db()->__insert_c(this);
1299#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001300 if (__il.size() > 0)
1301 {
1302 allocate(__il.size());
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001303 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304 }
1305}
1306
Howard Hinnant33711792011-08-12 21:56:02 +00001307#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1308
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001310inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001311vector<_Tp, _Allocator>&
1312vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001313 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001314{
1315 __move_assign(__x, integral_constant<bool,
1316 __alloc_traits::propagate_on_container_move_assignment::value>());
1317 return *this;
1318}
1319
1320template <class _Tp, class _Allocator>
1321void
1322vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001323 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324{
1325 if (__base::__alloc() != __c.__alloc())
1326 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001327 typedef move_iterator<iterator> _Ip;
1328 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001329 }
1330 else
1331 __move_assign(__c, true_type());
1332}
1333
1334template <class _Tp, class _Allocator>
1335void
1336vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00001337 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338{
1339 deallocate();
Marshall Clow136d45c2014-07-21 15:11:13 +00001340 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341 this->__begin_ = __c.__begin_;
1342 this->__end_ = __c.__end_;
1343 this->__end_cap() = __c.__end_cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001344 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnanta1e60cd2011-09-19 16:34:29 +00001345#if _LIBCPP_DEBUG_LEVEL >= 2
1346 __get_db()->swap(this, &__c);
1347#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001348}
1349
Howard Hinnant74279a52010-09-04 23:28:19 +00001350#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001351
1352template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001353inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001354vector<_Tp, _Allocator>&
1355vector<_Tp, _Allocator>::operator=(const vector& __x)
1356{
1357 if (this != &__x)
1358 {
1359 __base::__copy_assign_alloc(__x);
1360 assign(__x.__begin_, __x.__end_);
1361 }
1362 return *this;
1363}
1364
1365template <class _Tp, class _Allocator>
1366template <class _InputIterator>
1367typename enable_if
1368<
1369 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001370 !__is_forward_iterator<_InputIterator>::value &&
1371 is_constructible<
1372 _Tp,
1373 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001374 void
1375>::type
1376vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1377{
1378 clear();
1379 for (; __first != __last; ++__first)
1380 push_back(*__first);
1381}
1382
1383template <class _Tp, class _Allocator>
1384template <class _ForwardIterator>
1385typename enable_if
1386<
Howard Hinnant88010252013-03-28 17:44:32 +00001387 __is_forward_iterator<_ForwardIterator>::value &&
1388 is_constructible<
1389 _Tp,
1390 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001391 void
1392>::type
1393vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1394{
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001395 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1396 if (__new_size <= capacity())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397 {
1398 _ForwardIterator __mid = __last;
1399 bool __growing = false;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001400 if (__new_size > size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001401 {
1402 __growing = true;
1403 __mid = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001404 _VSTD::advance(__mid, size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001406 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001407 if (__growing)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001408 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001409 else
1410 this->__destruct_at_end(__m);
1411 }
1412 else
1413 {
1414 deallocate();
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001415 allocate(__recommend(__new_size));
1416 __construct_at_end(__first, __last, __new_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417 }
1418}
1419
1420template <class _Tp, class _Allocator>
1421void
1422vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1423{
1424 if (__n <= capacity())
1425 {
1426 size_type __s = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001427 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428 if (__n > __s)
1429 __construct_at_end(__n - __s, __u);
1430 else
Howard Hinnant155c2af2010-05-24 17:49:41 +00001431 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432 }
1433 else
1434 {
1435 deallocate();
1436 allocate(__recommend(static_cast<size_type>(__n)));
1437 __construct_at_end(__n, __u);
1438 }
1439}
1440
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001441template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001442inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001444vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001446#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447 return iterator(this, __p);
1448#else
1449 return iterator(__p);
1450#endif
1451}
1452
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001453template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001454inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001456vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001458#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459 return const_iterator(this, __p);
1460#else
1461 return const_iterator(__p);
1462#endif
1463}
1464
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001465template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001466inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001467typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001468vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469{
1470 return __make_iter(this->__begin_);
1471}
1472
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001473template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001474inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001475typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001476vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477{
1478 return __make_iter(this->__begin_);
1479}
1480
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001481template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001482inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001483typename vector<_Tp, _Allocator>::iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001484vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001485{
1486 return __make_iter(this->__end_);
1487}
1488
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001489template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001490inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001491typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnant1c936782011-06-03 19:40:40 +00001492vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493{
1494 return __make_iter(this->__end_);
1495}
1496
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001497template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001498inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499typename vector<_Tp, _Allocator>::reference
1500vector<_Tp, _Allocator>::operator[](size_type __n)
1501{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001502 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001503 return this->__begin_[__n];
1504}
1505
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001506template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001507inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001508typename vector<_Tp, _Allocator>::const_reference
1509vector<_Tp, _Allocator>::operator[](size_type __n) const
1510{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001511 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001512 return this->__begin_[__n];
1513}
1514
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001515template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001516typename vector<_Tp, _Allocator>::reference
1517vector<_Tp, _Allocator>::at(size_type __n)
1518{
1519 if (__n >= size())
1520 this->__throw_out_of_range();
1521 return this->__begin_[__n];
1522}
1523
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001524template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525typename vector<_Tp, _Allocator>::const_reference
1526vector<_Tp, _Allocator>::at(size_type __n) const
1527{
1528 if (__n >= size())
1529 this->__throw_out_of_range();
1530 return this->__begin_[__n];
1531}
1532
1533template <class _Tp, class _Allocator>
1534void
1535vector<_Tp, _Allocator>::reserve(size_type __n)
1536{
1537 if (__n > capacity())
1538 {
1539 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001540 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541 __swap_out_circular_buffer(__v);
1542 }
1543}
1544
1545template <class _Tp, class _Allocator>
1546void
Howard Hinnant1c936782011-06-03 19:40:40 +00001547vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548{
1549 if (capacity() > size())
1550 {
1551#ifndef _LIBCPP_NO_EXCEPTIONS
1552 try
1553 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001554#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001555 allocator_type& __a = this->__alloc();
Howard Hinnant1c936782011-06-03 19:40:40 +00001556 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557 __swap_out_circular_buffer(__v);
1558#ifndef _LIBCPP_NO_EXCEPTIONS
1559 }
1560 catch (...)
1561 {
1562 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001563#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564 }
1565}
1566
1567template <class _Tp, class _Allocator>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001568template <class _Up>
1569void
1570#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1571vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1572#else
1573vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1574#endif
1575{
1576 allocator_type& __a = this->__alloc();
1577 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1578 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001579 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1580 __v.__end_++;
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001581 __swap_out_circular_buffer(__v);
1582}
1583
1584template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001585inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586void
1587vector<_Tp, _Allocator>::push_back(const_reference __x)
1588{
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001589 if (this->__end_ != this->__end_cap())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001591 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001593 _VSTD::__to_raw_pointer(this->__end_), __x);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001594 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595 ++this->__end_;
1596 }
1597 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001598 __push_back_slow_path(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599}
1600
Howard Hinnant74279a52010-09-04 23:28:19 +00001601#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602
1603template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001604inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001605void
1606vector<_Tp, _Allocator>::push_back(value_type&& __x)
1607{
1608 if (this->__end_ < this->__end_cap())
1609 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001610 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001611 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001612 _VSTD::__to_raw_pointer(this->__end_),
1613 _VSTD::move(__x));
Kostya Serebryany4963c252014-09-02 23:43:38 +00001614 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001615 ++this->__end_;
1616 }
1617 else
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001618 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001619}
1620
Howard Hinnant74279a52010-09-04 23:28:19 +00001621#ifndef _LIBCPP_HAS_NO_VARIADICS
1622
Howard Hinnantc51e1022010-05-11 19:42:16 +00001623template <class _Tp, class _Allocator>
1624template <class... _Args>
1625void
Howard Hinnantb6c49562012-02-26 15:30:12 +00001626vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1627{
1628 allocator_type& __a = this->__alloc();
1629 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1630// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001631 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1632 __v.__end_++;
Howard Hinnantb6c49562012-02-26 15:30:12 +00001633 __swap_out_circular_buffer(__v);
1634}
1635
1636template <class _Tp, class _Allocator>
1637template <class... _Args>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001638inline
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001639typename vector<_Tp, _Allocator>::reference
Howard Hinnantc51e1022010-05-11 19:42:16 +00001640vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1641{
1642 if (this->__end_ < this->__end_cap())
1643 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001644 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001645 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001646 _VSTD::__to_raw_pointer(this->__end_),
1647 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001648 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001649 ++this->__end_;
1650 }
1651 else
Howard Hinnantb6c49562012-02-26 15:30:12 +00001652 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001653 return this->back();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001654}
1655
Howard Hinnant74279a52010-09-04 23:28:19 +00001656#endif // _LIBCPP_HAS_NO_VARIADICS
1657#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658
1659template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001660inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001661void
1662vector<_Tp, _Allocator>::pop_back()
1663{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001664 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665 this->__destruct_at_end(this->__end_ - 1);
1666}
1667
1668template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00001669inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001670typename vector<_Tp, _Allocator>::iterator
1671vector<_Tp, _Allocator>::erase(const_iterator __position)
1672{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001673#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001674 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1675 "vector::erase(iterator) called with an iterator not"
1676 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001677#endif
Howard Hinnant58d52d52013-03-25 22:12:26 +00001678 _LIBCPP_ASSERT(__position != end(),
1679 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant76053d72013-06-27 19:35:32 +00001680 difference_type __ps = __position - cbegin();
1681 pointer __p = this->__begin_ + __ps;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001682 iterator __r = __make_iter(__p);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001683 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001684 return __r;
1685}
1686
1687template <class _Tp, class _Allocator>
1688typename vector<_Tp, _Allocator>::iterator
1689vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1690{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001691#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001692 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1693 "vector::erase(iterator, iterator) called with an iterator not"
1694 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001695#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001696 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697 pointer __p = this->__begin_ + (__first - begin());
1698 iterator __r = __make_iter(__p);
Howard Hinnantc580cc32013-04-18 15:02:57 +00001699 if (__first != __last)
1700 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701 return __r;
1702}
1703
1704template <class _Tp, class _Allocator>
1705void
1706vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1707{
1708 pointer __old_last = this->__end_;
1709 difference_type __n = __old_last - __to;
1710 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1711 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001712 _VSTD::__to_raw_pointer(this->__end_),
1713 _VSTD::move(*__i));
1714 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715}
1716
1717template <class _Tp, class _Allocator>
1718typename vector<_Tp, _Allocator>::iterator
1719vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1720{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001721#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001722 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1723 "vector::insert(iterator, x) called with an iterator not"
1724 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001725#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001726 pointer __p = this->__begin_ + (__position - begin());
1727 if (this->__end_ < this->__end_cap())
1728 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001729 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730 if (__p == this->__end_)
1731 {
1732 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001733 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001734 ++this->__end_;
1735 }
1736 else
1737 {
1738 __move_range(__p, this->__end_, __p + 1);
1739 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1740 if (__p <= __xr && __xr < this->__end_)
1741 ++__xr;
1742 *__p = *__xr;
1743 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001744 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745 }
1746 else
1747 {
1748 allocator_type& __a = this->__alloc();
1749 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1750 __v.push_back(__x);
1751 __p = __swap_out_circular_buffer(__v, __p);
1752 }
1753 return __make_iter(__p);
1754}
1755
Howard Hinnant74279a52010-09-04 23:28:19 +00001756#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001757
1758template <class _Tp, class _Allocator>
1759typename vector<_Tp, _Allocator>::iterator
1760vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1761{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001762#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001763 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1764 "vector::insert(iterator, x) called with an iterator not"
1765 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001766#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001767 pointer __p = this->__begin_ + (__position - begin());
1768 if (this->__end_ < this->__end_cap())
1769 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001770 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001771 if (__p == this->__end_)
1772 {
1773 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001774 _VSTD::__to_raw_pointer(this->__end_),
1775 _VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776 ++this->__end_;
1777 }
1778 else
1779 {
1780 __move_range(__p, this->__end_, __p + 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001781 *__p = _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001782 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001783 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001784 }
1785 else
1786 {
1787 allocator_type& __a = this->__alloc();
1788 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001789 __v.push_back(_VSTD::move(__x));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001790 __p = __swap_out_circular_buffer(__v, __p);
1791 }
1792 return __make_iter(__p);
1793}
1794
Howard Hinnant74279a52010-09-04 23:28:19 +00001795#ifndef _LIBCPP_HAS_NO_VARIADICS
1796
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797template <class _Tp, class _Allocator>
1798template <class... _Args>
1799typename vector<_Tp, _Allocator>::iterator
1800vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1801{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001802#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001803 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1804 "vector::emplace(iterator, x) called with an iterator not"
1805 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001806#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001807 pointer __p = this->__begin_ + (__position - begin());
1808 if (this->__end_ < this->__end_cap())
1809 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001810 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811 if (__p == this->__end_)
1812 {
1813 __alloc_traits::construct(this->__alloc(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001814 _VSTD::__to_raw_pointer(this->__end_),
1815 _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001816 ++this->__end_;
1817 }
1818 else
1819 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00001820 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001821 __move_range(__p, this->__end_, __p + 1);
Marshall Clowa591b9a2016-07-11 21:38:08 +00001822 *__p = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001823 }
Kostya Serebryany4963c252014-09-02 23:43:38 +00001824 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825 }
1826 else
1827 {
1828 allocator_type& __a = this->__alloc();
1829 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001830 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831 __p = __swap_out_circular_buffer(__v, __p);
1832 }
1833 return __make_iter(__p);
1834}
1835
Howard Hinnant74279a52010-09-04 23:28:19 +00001836#endif // _LIBCPP_HAS_NO_VARIADICS
1837#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838
1839template <class _Tp, class _Allocator>
1840typename vector<_Tp, _Allocator>::iterator
1841vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1842{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001843#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001844 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1845 "vector::insert(iterator, n, x) called with an iterator not"
1846 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001847#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001848 pointer __p = this->__begin_ + (__position - begin());
1849 if (__n > 0)
1850 {
1851 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1852 {
1853 size_type __old_n = __n;
1854 pointer __old_last = this->__end_;
1855 if (__n > static_cast<size_type>(this->__end_ - __p))
1856 {
1857 size_type __cx = __n - (this->__end_ - __p);
1858 __construct_at_end(__cx, __x);
1859 __n -= __cx;
1860 }
1861 if (__n > 0)
1862 {
Eric Fiselier2a649652014-11-14 18:28:36 +00001863 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001864 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001865 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1867 if (__p <= __xr && __xr < this->__end_)
1868 __xr += __old_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001869 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001870 }
1871 }
1872 else
1873 {
1874 allocator_type& __a = this->__alloc();
1875 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1876 __v.__construct_at_end(__n, __x);
1877 __p = __swap_out_circular_buffer(__v, __p);
1878 }
1879 }
1880 return __make_iter(__p);
1881}
1882
1883template <class _Tp, class _Allocator>
1884template <class _InputIterator>
1885typename enable_if
1886<
1887 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant88010252013-03-28 17:44:32 +00001888 !__is_forward_iterator<_InputIterator>::value &&
1889 is_constructible<
1890 _Tp,
1891 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892 typename vector<_Tp, _Allocator>::iterator
1893>::type
1894vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1895{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001896#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001897 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1898 "vector::insert(iterator, range) called with an iterator not"
1899 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001900#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901 difference_type __off = __position - begin();
1902 pointer __p = this->__begin_ + __off;
1903 allocator_type& __a = this->__alloc();
1904 pointer __old_last = this->__end_;
1905 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1906 {
Eric Fiselier489fd502015-07-18 18:22:12 +00001907 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001908 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001909 *__first);
1910 ++this->__end_;
Eric Fiselier489fd502015-07-18 18:22:12 +00001911 __annotator.__done();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001912 }
1913 __split_buffer<value_type, allocator_type&> __v(__a);
1914 if (__first != __last)
1915 {
1916#ifndef _LIBCPP_NO_EXCEPTIONS
1917 try
1918 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001919#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001920 __v.__construct_at_end(__first, __last);
1921 difference_type __old_size = __old_last - this->__begin_;
1922 difference_type __old_p = __p - this->__begin_;
1923 reserve(__recommend(size() + __v.size()));
1924 __p = this->__begin_ + __old_p;
1925 __old_last = this->__begin_ + __old_size;
1926#ifndef _LIBCPP_NO_EXCEPTIONS
1927 }
1928 catch (...)
1929 {
1930 erase(__make_iter(__old_last), end());
1931 throw;
1932 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001933#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001934 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001935 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant9cd22302011-09-16 18:41:29 +00001936 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1937 make_move_iterator(__v.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001938 return begin() + __off;
1939}
1940
1941template <class _Tp, class _Allocator>
1942template <class _ForwardIterator>
1943typename enable_if
1944<
Howard Hinnant88010252013-03-28 17:44:32 +00001945 __is_forward_iterator<_ForwardIterator>::value &&
1946 is_constructible<
1947 _Tp,
1948 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001949 typename vector<_Tp, _Allocator>::iterator
1950>::type
1951vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1952{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001953#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001954 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1955 "vector::insert(iterator, range) called with an iterator not"
1956 " referring to this vector");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001957#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001958 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001959 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001960 if (__n > 0)
1961 {
1962 if (__n <= this->__end_cap() - this->__end_)
1963 {
1964 size_type __old_n = __n;
1965 pointer __old_last = this->__end_;
1966 _ForwardIterator __m = __last;
1967 difference_type __dx = this->__end_ - __p;
1968 if (__n > __dx)
1969 {
1970 __m = __first;
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001971 difference_type __diff = this->__end_ - __p;
1972 _VSTD::advance(__m, __diff);
1973 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001974 __n = __dx;
1975 }
1976 if (__n > 0)
1977 {
Kostya Serebryany4963c252014-09-02 23:43:38 +00001978 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001979 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany4963c252014-09-02 23:43:38 +00001980 __annotator.__done();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001981 _VSTD::copy(__first, __m, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001982 }
1983 }
1984 else
1985 {
1986 allocator_type& __a = this->__alloc();
1987 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1988 __v.__construct_at_end(__first, __last);
1989 __p = __swap_out_circular_buffer(__v, __p);
1990 }
1991 }
1992 return __make_iter(__p);
1993}
1994
1995template <class _Tp, class _Allocator>
1996void
1997vector<_Tp, _Allocator>::resize(size_type __sz)
1998{
1999 size_type __cs = size();
2000 if (__cs < __sz)
2001 this->__append(__sz - __cs);
2002 else if (__cs > __sz)
2003 this->__destruct_at_end(this->__begin_ + __sz);
2004}
2005
2006template <class _Tp, class _Allocator>
2007void
2008vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2009{
2010 size_type __cs = size();
2011 if (__cs < __sz)
2012 this->__append(__sz - __cs, __x);
2013 else if (__cs > __sz)
2014 this->__destruct_at_end(this->__begin_ + __sz);
2015}
2016
2017template <class _Tp, class _Allocator>
2018void
2019vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002020#if _LIBCPP_STD_VER >= 14
2021 _NOEXCEPT
2022#else
2023 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2024 __is_nothrow_swappable<allocator_type>::value)
2025#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002026{
Howard Hinnant27e0e772011-09-14 18:33:51 +00002027 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2028 this->__alloc() == __x.__alloc(),
2029 "vector::swap: Either propagate_on_container_swap must be true"
2030 " or the allocators must compare equal");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002031 _VSTD::swap(this->__begin_, __x.__begin_);
2032 _VSTD::swap(this->__end_, __x.__end_);
2033 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Marshall Clow8982dcd2015-07-13 20:04:56 +00002034 __swap_allocator(this->__alloc(), __x.__alloc(),
2035 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002036#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002037 __get_db()->swap(this, &__x);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002038#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002039}
2040
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002041template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002042bool
2043vector<_Tp, _Allocator>::__invariants() const
2044{
Howard Hinnant76053d72013-06-27 19:35:32 +00002045 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002046 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002047 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002048 return false;
2049 }
2050 else
2051 {
2052 if (this->__begin_ > this->__end_)
2053 return false;
2054 if (this->__begin_ == this->__end_cap())
2055 return false;
2056 if (this->__end_ > this->__end_cap())
2057 return false;
2058 }
2059 return true;
2060}
2061
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002062#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002063
Howard Hinnantc51e1022010-05-11 19:42:16 +00002064template <class _Tp, class _Allocator>
Howard Hinnant27e0e772011-09-14 18:33:51 +00002065bool
2066vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2067{
2068 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2069}
2070
2071template <class _Tp, class _Allocator>
2072bool
2073vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2074{
2075 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2076}
2077
2078template <class _Tp, class _Allocator>
2079bool
2080vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2081{
2082 const_pointer __p = __i->base() + __n;
2083 return this->__begin_ <= __p && __p <= this->__end_;
2084}
2085
2086template <class _Tp, class _Allocator>
2087bool
2088vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2089{
2090 const_pointer __p = __i->base() + __n;
2091 return this->__begin_ <= __p && __p < this->__end_;
2092}
2093
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002094#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002095
2096template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002097inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002098void
2099vector<_Tp, _Allocator>::__invalidate_all_iterators()
2100{
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002101#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00002102 __get_db()->__invalidate_all(this);
Howard Hinnanta47c6d52011-09-16 17:29:17 +00002103#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002104}
2105
2106// vector<bool>
2107
2108template <class _Allocator> class vector<bool, _Allocator>;
2109
2110template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2111
2112template <class _Allocator>
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002113struct __has_storage_type<vector<bool, _Allocator> >
2114{
2115 static const bool value = true;
2116};
2117
2118template <class _Allocator>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002119class _LIBCPP_TYPE_VIS_ONLY vector<bool, _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002120 : private __vector_base_common<true>
2121{
2122public:
2123 typedef vector __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002124 typedef bool value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002125 typedef _Allocator allocator_type;
2126 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002127 typedef typename __alloc_traits::size_type size_type;
2128 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant896b7622012-05-07 16:50:38 +00002129 typedef size_type __storage_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002130 typedef __bit_iterator<vector, false> pointer;
2131 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002132 typedef pointer iterator;
2133 typedef const_pointer const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002134 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2135 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136
2137private:
Marshall Clow940e01c2015-04-07 05:21:38 +00002138 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002139 typedef allocator_traits<__storage_allocator> __storage_traits;
2140 typedef typename __storage_traits::pointer __storage_pointer;
2141 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2142
2143 __storage_pointer __begin_;
2144 size_type __size_;
2145 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002146public:
Howard Hinnantb4ef6482011-07-02 20:33:23 +00002147 typedef __bit_reference<vector> reference;
2148 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant71d1c192011-07-09 15:50:42 +00002149private:
Howard Hinnant1c936782011-06-03 19:40:40 +00002150 _LIBCPP_INLINE_VISIBILITY
2151 size_type& __cap() _NOEXCEPT
2152 {return __cap_alloc_.first();}
2153 _LIBCPP_INLINE_VISIBILITY
2154 const size_type& __cap() const _NOEXCEPT
2155 {return __cap_alloc_.first();}
2156 _LIBCPP_INLINE_VISIBILITY
2157 __storage_allocator& __alloc() _NOEXCEPT
2158 {return __cap_alloc_.second();}
2159 _LIBCPP_INLINE_VISIBILITY
2160 const __storage_allocator& __alloc() const _NOEXCEPT
2161 {return __cap_alloc_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162
2163 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2164
Howard Hinnant1c936782011-06-03 19:40:40 +00002165 _LIBCPP_INLINE_VISIBILITY
2166 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167 {return __n * __bits_per_word;}
Howard Hinnant1c936782011-06-03 19:40:40 +00002168 _LIBCPP_INLINE_VISIBILITY
2169 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170 {return (__n - 1) / __bits_per_word + 1;}
2171
2172public:
Howard Hinnant1c936782011-06-03 19:40:40 +00002173 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe546cbb2015-06-04 02:05:41 +00002174 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow8f282f42015-06-04 00:10:20 +00002175
2176 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2177#if _LIBCPP_STD_VER <= 14
2178 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2179#else
2180 _NOEXCEPT;
2181#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002182 ~vector();
2183 explicit vector(size_type __n);
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002184#if _LIBCPP_STD_VER > 11
2185 explicit vector(size_type __n, const allocator_type& __a);
2186#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187 vector(size_type __n, const value_type& __v);
2188 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2189 template <class _InputIterator>
2190 vector(_InputIterator __first, _InputIterator __last,
2191 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2192 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2193 template <class _InputIterator>
2194 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2195 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2196 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2197 template <class _ForwardIterator>
2198 vector(_ForwardIterator __first, _ForwardIterator __last,
2199 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2200 template <class _ForwardIterator>
2201 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2202 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2203
2204 vector(const vector& __v);
2205 vector(const vector& __v, const allocator_type& __a);
2206 vector& operator=(const vector& __v);
Howard Hinnant33711792011-08-12 21:56:02 +00002207#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208 vector(initializer_list<value_type> __il);
2209 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnant33711792011-08-12 21:56:02 +00002210#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211
Howard Hinnant74279a52010-09-04 23:28:19 +00002212#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1c936782011-06-03 19:40:40 +00002213 _LIBCPP_INLINE_VISIBILITY
2214 vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002215#if _LIBCPP_STD_VER > 14
2216 _NOEXCEPT;
2217#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002218 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowe5108202015-07-14 14:46:32 +00002219#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220 vector(vector&& __v, const allocator_type& __a);
Howard Hinnant1c936782011-06-03 19:40:40 +00002221 _LIBCPP_INLINE_VISIBILITY
2222 vector& operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002223 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnant74279a52010-09-04 23:28:19 +00002224#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant33711792011-08-12 21:56:02 +00002225#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002226 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227 vector& operator=(initializer_list<value_type> __il)
2228 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnant33711792011-08-12 21:56:02 +00002229#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230
2231 template <class _InputIterator>
2232 typename enable_if
2233 <
2234 __is_input_iterator<_InputIterator>::value &&
2235 !__is_forward_iterator<_InputIterator>::value,
2236 void
2237 >::type
2238 assign(_InputIterator __first, _InputIterator __last);
2239 template <class _ForwardIterator>
2240 typename enable_if
2241 <
2242 __is_forward_iterator<_ForwardIterator>::value,
2243 void
2244 >::type
2245 assign(_ForwardIterator __first, _ForwardIterator __last);
2246
2247 void assign(size_type __n, const value_type& __x);
Howard Hinnant33711792011-08-12 21:56:02 +00002248#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002250 void assign(initializer_list<value_type> __il)
2251 {assign(__il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +00002252#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002253
Howard Hinnant1c936782011-06-03 19:40:40 +00002254 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255 {return allocator_type(this->__alloc());}
2256
Howard Hinnant1c936782011-06-03 19:40:40 +00002257 size_type max_size() const _NOEXCEPT;
2258 _LIBCPP_INLINE_VISIBILITY
2259 size_type capacity() const _NOEXCEPT
2260 {return __internal_cap_to_external(__cap());}
2261 _LIBCPP_INLINE_VISIBILITY
2262 size_type size() const _NOEXCEPT
2263 {return __size_;}
2264 _LIBCPP_INLINE_VISIBILITY
2265 bool empty() const _NOEXCEPT
2266 {return __size_ == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002267 void reserve(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002268 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269
Howard Hinnant1c936782011-06-03 19:40:40 +00002270 _LIBCPP_INLINE_VISIBILITY
2271 iterator begin() _NOEXCEPT
2272 {return __make_iter(0);}
2273 _LIBCPP_INLINE_VISIBILITY
2274 const_iterator begin() const _NOEXCEPT
2275 {return __make_iter(0);}
2276 _LIBCPP_INLINE_VISIBILITY
2277 iterator end() _NOEXCEPT
2278 {return __make_iter(__size_);}
2279 _LIBCPP_INLINE_VISIBILITY
2280 const_iterator end() const _NOEXCEPT
2281 {return __make_iter(__size_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282
Howard Hinnant1c936782011-06-03 19:40:40 +00002283 _LIBCPP_INLINE_VISIBILITY
2284 reverse_iterator rbegin() _NOEXCEPT
2285 {return reverse_iterator(end());}
2286 _LIBCPP_INLINE_VISIBILITY
2287 const_reverse_iterator rbegin() const _NOEXCEPT
2288 {return const_reverse_iterator(end());}
2289 _LIBCPP_INLINE_VISIBILITY
2290 reverse_iterator rend() _NOEXCEPT
2291 {return reverse_iterator(begin());}
2292 _LIBCPP_INLINE_VISIBILITY
2293 const_reverse_iterator rend() const _NOEXCEPT
2294 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002295
Howard Hinnant1c936782011-06-03 19:40:40 +00002296 _LIBCPP_INLINE_VISIBILITY
2297 const_iterator cbegin() const _NOEXCEPT
2298 {return __make_iter(0);}
2299 _LIBCPP_INLINE_VISIBILITY
2300 const_iterator cend() const _NOEXCEPT
2301 {return __make_iter(__size_);}
2302 _LIBCPP_INLINE_VISIBILITY
2303 const_reverse_iterator crbegin() const _NOEXCEPT
2304 {return rbegin();}
2305 _LIBCPP_INLINE_VISIBILITY
2306 const_reverse_iterator crend() const _NOEXCEPT
2307 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002308
2309 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2310 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2311 reference at(size_type __n);
2312 const_reference at(size_type __n) const;
2313
2314 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2315 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2316 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2317 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2318
2319 void push_back(const value_type& __x);
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002320#if _LIBCPP_STD_VER > 11
2321 template <class... _Args>
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002322 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args) {
2323 push_back ( value_type ( _VSTD::forward<_Args>(__args)... ));
2324 return this->back();
2325 }
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002326#endif
2327
Howard Hinnantc51e1022010-05-11 19:42:16 +00002328 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2329
Marshall Clowc46bb8e2013-08-13 23:54:12 +00002330#if _LIBCPP_STD_VER > 11
2331 template <class... _Args>
2332 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2333 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2334#endif
2335
Howard Hinnantc51e1022010-05-11 19:42:16 +00002336 iterator insert(const_iterator __position, const value_type& __x);
2337 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2338 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2339 template <class _InputIterator>
2340 typename enable_if
2341 <
2342 __is_input_iterator <_InputIterator>::value &&
2343 !__is_forward_iterator<_InputIterator>::value,
2344 iterator
2345 >::type
2346 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2347 template <class _ForwardIterator>
2348 typename enable_if
2349 <
2350 __is_forward_iterator<_ForwardIterator>::value,
2351 iterator
2352 >::type
2353 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant33711792011-08-12 21:56:02 +00002354#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002356 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2357 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +00002358#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002359
Howard Hinnantcf823322010-12-17 14:46:43 +00002360 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002361 iterator erase(const_iterator __first, const_iterator __last);
2362
Howard Hinnant1c936782011-06-03 19:40:40 +00002363 _LIBCPP_INLINE_VISIBILITY
2364 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002365
Howard Hinnant1c936782011-06-03 19:40:40 +00002366 void swap(vector&)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002367#if _LIBCPP_STD_VER >= 14
2368 _NOEXCEPT;
2369#else
2370 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2371 __is_nothrow_swappable<allocator_type>::value);
2372#endif
Marshall Clow89eb3822016-04-07 14:20:31 +00002373 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002374
2375 void resize(size_type __sz, value_type __x = false);
Howard Hinnant1c936782011-06-03 19:40:40 +00002376 void flip() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002377
2378 bool __invariants() const;
2379
2380private:
Howard Hinnantcf823322010-12-17 14:46:43 +00002381 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002382 void allocate(size_type __n);
Howard Hinnant1c936782011-06-03 19:40:40 +00002383 void deallocate() _NOEXCEPT;
2384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00002385 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow66f34152014-07-28 15:02:42 +00002386 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnantcf823322010-12-17 14:46:43 +00002387 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2388 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002389 template <class _ForwardIterator>
2390 typename enable_if
2391 <
2392 __is_forward_iterator<_ForwardIterator>::value,
2393 void
2394 >::type
2395 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2396 void __append(size_type __n, const_reference __x);
Howard Hinnant1c936782011-06-03 19:40:40 +00002397 _LIBCPP_INLINE_VISIBILITY
2398 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002399 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002400 _LIBCPP_INLINE_VISIBILITY
2401 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002402 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant1c936782011-06-03 19:40:40 +00002403 _LIBCPP_INLINE_VISIBILITY
2404 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002405 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002406 _LIBCPP_INLINE_VISIBILITY
2407 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002408 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnant1c936782011-06-03 19:40:40 +00002409 _LIBCPP_INLINE_VISIBILITY
2410 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00002411 {return begin() + (__p - cbegin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002412
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002414 void __copy_assign_alloc(const vector& __v)
2415 {__copy_assign_alloc(__v, integral_constant<bool,
2416 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002418 void __copy_assign_alloc(const vector& __c, true_type)
2419 {
2420 if (__alloc() != __c.__alloc())
2421 deallocate();
2422 __alloc() = __c.__alloc();
2423 }
2424
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002426 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427 {}
2428
2429 void __move_assign(vector& __c, false_type);
Howard Hinnant1c936782011-06-03 19:40:40 +00002430 void __move_assign(vector& __c, true_type)
2431 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002433 void __move_assign_alloc(vector& __c)
Howard Hinnant1c936782011-06-03 19:40:40 +00002434 _NOEXCEPT_(
2435 !__storage_traits::propagate_on_container_move_assignment::value ||
2436 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002437 {__move_assign_alloc(__c, integral_constant<bool,
2438 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00002440 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002441 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002442 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002443 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002444 }
2445
Howard Hinnant1c265cd2010-09-23 18:58:28 +00002446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002447 void __move_assign_alloc(vector&, false_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002448 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002449 {}
2450
Howard Hinnant1c936782011-06-03 19:40:40 +00002451 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002452
2453 friend class __bit_reference<vector>;
2454 friend class __bit_const_reference<vector>;
2455 friend class __bit_iterator<vector, false>;
2456 friend class __bit_iterator<vector, true>;
Howard Hinnant4210a0f2012-08-17 17:10:18 +00002457 friend struct __bit_array<vector>;
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002458 friend struct _LIBCPP_TYPE_VIS_ONLY hash<vector>;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002459};
2460
2461template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002462inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002463void
2464vector<bool, _Allocator>::__invalidate_all_iterators()
2465{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002466}
2467
2468// Allocate space for __n objects
2469// throws length_error if __n > max_size()
2470// throws (probably bad_alloc) if memory run out
2471// Precondition: __begin_ == __end_ == __cap() == 0
2472// Precondition: __n > 0
2473// Postcondition: capacity() == __n
2474// Postcondition: size() == 0
2475template <class _Allocator>
2476void
2477vector<bool, _Allocator>::allocate(size_type __n)
2478{
2479 if (__n > max_size())
2480 this->__throw_length_error();
2481 __n = __external_cap_to_internal(__n);
2482 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2483 this->__size_ = 0;
2484 this->__cap() = __n;
2485}
2486
2487template <class _Allocator>
2488void
Howard Hinnant1c936782011-06-03 19:40:40 +00002489vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002490{
Howard Hinnant76053d72013-06-27 19:35:32 +00002491 if (this->__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002492 {
2493 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2494 __invalidate_all_iterators();
Howard Hinnant76053d72013-06-27 19:35:32 +00002495 this->__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002496 this->__size_ = this->__cap() = 0;
2497 }
2498}
2499
2500template <class _Allocator>
2501typename vector<bool, _Allocator>::size_type
Howard Hinnant1c936782011-06-03 19:40:40 +00002502vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002503{
2504 size_type __amax = __storage_traits::max_size(__alloc());
2505 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2506 if (__nmax / __bits_per_word <= __amax)
2507 return __nmax;
2508 return __internal_cap_to_external(__amax);
2509}
2510
2511// Precondition: __new_size > capacity()
2512template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002513inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002514typename vector<bool, _Allocator>::size_type
2515vector<bool, _Allocator>::__recommend(size_type __new_size) const
2516{
2517 const size_type __ms = max_size();
2518 if (__new_size > __ms)
2519 this->__throw_length_error();
2520 const size_type __cap = capacity();
2521 if (__cap >= __ms / 2)
2522 return __ms;
Howard Hinnantea382952013-08-14 18:00:20 +00002523 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524}
2525
2526// Default constructs __n objects starting at __end_
2527// Precondition: __n > 0
2528// Precondition: size() + __n <= capacity()
2529// Postcondition: size() == size() + __n
2530template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002531inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002532void
2533vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2534{
2535 size_type __old_size = this->__size_;
2536 this->__size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002537 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002538}
2539
2540template <class _Allocator>
2541template <class _ForwardIterator>
2542typename enable_if
2543<
2544 __is_forward_iterator<_ForwardIterator>::value,
2545 void
2546>::type
2547vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2548{
2549 size_type __old_size = this->__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002550 this->__size_ += _VSTD::distance(__first, __last);
2551 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002552}
2553
2554template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002555inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002556vector<bool, _Allocator>::vector()
Marshall Clowe546cbb2015-06-04 02:05:41 +00002557 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant76053d72013-06-27 19:35:32 +00002558 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002559 __size_(0),
2560 __cap_alloc_(0)
2561{
2562}
2563
2564template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002565inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002566vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow8f282f42015-06-04 00:10:20 +00002567#if _LIBCPP_STD_VER <= 14
2568 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2569#else
2570 _NOEXCEPT
2571#endif
Howard Hinnant76053d72013-06-27 19:35:32 +00002572 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002573 __size_(0),
2574 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2575{
2576}
2577
2578template <class _Allocator>
2579vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant76053d72013-06-27 19:35:32 +00002580 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002581 __size_(0),
2582 __cap_alloc_(0)
2583{
2584 if (__n > 0)
2585 {
2586 allocate(__n);
2587 __construct_at_end(__n, false);
2588 }
2589}
2590
Marshall Clowd3cbeaa2013-09-14 00:47:59 +00002591#if _LIBCPP_STD_VER > 11
2592template <class _Allocator>
2593vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2594 : __begin_(nullptr),
2595 __size_(0),
2596 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2597{
2598 if (__n > 0)
2599 {
2600 allocate(__n);
2601 __construct_at_end(__n, false);
2602 }
2603}
2604#endif
2605
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606template <class _Allocator>
2607vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant76053d72013-06-27 19:35:32 +00002608 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609 __size_(0),
2610 __cap_alloc_(0)
2611{
2612 if (__n > 0)
2613 {
2614 allocate(__n);
2615 __construct_at_end(__n, __x);
2616 }
2617}
2618
2619template <class _Allocator>
2620vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002621 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002622 __size_(0),
2623 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2624{
2625 if (__n > 0)
2626 {
2627 allocate(__n);
2628 __construct_at_end(__n, __x);
2629 }
2630}
2631
2632template <class _Allocator>
2633template <class _InputIterator>
2634vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2635 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2636 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002637 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002638 __size_(0),
2639 __cap_alloc_(0)
2640{
2641#ifndef _LIBCPP_NO_EXCEPTIONS
2642 try
2643 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002644#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002645 for (; __first != __last; ++__first)
2646 push_back(*__first);
2647#ifndef _LIBCPP_NO_EXCEPTIONS
2648 }
2649 catch (...)
2650 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002651 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002652 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2653 __invalidate_all_iterators();
2654 throw;
2655 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002656#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002657}
2658
2659template <class _Allocator>
2660template <class _InputIterator>
2661vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2662 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2663 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002664 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002665 __size_(0),
2666 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2667{
2668#ifndef _LIBCPP_NO_EXCEPTIONS
2669 try
2670 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002671#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002672 for (; __first != __last; ++__first)
2673 push_back(*__first);
2674#ifndef _LIBCPP_NO_EXCEPTIONS
2675 }
2676 catch (...)
2677 {
Howard Hinnant76053d72013-06-27 19:35:32 +00002678 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002679 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2680 __invalidate_all_iterators();
2681 throw;
2682 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002683#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002684}
2685
2686template <class _Allocator>
2687template <class _ForwardIterator>
2688vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2689 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002690 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691 __size_(0),
2692 __cap_alloc_(0)
2693{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002694 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695 if (__n > 0)
2696 {
2697 allocate(__n);
2698 __construct_at_end(__first, __last);
2699 }
2700}
2701
2702template <class _Allocator>
2703template <class _ForwardIterator>
2704vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2705 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant76053d72013-06-27 19:35:32 +00002706 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002707 __size_(0),
2708 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2709{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002710 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002711 if (__n > 0)
2712 {
2713 allocate(__n);
2714 __construct_at_end(__first, __last);
2715 }
2716}
2717
Howard Hinnant33711792011-08-12 21:56:02 +00002718#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2719
Howard Hinnantc51e1022010-05-11 19:42:16 +00002720template <class _Allocator>
2721vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant76053d72013-06-27 19:35:32 +00002722 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002723 __size_(0),
2724 __cap_alloc_(0)
2725{
2726 size_type __n = static_cast<size_type>(__il.size());
2727 if (__n > 0)
2728 {
2729 allocate(__n);
2730 __construct_at_end(__il.begin(), __il.end());
2731 }
2732}
2733
2734template <class _Allocator>
2735vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002736 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002737 __size_(0),
2738 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2739{
2740 size_type __n = static_cast<size_type>(__il.size());
2741 if (__n > 0)
2742 {
2743 allocate(__n);
2744 __construct_at_end(__il.begin(), __il.end());
2745 }
2746}
2747
Howard Hinnant33711792011-08-12 21:56:02 +00002748#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2749
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002751vector<bool, _Allocator>::~vector()
2752{
Howard Hinnant76053d72013-06-27 19:35:32 +00002753 if (__begin_ != nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002755 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756}
2757
2758template <class _Allocator>
2759vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant76053d72013-06-27 19:35:32 +00002760 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002761 __size_(0),
2762 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2763{
2764 if (__v.size() > 0)
2765 {
2766 allocate(__v.size());
2767 __construct_at_end(__v.begin(), __v.end());
2768 }
2769}
2770
2771template <class _Allocator>
2772vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002773 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002774 __size_(0),
2775 __cap_alloc_(0, __a)
2776{
2777 if (__v.size() > 0)
2778 {
2779 allocate(__v.size());
2780 __construct_at_end(__v.begin(), __v.end());
2781 }
2782}
2783
2784template <class _Allocator>
2785vector<bool, _Allocator>&
2786vector<bool, _Allocator>::operator=(const vector& __v)
2787{
2788 if (this != &__v)
2789 {
2790 __copy_assign_alloc(__v);
2791 if (__v.__size_)
2792 {
2793 if (__v.__size_ > capacity())
2794 {
2795 deallocate();
2796 allocate(__v.__size_);
2797 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002798 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002799 }
2800 __size_ = __v.__size_;
2801 }
2802 return *this;
2803}
2804
Howard Hinnant74279a52010-09-04 23:28:19 +00002805#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2806
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002808inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clowe5108202015-07-14 14:46:32 +00002810#if _LIBCPP_STD_VER > 14
2811 _NOEXCEPT
2812#else
Howard Hinnant1c936782011-06-03 19:40:40 +00002813 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowe5108202015-07-14 14:46:32 +00002814#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002815 : __begin_(__v.__begin_),
2816 __size_(__v.__size_),
2817 __cap_alloc_(__v.__cap_alloc_)
2818{
Howard Hinnant76053d72013-06-27 19:35:32 +00002819 __v.__begin_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002820 __v.__size_ = 0;
2821 __v.__cap() = 0;
2822}
2823
2824template <class _Allocator>
2825vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant76053d72013-06-27 19:35:32 +00002826 : __begin_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827 __size_(0),
2828 __cap_alloc_(0, __a)
2829{
2830 if (__a == allocator_type(__v.__alloc()))
2831 {
2832 this->__begin_ = __v.__begin_;
2833 this->__size_ = __v.__size_;
2834 this->__cap() = __v.__cap();
2835 __v.__begin_ = nullptr;
2836 __v.__cap() = __v.__size_ = 0;
2837 }
2838 else if (__v.size() > 0)
2839 {
2840 allocate(__v.size());
2841 __construct_at_end(__v.begin(), __v.end());
2842 }
2843}
2844
2845template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002846inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002847vector<bool, _Allocator>&
2848vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002849 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002850{
2851 __move_assign(__v, integral_constant<bool,
2852 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002853 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854}
2855
2856template <class _Allocator>
2857void
2858vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2859{
2860 if (__alloc() != __c.__alloc())
2861 assign(__c.begin(), __c.end());
2862 else
2863 __move_assign(__c, true_type());
2864}
2865
2866template <class _Allocator>
2867void
2868vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnant1c936782011-06-03 19:40:40 +00002869 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002870{
2871 deallocate();
Marshall Clowb4871fa2014-07-21 15:15:15 +00002872 __move_assign_alloc(__c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873 this->__begin_ = __c.__begin_;
2874 this->__size_ = __c.__size_;
2875 this->__cap() = __c.__cap();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876 __c.__begin_ = nullptr;
2877 __c.__cap() = __c.__size_ = 0;
2878}
Howard Hinnant74279a52010-09-04 23:28:19 +00002879
2880#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881
2882template <class _Allocator>
2883void
2884vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2885{
2886 __size_ = 0;
2887 if (__n > 0)
2888 {
2889 size_type __c = capacity();
2890 if (__n <= __c)
2891 __size_ = __n;
2892 else
2893 {
2894 vector __v(__alloc());
2895 __v.reserve(__recommend(__n));
2896 __v.__size_ = __n;
2897 swap(__v);
2898 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002899 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002900 }
2901}
2902
2903template <class _Allocator>
2904template <class _InputIterator>
2905typename enable_if
2906<
2907 __is_input_iterator<_InputIterator>::value &&
2908 !__is_forward_iterator<_InputIterator>::value,
2909 void
2910>::type
2911vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2912{
2913 clear();
2914 for (; __first != __last; ++__first)
2915 push_back(*__first);
2916}
2917
2918template <class _Allocator>
2919template <class _ForwardIterator>
2920typename enable_if
2921<
2922 __is_forward_iterator<_ForwardIterator>::value,
2923 void
2924>::type
2925vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2926{
2927 clear();
Eric Fiselier6003c772016-12-23 23:37:52 +00002928 difference_type __ns = _VSTD::distance(__first, __last);
2929 _LIBCPP_ASSERT(__ns >= 0, "invalid range specified");
2930 const size_t __n = static_cast<size_type>(__ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002931 if (__n)
2932 {
2933 if (__n > capacity())
2934 {
2935 deallocate();
2936 allocate(__n);
2937 }
2938 __construct_at_end(__first, __last);
2939 }
2940}
2941
2942template <class _Allocator>
2943void
2944vector<bool, _Allocator>::reserve(size_type __n)
2945{
2946 if (__n > capacity())
2947 {
2948 vector __v(this->__alloc());
2949 __v.allocate(__n);
2950 __v.__construct_at_end(this->begin(), this->end());
2951 swap(__v);
2952 __invalidate_all_iterators();
2953 }
2954}
2955
2956template <class _Allocator>
2957void
Howard Hinnant1c936782011-06-03 19:40:40 +00002958vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002959{
2960 if (__external_cap_to_internal(size()) > __cap())
2961 {
2962#ifndef _LIBCPP_NO_EXCEPTIONS
2963 try
2964 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002965#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966 vector(*this, allocator_type(__alloc())).swap(*this);
2967#ifndef _LIBCPP_NO_EXCEPTIONS
2968 }
2969 catch (...)
2970 {
2971 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002972#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002973 }
2974}
2975
2976template <class _Allocator>
2977typename vector<bool, _Allocator>::reference
2978vector<bool, _Allocator>::at(size_type __n)
2979{
2980 if (__n >= size())
2981 this->__throw_out_of_range();
2982 return (*this)[__n];
2983}
2984
2985template <class _Allocator>
2986typename vector<bool, _Allocator>::const_reference
2987vector<bool, _Allocator>::at(size_type __n) const
2988{
2989 if (__n >= size())
2990 this->__throw_out_of_range();
2991 return (*this)[__n];
2992}
2993
2994template <class _Allocator>
2995void
2996vector<bool, _Allocator>::push_back(const value_type& __x)
2997{
2998 if (this->__size_ == this->capacity())
2999 reserve(__recommend(this->__size_ + 1));
3000 ++this->__size_;
3001 back() = __x;
3002}
3003
3004template <class _Allocator>
3005typename vector<bool, _Allocator>::iterator
3006vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3007{
3008 iterator __r;
3009 if (size() < capacity())
3010 {
3011 const_iterator __old_end = end();
3012 ++__size_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003013 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003014 __r = __const_iterator_cast(__position);
3015 }
3016 else
3017 {
3018 vector __v(__alloc());
3019 __v.reserve(__recommend(__size_ + 1));
3020 __v.__size_ = __size_ + 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003021 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3022 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003023 swap(__v);
3024 }
3025 *__r = __x;
3026 return __r;
3027}
3028
3029template <class _Allocator>
3030typename vector<bool, _Allocator>::iterator
3031vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3032{
3033 iterator __r;
3034 size_type __c = capacity();
3035 if (__n <= __c && size() <= __c - __n)
3036 {
3037 const_iterator __old_end = end();
3038 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003039 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003040 __r = __const_iterator_cast(__position);
3041 }
3042 else
3043 {
3044 vector __v(__alloc());
3045 __v.reserve(__recommend(__size_ + __n));
3046 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003047 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3048 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003049 swap(__v);
3050 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003051 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003052 return __r;
3053}
3054
3055template <class _Allocator>
3056template <class _InputIterator>
3057typename enable_if
3058<
3059 __is_input_iterator <_InputIterator>::value &&
3060 !__is_forward_iterator<_InputIterator>::value,
3061 typename vector<bool, _Allocator>::iterator
3062>::type
3063vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3064{
3065 difference_type __off = __position - begin();
3066 iterator __p = __const_iterator_cast(__position);
3067 iterator __old_end = end();
3068 for (; size() != capacity() && __first != __last; ++__first)
3069 {
3070 ++this->__size_;
3071 back() = *__first;
3072 }
3073 vector __v(__alloc());
3074 if (__first != __last)
3075 {
3076#ifndef _LIBCPP_NO_EXCEPTIONS
3077 try
3078 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003079#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003080 __v.assign(__first, __last);
3081 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3082 difference_type __old_p = __p - begin();
3083 reserve(__recommend(size() + __v.size()));
3084 __p = begin() + __old_p;
3085 __old_end = begin() + __old_size;
3086#ifndef _LIBCPP_NO_EXCEPTIONS
3087 }
3088 catch (...)
3089 {
3090 erase(__old_end, end());
3091 throw;
3092 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003093#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003094 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003095 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003096 insert(__p, __v.begin(), __v.end());
3097 return begin() + __off;
3098}
3099
3100template <class _Allocator>
3101template <class _ForwardIterator>
3102typename enable_if
3103<
3104 __is_forward_iterator<_ForwardIterator>::value,
3105 typename vector<bool, _Allocator>::iterator
3106>::type
3107vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3108{
Eric Fiselier654dd332016-12-11 05:31:00 +00003109 const difference_type __n_signed = _VSTD::distance(__first, __last);
3110 _LIBCPP_ASSERT(__n_signed >= 0, "invalid range specified");
3111 const size_type __n = static_cast<size_type>(__n_signed);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003112 iterator __r;
3113 size_type __c = capacity();
3114 if (__n <= __c && size() <= __c - __n)
3115 {
3116 const_iterator __old_end = end();
3117 __size_ += __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003118 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003119 __r = __const_iterator_cast(__position);
3120 }
3121 else
3122 {
3123 vector __v(__alloc());
3124 __v.reserve(__recommend(__size_ + __n));
3125 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003126 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3127 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128 swap(__v);
3129 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003130 _VSTD::copy(__first, __last, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003131 return __r;
3132}
3133
3134template <class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003135inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003136typename vector<bool, _Allocator>::iterator
3137vector<bool, _Allocator>::erase(const_iterator __position)
3138{
3139 iterator __r = __const_iterator_cast(__position);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003140 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003141 --__size_;
3142 return __r;
3143}
3144
3145template <class _Allocator>
3146typename vector<bool, _Allocator>::iterator
3147vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3148{
3149 iterator __r = __const_iterator_cast(__first);
3150 difference_type __d = __last - __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003151 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003152 __size_ -= __d;
3153 return __r;
3154}
3155
3156template <class _Allocator>
3157void
3158vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003159#if _LIBCPP_STD_VER >= 14
3160 _NOEXCEPT
3161#else
3162 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3163 __is_nothrow_swappable<allocator_type>::value)
3164#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003166 _VSTD::swap(this->__begin_, __x.__begin_);
3167 _VSTD::swap(this->__size_, __x.__size_);
3168 _VSTD::swap(this->__cap(), __x.__cap());
Marshall Clow8982dcd2015-07-13 20:04:56 +00003169 __swap_allocator(this->__alloc(), __x.__alloc(),
3170 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171}
3172
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003173template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003174void
3175vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3176{
3177 size_type __cs = size();
3178 if (__cs < __sz)
3179 {
3180 iterator __r;
3181 size_type __c = capacity();
3182 size_type __n = __sz - __cs;
3183 if (__n <= __c && __cs <= __c - __n)
3184 {
3185 __r = end();
3186 __size_ += __n;
3187 }
3188 else
3189 {
3190 vector __v(__alloc());
3191 __v.reserve(__recommend(__size_ + __n));
3192 __v.__size_ = __size_ + __n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003193 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003194 swap(__v);
3195 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003196 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003197 }
3198 else
3199 __size_ = __sz;
3200}
3201
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003202template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003203void
Howard Hinnant1c936782011-06-03 19:40:40 +00003204vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003205{
3206 // do middle whole words
3207 size_type __n = __size_;
3208 __storage_pointer __p = __begin_;
3209 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3210 *__p = ~*__p;
3211 // do last partial word
3212 if (__n > 0)
3213 {
3214 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3215 __storage_type __b = *__p & __m;
3216 *__p &= ~__m;
3217 *__p |= ~__b & __m;
3218 }
3219}
3220
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003221template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003222bool
3223vector<bool, _Allocator>::__invariants() const
3224{
Howard Hinnant76053d72013-06-27 19:35:32 +00003225 if (this->__begin_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003226 {
3227 if (this->__size_ != 0 || this->__cap() != 0)
3228 return false;
3229 }
3230 else
3231 {
3232 if (this->__cap() == 0)
3233 return false;
3234 if (this->__size_ > this->capacity())
3235 return false;
3236 }
3237 return true;
3238}
3239
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003240template <class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003241size_t
Howard Hinnant1c936782011-06-03 19:40:40 +00003242vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003243{
3244 size_t __h = 0;
3245 // do middle whole words
3246 size_type __n = __size_;
3247 __storage_pointer __p = __begin_;
3248 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3249 __h ^= *__p;
3250 // do last partial word
3251 if (__n > 0)
3252 {
3253 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3254 __h ^= *__p & __m;
3255 }
3256 return __h;
3257}
3258
3259template <class _Allocator>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003260struct _LIBCPP_TYPE_VIS_ONLY hash<vector<bool, _Allocator> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003261 : public unary_function<vector<bool, _Allocator>, size_t>
3262{
Howard Hinnant1c265cd2010-09-23 18:58:28 +00003263 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1c936782011-06-03 19:40:40 +00003264 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003265 {return __vec.__hash_code();}
3266};
3267
3268template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003269inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003270bool
3271operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3272{
3273 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003274 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003275}
3276
3277template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003278inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003279bool
3280operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3281{
3282 return !(__x == __y);
3283}
3284
3285template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003286inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003287bool
3288operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3289{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003290 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003291}
3292
3293template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003294inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003295bool
3296operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3297{
3298 return __y < __x;
3299}
3300
3301template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003302inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003303bool
3304operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3305{
3306 return !(__x < __y);
3307}
3308
3309template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003310inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003311bool
3312operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3313{
3314 return !(__y < __x);
3315}
3316
3317template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003318inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003319void
3320swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnant1c936782011-06-03 19:40:40 +00003321 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003322{
3323 __x.swap(__y);
3324}
3325
3326_LIBCPP_END_NAMESPACE_STD
3327
3328#endif // _LIBCPP_VECTOR