blob: 0e413350de765fffbf2257e98384bcfe671ad8f5 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------------ vector --------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_VECTOR
12#define _LIBCPP_VECTOR
13
14/*
15 vector synopsis
16
17namespace std
18{
19
Howard Hinnant324bb032010-08-22 00:02:43 +000020template <class T, class Allocator = allocator<T> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021class vector
Howard Hinnant324bb032010-08-22 00:02:43 +000022{
23public:
24 typedef T value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000025 typedef Allocator allocator_type;
26 typedef typename allocator_type::reference reference;
27 typedef typename allocator_type::const_reference const_reference;
28 typedef implementation-defined iterator;
29 typedef implementation-defined const_iterator;
30 typedef typename allocator_type::size_type size_type;
31 typedef typename allocator_type::difference_type difference_type;
32 typedef typename allocator_type::pointer pointer;
33 typedef typename allocator_type::const_pointer const_pointer;
34 typedef std::reverse_iterator<iterator> reverse_iterator;
35 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
36
Howard Hinnantd1d27a42011-06-03 19:40:40 +000037 vector()
38 noexcept(is_nothrow_default_constructible<allocator_type>::value);
39 explicit vector(const allocator_type&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000040 explicit vector(size_type n);
Marshall Clowa49a2c92013-09-14 00:47:59 +000041 explicit vector(size_type n, const allocator_type&); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
43 template <class InputIterator>
44 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
45 vector(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +000046 vector(vector&& x)
47 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000048 vector(initializer_list<value_type> il);
49 vector(initializer_list<value_type> il, const allocator_type& a);
50 ~vector();
51 vector& operator=(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +000052 vector& operator=(vector&& x)
53 noexcept(
Marshall Clowaf961ed2015-08-18 18:57:00 +000054 allocator_type::propagate_on_container_move_assignment::value ||
55 allocator_type::is_always_equal::value); // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056 vector& operator=(initializer_list<value_type> il);
57 template <class InputIterator>
58 void assign(InputIterator first, InputIterator last);
59 void assign(size_type n, const value_type& u);
60 void assign(initializer_list<value_type> il);
61
Howard Hinnantd1d27a42011-06-03 19:40:40 +000062 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063
Howard Hinnantd1d27a42011-06-03 19:40:40 +000064 iterator begin() noexcept;
65 const_iterator begin() const noexcept;
66 iterator end() noexcept;
67 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000068
Howard Hinnantd1d27a42011-06-03 19:40:40 +000069 reverse_iterator rbegin() noexcept;
70 const_reverse_iterator rbegin() const noexcept;
71 reverse_iterator rend() noexcept;
72 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000073
Howard Hinnantd1d27a42011-06-03 19:40:40 +000074 const_iterator cbegin() const noexcept;
75 const_iterator cend() const noexcept;
76 const_reverse_iterator crbegin() const noexcept;
77 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000078
Howard Hinnantd1d27a42011-06-03 19:40:40 +000079 size_type size() const noexcept;
80 size_type max_size() const noexcept;
81 size_type capacity() const noexcept;
82 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +000084 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000085
86 reference operator[](size_type n);
87 const_reference operator[](size_type n) const;
88 reference at(size_type n);
89 const_reference at(size_type n) const;
90
91 reference front();
92 const_reference front() const;
93 reference back();
94 const_reference back() const;
95
Howard Hinnantd1d27a42011-06-03 19:40:40 +000096 value_type* data() noexcept;
97 const value_type* data() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098
99 void push_back(const value_type& x);
100 void push_back(value_type&& x);
101 template <class... Args>
102 void emplace_back(Args&&... args);
103 void pop_back();
104
105 template <class... Args> iterator emplace(const_iterator position, Args&&... args);
106 iterator insert(const_iterator position, const value_type& x);
107 iterator insert(const_iterator position, value_type&& x);
108 iterator insert(const_iterator position, size_type n, const value_type& x);
109 template <class InputIterator>
110 iterator insert(const_iterator position, InputIterator first, InputIterator last);
111 iterator insert(const_iterator position, initializer_list<value_type> il);
112
113 iterator erase(const_iterator position);
114 iterator erase(const_iterator first, const_iterator last);
115
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000116 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000117
118 void resize(size_type sz);
119 void resize(size_type sz, const value_type& c);
120
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000121 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +0000122 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
123 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000124
125 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000126};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000127
Howard Hinnant324bb032010-08-22 00:02:43 +0000128template <class Allocator = allocator<T> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129class vector<bool, Allocator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000130{
131public:
132 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000133 typedef Allocator allocator_type;
134 typedef implementation-defined iterator;
135 typedef implementation-defined const_iterator;
136 typedef typename allocator_type::size_type size_type;
137 typedef typename allocator_type::difference_type difference_type;
138 typedef iterator pointer;
139 typedef const_iterator const_pointer;
140 typedef std::reverse_iterator<iterator> reverse_iterator;
141 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
142
143 class reference
144 {
145 public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000146 reference(const reference&) noexcept;
147 operator bool() const noexcept;
148 reference& operator=(const bool x) noexcept;
149 reference& operator=(const reference& x) noexcept;
150 iterator operator&() const noexcept;
151 void flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152 };
153
154 class const_reference
155 {
156 public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000157 const_reference(const reference&) noexcept;
158 operator bool() const noexcept;
159 const_iterator operator&() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160 };
161
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000162 vector()
163 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant9cbee432011-09-02 20:42:31 +0000164 explicit vector(const allocator_type&);
Marshall Clowa49a2c92013-09-14 00:47:59 +0000165 explicit vector(size_type n, const allocator_type& a = allocator_type()); // C++14
166 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000167 template <class InputIterator>
168 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
169 vector(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000170 vector(vector&& x)
171 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000172 vector(initializer_list<value_type> il);
173 vector(initializer_list<value_type> il, const allocator_type& a);
174 ~vector();
175 vector& operator=(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000176 vector& operator=(vector&& x)
177 noexcept(
Marshall Clowaf961ed2015-08-18 18:57:00 +0000178 allocator_type::propagate_on_container_move_assignment::value ||
179 allocator_type::is_always_equal::value); // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000180 vector& operator=(initializer_list<value_type> il);
181 template <class InputIterator>
182 void assign(InputIterator first, InputIterator last);
183 void assign(size_type n, const value_type& u);
184 void assign(initializer_list<value_type> il);
185
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000186 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000188 iterator begin() noexcept;
189 const_iterator begin() const noexcept;
190 iterator end() noexcept;
191 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000192
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000193 reverse_iterator rbegin() noexcept;
194 const_reverse_iterator rbegin() const noexcept;
195 reverse_iterator rend() noexcept;
196 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000197
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000198 const_iterator cbegin() const noexcept;
199 const_iterator cend() const noexcept;
200 const_reverse_iterator crbegin() const noexcept;
201 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000203 size_type size() const noexcept;
204 size_type max_size() const noexcept;
205 size_type capacity() const noexcept;
206 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000207 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000208 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000209
210 reference operator[](size_type n);
211 const_reference operator[](size_type n) const;
212 reference at(size_type n);
213 const_reference at(size_type n) const;
214
215 reference front();
216 const_reference front() const;
217 reference back();
218 const_reference back() const;
219
220 void push_back(const value_type& x);
Marshall Clow198a2a52013-08-13 23:54:12 +0000221 template <class... Args> void emplace_back(Args&&... args); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000222 void pop_back();
223
Marshall Clow198a2a52013-08-13 23:54:12 +0000224 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000225 iterator insert(const_iterator position, const value_type& x);
226 iterator insert(const_iterator position, size_type n, const value_type& x);
227 template <class InputIterator>
228 iterator insert(const_iterator position, InputIterator first, InputIterator last);
229 iterator insert(const_iterator position, initializer_list<value_type> il);
230
231 iterator erase(const_iterator position);
232 iterator erase(const_iterator first, const_iterator last);
233
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000234 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235
236 void resize(size_type sz);
237 void resize(size_type sz, value_type x);
238
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000239 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +0000240 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
241 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000242 void flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243
244 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000245};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000246
247template <class Allocator> struct hash<std::vector<bool, Allocator>>;
248
249template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
250template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
251template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
252template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
253template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
254template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
255
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000256template <class T, class Allocator>
257void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
258 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000259
260} // std
261
262*/
263
264#include <__config>
Eric Fiselierb3792282016-02-20 00:19:45 +0000265#include <iosfwd> // for forward declaration of vector
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266#include <__bit_reference>
267#include <type_traits>
268#include <climits>
269#include <limits>
270#include <initializer_list>
271#include <memory>
272#include <stdexcept>
273#include <algorithm>
274#include <cstring>
275#include <__split_buffer>
276#include <__functional_base>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000277
Howard Hinnant66c6f972011-11-29 16:45:27 +0000278#include <__undef_min_max>
279
Eric Fiselierb9536102014-08-10 23:53:08 +0000280#include <__debug>
Howard Hinnant8b00e6c2013-08-02 00:26:35 +0000281
Howard Hinnant08e17472011-10-17 20:05:10 +0000282#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000283#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000284#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000285
286_LIBCPP_BEGIN_NAMESPACE_STD
287
288template <bool>
289class __vector_base_common
290{
291protected:
292 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
293 void __throw_length_error() const;
294 void __throw_out_of_range() const;
295};
296
297template <bool __b>
298void
299__vector_base_common<__b>::__throw_length_error() const
300{
301#ifndef _LIBCPP_NO_EXCEPTIONS
302 throw length_error("vector");
303#else
304 assert(!"vector length_error");
305#endif
306}
307
308template <bool __b>
309void
310__vector_base_common<__b>::__throw_out_of_range() const
311{
312#ifndef _LIBCPP_NO_EXCEPTIONS
313 throw out_of_range("vector");
314#else
315 assert(!"vector out_of_range");
316#endif
317}
318
Howard Hinnante9df0a52013-08-01 18:17:34 +0000319#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000320#pragma warning( push )
321#pragma warning( disable: 4231 )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000322#endif // _LIBCPP_MSVC
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000323_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS __vector_base_common<true>)
Howard Hinnante9df0a52013-08-01 18:17:34 +0000324#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000325#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000326#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000327
328template <class _Tp, class _Allocator>
329class __vector_base
330 : protected __vector_base_common<true>
331{
332protected:
Howard Hinnant324bb032010-08-22 00:02:43 +0000333 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000334 typedef _Allocator allocator_type;
335 typedef allocator_traits<allocator_type> __alloc_traits;
336 typedef value_type& reference;
337 typedef const value_type& const_reference;
338 typedef typename __alloc_traits::size_type size_type;
339 typedef typename __alloc_traits::difference_type difference_type;
340 typedef typename __alloc_traits::pointer pointer;
341 typedef typename __alloc_traits::const_pointer const_pointer;
342 typedef pointer iterator;
343 typedef const_pointer const_iterator;
344
345 pointer __begin_;
346 pointer __end_;
347 __compressed_pair<pointer, allocator_type> __end_cap_;
348
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000349 _LIBCPP_INLINE_VISIBILITY
350 allocator_type& __alloc() _NOEXCEPT
351 {return __end_cap_.second();}
352 _LIBCPP_INLINE_VISIBILITY
353 const allocator_type& __alloc() const _NOEXCEPT
354 {return __end_cap_.second();}
355 _LIBCPP_INLINE_VISIBILITY
356 pointer& __end_cap() _NOEXCEPT
357 {return __end_cap_.first();}
358 _LIBCPP_INLINE_VISIBILITY
359 const pointer& __end_cap() const _NOEXCEPT
360 {return __end_cap_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000361
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000362 _LIBCPP_INLINE_VISIBILITY
363 __vector_base()
364 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000365 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000366 ~__vector_base();
367
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000368 _LIBCPP_INLINE_VISIBILITY
369 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
370 _LIBCPP_INLINE_VISIBILITY
371 size_type capacity() const _NOEXCEPT
372 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000373
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000375 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000376
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000378 void __copy_assign_alloc(const __vector_base& __c)
379 {__copy_assign_alloc(__c, integral_constant<bool,
380 __alloc_traits::propagate_on_container_copy_assignment::value>());}
381
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000383 void __move_assign_alloc(__vector_base& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000384 _NOEXCEPT_(
385 !__alloc_traits::propagate_on_container_move_assignment::value ||
386 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387 {__move_assign_alloc(__c, integral_constant<bool,
388 __alloc_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000389private:
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000391 void __copy_assign_alloc(const __vector_base& __c, true_type)
392 {
393 if (__alloc() != __c.__alloc())
394 {
395 clear();
396 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
397 __begin_ = __end_ = __end_cap() = nullptr;
398 }
399 __alloc() = __c.__alloc();
400 }
401
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000402 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000403 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000404 {}
405
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000407 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000408 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000410 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000411 }
412
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000414 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000415 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000416 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000417};
418
419template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000420inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000421void
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000422__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000423{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000424 while (__new_last != __end_)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000425 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000426}
427
428template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000429inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000431 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000432 : __begin_(nullptr),
433 __end_(nullptr),
434 __end_cap_(nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000435{
436}
437
438template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000439inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000440__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000441 : __begin_(nullptr),
442 __end_(nullptr),
443 __end_cap_(nullptr, __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000444{
445}
446
447template <class _Tp, class _Allocator>
448__vector_base<_Tp, _Allocator>::~__vector_base()
449{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000450 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000451 {
452 clear();
453 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
454 }
455}
456
Eric Fiselierb3792282016-02-20 00:19:45 +0000457template <class _Tp, class _Allocator /* = allocator<_Tp> */>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000458class _LIBCPP_TYPE_VIS_ONLY vector
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000459 : private __vector_base<_Tp, _Allocator>
460{
461private:
462 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000463 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant324bb032010-08-22 00:02:43 +0000464public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +0000466 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000467 typedef _Allocator allocator_type;
468 typedef typename __base::__alloc_traits __alloc_traits;
469 typedef typename __base::reference reference;
470 typedef typename __base::const_reference const_reference;
471 typedef typename __base::size_type size_type;
472 typedef typename __base::difference_type difference_type;
473 typedef typename __base::pointer pointer;
474 typedef typename __base::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000475 typedef __wrap_iter<pointer> iterator;
476 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000477 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
478 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000479
Howard Hinnant02d5e182013-03-26 19:04:56 +0000480 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
481 "Allocator::value_type must be same type as value_type");
482
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000483 _LIBCPP_INLINE_VISIBILITY
Marshall Clowc912c0c2015-06-04 02:05:41 +0000484 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant7a563db2011-09-14 18:33:51 +0000485 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000486#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000487 __get_db()->__insert_c(this);
488#endif
489 }
490 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
Marshall Clow127db912015-06-04 00:10:20 +0000491#if _LIBCPP_STD_VER <= 14
492 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
493#else
494 _NOEXCEPT
495#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +0000496 : __base(__a)
497 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000498#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000499 __get_db()->__insert_c(this);
500#endif
501 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000502 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59 +0000503#if _LIBCPP_STD_VER > 11
504 explicit vector(size_type __n, const allocator_type& __a);
505#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000506 vector(size_type __n, const_reference __x);
507 vector(size_type __n, const_reference __x, const allocator_type& __a);
508 template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +0000509 vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000510 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000511 !__is_forward_iterator<_InputIterator>::value &&
512 is_constructible<
513 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +0000514 typename iterator_traits<_InputIterator>::reference>::value,
515 _InputIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000516 template <class _InputIterator>
517 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
518 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000519 !__is_forward_iterator<_InputIterator>::value &&
520 is_constructible<
521 value_type,
522 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000523 template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +0000524 vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000525 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
526 is_constructible<
527 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +0000528 typename iterator_traits<_ForwardIterator>::reference>::value,
529 _ForwardIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000530 template <class _ForwardIterator>
531 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +0000532 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
533 is_constructible<
534 value_type,
535 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000536#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000538 vector(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000540 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000541#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantabe26282011-09-16 17:29:17 +0000542#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7a563db2011-09-14 18:33:51 +0000544 ~vector()
545 {
546 __get_db()->__erase_c(this);
547 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000548#endif
549
550 vector(const vector& __x);
551 vector(const vector& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000553 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000554#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000556 vector(vector&& __x)
Marshall Clow119ed472015-07-14 14:46:32 +0000557#if _LIBCPP_STD_VER > 14
558 _NOEXCEPT;
559#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000560 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow119ed472015-07-14 14:46:32 +0000561#endif
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000563 vector(vector&& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000565 vector& operator=(vector&& __x)
Marshall Clowaf961ed2015-08-18 18:57:00 +0000566 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnant73d21a42010-09-04 23:28:19 +0000567#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000568#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000570 vector& operator=(initializer_list<value_type> __il)
571 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000572#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000573
574 template <class _InputIterator>
575 typename enable_if
576 <
577 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000578 !__is_forward_iterator<_InputIterator>::value &&
579 is_constructible<
580 value_type,
581 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000582 void
583 >::type
584 assign(_InputIterator __first, _InputIterator __last);
585 template <class _ForwardIterator>
586 typename enable_if
587 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000588 __is_forward_iterator<_ForwardIterator>::value &&
589 is_constructible<
590 value_type,
591 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000592 void
593 >::type
594 assign(_ForwardIterator __first, _ForwardIterator __last);
595
596 void assign(size_type __n, const_reference __u);
Howard Hinnante3e32912011-08-12 21:56:02 +0000597#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000598 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000599 void assign(initializer_list<value_type> __il)
600 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000601#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000602
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000603 _LIBCPP_INLINE_VISIBILITY
604 allocator_type get_allocator() const _NOEXCEPT
605 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000606
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000607 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
608 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
609 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
610 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000611
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000612 _LIBCPP_INLINE_VISIBILITY
613 reverse_iterator rbegin() _NOEXCEPT
614 {return reverse_iterator(end());}
615 _LIBCPP_INLINE_VISIBILITY
616 const_reverse_iterator rbegin() const _NOEXCEPT
617 {return const_reverse_iterator(end());}
618 _LIBCPP_INLINE_VISIBILITY
619 reverse_iterator rend() _NOEXCEPT
620 {return reverse_iterator(begin());}
621 _LIBCPP_INLINE_VISIBILITY
622 const_reverse_iterator rend() const _NOEXCEPT
623 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000624
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000625 _LIBCPP_INLINE_VISIBILITY
626 const_iterator cbegin() const _NOEXCEPT
627 {return begin();}
628 _LIBCPP_INLINE_VISIBILITY
629 const_iterator cend() const _NOEXCEPT
630 {return end();}
631 _LIBCPP_INLINE_VISIBILITY
632 const_reverse_iterator crbegin() const _NOEXCEPT
633 {return rbegin();}
634 _LIBCPP_INLINE_VISIBILITY
635 const_reverse_iterator crend() const _NOEXCEPT
636 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000637
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000638 _LIBCPP_INLINE_VISIBILITY
639 size_type size() const _NOEXCEPT
640 {return static_cast<size_type>(this->__end_ - this->__begin_);}
641 _LIBCPP_INLINE_VISIBILITY
642 size_type capacity() const _NOEXCEPT
643 {return __base::capacity();}
644 _LIBCPP_INLINE_VISIBILITY
645 bool empty() const _NOEXCEPT
646 {return this->__begin_ == this->__end_;}
647 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000649 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000650
651 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
652 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
653 reference at(size_type __n);
654 const_reference at(size_type __n) const;
655
Howard Hinnant7a563db2011-09-14 18:33:51 +0000656 _LIBCPP_INLINE_VISIBILITY reference front()
657 {
658 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
659 return *this->__begin_;
660 }
661 _LIBCPP_INLINE_VISIBILITY const_reference front() const
662 {
663 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
664 return *this->__begin_;
665 }
666 _LIBCPP_INLINE_VISIBILITY reference back()
667 {
668 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
669 return *(this->__end_ - 1);
670 }
671 _LIBCPP_INLINE_VISIBILITY const_reference back() const
672 {
673 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
674 return *(this->__end_ - 1);
675 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000676
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000677 _LIBCPP_INLINE_VISIBILITY
678 value_type* data() _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000679 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000680 _LIBCPP_INLINE_VISIBILITY
681 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000682 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000683
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000684 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000685#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000686 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000687#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000688 template <class... _Args>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000690 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000691#endif // _LIBCPP_HAS_NO_VARIADICS
692#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000693 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000694 void pop_back();
695
696 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000697#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000698 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000699#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000700 template <class... _Args>
701 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000702#endif // _LIBCPP_HAS_NO_VARIADICS
703#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000704 iterator insert(const_iterator __position, size_type __n, const_reference __x);
705 template <class _InputIterator>
706 typename enable_if
707 <
708 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +0000709 !__is_forward_iterator<_InputIterator>::value &&
710 is_constructible<
711 value_type,
712 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000713 iterator
714 >::type
715 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
716 template <class _ForwardIterator>
717 typename enable_if
718 <
Howard Hinnant742fecb2013-03-28 17:44:32 +0000719 __is_forward_iterator<_ForwardIterator>::value &&
720 is_constructible<
721 value_type,
722 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723 iterator
724 >::type
725 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +0000726#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000728 iterator insert(const_iterator __position, initializer_list<value_type> __il)
729 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000730#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000731
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000732 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000733 iterator erase(const_iterator __first, const_iterator __last);
734
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000735 _LIBCPP_INLINE_VISIBILITY
736 void clear() _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000737 {
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000738 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000739 __base::clear();
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000740 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000741 __invalidate_all_iterators();
742 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000743
744 void resize(size_type __sz);
745 void resize(size_type __sz, const_reference __x);
746
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000747 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +0000748#if _LIBCPP_STD_VER >= 14
749 _NOEXCEPT;
750#else
751 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
752 __is_nothrow_swappable<allocator_type>::value);
753#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000754
755 bool __invariants() const;
756
Howard Hinnantabe26282011-09-16 17:29:17 +0000757#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000758
759 bool __dereferenceable(const const_iterator* __i) const;
760 bool __decrementable(const const_iterator* __i) const;
761 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
762 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
763
Howard Hinnantabe26282011-09-16 17:29:17 +0000764#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000765
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000766private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000767 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000768 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000769 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000770 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31 +0000771 void __construct_at_end(size_type __n);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000773 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000774 template <class _ForwardIterator>
775 typename enable_if
776 <
777 __is_forward_iterator<_ForwardIterator>::value,
778 void
779 >::type
Eric Fiselier088ed9f2015-03-31 16:54:19 +0000780 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000781 void __append(size_type __n);
782 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000784 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000786 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000787 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
788 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
789 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000790 void __move_assign(vector& __c, true_type)
791 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Marshall Clowaf961ed2015-08-18 18:57:00 +0000792 void __move_assign(vector& __c, false_type)
793 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000795 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +0000796 {
Howard Hinnantabe26282011-09-16 17:29:17 +0000797#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +0000798 __c_node* __c = __get_db()->__find_c_and_lock(this);
799 for (__i_node** __p = __c->end_; __p != __c->beg_; )
800 {
801 --__p;
802 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
803 if (__i->base() > __new_last)
804 {
805 (*__p)->__c_ = nullptr;
806 if (--__c->end_ != __p)
807 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
808 }
809 }
810 __get_db()->unlock();
811#endif
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000812 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000813 __base::__destruct_at_end(__new_last);
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000814 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000815 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000816 template <class _Up>
817 void
818#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
819 __push_back_slow_path(_Up&& __x);
820#else
821 __push_back_slow_path(_Up& __x);
822#endif
Howard Hinnant0438ea22012-02-26 15:30:12 +0000823#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
824 template <class... _Args>
825 void
826 __emplace_back_slow_path(_Args&&... __args);
827#endif
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000828 // The following functions are no-ops outside of AddressSanitizer mode.
829 // We call annotatations only for the default Allocator because other allocators
830 // may not meet the AddressSanitizer alignment constraints.
831 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
832 void __annotate_contiguous_container
Kostya Serebryany497f9122014-09-02 23:43:38 +0000833 (const void *__beg, const void *__end, const void *__old_mid, const void *__new_mid) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000834 {
835#ifndef _LIBCPP_HAS_NO_ASAN
836 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
837 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
838#endif
839 }
840
Kostya Serebryany497f9122014-09-02 23:43:38 +0000841 void __annotate_new(size_type __current_size) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000842 {
843 __annotate_contiguous_container(data(), data() + capacity(),
844 data() + capacity(), data() + __current_size);
845 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000846 void __annotate_delete() const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000847 {
848 __annotate_contiguous_container(data(), data() + capacity(),
849 data() + size(), data() + capacity());
850 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000851 void __annotate_increase(size_type __n) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000852 {
853 __annotate_contiguous_container(data(), data() + capacity(),
854 data() + size(), data() + size() + __n);
855 }
Kostya Serebryany497f9122014-09-02 23:43:38 +0000856 void __annotate_shrink(size_type __old_size) const
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000857 {
858 __annotate_contiguous_container(data(), data() + capacity(),
859 data() + __old_size, data() + size());
860 }
Marshall Clow26f472d2014-09-03 21:37:43 +0000861#ifndef _LIBCPP_HAS_NO_ASAN
Kostya Serebryany497f9122014-09-02 23:43:38 +0000862 // The annotation for size increase should happen before the actual increase,
863 // but if an exception is thrown after that the annotation has to be undone.
864 struct __RAII_IncreaseAnnotator {
865 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000866 : __commit(false), __v(__v), __old_size(__v.size() + __n) {
Kostya Serebryany497f9122014-09-02 23:43:38 +0000867 __v.__annotate_increase(__n);
868 }
869 void __done() { __commit = true; }
870 ~__RAII_IncreaseAnnotator() {
871 if (__commit) return;
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000872 __v.__annotate_shrink(__old_size);
Kostya Serebryany497f9122014-09-02 23:43:38 +0000873 }
874 bool __commit;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000875 const vector &__v;
Eric Fiselier9f4f2212015-03-10 00:25:20 +0000876 size_type __old_size;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000877 };
Marshall Clow26f472d2014-09-03 21:37:43 +0000878#else
879 struct __RAII_IncreaseAnnotator {
880 inline __RAII_IncreaseAnnotator(const vector &, size_type __n = 1) {}
881 inline void __done() {}
882 };
883#endif
884
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000885};
886
887template <class _Tp, class _Allocator>
888void
889vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
890{
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000891 __annotate_delete();
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000892 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000893 _VSTD::swap(this->__begin_, __v.__begin_);
894 _VSTD::swap(this->__end_, __v.__end_);
895 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000896 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000897 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000898 __invalidate_all_iterators();
899}
900
901template <class _Tp, class _Allocator>
902typename vector<_Tp, _Allocator>::pointer
903vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
904{
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000905 __annotate_delete();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000906 pointer __r = __v.__begin_;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000907 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
908 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000909 _VSTD::swap(this->__begin_, __v.__begin_);
910 _VSTD::swap(this->__end_, __v.__end_);
911 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000912 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000913 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000914 __invalidate_all_iterators();
915 return __r;
916}
917
918// Allocate space for __n objects
919// throws length_error if __n > max_size()
920// throws (probably bad_alloc) if memory run out
921// Precondition: __begin_ == __end_ == __end_cap() == 0
922// Precondition: __n > 0
923// Postcondition: capacity() == __n
924// Postcondition: size() == 0
925template <class _Tp, class _Allocator>
926void
927vector<_Tp, _Allocator>::allocate(size_type __n)
928{
929 if (__n > max_size())
930 this->__throw_length_error();
931 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
932 this->__end_cap() = this->__begin_ + __n;
Marshall Clow1f50f2d2014-05-08 14:14:06 +0000933 __annotate_new(0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000934}
935
936template <class _Tp, class _Allocator>
937void
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000938vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000939{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000940 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000941 {
942 clear();
943 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant2c39cbe2013-06-27 19:35:32 +0000944 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000945 }
946}
947
948template <class _Tp, class _Allocator>
949typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +0000950vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000951{
Sean Hunt110b8bf2011-07-29 23:31:58 +0000952 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()), numeric_limits<size_type>::max() / 2); // end() >= begin(), always
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000953}
954
955// Precondition: __new_size > capacity()
956template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000957inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000958typename vector<_Tp, _Allocator>::size_type
959vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
960{
961 const size_type __ms = max_size();
962 if (__new_size > __ms)
963 this->__throw_length_error();
964 const size_type __cap = capacity();
965 if (__cap >= __ms / 2)
966 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58 +0000967 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000968}
969
970// Default constructs __n objects starting at __end_
971// throws if construction throws
972// Precondition: __n > 0
973// Precondition: size() + __n <= capacity()
974// Postcondition: size() == size() + __n
975template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000976void
977vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
978{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000979 allocator_type& __a = this->__alloc();
980 do
981 {
Kostya Serebryany497f9122014-09-02 23:43:38 +0000982 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000983 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000984 ++this->__end_;
985 --__n;
Kostya Serebryany497f9122014-09-02 23:43:38 +0000986 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000987 } while (__n > 0);
988}
989
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990// Copy constructs __n objects starting at __end_ from __x
991// throws if construction throws
992// Precondition: __n > 0
993// Precondition: size() + __n <= capacity()
994// Postcondition: size() == old size() + __n
995// Postcondition: [i] == __x for all i in [size() - __n, __n)
996template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000997inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000998void
999vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1000{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001001 allocator_type& __a = this->__alloc();
1002 do
1003 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001004 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001005 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001006 ++this->__end_;
1007 --__n;
Kostya Serebryany497f9122014-09-02 23:43:38 +00001008 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001009 } while (__n > 0);
1010}
1011
1012template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013template <class _ForwardIterator>
1014typename enable_if
1015<
1016 __is_forward_iterator<_ForwardIterator>::value,
1017 void
1018>::type
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001019vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001020{
1021 allocator_type& __a = this->__alloc();
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001022 __RAII_IncreaseAnnotator __annotator(*this, __n);
1023 __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
1024 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001025}
1026
1027// Default constructs __n objects starting at __end_
1028// throws if construction throws
1029// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001030// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001031template <class _Tp, class _Allocator>
1032void
1033vector<_Tp, _Allocator>::__append(size_type __n)
1034{
1035 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1036 this->__construct_at_end(__n);
1037 else
1038 {
1039 allocator_type& __a = this->__alloc();
1040 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1041 __v.__construct_at_end(__n);
1042 __swap_out_circular_buffer(__v);
1043 }
1044}
1045
1046// Default constructs __n objects starting at __end_
1047// throws if construction throws
1048// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001049// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050template <class _Tp, class _Allocator>
1051void
1052vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1053{
1054 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1055 this->__construct_at_end(__n, __x);
1056 else
1057 {
1058 allocator_type& __a = this->__alloc();
1059 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1060 __v.__construct_at_end(__n, __x);
1061 __swap_out_circular_buffer(__v);
1062 }
1063}
1064
1065template <class _Tp, class _Allocator>
1066vector<_Tp, _Allocator>::vector(size_type __n)
1067{
Howard Hinnant0442b122011-09-16 18:41:29 +00001068#if _LIBCPP_DEBUG_LEVEL >= 2
1069 __get_db()->__insert_c(this);
1070#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001071 if (__n > 0)
1072 {
1073 allocate(__n);
1074 __construct_at_end(__n);
1075 }
1076}
1077
Marshall Clowa49a2c92013-09-14 00:47:59 +00001078#if _LIBCPP_STD_VER > 11
1079template <class _Tp, class _Allocator>
1080vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1081 : __base(__a)
1082{
1083#if _LIBCPP_DEBUG_LEVEL >= 2
1084 __get_db()->__insert_c(this);
1085#endif
1086 if (__n > 0)
1087 {
1088 allocate(__n);
1089 __construct_at_end(__n);
1090 }
1091}
1092#endif
1093
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001094template <class _Tp, class _Allocator>
1095vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1096{
Howard Hinnant0442b122011-09-16 18:41:29 +00001097#if _LIBCPP_DEBUG_LEVEL >= 2
1098 __get_db()->__insert_c(this);
1099#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001100 if (__n > 0)
1101 {
1102 allocate(__n);
1103 __construct_at_end(__n, __x);
1104 }
1105}
1106
1107template <class _Tp, class _Allocator>
1108vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1109 : __base(__a)
1110{
Howard Hinnant0442b122011-09-16 18:41:29 +00001111#if _LIBCPP_DEBUG_LEVEL >= 2
1112 __get_db()->__insert_c(this);
1113#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001114 if (__n > 0)
1115 {
1116 allocate(__n);
1117 __construct_at_end(__n, __x);
1118 }
1119}
1120
1121template <class _Tp, class _Allocator>
1122template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +00001123vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001124 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001125 !__is_forward_iterator<_InputIterator>::value &&
1126 is_constructible<
1127 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +00001128 typename iterator_traits<_InputIterator>::reference>::value,
1129 _InputIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001130{
Howard Hinnantabe26282011-09-16 17:29:17 +00001131#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001132 __get_db()->__insert_c(this);
1133#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001134 for (; __first != __last; ++__first)
1135 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001136}
1137
1138template <class _Tp, class _Allocator>
1139template <class _InputIterator>
1140vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1141 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001142 !__is_forward_iterator<_InputIterator>::value &&
1143 is_constructible<
1144 value_type,
1145 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001146 : __base(__a)
1147{
Howard Hinnantabe26282011-09-16 17:29:17 +00001148#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001149 __get_db()->__insert_c(this);
1150#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001151 for (; __first != __last; ++__first)
1152 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001153}
1154
1155template <class _Tp, class _Allocator>
1156template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:54 +00001157vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001158 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1159 is_constructible<
1160 value_type,
Howard Hinnantde589f22013-09-21 21:13:54 +00001161 typename iterator_traits<_ForwardIterator>::reference>::value,
1162 _ForwardIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001163{
Howard Hinnant0442b122011-09-16 18:41:29 +00001164#if _LIBCPP_DEBUG_LEVEL >= 2
1165 __get_db()->__insert_c(this);
1166#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001167 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001168 if (__n > 0)
1169 {
1170 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001171 __construct_at_end(__first, __last, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001172 }
1173}
1174
1175template <class _Tp, class _Allocator>
1176template <class _ForwardIterator>
1177vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32 +00001178 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1179 is_constructible<
1180 value_type,
1181 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001182 : __base(__a)
1183{
Howard Hinnant0442b122011-09-16 18:41:29 +00001184#if _LIBCPP_DEBUG_LEVEL >= 2
1185 __get_db()->__insert_c(this);
1186#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001187 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001188 if (__n > 0)
1189 {
1190 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001191 __construct_at_end(__first, __last, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001192 }
1193}
1194
1195template <class _Tp, class _Allocator>
1196vector<_Tp, _Allocator>::vector(const vector& __x)
1197 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1198{
Howard Hinnant0442b122011-09-16 18:41:29 +00001199#if _LIBCPP_DEBUG_LEVEL >= 2
1200 __get_db()->__insert_c(this);
1201#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001202 size_type __n = __x.size();
1203 if (__n > 0)
1204 {
1205 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001206 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001207 }
1208}
1209
1210template <class _Tp, class _Allocator>
1211vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1212 : __base(__a)
1213{
Howard Hinnant0442b122011-09-16 18:41:29 +00001214#if _LIBCPP_DEBUG_LEVEL >= 2
1215 __get_db()->__insert_c(this);
1216#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001217 size_type __n = __x.size();
1218 if (__n > 0)
1219 {
1220 allocate(__n);
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001221 __construct_at_end(__x.__begin_, __x.__end_, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001222 }
1223}
1224
Howard Hinnant73d21a42010-09-04 23:28:19 +00001225#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001226
1227template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001228inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001229vector<_Tp, _Allocator>::vector(vector&& __x)
Marshall Clow119ed472015-07-14 14:46:32 +00001230#if _LIBCPP_STD_VER > 14
1231 _NOEXCEPT
1232#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001233 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow119ed472015-07-14 14:46:32 +00001234#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001235 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001236{
Howard Hinnantabe26282011-09-16 17:29:17 +00001237#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001238 __get_db()->__insert_c(this);
Howard Hinnante6125bd2011-09-19 16:34:29 +00001239 __get_db()->swap(this, &__x);
Howard Hinnant7a563db2011-09-14 18:33:51 +00001240#endif
Howard Hinnant0442b122011-09-16 18:41:29 +00001241 this->__begin_ = __x.__begin_;
1242 this->__end_ = __x.__end_;
1243 this->__end_cap() = __x.__end_cap();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001244 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245}
1246
1247template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001248inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001249vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1250 : __base(__a)
1251{
Howard Hinnant0442b122011-09-16 18:41:29 +00001252#if _LIBCPP_DEBUG_LEVEL >= 2
1253 __get_db()->__insert_c(this);
1254#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001255 if (__a == __x.__alloc())
1256 {
1257 this->__begin_ = __x.__begin_;
1258 this->__end_ = __x.__end_;
1259 this->__end_cap() = __x.__end_cap();
1260 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001261#if _LIBCPP_DEBUG_LEVEL >= 2
1262 __get_db()->swap(this, &__x);
1263#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001264 }
1265 else
1266 {
Howard Hinnant99968442011-11-29 18:15:50 +00001267 typedef move_iterator<iterator> _Ip;
1268 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001269 }
1270}
1271
Howard Hinnante3e32912011-08-12 21:56:02 +00001272#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1273
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001274template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001275inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001276vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1277{
Howard Hinnant0442b122011-09-16 18:41:29 +00001278#if _LIBCPP_DEBUG_LEVEL >= 2
1279 __get_db()->__insert_c(this);
1280#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001281 if (__il.size() > 0)
1282 {
1283 allocate(__il.size());
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001284 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001285 }
1286}
1287
1288template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001289inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001290vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1291 : __base(__a)
1292{
Howard Hinnant0442b122011-09-16 18:41:29 +00001293#if _LIBCPP_DEBUG_LEVEL >= 2
1294 __get_db()->__insert_c(this);
1295#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001296 if (__il.size() > 0)
1297 {
1298 allocate(__il.size());
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001299 __construct_at_end(__il.begin(), __il.end(), __il.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001300 }
1301}
1302
Howard Hinnante3e32912011-08-12 21:56:02 +00001303#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1304
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001305template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001306inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001307vector<_Tp, _Allocator>&
1308vector<_Tp, _Allocator>::operator=(vector&& __x)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001309 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001310{
1311 __move_assign(__x, integral_constant<bool,
1312 __alloc_traits::propagate_on_container_move_assignment::value>());
1313 return *this;
1314}
1315
1316template <class _Tp, class _Allocator>
1317void
1318vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001319 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001320{
1321 if (__base::__alloc() != __c.__alloc())
1322 {
Howard Hinnant99968442011-11-29 18:15:50 +00001323 typedef move_iterator<iterator> _Ip;
1324 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001325 }
1326 else
1327 __move_assign(__c, true_type());
1328}
1329
1330template <class _Tp, class _Allocator>
1331void
1332vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001333 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001334{
1335 deallocate();
Marshall Clow3c2eac62014-07-21 15:11:13 +00001336 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001337 this->__begin_ = __c.__begin_;
1338 this->__end_ = __c.__end_;
1339 this->__end_cap() = __c.__end_cap();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001340 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:29 +00001341#if _LIBCPP_DEBUG_LEVEL >= 2
1342 __get_db()->swap(this, &__c);
1343#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001344}
1345
Howard Hinnant73d21a42010-09-04 23:28:19 +00001346#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001347
1348template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001349inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001350vector<_Tp, _Allocator>&
1351vector<_Tp, _Allocator>::operator=(const vector& __x)
1352{
1353 if (this != &__x)
1354 {
1355 __base::__copy_assign_alloc(__x);
1356 assign(__x.__begin_, __x.__end_);
1357 }
1358 return *this;
1359}
1360
1361template <class _Tp, class _Allocator>
1362template <class _InputIterator>
1363typename enable_if
1364<
1365 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001366 !__is_forward_iterator<_InputIterator>::value &&
1367 is_constructible<
1368 _Tp,
1369 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001370 void
1371>::type
1372vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1373{
1374 clear();
1375 for (; __first != __last; ++__first)
1376 push_back(*__first);
1377}
1378
1379template <class _Tp, class _Allocator>
1380template <class _ForwardIterator>
1381typename enable_if
1382<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001383 __is_forward_iterator<_ForwardIterator>::value &&
1384 is_constructible<
1385 _Tp,
1386 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001387 void
1388>::type
1389vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1390{
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001391 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last));
1392 if (__new_size <= capacity())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001393 {
1394 _ForwardIterator __mid = __last;
1395 bool __growing = false;
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001396 if (__new_size > size())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001397 {
1398 __growing = true;
1399 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001400 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001401 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001402 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001403 if (__growing)
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001404 __construct_at_end(__mid, __last, __new_size - size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001405 else
1406 this->__destruct_at_end(__m);
1407 }
1408 else
1409 {
1410 deallocate();
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001411 allocate(__recommend(__new_size));
1412 __construct_at_end(__first, __last, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001413 }
1414}
1415
1416template <class _Tp, class _Allocator>
1417void
1418vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1419{
1420 if (__n <= capacity())
1421 {
1422 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001423 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001424 if (__n > __s)
1425 __construct_at_end(__n - __s, __u);
1426 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001427 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001428 }
1429 else
1430 {
1431 deallocate();
1432 allocate(__recommend(static_cast<size_type>(__n)));
1433 __construct_at_end(__n, __u);
1434 }
1435}
1436
Howard Hinnant324bb032010-08-22 00:02:43 +00001437template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001438inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001439typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001440vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441{
Howard Hinnantabe26282011-09-16 17:29:17 +00001442#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001443 return iterator(this, __p);
1444#else
1445 return iterator(__p);
1446#endif
1447}
1448
Howard Hinnant324bb032010-08-22 00:02:43 +00001449template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001450inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001451typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001452vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001453{
Howard Hinnantabe26282011-09-16 17:29:17 +00001454#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001455 return const_iterator(this, __p);
1456#else
1457 return const_iterator(__p);
1458#endif
1459}
1460
Howard Hinnant324bb032010-08-22 00:02:43 +00001461template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001462inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001464vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001465{
1466 return __make_iter(this->__begin_);
1467}
1468
Howard Hinnant324bb032010-08-22 00:02:43 +00001469template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001470inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001471typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001472vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001473{
1474 return __make_iter(this->__begin_);
1475}
1476
Howard Hinnant324bb032010-08-22 00:02:43 +00001477template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001478inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001479typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001480vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001481{
1482 return __make_iter(this->__end_);
1483}
1484
Howard Hinnant324bb032010-08-22 00:02:43 +00001485template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001486inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001487typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001488vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001489{
1490 return __make_iter(this->__end_);
1491}
1492
Howard Hinnant324bb032010-08-22 00:02:43 +00001493template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001494inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001495typename vector<_Tp, _Allocator>::reference
1496vector<_Tp, _Allocator>::operator[](size_type __n)
1497{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001498 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001499 return this->__begin_[__n];
1500}
1501
Howard Hinnant324bb032010-08-22 00:02:43 +00001502template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001503inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504typename vector<_Tp, _Allocator>::const_reference
1505vector<_Tp, _Allocator>::operator[](size_type __n) const
1506{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001507 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001508 return this->__begin_[__n];
1509}
1510
Howard Hinnant324bb032010-08-22 00:02:43 +00001511template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001512typename vector<_Tp, _Allocator>::reference
1513vector<_Tp, _Allocator>::at(size_type __n)
1514{
1515 if (__n >= size())
1516 this->__throw_out_of_range();
1517 return this->__begin_[__n];
1518}
1519
Howard Hinnant324bb032010-08-22 00:02:43 +00001520template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001521typename vector<_Tp, _Allocator>::const_reference
1522vector<_Tp, _Allocator>::at(size_type __n) const
1523{
1524 if (__n >= size())
1525 this->__throw_out_of_range();
1526 return this->__begin_[__n];
1527}
1528
1529template <class _Tp, class _Allocator>
1530void
1531vector<_Tp, _Allocator>::reserve(size_type __n)
1532{
1533 if (__n > capacity())
1534 {
1535 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001536 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001537 __swap_out_circular_buffer(__v);
1538 }
1539}
1540
1541template <class _Tp, class _Allocator>
1542void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001543vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001544{
1545 if (capacity() > size())
1546 {
1547#ifndef _LIBCPP_NO_EXCEPTIONS
1548 try
1549 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001550#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001551 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001552 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001553 __swap_out_circular_buffer(__v);
1554#ifndef _LIBCPP_NO_EXCEPTIONS
1555 }
1556 catch (...)
1557 {
1558 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001559#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001560 }
1561}
1562
1563template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001564template <class _Up>
1565void
1566#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1567vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1568#else
1569vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1570#endif
1571{
1572 allocator_type& __a = this->__alloc();
1573 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1574 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnantf619e232013-01-11 20:36:59 +00001575 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1576 __v.__end_++;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001577 __swap_out_circular_buffer(__v);
1578}
1579
1580template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001581inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001582void
1583vector<_Tp, _Allocator>::push_back(const_reference __x)
1584{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001585 if (this->__end_ != this->__end_cap())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001586 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001587 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001588 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001589 _VSTD::__to_raw_pointer(this->__end_), __x);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001590 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001591 ++this->__end_;
1592 }
1593 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001594 __push_back_slow_path(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001595}
1596
Howard Hinnant73d21a42010-09-04 23:28:19 +00001597#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001598
1599template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001600inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001601void
1602vector<_Tp, _Allocator>::push_back(value_type&& __x)
1603{
1604 if (this->__end_ < this->__end_cap())
1605 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001606 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001607 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001608 _VSTD::__to_raw_pointer(this->__end_),
1609 _VSTD::move(__x));
Kostya Serebryany497f9122014-09-02 23:43:38 +00001610 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611 ++this->__end_;
1612 }
1613 else
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001614 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001615}
1616
Howard Hinnant73d21a42010-09-04 23:28:19 +00001617#ifndef _LIBCPP_HAS_NO_VARIADICS
1618
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001619template <class _Tp, class _Allocator>
1620template <class... _Args>
1621void
Howard Hinnant0438ea22012-02-26 15:30:12 +00001622vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1623{
1624 allocator_type& __a = this->__alloc();
1625 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1626// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantf619e232013-01-11 20:36:59 +00001627 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1628 __v.__end_++;
Howard Hinnant0438ea22012-02-26 15:30:12 +00001629 __swap_out_circular_buffer(__v);
1630}
1631
1632template <class _Tp, class _Allocator>
1633template <class... _Args>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001634inline
Howard Hinnant0438ea22012-02-26 15:30:12 +00001635void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001636vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1637{
1638 if (this->__end_ < this->__end_cap())
1639 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001640 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001641 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001642 _VSTD::__to_raw_pointer(this->__end_),
1643 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001644 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001645 ++this->__end_;
1646 }
1647 else
Howard Hinnant0438ea22012-02-26 15:30:12 +00001648 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001649}
1650
Howard Hinnant73d21a42010-09-04 23:28:19 +00001651#endif // _LIBCPP_HAS_NO_VARIADICS
1652#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001653
1654template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001655inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001656void
1657vector<_Tp, _Allocator>::pop_back()
1658{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001659 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001660 this->__destruct_at_end(this->__end_ - 1);
1661}
1662
1663template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001664inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001665typename vector<_Tp, _Allocator>::iterator
1666vector<_Tp, _Allocator>::erase(const_iterator __position)
1667{
Howard Hinnantabe26282011-09-16 17:29:17 +00001668#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001669 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1670 "vector::erase(iterator) called with an iterator not"
1671 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001672#endif
Howard Hinnant782da332013-03-25 22:12:26 +00001673 _LIBCPP_ASSERT(__position != end(),
1674 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00001675 difference_type __ps = __position - cbegin();
1676 pointer __p = this->__begin_ + __ps;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001677 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001678 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001679 return __r;
1680}
1681
1682template <class _Tp, class _Allocator>
1683typename vector<_Tp, _Allocator>::iterator
1684vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1685{
Howard Hinnantabe26282011-09-16 17:29:17 +00001686#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001687 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1688 "vector::erase(iterator, iterator) called with an iterator not"
1689 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001690#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +00001691 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001692 pointer __p = this->__begin_ + (__first - begin());
1693 iterator __r = __make_iter(__p);
Howard Hinnantb4e67cf2013-04-18 15:02:57 +00001694 if (__first != __last)
1695 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001696 return __r;
1697}
1698
1699template <class _Tp, class _Allocator>
1700void
1701vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1702{
1703 pointer __old_last = this->__end_;
1704 difference_type __n = __old_last - __to;
1705 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1706 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001707 _VSTD::__to_raw_pointer(this->__end_),
1708 _VSTD::move(*__i));
1709 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001710}
1711
1712template <class _Tp, class _Allocator>
1713typename vector<_Tp, _Allocator>::iterator
1714vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1715{
Howard Hinnantabe26282011-09-16 17:29:17 +00001716#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001717 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1718 "vector::insert(iterator, x) called with an iterator not"
1719 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001720#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001721 pointer __p = this->__begin_ + (__position - begin());
1722 if (this->__end_ < this->__end_cap())
1723 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001724 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001725 if (__p == this->__end_)
1726 {
1727 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001728 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001729 ++this->__end_;
1730 }
1731 else
1732 {
1733 __move_range(__p, this->__end_, __p + 1);
1734 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1735 if (__p <= __xr && __xr < this->__end_)
1736 ++__xr;
1737 *__p = *__xr;
1738 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001739 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001740 }
1741 else
1742 {
1743 allocator_type& __a = this->__alloc();
1744 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1745 __v.push_back(__x);
1746 __p = __swap_out_circular_buffer(__v, __p);
1747 }
1748 return __make_iter(__p);
1749}
1750
Howard Hinnant73d21a42010-09-04 23:28:19 +00001751#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001752
1753template <class _Tp, class _Allocator>
1754typename vector<_Tp, _Allocator>::iterator
1755vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1756{
Howard Hinnantabe26282011-09-16 17:29:17 +00001757#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001758 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1759 "vector::insert(iterator, x) called with an iterator not"
1760 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001761#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001762 pointer __p = this->__begin_ + (__position - begin());
1763 if (this->__end_ < this->__end_cap())
1764 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001765 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001766 if (__p == this->__end_)
1767 {
1768 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001769 _VSTD::__to_raw_pointer(this->__end_),
1770 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001771 ++this->__end_;
1772 }
1773 else
1774 {
1775 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001776 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001777 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001778 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001779 }
1780 else
1781 {
1782 allocator_type& __a = this->__alloc();
1783 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001784 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001785 __p = __swap_out_circular_buffer(__v, __p);
1786 }
1787 return __make_iter(__p);
1788}
1789
Howard Hinnant73d21a42010-09-04 23:28:19 +00001790#ifndef _LIBCPP_HAS_NO_VARIADICS
1791
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001792template <class _Tp, class _Allocator>
1793template <class... _Args>
1794typename vector<_Tp, _Allocator>::iterator
1795vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1796{
Howard Hinnantabe26282011-09-16 17:29:17 +00001797#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001798 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1799 "vector::emplace(iterator, x) called with an iterator not"
1800 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001801#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001802 pointer __p = this->__begin_ + (__position - begin());
1803 if (this->__end_ < this->__end_cap())
1804 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001805 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001806 if (__p == this->__end_)
1807 {
1808 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001809 _VSTD::__to_raw_pointer(this->__end_),
1810 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001811 ++this->__end_;
1812 }
1813 else
1814 {
Howard Hinnanta58402a2012-07-08 23:23:04 +00001815 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001816 __move_range(__p, this->__end_, __p + 1);
Howard Hinnanta58402a2012-07-08 23:23:04 +00001817 *__p = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001818 }
Kostya Serebryany497f9122014-09-02 23:43:38 +00001819 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001820 }
1821 else
1822 {
1823 allocator_type& __a = this->__alloc();
1824 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001825 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001826 __p = __swap_out_circular_buffer(__v, __p);
1827 }
1828 return __make_iter(__p);
1829}
1830
Howard Hinnant73d21a42010-09-04 23:28:19 +00001831#endif // _LIBCPP_HAS_NO_VARIADICS
1832#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001833
1834template <class _Tp, class _Allocator>
1835typename vector<_Tp, _Allocator>::iterator
1836vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1837{
Howard Hinnantabe26282011-09-16 17:29:17 +00001838#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001839 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1840 "vector::insert(iterator, n, x) called with an iterator not"
1841 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001842#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001843 pointer __p = this->__begin_ + (__position - begin());
1844 if (__n > 0)
1845 {
1846 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1847 {
1848 size_type __old_n = __n;
1849 pointer __old_last = this->__end_;
1850 if (__n > static_cast<size_type>(this->__end_ - __p))
1851 {
1852 size_type __cx = __n - (this->__end_ - __p);
1853 __construct_at_end(__cx, __x);
1854 __n -= __cx;
1855 }
1856 if (__n > 0)
1857 {
Eric Fiselierd7590952014-11-14 18:28:36 +00001858 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001859 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001860 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001861 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1862 if (__p <= __xr && __xr < this->__end_)
1863 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001864 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001865 }
1866 }
1867 else
1868 {
1869 allocator_type& __a = this->__alloc();
1870 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1871 __v.__construct_at_end(__n, __x);
1872 __p = __swap_out_circular_buffer(__v, __p);
1873 }
1874 }
1875 return __make_iter(__p);
1876}
1877
1878template <class _Tp, class _Allocator>
1879template <class _InputIterator>
1880typename enable_if
1881<
1882 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32 +00001883 !__is_forward_iterator<_InputIterator>::value &&
1884 is_constructible<
1885 _Tp,
1886 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001887 typename vector<_Tp, _Allocator>::iterator
1888>::type
1889vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1890{
Howard Hinnantabe26282011-09-16 17:29:17 +00001891#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001892 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1893 "vector::insert(iterator, range) called with an iterator not"
1894 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001895#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001896 difference_type __off = __position - begin();
1897 pointer __p = this->__begin_ + __off;
1898 allocator_type& __a = this->__alloc();
1899 pointer __old_last = this->__end_;
1900 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1901 {
Eric Fiselier7d439a42015-07-18 18:22:12 +00001902 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001903 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001904 *__first);
1905 ++this->__end_;
Eric Fiselier7d439a42015-07-18 18:22:12 +00001906 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001907 }
1908 __split_buffer<value_type, allocator_type&> __v(__a);
1909 if (__first != __last)
1910 {
1911#ifndef _LIBCPP_NO_EXCEPTIONS
1912 try
1913 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001914#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001915 __v.__construct_at_end(__first, __last);
1916 difference_type __old_size = __old_last - this->__begin_;
1917 difference_type __old_p = __p - this->__begin_;
1918 reserve(__recommend(size() + __v.size()));
1919 __p = this->__begin_ + __old_p;
1920 __old_last = this->__begin_ + __old_size;
1921#ifndef _LIBCPP_NO_EXCEPTIONS
1922 }
1923 catch (...)
1924 {
1925 erase(__make_iter(__old_last), end());
1926 throw;
1927 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001928#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001929 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00001930 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant0442b122011-09-16 18:41:29 +00001931 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1932 make_move_iterator(__v.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001933 return begin() + __off;
1934}
1935
1936template <class _Tp, class _Allocator>
1937template <class _ForwardIterator>
1938typename enable_if
1939<
Howard Hinnant742fecb2013-03-28 17:44:32 +00001940 __is_forward_iterator<_ForwardIterator>::value &&
1941 is_constructible<
1942 _Tp,
1943 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001944 typename vector<_Tp, _Allocator>::iterator
1945>::type
1946vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1947{
Howard Hinnantabe26282011-09-16 17:29:17 +00001948#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00001949 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1950 "vector::insert(iterator, range) called with an iterator not"
1951 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:17 +00001952#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00001954 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001955 if (__n > 0)
1956 {
1957 if (__n <= this->__end_cap() - this->__end_)
1958 {
1959 size_type __old_n = __n;
1960 pointer __old_last = this->__end_;
1961 _ForwardIterator __m = __last;
1962 difference_type __dx = this->__end_ - __p;
1963 if (__n > __dx)
1964 {
1965 __m = __first;
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001966 difference_type __diff = this->__end_ - __p;
1967 _VSTD::advance(__m, __diff);
1968 __construct_at_end(__m, __last, __n - __diff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001969 __n = __dx;
1970 }
1971 if (__n > 0)
1972 {
Kostya Serebryany497f9122014-09-02 23:43:38 +00001973 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001974 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:38 +00001975 __annotator.__done();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001976 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001977 }
1978 }
1979 else
1980 {
1981 allocator_type& __a = this->__alloc();
1982 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1983 __v.__construct_at_end(__first, __last);
1984 __p = __swap_out_circular_buffer(__v, __p);
1985 }
1986 }
1987 return __make_iter(__p);
1988}
1989
1990template <class _Tp, class _Allocator>
1991void
1992vector<_Tp, _Allocator>::resize(size_type __sz)
1993{
1994 size_type __cs = size();
1995 if (__cs < __sz)
1996 this->__append(__sz - __cs);
1997 else if (__cs > __sz)
1998 this->__destruct_at_end(this->__begin_ + __sz);
1999}
2000
2001template <class _Tp, class _Allocator>
2002void
2003vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2004{
2005 size_type __cs = size();
2006 if (__cs < __sz)
2007 this->__append(__sz - __cs, __x);
2008 else if (__cs > __sz)
2009 this->__destruct_at_end(this->__begin_ + __sz);
2010}
2011
2012template <class _Tp, class _Allocator>
2013void
2014vector<_Tp, _Allocator>::swap(vector& __x)
Marshall Clow7d914d12015-07-13 20:04:56 +00002015#if _LIBCPP_STD_VER >= 14
2016 _NOEXCEPT
2017#else
2018 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2019 __is_nothrow_swappable<allocator_type>::value)
2020#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002021{
Howard Hinnant7a563db2011-09-14 18:33:51 +00002022 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2023 this->__alloc() == __x.__alloc(),
2024 "vector::swap: Either propagate_on_container_swap must be true"
2025 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:19 +00002026 _VSTD::swap(this->__begin_, __x.__begin_);
2027 _VSTD::swap(this->__end_, __x.__end_);
2028 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Marshall Clow7d914d12015-07-13 20:04:56 +00002029 __swap_allocator(this->__alloc(), __x.__alloc(),
2030 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantabe26282011-09-16 17:29:17 +00002031#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002032 __get_db()->swap(this, &__x);
Howard Hinnantabe26282011-09-16 17:29:17 +00002033#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002034}
2035
Howard Hinnant324bb032010-08-22 00:02:43 +00002036template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002037bool
2038vector<_Tp, _Allocator>::__invariants() const
2039{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002040 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002041 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002042 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002043 return false;
2044 }
2045 else
2046 {
2047 if (this->__begin_ > this->__end_)
2048 return false;
2049 if (this->__begin_ == this->__end_cap())
2050 return false;
2051 if (this->__end_ > this->__end_cap())
2052 return false;
2053 }
2054 return true;
2055}
2056
Howard Hinnantabe26282011-09-16 17:29:17 +00002057#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002058
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002059template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:51 +00002060bool
2061vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2062{
2063 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2064}
2065
2066template <class _Tp, class _Allocator>
2067bool
2068vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2069{
2070 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2071}
2072
2073template <class _Tp, class _Allocator>
2074bool
2075vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2076{
2077 const_pointer __p = __i->base() + __n;
2078 return this->__begin_ <= __p && __p <= this->__end_;
2079}
2080
2081template <class _Tp, class _Allocator>
2082bool
2083vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2084{
2085 const_pointer __p = __i->base() + __n;
2086 return this->__begin_ <= __p && __p < this->__end_;
2087}
2088
Howard Hinnantabe26282011-09-16 17:29:17 +00002089#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002090
2091template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002092inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002093void
2094vector<_Tp, _Allocator>::__invalidate_all_iterators()
2095{
Howard Hinnantabe26282011-09-16 17:29:17 +00002096#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51 +00002097 __get_db()->__invalidate_all(this);
Howard Hinnantabe26282011-09-16 17:29:17 +00002098#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002099}
2100
2101// vector<bool>
2102
2103template <class _Allocator> class vector<bool, _Allocator>;
2104
2105template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2106
2107template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002108struct __has_storage_type<vector<bool, _Allocator> >
2109{
2110 static const bool value = true;
2111};
2112
2113template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002114class _LIBCPP_TYPE_VIS_ONLY vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002115 : private __vector_base_common<true>
2116{
2117public:
2118 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00002119 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002120 typedef _Allocator allocator_type;
2121 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002122 typedef typename __alloc_traits::size_type size_type;
2123 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantf867f632012-05-07 16:50:38 +00002124 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002125 typedef __bit_iterator<vector, false> pointer;
2126 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002127 typedef pointer iterator;
2128 typedef const_pointer const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002129 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2130 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002131
2132private:
Marshall Clow66302c62015-04-07 05:21:38 +00002133 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002134 typedef allocator_traits<__storage_allocator> __storage_traits;
2135 typedef typename __storage_traits::pointer __storage_pointer;
2136 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2137
2138 __storage_pointer __begin_;
2139 size_type __size_;
2140 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002141public:
Howard Hinnantf03c3b42011-07-02 20:33:23 +00002142 typedef __bit_reference<vector> reference;
2143 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:42 +00002144private:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002145 _LIBCPP_INLINE_VISIBILITY
2146 size_type& __cap() _NOEXCEPT
2147 {return __cap_alloc_.first();}
2148 _LIBCPP_INLINE_VISIBILITY
2149 const size_type& __cap() const _NOEXCEPT
2150 {return __cap_alloc_.first();}
2151 _LIBCPP_INLINE_VISIBILITY
2152 __storage_allocator& __alloc() _NOEXCEPT
2153 {return __cap_alloc_.second();}
2154 _LIBCPP_INLINE_VISIBILITY
2155 const __storage_allocator& __alloc() const _NOEXCEPT
2156 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002157
2158 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2159
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002160 _LIBCPP_INLINE_VISIBILITY
2161 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002162 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002163 _LIBCPP_INLINE_VISIBILITY
2164 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002165 {return (__n - 1) / __bits_per_word + 1;}
2166
2167public:
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002168 _LIBCPP_INLINE_VISIBILITY
Marshall Clowc912c0c2015-06-04 02:05:41 +00002169 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow127db912015-06-04 00:10:20 +00002170
2171 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
2172#if _LIBCPP_STD_VER <= 14
2173 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
2174#else
2175 _NOEXCEPT;
2176#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002177 ~vector();
2178 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59 +00002179#if _LIBCPP_STD_VER > 11
2180 explicit vector(size_type __n, const allocator_type& __a);
2181#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002182 vector(size_type __n, const value_type& __v);
2183 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2184 template <class _InputIterator>
2185 vector(_InputIterator __first, _InputIterator __last,
2186 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2187 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2188 template <class _InputIterator>
2189 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2190 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2191 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2192 template <class _ForwardIterator>
2193 vector(_ForwardIterator __first, _ForwardIterator __last,
2194 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2195 template <class _ForwardIterator>
2196 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2197 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2198
2199 vector(const vector& __v);
2200 vector(const vector& __v, const allocator_type& __a);
2201 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00002202#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002203 vector(initializer_list<value_type> __il);
2204 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00002205#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002206
Howard Hinnant73d21a42010-09-04 23:28:19 +00002207#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002208 _LIBCPP_INLINE_VISIBILITY
2209 vector(vector&& __v)
Marshall Clow119ed472015-07-14 14:46:32 +00002210#if _LIBCPP_STD_VER > 14
2211 _NOEXCEPT;
2212#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002213 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow119ed472015-07-14 14:46:32 +00002214#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002215 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002216 _LIBCPP_INLINE_VISIBILITY
2217 vector& operator=(vector&& __v)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002218 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnant73d21a42010-09-04 23:28:19 +00002219#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +00002220#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002222 vector& operator=(initializer_list<value_type> __il)
2223 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00002224#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002225
2226 template <class _InputIterator>
2227 typename enable_if
2228 <
2229 __is_input_iterator<_InputIterator>::value &&
2230 !__is_forward_iterator<_InputIterator>::value,
2231 void
2232 >::type
2233 assign(_InputIterator __first, _InputIterator __last);
2234 template <class _ForwardIterator>
2235 typename enable_if
2236 <
2237 __is_forward_iterator<_ForwardIterator>::value,
2238 void
2239 >::type
2240 assign(_ForwardIterator __first, _ForwardIterator __last);
2241
2242 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +00002243#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002244 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002245 void assign(initializer_list<value_type> __il)
2246 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002247#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002248
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002249 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002250 {return allocator_type(this->__alloc());}
2251
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002252 size_type max_size() const _NOEXCEPT;
2253 _LIBCPP_INLINE_VISIBILITY
2254 size_type capacity() const _NOEXCEPT
2255 {return __internal_cap_to_external(__cap());}
2256 _LIBCPP_INLINE_VISIBILITY
2257 size_type size() const _NOEXCEPT
2258 {return __size_;}
2259 _LIBCPP_INLINE_VISIBILITY
2260 bool empty() const _NOEXCEPT
2261 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002262 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002263 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002264
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002265 _LIBCPP_INLINE_VISIBILITY
2266 iterator begin() _NOEXCEPT
2267 {return __make_iter(0);}
2268 _LIBCPP_INLINE_VISIBILITY
2269 const_iterator begin() const _NOEXCEPT
2270 {return __make_iter(0);}
2271 _LIBCPP_INLINE_VISIBILITY
2272 iterator end() _NOEXCEPT
2273 {return __make_iter(__size_);}
2274 _LIBCPP_INLINE_VISIBILITY
2275 const_iterator end() const _NOEXCEPT
2276 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002277
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002278 _LIBCPP_INLINE_VISIBILITY
2279 reverse_iterator rbegin() _NOEXCEPT
2280 {return reverse_iterator(end());}
2281 _LIBCPP_INLINE_VISIBILITY
2282 const_reverse_iterator rbegin() const _NOEXCEPT
2283 {return const_reverse_iterator(end());}
2284 _LIBCPP_INLINE_VISIBILITY
2285 reverse_iterator rend() _NOEXCEPT
2286 {return reverse_iterator(begin());}
2287 _LIBCPP_INLINE_VISIBILITY
2288 const_reverse_iterator rend() const _NOEXCEPT
2289 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002290
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002291 _LIBCPP_INLINE_VISIBILITY
2292 const_iterator cbegin() const _NOEXCEPT
2293 {return __make_iter(0);}
2294 _LIBCPP_INLINE_VISIBILITY
2295 const_iterator cend() const _NOEXCEPT
2296 {return __make_iter(__size_);}
2297 _LIBCPP_INLINE_VISIBILITY
2298 const_reverse_iterator crbegin() const _NOEXCEPT
2299 {return rbegin();}
2300 _LIBCPP_INLINE_VISIBILITY
2301 const_reverse_iterator crend() const _NOEXCEPT
2302 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002303
2304 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2305 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2306 reference at(size_type __n);
2307 const_reference at(size_type __n) const;
2308
2309 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2310 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2311 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2312 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2313
2314 void push_back(const value_type& __x);
Marshall Clow198a2a52013-08-13 23:54:12 +00002315#if _LIBCPP_STD_VER > 11
2316 template <class... _Args>
2317 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2318 { push_back ( value_type ( _VSTD::forward<_Args>(__args)... )); }
2319#endif
2320
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002321 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2322
Marshall Clow198a2a52013-08-13 23:54:12 +00002323#if _LIBCPP_STD_VER > 11
2324 template <class... _Args>
2325 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2326 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2327#endif
2328
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002329 iterator insert(const_iterator __position, const value_type& __x);
2330 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2331 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2332 template <class _InputIterator>
2333 typename enable_if
2334 <
2335 __is_input_iterator <_InputIterator>::value &&
2336 !__is_forward_iterator<_InputIterator>::value,
2337 iterator
2338 >::type
2339 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2340 template <class _ForwardIterator>
2341 typename enable_if
2342 <
2343 __is_forward_iterator<_ForwardIterator>::value,
2344 iterator
2345 >::type
2346 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00002347#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002348 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002349 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2350 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00002351#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002352
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002353 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002354 iterator erase(const_iterator __first, const_iterator __last);
2355
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002356 _LIBCPP_INLINE_VISIBILITY
2357 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002358
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002359 void swap(vector&)
Marshall Clow7d914d12015-07-13 20:04:56 +00002360#if _LIBCPP_STD_VER >= 14
2361 _NOEXCEPT;
2362#else
2363 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2364 __is_nothrow_swappable<allocator_type>::value);
2365#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002366
2367 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002368 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002369
2370 bool __invariants() const;
2371
2372private:
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002373 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002374 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002375 void deallocate() _NOEXCEPT;
2376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00002377 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow0d1965d2014-07-28 15:02:42 +00002378 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002379 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2380 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002381 template <class _ForwardIterator>
2382 typename enable_if
2383 <
2384 __is_forward_iterator<_ForwardIterator>::value,
2385 void
2386 >::type
2387 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2388 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002389 _LIBCPP_INLINE_VISIBILITY
2390 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002391 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002392 _LIBCPP_INLINE_VISIBILITY
2393 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002394 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002395 _LIBCPP_INLINE_VISIBILITY
2396 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002397 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002398 _LIBCPP_INLINE_VISIBILITY
2399 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002400 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002401 _LIBCPP_INLINE_VISIBILITY
2402 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002403 {return begin() + (__p - cbegin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002404
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002406 void __copy_assign_alloc(const vector& __v)
2407 {__copy_assign_alloc(__v, integral_constant<bool,
2408 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002410 void __copy_assign_alloc(const vector& __c, true_type)
2411 {
2412 if (__alloc() != __c.__alloc())
2413 deallocate();
2414 __alloc() = __c.__alloc();
2415 }
2416
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002418 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002419 {}
2420
2421 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002422 void __move_assign(vector& __c, true_type)
2423 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002425 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002426 _NOEXCEPT_(
2427 !__storage_traits::propagate_on_container_move_assignment::value ||
2428 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002429 {__move_assign_alloc(__c, integral_constant<bool,
2430 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002431 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00002432 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002433 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002434 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002435 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002436 }
2437
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002438 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002439 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002440 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002441 {}
2442
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002443 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002444
2445 friend class __bit_reference<vector>;
2446 friend class __bit_const_reference<vector>;
2447 friend class __bit_iterator<vector, false>;
2448 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:18 +00002449 friend struct __bit_array<vector>;
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002450 friend struct _LIBCPP_TYPE_VIS_ONLY hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002451};
2452
2453template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002454inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002455void
2456vector<bool, _Allocator>::__invalidate_all_iterators()
2457{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002458}
2459
2460// Allocate space for __n objects
2461// throws length_error if __n > max_size()
2462// throws (probably bad_alloc) if memory run out
2463// Precondition: __begin_ == __end_ == __cap() == 0
2464// Precondition: __n > 0
2465// Postcondition: capacity() == __n
2466// Postcondition: size() == 0
2467template <class _Allocator>
2468void
2469vector<bool, _Allocator>::allocate(size_type __n)
2470{
2471 if (__n > max_size())
2472 this->__throw_length_error();
2473 __n = __external_cap_to_internal(__n);
2474 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2475 this->__size_ = 0;
2476 this->__cap() = __n;
2477}
2478
2479template <class _Allocator>
2480void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002481vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002482{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002483 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002484 {
2485 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2486 __invalidate_all_iterators();
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002487 this->__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002488 this->__size_ = this->__cap() = 0;
2489 }
2490}
2491
2492template <class _Allocator>
2493typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002494vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002495{
2496 size_type __amax = __storage_traits::max_size(__alloc());
2497 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2498 if (__nmax / __bits_per_word <= __amax)
2499 return __nmax;
2500 return __internal_cap_to_external(__amax);
2501}
2502
2503// Precondition: __new_size > capacity()
2504template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002505inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002506typename vector<bool, _Allocator>::size_type
2507vector<bool, _Allocator>::__recommend(size_type __new_size) const
2508{
2509 const size_type __ms = max_size();
2510 if (__new_size > __ms)
2511 this->__throw_length_error();
2512 const size_type __cap = capacity();
2513 if (__cap >= __ms / 2)
2514 return __ms;
Howard Hinnant7f764502013-08-14 18:00:20 +00002515 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002516}
2517
2518// Default constructs __n objects starting at __end_
2519// Precondition: __n > 0
2520// Precondition: size() + __n <= capacity()
2521// Postcondition: size() == size() + __n
2522template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002523inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002524void
2525vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2526{
2527 size_type __old_size = this->__size_;
2528 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002529 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002530}
2531
2532template <class _Allocator>
2533template <class _ForwardIterator>
2534typename enable_if
2535<
2536 __is_forward_iterator<_ForwardIterator>::value,
2537 void
2538>::type
2539vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2540{
2541 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002542 this->__size_ += _VSTD::distance(__first, __last);
2543 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002544}
2545
2546template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002547inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002548vector<bool, _Allocator>::vector()
Marshall Clowc912c0c2015-06-04 02:05:41 +00002549 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002550 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002551 __size_(0),
2552 __cap_alloc_(0)
2553{
2554}
2555
2556template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002557inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002558vector<bool, _Allocator>::vector(const allocator_type& __a)
Marshall Clow127db912015-06-04 00:10:20 +00002559#if _LIBCPP_STD_VER <= 14
2560 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
2561#else
2562 _NOEXCEPT
2563#endif
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002564 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002565 __size_(0),
2566 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2567{
2568}
2569
2570template <class _Allocator>
2571vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002572 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002573 __size_(0),
2574 __cap_alloc_(0)
2575{
2576 if (__n > 0)
2577 {
2578 allocate(__n);
2579 __construct_at_end(__n, false);
2580 }
2581}
2582
Marshall Clowa49a2c92013-09-14 00:47:59 +00002583#if _LIBCPP_STD_VER > 11
2584template <class _Allocator>
2585vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2586 : __begin_(nullptr),
2587 __size_(0),
2588 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2589{
2590 if (__n > 0)
2591 {
2592 allocate(__n);
2593 __construct_at_end(__n, false);
2594 }
2595}
2596#endif
2597
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002598template <class _Allocator>
2599vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002600 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002601 __size_(0),
2602 __cap_alloc_(0)
2603{
2604 if (__n > 0)
2605 {
2606 allocate(__n);
2607 __construct_at_end(__n, __x);
2608 }
2609}
2610
2611template <class _Allocator>
2612vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002613 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002614 __size_(0),
2615 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2616{
2617 if (__n > 0)
2618 {
2619 allocate(__n);
2620 __construct_at_end(__n, __x);
2621 }
2622}
2623
2624template <class _Allocator>
2625template <class _InputIterator>
2626vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2627 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2628 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002629 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002630 __size_(0),
2631 __cap_alloc_(0)
2632{
2633#ifndef _LIBCPP_NO_EXCEPTIONS
2634 try
2635 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002636#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002637 for (; __first != __last; ++__first)
2638 push_back(*__first);
2639#ifndef _LIBCPP_NO_EXCEPTIONS
2640 }
2641 catch (...)
2642 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002643 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002644 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2645 __invalidate_all_iterators();
2646 throw;
2647 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002648#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002649}
2650
2651template <class _Allocator>
2652template <class _InputIterator>
2653vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2654 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2655 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002656 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002657 __size_(0),
2658 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2659{
2660#ifndef _LIBCPP_NO_EXCEPTIONS
2661 try
2662 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002663#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002664 for (; __first != __last; ++__first)
2665 push_back(*__first);
2666#ifndef _LIBCPP_NO_EXCEPTIONS
2667 }
2668 catch (...)
2669 {
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002670 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002671 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2672 __invalidate_all_iterators();
2673 throw;
2674 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002675#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002676}
2677
2678template <class _Allocator>
2679template <class _ForwardIterator>
2680vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2681 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002682 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002683 __size_(0),
2684 __cap_alloc_(0)
2685{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002686 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002687 if (__n > 0)
2688 {
2689 allocate(__n);
2690 __construct_at_end(__first, __last);
2691 }
2692}
2693
2694template <class _Allocator>
2695template <class _ForwardIterator>
2696vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2697 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002698 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002699 __size_(0),
2700 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2701{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002702 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002703 if (__n > 0)
2704 {
2705 allocate(__n);
2706 __construct_at_end(__first, __last);
2707 }
2708}
2709
Howard Hinnante3e32912011-08-12 21:56:02 +00002710#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2711
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002712template <class _Allocator>
2713vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002714 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002715 __size_(0),
2716 __cap_alloc_(0)
2717{
2718 size_type __n = static_cast<size_type>(__il.size());
2719 if (__n > 0)
2720 {
2721 allocate(__n);
2722 __construct_at_end(__il.begin(), __il.end());
2723 }
2724}
2725
2726template <class _Allocator>
2727vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002728 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002729 __size_(0),
2730 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2731{
2732 size_type __n = static_cast<size_type>(__il.size());
2733 if (__n > 0)
2734 {
2735 allocate(__n);
2736 __construct_at_end(__il.begin(), __il.end());
2737 }
2738}
2739
Howard Hinnante3e32912011-08-12 21:56:02 +00002740#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2741
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002742template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002743vector<bool, _Allocator>::~vector()
2744{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002745 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002746 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002747 __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002748}
2749
2750template <class _Allocator>
2751vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002752 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002753 __size_(0),
2754 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2755{
2756 if (__v.size() > 0)
2757 {
2758 allocate(__v.size());
2759 __construct_at_end(__v.begin(), __v.end());
2760 }
2761}
2762
2763template <class _Allocator>
2764vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002765 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002766 __size_(0),
2767 __cap_alloc_(0, __a)
2768{
2769 if (__v.size() > 0)
2770 {
2771 allocate(__v.size());
2772 __construct_at_end(__v.begin(), __v.end());
2773 }
2774}
2775
2776template <class _Allocator>
2777vector<bool, _Allocator>&
2778vector<bool, _Allocator>::operator=(const vector& __v)
2779{
2780 if (this != &__v)
2781 {
2782 __copy_assign_alloc(__v);
2783 if (__v.__size_)
2784 {
2785 if (__v.__size_ > capacity())
2786 {
2787 deallocate();
2788 allocate(__v.__size_);
2789 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002790 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002791 }
2792 __size_ = __v.__size_;
2793 }
2794 return *this;
2795}
2796
Howard Hinnant73d21a42010-09-04 23:28:19 +00002797#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2798
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002799template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002800inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002801vector<bool, _Allocator>::vector(vector&& __v)
Marshall Clow119ed472015-07-14 14:46:32 +00002802#if _LIBCPP_STD_VER > 14
2803 _NOEXCEPT
2804#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002805 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow119ed472015-07-14 14:46:32 +00002806#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002807 : __begin_(__v.__begin_),
2808 __size_(__v.__size_),
2809 __cap_alloc_(__v.__cap_alloc_)
2810{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002811 __v.__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002812 __v.__size_ = 0;
2813 __v.__cap() = 0;
2814}
2815
2816template <class _Allocator>
2817vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00002818 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002819 __size_(0),
2820 __cap_alloc_(0, __a)
2821{
2822 if (__a == allocator_type(__v.__alloc()))
2823 {
2824 this->__begin_ = __v.__begin_;
2825 this->__size_ = __v.__size_;
2826 this->__cap() = __v.__cap();
2827 __v.__begin_ = nullptr;
2828 __v.__cap() = __v.__size_ = 0;
2829 }
2830 else if (__v.size() > 0)
2831 {
2832 allocate(__v.size());
2833 __construct_at_end(__v.begin(), __v.end());
2834 }
2835}
2836
2837template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002838inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002839vector<bool, _Allocator>&
2840vector<bool, _Allocator>::operator=(vector&& __v)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002841 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002842{
2843 __move_assign(__v, integral_constant<bool,
2844 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002845 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002846}
2847
2848template <class _Allocator>
2849void
2850vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2851{
2852 if (__alloc() != __c.__alloc())
2853 assign(__c.begin(), __c.end());
2854 else
2855 __move_assign(__c, true_type());
2856}
2857
2858template <class _Allocator>
2859void
2860vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002861 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002862{
2863 deallocate();
Marshall Clowdb5e54d2014-07-21 15:15:15 +00002864 __move_assign_alloc(__c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002865 this->__begin_ = __c.__begin_;
2866 this->__size_ = __c.__size_;
2867 this->__cap() = __c.__cap();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002868 __c.__begin_ = nullptr;
2869 __c.__cap() = __c.__size_ = 0;
2870}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002871
2872#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002873
2874template <class _Allocator>
2875void
2876vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2877{
2878 __size_ = 0;
2879 if (__n > 0)
2880 {
2881 size_type __c = capacity();
2882 if (__n <= __c)
2883 __size_ = __n;
2884 else
2885 {
2886 vector __v(__alloc());
2887 __v.reserve(__recommend(__n));
2888 __v.__size_ = __n;
2889 swap(__v);
2890 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002891 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002892 }
2893}
2894
2895template <class _Allocator>
2896template <class _InputIterator>
2897typename enable_if
2898<
2899 __is_input_iterator<_InputIterator>::value &&
2900 !__is_forward_iterator<_InputIterator>::value,
2901 void
2902>::type
2903vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2904{
2905 clear();
2906 for (; __first != __last; ++__first)
2907 push_back(*__first);
2908}
2909
2910template <class _Allocator>
2911template <class _ForwardIterator>
2912typename enable_if
2913<
2914 __is_forward_iterator<_ForwardIterator>::value,
2915 void
2916>::type
2917vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2918{
2919 clear();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002920 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002921 if (__n)
2922 {
2923 if (__n > capacity())
2924 {
2925 deallocate();
2926 allocate(__n);
2927 }
2928 __construct_at_end(__first, __last);
2929 }
2930}
2931
2932template <class _Allocator>
2933void
2934vector<bool, _Allocator>::reserve(size_type __n)
2935{
2936 if (__n > capacity())
2937 {
2938 vector __v(this->__alloc());
2939 __v.allocate(__n);
2940 __v.__construct_at_end(this->begin(), this->end());
2941 swap(__v);
2942 __invalidate_all_iterators();
2943 }
2944}
2945
2946template <class _Allocator>
2947void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00002948vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002949{
2950 if (__external_cap_to_internal(size()) > __cap())
2951 {
2952#ifndef _LIBCPP_NO_EXCEPTIONS
2953 try
2954 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002955#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002956 vector(*this, allocator_type(__alloc())).swap(*this);
2957#ifndef _LIBCPP_NO_EXCEPTIONS
2958 }
2959 catch (...)
2960 {
2961 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002962#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002963 }
2964}
2965
2966template <class _Allocator>
2967typename vector<bool, _Allocator>::reference
2968vector<bool, _Allocator>::at(size_type __n)
2969{
2970 if (__n >= size())
2971 this->__throw_out_of_range();
2972 return (*this)[__n];
2973}
2974
2975template <class _Allocator>
2976typename vector<bool, _Allocator>::const_reference
2977vector<bool, _Allocator>::at(size_type __n) const
2978{
2979 if (__n >= size())
2980 this->__throw_out_of_range();
2981 return (*this)[__n];
2982}
2983
2984template <class _Allocator>
2985void
2986vector<bool, _Allocator>::push_back(const value_type& __x)
2987{
2988 if (this->__size_ == this->capacity())
2989 reserve(__recommend(this->__size_ + 1));
2990 ++this->__size_;
2991 back() = __x;
2992}
2993
2994template <class _Allocator>
2995typename vector<bool, _Allocator>::iterator
2996vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2997{
2998 iterator __r;
2999 if (size() < capacity())
3000 {
3001 const_iterator __old_end = end();
3002 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003003 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003004 __r = __const_iterator_cast(__position);
3005 }
3006 else
3007 {
3008 vector __v(__alloc());
3009 __v.reserve(__recommend(__size_ + 1));
3010 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003011 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3012 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003013 swap(__v);
3014 }
3015 *__r = __x;
3016 return __r;
3017}
3018
3019template <class _Allocator>
3020typename vector<bool, _Allocator>::iterator
3021vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3022{
3023 iterator __r;
3024 size_type __c = capacity();
3025 if (__n <= __c && size() <= __c - __n)
3026 {
3027 const_iterator __old_end = end();
3028 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003029 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003030 __r = __const_iterator_cast(__position);
3031 }
3032 else
3033 {
3034 vector __v(__alloc());
3035 __v.reserve(__recommend(__size_ + __n));
3036 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003037 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3038 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003039 swap(__v);
3040 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003041 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003042 return __r;
3043}
3044
3045template <class _Allocator>
3046template <class _InputIterator>
3047typename enable_if
3048<
3049 __is_input_iterator <_InputIterator>::value &&
3050 !__is_forward_iterator<_InputIterator>::value,
3051 typename vector<bool, _Allocator>::iterator
3052>::type
3053vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3054{
3055 difference_type __off = __position - begin();
3056 iterator __p = __const_iterator_cast(__position);
3057 iterator __old_end = end();
3058 for (; size() != capacity() && __first != __last; ++__first)
3059 {
3060 ++this->__size_;
3061 back() = *__first;
3062 }
3063 vector __v(__alloc());
3064 if (__first != __last)
3065 {
3066#ifndef _LIBCPP_NO_EXCEPTIONS
3067 try
3068 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003069#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003070 __v.assign(__first, __last);
3071 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3072 difference_type __old_p = __p - begin();
3073 reserve(__recommend(size() + __v.size()));
3074 __p = begin() + __old_p;
3075 __old_end = begin() + __old_size;
3076#ifndef _LIBCPP_NO_EXCEPTIONS
3077 }
3078 catch (...)
3079 {
3080 erase(__old_end, end());
3081 throw;
3082 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003083#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003084 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003085 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003086 insert(__p, __v.begin(), __v.end());
3087 return begin() + __off;
3088}
3089
3090template <class _Allocator>
3091template <class _ForwardIterator>
3092typename enable_if
3093<
3094 __is_forward_iterator<_ForwardIterator>::value,
3095 typename vector<bool, _Allocator>::iterator
3096>::type
3097vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3098{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003099 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003100 iterator __r;
3101 size_type __c = capacity();
3102 if (__n <= __c && size() <= __c - __n)
3103 {
3104 const_iterator __old_end = end();
3105 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003106 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003107 __r = __const_iterator_cast(__position);
3108 }
3109 else
3110 {
3111 vector __v(__alloc());
3112 __v.reserve(__recommend(__size_ + __n));
3113 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003114 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3115 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003116 swap(__v);
3117 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003118 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003119 return __r;
3120}
3121
3122template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003123inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003124typename vector<bool, _Allocator>::iterator
3125vector<bool, _Allocator>::erase(const_iterator __position)
3126{
3127 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003128 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003129 --__size_;
3130 return __r;
3131}
3132
3133template <class _Allocator>
3134typename vector<bool, _Allocator>::iterator
3135vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3136{
3137 iterator __r = __const_iterator_cast(__first);
3138 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003139 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003140 __size_ -= __d;
3141 return __r;
3142}
3143
3144template <class _Allocator>
3145void
3146vector<bool, _Allocator>::swap(vector& __x)
Marshall Clow7d914d12015-07-13 20:04:56 +00003147#if _LIBCPP_STD_VER >= 14
3148 _NOEXCEPT
3149#else
3150 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3151 __is_nothrow_swappable<allocator_type>::value)
3152#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003153{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003154 _VSTD::swap(this->__begin_, __x.__begin_);
3155 _VSTD::swap(this->__size_, __x.__size_);
3156 _VSTD::swap(this->__cap(), __x.__cap());
Marshall Clow7d914d12015-07-13 20:04:56 +00003157 __swap_allocator(this->__alloc(), __x.__alloc(),
3158 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003159}
3160
Howard Hinnant324bb032010-08-22 00:02:43 +00003161template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003162void
3163vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3164{
3165 size_type __cs = size();
3166 if (__cs < __sz)
3167 {
3168 iterator __r;
3169 size_type __c = capacity();
3170 size_type __n = __sz - __cs;
3171 if (__n <= __c && __cs <= __c - __n)
3172 {
3173 __r = end();
3174 __size_ += __n;
3175 }
3176 else
3177 {
3178 vector __v(__alloc());
3179 __v.reserve(__recommend(__size_ + __n));
3180 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003181 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003182 swap(__v);
3183 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003184 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003185 }
3186 else
3187 __size_ = __sz;
3188}
3189
Howard Hinnant324bb032010-08-22 00:02:43 +00003190template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003191void
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003192vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003193{
3194 // do middle whole words
3195 size_type __n = __size_;
3196 __storage_pointer __p = __begin_;
3197 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3198 *__p = ~*__p;
3199 // do last partial word
3200 if (__n > 0)
3201 {
3202 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3203 __storage_type __b = *__p & __m;
3204 *__p &= ~__m;
3205 *__p |= ~__b & __m;
3206 }
3207}
3208
Howard Hinnant324bb032010-08-22 00:02:43 +00003209template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003210bool
3211vector<bool, _Allocator>::__invariants() const
3212{
Howard Hinnant2c39cbe2013-06-27 19:35:32 +00003213 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003214 {
3215 if (this->__size_ != 0 || this->__cap() != 0)
3216 return false;
3217 }
3218 else
3219 {
3220 if (this->__cap() == 0)
3221 return false;
3222 if (this->__size_ > this->capacity())
3223 return false;
3224 }
3225 return true;
3226}
3227
Howard Hinnant324bb032010-08-22 00:02:43 +00003228template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003229size_t
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003230vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003231{
3232 size_t __h = 0;
3233 // do middle whole words
3234 size_type __n = __size_;
3235 __storage_pointer __p = __begin_;
3236 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3237 __h ^= *__p;
3238 // do last partial word
3239 if (__n > 0)
3240 {
3241 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3242 __h ^= *__p & __m;
3243 }
3244 return __h;
3245}
3246
3247template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003248struct _LIBCPP_TYPE_VIS_ONLY hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003249 : public unary_function<vector<bool, _Allocator>, size_t>
3250{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00003251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003252 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003253 {return __vec.__hash_code();}
3254};
3255
3256template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003257inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003258bool
3259operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3260{
3261 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003262 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003263}
3264
3265template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003266inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003267bool
3268operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3269{
3270 return !(__x == __y);
3271}
3272
3273template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003274inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003275bool
3276operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3277{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003278 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003279}
3280
3281template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003282inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003283bool
3284operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3285{
3286 return __y < __x;
3287}
3288
3289template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003290inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003291bool
3292operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3293{
3294 return !(__x < __y);
3295}
3296
3297template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003298inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003299bool
3300operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3301{
3302 return !(__y < __x);
3303}
3304
3305template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003306inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003307void
3308swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40 +00003309 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003310{
3311 __x.swap(__y);
3312}
3313
3314_LIBCPP_END_NAMESPACE_STD
3315
3316#endif // _LIBCPP_VECTOR