blob: d23164b9a2aa3b8c99dff08e4d58bf6b856c073e [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 }
Marshall Clow26f472d2014-09-03 21:37:43866#ifndef _LIBCPP_HAS_NO_ASAN
Kostya Serebryany497f9122014-09-02 23:43:38867 // The annotation for size increase should happen before the actual increase,
868 // but if an exception is thrown after that the annotation has to be undone.
869 struct __RAII_IncreaseAnnotator {
870 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1)
Eric Fiselier9f4f2212015-03-10 00:25:20871 : __commit(false), __v(__v), __old_size(__v.size() + __n) {
Kostya Serebryany497f9122014-09-02 23:43:38872 __v.__annotate_increase(__n);
873 }
874 void __done() { __commit = true; }
875 ~__RAII_IncreaseAnnotator() {
876 if (__commit) return;
Eric Fiselier9f4f2212015-03-10 00:25:20877 __v.__annotate_shrink(__old_size);
Kostya Serebryany497f9122014-09-02 23:43:38878 }
879 bool __commit;
Kostya Serebryany497f9122014-09-02 23:43:38880 const vector &__v;
Eric Fiselier9f4f2212015-03-10 00:25:20881 size_type __old_size;
Kostya Serebryany497f9122014-09-02 23:43:38882 };
Marshall Clow26f472d2014-09-03 21:37:43883#else
884 struct __RAII_IncreaseAnnotator {
885 inline __RAII_IncreaseAnnotator(const vector &, size_type __n = 1) {}
886 inline void __done() {}
887 };
888#endif
889
Howard Hinnantbc8d3f92010-05-11 19:42:16890};
891
892template <class _Tp, class _Allocator>
893void
894vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
895{
Marshall Clow1f50f2d2014-05-08 14:14:06896 __annotate_delete();
Howard Hinnantb0bfd9b2012-02-15 00:41:34897 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnant0949eed2011-06-30 21:18:19898 _VSTD::swap(this->__begin_, __v.__begin_);
899 _VSTD::swap(this->__end_, __v.__end_);
900 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16901 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06902 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16903 __invalidate_all_iterators();
904}
905
906template <class _Tp, class _Allocator>
907typename vector<_Tp, _Allocator>::pointer
908vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
909{
Marshall Clow1f50f2d2014-05-08 14:14:06910 __annotate_delete();
Howard Hinnantbc8d3f92010-05-11 19:42:16911 pointer __r = __v.__begin_;
Howard Hinnantb0bfd9b2012-02-15 00:41:34912 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
913 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnant0949eed2011-06-30 21:18:19914 _VSTD::swap(this->__begin_, __v.__begin_);
915 _VSTD::swap(this->__end_, __v.__end_);
916 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16917 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06918 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16919 __invalidate_all_iterators();
920 return __r;
921}
922
923// Allocate space for __n objects
924// throws length_error if __n > max_size()
925// throws (probably bad_alloc) if memory run out
926// Precondition: __begin_ == __end_ == __end_cap() == 0
927// Precondition: __n > 0
928// Postcondition: capacity() == __n
929// Postcondition: size() == 0
930template <class _Tp, class _Allocator>
931void
932vector<_Tp, _Allocator>::allocate(size_type __n)
933{
934 if (__n > max_size())
935 this->__throw_length_error();
936 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
937 this->__end_cap() = this->__begin_ + __n;
Marshall Clow1f50f2d2014-05-08 14:14:06938 __annotate_new(0);
Howard Hinnantbc8d3f92010-05-11 19:42:16939}
940
941template <class _Tp, class _Allocator>
942void
Howard Hinnantd1d27a42011-06-03 19:40:40943vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16944{
Howard Hinnant2c39cbe2013-06-27 19:35:32945 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16946 {
947 clear();
948 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant2c39cbe2013-06-27 19:35:32949 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16950 }
951}
952
953template <class _Tp, class _Allocator>
954typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40955vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16956{
Sean Hunt110b8bf2011-07-29 23:31:58957 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:16958}
959
960// Precondition: __new_size > capacity()
961template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00962inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16963typename vector<_Tp, _Allocator>::size_type
964vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
965{
966 const size_type __ms = max_size();
967 if (__new_size > __ms)
968 this->__throw_length_error();
969 const size_type __cap = capacity();
970 if (__cap >= __ms / 2)
971 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58972 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16973}
974
975// Default constructs __n objects starting at __end_
976// throws if construction throws
977// Precondition: __n > 0
978// Precondition: size() + __n <= capacity()
979// Postcondition: size() == size() + __n
980template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16981void
982vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
983{
Howard Hinnantbc8d3f92010-05-11 19:42:16984 allocator_type& __a = this->__alloc();
985 do
986 {
Kostya Serebryany497f9122014-09-02 23:43:38987 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:19988 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16989 ++this->__end_;
990 --__n;
Kostya Serebryany497f9122014-09-02 23:43:38991 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:16992 } while (__n > 0);
993}
994
Howard Hinnantbc8d3f92010-05-11 19:42:16995// Copy constructs __n objects starting at __end_ from __x
996// throws if construction throws
997// Precondition: __n > 0
998// Precondition: size() + __n <= capacity()
999// Postcondition: size() == old size() + __n
1000// Postcondition: [i] == __x for all i in [size() - __n, __n)
1001template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001002inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161003void
1004vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
1005{
Howard Hinnantbc8d3f92010-05-11 19:42:161006 allocator_type& __a = this->__alloc();
1007 do
1008 {
Kostya Serebryany497f9122014-09-02 23:43:381009 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:191010 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:161011 ++this->__end_;
1012 --__n;
Kostya Serebryany497f9122014-09-02 23:43:381013 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161014 } while (__n > 0);
1015}
1016
1017template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:161018template <class _ForwardIterator>
1019typename enable_if
1020<
1021 __is_forward_iterator<_ForwardIterator>::value,
1022 void
1023>::type
1024vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
1025{
1026 allocator_type& __a = this->__alloc();
1027 for (; __first != __last; ++__first)
1028 {
Kostya Serebryany497f9122014-09-02 23:43:381029 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnant0949eed2011-06-30 21:18:191030 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Kostya Serebryany497f9122014-09-02 23:43:381031 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161032 ++this->__end_;
1033 }
1034}
1035
1036// Default constructs __n objects starting at __end_
1037// throws if construction throws
1038// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:401039// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:161040template <class _Tp, class _Allocator>
1041void
1042vector<_Tp, _Allocator>::__append(size_type __n)
1043{
1044 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1045 this->__construct_at_end(__n);
1046 else
1047 {
1048 allocator_type& __a = this->__alloc();
1049 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1050 __v.__construct_at_end(__n);
1051 __swap_out_circular_buffer(__v);
1052 }
1053}
1054
1055// Default constructs __n objects starting at __end_
1056// throws if construction throws
1057// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:401058// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:161059template <class _Tp, class _Allocator>
1060void
1061vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1062{
1063 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1064 this->__construct_at_end(__n, __x);
1065 else
1066 {
1067 allocator_type& __a = this->__alloc();
1068 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1069 __v.__construct_at_end(__n, __x);
1070 __swap_out_circular_buffer(__v);
1071 }
1072}
1073
1074template <class _Tp, class _Allocator>
1075vector<_Tp, _Allocator>::vector(size_type __n)
1076{
Howard Hinnant0442b122011-09-16 18:41:291077#if _LIBCPP_DEBUG_LEVEL >= 2
1078 __get_db()->__insert_c(this);
1079#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161080 if (__n > 0)
1081 {
1082 allocate(__n);
1083 __construct_at_end(__n);
1084 }
1085}
1086
Marshall Clowa49a2c92013-09-14 00:47:591087#if _LIBCPP_STD_VER > 11
1088template <class _Tp, class _Allocator>
1089vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1090 : __base(__a)
1091{
1092#if _LIBCPP_DEBUG_LEVEL >= 2
1093 __get_db()->__insert_c(this);
1094#endif
1095 if (__n > 0)
1096 {
1097 allocate(__n);
1098 __construct_at_end(__n);
1099 }
1100}
1101#endif
1102
Howard Hinnantbc8d3f92010-05-11 19:42:161103template <class _Tp, class _Allocator>
1104vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1105{
Howard Hinnant0442b122011-09-16 18:41:291106#if _LIBCPP_DEBUG_LEVEL >= 2
1107 __get_db()->__insert_c(this);
1108#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161109 if (__n > 0)
1110 {
1111 allocate(__n);
1112 __construct_at_end(__n, __x);
1113 }
1114}
1115
1116template <class _Tp, class _Allocator>
1117vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1118 : __base(__a)
1119{
Howard Hinnant0442b122011-09-16 18:41:291120#if _LIBCPP_DEBUG_LEVEL >= 2
1121 __get_db()->__insert_c(this);
1122#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161123 if (__n > 0)
1124 {
1125 allocate(__n);
1126 __construct_at_end(__n, __x);
1127 }
1128}
1129
1130template <class _Tp, class _Allocator>
1131template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:541132vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:161133 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:321134 !__is_forward_iterator<_InputIterator>::value &&
1135 is_constructible<
1136 value_type,
Howard Hinnantde589f22013-09-21 21:13:541137 typename iterator_traits<_InputIterator>::reference>::value,
1138 _InputIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:161139{
Howard Hinnantabe26282011-09-16 17:29:171140#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511141 __get_db()->__insert_c(this);
1142#endif
Howard Hinnant0442b122011-09-16 18:41:291143 for (; __first != __last; ++__first)
1144 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:161145}
1146
1147template <class _Tp, class _Allocator>
1148template <class _InputIterator>
1149vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1150 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:321151 !__is_forward_iterator<_InputIterator>::value &&
1152 is_constructible<
1153 value_type,
1154 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:161155 : __base(__a)
1156{
Howard Hinnantabe26282011-09-16 17:29:171157#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511158 __get_db()->__insert_c(this);
1159#endif
Howard Hinnant0442b122011-09-16 18:41:291160 for (; __first != __last; ++__first)
1161 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:161162}
1163
1164template <class _Tp, class _Allocator>
1165template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:541166vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:321167 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1168 is_constructible<
1169 value_type,
Howard Hinnantde589f22013-09-21 21:13:541170 typename iterator_traits<_ForwardIterator>::reference>::value,
1171 _ForwardIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:161172{
Howard Hinnant0442b122011-09-16 18:41:291173#if _LIBCPP_DEBUG_LEVEL >= 2
1174 __get_db()->__insert_c(this);
1175#endif
Howard Hinnant0949eed2011-06-30 21:18:191176 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:161177 if (__n > 0)
1178 {
1179 allocate(__n);
1180 __construct_at_end(__first, __last);
1181 }
1182}
1183
1184template <class _Tp, class _Allocator>
1185template <class _ForwardIterator>
1186vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:321187 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1188 is_constructible<
1189 value_type,
1190 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:161191 : __base(__a)
1192{
Howard Hinnant0442b122011-09-16 18:41:291193#if _LIBCPP_DEBUG_LEVEL >= 2
1194 __get_db()->__insert_c(this);
1195#endif
Howard Hinnant0949eed2011-06-30 21:18:191196 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:161197 if (__n > 0)
1198 {
1199 allocate(__n);
1200 __construct_at_end(__first, __last);
1201 }
1202}
1203
1204template <class _Tp, class _Allocator>
1205vector<_Tp, _Allocator>::vector(const vector& __x)
1206 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1207{
Howard Hinnant0442b122011-09-16 18:41:291208#if _LIBCPP_DEBUG_LEVEL >= 2
1209 __get_db()->__insert_c(this);
1210#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161211 size_type __n = __x.size();
1212 if (__n > 0)
1213 {
1214 allocate(__n);
1215 __construct_at_end(__x.__begin_, __x.__end_);
1216 }
1217}
1218
1219template <class _Tp, class _Allocator>
1220vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1221 : __base(__a)
1222{
Howard Hinnant0442b122011-09-16 18:41:291223#if _LIBCPP_DEBUG_LEVEL >= 2
1224 __get_db()->__insert_c(this);
1225#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161226 size_type __n = __x.size();
1227 if (__n > 0)
1228 {
1229 allocate(__n);
1230 __construct_at_end(__x.__begin_, __x.__end_);
1231 }
1232}
1233
Howard Hinnant73d21a42010-09-04 23:28:191234#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161235
1236template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001237inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161238vector<_Tp, _Allocator>::vector(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:401239 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:191240 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:161241{
Howard Hinnantabe26282011-09-16 17:29:171242#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511243 __get_db()->__insert_c(this);
Howard Hinnante6125bd2011-09-19 16:34:291244 __get_db()->swap(this, &__x);
Howard Hinnant7a563db2011-09-14 18:33:511245#endif
Howard Hinnant0442b122011-09-16 18:41:291246 this->__begin_ = __x.__begin_;
1247 this->__end_ = __x.__end_;
1248 this->__end_cap() = __x.__end_cap();
Howard Hinnant2c39cbe2013-06-27 19:35:321249 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:161250}
1251
1252template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001253inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161254vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1255 : __base(__a)
1256{
Howard Hinnant0442b122011-09-16 18:41:291257#if _LIBCPP_DEBUG_LEVEL >= 2
1258 __get_db()->__insert_c(this);
1259#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161260 if (__a == __x.__alloc())
1261 {
1262 this->__begin_ = __x.__begin_;
1263 this->__end_ = __x.__end_;
1264 this->__end_cap() = __x.__end_cap();
1265 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:291266#if _LIBCPP_DEBUG_LEVEL >= 2
1267 __get_db()->swap(this, &__x);
1268#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161269 }
1270 else
1271 {
Howard Hinnant99968442011-11-29 18:15:501272 typedef move_iterator<iterator> _Ip;
1273 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:161274 }
1275}
1276
Howard Hinnante3e32912011-08-12 21:56:021277#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1278
Howard Hinnantbc8d3f92010-05-11 19:42:161279template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001280inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161281vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1282{
Howard Hinnant0442b122011-09-16 18:41:291283#if _LIBCPP_DEBUG_LEVEL >= 2
1284 __get_db()->__insert_c(this);
1285#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161286 if (__il.size() > 0)
1287 {
1288 allocate(__il.size());
1289 __construct_at_end(__il.begin(), __il.end());
1290 }
1291}
1292
1293template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001294inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161295vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1296 : __base(__a)
1297{
Howard Hinnant0442b122011-09-16 18:41:291298#if _LIBCPP_DEBUG_LEVEL >= 2
1299 __get_db()->__insert_c(this);
1300#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161301 if (__il.size() > 0)
1302 {
1303 allocate(__il.size());
1304 __construct_at_end(__il.begin(), __il.end());
1305 }
1306}
1307
Howard Hinnante3e32912011-08-12 21:56:021308#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1309
Howard Hinnantbc8d3f92010-05-11 19:42:161310template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001311inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161312vector<_Tp, _Allocator>&
1313vector<_Tp, _Allocator>::operator=(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:401314 _NOEXCEPT_(
1315 __alloc_traits::propagate_on_container_move_assignment::value &&
1316 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:161317{
1318 __move_assign(__x, integral_constant<bool,
1319 __alloc_traits::propagate_on_container_move_assignment::value>());
1320 return *this;
1321}
1322
1323template <class _Tp, class _Allocator>
1324void
1325vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1326{
1327 if (__base::__alloc() != __c.__alloc())
1328 {
Howard Hinnant99968442011-11-29 18:15:501329 typedef move_iterator<iterator> _Ip;
1330 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:161331 }
1332 else
1333 __move_assign(__c, true_type());
1334}
1335
1336template <class _Tp, class _Allocator>
1337void
1338vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:401339 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:161340{
1341 deallocate();
Marshall Clow3c2eac62014-07-21 15:11:131342 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantbc8d3f92010-05-11 19:42:161343 this->__begin_ = __c.__begin_;
1344 this->__end_ = __c.__end_;
1345 this->__end_cap() = __c.__end_cap();
Howard Hinnantbc8d3f92010-05-11 19:42:161346 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:291347#if _LIBCPP_DEBUG_LEVEL >= 2
1348 __get_db()->swap(this, &__c);
1349#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161350}
1351
Howard Hinnant73d21a42010-09-04 23:28:191352#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161353
1354template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001355inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161356vector<_Tp, _Allocator>&
1357vector<_Tp, _Allocator>::operator=(const vector& __x)
1358{
1359 if (this != &__x)
1360 {
1361 __base::__copy_assign_alloc(__x);
1362 assign(__x.__begin_, __x.__end_);
1363 }
1364 return *this;
1365}
1366
1367template <class _Tp, class _Allocator>
1368template <class _InputIterator>
1369typename enable_if
1370<
1371 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:321372 !__is_forward_iterator<_InputIterator>::value &&
1373 is_constructible<
1374 _Tp,
1375 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:161376 void
1377>::type
1378vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1379{
1380 clear();
1381 for (; __first != __last; ++__first)
1382 push_back(*__first);
1383}
1384
1385template <class _Tp, class _Allocator>
1386template <class _ForwardIterator>
1387typename enable_if
1388<
Howard Hinnant742fecb2013-03-28 17:44:321389 __is_forward_iterator<_ForwardIterator>::value &&
1390 is_constructible<
1391 _Tp,
1392 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:161393 void
1394>::type
1395vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1396{
Howard Hinnant0949eed2011-06-30 21:18:191397 typename iterator_traits<_ForwardIterator>::difference_type __new_size = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:161398 if (static_cast<size_type>(__new_size) <= capacity())
1399 {
1400 _ForwardIterator __mid = __last;
1401 bool __growing = false;
1402 if (static_cast<size_type>(__new_size) > size())
1403 {
1404 __growing = true;
1405 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:191406 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:161407 }
Howard Hinnant0949eed2011-06-30 21:18:191408 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:161409 if (__growing)
1410 __construct_at_end(__mid, __last);
1411 else
1412 this->__destruct_at_end(__m);
1413 }
1414 else
1415 {
1416 deallocate();
1417 allocate(__recommend(static_cast<size_type>(__new_size)));
1418 __construct_at_end(__first, __last);
1419 }
1420}
1421
1422template <class _Tp, class _Allocator>
1423void
1424vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1425{
1426 if (__n <= capacity())
1427 {
1428 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:191429 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:161430 if (__n > __s)
1431 __construct_at_end(__n - __s, __u);
1432 else
Howard Hinnantadff4892010-05-24 17:49:411433 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:161434 }
1435 else
1436 {
1437 deallocate();
1438 allocate(__recommend(static_cast<size_type>(__n)));
1439 __construct_at_end(__n, __u);
1440 }
1441}
1442
Howard Hinnant324bb032010-08-22 00:02:431443template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001444inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161445typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:401446vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161447{
Howard Hinnantabe26282011-09-16 17:29:171448#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:161449 return iterator(this, __p);
1450#else
1451 return iterator(__p);
1452#endif
1453}
1454
Howard Hinnant324bb032010-08-22 00:02:431455template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001456inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161457typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:401458vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161459{
Howard Hinnantabe26282011-09-16 17:29:171460#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:161461 return const_iterator(this, __p);
1462#else
1463 return const_iterator(__p);
1464#endif
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>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:401470vector<_Tp, _Allocator>::begin() _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>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:401478vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161479{
1480 return __make_iter(this->__begin_);
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>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:401486vector<_Tp, _Allocator>::end() _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>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:401494vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161495{
1496 return __make_iter(this->__end_);
1497}
1498
Howard Hinnant324bb032010-08-22 00:02:431499template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001500inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161501typename vector<_Tp, _Allocator>::reference
1502vector<_Tp, _Allocator>::operator[](size_type __n)
1503{
Howard Hinnant7a563db2011-09-14 18:33:511504 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:161505 return this->__begin_[__n];
1506}
1507
Howard Hinnant324bb032010-08-22 00:02:431508template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001509inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161510typename vector<_Tp, _Allocator>::const_reference
1511vector<_Tp, _Allocator>::operator[](size_type __n) const
1512{
Howard Hinnant7a563db2011-09-14 18:33:511513 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:161514 return this->__begin_[__n];
1515}
1516
Howard Hinnant324bb032010-08-22 00:02:431517template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:161518typename vector<_Tp, _Allocator>::reference
1519vector<_Tp, _Allocator>::at(size_type __n)
1520{
1521 if (__n >= size())
1522 this->__throw_out_of_range();
1523 return this->__begin_[__n];
1524}
1525
Howard Hinnant324bb032010-08-22 00:02:431526template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:161527typename vector<_Tp, _Allocator>::const_reference
1528vector<_Tp, _Allocator>::at(size_type __n) const
1529{
1530 if (__n >= size())
1531 this->__throw_out_of_range();
1532 return this->__begin_[__n];
1533}
1534
1535template <class _Tp, class _Allocator>
1536void
1537vector<_Tp, _Allocator>::reserve(size_type __n)
1538{
1539 if (__n > capacity())
1540 {
1541 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:401542 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:161543 __swap_out_circular_buffer(__v);
1544 }
1545}
1546
1547template <class _Tp, class _Allocator>
1548void
Howard Hinnantd1d27a42011-06-03 19:40:401549vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161550{
1551 if (capacity() > size())
1552 {
1553#ifndef _LIBCPP_NO_EXCEPTIONS
1554 try
1555 {
Howard Hinnant324bb032010-08-22 00:02:431556#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161557 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:401558 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:161559 __swap_out_circular_buffer(__v);
1560#ifndef _LIBCPP_NO_EXCEPTIONS
1561 }
1562 catch (...)
1563 {
1564 }
Howard Hinnant324bb032010-08-22 00:02:431565#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161566 }
1567}
1568
1569template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:341570template <class _Up>
1571void
1572#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1573vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1574#else
1575vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1576#endif
1577{
1578 allocator_type& __a = this->__alloc();
1579 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1580 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnantf619e232013-01-11 20:36:591581 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1582 __v.__end_++;
Howard Hinnantb0bfd9b2012-02-15 00:41:341583 __swap_out_circular_buffer(__v);
1584}
1585
1586template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001587inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161588void
1589vector<_Tp, _Allocator>::push_back(const_reference __x)
1590{
Howard Hinnantb0bfd9b2012-02-15 00:41:341591 if (this->__end_ != this->__end_cap())
Howard Hinnantbc8d3f92010-05-11 19:42:161592 {
Kostya Serebryany497f9122014-09-02 23:43:381593 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:161594 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191595 _VSTD::__to_raw_pointer(this->__end_), __x);
Kostya Serebryany497f9122014-09-02 23:43:381596 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161597 ++this->__end_;
1598 }
1599 else
Howard Hinnantb0bfd9b2012-02-15 00:41:341600 __push_back_slow_path(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:161601}
1602
Howard Hinnant73d21a42010-09-04 23:28:191603#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161604
1605template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001606inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161607void
1608vector<_Tp, _Allocator>::push_back(value_type&& __x)
1609{
1610 if (this->__end_ < this->__end_cap())
1611 {
Kostya Serebryany497f9122014-09-02 23:43:381612 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:161613 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191614 _VSTD::__to_raw_pointer(this->__end_),
1615 _VSTD::move(__x));
Kostya Serebryany497f9122014-09-02 23:43:381616 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161617 ++this->__end_;
1618 }
1619 else
Howard Hinnantb0bfd9b2012-02-15 00:41:341620 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:161621}
1622
Howard Hinnant73d21a42010-09-04 23:28:191623#ifndef _LIBCPP_HAS_NO_VARIADICS
1624
Howard Hinnantbc8d3f92010-05-11 19:42:161625template <class _Tp, class _Allocator>
1626template <class... _Args>
1627void
Howard Hinnant0438ea22012-02-26 15:30:121628vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1629{
1630 allocator_type& __a = this->__alloc();
1631 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1632// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantf619e232013-01-11 20:36:591633 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1634 __v.__end_++;
Howard Hinnant0438ea22012-02-26 15:30:121635 __swap_out_circular_buffer(__v);
1636}
1637
1638template <class _Tp, class _Allocator>
1639template <class... _Args>
Howard Hinnant1e564242013-10-04 22:09:001640inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0438ea22012-02-26 15:30:121641void
Howard Hinnantbc8d3f92010-05-11 19:42:161642vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1643{
1644 if (this->__end_ < this->__end_cap())
1645 {
Kostya Serebryany497f9122014-09-02 23:43:381646 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:161647 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191648 _VSTD::__to_raw_pointer(this->__end_),
1649 _VSTD::forward<_Args>(__args)...);
Kostya Serebryany497f9122014-09-02 23:43:381650 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161651 ++this->__end_;
1652 }
1653 else
Howard Hinnant0438ea22012-02-26 15:30:121654 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:161655}
1656
Howard Hinnant73d21a42010-09-04 23:28:191657#endif // _LIBCPP_HAS_NO_VARIADICS
1658#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161659
1660template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001661inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161662void
1663vector<_Tp, _Allocator>::pop_back()
1664{
Howard Hinnant7a563db2011-09-14 18:33:511665 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:161666 this->__destruct_at_end(this->__end_ - 1);
1667}
1668
1669template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001670inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161671typename vector<_Tp, _Allocator>::iterator
1672vector<_Tp, _Allocator>::erase(const_iterator __position)
1673{
Howard Hinnantabe26282011-09-16 17:29:171674#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511675 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1676 "vector::erase(iterator) called with an iterator not"
1677 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171678#endif
Howard Hinnant782da332013-03-25 22:12:261679 _LIBCPP_ASSERT(__position != end(),
1680 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant2c39cbe2013-06-27 19:35:321681 difference_type __ps = __position - cbegin();
1682 pointer __p = this->__begin_ + __ps;
Howard Hinnantbc8d3f92010-05-11 19:42:161683 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:191684 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:161685 return __r;
1686}
1687
1688template <class _Tp, class _Allocator>
1689typename vector<_Tp, _Allocator>::iterator
1690vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1691{
Howard Hinnantabe26282011-09-16 17:29:171692#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511693 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1694 "vector::erase(iterator, iterator) called with an iterator not"
1695 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171696#endif
Howard Hinnant7a563db2011-09-14 18:33:511697 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:161698 pointer __p = this->__begin_ + (__first - begin());
1699 iterator __r = __make_iter(__p);
Howard Hinnantb4e67cf2013-04-18 15:02:571700 if (__first != __last)
1701 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:161702 return __r;
1703}
1704
1705template <class _Tp, class _Allocator>
1706void
1707vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1708{
1709 pointer __old_last = this->__end_;
1710 difference_type __n = __old_last - __to;
1711 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1712 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191713 _VSTD::__to_raw_pointer(this->__end_),
1714 _VSTD::move(*__i));
1715 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:161716}
1717
1718template <class _Tp, class _Allocator>
1719typename vector<_Tp, _Allocator>::iterator
1720vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1721{
Howard Hinnantabe26282011-09-16 17:29:171722#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511723 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1724 "vector::insert(iterator, x) called with an iterator not"
1725 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171726#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161727 pointer __p = this->__begin_ + (__position - begin());
1728 if (this->__end_ < this->__end_cap())
1729 {
Kostya Serebryany497f9122014-09-02 23:43:381730 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:161731 if (__p == this->__end_)
1732 {
1733 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191734 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:161735 ++this->__end_;
1736 }
1737 else
1738 {
1739 __move_range(__p, this->__end_, __p + 1);
1740 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1741 if (__p <= __xr && __xr < this->__end_)
1742 ++__xr;
1743 *__p = *__xr;
1744 }
Kostya Serebryany497f9122014-09-02 23:43:381745 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161746 }
1747 else
1748 {
1749 allocator_type& __a = this->__alloc();
1750 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1751 __v.push_back(__x);
1752 __p = __swap_out_circular_buffer(__v, __p);
1753 }
1754 return __make_iter(__p);
1755}
1756
Howard Hinnant73d21a42010-09-04 23:28:191757#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161758
1759template <class _Tp, class _Allocator>
1760typename vector<_Tp, _Allocator>::iterator
1761vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1762{
Howard Hinnantabe26282011-09-16 17:29:171763#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511764 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1765 "vector::insert(iterator, x) called with an iterator not"
1766 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171767#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161768 pointer __p = this->__begin_ + (__position - begin());
1769 if (this->__end_ < this->__end_cap())
1770 {
Kostya Serebryany497f9122014-09-02 23:43:381771 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:161772 if (__p == this->__end_)
1773 {
1774 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191775 _VSTD::__to_raw_pointer(this->__end_),
1776 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:161777 ++this->__end_;
1778 }
1779 else
1780 {
1781 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:191782 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:161783 }
Kostya Serebryany497f9122014-09-02 23:43:381784 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161785 }
1786 else
1787 {
1788 allocator_type& __a = this->__alloc();
1789 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:191790 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:161791 __p = __swap_out_circular_buffer(__v, __p);
1792 }
1793 return __make_iter(__p);
1794}
1795
Howard Hinnant73d21a42010-09-04 23:28:191796#ifndef _LIBCPP_HAS_NO_VARIADICS
1797
Howard Hinnantbc8d3f92010-05-11 19:42:161798template <class _Tp, class _Allocator>
1799template <class... _Args>
1800typename vector<_Tp, _Allocator>::iterator
1801vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1802{
Howard Hinnantabe26282011-09-16 17:29:171803#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511804 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1805 "vector::emplace(iterator, x) called with an iterator not"
1806 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171807#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161808 pointer __p = this->__begin_ + (__position - begin());
1809 if (this->__end_ < this->__end_cap())
1810 {
Kostya Serebryany497f9122014-09-02 23:43:381811 __RAII_IncreaseAnnotator __annotator(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:161812 if (__p == this->__end_)
1813 {
1814 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191815 _VSTD::__to_raw_pointer(this->__end_),
1816 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:161817 ++this->__end_;
1818 }
1819 else
1820 {
Howard Hinnanta58402a2012-07-08 23:23:041821 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:161822 __move_range(__p, this->__end_, __p + 1);
Howard Hinnanta58402a2012-07-08 23:23:041823 *__p = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:161824 }
Kostya Serebryany497f9122014-09-02 23:43:381825 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161826 }
1827 else
1828 {
1829 allocator_type& __a = this->__alloc();
1830 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:191831 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:161832 __p = __swap_out_circular_buffer(__v, __p);
1833 }
1834 return __make_iter(__p);
1835}
1836
Howard Hinnant73d21a42010-09-04 23:28:191837#endif // _LIBCPP_HAS_NO_VARIADICS
1838#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161839
1840template <class _Tp, class _Allocator>
1841typename vector<_Tp, _Allocator>::iterator
1842vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1843{
Howard Hinnantabe26282011-09-16 17:29:171844#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511845 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1846 "vector::insert(iterator, n, x) called with an iterator not"
1847 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171848#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161849 pointer __p = this->__begin_ + (__position - begin());
1850 if (__n > 0)
1851 {
1852 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1853 {
1854 size_type __old_n = __n;
1855 pointer __old_last = this->__end_;
1856 if (__n > static_cast<size_type>(this->__end_ - __p))
1857 {
1858 size_type __cx = __n - (this->__end_ - __p);
1859 __construct_at_end(__cx, __x);
1860 __n -= __cx;
1861 }
1862 if (__n > 0)
1863 {
Eric Fiselierd7590952014-11-14 18:28:361864 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:161865 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:381866 __annotator.__done();
Howard Hinnantbc8d3f92010-05-11 19:42:161867 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1868 if (__p <= __xr && __xr < this->__end_)
1869 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:191870 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:161871 }
1872 }
1873 else
1874 {
1875 allocator_type& __a = this->__alloc();
1876 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1877 __v.__construct_at_end(__n, __x);
1878 __p = __swap_out_circular_buffer(__v, __p);
1879 }
1880 }
1881 return __make_iter(__p);
1882}
1883
1884template <class _Tp, class _Allocator>
1885template <class _InputIterator>
1886typename enable_if
1887<
1888 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:321889 !__is_forward_iterator<_InputIterator>::value &&
1890 is_constructible<
1891 _Tp,
1892 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:161893 typename vector<_Tp, _Allocator>::iterator
1894>::type
1895vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1896{
Howard Hinnantabe26282011-09-16 17:29:171897#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511898 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1899 "vector::insert(iterator, range) called with an iterator not"
1900 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171901#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161902 difference_type __off = __position - begin();
1903 pointer __p = this->__begin_ + __off;
1904 allocator_type& __a = this->__alloc();
1905 pointer __old_last = this->__end_;
1906 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1907 {
Howard Hinnant0949eed2011-06-30 21:18:191908 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:161909 *__first);
1910 ++this->__end_;
1911 }
1912 __split_buffer<value_type, allocator_type&> __v(__a);
1913 if (__first != __last)
1914 {
1915#ifndef _LIBCPP_NO_EXCEPTIONS
1916 try
1917 {
Howard Hinnant324bb032010-08-22 00:02:431918#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161919 __v.__construct_at_end(__first, __last);
1920 difference_type __old_size = __old_last - this->__begin_;
1921 difference_type __old_p = __p - this->__begin_;
1922 reserve(__recommend(size() + __v.size()));
1923 __p = this->__begin_ + __old_p;
1924 __old_last = this->__begin_ + __old_size;
1925#ifndef _LIBCPP_NO_EXCEPTIONS
1926 }
1927 catch (...)
1928 {
1929 erase(__make_iter(__old_last), end());
1930 throw;
1931 }
Howard Hinnant324bb032010-08-22 00:02:431932#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161933 }
Howard Hinnant0949eed2011-06-30 21:18:191934 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant0442b122011-09-16 18:41:291935 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1936 make_move_iterator(__v.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:161937 return begin() + __off;
1938}
1939
1940template <class _Tp, class _Allocator>
1941template <class _ForwardIterator>
1942typename enable_if
1943<
Howard Hinnant742fecb2013-03-28 17:44:321944 __is_forward_iterator<_ForwardIterator>::value &&
1945 is_constructible<
1946 _Tp,
1947 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:161948 typename vector<_Tp, _Allocator>::iterator
1949>::type
1950vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1951{
Howard Hinnantabe26282011-09-16 17:29:171952#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511953 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1954 "vector::insert(iterator, range) called with an iterator not"
1955 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171956#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161957 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:191958 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:161959 if (__n > 0)
1960 {
1961 if (__n <= this->__end_cap() - this->__end_)
1962 {
1963 size_type __old_n = __n;
1964 pointer __old_last = this->__end_;
1965 _ForwardIterator __m = __last;
1966 difference_type __dx = this->__end_ - __p;
1967 if (__n > __dx)
1968 {
1969 __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:191970 _VSTD::advance(__m, this->__end_ - __p);
Howard Hinnantbc8d3f92010-05-11 19:42:161971 __construct_at_end(__m, __last);
1972 __n = __dx;
1973 }
1974 if (__n > 0)
1975 {
Kostya Serebryany497f9122014-09-02 23:43:381976 __RAII_IncreaseAnnotator __annotator(*this, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:161977 __move_range(__p, __old_last, __p + __old_n);
Kostya Serebryany497f9122014-09-02 23:43:381978 __annotator.__done();
Howard Hinnant0949eed2011-06-30 21:18:191979 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:161980 }
1981 }
1982 else
1983 {
1984 allocator_type& __a = this->__alloc();
1985 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1986 __v.__construct_at_end(__first, __last);
1987 __p = __swap_out_circular_buffer(__v, __p);
1988 }
1989 }
1990 return __make_iter(__p);
1991}
1992
1993template <class _Tp, class _Allocator>
1994void
1995vector<_Tp, _Allocator>::resize(size_type __sz)
1996{
1997 size_type __cs = size();
1998 if (__cs < __sz)
1999 this->__append(__sz - __cs);
2000 else if (__cs > __sz)
2001 this->__destruct_at_end(this->__begin_ + __sz);
2002}
2003
2004template <class _Tp, class _Allocator>
2005void
2006vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
2007{
2008 size_type __cs = size();
2009 if (__cs < __sz)
2010 this->__append(__sz - __cs, __x);
2011 else if (__cs > __sz)
2012 this->__destruct_at_end(this->__begin_ + __sz);
2013}
2014
2015template <class _Tp, class _Allocator>
2016void
2017vector<_Tp, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:402018 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2019 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162020{
Howard Hinnant7a563db2011-09-14 18:33:512021 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2022 this->__alloc() == __x.__alloc(),
2023 "vector::swap: Either propagate_on_container_swap must be true"
2024 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:192025 _VSTD::swap(this->__begin_, __x.__begin_);
2026 _VSTD::swap(this->__end_, __x.__end_);
2027 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:162028 __base::__swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnantabe26282011-09-16 17:29:172029#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:512030 __get_db()->swap(this, &__x);
Howard Hinnantabe26282011-09-16 17:29:172031#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:162032}
2033
Howard Hinnant324bb032010-08-22 00:02:432034template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:162035bool
2036vector<_Tp, _Allocator>::__invariants() const
2037{
Howard Hinnant2c39cbe2013-06-27 19:35:322038 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162039 {
Howard Hinnant2c39cbe2013-06-27 19:35:322040 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162041 return false;
2042 }
2043 else
2044 {
2045 if (this->__begin_ > this->__end_)
2046 return false;
2047 if (this->__begin_ == this->__end_cap())
2048 return false;
2049 if (this->__end_ > this->__end_cap())
2050 return false;
2051 }
2052 return true;
2053}
2054
Howard Hinnantabe26282011-09-16 17:29:172055#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:512056
Howard Hinnantbc8d3f92010-05-11 19:42:162057template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:512058bool
2059vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2060{
2061 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2062}
2063
2064template <class _Tp, class _Allocator>
2065bool
2066vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2067{
2068 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2069}
2070
2071template <class _Tp, class _Allocator>
2072bool
2073vector<_Tp, _Allocator>::__addable(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
2079template <class _Tp, class _Allocator>
2080bool
2081vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2082{
2083 const_pointer __p = __i->base() + __n;
2084 return this->__begin_ <= __p && __p < this->__end_;
2085}
2086
Howard Hinnantabe26282011-09-16 17:29:172087#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:512088
2089template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002090inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162091void
2092vector<_Tp, _Allocator>::__invalidate_all_iterators()
2093{
Howard Hinnantabe26282011-09-16 17:29:172094#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:512095 __get_db()->__invalidate_all(this);
Howard Hinnantabe26282011-09-16 17:29:172096#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:162097}
2098
2099// vector<bool>
2100
2101template <class _Allocator> class vector<bool, _Allocator>;
2102
2103template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2104
2105template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:232106struct __has_storage_type<vector<bool, _Allocator> >
2107{
2108 static const bool value = true;
2109};
2110
2111template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:342112class _LIBCPP_TYPE_VIS_ONLY vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:162113 : private __vector_base_common<true>
2114{
2115public:
2116 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:432117 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:162118 typedef _Allocator allocator_type;
2119 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:162120 typedef typename __alloc_traits::size_type size_type;
2121 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantf867f632012-05-07 16:50:382122 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:162123 typedef __bit_iterator<vector, false> pointer;
2124 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:162125 typedef pointer iterator;
2126 typedef const_pointer const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:192127 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2128 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:162129
2130private:
Howard Hinnantbc8d3f92010-05-11 19:42:162131 typedef typename __alloc_traits::template
2132#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
2133 rebind_alloc<__storage_type>
2134#else
2135 rebind_alloc<__storage_type>::other
2136#endif
2137 __storage_allocator;
2138 typedef allocator_traits<__storage_allocator> __storage_traits;
2139 typedef typename __storage_traits::pointer __storage_pointer;
2140 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2141
2142 __storage_pointer __begin_;
2143 size_type __size_;
2144 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:422145public:
Howard Hinnantf03c3b42011-07-02 20:33:232146 typedef __bit_reference<vector> reference;
2147 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:422148private:
Howard Hinnantd1d27a42011-06-03 19:40:402149 _LIBCPP_INLINE_VISIBILITY
2150 size_type& __cap() _NOEXCEPT
2151 {return __cap_alloc_.first();}
2152 _LIBCPP_INLINE_VISIBILITY
2153 const size_type& __cap() const _NOEXCEPT
2154 {return __cap_alloc_.first();}
2155 _LIBCPP_INLINE_VISIBILITY
2156 __storage_allocator& __alloc() _NOEXCEPT
2157 {return __cap_alloc_.second();}
2158 _LIBCPP_INLINE_VISIBILITY
2159 const __storage_allocator& __alloc() const _NOEXCEPT
2160 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:162161
2162 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2163
Howard Hinnantd1d27a42011-06-03 19:40:402164 _LIBCPP_INLINE_VISIBILITY
2165 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162166 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:402167 _LIBCPP_INLINE_VISIBILITY
2168 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162169 {return (__n - 1) / __bits_per_word + 1;}
2170
2171public:
Howard Hinnantd1d27a42011-06-03 19:40:402172 _LIBCPP_INLINE_VISIBILITY
2173 vector()
2174 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:432175 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:162176 ~vector();
2177 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:592178#if _LIBCPP_STD_VER > 11
2179 explicit vector(size_type __n, const allocator_type& __a);
2180#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162181 vector(size_type __n, const value_type& __v);
2182 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2183 template <class _InputIterator>
2184 vector(_InputIterator __first, _InputIterator __last,
2185 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2186 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2187 template <class _InputIterator>
2188 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2189 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2190 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2191 template <class _ForwardIterator>
2192 vector(_ForwardIterator __first, _ForwardIterator __last,
2193 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2194 template <class _ForwardIterator>
2195 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2196 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2197
2198 vector(const vector& __v);
2199 vector(const vector& __v, const allocator_type& __a);
2200 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:022201#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:162202 vector(initializer_list<value_type> __il);
2203 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:022204#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:162205
Howard Hinnant73d21a42010-09-04 23:28:192206#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:402207 _LIBCPP_INLINE_VISIBILITY
2208 vector(vector&& __v)
2209 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:162210 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:402211 _LIBCPP_INLINE_VISIBILITY
2212 vector& operator=(vector&& __v)
2213 _NOEXCEPT_(
2214 __alloc_traits::propagate_on_container_move_assignment::value &&
2215 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:192216#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:022217#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:282218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162219 vector& operator=(initializer_list<value_type> __il)
2220 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:022221#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:162222
2223 template <class _InputIterator>
2224 typename enable_if
2225 <
2226 __is_input_iterator<_InputIterator>::value &&
2227 !__is_forward_iterator<_InputIterator>::value,
2228 void
2229 >::type
2230 assign(_InputIterator __first, _InputIterator __last);
2231 template <class _ForwardIterator>
2232 typename enable_if
2233 <
2234 __is_forward_iterator<_ForwardIterator>::value,
2235 void
2236 >::type
2237 assign(_ForwardIterator __first, _ForwardIterator __last);
2238
2239 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:022240#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:282241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162242 void assign(initializer_list<value_type> __il)
2243 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:022244#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:162245
Howard Hinnantd1d27a42011-06-03 19:40:402246 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162247 {return allocator_type(this->__alloc());}
2248
Howard Hinnantd1d27a42011-06-03 19:40:402249 size_type max_size() const _NOEXCEPT;
2250 _LIBCPP_INLINE_VISIBILITY
2251 size_type capacity() const _NOEXCEPT
2252 {return __internal_cap_to_external(__cap());}
2253 _LIBCPP_INLINE_VISIBILITY
2254 size_type size() const _NOEXCEPT
2255 {return __size_;}
2256 _LIBCPP_INLINE_VISIBILITY
2257 bool empty() const _NOEXCEPT
2258 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:162259 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:402260 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:162261
Howard Hinnantd1d27a42011-06-03 19:40:402262 _LIBCPP_INLINE_VISIBILITY
2263 iterator begin() _NOEXCEPT
2264 {return __make_iter(0);}
2265 _LIBCPP_INLINE_VISIBILITY
2266 const_iterator begin() const _NOEXCEPT
2267 {return __make_iter(0);}
2268 _LIBCPP_INLINE_VISIBILITY
2269 iterator end() _NOEXCEPT
2270 {return __make_iter(__size_);}
2271 _LIBCPP_INLINE_VISIBILITY
2272 const_iterator end() const _NOEXCEPT
2273 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:162274
Howard Hinnantd1d27a42011-06-03 19:40:402275 _LIBCPP_INLINE_VISIBILITY
2276 reverse_iterator rbegin() _NOEXCEPT
2277 {return reverse_iterator(end());}
2278 _LIBCPP_INLINE_VISIBILITY
2279 const_reverse_iterator rbegin() const _NOEXCEPT
2280 {return const_reverse_iterator(end());}
2281 _LIBCPP_INLINE_VISIBILITY
2282 reverse_iterator rend() _NOEXCEPT
2283 {return reverse_iterator(begin());}
2284 _LIBCPP_INLINE_VISIBILITY
2285 const_reverse_iterator rend() const _NOEXCEPT
2286 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:162287
Howard Hinnantd1d27a42011-06-03 19:40:402288 _LIBCPP_INLINE_VISIBILITY
2289 const_iterator cbegin() const _NOEXCEPT
2290 {return __make_iter(0);}
2291 _LIBCPP_INLINE_VISIBILITY
2292 const_iterator cend() const _NOEXCEPT
2293 {return __make_iter(__size_);}
2294 _LIBCPP_INLINE_VISIBILITY
2295 const_reverse_iterator crbegin() const _NOEXCEPT
2296 {return rbegin();}
2297 _LIBCPP_INLINE_VISIBILITY
2298 const_reverse_iterator crend() const _NOEXCEPT
2299 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:162300
2301 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2302 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2303 reference at(size_type __n);
2304 const_reference at(size_type __n) const;
2305
2306 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2307 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2308 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2309 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2310
2311 void push_back(const value_type& __x);
Marshall Clow198a2a52013-08-13 23:54:122312#if _LIBCPP_STD_VER > 11
2313 template <class... _Args>
2314 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2315 { push_back ( value_type ( _VSTD::forward<_Args>(__args)... )); }
2316#endif
2317
Howard Hinnantbc8d3f92010-05-11 19:42:162318 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2319
Marshall Clow198a2a52013-08-13 23:54:122320#if _LIBCPP_STD_VER > 11
2321 template <class... _Args>
2322 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2323 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2324#endif
2325
Howard Hinnantbc8d3f92010-05-11 19:42:162326 iterator insert(const_iterator __position, const value_type& __x);
2327 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2328 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2329 template <class _InputIterator>
2330 typename enable_if
2331 <
2332 __is_input_iterator <_InputIterator>::value &&
2333 !__is_forward_iterator<_InputIterator>::value,
2334 iterator
2335 >::type
2336 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2337 template <class _ForwardIterator>
2338 typename enable_if
2339 <
2340 __is_forward_iterator<_ForwardIterator>::value,
2341 iterator
2342 >::type
2343 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:022344#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:282345 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162346 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2347 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:022348#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:162349
Howard Hinnant2d72b1e2010-12-17 14:46:432350 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:162351 iterator erase(const_iterator __first, const_iterator __last);
2352
Howard Hinnantd1d27a42011-06-03 19:40:402353 _LIBCPP_INLINE_VISIBILITY
2354 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:162355
Howard Hinnantd1d27a42011-06-03 19:40:402356 void swap(vector&)
2357 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2358 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:162359
2360 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:402361 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:162362
2363 bool __invariants() const;
2364
2365private:
Howard Hinnant2d72b1e2010-12-17 14:46:432366 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:162367 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:402368 void deallocate() _NOEXCEPT;
2369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:202370 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow0d1965d2014-07-28 15:02:422371 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:432372 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2373 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162374 template <class _ForwardIterator>
2375 typename enable_if
2376 <
2377 __is_forward_iterator<_ForwardIterator>::value,
2378 void
2379 >::type
2380 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2381 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:402382 _LIBCPP_INLINE_VISIBILITY
2383 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162384 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:402385 _LIBCPP_INLINE_VISIBILITY
2386 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162387 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:402388 _LIBCPP_INLINE_VISIBILITY
2389 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162390 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:402391 _LIBCPP_INLINE_VISIBILITY
2392 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162393 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:402394 _LIBCPP_INLINE_VISIBILITY
2395 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant2c39cbe2013-06-27 19:35:322396 {return begin() + (__p - cbegin());}
Howard Hinnantbc8d3f92010-05-11 19:42:162397
Howard Hinnantee6ccd02010-09-23 18:58:282398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162399 void __copy_assign_alloc(const vector& __v)
2400 {__copy_assign_alloc(__v, integral_constant<bool,
2401 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:282402 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162403 void __copy_assign_alloc(const vector& __c, true_type)
2404 {
2405 if (__alloc() != __c.__alloc())
2406 deallocate();
2407 __alloc() = __c.__alloc();
2408 }
2409
Howard Hinnantee6ccd02010-09-23 18:58:282410 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:042411 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:162412 {}
2413
2414 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:402415 void __move_assign(vector& __c, true_type)
2416 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:282417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162418 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:402419 _NOEXCEPT_(
2420 !__storage_traits::propagate_on_container_move_assignment::value ||
2421 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162422 {__move_assign_alloc(__c, integral_constant<bool,
2423 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:282424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:312425 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:402426 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162427 {
Howard Hinnant0949eed2011-06-30 21:18:192428 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:162429 }
2430
Howard Hinnantee6ccd02010-09-23 18:58:282431 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:042432 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:402433 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162434 {}
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)
Howard Hinnantd1d27a42011-06-03 19:40:402438 _NOEXCEPT_(
2439 !__storage_traits::propagate_on_container_swap::value ||
2440 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162441 {__swap_alloc(__x, __y, integral_constant<bool,
2442 __storage_traits::propagate_on_container_swap::value>());}
2443
Howard Hinnantee6ccd02010-09-23 18:58:282444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162445 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:402446 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162447 {
Howard Hinnant0949eed2011-06-30 21:18:192448 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:162449 swap(__x, __y);
2450 }
Howard Hinnantee6ccd02010-09-23 18:58:282451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:042452 static void __swap_alloc(__storage_allocator&, __storage_allocator&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:402453 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162454 {}
2455
Howard Hinnantd1d27a42011-06-03 19:40:402456 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:162457
2458 friend class __bit_reference<vector>;
2459 friend class __bit_const_reference<vector>;
2460 friend class __bit_iterator<vector, false>;
2461 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:182462 friend struct __bit_array<vector>;
Howard Hinnant0f678bd2013-08-12 18:38:342463 friend struct _LIBCPP_TYPE_VIS_ONLY hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:162464};
2465
2466template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002467inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162468void
2469vector<bool, _Allocator>::__invalidate_all_iterators()
2470{
Howard Hinnantbc8d3f92010-05-11 19:42:162471}
2472
2473// Allocate space for __n objects
2474// throws length_error if __n > max_size()
2475// throws (probably bad_alloc) if memory run out
2476// Precondition: __begin_ == __end_ == __cap() == 0
2477// Precondition: __n > 0
2478// Postcondition: capacity() == __n
2479// Postcondition: size() == 0
2480template <class _Allocator>
2481void
2482vector<bool, _Allocator>::allocate(size_type __n)
2483{
2484 if (__n > max_size())
2485 this->__throw_length_error();
2486 __n = __external_cap_to_internal(__n);
2487 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2488 this->__size_ = 0;
2489 this->__cap() = __n;
2490}
2491
2492template <class _Allocator>
2493void
Howard Hinnantd1d27a42011-06-03 19:40:402494vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162495{
Howard Hinnant2c39cbe2013-06-27 19:35:322496 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162497 {
2498 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2499 __invalidate_all_iterators();
Howard Hinnant2c39cbe2013-06-27 19:35:322500 this->__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:162501 this->__size_ = this->__cap() = 0;
2502 }
2503}
2504
2505template <class _Allocator>
2506typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:402507vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162508{
2509 size_type __amax = __storage_traits::max_size(__alloc());
2510 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2511 if (__nmax / __bits_per_word <= __amax)
2512 return __nmax;
2513 return __internal_cap_to_external(__amax);
2514}
2515
2516// Precondition: __new_size > capacity()
2517template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002518inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162519typename vector<bool, _Allocator>::size_type
2520vector<bool, _Allocator>::__recommend(size_type __new_size) const
2521{
2522 const size_type __ms = max_size();
2523 if (__new_size > __ms)
2524 this->__throw_length_error();
2525 const size_type __cap = capacity();
2526 if (__cap >= __ms / 2)
2527 return __ms;
Howard Hinnant7f764502013-08-14 18:00:202528 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:162529}
2530
2531// Default constructs __n objects starting at __end_
2532// Precondition: __n > 0
2533// Precondition: size() + __n <= capacity()
2534// Postcondition: size() == size() + __n
2535template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002536inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162537void
2538vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2539{
2540 size_type __old_size = this->__size_;
2541 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:192542 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162543}
2544
2545template <class _Allocator>
2546template <class _ForwardIterator>
2547typename enable_if
2548<
2549 __is_forward_iterator<_ForwardIterator>::value,
2550 void
2551>::type
2552vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2553{
2554 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:192555 this->__size_ += _VSTD::distance(__first, __last);
2556 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:162557}
2558
2559template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002560inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162561vector<bool, _Allocator>::vector()
Howard Hinnantd1d27a42011-06-03 19:40:402562 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:322563 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162564 __size_(0),
2565 __cap_alloc_(0)
2566{
2567}
2568
2569template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002570inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162571vector<bool, _Allocator>::vector(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:322572 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162573 __size_(0),
2574 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2575{
2576}
2577
2578template <class _Allocator>
2579vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant2c39cbe2013-06-27 19:35:322580 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162581 __size_(0),
2582 __cap_alloc_(0)
2583{
2584 if (__n > 0)
2585 {
2586 allocate(__n);
2587 __construct_at_end(__n, false);
2588 }
2589}
2590
Marshall Clowa49a2c92013-09-14 00:47:592591#if _LIBCPP_STD_VER > 11
2592template <class _Allocator>
2593vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2594 : __begin_(nullptr),
2595 __size_(0),
2596 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2597{
2598 if (__n > 0)
2599 {
2600 allocate(__n);
2601 __construct_at_end(__n, false);
2602 }
2603}
2604#endif
2605
Howard Hinnantbc8d3f92010-05-11 19:42:162606template <class _Allocator>
2607vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant2c39cbe2013-06-27 19:35:322608 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162609 __size_(0),
2610 __cap_alloc_(0)
2611{
2612 if (__n > 0)
2613 {
2614 allocate(__n);
2615 __construct_at_end(__n, __x);
2616 }
2617}
2618
2619template <class _Allocator>
2620vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:322621 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162622 __size_(0),
2623 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2624{
2625 if (__n > 0)
2626 {
2627 allocate(__n);
2628 __construct_at_end(__n, __x);
2629 }
2630}
2631
2632template <class _Allocator>
2633template <class _InputIterator>
2634vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2635 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2636 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:322637 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162638 __size_(0),
2639 __cap_alloc_(0)
2640{
2641#ifndef _LIBCPP_NO_EXCEPTIONS
2642 try
2643 {
Howard Hinnant324bb032010-08-22 00:02:432644#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162645 for (; __first != __last; ++__first)
2646 push_back(*__first);
2647#ifndef _LIBCPP_NO_EXCEPTIONS
2648 }
2649 catch (...)
2650 {
Howard Hinnant2c39cbe2013-06-27 19:35:322651 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162652 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2653 __invalidate_all_iterators();
2654 throw;
2655 }
Howard Hinnant324bb032010-08-22 00:02:432656#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162657}
2658
2659template <class _Allocator>
2660template <class _InputIterator>
2661vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2662 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2663 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:322664 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162665 __size_(0),
2666 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2667{
2668#ifndef _LIBCPP_NO_EXCEPTIONS
2669 try
2670 {
Howard Hinnant324bb032010-08-22 00:02:432671#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162672 for (; __first != __last; ++__first)
2673 push_back(*__first);
2674#ifndef _LIBCPP_NO_EXCEPTIONS
2675 }
2676 catch (...)
2677 {
Howard Hinnant2c39cbe2013-06-27 19:35:322678 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162679 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2680 __invalidate_all_iterators();
2681 throw;
2682 }
Howard Hinnant324bb032010-08-22 00:02:432683#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162684}
2685
2686template <class _Allocator>
2687template <class _ForwardIterator>
2688vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2689 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:322690 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162691 __size_(0),
2692 __cap_alloc_(0)
2693{
Howard Hinnant0949eed2011-06-30 21:18:192694 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:162695 if (__n > 0)
2696 {
2697 allocate(__n);
2698 __construct_at_end(__first, __last);
2699 }
2700}
2701
2702template <class _Allocator>
2703template <class _ForwardIterator>
2704vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2705 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:322706 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162707 __size_(0),
2708 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2709{
Howard Hinnant0949eed2011-06-30 21:18:192710 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:162711 if (__n > 0)
2712 {
2713 allocate(__n);
2714 __construct_at_end(__first, __last);
2715 }
2716}
2717
Howard Hinnante3e32912011-08-12 21:56:022718#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2719
Howard Hinnantbc8d3f92010-05-11 19:42:162720template <class _Allocator>
2721vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant2c39cbe2013-06-27 19:35:322722 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162723 __size_(0),
2724 __cap_alloc_(0)
2725{
2726 size_type __n = static_cast<size_type>(__il.size());
2727 if (__n > 0)
2728 {
2729 allocate(__n);
2730 __construct_at_end(__il.begin(), __il.end());
2731 }
2732}
2733
2734template <class _Allocator>
2735vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:322736 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162737 __size_(0),
2738 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2739{
2740 size_type __n = static_cast<size_type>(__il.size());
2741 if (__n > 0)
2742 {
2743 allocate(__n);
2744 __construct_at_end(__il.begin(), __il.end());
2745 }
2746}
2747
Howard Hinnante3e32912011-08-12 21:56:022748#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2749
Howard Hinnantbc8d3f92010-05-11 19:42:162750template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:162751vector<bool, _Allocator>::~vector()
2752{
Howard Hinnant2c39cbe2013-06-27 19:35:322753 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162754 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantbc8d3f92010-05-11 19:42:162755 __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:162756}
2757
2758template <class _Allocator>
2759vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant2c39cbe2013-06-27 19:35:322760 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162761 __size_(0),
2762 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2763{
2764 if (__v.size() > 0)
2765 {
2766 allocate(__v.size());
2767 __construct_at_end(__v.begin(), __v.end());
2768 }
2769}
2770
2771template <class _Allocator>
2772vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:322773 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162774 __size_(0),
2775 __cap_alloc_(0, __a)
2776{
2777 if (__v.size() > 0)
2778 {
2779 allocate(__v.size());
2780 __construct_at_end(__v.begin(), __v.end());
2781 }
2782}
2783
2784template <class _Allocator>
2785vector<bool, _Allocator>&
2786vector<bool, _Allocator>::operator=(const vector& __v)
2787{
2788 if (this != &__v)
2789 {
2790 __copy_assign_alloc(__v);
2791 if (__v.__size_)
2792 {
2793 if (__v.__size_ > capacity())
2794 {
2795 deallocate();
2796 allocate(__v.__size_);
2797 }
Howard Hinnant0949eed2011-06-30 21:18:192798 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:162799 }
2800 __size_ = __v.__size_;
2801 }
2802 return *this;
2803}
2804
Howard Hinnant73d21a42010-09-04 23:28:192805#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2806
Howard Hinnantbc8d3f92010-05-11 19:42:162807template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002808inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162809vector<bool, _Allocator>::vector(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:402810 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162811 : __begin_(__v.__begin_),
2812 __size_(__v.__size_),
2813 __cap_alloc_(__v.__cap_alloc_)
2814{
Howard Hinnant2c39cbe2013-06-27 19:35:322815 __v.__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:162816 __v.__size_ = 0;
2817 __v.__cap() = 0;
2818}
2819
2820template <class _Allocator>
2821vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:322822 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162823 __size_(0),
2824 __cap_alloc_(0, __a)
2825{
2826 if (__a == allocator_type(__v.__alloc()))
2827 {
2828 this->__begin_ = __v.__begin_;
2829 this->__size_ = __v.__size_;
2830 this->__cap() = __v.__cap();
2831 __v.__begin_ = nullptr;
2832 __v.__cap() = __v.__size_ = 0;
2833 }
2834 else if (__v.size() > 0)
2835 {
2836 allocate(__v.size());
2837 __construct_at_end(__v.begin(), __v.end());
2838 }
2839}
2840
2841template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002842inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162843vector<bool, _Allocator>&
2844vector<bool, _Allocator>::operator=(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:402845 _NOEXCEPT_(
2846 __alloc_traits::propagate_on_container_move_assignment::value &&
2847 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162848{
2849 __move_assign(__v, integral_constant<bool,
2850 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:452851 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:162852}
2853
2854template <class _Allocator>
2855void
2856vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2857{
2858 if (__alloc() != __c.__alloc())
2859 assign(__c.begin(), __c.end());
2860 else
2861 __move_assign(__c, true_type());
2862}
2863
2864template <class _Allocator>
2865void
2866vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:402867 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162868{
2869 deallocate();
Marshall Clowdb5e54d2014-07-21 15:15:152870 __move_assign_alloc(__c);
Howard Hinnantbc8d3f92010-05-11 19:42:162871 this->__begin_ = __c.__begin_;
2872 this->__size_ = __c.__size_;
2873 this->__cap() = __c.__cap();
Howard Hinnantbc8d3f92010-05-11 19:42:162874 __c.__begin_ = nullptr;
2875 __c.__cap() = __c.__size_ = 0;
2876}
Howard Hinnant73d21a42010-09-04 23:28:192877
2878#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:162879
2880template <class _Allocator>
2881void
2882vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2883{
2884 __size_ = 0;
2885 if (__n > 0)
2886 {
2887 size_type __c = capacity();
2888 if (__n <= __c)
2889 __size_ = __n;
2890 else
2891 {
2892 vector __v(__alloc());
2893 __v.reserve(__recommend(__n));
2894 __v.__size_ = __n;
2895 swap(__v);
2896 }
Howard Hinnant0949eed2011-06-30 21:18:192897 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162898 }
2899}
2900
2901template <class _Allocator>
2902template <class _InputIterator>
2903typename enable_if
2904<
2905 __is_input_iterator<_InputIterator>::value &&
2906 !__is_forward_iterator<_InputIterator>::value,
2907 void
2908>::type
2909vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2910{
2911 clear();
2912 for (; __first != __last; ++__first)
2913 push_back(*__first);
2914}
2915
2916template <class _Allocator>
2917template <class _ForwardIterator>
2918typename enable_if
2919<
2920 __is_forward_iterator<_ForwardIterator>::value,
2921 void
2922>::type
2923vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2924{
2925 clear();
Howard Hinnant0949eed2011-06-30 21:18:192926 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:162927 if (__n)
2928 {
2929 if (__n > capacity())
2930 {
2931 deallocate();
2932 allocate(__n);
2933 }
2934 __construct_at_end(__first, __last);
2935 }
2936}
2937
2938template <class _Allocator>
2939void
2940vector<bool, _Allocator>::reserve(size_type __n)
2941{
2942 if (__n > capacity())
2943 {
2944 vector __v(this->__alloc());
2945 __v.allocate(__n);
2946 __v.__construct_at_end(this->begin(), this->end());
2947 swap(__v);
2948 __invalidate_all_iterators();
2949 }
2950}
2951
2952template <class _Allocator>
2953void
Howard Hinnantd1d27a42011-06-03 19:40:402954vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162955{
2956 if (__external_cap_to_internal(size()) > __cap())
2957 {
2958#ifndef _LIBCPP_NO_EXCEPTIONS
2959 try
2960 {
Howard Hinnant324bb032010-08-22 00:02:432961#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162962 vector(*this, allocator_type(__alloc())).swap(*this);
2963#ifndef _LIBCPP_NO_EXCEPTIONS
2964 }
2965 catch (...)
2966 {
2967 }
Howard Hinnant324bb032010-08-22 00:02:432968#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162969 }
2970}
2971
2972template <class _Allocator>
2973typename vector<bool, _Allocator>::reference
2974vector<bool, _Allocator>::at(size_type __n)
2975{
2976 if (__n >= size())
2977 this->__throw_out_of_range();
2978 return (*this)[__n];
2979}
2980
2981template <class _Allocator>
2982typename vector<bool, _Allocator>::const_reference
2983vector<bool, _Allocator>::at(size_type __n) const
2984{
2985 if (__n >= size())
2986 this->__throw_out_of_range();
2987 return (*this)[__n];
2988}
2989
2990template <class _Allocator>
2991void
2992vector<bool, _Allocator>::push_back(const value_type& __x)
2993{
2994 if (this->__size_ == this->capacity())
2995 reserve(__recommend(this->__size_ + 1));
2996 ++this->__size_;
2997 back() = __x;
2998}
2999
3000template <class _Allocator>
3001typename vector<bool, _Allocator>::iterator
3002vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
3003{
3004 iterator __r;
3005 if (size() < capacity())
3006 {
3007 const_iterator __old_end = end();
3008 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:193009 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:163010 __r = __const_iterator_cast(__position);
3011 }
3012 else
3013 {
3014 vector __v(__alloc());
3015 __v.reserve(__recommend(__size_ + 1));
3016 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:193017 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3018 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:163019 swap(__v);
3020 }
3021 *__r = __x;
3022 return __r;
3023}
3024
3025template <class _Allocator>
3026typename vector<bool, _Allocator>::iterator
3027vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3028{
3029 iterator __r;
3030 size_type __c = capacity();
3031 if (__n <= __c && size() <= __c - __n)
3032 {
3033 const_iterator __old_end = end();
3034 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:193035 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:163036 __r = __const_iterator_cast(__position);
3037 }
3038 else
3039 {
3040 vector __v(__alloc());
3041 __v.reserve(__recommend(__size_ + __n));
3042 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:193043 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3044 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:163045 swap(__v);
3046 }
Howard Hinnant0949eed2011-06-30 21:18:193047 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:163048 return __r;
3049}
3050
3051template <class _Allocator>
3052template <class _InputIterator>
3053typename enable_if
3054<
3055 __is_input_iterator <_InputIterator>::value &&
3056 !__is_forward_iterator<_InputIterator>::value,
3057 typename vector<bool, _Allocator>::iterator
3058>::type
3059vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3060{
3061 difference_type __off = __position - begin();
3062 iterator __p = __const_iterator_cast(__position);
3063 iterator __old_end = end();
3064 for (; size() != capacity() && __first != __last; ++__first)
3065 {
3066 ++this->__size_;
3067 back() = *__first;
3068 }
3069 vector __v(__alloc());
3070 if (__first != __last)
3071 {
3072#ifndef _LIBCPP_NO_EXCEPTIONS
3073 try
3074 {
Howard Hinnant324bb032010-08-22 00:02:433075#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:163076 __v.assign(__first, __last);
3077 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3078 difference_type __old_p = __p - begin();
3079 reserve(__recommend(size() + __v.size()));
3080 __p = begin() + __old_p;
3081 __old_end = begin() + __old_size;
3082#ifndef _LIBCPP_NO_EXCEPTIONS
3083 }
3084 catch (...)
3085 {
3086 erase(__old_end, end());
3087 throw;
3088 }
Howard Hinnant324bb032010-08-22 00:02:433089#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:163090 }
Howard Hinnant0949eed2011-06-30 21:18:193091 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:163092 insert(__p, __v.begin(), __v.end());
3093 return begin() + __off;
3094}
3095
3096template <class _Allocator>
3097template <class _ForwardIterator>
3098typename enable_if
3099<
3100 __is_forward_iterator<_ForwardIterator>::value,
3101 typename vector<bool, _Allocator>::iterator
3102>::type
3103vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3104{
Howard Hinnant0949eed2011-06-30 21:18:193105 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:163106 iterator __r;
3107 size_type __c = capacity();
3108 if (__n <= __c && size() <= __c - __n)
3109 {
3110 const_iterator __old_end = end();
3111 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:193112 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:163113 __r = __const_iterator_cast(__position);
3114 }
3115 else
3116 {
3117 vector __v(__alloc());
3118 __v.reserve(__recommend(__size_ + __n));
3119 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:193120 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3121 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:163122 swap(__v);
3123 }
Howard Hinnant0949eed2011-06-30 21:18:193124 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:163125 return __r;
3126}
3127
3128template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003129inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163130typename vector<bool, _Allocator>::iterator
3131vector<bool, _Allocator>::erase(const_iterator __position)
3132{
3133 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:193134 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:163135 --__size_;
3136 return __r;
3137}
3138
3139template <class _Allocator>
3140typename vector<bool, _Allocator>::iterator
3141vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3142{
3143 iterator __r = __const_iterator_cast(__first);
3144 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:193145 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:163146 __size_ -= __d;
3147 return __r;
3148}
3149
3150template <class _Allocator>
3151void
3152vector<bool, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:403153 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3154 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:163155{
Howard Hinnant0949eed2011-06-30 21:18:193156 _VSTD::swap(this->__begin_, __x.__begin_);
3157 _VSTD::swap(this->__size_, __x.__size_);
3158 _VSTD::swap(this->__cap(), __x.__cap());
Howard Hinnantbc8d3f92010-05-11 19:42:163159 __swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:163160}
3161
Howard Hinnant324bb032010-08-22 00:02:433162template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:163163void
3164vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3165{
3166 size_type __cs = size();
3167 if (__cs < __sz)
3168 {
3169 iterator __r;
3170 size_type __c = capacity();
3171 size_type __n = __sz - __cs;
3172 if (__n <= __c && __cs <= __c - __n)
3173 {
3174 __r = end();
3175 __size_ += __n;
3176 }
3177 else
3178 {
3179 vector __v(__alloc());
3180 __v.reserve(__recommend(__size_ + __n));
3181 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:193182 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:163183 swap(__v);
3184 }
Howard Hinnant0949eed2011-06-30 21:18:193185 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:163186 }
3187 else
3188 __size_ = __sz;
3189}
3190
Howard Hinnant324bb032010-08-22 00:02:433191template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:163192void
Howard Hinnantd1d27a42011-06-03 19:40:403193vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:163194{
3195 // do middle whole words
3196 size_type __n = __size_;
3197 __storage_pointer __p = __begin_;
3198 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3199 *__p = ~*__p;
3200 // do last partial word
3201 if (__n > 0)
3202 {
3203 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3204 __storage_type __b = *__p & __m;
3205 *__p &= ~__m;
3206 *__p |= ~__b & __m;
3207 }
3208}
3209
Howard Hinnant324bb032010-08-22 00:02:433210template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:163211bool
3212vector<bool, _Allocator>::__invariants() const
3213{
Howard Hinnant2c39cbe2013-06-27 19:35:323214 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:163215 {
3216 if (this->__size_ != 0 || this->__cap() != 0)
3217 return false;
3218 }
3219 else
3220 {
3221 if (this->__cap() == 0)
3222 return false;
3223 if (this->__size_ > this->capacity())
3224 return false;
3225 }
3226 return true;
3227}
3228
Howard Hinnant324bb032010-08-22 00:02:433229template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:163230size_t
Howard Hinnantd1d27a42011-06-03 19:40:403231vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:163232{
3233 size_t __h = 0;
3234 // do middle whole words
3235 size_type __n = __size_;
3236 __storage_pointer __p = __begin_;
3237 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3238 __h ^= *__p;
3239 // do last partial word
3240 if (__n > 0)
3241 {
3242 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3243 __h ^= *__p & __m;
3244 }
3245 return __h;
3246}
3247
3248template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:343249struct _LIBCPP_TYPE_VIS_ONLY hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:163250 : public unary_function<vector<bool, _Allocator>, size_t>
3251{
Howard Hinnantee6ccd02010-09-23 18:58:283252 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:403253 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:163254 {return __vec.__hash_code();}
3255};
3256
3257template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003258inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163259bool
3260operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3261{
3262 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:193263 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:163264}
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{
3271 return !(__x == __y);
3272}
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{
Howard Hinnant0949eed2011-06-30 21:18:193279 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:163280}
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 __y < __x;
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 !(__x < __y);
3296}
3297
3298template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003299inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163300bool
3301operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3302{
3303 return !(__y < __x);
3304}
3305
3306template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003307inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163308void
3309swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:403310 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:163311{
3312 __x.swap(__y);
3313}
3314
3315_LIBCPP_END_NAMESPACE_STD
3316
3317#endif // _LIBCPP_VECTOR