blob: 12195df56124284651659f681523b23c8e098ece [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===------------------------------ vector --------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// 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:168//
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:4320template <class T, class Allocator = allocator<T> >
Howard Hinnantbc8d3f92010-05-11 19:42:1621class vector
Howard Hinnant324bb032010-08-22 00:02:4322{
23public:
24 typedef T value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:1625 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:4037 vector()
38 noexcept(is_nothrow_default_constructible<allocator_type>::value);
39 explicit vector(const allocator_type&);
Howard Hinnantbc8d3f92010-05-11 19:42:1640 explicit vector(size_type n);
Marshall Clowa49a2c92013-09-14 00:47:5941 explicit vector(size_type n, const allocator_type&); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:1642 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:4046 vector(vector&& x)
47 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:1648 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:4052 vector& operator=(vector&& x)
53 noexcept(
54 allocator_type::propagate_on_container_move_assignment::value &&
55 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:1656 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:4062 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1663
Howard Hinnantd1d27a42011-06-03 19:40:4064 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:1668
Howard Hinnantd1d27a42011-06-03 19:40:4069 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:1673
Howard Hinnantd1d27a42011-06-03 19:40:4074 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:1678
Howard Hinnantd1d27a42011-06-03 19:40:4079 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:1683 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:4084 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1685
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:4096 value_type* data() noexcept;
97 const value_type* data() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1698
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:40116 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16117
118 void resize(size_type sz);
119 void resize(size_type sz, const value_type& c);
120
Howard Hinnantd1d27a42011-06-03 19:40:40121 void swap(vector&)
122 noexcept(!allocator_type::propagate_on_container_swap::value ||
123 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16124
125 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43126};
Howard Hinnantbc8d3f92010-05-11 19:42:16127
Howard Hinnant324bb032010-08-22 00:02:43128template <class Allocator = allocator<T> >
Howard Hinnantbc8d3f92010-05-11 19:42:16129class vector<bool, Allocator>
Howard Hinnant324bb032010-08-22 00:02:43130{
131public:
132 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16133 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:40146 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:16152 };
153
154 class const_reference
155 {
156 public:
Howard Hinnantd1d27a42011-06-03 19:40:40157 const_reference(const reference&) noexcept;
158 operator bool() const noexcept;
159 const_iterator operator&() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16160 };
161
Howard Hinnantd1d27a42011-06-03 19:40:40162 vector()
163 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant9cbee432011-09-02 20:42:31164 explicit vector(const allocator_type&);
Marshall Clowa49a2c92013-09-14 00:47:59165 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:16167 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:40170 vector(vector&& x)
171 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16172 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:40176 vector& operator=(vector&& x)
177 noexcept(
178 allocator_type::propagate_on_container_move_assignment::value &&
179 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16180 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:40186 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16187
Howard Hinnantd1d27a42011-06-03 19:40:40188 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:16192
Howard Hinnantd1d27a42011-06-03 19:40:40193 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:16197
Howard Hinnantd1d27a42011-06-03 19:40:40198 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:16202
Howard Hinnantd1d27a42011-06-03 19:40:40203 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:16207 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:40208 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16209
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:12221 template <class... Args> void emplace_back(Args&&... args); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16222 void pop_back();
223
Marshall Clow198a2a52013-08-13 23:54:12224 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16225 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:40234 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16235
236 void resize(size_type sz);
237 void resize(size_type sz, value_type x);
238
Howard Hinnantd1d27a42011-06-03 19:40:40239 void swap(vector&)
240 noexcept(!allocator_type::propagate_on_container_swap::value ||
241 __is_nothrow_swappable<allocator_type>::value);
242 void flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16243
244 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43245};
Howard Hinnantbc8d3f92010-05-11 19:42:16246
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:40256template <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:16259
260} // std
261
262*/
263
264#include <__config>
265#include <__bit_reference>
266#include <type_traits>
267#include <climits>
268#include <limits>
269#include <initializer_list>
270#include <memory>
271#include <stdexcept>
272#include <algorithm>
273#include <cstring>
274#include <__split_buffer>
275#include <__functional_base>
Howard Hinnantbc8d3f92010-05-11 19:42:16276
Howard Hinnant66c6f972011-11-29 16:45:27277#include <__undef_min_max>
278
Eric Fiselierb9536102014-08-10 23:53:08279#include <__debug>
Howard Hinnant8b00e6c2013-08-02 00:26:35280
Howard Hinnant08e17472011-10-17 20:05:10281#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16282#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10283#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16284
285_LIBCPP_BEGIN_NAMESPACE_STD
286
287template <bool>
288class __vector_base_common
289{
290protected:
291 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
292 void __throw_length_error() const;
293 void __throw_out_of_range() const;
294};
295
296template <bool __b>
297void
298__vector_base_common<__b>::__throw_length_error() const
299{
300#ifndef _LIBCPP_NO_EXCEPTIONS
301 throw length_error("vector");
302#else
303 assert(!"vector length_error");
304#endif
305}
306
307template <bool __b>
308void
309__vector_base_common<__b>::__throw_out_of_range() const
310{
311#ifndef _LIBCPP_NO_EXCEPTIONS
312 throw out_of_range("vector");
313#else
314 assert(!"vector out_of_range");
315#endif
316}
317
Howard Hinnante9df0a52013-08-01 18:17:34318#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45319#pragma warning( push )
320#pragma warning( disable: 4231 )
Howard Hinnante9df0a52013-08-01 18:17:34321#endif // _LIBCPP_MSVC
Howard Hinnant0f678bd2013-08-12 18:38:34322_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS __vector_base_common<true>)
Howard Hinnante9df0a52013-08-01 18:17:34323#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45324#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34325#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16326
327template <class _Tp, class _Allocator>
328class __vector_base
329 : protected __vector_base_common<true>
330{
331protected:
Howard Hinnant324bb032010-08-22 00:02:43332 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16333 typedef _Allocator allocator_type;
334 typedef allocator_traits<allocator_type> __alloc_traits;
335 typedef value_type& reference;
336 typedef const value_type& const_reference;
337 typedef typename __alloc_traits::size_type size_type;
338 typedef typename __alloc_traits::difference_type difference_type;
339 typedef typename __alloc_traits::pointer pointer;
340 typedef typename __alloc_traits::const_pointer const_pointer;
341 typedef pointer iterator;
342 typedef const_pointer const_iterator;
343
344 pointer __begin_;
345 pointer __end_;
346 __compressed_pair<pointer, allocator_type> __end_cap_;
347
Howard Hinnantd1d27a42011-06-03 19:40:40348 _LIBCPP_INLINE_VISIBILITY
349 allocator_type& __alloc() _NOEXCEPT
350 {return __end_cap_.second();}
351 _LIBCPP_INLINE_VISIBILITY
352 const allocator_type& __alloc() const _NOEXCEPT
353 {return __end_cap_.second();}
354 _LIBCPP_INLINE_VISIBILITY
355 pointer& __end_cap() _NOEXCEPT
356 {return __end_cap_.first();}
357 _LIBCPP_INLINE_VISIBILITY
358 const pointer& __end_cap() const _NOEXCEPT
359 {return __end_cap_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16360
Howard Hinnantd1d27a42011-06-03 19:40:40361 _LIBCPP_INLINE_VISIBILITY
362 __vector_base()
363 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43364 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16365 ~__vector_base();
366
Howard Hinnantd1d27a42011-06-03 19:40:40367 _LIBCPP_INLINE_VISIBILITY
368 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
369 _LIBCPP_INLINE_VISIBILITY
370 size_type capacity() const _NOEXCEPT
371 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16372
Howard Hinnantd1d27a42011-06-03 19:40:40373 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32374 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16375
Howard Hinnantee6ccd02010-09-23 18:58:28376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16377 void __copy_assign_alloc(const __vector_base& __c)
378 {__copy_assign_alloc(__c, integral_constant<bool,
379 __alloc_traits::propagate_on_container_copy_assignment::value>());}
380
Howard Hinnantee6ccd02010-09-23 18:58:28381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16382 void __move_assign_alloc(__vector_base& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40383 _NOEXCEPT_(
384 !__alloc_traits::propagate_on_container_move_assignment::value ||
385 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16386 {__move_assign_alloc(__c, integral_constant<bool,
387 __alloc_traits::propagate_on_container_move_assignment::value>());}
388
Howard Hinnantee6ccd02010-09-23 18:58:28389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16390 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40391 _NOEXCEPT_(
392 !__alloc_traits::propagate_on_container_swap::value ||
393 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16394 {__swap_alloc(__x, __y, integral_constant<bool,
395 __alloc_traits::propagate_on_container_swap::value>());}
396private:
Howard Hinnantee6ccd02010-09-23 18:58:28397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16398 void __copy_assign_alloc(const __vector_base& __c, true_type)
399 {
400 if (__alloc() != __c.__alloc())
401 {
402 clear();
403 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
404 __begin_ = __end_ = __end_cap() = nullptr;
405 }
406 __alloc() = __c.__alloc();
407 }
408
Howard Hinnantee6ccd02010-09-23 18:58:28409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04410 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16411 {}
412
Howard Hinnantee6ccd02010-09-23 18:58:28413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31414 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40415 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16416 {
Howard Hinnant0949eed2011-06-30 21:18:19417 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16418 }
419
Howard Hinnantee6ccd02010-09-23 18:58:28420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04421 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40422 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16423 {}
424
Howard Hinnantee6ccd02010-09-23 18:58:28425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16426 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40427 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16428 {
Howard Hinnant0949eed2011-06-30 21:18:19429 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16430 swap(__x, __y);
431 }
Howard Hinnantee6ccd02010-09-23 18:58:28432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04433 static void __swap_alloc(allocator_type&, allocator_type&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40434 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16435 {}
436};
437
438template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00439inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16440void
Howard Hinnant2c39cbe2013-06-27 19:35:32441__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16442{
Howard Hinnantb0bfd9b2012-02-15 00:41:34443 while (__new_last != __end_)
Howard Hinnant2c39cbe2013-06-27 19:35:32444 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16445}
446
447template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00448inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16449__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnantd1d27a42011-06-03 19:40:40450 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32451 : __begin_(nullptr),
452 __end_(nullptr),
453 __end_cap_(nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16454{
455}
456
457template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00458inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16459__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32460 : __begin_(nullptr),
461 __end_(nullptr),
462 __end_cap_(nullptr, __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16463{
464}
465
466template <class _Tp, class _Allocator>
467__vector_base<_Tp, _Allocator>::~__vector_base()
468{
Howard Hinnant2c39cbe2013-06-27 19:35:32469 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16470 {
471 clear();
472 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
473 }
474}
475
476template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34477class _LIBCPP_TYPE_VIS_ONLY vector
Howard Hinnantbc8d3f92010-05-11 19:42:16478 : private __vector_base<_Tp, _Allocator>
479{
480private:
481 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow1f50f2d2014-05-08 14:14:06482 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant324bb032010-08-22 00:02:43483public:
Howard Hinnantbc8d3f92010-05-11 19:42:16484 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43485 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16486 typedef _Allocator allocator_type;
487 typedef typename __base::__alloc_traits __alloc_traits;
488 typedef typename __base::reference reference;
489 typedef typename __base::const_reference const_reference;
490 typedef typename __base::size_type size_type;
491 typedef typename __base::difference_type difference_type;
492 typedef typename __base::pointer pointer;
493 typedef typename __base::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16494 typedef __wrap_iter<pointer> iterator;
495 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19496 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
497 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16498
Howard Hinnant02d5e182013-03-26 19:04:56499 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
500 "Allocator::value_type must be same type as value_type");
501
Howard Hinnantd1d27a42011-06-03 19:40:40502 _LIBCPP_INLINE_VISIBILITY
503 vector()
504 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant7a563db2011-09-14 18:33:51505 {
Howard Hinnantabe26282011-09-16 17:29:17506#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51507 __get_db()->__insert_c(this);
508#endif
509 }
510 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
511 : __base(__a)
512 {
Howard Hinnantabe26282011-09-16 17:29:17513#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51514 __get_db()->__insert_c(this);
515#endif
516 }
Howard Hinnantbc8d3f92010-05-11 19:42:16517 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59518#if _LIBCPP_STD_VER > 11
519 explicit vector(size_type __n, const allocator_type& __a);
520#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16521 vector(size_type __n, const_reference __x);
522 vector(size_type __n, const_reference __x, const allocator_type& __a);
523 template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:54524 vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:16525 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32526 !__is_forward_iterator<_InputIterator>::value &&
527 is_constructible<
528 value_type,
Howard Hinnantde589f22013-09-21 21:13:54529 typename iterator_traits<_InputIterator>::reference>::value,
530 _InputIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16531 template <class _InputIterator>
532 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
533 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32534 !__is_forward_iterator<_InputIterator>::value &&
535 is_constructible<
536 value_type,
537 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16538 template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:54539 vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:32540 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
541 is_constructible<
542 value_type,
Howard Hinnantde589f22013-09-21 21:13:54543 typename iterator_traits<_ForwardIterator>::reference>::value,
544 _ForwardIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16545 template <class _ForwardIterator>
546 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32547 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
548 is_constructible<
549 value_type,
550 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02551#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16553 vector(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16555 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02556#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantabe26282011-09-16 17:29:17557#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantee6ccd02010-09-23 18:58:28558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7a563db2011-09-14 18:33:51559 ~vector()
560 {
561 __get_db()->__erase_c(this);
562 }
Howard Hinnantbc8d3f92010-05-11 19:42:16563#endif
564
565 vector(const vector& __x);
566 vector(const vector& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16568 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19569#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40571 vector(vector&& __x)
572 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16574 vector(vector&& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43575 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40576 vector& operator=(vector&& __x)
577 _NOEXCEPT_(
578 __alloc_traits::propagate_on_container_move_assignment::value &&
579 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19580#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02581#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16583 vector& operator=(initializer_list<value_type> __il)
584 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02585#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16586
587 template <class _InputIterator>
588 typename enable_if
589 <
590 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32591 !__is_forward_iterator<_InputIterator>::value &&
592 is_constructible<
593 value_type,
594 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16595 void
596 >::type
597 assign(_InputIterator __first, _InputIterator __last);
598 template <class _ForwardIterator>
599 typename enable_if
600 <
Howard Hinnant742fecb2013-03-28 17:44:32601 __is_forward_iterator<_ForwardIterator>::value &&
602 is_constructible<
603 value_type,
604 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16605 void
606 >::type
607 assign(_ForwardIterator __first, _ForwardIterator __last);
608
609 void assign(size_type __n, const_reference __u);
Howard Hinnante3e32912011-08-12 21:56:02610#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16612 void assign(initializer_list<value_type> __il)
613 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02614#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16615
Howard Hinnantd1d27a42011-06-03 19:40:40616 _LIBCPP_INLINE_VISIBILITY
617 allocator_type get_allocator() const _NOEXCEPT
618 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16619
Howard Hinnantd1d27a42011-06-03 19:40:40620 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
621 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
622 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
623 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16624
Howard Hinnantd1d27a42011-06-03 19:40:40625 _LIBCPP_INLINE_VISIBILITY
626 reverse_iterator rbegin() _NOEXCEPT
627 {return reverse_iterator(end());}
628 _LIBCPP_INLINE_VISIBILITY
629 const_reverse_iterator rbegin() const _NOEXCEPT
630 {return const_reverse_iterator(end());}
631 _LIBCPP_INLINE_VISIBILITY
632 reverse_iterator rend() _NOEXCEPT
633 {return reverse_iterator(begin());}
634 _LIBCPP_INLINE_VISIBILITY
635 const_reverse_iterator rend() const _NOEXCEPT
636 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16637
Howard Hinnantd1d27a42011-06-03 19:40:40638 _LIBCPP_INLINE_VISIBILITY
639 const_iterator cbegin() const _NOEXCEPT
640 {return begin();}
641 _LIBCPP_INLINE_VISIBILITY
642 const_iterator cend() const _NOEXCEPT
643 {return end();}
644 _LIBCPP_INLINE_VISIBILITY
645 const_reverse_iterator crbegin() const _NOEXCEPT
646 {return rbegin();}
647 _LIBCPP_INLINE_VISIBILITY
648 const_reverse_iterator crend() const _NOEXCEPT
649 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16650
Howard Hinnantd1d27a42011-06-03 19:40:40651 _LIBCPP_INLINE_VISIBILITY
652 size_type size() const _NOEXCEPT
653 {return static_cast<size_type>(this->__end_ - this->__begin_);}
654 _LIBCPP_INLINE_VISIBILITY
655 size_type capacity() const _NOEXCEPT
656 {return __base::capacity();}
657 _LIBCPP_INLINE_VISIBILITY
658 bool empty() const _NOEXCEPT
659 {return this->__begin_ == this->__end_;}
660 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16661 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40662 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16663
664 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
665 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
666 reference at(size_type __n);
667 const_reference at(size_type __n) const;
668
Howard Hinnant7a563db2011-09-14 18:33:51669 _LIBCPP_INLINE_VISIBILITY reference front()
670 {
671 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
672 return *this->__begin_;
673 }
674 _LIBCPP_INLINE_VISIBILITY const_reference front() const
675 {
676 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
677 return *this->__begin_;
678 }
679 _LIBCPP_INLINE_VISIBILITY reference back()
680 {
681 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
682 return *(this->__end_ - 1);
683 }
684 _LIBCPP_INLINE_VISIBILITY const_reference back() const
685 {
686 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
687 return *(this->__end_ - 1);
688 }
Howard Hinnantbc8d3f92010-05-11 19:42:16689
Howard Hinnantd1d27a42011-06-03 19:40:40690 _LIBCPP_INLINE_VISIBILITY
691 value_type* data() _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19692 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40693 _LIBCPP_INLINE_VISIBILITY
694 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19695 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16696
Howard Hinnant2d72b1e2010-12-17 14:46:43697 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19698#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb0bfd9b2012-02-15 00:41:34699 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19700#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16701 template <class... _Args>
702 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19703#endif // _LIBCPP_HAS_NO_VARIADICS
704#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16705 void pop_back();
706
707 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19708#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16709 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19710#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16711 template <class... _Args>
712 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19713#endif // _LIBCPP_HAS_NO_VARIADICS
714#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16715 iterator insert(const_iterator __position, size_type __n, const_reference __x);
716 template <class _InputIterator>
717 typename enable_if
718 <
719 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32720 !__is_forward_iterator<_InputIterator>::value &&
721 is_constructible<
722 value_type,
723 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16724 iterator
725 >::type
726 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
727 template <class _ForwardIterator>
728 typename enable_if
729 <
Howard Hinnant742fecb2013-03-28 17:44:32730 __is_forward_iterator<_ForwardIterator>::value &&
731 is_constructible<
732 value_type,
733 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16734 iterator
735 >::type
736 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02737#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28738 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16739 iterator insert(const_iterator __position, initializer_list<value_type> __il)
740 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02741#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16742
Howard Hinnant2d72b1e2010-12-17 14:46:43743 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16744 iterator erase(const_iterator __first, const_iterator __last);
745
Howard Hinnantd1d27a42011-06-03 19:40:40746 _LIBCPP_INLINE_VISIBILITY
747 void clear() _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51748 {
Marshall Clow1f50f2d2014-05-08 14:14:06749 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51750 __base::clear();
Marshall Clow1f50f2d2014-05-08 14:14:06751 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51752 __invalidate_all_iterators();
753 }
Howard Hinnantbc8d3f92010-05-11 19:42:16754
755 void resize(size_type __sz);
756 void resize(size_type __sz, const_reference __x);
757
Howard Hinnantd1d27a42011-06-03 19:40:40758 void swap(vector&)
759 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
760 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16761
762 bool __invariants() const;
763
Howard Hinnantabe26282011-09-16 17:29:17764#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51765
766 bool __dereferenceable(const const_iterator* __i) const;
767 bool __decrementable(const const_iterator* __i) const;
768 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
769 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
770
Howard Hinnantabe26282011-09-16 17:29:17771#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51772
Howard Hinnantbc8d3f92010-05-11 19:42:16773private:
Howard Hinnant2d72b1e2010-12-17 14:46:43774 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16775 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40776 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43777 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31778 void __construct_at_end(size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16779 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16780 template <class _ForwardIterator>
781 typename enable_if
782 <
783 __is_forward_iterator<_ForwardIterator>::value,
784 void
785 >::type
786 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16787 void __append(size_type __n);
788 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40790 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40792 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16793 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
794 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
795 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40796 void __move_assign(vector& __c, true_type)
797 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16798 void __move_assign(vector& __c, false_type);
Howard Hinnant7a563db2011-09-14 18:33:51799 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32800 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51801 {
Howard Hinnantabe26282011-09-16 17:29:17802#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51803 __c_node* __c = __get_db()->__find_c_and_lock(this);
804 for (__i_node** __p = __c->end_; __p != __c->beg_; )
805 {
806 --__p;
807 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
808 if (__i->base() > __new_last)
809 {
810 (*__p)->__c_ = nullptr;
811 if (--__c->end_ != __p)
812 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
813 }
814 }
815 __get_db()->unlock();
816#endif
Marshall Clow1f50f2d2014-05-08 14:14:06817 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51818 __base::__destruct_at_end(__new_last);
Marshall Clow1f50f2d2014-05-08 14:14:06819 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51820 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34821 template <class _Up>
822 void
823#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
824 __push_back_slow_path(_Up&& __x);
825#else
826 __push_back_slow_path(_Up& __x);
827#endif
Howard Hinnant0438ea22012-02-26 15:30:12828#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
829 template <class... _Args>
830 void
831 __emplace_back_slow_path(_Args&&... __args);
832#endif
Marshall Clow1f50f2d2014-05-08 14:14:06833 // The following functions are no-ops outside of AddressSanitizer mode.
834 // We call annotatations only for the default Allocator because other allocators
835 // may not meet the AddressSanitizer alignment constraints.
836 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
837 void __annotate_contiguous_container
Kostya Serebryany497f9122014-09-02 23:43:38838 (const void *__beg, const void *__end, const void *__old_mid, const void *__new_mid) const
Marshall Clow1f50f2d2014-05-08 14:14:06839 {
840#ifndef _LIBCPP_HAS_NO_ASAN
841 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
842 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
843#endif
844 }
845
Kostya Serebryany497f9122014-09-02 23:43:38846 void __annotate_new(size_type __current_size) const
Marshall Clow1f50f2d2014-05-08 14:14:06847 {
848 __annotate_contiguous_container(data(), data() + capacity(),
849 data() + capacity(), data() + __current_size);
850 }
Kostya Serebryany497f9122014-09-02 23:43:38851 void __annotate_delete() const
Marshall Clow1f50f2d2014-05-08 14:14:06852 {
853 __annotate_contiguous_container(data(), data() + capacity(),
854 data() + size(), data() + capacity());
855 }
Kostya Serebryany497f9122014-09-02 23:43:38856 void __annotate_increase(size_type __n) const
Marshall Clow1f50f2d2014-05-08 14:14:06857 {
858 __annotate_contiguous_container(data(), data() + capacity(),
859 data() + size(), data() + size() + __n);
860 }
Kostya Serebryany497f9122014-09-02 23:43:38861 void __annotate_shrink(size_type __old_size) const
Marshall Clow1f50f2d2014-05-08 14:14:06862 {
863 __annotate_contiguous_container(data(), data() + capacity(),
864 data() + __old_size, data() + size());
865 }
Kostya Serebryany497f9122014-09-02 23:43:38866 // The annotation for size increase should happen before the actual increase,
867 // but if an exception is thrown after that the annotation has to be undone.
868 struct __RAII_IncreaseAnnotator {
869 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
870 : __commit(false), __v(__v), __n(__n) {
871 __v.__annotate_increase(__n);
872 }
873 void __done() { __commit = true; }
874 ~__RAII_IncreaseAnnotator() {
875 if (__commit) return;
876 __v.__annotate_shrink(__v.size() + __n);
877 }
878 bool __commit;
879 size_type __n;
880 const vector &__v;
881 };
Howard Hinnantbc8d3f92010-05-11 19:42:16882};
883
884template <class _Tp, class _Allocator>
885void
886vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
887{
Marshall Clow1f50f2d2014-05-08 14:14:06888 __annotate_delete();
Howard Hinnantb0bfd9b2012-02-15 00:41:34889 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnant0949eed2011-06-30 21:18:19890 _VSTD::swap(this->__begin_, __v.__begin_);
891 _VSTD::swap(this->__end_, __v.__end_);
892 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16893 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06894 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16895 __invalidate_all_iterators();
896}
897
898template <class _Tp, class _Allocator>
899typename vector<_Tp, _Allocator>::pointer
900vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
901{
Marshall Clow1f50f2d2014-05-08 14:14:06902 __annotate_delete();
Howard Hinnantbc8d3f92010-05-11 19:42:16903 pointer __r = __v.__begin_;
Howard Hinnantb0bfd9b2012-02-15 00:41:34904 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
905 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnant0949eed2011-06-30 21:18:19906 _VSTD::swap(this->__begin_, __v.__begin_);
907 _VSTD::swap(this->__end_, __v.__end_);
908 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16909 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06910 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16911 __invalidate_all_iterators();
912 return __r;
913}
914
915// Allocate space for __n objects
916// throws length_error if __n > max_size()
917// throws (probably bad_alloc) if memory run out
918// Precondition: __begin_ == __end_ == __end_cap() == 0
919// Precondition: __n > 0
920// Postcondition: capacity() == __n
921// Postcondition: size() == 0
922template <class _Tp, class _Allocator>
923void
924vector<_Tp, _Allocator>::allocate(size_type __n)
925{
926 if (__n > max_size())
927 this->__throw_length_error();
928 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
929 this->__end_cap() = this->__begin_ + __n;
Marshall Clow1f50f2d2014-05-08 14:14:06930 __annotate_new(0);
Howard Hinnantbc8d3f92010-05-11 19:42:16931}
932
933template <class _Tp, class _Allocator>
934void
Howard Hinnantd1d27a42011-06-03 19:40:40935vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16936{
Howard Hinnant2c39cbe2013-06-27 19:35:32937 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16938 {
939 clear();
940 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant2c39cbe2013-06-27 19:35:32941 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16942 }
943}
944
945template <class _Tp, class _Allocator>
946typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40947vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16948{
Sean Hunt110b8bf2011-07-29 23:31:58949 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:16950}
951
952// Precondition: __new_size > capacity()
953template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00954inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16955typename vector<_Tp, _Allocator>::size_type
956vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
957{
958 const size_type __ms = max_size();
959 if (__new_size > __ms)
960 this->__throw_length_error();
961 const size_type __cap = capacity();
962 if (__cap >= __ms / 2)
963 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58964 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16965}
966
967// Default constructs __n objects starting at __end_
968// throws if construction throws
969// Precondition: __n > 0
970// Precondition: size() + __n <= capacity()
971// Postcondition: size() == size() + __n
972template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16973void
974vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
975{
Howard Hinnantbc8d3f92010-05-11 19:42:16976 allocator_type& __a = this->__alloc();
Howard Hinnantbc8d3f92010-05-11 19:42:16977 do
978 {
Kostya Serebryany497f9122014-09-02 23:43:38979 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19980 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16981 ++this->__end_;
982 --__n;
Kostya Serebryany497f9122014-09-02 23:43:38983 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16984 } while (__n > 0);
985}
986
Howard Hinnantbc8d3f92010-05-11 19:42:16987// Copy constructs __n objects starting at __end_ from __x
988// throws if construction throws
989// Precondition: __n > 0
990// Precondition: size() + __n <= capacity()
991// Postcondition: size() == old size() + __n
992// Postcondition: [i] == __x for all i in [size() - __n, __n)
993template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00994inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16995void
996vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
997{
Howard Hinnantbc8d3f92010-05-11 19:42:16998 allocator_type& __a = this->__alloc();
Howard Hinnantbc8d3f92010-05-11 19:42:16999 do
1000 {
Kostya Serebryany497f9122014-09-02 23:43:381001 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:191002 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:161003 ++this->__end_;
1004 --__n;
Kostya Serebryany497f9122014-09-02 23:43:381005 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161006 } while (__n > 0);
1007}
1008
1009template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:161010template <class _ForwardIterator>
1011typename enable_if
1012<
1013 __is_forward_iterator<_ForwardIterator>::value,
1014 void
1015>::type
1016vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
1017{
1018 allocator_type& __a = this->__alloc();
1019 for (; __first != __last; ++__first)
1020 {
Kostya Serebryany497f9122014-09-02 23:43:381021 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:191022 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Kostya Serebryany497f9122014-09-02 23:43:381023 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161024 ++this->__end_;
1025 }
1026}
1027
1028// Default constructs __n objects starting at __end_
1029// throws if construction throws
1030// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:401031// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:161032template <class _Tp, class _Allocator>
1033void
1034vector<_Tp, _Allocator>::__append(size_type __n)
1035{
1036 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1037 this->__construct_at_end(__n);
1038 else
1039 {
1040 allocator_type& __a = this->__alloc();
1041 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1042 __v.__construct_at_end(__n);
1043 __swap_out_circular_buffer(__v);
1044 }
1045}
1046
1047// Default constructs __n objects starting at __end_
1048// throws if construction throws
1049// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:401050// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:161051template <class _Tp, class _Allocator>
1052void
1053vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1054{
1055 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1056 this->__construct_at_end(__n, __x);
1057 else
1058 {
1059 allocator_type& __a = this->__alloc();
1060 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1061 __v.__construct_at_end(__n, __x);
1062 __swap_out_circular_buffer(__v);
1063 }
1064}
1065
1066template <class _Tp, class _Allocator>
1067vector<_Tp, _Allocator>::vector(size_type __n)
1068{
Howard Hinnant0442b122011-09-16 18:41:291069#if _LIBCPP_DEBUG_LEVEL >= 2
1070 __get_db()->__insert_c(this);
1071#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161072 if (__n > 0)
1073 {
1074 allocate(__n);
1075 __construct_at_end(__n);
1076 }
1077}
1078
Marshall Clowa49a2c92013-09-14 00:47:591079#if _LIBCPP_STD_VER > 11
1080template <class _Tp, class _Allocator>
1081vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1082 : __base(__a)
1083{
1084#if _LIBCPP_DEBUG_LEVEL >= 2
1085 __get_db()->__insert_c(this);
1086#endif
1087 if (__n > 0)
1088 {
1089 allocate(__n);
1090 __construct_at_end(__n);
1091 }
1092}
1093#endif
1094
Howard Hinnantbc8d3f92010-05-11 19:42:161095template <class _Tp, class _Allocator>
1096vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1097{
Howard Hinnant0442b122011-09-16 18:41:291098#if _LIBCPP_DEBUG_LEVEL >= 2
1099 __get_db()->__insert_c(this);
1100#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161101 if (__n > 0)
1102 {
1103 allocate(__n);
1104 __construct_at_end(__n, __x);
1105 }
1106}
1107
1108template <class _Tp, class _Allocator>
1109vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1110 : __base(__a)
1111{
Howard Hinnant0442b122011-09-16 18:41:291112#if _LIBCPP_DEBUG_LEVEL >= 2
1113 __get_db()->__insert_c(this);
1114#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161115 if (__n > 0)
1116 {
1117 allocate(__n);
1118 __construct_at_end(__n, __x);
1119 }
1120}
1121
1122template <class _Tp, class _Allocator>
1123template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:541124vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:161125 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:321126 !__is_forward_iterator<_InputIterator>::value &&
1127 is_constructible<
1128 value_type,
Howard Hinnantde589f22013-09-21 21:13:541129 typename iterator_traits<_InputIterator>::reference>::value,
1130 _InputIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:161131{
Howard Hinnantabe26282011-09-16 17:29:171132#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511133 __get_db()->__insert_c(this);
1134#endif
Howard Hinnant0442b122011-09-16 18:41:291135 for (; __first != __last; ++__first)
1136 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:161137}
1138
1139template <class _Tp, class _Allocator>
1140template <class _InputIterator>
1141vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1142 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:321143 !__is_forward_iterator<_InputIterator>::value &&
1144 is_constructible<
1145 value_type,
1146 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:161147 : __base(__a)
1148{
Howard Hinnantabe26282011-09-16 17:29:171149#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511150 __get_db()->__insert_c(this);
1151#endif
Howard Hinnant0442b122011-09-16 18:41:291152 for (; __first != __last; ++__first)
1153 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:161154}
1155
1156template <class _Tp, class _Allocator>
1157template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:541158vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:321159 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1160 is_constructible<
1161 value_type,
Howard Hinnantde589f22013-09-21 21:13:541162 typename iterator_traits<_ForwardIterator>::reference>::value,
1163 _ForwardIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:161164{
Howard Hinnant0442b122011-09-16 18:41:291165#if _LIBCPP_DEBUG_LEVEL >= 2
1166 __get_db()->__insert_c(this);
1167#endif
Howard Hinnant0949eed2011-06-30 21:18:191168 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:161169 if (__n > 0)
1170 {
1171 allocate(__n);
1172 __construct_at_end(__first, __last);
1173 }
1174}
1175
1176template <class _Tp, class _Allocator>
1177template <class _ForwardIterator>
1178vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:321179 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1180 is_constructible<
1181 value_type,
1182 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:161183 : __base(__a)
1184{
Howard Hinnant0442b122011-09-16 18:41:291185#if _LIBCPP_DEBUG_LEVEL >= 2
1186 __get_db()->__insert_c(this);
1187#endif
Howard Hinnant0949eed2011-06-30 21:18:191188 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:161189 if (__n > 0)
1190 {
1191 allocate(__n);
1192 __construct_at_end(__first, __last);
1193 }
1194}
1195
1196template <class _Tp, class _Allocator>
1197vector<_Tp, _Allocator>::vector(const vector& __x)
1198 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1199{
Howard Hinnant0442b122011-09-16 18:41:291200#if _LIBCPP_DEBUG_LEVEL >= 2
1201 __get_db()->__insert_c(this);
1202#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161203 size_type __n = __x.size();
1204 if (__n > 0)
1205 {
1206 allocate(__n);
1207 __construct_at_end(__x.__begin_, __x.__end_);
1208 }
1209}
1210
1211template <class _Tp, class _Allocator>
1212vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1213 : __base(__a)
1214{
Howard Hinnant0442b122011-09-16 18:41:291215#if _LIBCPP_DEBUG_LEVEL >= 2
1216 __get_db()->__insert_c(this);
1217#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161218 size_type __n = __x.size();
1219 if (__n > 0)
1220 {
1221 allocate(__n);
1222 __construct_at_end(__x.__begin_, __x.__end_);
1223 }
1224}
1225
Howard Hinnant73d21a42010-09-04 23:28:191226#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161227
1228template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001229inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161230vector<_Tp, _Allocator>::vector(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:401231 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:191232 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:161233{
Howard Hinnantabe26282011-09-16 17:29:171234#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511235 __get_db()->__insert_c(this);
Howard Hinnante6125bd2011-09-19 16:34:291236 __get_db()->swap(this, &__x);
Howard Hinnant7a563db2011-09-14 18:33:511237#endif
Howard Hinnant0442b122011-09-16 18:41:291238 this->__begin_ = __x.__begin_;
1239 this->__end_ = __x.__end_;
1240 this->__end_cap() = __x.__end_cap();
Howard Hinnant2c39cbe2013-06-27 19:35:321241 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:161242}
1243
1244template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001245inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161246vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1247 : __base(__a)
1248{
Howard Hinnant0442b122011-09-16 18:41:291249#if _LIBCPP_DEBUG_LEVEL >= 2
1250 __get_db()->__insert_c(this);
1251#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161252 if (__a == __x.__alloc())
1253 {
1254 this->__begin_ = __x.__begin_;
1255 this->__end_ = __x.__end_;
1256 this->__end_cap() = __x.__end_cap();
1257 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:291258#if _LIBCPP_DEBUG_LEVEL >= 2
1259 __get_db()->swap(this, &__x);
1260#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161261 }
1262 else
1263 {
Howard Hinnant99968442011-11-29 18:15:501264 typedef move_iterator<iterator> _Ip;
1265 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:161266 }
1267}
1268
Howard Hinnante3e32912011-08-12 21:56:021269#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1270
Howard Hinnantbc8d3f92010-05-11 19:42:161271template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001272inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161273vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1274{
Howard Hinnant0442b122011-09-16 18:41:291275#if _LIBCPP_DEBUG_LEVEL >= 2
1276 __get_db()->__insert_c(this);
1277#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161278 if (__il.size() > 0)
1279 {
1280 allocate(__il.size());
1281 __construct_at_end(__il.begin(), __il.end());
1282 }
1283}
1284
1285template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001286inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161287vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1288 : __base(__a)
1289{
Howard Hinnant0442b122011-09-16 18:41:291290#if _LIBCPP_DEBUG_LEVEL >= 2
1291 __get_db()->__insert_c(this);
1292#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161293 if (__il.size() > 0)
1294 {
1295 allocate(__il.size());
1296 __construct_at_end(__il.begin(), __il.end());
1297 }
1298}
1299
Howard Hinnante3e32912011-08-12 21:56:021300#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1301
Howard Hinnantbc8d3f92010-05-11 19:42:161302template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001303inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161304vector<_Tp, _Allocator>&
1305vector<_Tp, _Allocator>::operator=(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:401306 _NOEXCEPT_(
1307 __alloc_traits::propagate_on_container_move_assignment::value &&
1308 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:161309{
1310 __move_assign(__x, integral_constant<bool,
1311 __alloc_traits::propagate_on_container_move_assignment::value>());
1312 return *this;
1313}
1314
1315template <class _Tp, class _Allocator>
1316void
1317vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1318{
1319 if (__base::__alloc() != __c.__alloc())
1320 {
Howard Hinnant99968442011-11-29 18:15:501321 typedef move_iterator<iterator> _Ip;
1322 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:161323 }
1324 else
1325 __move_assign(__c, true_type());
1326}
1327
1328template <class _Tp, class _Allocator>
1329void
1330vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:401331 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:161332{
1333 deallocate();
Marshall Clow3c2eac62014-07-21 15:11:131334 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantbc8d3f92010-05-11 19:42:161335 this->__begin_ = __c.__begin_;
1336 this->__end_ = __c.__end_;
1337 this->__end_cap() = __c.__end_cap();
Howard Hinnantbc8d3f92010-05-11 19:42:161338 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:291339#if _LIBCPP_DEBUG_LEVEL >= 2
1340 __get_db()->swap(this, &__c);
1341#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161342}
1343
Howard Hinnant73d21a42010-09-04 23:28:191344#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161345
1346template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001347inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161348vector<_Tp, _Allocator>&
1349vector<_Tp, _Allocator>::operator=(const vector& __x)
1350{
1351 if (this != &__x)
1352 {
1353 __base::__copy_assign_alloc(__x);
1354 assign(__x.__begin_, __x.__end_);
1355 }
1356 return *this;
1357}
1358
1359template <class _Tp, class _Allocator>
1360template <class _InputIterator>
1361typename enable_if
1362<
1363 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:321364 !__is_forward_iterator<_InputIterator>::value &&
1365 is_constructible<
1366 _Tp,
1367 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:161368 void
1369>::type
1370vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1371{
1372 clear();
1373 for (; __first != __last; ++__first)
1374 push_back(*__first);
1375}
1376
1377template <class _Tp, class _Allocator>
1378template <class _ForwardIterator>
1379typename enable_if
1380<
Howard Hinnant742fecb2013-03-28 17:44:321381 __is_forward_iterator<_ForwardIterator>::value &&
1382 is_constructible<
1383 _Tp,
1384 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:161385 void
1386>::type
1387vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1388{
Howard Hinnant0949eed2011-06-30 21:18:191389 typename iterator_traits<_ForwardIterator>::difference_type __new_size = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:161390 if (static_cast<size_type>(__new_size) <= capacity())
1391 {
1392 _ForwardIterator __mid = __last;
1393 bool __growing = false;
1394 if (static_cast<size_type>(__new_size) > size())
1395 {
1396 __growing = true;
1397 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:191398 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:161399 }
Howard Hinnant0949eed2011-06-30 21:18:191400 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:161401 if (__growing)
1402 __construct_at_end(__mid, __last);
1403 else
1404 this->__destruct_at_end(__m);
1405 }
1406 else
1407 {
1408 deallocate();
1409 allocate(__recommend(static_cast<size_type>(__new_size)));
1410 __construct_at_end(__first, __last);
1411 }
1412}
1413
1414template <class _Tp, class _Allocator>
1415void
1416vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1417{
1418 if (__n <= capacity())
1419 {
1420 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:191421 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:161422 if (__n > __s)
1423 __construct_at_end(__n - __s, __u);
1424 else
Howard Hinnantadff4892010-05-24 17:49:411425 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:161426 }
1427 else
1428 {
1429 deallocate();
1430 allocate(__recommend(static_cast<size_type>(__n)));
1431 __construct_at_end(__n, __u);
1432 }
1433}
1434
Howard Hinnant324bb032010-08-22 00:02:431435template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001436inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161437typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:401438vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161439{
Howard Hinnantabe26282011-09-16 17:29:171440#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:161441 return iterator(this, __p);
1442#else
1443 return iterator(__p);
1444#endif
1445}
1446
Howard Hinnant324bb032010-08-22 00:02:431447template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001448inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161449typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:401450vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161451{
Howard Hinnantabe26282011-09-16 17:29:171452#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:161453 return const_iterator(this, __p);
1454#else
1455 return const_iterator(__p);
1456#endif
1457}
1458
Howard Hinnant324bb032010-08-22 00:02:431459template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001460inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161461typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:401462vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161463{
1464 return __make_iter(this->__begin_);
1465}
1466
Howard Hinnant324bb032010-08-22 00:02:431467template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001468inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161469typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:401470vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161471{
1472 return __make_iter(this->__begin_);
1473}
1474
Howard Hinnant324bb032010-08-22 00:02:431475template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001476inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161477typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:401478vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161479{
1480 return __make_iter(this->__end_);
1481}
1482
Howard Hinnant324bb032010-08-22 00:02:431483template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001484inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161485typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:401486vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161487{
1488 return __make_iter(this->__end_);
1489}
1490
Howard Hinnant324bb032010-08-22 00:02:431491template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001492inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161493typename vector<_Tp, _Allocator>::reference
1494vector<_Tp, _Allocator>::operator[](size_type __n)
1495{
Howard Hinnant7a563db2011-09-14 18:33:511496 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:161497 return this->__begin_[__n];
1498}
1499
Howard Hinnant324bb032010-08-22 00:02:431500template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001501inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161502typename vector<_Tp, _Allocator>::const_reference
1503vector<_Tp, _Allocator>::operator[](size_type __n) const
1504{
Howard Hinnant7a563db2011-09-14 18:33:511505 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:161506 return this->__begin_[__n];
1507}
1508
Howard Hinnant324bb032010-08-22 00:02:431509template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:161510typename vector<_Tp, _Allocator>::reference
1511vector<_Tp, _Allocator>::at(size_type __n)
1512{
1513 if (__n >= size())
1514 this->__throw_out_of_range();
1515 return this->__begin_[__n];
1516}
1517
Howard Hinnant324bb032010-08-22 00:02:431518template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:161519typename vector<_Tp, _Allocator>::const_reference
1520vector<_Tp, _Allocator>::at(size_type __n) const
1521{
1522 if (__n >= size())
1523 this->__throw_out_of_range();
1524 return this->__begin_[__n];
1525}
1526
1527template <class _Tp, class _Allocator>
1528void
1529vector<_Tp, _Allocator>::reserve(size_type __n)
1530{
1531 if (__n > capacity())
1532 {
1533 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:401534 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:161535 __swap_out_circular_buffer(__v);
1536 }
1537}
1538
1539template <class _Tp, class _Allocator>
1540void
Howard Hinnantd1d27a42011-06-03 19:40:401541vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161542{
1543 if (capacity() > size())
1544 {
1545#ifndef _LIBCPP_NO_EXCEPTIONS
1546 try
1547 {
Howard Hinnant324bb032010-08-22 00:02:431548#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161549 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:401550 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:161551 __swap_out_circular_buffer(__v);
1552#ifndef _LIBCPP_NO_EXCEPTIONS
1553 }
1554 catch (...)
1555 {
1556 }
Howard Hinnant324bb032010-08-22 00:02:431557#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161558 }
1559}
1560
1561template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:341562template <class _Up>
1563void
1564#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1565vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1566#else
1567vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1568#endif
1569{
1570 allocator_type& __a = this->__alloc();
1571 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1572 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnantf619e232013-01-11 20:36:591573 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1574 __v.__end_++;
Howard Hinnantb0bfd9b2012-02-15 00:41:341575 __swap_out_circular_buffer(__v);
1576}
1577
1578template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001579inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161580void
1581vector<_Tp, _Allocator>::push_back(const_reference __x)
1582{
Howard Hinnantb0bfd9b2012-02-15 00:41:341583 if (this->__end_ != this->__end_cap())
Howard Hinnantbc8d3f92010-05-11 19:42:161584 {
Kostya Serebryany497f9122014-09-02 23:43:381585 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:161586 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191587 _VSTD::__to_raw_pointer(this->__end_), __x);
Kostya Serebryany497f9122014-09-02 23:43:381588 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161589 ++this->__end_;
1590 }
1591 else
Howard Hinnantb0bfd9b2012-02-15 00:41:341592 __push_back_slow_path(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:161593}
1594
Howard Hinnant73d21a42010-09-04 23:28:191595#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161596
1597template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001598inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161599void
1600vector<_Tp, _Allocator>::push_back(value_type&& __x)
1601{
1602 if (this->__end_ < this->__end_cap())
1603 {
Kostya Serebryany497f9122014-09-02 23:43:381604 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:161605 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191606 _VSTD::__to_raw_pointer(this->__end_),
1607 _VSTD::move(__x));
Kostya Serebryany497f9122014-09-02 23:43:381608 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161609 ++this->__end_;
1610 }
1611 else
Howard Hinnantb0bfd9b2012-02-15 00:41:341612 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:161613}
1614
Howard Hinnant73d21a42010-09-04 23:28:191615#ifndef _LIBCPP_HAS_NO_VARIADICS
1616
Howard Hinnantbc8d3f92010-05-11 19:42:161617template <class _Tp, class _Allocator>
1618template <class... _Args>
1619void
Howard Hinnant0438ea22012-02-26 15:30:121620vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1621{
1622 allocator_type& __a = this->__alloc();
1623 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1624// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantf619e232013-01-11 20:36:591625 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1626 __v.__end_++;
Howard Hinnant0438ea22012-02-26 15:30:121627 __swap_out_circular_buffer(__v);
1628}
1629
1630template <class _Tp, class _Allocator>
1631template <class... _Args>
Howard Hinnant1e564242013-10-04 22:09:001632inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0438ea22012-02-26 15:30:121633void
Howard Hinnantbc8d3f92010-05-11 19:42:161634vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1635{
1636 if (this->__end_ < this->__end_cap())
1637 {
Kostya Serebryany497f9122014-09-02 23:43:381638 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:161639 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191640 _VSTD::__to_raw_pointer(this->__end_),
1641 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany497f9122014-09-02 23:43:381642 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161643 ++this->__end_;
1644 }
1645 else
Howard Hinnant0438ea22012-02-26 15:30:121646 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:161647}
1648
Howard Hinnant73d21a42010-09-04 23:28:191649#endif // _LIBCPP_HAS_NO_VARIADICS
1650#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161651
1652template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001653inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161654void
1655vector<_Tp, _Allocator>::pop_back()
1656{
Howard Hinnant7a563db2011-09-14 18:33:511657 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:161658 this->__destruct_at_end(this->__end_ - 1);
1659}
1660
1661template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001662inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161663typename vector<_Tp, _Allocator>::iterator
1664vector<_Tp, _Allocator>::erase(const_iterator __position)
1665{
Howard Hinnantabe26282011-09-16 17:29:171666#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511667 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1668 "vector::erase(iterator) called with an iterator not"
1669 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171670#endif
Howard Hinnant782da332013-03-25 22:12:261671 _LIBCPP_ASSERT(__position != end(),
1672 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant2c39cbe2013-06-27 19:35:321673 difference_type __ps = __position - cbegin();
1674 pointer __p = this->__begin_ + __ps;
Howard Hinnantbc8d3f92010-05-11 19:42:161675 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:191676 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:161677 return __r;
1678}
1679
1680template <class _Tp, class _Allocator>
1681typename vector<_Tp, _Allocator>::iterator
1682vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1683{
Howard Hinnantabe26282011-09-16 17:29:171684#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511685 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1686 "vector::erase(iterator, iterator) called with an iterator not"
1687 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171688#endif
Howard Hinnant7a563db2011-09-14 18:33:511689 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:161690 pointer __p = this->__begin_ + (__first - begin());
1691 iterator __r = __make_iter(__p);
Howard Hinnantb4e67cf2013-04-18 15:02:571692 if (__first != __last)
1693 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:161694 return __r;
1695}
1696
1697template <class _Tp, class _Allocator>
1698void
1699vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1700{
1701 pointer __old_last = this->__end_;
1702 difference_type __n = __old_last - __to;
1703 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1704 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191705 _VSTD::__to_raw_pointer(this->__end_),
1706 _VSTD::move(*__i));
1707 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:161708}
1709
1710template <class _Tp, class _Allocator>
1711typename vector<_Tp, _Allocator>::iterator
1712vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1713{
Howard Hinnantabe26282011-09-16 17:29:171714#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511715 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1716 "vector::insert(iterator, x) called with an iterator not"
1717 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171718#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161719 pointer __p = this->__begin_ + (__position - begin());
1720 if (this->__end_ < this->__end_cap())
1721 {
Kostya Serebryany497f9122014-09-02 23:43:381722 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:161723 if (__p == this->__end_)
1724 {
1725 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191726 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:161727 ++this->__end_;
1728 }
1729 else
1730 {
1731 __move_range(__p, this->__end_, __p + 1);
1732 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1733 if (__p <= __xr && __xr < this->__end_)
1734 ++__xr;
1735 *__p = *__xr;
1736 }
Kostya Serebryany497f9122014-09-02 23:43:381737 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161738 }
1739 else
1740 {
1741 allocator_type& __a = this->__alloc();
1742 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1743 __v.push_back(__x);
1744 __p = __swap_out_circular_buffer(__v, __p);
1745 }
1746 return __make_iter(__p);
1747}
1748
Howard Hinnant73d21a42010-09-04 23:28:191749#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161750
1751template <class _Tp, class _Allocator>
1752typename vector<_Tp, _Allocator>::iterator
1753vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1754{
Howard Hinnantabe26282011-09-16 17:29:171755#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511756 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1757 "vector::insert(iterator, x) called with an iterator not"
1758 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171759#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161760 pointer __p = this->__begin_ + (__position - begin());
1761 if (this->__end_ < this->__end_cap())
1762 {
Kostya Serebryany497f9122014-09-02 23:43:381763 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:161764 if (__p == this->__end_)
1765 {
1766 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191767 _VSTD::__to_raw_pointer(this->__end_),
1768 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:161769 ++this->__end_;
1770 }
1771 else
1772 {
1773 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:191774 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:161775 }
Kostya Serebryany497f9122014-09-02 23:43:381776 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161777 }
1778 else
1779 {
1780 allocator_type& __a = this->__alloc();
1781 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:191782 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:161783 __p = __swap_out_circular_buffer(__v, __p);
1784 }
1785 return __make_iter(__p);
1786}
1787
Howard Hinnant73d21a42010-09-04 23:28:191788#ifndef _LIBCPP_HAS_NO_VARIADICS
1789
Howard Hinnantbc8d3f92010-05-11 19:42:161790template <class _Tp, class _Allocator>
1791template <class... _Args>
1792typename vector<_Tp, _Allocator>::iterator
1793vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1794{
Howard Hinnantabe26282011-09-16 17:29:171795#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511796 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1797 "vector::emplace(iterator, x) called with an iterator not"
1798 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171799#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161800 pointer __p = this->__begin_ + (__position - begin());
1801 if (this->__end_ < this->__end_cap())
1802 {
Kostya Serebryany497f9122014-09-02 23:43:381803 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:161804 if (__p == this->__end_)
1805 {
1806 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191807 _VSTD::__to_raw_pointer(this->__end_),
1808 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:161809 ++this->__end_;
1810 }
1811 else
1812 {
Howard Hinnanta58402a2012-07-08 23:23:041813 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:161814 __move_range(__p, this->__end_, __p + 1);
Howard Hinnanta58402a2012-07-08 23:23:041815 *__p = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:161816 }
Kostya Serebryany497f9122014-09-02 23:43:381817 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161818 }
1819 else
1820 {
1821 allocator_type& __a = this->__alloc();
1822 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:191823 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:161824 __p = __swap_out_circular_buffer(__v, __p);
1825 }
1826 return __make_iter(__p);
1827}
1828
Howard Hinnant73d21a42010-09-04 23:28:191829#endif // _LIBCPP_HAS_NO_VARIADICS
1830#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161831
1832template <class _Tp, class _Allocator>
1833typename vector<_Tp, _Allocator>::iterator
1834vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1835{
Howard Hinnantabe26282011-09-16 17:29:171836#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511837 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1838 "vector::insert(iterator, n, x) called with an iterator not"
1839 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171840#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161841 pointer __p = this->__begin_ + (__position - begin());
1842 if (__n > 0)
1843 {
1844 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1845 {
1846 size_type __old_n = __n;
1847 pointer __old_last = this->__end_;
1848 if (__n > static_cast<size_type>(this->__end_ - __p))
1849 {
1850 size_type __cx = __n - (this->__end_ - __p);
1851 __construct_at_end(__cx, __x);
1852 __n -= __cx;
1853 }
1854 if (__n > 0)
1855 {
Kostya Serebryany497f9122014-09-02 23:43:381856 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:161857 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:381858 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161859 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1860 if (__p <= __xr && __xr < this->__end_)
1861 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:191862 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:161863 }
1864 }
1865 else
1866 {
1867 allocator_type& __a = this->__alloc();
1868 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1869 __v.__construct_at_end(__n, __x);
1870 __p = __swap_out_circular_buffer(__v, __p);
1871 }
1872 }
1873 return __make_iter(__p);
1874}
1875
1876template <class _Tp, class _Allocator>
1877template <class _InputIterator>
1878typename enable_if
1879<
1880 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:321881 !__is_forward_iterator<_InputIterator>::value &&
1882 is_constructible<
1883 _Tp,
1884 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:161885 typename vector<_Tp, _Allocator>::iterator
1886>::type
1887vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1888{
Howard Hinnantabe26282011-09-16 17:29:171889#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511890 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1891 "vector::insert(iterator, range) called with an iterator not"
1892 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171893#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161894 difference_type __off = __position - begin();
1895 pointer __p = this->__begin_ + __off;
1896 allocator_type& __a = this->__alloc();
1897 pointer __old_last = this->__end_;
1898 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1899 {
Howard Hinnant0949eed2011-06-30 21:18:191900 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:161901 *__first);
1902 ++this->__end_;
1903 }
1904 __split_buffer<value_type, allocator_type&> __v(__a);
1905 if (__first != __last)
1906 {
1907#ifndef _LIBCPP_NO_EXCEPTIONS
1908 try
1909 {
Howard Hinnant324bb032010-08-22 00:02:431910#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161911 __v.__construct_at_end(__first, __last);
1912 difference_type __old_size = __old_last - this->__begin_;
1913 difference_type __old_p = __p - this->__begin_;
1914 reserve(__recommend(size() + __v.size()));
1915 __p = this->__begin_ + __old_p;
1916 __old_last = this->__begin_ + __old_size;
1917#ifndef _LIBCPP_NO_EXCEPTIONS
1918 }
1919 catch (...)
1920 {
1921 erase(__make_iter(__old_last), end());
1922 throw;
1923 }
Howard Hinnant324bb032010-08-22 00:02:431924#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161925 }
Howard Hinnant0949eed2011-06-30 21:18:191926 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant0442b122011-09-16 18:41:291927 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1928 make_move_iterator(__v.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:161929 return begin() + __off;
1930}
1931
1932template <class _Tp, class _Allocator>
1933template <class _ForwardIterator>
1934typename enable_if
1935<
Howard Hinnant742fecb2013-03-28 17:44:321936 __is_forward_iterator<_ForwardIterator>::value &&
1937 is_constructible<
1938 _Tp,
1939 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:161940 typename vector<_Tp, _Allocator>::iterator
1941>::type
1942vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1943{
Howard Hinnantabe26282011-09-16 17:29:171944#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511945 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1946 "vector::insert(iterator, range) called with an iterator not"
1947 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171948#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161949 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:191950 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:161951 if (__n > 0)
1952 {
1953 if (__n <= this->__end_cap() - this->__end_)
1954 {
1955 size_type __old_n = __n;
1956 pointer __old_last = this->__end_;
1957 _ForwardIterator __m = __last;
1958 difference_type __dx = this->__end_ - __p;
1959 if (__n > __dx)
1960 {
1961 __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:191962 _VSTD::advance(__m, this->__end_ - __p);
Howard Hinnantbc8d3f92010-05-11 19:42:161963 __construct_at_end(__m, __last);
1964 __n = __dx;
1965 }
1966 if (__n > 0)
1967 {
Kostya Serebryany497f9122014-09-02 23:43:381968 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:161969 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:381970 __annotator.__done();
Howard Hinnant0949eed2011-06-30 21:18:191971 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:161972 }
1973 }
1974 else
1975 {
1976 allocator_type& __a = this->__alloc();
1977 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1978 __v.__construct_at_end(__first, __last);
1979 __p = __swap_out_circular_buffer(__v, __p);
1980 }
1981 }
1982 return __make_iter(__p);
1983}
1984
1985template <class _Tp, class _Allocator>
1986void
1987vector<_Tp, _Allocator>::resize(size_type __sz)
1988{
1989 size_type __cs = size();
1990 if (__cs < __sz)
1991 this->__append(__sz - __cs);
1992 else if (__cs > __sz)
1993 this->__destruct_at_end(this->__begin_ + __sz);
1994}
1995
1996template <class _Tp, class _Allocator>
1997void
1998vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
1999{
2000 size_type __cs = size();
2001 if (__cs < __sz)
2002 this->__append(__sz - __cs, __x);
2003 else if (__cs > __sz)
2004 this->__destruct_at_end(this->__begin_ + __sz);
2005}
2006
2007template <class _Tp, class _Allocator>
2008void
2009vector<_Tp, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:402010 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2011 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162012{
Howard Hinnant7a563db2011-09-14 18:33:512013 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2014 this->__alloc() == __x.__alloc(),
2015 "vector::swap: Either propagate_on_container_swap must be true"
2016 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:192017 _VSTD::swap(this->__begin_, __x.__begin_);
2018 _VSTD::swap(this->__end_, __x.__end_);
2019 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:162020 __base::__swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnantabe26282011-09-16 17:29:172021#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:512022 __get_db()->swap(this, &__x);
Howard Hinnantabe26282011-09-16 17:29:172023#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:162024}
2025
Howard Hinnant324bb032010-08-22 00:02:432026template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:162027bool
2028vector<_Tp, _Allocator>::__invariants() const
2029{
Howard Hinnant2c39cbe2013-06-27 19:35:322030 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162031 {
Howard Hinnant2c39cbe2013-06-27 19:35:322032 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162033 return false;
2034 }
2035 else
2036 {
2037 if (this->__begin_ > this->__end_)
2038 return false;
2039 if (this->__begin_ == this->__end_cap())
2040 return false;
2041 if (this->__end_ > this->__end_cap())
2042 return false;
2043 }
2044 return true;
2045}
2046
Howard Hinnantabe26282011-09-16 17:29:172047#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:512048
Howard Hinnantbc8d3f92010-05-11 19:42:162049template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:512050bool
2051vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2052{
2053 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2054}
2055
2056template <class _Tp, class _Allocator>
2057bool
2058vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2059{
2060 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2061}
2062
2063template <class _Tp, class _Allocator>
2064bool
2065vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2066{
2067 const_pointer __p = __i->base() + __n;
2068 return this->__begin_ <= __p && __p <= this->__end_;
2069}
2070
2071template <class _Tp, class _Allocator>
2072bool
2073vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2074{
2075 const_pointer __p = __i->base() + __n;
2076 return this->__begin_ <= __p && __p < this->__end_;
2077}
2078
Howard Hinnantabe26282011-09-16 17:29:172079#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:512080
2081template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002082inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162083void
2084vector<_Tp, _Allocator>::__invalidate_all_iterators()
2085{
Howard Hinnantabe26282011-09-16 17:29:172086#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:512087 __get_db()->__invalidate_all(this);
Howard Hinnantabe26282011-09-16 17:29:172088#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:162089}
2090
2091// vector<bool>
2092
2093template <class _Allocator> class vector<bool, _Allocator>;
2094
2095template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2096
2097template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:232098struct __has_storage_type<vector<bool, _Allocator> >
2099{
2100 static const bool value = true;
2101};
2102
2103template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:342104class _LIBCPP_TYPE_VIS_ONLY vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:162105 : private __vector_base_common<true>
2106{
2107public:
2108 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:432109 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:162110 typedef _Allocator allocator_type;
2111 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:162112 typedef typename __alloc_traits::size_type size_type;
2113 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantf867f632012-05-07 16:50:382114 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:162115 typedef __bit_iterator<vector, false> pointer;
2116 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:162117 typedef pointer iterator;
2118 typedef const_pointer const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:192119 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2120 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:162121
2122private:
Howard Hinnantbc8d3f92010-05-11 19:42:162123 typedef typename __alloc_traits::template
2124#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
2125 rebind_alloc<__storage_type>
2126#else
2127 rebind_alloc<__storage_type>::other
2128#endif
2129 __storage_allocator;
2130 typedef allocator_traits<__storage_allocator> __storage_traits;
2131 typedef typename __storage_traits::pointer __storage_pointer;
2132 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2133
2134 __storage_pointer __begin_;
2135 size_type __size_;
2136 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:422137public:
Howard Hinnantf03c3b42011-07-02 20:33:232138 typedef __bit_reference<vector> reference;
2139 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:422140private:
Howard Hinnantd1d27a42011-06-03 19:40:402141 _LIBCPP_INLINE_VISIBILITY
2142 size_type& __cap() _NOEXCEPT
2143 {return __cap_alloc_.first();}
2144 _LIBCPP_INLINE_VISIBILITY
2145 const size_type& __cap() const _NOEXCEPT
2146 {return __cap_alloc_.first();}
2147 _LIBCPP_INLINE_VISIBILITY
2148 __storage_allocator& __alloc() _NOEXCEPT
2149 {return __cap_alloc_.second();}
2150 _LIBCPP_INLINE_VISIBILITY
2151 const __storage_allocator& __alloc() const _NOEXCEPT
2152 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:162153
2154 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2155
Howard Hinnantd1d27a42011-06-03 19:40:402156 _LIBCPP_INLINE_VISIBILITY
2157 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162158 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:402159 _LIBCPP_INLINE_VISIBILITY
2160 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162161 {return (__n - 1) / __bits_per_word + 1;}
2162
2163public:
Howard Hinnantd1d27a42011-06-03 19:40:402164 _LIBCPP_INLINE_VISIBILITY
2165 vector()
2166 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:432167 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:162168 ~vector();
2169 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:592170#if _LIBCPP_STD_VER > 11
2171 explicit vector(size_type __n, const allocator_type& __a);
2172#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162173 vector(size_type __n, const value_type& __v);
2174 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2175 template <class _InputIterator>
2176 vector(_InputIterator __first, _InputIterator __last,
2177 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2178 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2179 template <class _InputIterator>
2180 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2181 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2182 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2183 template <class _ForwardIterator>
2184 vector(_ForwardIterator __first, _ForwardIterator __last,
2185 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2186 template <class _ForwardIterator>
2187 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2188 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2189
2190 vector(const vector& __v);
2191 vector(const vector& __v, const allocator_type& __a);
2192 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:022193#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:162194 vector(initializer_list<value_type> __il);
2195 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:022196#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:162197
Howard Hinnant73d21a42010-09-04 23:28:192198#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:402199 _LIBCPP_INLINE_VISIBILITY
2200 vector(vector&& __v)
2201 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:162202 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:402203 _LIBCPP_INLINE_VISIBILITY
2204 vector& operator=(vector&& __v)
2205 _NOEXCEPT_(
2206 __alloc_traits::propagate_on_container_move_assignment::value &&
2207 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:192208#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:022209#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:282210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162211 vector& operator=(initializer_list<value_type> __il)
2212 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:022213#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:162214
2215 template <class _InputIterator>
2216 typename enable_if
2217 <
2218 __is_input_iterator<_InputIterator>::value &&
2219 !__is_forward_iterator<_InputIterator>::value,
2220 void
2221 >::type
2222 assign(_InputIterator __first, _InputIterator __last);
2223 template <class _ForwardIterator>
2224 typename enable_if
2225 <
2226 __is_forward_iterator<_ForwardIterator>::value,
2227 void
2228 >::type
2229 assign(_ForwardIterator __first, _ForwardIterator __last);
2230
2231 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:022232#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:282233 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162234 void assign(initializer_list<value_type> __il)
2235 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:022236#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:162237
Howard Hinnantd1d27a42011-06-03 19:40:402238 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162239 {return allocator_type(this->__alloc());}
2240
Howard Hinnantd1d27a42011-06-03 19:40:402241 size_type max_size() const _NOEXCEPT;
2242 _LIBCPP_INLINE_VISIBILITY
2243 size_type capacity() const _NOEXCEPT
2244 {return __internal_cap_to_external(__cap());}
2245 _LIBCPP_INLINE_VISIBILITY
2246 size_type size() const _NOEXCEPT
2247 {return __size_;}
2248 _LIBCPP_INLINE_VISIBILITY
2249 bool empty() const _NOEXCEPT
2250 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:162251 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:402252 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:162253
Howard Hinnantd1d27a42011-06-03 19:40:402254 _LIBCPP_INLINE_VISIBILITY
2255 iterator begin() _NOEXCEPT
2256 {return __make_iter(0);}
2257 _LIBCPP_INLINE_VISIBILITY
2258 const_iterator begin() const _NOEXCEPT
2259 {return __make_iter(0);}
2260 _LIBCPP_INLINE_VISIBILITY
2261 iterator end() _NOEXCEPT
2262 {return __make_iter(__size_);}
2263 _LIBCPP_INLINE_VISIBILITY
2264 const_iterator end() const _NOEXCEPT
2265 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:162266
Howard Hinnantd1d27a42011-06-03 19:40:402267 _LIBCPP_INLINE_VISIBILITY
2268 reverse_iterator rbegin() _NOEXCEPT
2269 {return reverse_iterator(end());}
2270 _LIBCPP_INLINE_VISIBILITY
2271 const_reverse_iterator rbegin() const _NOEXCEPT
2272 {return const_reverse_iterator(end());}
2273 _LIBCPP_INLINE_VISIBILITY
2274 reverse_iterator rend() _NOEXCEPT
2275 {return reverse_iterator(begin());}
2276 _LIBCPP_INLINE_VISIBILITY
2277 const_reverse_iterator rend() const _NOEXCEPT
2278 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:162279
Howard Hinnantd1d27a42011-06-03 19:40:402280 _LIBCPP_INLINE_VISIBILITY
2281 const_iterator cbegin() const _NOEXCEPT
2282 {return __make_iter(0);}
2283 _LIBCPP_INLINE_VISIBILITY
2284 const_iterator cend() const _NOEXCEPT
2285 {return __make_iter(__size_);}
2286 _LIBCPP_INLINE_VISIBILITY
2287 const_reverse_iterator crbegin() const _NOEXCEPT
2288 {return rbegin();}
2289 _LIBCPP_INLINE_VISIBILITY
2290 const_reverse_iterator crend() const _NOEXCEPT
2291 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:162292
2293 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2294 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2295 reference at(size_type __n);
2296 const_reference at(size_type __n) const;
2297
2298 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2299 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2300 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2301 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2302
2303 void push_back(const value_type& __x);
Marshall Clow198a2a52013-08-13 23:54:122304#if _LIBCPP_STD_VER > 11
2305 template <class... _Args>
2306 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2307 { push_back ( value_type ( _VSTD::forward<_Args>(__args)... )); }
2308#endif
2309
Howard Hinnantbc8d3f92010-05-11 19:42:162310 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2311
Marshall Clow198a2a52013-08-13 23:54:122312#if _LIBCPP_STD_VER > 11
2313 template <class... _Args>
2314 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2315 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2316#endif
2317
Howard Hinnantbc8d3f92010-05-11 19:42:162318 iterator insert(const_iterator __position, const value_type& __x);
2319 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2320 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2321 template <class _InputIterator>
2322 typename enable_if
2323 <
2324 __is_input_iterator <_InputIterator>::value &&
2325 !__is_forward_iterator<_InputIterator>::value,
2326 iterator
2327 >::type
2328 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2329 template <class _ForwardIterator>
2330 typename enable_if
2331 <
2332 __is_forward_iterator<_ForwardIterator>::value,
2333 iterator
2334 >::type
2335 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:022336#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:282337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162338 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2339 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:022340#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:162341
Howard Hinnant2d72b1e2010-12-17 14:46:432342 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:162343 iterator erase(const_iterator __first, const_iterator __last);
2344
Howard Hinnantd1d27a42011-06-03 19:40:402345 _LIBCPP_INLINE_VISIBILITY
2346 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:162347
Howard Hinnantd1d27a42011-06-03 19:40:402348 void swap(vector&)
2349 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2350 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:162351
2352 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:402353 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:162354
2355 bool __invariants() const;
2356
2357private:
Howard Hinnant2d72b1e2010-12-17 14:46:432358 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:162359 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:402360 void deallocate() _NOEXCEPT;
2361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:202362 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow0d1965d2014-07-28 15:02:422363 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:432364 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2365 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162366 template <class _ForwardIterator>
2367 typename enable_if
2368 <
2369 __is_forward_iterator<_ForwardIterator>::value,
2370 void
2371 >::type
2372 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2373 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:402374 _LIBCPP_INLINE_VISIBILITY
2375 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162376 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:402377 _LIBCPP_INLINE_VISIBILITY
2378 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162379 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:402380 _LIBCPP_INLINE_VISIBILITY
2381 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162382 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:402383 _LIBCPP_INLINE_VISIBILITY
2384 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162385 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:402386 _LIBCPP_INLINE_VISIBILITY
2387 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant2c39cbe2013-06-27 19:35:322388 {return begin() + (__p - cbegin());}
Howard Hinnantbc8d3f92010-05-11 19:42:162389
Howard Hinnantee6ccd02010-09-23 18:58:282390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162391 void __copy_assign_alloc(const vector& __v)
2392 {__copy_assign_alloc(__v, integral_constant<bool,
2393 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:282394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162395 void __copy_assign_alloc(const vector& __c, true_type)
2396 {
2397 if (__alloc() != __c.__alloc())
2398 deallocate();
2399 __alloc() = __c.__alloc();
2400 }
2401
Howard Hinnantee6ccd02010-09-23 18:58:282402 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:042403 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:162404 {}
2405
2406 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:402407 void __move_assign(vector& __c, true_type)
2408 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:282409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162410 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:402411 _NOEXCEPT_(
2412 !__storage_traits::propagate_on_container_move_assignment::value ||
2413 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162414 {__move_assign_alloc(__c, integral_constant<bool,
2415 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:282416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:312417 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:402418 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162419 {
Howard Hinnant0949eed2011-06-30 21:18:192420 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:162421 }
2422
Howard Hinnantee6ccd02010-09-23 18:58:282423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:042424 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:402425 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162426 {}
2427
Howard Hinnantee6ccd02010-09-23 18:58:282428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162429 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y)
Howard Hinnantd1d27a42011-06-03 19:40:402430 _NOEXCEPT_(
2431 !__storage_traits::propagate_on_container_swap::value ||
2432 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162433 {__swap_alloc(__x, __y, integral_constant<bool,
2434 __storage_traits::propagate_on_container_swap::value>());}
2435
Howard Hinnantee6ccd02010-09-23 18:58:282436 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162437 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:402438 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162439 {
Howard Hinnant0949eed2011-06-30 21:18:192440 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:162441 swap(__x, __y);
2442 }
Howard Hinnantee6ccd02010-09-23 18:58:282443 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:042444 static void __swap_alloc(__storage_allocator&, __storage_allocator&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:402445 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162446 {}
2447
Howard Hinnantd1d27a42011-06-03 19:40:402448 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:162449
2450 friend class __bit_reference<vector>;
2451 friend class __bit_const_reference<vector>;
2452 friend class __bit_iterator<vector, false>;
2453 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:182454 friend struct __bit_array<vector>;
Howard Hinnant0f678bd2013-08-12 18:38:342455 friend struct _LIBCPP_TYPE_VIS_ONLY hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:162456};
2457
2458template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002459inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162460void
2461vector<bool, _Allocator>::__invalidate_all_iterators()
2462{
Howard Hinnantbc8d3f92010-05-11 19:42:162463}
2464
2465// Allocate space for __n objects
2466// throws length_error if __n > max_size()
2467// throws (probably bad_alloc) if memory run out
2468// Precondition: __begin_ == __end_ == __cap() == 0
2469// Precondition: __n > 0
2470// Postcondition: capacity() == __n
2471// Postcondition: size() == 0
2472template <class _Allocator>
2473void
2474vector<bool, _Allocator>::allocate(size_type __n)
2475{
2476 if (__n > max_size())
2477 this->__throw_length_error();
2478 __n = __external_cap_to_internal(__n);
2479 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2480 this->__size_ = 0;
2481 this->__cap() = __n;
2482}
2483
2484template <class _Allocator>
2485void
Howard Hinnantd1d27a42011-06-03 19:40:402486vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162487{
Howard Hinnant2c39cbe2013-06-27 19:35:322488 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162489 {
2490 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2491 __invalidate_all_iterators();
Howard Hinnant2c39cbe2013-06-27 19:35:322492 this->__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:162493 this->__size_ = this->__cap() = 0;
2494 }
2495}
2496
2497template <class _Allocator>
2498typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:402499vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162500{
2501 size_type __amax = __storage_traits::max_size(__alloc());
2502 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2503 if (__nmax / __bits_per_word <= __amax)
2504 return __nmax;
2505 return __internal_cap_to_external(__amax);
2506}
2507
2508// Precondition: __new_size > capacity()
2509template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002510inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162511typename vector<bool, _Allocator>::size_type
2512vector<bool, _Allocator>::__recommend(size_type __new_size) const
2513{
2514 const size_type __ms = max_size();
2515 if (__new_size > __ms)
2516 this->__throw_length_error();
2517 const size_type __cap = capacity();
2518 if (__cap >= __ms / 2)
2519 return __ms;
Howard Hinnant7f764502013-08-14 18:00:202520 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:162521}
2522
2523// Default constructs __n objects starting at __end_
2524// Precondition: __n > 0
2525// Precondition: size() + __n <= capacity()
2526// Postcondition: size() == size() + __n
2527template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002528inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162529void
2530vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2531{
2532 size_type __old_size = this->__size_;
2533 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:192534 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162535}
2536
2537template <class _Allocator>
2538template <class _ForwardIterator>
2539typename enable_if
2540<
2541 __is_forward_iterator<_ForwardIterator>::value,
2542 void
2543>::type
2544vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2545{
2546 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:192547 this->__size_ += _VSTD::distance(__first, __last);
2548 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:162549}
2550
2551template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002552inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162553vector<bool, _Allocator>::vector()
Howard Hinnantd1d27a42011-06-03 19:40:402554 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:322555 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162556 __size_(0),
2557 __cap_alloc_(0)
2558{
2559}
2560
2561template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002562inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162563vector<bool, _Allocator>::vector(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:322564 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162565 __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:322572 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162573 __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:592583#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:162598template <class _Allocator>
2599vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant2c39cbe2013-06-27 19:35:322600 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162601 __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:322613 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162614 __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:322629 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162630 __size_(0),
2631 __cap_alloc_(0)
2632{
2633#ifndef _LIBCPP_NO_EXCEPTIONS
2634 try
2635 {
Howard Hinnant324bb032010-08-22 00:02:432636#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162637 for (; __first != __last; ++__first)
2638 push_back(*__first);
2639#ifndef _LIBCPP_NO_EXCEPTIONS
2640 }
2641 catch (...)
2642 {
Howard Hinnant2c39cbe2013-06-27 19:35:322643 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162644 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2645 __invalidate_all_iterators();
2646 throw;
2647 }
Howard Hinnant324bb032010-08-22 00:02:432648#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162649}
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:322656 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162657 __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:432663#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162664 for (; __first != __last; ++__first)
2665 push_back(*__first);
2666#ifndef _LIBCPP_NO_EXCEPTIONS
2667 }
2668 catch (...)
2669 {
Howard Hinnant2c39cbe2013-06-27 19:35:322670 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162671 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2672 __invalidate_all_iterators();
2673 throw;
2674 }
Howard Hinnant324bb032010-08-22 00:02:432675#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162676}
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:322682 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162683 __size_(0),
2684 __cap_alloc_(0)
2685{
Howard Hinnant0949eed2011-06-30 21:18:192686 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:162687 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:322698 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162699 __size_(0),
2700 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2701{
Howard Hinnant0949eed2011-06-30 21:18:192702 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:162703 if (__n > 0)
2704 {
2705 allocate(__n);
2706 __construct_at_end(__first, __last);
2707 }
2708}
2709
Howard Hinnante3e32912011-08-12 21:56:022710#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2711
Howard Hinnantbc8d3f92010-05-11 19:42:162712template <class _Allocator>
2713vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant2c39cbe2013-06-27 19:35:322714 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162715 __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:322728 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162729 __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:022740#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2741
Howard Hinnantbc8d3f92010-05-11 19:42:162742template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:162743vector<bool, _Allocator>::~vector()
2744{
Howard Hinnant2c39cbe2013-06-27 19:35:322745 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162746 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantbc8d3f92010-05-11 19:42:162747 __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:162748}
2749
2750template <class _Allocator>
2751vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant2c39cbe2013-06-27 19:35:322752 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162753 __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:322765 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162766 __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:192790 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:162791 }
2792 __size_ = __v.__size_;
2793 }
2794 return *this;
2795}
2796
Howard Hinnant73d21a42010-09-04 23:28:192797#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2798
Howard Hinnantbc8d3f92010-05-11 19:42:162799template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002800inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162801vector<bool, _Allocator>::vector(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:402802 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162803 : __begin_(__v.__begin_),
2804 __size_(__v.__size_),
2805 __cap_alloc_(__v.__cap_alloc_)
2806{
Howard Hinnant2c39cbe2013-06-27 19:35:322807 __v.__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:162808 __v.__size_ = 0;
2809 __v.__cap() = 0;
2810}
2811
2812template <class _Allocator>
2813vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:322814 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162815 __size_(0),
2816 __cap_alloc_(0, __a)
2817{
2818 if (__a == allocator_type(__v.__alloc()))
2819 {
2820 this->__begin_ = __v.__begin_;
2821 this->__size_ = __v.__size_;
2822 this->__cap() = __v.__cap();
2823 __v.__begin_ = nullptr;
2824 __v.__cap() = __v.__size_ = 0;
2825 }
2826 else if (__v.size() > 0)
2827 {
2828 allocate(__v.size());
2829 __construct_at_end(__v.begin(), __v.end());
2830 }
2831}
2832
2833template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002834inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162835vector<bool, _Allocator>&
2836vector<bool, _Allocator>::operator=(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:402837 _NOEXCEPT_(
2838 __alloc_traits::propagate_on_container_move_assignment::value &&
2839 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162840{
2841 __move_assign(__v, integral_constant<bool,
2842 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:452843 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:162844}
2845
2846template <class _Allocator>
2847void
2848vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2849{
2850 if (__alloc() != __c.__alloc())
2851 assign(__c.begin(), __c.end());
2852 else
2853 __move_assign(__c, true_type());
2854}
2855
2856template <class _Allocator>
2857void
2858vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:402859 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162860{
2861 deallocate();
Marshall Clowdb5e54d2014-07-21 15:15:152862 __move_assign_alloc(__c);
Howard Hinnantbc8d3f92010-05-11 19:42:162863 this->__begin_ = __c.__begin_;
2864 this->__size_ = __c.__size_;
2865 this->__cap() = __c.__cap();
Howard Hinnantbc8d3f92010-05-11 19:42:162866 __c.__begin_ = nullptr;
2867 __c.__cap() = __c.__size_ = 0;
2868}
Howard Hinnant73d21a42010-09-04 23:28:192869
2870#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:162871
2872template <class _Allocator>
2873void
2874vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2875{
2876 __size_ = 0;
2877 if (__n > 0)
2878 {
2879 size_type __c = capacity();
2880 if (__n <= __c)
2881 __size_ = __n;
2882 else
2883 {
2884 vector __v(__alloc());
2885 __v.reserve(__recommend(__n));
2886 __v.__size_ = __n;
2887 swap(__v);
2888 }
Howard Hinnant0949eed2011-06-30 21:18:192889 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162890 }
2891}
2892
2893template <class _Allocator>
2894template <class _InputIterator>
2895typename enable_if
2896<
2897 __is_input_iterator<_InputIterator>::value &&
2898 !__is_forward_iterator<_InputIterator>::value,
2899 void
2900>::type
2901vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2902{
2903 clear();
2904 for (; __first != __last; ++__first)
2905 push_back(*__first);
2906}
2907
2908template <class _Allocator>
2909template <class _ForwardIterator>
2910typename enable_if
2911<
2912 __is_forward_iterator<_ForwardIterator>::value,
2913 void
2914>::type
2915vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2916{
2917 clear();
Howard Hinnant0949eed2011-06-30 21:18:192918 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:162919 if (__n)
2920 {
2921 if (__n > capacity())
2922 {
2923 deallocate();
2924 allocate(__n);
2925 }
2926 __construct_at_end(__first, __last);
2927 }
2928}
2929
2930template <class _Allocator>
2931void
2932vector<bool, _Allocator>::reserve(size_type __n)
2933{
2934 if (__n > capacity())
2935 {
2936 vector __v(this->__alloc());
2937 __v.allocate(__n);
2938 __v.__construct_at_end(this->begin(), this->end());
2939 swap(__v);
2940 __invalidate_all_iterators();
2941 }
2942}
2943
2944template <class _Allocator>
2945void
Howard Hinnantd1d27a42011-06-03 19:40:402946vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162947{
2948 if (__external_cap_to_internal(size()) > __cap())
2949 {
2950#ifndef _LIBCPP_NO_EXCEPTIONS
2951 try
2952 {
Howard Hinnant324bb032010-08-22 00:02:432953#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162954 vector(*this, allocator_type(__alloc())).swap(*this);
2955#ifndef _LIBCPP_NO_EXCEPTIONS
2956 }
2957 catch (...)
2958 {
2959 }
Howard Hinnant324bb032010-08-22 00:02:432960#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162961 }
2962}
2963
2964template <class _Allocator>
2965typename vector<bool, _Allocator>::reference
2966vector<bool, _Allocator>::at(size_type __n)
2967{
2968 if (__n >= size())
2969 this->__throw_out_of_range();
2970 return (*this)[__n];
2971}
2972
2973template <class _Allocator>
2974typename vector<bool, _Allocator>::const_reference
2975vector<bool, _Allocator>::at(size_type __n) const
2976{
2977 if (__n >= size())
2978 this->__throw_out_of_range();
2979 return (*this)[__n];
2980}
2981
2982template <class _Allocator>
2983void
2984vector<bool, _Allocator>::push_back(const value_type& __x)
2985{
2986 if (this->__size_ == this->capacity())
2987 reserve(__recommend(this->__size_ + 1));
2988 ++this->__size_;
2989 back() = __x;
2990}
2991
2992template <class _Allocator>
2993typename vector<bool, _Allocator>::iterator
2994vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2995{
2996 iterator __r;
2997 if (size() < capacity())
2998 {
2999 const_iterator __old_end = end();
3000 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:193001 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:163002 __r = __const_iterator_cast(__position);
3003 }
3004 else
3005 {
3006 vector __v(__alloc());
3007 __v.reserve(__recommend(__size_ + 1));
3008 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:193009 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3010 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:163011 swap(__v);
3012 }
3013 *__r = __x;
3014 return __r;
3015}
3016
3017template <class _Allocator>
3018typename vector<bool, _Allocator>::iterator
3019vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3020{
3021 iterator __r;
3022 size_type __c = capacity();
3023 if (__n <= __c && size() <= __c - __n)
3024 {
3025 const_iterator __old_end = end();
3026 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:193027 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:163028 __r = __const_iterator_cast(__position);
3029 }
3030 else
3031 {
3032 vector __v(__alloc());
3033 __v.reserve(__recommend(__size_ + __n));
3034 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:193035 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3036 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:163037 swap(__v);
3038 }
Howard Hinnant0949eed2011-06-30 21:18:193039 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:163040 return __r;
3041}
3042
3043template <class _Allocator>
3044template <class _InputIterator>
3045typename enable_if
3046<
3047 __is_input_iterator <_InputIterator>::value &&
3048 !__is_forward_iterator<_InputIterator>::value,
3049 typename vector<bool, _Allocator>::iterator
3050>::type
3051vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3052{
3053 difference_type __off = __position - begin();
3054 iterator __p = __const_iterator_cast(__position);
3055 iterator __old_end = end();
3056 for (; size() != capacity() && __first != __last; ++__first)
3057 {
3058 ++this->__size_;
3059 back() = *__first;
3060 }
3061 vector __v(__alloc());
3062 if (__first != __last)
3063 {
3064#ifndef _LIBCPP_NO_EXCEPTIONS
3065 try
3066 {
Howard Hinnant324bb032010-08-22 00:02:433067#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:163068 __v.assign(__first, __last);
3069 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3070 difference_type __old_p = __p - begin();
3071 reserve(__recommend(size() + __v.size()));
3072 __p = begin() + __old_p;
3073 __old_end = begin() + __old_size;
3074#ifndef _LIBCPP_NO_EXCEPTIONS
3075 }
3076 catch (...)
3077 {
3078 erase(__old_end, end());
3079 throw;
3080 }
Howard Hinnant324bb032010-08-22 00:02:433081#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:163082 }
Howard Hinnant0949eed2011-06-30 21:18:193083 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:163084 insert(__p, __v.begin(), __v.end());
3085 return begin() + __off;
3086}
3087
3088template <class _Allocator>
3089template <class _ForwardIterator>
3090typename enable_if
3091<
3092 __is_forward_iterator<_ForwardIterator>::value,
3093 typename vector<bool, _Allocator>::iterator
3094>::type
3095vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3096{
Howard Hinnant0949eed2011-06-30 21:18:193097 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:163098 iterator __r;
3099 size_type __c = capacity();
3100 if (__n <= __c && size() <= __c - __n)
3101 {
3102 const_iterator __old_end = end();
3103 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:193104 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:163105 __r = __const_iterator_cast(__position);
3106 }
3107 else
3108 {
3109 vector __v(__alloc());
3110 __v.reserve(__recommend(__size_ + __n));
3111 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:193112 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3113 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:163114 swap(__v);
3115 }
Howard Hinnant0949eed2011-06-30 21:18:193116 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:163117 return __r;
3118}
3119
3120template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003121inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163122typename vector<bool, _Allocator>::iterator
3123vector<bool, _Allocator>::erase(const_iterator __position)
3124{
3125 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:193126 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:163127 --__size_;
3128 return __r;
3129}
3130
3131template <class _Allocator>
3132typename vector<bool, _Allocator>::iterator
3133vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3134{
3135 iterator __r = __const_iterator_cast(__first);
3136 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:193137 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:163138 __size_ -= __d;
3139 return __r;
3140}
3141
3142template <class _Allocator>
3143void
3144vector<bool, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:403145 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3146 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:163147{
Howard Hinnant0949eed2011-06-30 21:18:193148 _VSTD::swap(this->__begin_, __x.__begin_);
3149 _VSTD::swap(this->__size_, __x.__size_);
3150 _VSTD::swap(this->__cap(), __x.__cap());
Howard Hinnantbc8d3f92010-05-11 19:42:163151 __swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:163152}
3153
Howard Hinnant324bb032010-08-22 00:02:433154template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:163155void
3156vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3157{
3158 size_type __cs = size();
3159 if (__cs < __sz)
3160 {
3161 iterator __r;
3162 size_type __c = capacity();
3163 size_type __n = __sz - __cs;
3164 if (__n <= __c && __cs <= __c - __n)
3165 {
3166 __r = end();
3167 __size_ += __n;
3168 }
3169 else
3170 {
3171 vector __v(__alloc());
3172 __v.reserve(__recommend(__size_ + __n));
3173 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:193174 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:163175 swap(__v);
3176 }
Howard Hinnant0949eed2011-06-30 21:18:193177 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:163178 }
3179 else
3180 __size_ = __sz;
3181}
3182
Howard Hinnant324bb032010-08-22 00:02:433183template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:163184void
Howard Hinnantd1d27a42011-06-03 19:40:403185vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:163186{
3187 // do middle whole words
3188 size_type __n = __size_;
3189 __storage_pointer __p = __begin_;
3190 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3191 *__p = ~*__p;
3192 // do last partial word
3193 if (__n > 0)
3194 {
3195 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3196 __storage_type __b = *__p & __m;
3197 *__p &= ~__m;
3198 *__p |= ~__b & __m;
3199 }
3200}
3201
Howard Hinnant324bb032010-08-22 00:02:433202template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:163203bool
3204vector<bool, _Allocator>::__invariants() const
3205{
Howard Hinnant2c39cbe2013-06-27 19:35:323206 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:163207 {
3208 if (this->__size_ != 0 || this->__cap() != 0)
3209 return false;
3210 }
3211 else
3212 {
3213 if (this->__cap() == 0)
3214 return false;
3215 if (this->__size_ > this->capacity())
3216 return false;
3217 }
3218 return true;
3219}
3220
Howard Hinnant324bb032010-08-22 00:02:433221template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:163222size_t
Howard Hinnantd1d27a42011-06-03 19:40:403223vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:163224{
3225 size_t __h = 0;
3226 // do middle whole words
3227 size_type __n = __size_;
3228 __storage_pointer __p = __begin_;
3229 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3230 __h ^= *__p;
3231 // do last partial word
3232 if (__n > 0)
3233 {
3234 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3235 __h ^= *__p & __m;
3236 }
3237 return __h;
3238}
3239
3240template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:343241struct _LIBCPP_TYPE_VIS_ONLY hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:163242 : public unary_function<vector<bool, _Allocator>, size_t>
3243{
Howard Hinnantee6ccd02010-09-23 18:58:283244 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:403245 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:163246 {return __vec.__hash_code();}
3247};
3248
3249template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003250inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163251bool
3252operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3253{
3254 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:193255 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:163256}
3257
3258template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003259inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163260bool
3261operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3262{
3263 return !(__x == __y);
3264}
3265
3266template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003267inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163268bool
3269operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3270{
Howard Hinnant0949eed2011-06-30 21:18:193271 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:163272}
3273
3274template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003275inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163276bool
3277operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3278{
3279 return __y < __x;
3280}
3281
3282template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003283inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163284bool
3285operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3286{
3287 return !(__x < __y);
3288}
3289
3290template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003291inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163292bool
3293operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3294{
3295 return !(__y < __x);
3296}
3297
3298template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003299inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163300void
3301swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:403302 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:163303{
3304 __x.swap(__y);
3305}
3306
3307_LIBCPP_END_NAMESPACE_STD
3308
3309#endif // _LIBCPP_VECTOR