blob: 074e44546afd6ecd5ef5110208416c4cd00bade2 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------------ vector --------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_VECTOR
12#define _LIBCPP_VECTOR
13
14/*
15 vector synopsis
16
17namespace std
18{
19
Howard Hinnant324bb032010-08-22 00:02:43 +000020template <class T, class Allocator = allocator<T> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021class vector
Howard Hinnant324bb032010-08-22 00:02:43 +000022{
23public:
24 typedef T value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000025 typedef Allocator allocator_type;
26 typedef typename allocator_type::reference reference;
27 typedef typename allocator_type::const_reference const_reference;
28 typedef implementation-defined iterator;
29 typedef implementation-defined const_iterator;
30 typedef typename allocator_type::size_type size_type;
31 typedef typename allocator_type::difference_type difference_type;
32 typedef typename allocator_type::pointer pointer;
33 typedef typename allocator_type::const_pointer const_pointer;
34 typedef std::reverse_iterator<iterator> reverse_iterator;
35 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
36
37 explicit vector(const allocator_type& = allocator_type());
38 explicit vector(size_type n);
39 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
40 template <class InputIterator>
41 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
42 vector(const vector& x);
43 vector(vector&& x);
44 vector(initializer_list<value_type> il);
45 vector(initializer_list<value_type> il, const allocator_type& a);
46 ~vector();
47 vector& operator=(const vector& x);
48 vector& operator=(vector&& x);
49 vector& operator=(initializer_list<value_type> il);
50 template <class InputIterator>
51 void assign(InputIterator first, InputIterator last);
52 void assign(size_type n, const value_type& u);
53 void assign(initializer_list<value_type> il);
54
55 allocator_type get_allocator() const;
56
57 iterator begin();
58 const_iterator begin() const;
59 iterator end();
60 const_iterator end() const;
61
62 reverse_iterator rbegin();
63 const_reverse_iterator rbegin() const;
64 reverse_iterator rend();
65 const_reverse_iterator rend() const;
66
67 const_iterator cbegin() const;
68 const_iterator cend() const;
69 const_reverse_iterator crbegin() const;
70 const_reverse_iterator crend() const;
71
72 size_type size() const;
73 size_type max_size() const;
74 size_type capacity() const;
75 bool empty() const;
76 void reserve(size_type n);
77 void shrink_to_fit();
78
79 reference operator[](size_type n);
80 const_reference operator[](size_type n) const;
81 reference at(size_type n);
82 const_reference at(size_type n) const;
83
84 reference front();
85 const_reference front() const;
86 reference back();
87 const_reference back() const;
88
89 value_type* data();
90 const value_type* data() const;
91
92 void push_back(const value_type& x);
93 void push_back(value_type&& x);
94 template <class... Args>
95 void emplace_back(Args&&... args);
96 void pop_back();
97
98 template <class... Args> iterator emplace(const_iterator position, Args&&... args);
99 iterator insert(const_iterator position, const value_type& x);
100 iterator insert(const_iterator position, value_type&& x);
101 iterator insert(const_iterator position, size_type n, const value_type& x);
102 template <class InputIterator>
103 iterator insert(const_iterator position, InputIterator first, InputIterator last);
104 iterator insert(const_iterator position, initializer_list<value_type> il);
105
106 iterator erase(const_iterator position);
107 iterator erase(const_iterator first, const_iterator last);
108
109 void clear();
110
111 void resize(size_type sz);
112 void resize(size_type sz, const value_type& c);
113
114 void swap(vector&);
115
116 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000117};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000118
Howard Hinnant324bb032010-08-22 00:02:43 +0000119template <class Allocator = allocator<T> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000120class vector<bool, Allocator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000121{
122public:
123 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000124 typedef Allocator allocator_type;
125 typedef implementation-defined iterator;
126 typedef implementation-defined const_iterator;
127 typedef typename allocator_type::size_type size_type;
128 typedef typename allocator_type::difference_type difference_type;
129 typedef iterator pointer;
130 typedef const_iterator const_pointer;
131 typedef std::reverse_iterator<iterator> reverse_iterator;
132 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
133
134 class reference
135 {
136 public:
137 reference(const reference&);
138 operator bool() const;
139 reference& operator=(const bool x);
140 reference& operator=(const reference& x);
141 iterator operator&() const;
142 void flip();
143 };
144
145 class const_reference
146 {
147 public:
148 const_reference(const reference&);
149 operator bool() const;
150 const_iterator operator&() const;
151 };
152
153 explicit vector(const allocator_type& = allocator_type());
154 explicit vector(size_type n, const value_type& value = value_type(), const allocator_type& = allocator_type());
155 template <class InputIterator>
156 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
157 vector(const vector& x);
158 vector(vector&& x);
159 vector(initializer_list<value_type> il);
160 vector(initializer_list<value_type> il, const allocator_type& a);
161 ~vector();
162 vector& operator=(const vector& x);
163 vector& operator=(vector&& x);
164 vector& operator=(initializer_list<value_type> il);
165 template <class InputIterator>
166 void assign(InputIterator first, InputIterator last);
167 void assign(size_type n, const value_type& u);
168 void assign(initializer_list<value_type> il);
169
170 allocator_type get_allocator() const;
171
172 iterator begin();
173 const_iterator begin() const;
174 iterator end();
175 const_iterator end() const;
176
177 reverse_iterator rbegin();
178 const_reverse_iterator rbegin() const;
179 reverse_iterator rend();
180 const_reverse_iterator rend() const;
181
182 const_iterator cbegin() const;
183 const_iterator cend() const;
184 const_reverse_iterator crbegin() const;
185 const_reverse_iterator crend() const;
186
187 size_type size() const;
188 size_type max_size() const;
189 size_type capacity() const;
190 bool empty() const;
191 void reserve(size_type n);
192 void shrink_to_fit();
193
194 reference operator[](size_type n);
195 const_reference operator[](size_type n) const;
196 reference at(size_type n);
197 const_reference at(size_type n) const;
198
199 reference front();
200 const_reference front() const;
201 reference back();
202 const_reference back() const;
203
204 void push_back(const value_type& x);
205 void pop_back();
206
207 iterator insert(const_iterator position, const value_type& x);
208 iterator insert(const_iterator position, size_type n, const value_type& x);
209 template <class InputIterator>
210 iterator insert(const_iterator position, InputIterator first, InputIterator last);
211 iterator insert(const_iterator position, initializer_list<value_type> il);
212
213 iterator erase(const_iterator position);
214 iterator erase(const_iterator first, const_iterator last);
215
216 void clear();
217
218 void resize(size_type sz);
219 void resize(size_type sz, value_type x);
220
221 void swap(vector&);
222 void flip();
223
224 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000225};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000226
227template <class Allocator> struct hash<std::vector<bool, Allocator>>;
228
229template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
230template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
231template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
232template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
233template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
234template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
235
236template <class T, class Allocator> void swap(vector<T,Allocator>& x, vector<T,Allocator>& y);
237
238} // std
239
240*/
241
242#include <__config>
243#include <__bit_reference>
244#include <type_traits>
245#include <climits>
246#include <limits>
247#include <initializer_list>
248#include <memory>
249#include <stdexcept>
250#include <algorithm>
251#include <cstring>
252#include <__split_buffer>
253#include <__functional_base>
254#if defined(_LIBCPP_DEBUG) || defined(_LIBCPP_NO_EXCEPTIONS)
255 #include <cassert>
256#endif
257
258#pragma GCC system_header
259
260_LIBCPP_BEGIN_NAMESPACE_STD
261
262template <bool>
263class __vector_base_common
264{
265protected:
266 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
267 void __throw_length_error() const;
268 void __throw_out_of_range() const;
269};
270
271template <bool __b>
272void
273__vector_base_common<__b>::__throw_length_error() const
274{
275#ifndef _LIBCPP_NO_EXCEPTIONS
276 throw length_error("vector");
277#else
278 assert(!"vector length_error");
279#endif
280}
281
282template <bool __b>
283void
284__vector_base_common<__b>::__throw_out_of_range() const
285{
286#ifndef _LIBCPP_NO_EXCEPTIONS
287 throw out_of_range("vector");
288#else
289 assert(!"vector out_of_range");
290#endif
291}
292
293extern template class __vector_base_common<true>;
294
295template <class _Tp, class _Allocator>
296class __vector_base
297 : protected __vector_base_common<true>
298{
299protected:
Howard Hinnant324bb032010-08-22 00:02:43 +0000300 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000301 typedef _Allocator allocator_type;
302 typedef allocator_traits<allocator_type> __alloc_traits;
303 typedef value_type& reference;
304 typedef const value_type& const_reference;
305 typedef typename __alloc_traits::size_type size_type;
306 typedef typename __alloc_traits::difference_type difference_type;
307 typedef typename __alloc_traits::pointer pointer;
308 typedef typename __alloc_traits::const_pointer const_pointer;
309 typedef pointer iterator;
310 typedef const_pointer const_iterator;
311
312 pointer __begin_;
313 pointer __end_;
314 __compressed_pair<pointer, allocator_type> __end_cap_;
315
316 _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __end_cap_.second();}
317 _LIBCPP_INLINE_VISIBILITY const allocator_type& __alloc() const {return __end_cap_.second();}
318 _LIBCPP_INLINE_VISIBILITY pointer& __end_cap() {return __end_cap_.first();}
319 _LIBCPP_INLINE_VISIBILITY const pointer& __end_cap() const {return __end_cap_.first();}
320
321 __vector_base();
322 __vector_base(const allocator_type& __a);
323 ~__vector_base();
324
325 _LIBCPP_INLINE_VISIBILITY void clear() {__destruct_at_end(__begin_);}
326 _LIBCPP_INLINE_VISIBILITY size_type capacity() const {return static_cast<size_type>(__end_cap() - __begin_);}
327
328 _LIBCPP_INLINE_VISIBILITY void __destruct_at_end(const_pointer __new_last)
Howard Hinnant1468b662010-11-19 22:17:28 +0000329 {__destruct_at_end(__new_last, is_trivially_destructible<value_type>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000330 void __destruct_at_end(const_pointer __new_last, false_type);
331 void __destruct_at_end(const_pointer __new_last, true_type);
332
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000334 void __copy_assign_alloc(const __vector_base& __c)
335 {__copy_assign_alloc(__c, integral_constant<bool,
336 __alloc_traits::propagate_on_container_copy_assignment::value>());}
337
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000339 void __move_assign_alloc(__vector_base& __c)
340 {__move_assign_alloc(__c, integral_constant<bool,
341 __alloc_traits::propagate_on_container_move_assignment::value>());}
342
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000344 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
345 {__swap_alloc(__x, __y, integral_constant<bool,
346 __alloc_traits::propagate_on_container_swap::value>());}
347private:
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000348 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000349 void __copy_assign_alloc(const __vector_base& __c, true_type)
350 {
351 if (__alloc() != __c.__alloc())
352 {
353 clear();
354 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
355 __begin_ = __end_ = __end_cap() = nullptr;
356 }
357 __alloc() = __c.__alloc();
358 }
359
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000361 void __copy_assign_alloc(const __vector_base& __c, false_type)
362 {}
363
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000364 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000365 void __move_assign_alloc(const __vector_base& __c, true_type)
366 {
367 __alloc() = _STD::move(__c.__alloc());
368 }
369
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371 void __move_assign_alloc(const __vector_base& __c, false_type)
372 {}
373
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
376 {
377 using _STD::swap;
378 swap(__x, __y);
379 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381 static void __swap_alloc(allocator_type& __x, allocator_type& __y, false_type)
382 {}
383};
384
385template <class _Tp, class _Allocator>
386_LIBCPP_INLINE_VISIBILITY inline
387void
388__vector_base<_Tp, _Allocator>::__destruct_at_end(const_pointer __new_last, false_type)
389{
390 while (__new_last < __end_)
391 __alloc_traits::destroy(__alloc(), const_cast<pointer>(--__end_));
392}
393
394template <class _Tp, class _Allocator>
395_LIBCPP_INLINE_VISIBILITY inline
396void
397__vector_base<_Tp, _Allocator>::__destruct_at_end(const_pointer __new_last, true_type)
398{
399 __end_ = const_cast<pointer>(__new_last);
400}
401
402template <class _Tp, class _Allocator>
403_LIBCPP_INLINE_VISIBILITY inline
404__vector_base<_Tp, _Allocator>::__vector_base()
405 : __begin_(0),
406 __end_(0),
407 __end_cap_(0)
408{
409}
410
411template <class _Tp, class _Allocator>
412_LIBCPP_INLINE_VISIBILITY inline
413__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
414 : __begin_(0),
415 __end_(0),
416 __end_cap_(0, __a)
417{
418}
419
420template <class _Tp, class _Allocator>
421__vector_base<_Tp, _Allocator>::~__vector_base()
422{
423 if (__begin_ != 0)
424 {
425 clear();
426 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
427 }
428}
429
430template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnant36cdf022010-09-10 16:42:26 +0000431class _LIBCPP_VISIBLE vector
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000432 : private __vector_base<_Tp, _Allocator>
433{
434private:
435 typedef __vector_base<_Tp, _Allocator> __base;
Howard Hinnant324bb032010-08-22 00:02:43 +0000436public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000437 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +0000438 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000439 typedef _Allocator allocator_type;
440 typedef typename __base::__alloc_traits __alloc_traits;
441 typedef typename __base::reference reference;
442 typedef typename __base::const_reference const_reference;
443 typedef typename __base::size_type size_type;
444 typedef typename __base::difference_type difference_type;
445 typedef typename __base::pointer pointer;
446 typedef typename __base::const_pointer const_pointer;
447#ifdef _LIBCPP_DEBUG
448 typedef __debug_iter<vector, pointer> iterator;
449 typedef __debug_iter<vector, const_pointer> const_iterator;
450
451 friend class __debug_iter<vector, pointer>;
452 friend class __debug_iter<vector, const_pointer>;
453
454 pair<iterator*, const_iterator*> __iterator_list_;
455
456 _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;}
457 _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;}
458#elif defined(_LIBCPP_RAW_ITERATORS)
459 typedef pointer iterator;
460 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000461#else // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000462 typedef __wrap_iter<pointer> iterator;
463 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000464#endif // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465 typedef _STD::reverse_iterator<iterator> reverse_iterator;
466 typedef _STD::reverse_iterator<const_iterator> const_reverse_iterator;
467
468 _LIBCPP_INLINE_VISIBILITY vector() {}
469 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a) : __base(__a) {}
470 explicit vector(size_type __n);
471 vector(size_type __n, const_reference __x);
472 vector(size_type __n, const_reference __x, const allocator_type& __a);
473 template <class _InputIterator>
474 vector(_InputIterator __first, _InputIterator __last,
475 typename enable_if<__is_input_iterator <_InputIterator>::value &&
476 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
477 template <class _InputIterator>
478 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
479 typename enable_if<__is_input_iterator <_InputIterator>::value &&
480 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
481 template <class _ForwardIterator>
482 vector(_ForwardIterator __first, _ForwardIterator __last,
483 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
484 template <class _ForwardIterator>
485 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
486 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
487 vector(initializer_list<value_type> __il);
488 vector(initializer_list<value_type> __il, const allocator_type& __a);
489#ifdef _LIBCPP_DEBUG
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000490 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000491 ~vector() {__invalidate_all_iterators();}
492#endif
493
494 vector(const vector& __x);
495 vector(const vector& __x, const allocator_type& __a);
496 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000497#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000498 vector(vector&& __x);
499 vector(vector&& __x, const allocator_type& __a);
500 vector& operator=(vector&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000501#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000502 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000503 vector& operator=(initializer_list<value_type> __il)
504 {assign(__il.begin(), __il.end()); return *this;}
505
506 template <class _InputIterator>
507 typename enable_if
508 <
509 __is_input_iterator <_InputIterator>::value &&
510 !__is_forward_iterator<_InputIterator>::value,
511 void
512 >::type
513 assign(_InputIterator __first, _InputIterator __last);
514 template <class _ForwardIterator>
515 typename enable_if
516 <
517 __is_forward_iterator<_ForwardIterator>::value,
518 void
519 >::type
520 assign(_ForwardIterator __first, _ForwardIterator __last);
521
522 void assign(size_type __n, const_reference __u);
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000523 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000524 void assign(initializer_list<value_type> __il)
525 {assign(__il.begin(), __il.end());}
526
527 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const {return this->__alloc();}
528
529 iterator begin();
530 const_iterator begin() const;
531 iterator end();
532 const_iterator end() const;
533
534 _LIBCPP_INLINE_VISIBILITY reverse_iterator rbegin() {return reverse_iterator(end());}
535 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rbegin() const {return const_reverse_iterator(end());}
536 _LIBCPP_INLINE_VISIBILITY reverse_iterator rend() {return reverse_iterator(begin());}
537 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rend() const {return const_reverse_iterator(begin());}
538
539 _LIBCPP_INLINE_VISIBILITY const_iterator cbegin() const {return begin();}
540 _LIBCPP_INLINE_VISIBILITY const_iterator cend() const {return end();}
541 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crbegin() const {return rbegin();}
542 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crend() const {return rend();}
543
544 _LIBCPP_INLINE_VISIBILITY size_type size() const {return static_cast<size_type>(this->__end_ - this->__begin_);}
545 _LIBCPP_INLINE_VISIBILITY size_type capacity() const {return __base::capacity();}
546 _LIBCPP_INLINE_VISIBILITY bool empty() const {return this->__begin_ == this->__end_;}
547 size_type max_size() const;
548 void reserve(size_type __n);
549 void shrink_to_fit();
550
551 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
552 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
553 reference at(size_type __n);
554 const_reference at(size_type __n) const;
555
556 _LIBCPP_INLINE_VISIBILITY reference front() {return *this->__begin_;}
557 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return *this->__begin_;}
558 _LIBCPP_INLINE_VISIBILITY reference back() {return *(this->__end_ - 1);}
559 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return *(this->__end_ - 1);}
560
561 _LIBCPP_INLINE_VISIBILITY value_type* data()
562 {return _STD::__to_raw_pointer(this->__begin_);}
563 _LIBCPP_INLINE_VISIBILITY const value_type* data() const
564 {return _STD::__to_raw_pointer(this->__begin_);}
565
566 void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000567#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000568 void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000569#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000570 template <class... _Args>
571 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000572#endif // _LIBCPP_HAS_NO_VARIADICS
573#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000574 void pop_back();
575
576 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000577#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000578 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000579#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000580 template <class... _Args>
581 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000582#endif // _LIBCPP_HAS_NO_VARIADICS
583#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000584 iterator insert(const_iterator __position, size_type __n, const_reference __x);
585 template <class _InputIterator>
586 typename enable_if
587 <
588 __is_input_iterator <_InputIterator>::value &&
589 !__is_forward_iterator<_InputIterator>::value,
590 iterator
591 >::type
592 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
593 template <class _ForwardIterator>
594 typename enable_if
595 <
596 __is_forward_iterator<_ForwardIterator>::value,
597 iterator
598 >::type
599 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000600 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000601 iterator insert(const_iterator __position, initializer_list<value_type> __il)
602 {return insert(__position, __il.begin(), __il.end());}
603
604 iterator erase(const_iterator __position);
605 iterator erase(const_iterator __first, const_iterator __last);
606
607 _LIBCPP_INLINE_VISIBILITY void clear() {__base::clear();}
608
609 void resize(size_type __sz);
610 void resize(size_type __sz, const_reference __x);
611
612 void swap(vector&);
613
614 bool __invariants() const;
615
616private:
617 void __invalidate_all_iterators();
618 void allocate(size_type __n);
619 void deallocate();
620 size_type __recommend(size_type __new_size) const;
621 void __construct_at_end(size_type __n);
622 void __construct_at_end(size_type __n, false_type);
623 void __construct_at_end(size_type __n, true_type);
624 void __construct_at_end(size_type __n, const_reference __x);
625 void __construct_at_end(size_type __n, const_reference __x, false_type);
626 void __construct_at_end(size_type __n, const_reference __x, true_type);
627 template <class _ForwardIterator>
628 typename enable_if
629 <
630 __is_forward_iterator<_ForwardIterator>::value,
631 void
632 >::type
633 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
634 void __move_construct_at_end(pointer __first, pointer __last);
635 void __append(size_type __n);
636 void __append(size_type __n, const_reference __x);
637 iterator __make_iter(pointer __p);
638 const_iterator __make_iter(const_pointer __p) const;
639 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
640 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
641 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
642 void __move_assign(vector& __c, true_type);
643 void __move_assign(vector& __c, false_type);
644};
645
646template <class _Tp, class _Allocator>
647void
648vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
649{
650 for (pointer __p = this->__end_; this->__begin_ < __p;)
651 __v.push_front(_STD::move(*--__p));
652 _STD::swap(this->__begin_, __v.__begin_);
653 _STD::swap(this->__end_, __v.__end_);
654 _STD::swap(this->__end_cap(), __v.__end_cap());
655 __v.__first_ = __v.__begin_;
656 __invalidate_all_iterators();
657}
658
659template <class _Tp, class _Allocator>
660typename vector<_Tp, _Allocator>::pointer
661vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
662{
663 pointer __r = __v.__begin_;
664 for (pointer __i = __p; this->__begin_ < __i;)
665 __v.push_front(_STD::move(*--__i));
666 for (pointer __i = __p; __i < this->__end_; ++__i)
667 __v.push_back(_STD::move(*__i));
668 _STD::swap(this->__begin_, __v.__begin_);
669 _STD::swap(this->__end_, __v.__end_);
670 _STD::swap(this->__end_cap(), __v.__end_cap());
671 __v.__first_ = __v.__begin_;
672 __invalidate_all_iterators();
673 return __r;
674}
675
676// Allocate space for __n objects
677// throws length_error if __n > max_size()
678// throws (probably bad_alloc) if memory run out
679// Precondition: __begin_ == __end_ == __end_cap() == 0
680// Precondition: __n > 0
681// Postcondition: capacity() == __n
682// Postcondition: size() == 0
683template <class _Tp, class _Allocator>
684void
685vector<_Tp, _Allocator>::allocate(size_type __n)
686{
687 if (__n > max_size())
688 this->__throw_length_error();
689 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
690 this->__end_cap() = this->__begin_ + __n;
691}
692
693template <class _Tp, class _Allocator>
694void
695vector<_Tp, _Allocator>::deallocate()
696{
697 if (this->__begin_ != 0)
698 {
699 clear();
700 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
701 __invalidate_all_iterators();
702 this->__begin_ = this->__end_ = this->__end_cap() = 0;
703 }
704}
705
706template <class _Tp, class _Allocator>
707typename vector<_Tp, _Allocator>::size_type
708vector<_Tp, _Allocator>::max_size() const
709{
710 return _STD::min(__alloc_traits::max_size(this->__alloc()), numeric_limits<size_type>::max() / 2); // end() >= begin(), always
711}
712
713// Precondition: __new_size > capacity()
714template <class _Tp, class _Allocator>
715_LIBCPP_INLINE_VISIBILITY inline
716typename vector<_Tp, _Allocator>::size_type
717vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
718{
719 const size_type __ms = max_size();
720 if (__new_size > __ms)
721 this->__throw_length_error();
722 const size_type __cap = capacity();
723 if (__cap >= __ms / 2)
724 return __ms;
725 return _STD::max(2*__cap, __new_size);
726}
727
728// Default constructs __n objects starting at __end_
729// throws if construction throws
730// Precondition: __n > 0
731// Precondition: size() + __n <= capacity()
732// Postcondition: size() == size() + __n
733template <class _Tp, class _Allocator>
734_LIBCPP_INLINE_VISIBILITY inline
735void
736vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
737{
738 __construct_at_end(__n, __is_zero_default_constructible<value_type>());
739}
740
741template <class _Tp, class _Allocator>
742void
743vector<_Tp, _Allocator>::__construct_at_end(size_type __n, false_type)
744{
745 allocator_type& __a = this->__alloc();
746 do
747 {
748 __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_));
749 ++this->__end_;
750 --__n;
751 } while (__n > 0);
752}
753
754template <class _Tp, class _Allocator>
755_LIBCPP_INLINE_VISIBILITY inline
756void
757vector<_Tp, _Allocator>::__construct_at_end(size_type __n, true_type)
758{
759 _STD::memset(this->__end_, 0, __n*sizeof(value_type));
760 this->__end_ += __n;
761}
762
763// Copy constructs __n objects starting at __end_ from __x
764// throws if construction throws
765// Precondition: __n > 0
766// Precondition: size() + __n <= capacity()
767// Postcondition: size() == old size() + __n
768// Postcondition: [i] == __x for all i in [size() - __n, __n)
769template <class _Tp, class _Allocator>
770_LIBCPP_INLINE_VISIBILITY inline
771void
772vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
773{
Howard Hinnant1468b662010-11-19 22:17:28 +0000774 __construct_at_end(__n, __x, integral_constant<bool, is_trivially_copy_constructible<value_type>::value &&
775 is_trivially_copy_assignable<value_type>::value>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000776}
777
778template <class _Tp, class _Allocator>
779void
780vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x, false_type)
781{
782 allocator_type& __a = this->__alloc();
783 do
784 {
785 __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_), __x);
786 ++this->__end_;
787 --__n;
788 } while (__n > 0);
789}
790
791template <class _Tp, class _Allocator>
792_LIBCPP_INLINE_VISIBILITY inline
793void
794vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x, true_type)
795{
796 _STD::fill_n(this->__end_, __n, __x);
797 this->__end_ += __n;
798}
799
800template <class _Tp, class _Allocator>
801template <class _ForwardIterator>
802typename enable_if
803<
804 __is_forward_iterator<_ForwardIterator>::value,
805 void
806>::type
807vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
808{
809 allocator_type& __a = this->__alloc();
810 for (; __first != __last; ++__first)
811 {
812 __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_), *__first);
813 ++this->__end_;
814 }
815}
816
817template <class _Tp, class _Allocator>
818void
819vector<_Tp, _Allocator>::__move_construct_at_end(pointer __first, pointer __last)
820{
821 allocator_type& __a = this->__alloc();
822 for (; __first != __last; ++__first)
823 {
824 __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_),
825 _STD::move(*__first));
826 ++this->__end_;
827 }
828}
829
830// Default constructs __n objects starting at __end_
831// throws if construction throws
832// Postcondition: size() == size() + __n
833// Exception safety: strong but assumes move ctor doesn't throw (copy ctor can)
834template <class _Tp, class _Allocator>
835void
836vector<_Tp, _Allocator>::__append(size_type __n)
837{
838 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
839 this->__construct_at_end(__n);
840 else
841 {
842 allocator_type& __a = this->__alloc();
843 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
844 __v.__construct_at_end(__n);
845 __swap_out_circular_buffer(__v);
846 }
847}
848
849// Default constructs __n objects starting at __end_
850// throws if construction throws
851// Postcondition: size() == size() + __n
852// Exception safety: strong but assumes move ctor doesn't throw (copy ctor can)
853template <class _Tp, class _Allocator>
854void
855vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
856{
857 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
858 this->__construct_at_end(__n, __x);
859 else
860 {
861 allocator_type& __a = this->__alloc();
862 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
863 __v.__construct_at_end(__n, __x);
864 __swap_out_circular_buffer(__v);
865 }
866}
867
868template <class _Tp, class _Allocator>
869vector<_Tp, _Allocator>::vector(size_type __n)
870{
871 if (__n > 0)
872 {
873 allocate(__n);
874 __construct_at_end(__n);
875 }
876}
877
878template <class _Tp, class _Allocator>
879vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
880{
881 if (__n > 0)
882 {
883 allocate(__n);
884 __construct_at_end(__n, __x);
885 }
886}
887
888template <class _Tp, class _Allocator>
889vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
890 : __base(__a)
891{
892 if (__n > 0)
893 {
894 allocate(__n);
895 __construct_at_end(__n, __x);
896 }
897}
898
899template <class _Tp, class _Allocator>
900template <class _InputIterator>
901vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
902 typename enable_if<__is_input_iterator <_InputIterator>::value &&
903 !__is_forward_iterator<_InputIterator>::value>::type*)
904{
905 for (; __first != __last; ++__first)
906 push_back(*__first);
907}
908
909template <class _Tp, class _Allocator>
910template <class _InputIterator>
911vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
912 typename enable_if<__is_input_iterator <_InputIterator>::value &&
913 !__is_forward_iterator<_InputIterator>::value>::type*)
914 : __base(__a)
915{
916 for (; __first != __last; ++__first)
917 push_back(*__first);
918}
919
920template <class _Tp, class _Allocator>
921template <class _ForwardIterator>
922vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
923 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
924{
925 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
926 if (__n > 0)
927 {
928 allocate(__n);
929 __construct_at_end(__first, __last);
930 }
931}
932
933template <class _Tp, class _Allocator>
934template <class _ForwardIterator>
935vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
936 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
937 : __base(__a)
938{
939 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
940 if (__n > 0)
941 {
942 allocate(__n);
943 __construct_at_end(__first, __last);
944 }
945}
946
947template <class _Tp, class _Allocator>
948vector<_Tp, _Allocator>::vector(const vector& __x)
949 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
950{
951 size_type __n = __x.size();
952 if (__n > 0)
953 {
954 allocate(__n);
955 __construct_at_end(__x.__begin_, __x.__end_);
956 }
957}
958
959template <class _Tp, class _Allocator>
960vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
961 : __base(__a)
962{
963 size_type __n = __x.size();
964 if (__n > 0)
965 {
966 allocate(__n);
967 __construct_at_end(__x.__begin_, __x.__end_);
968 }
969}
970
Howard Hinnant73d21a42010-09-04 23:28:19 +0000971#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000972
973template <class _Tp, class _Allocator>
974_LIBCPP_INLINE_VISIBILITY inline
975vector<_Tp, _Allocator>::vector(vector&& __x)
976 : __base(_STD::move(__x.__alloc()))
977{
978 this->__begin_ = __x.__begin_;
979 this->__end_ = __x.__end_;
980 this->__end_cap() = __x.__end_cap();
981 __x.__begin_ = __x.__end_ = __x.__end_cap() = 0;
982 __x.__invalidate_all_iterators();
983}
984
985template <class _Tp, class _Allocator>
986_LIBCPP_INLINE_VISIBILITY inline
987vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
988 : __base(__a)
989{
990 if (__a == __x.__alloc())
991 {
992 this->__begin_ = __x.__begin_;
993 this->__end_ = __x.__end_;
994 this->__end_cap() = __x.__end_cap();
995 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
996 __x.__invalidate_all_iterators();
997 }
998 else
999 {
1000 typedef move_iterator<iterator> _I;
1001 assign(_I(__x.begin()), _I(__x.end()));
1002 }
1003}
1004
1005template <class _Tp, class _Allocator>
1006_LIBCPP_INLINE_VISIBILITY inline
1007vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1008{
1009 if (__il.size() > 0)
1010 {
1011 allocate(__il.size());
1012 __construct_at_end(__il.begin(), __il.end());
1013 }
1014}
1015
1016template <class _Tp, class _Allocator>
1017_LIBCPP_INLINE_VISIBILITY inline
1018vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1019 : __base(__a)
1020{
1021 if (__il.size() > 0)
1022 {
1023 allocate(__il.size());
1024 __construct_at_end(__il.begin(), __il.end());
1025 }
1026}
1027
1028template <class _Tp, class _Allocator>
1029_LIBCPP_INLINE_VISIBILITY inline
1030vector<_Tp, _Allocator>&
1031vector<_Tp, _Allocator>::operator=(vector&& __x)
1032{
1033 __move_assign(__x, integral_constant<bool,
1034 __alloc_traits::propagate_on_container_move_assignment::value>());
1035 return *this;
1036}
1037
1038template <class _Tp, class _Allocator>
1039void
1040vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1041{
1042 if (__base::__alloc() != __c.__alloc())
1043 {
1044 typedef move_iterator<iterator> _I;
1045 assign(_I(__c.begin()), _I(__c.end()));
1046 }
1047 else
1048 __move_assign(__c, true_type());
1049}
1050
1051template <class _Tp, class _Allocator>
1052void
1053vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
1054{
1055 deallocate();
1056 this->__begin_ = __c.__begin_;
1057 this->__end_ = __c.__end_;
1058 this->__end_cap() = __c.__end_cap();
1059 __base::__move_assign_alloc(__c);
1060 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
1061}
1062
Howard Hinnant73d21a42010-09-04 23:28:19 +00001063#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001064
1065template <class _Tp, class _Allocator>
1066_LIBCPP_INLINE_VISIBILITY inline
1067vector<_Tp, _Allocator>&
1068vector<_Tp, _Allocator>::operator=(const vector& __x)
1069{
1070 if (this != &__x)
1071 {
1072 __base::__copy_assign_alloc(__x);
1073 assign(__x.__begin_, __x.__end_);
1074 }
1075 return *this;
1076}
1077
1078template <class _Tp, class _Allocator>
1079template <class _InputIterator>
1080typename enable_if
1081<
1082 __is_input_iterator <_InputIterator>::value &&
1083 !__is_forward_iterator<_InputIterator>::value,
1084 void
1085>::type
1086vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1087{
1088 clear();
1089 for (; __first != __last; ++__first)
1090 push_back(*__first);
1091}
1092
1093template <class _Tp, class _Allocator>
1094template <class _ForwardIterator>
1095typename enable_if
1096<
1097 __is_forward_iterator<_ForwardIterator>::value,
1098 void
1099>::type
1100vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1101{
1102 typename iterator_traits<_ForwardIterator>::difference_type __new_size = _STD::distance(__first, __last);
1103 if (static_cast<size_type>(__new_size) <= capacity())
1104 {
1105 _ForwardIterator __mid = __last;
1106 bool __growing = false;
1107 if (static_cast<size_type>(__new_size) > size())
1108 {
1109 __growing = true;
1110 __mid = __first;
1111 _STD::advance(__mid, size());
1112 }
1113 pointer __m = _STD::copy(__first, __mid, this->__begin_);
1114 if (__growing)
1115 __construct_at_end(__mid, __last);
1116 else
1117 this->__destruct_at_end(__m);
1118 }
1119 else
1120 {
1121 deallocate();
1122 allocate(__recommend(static_cast<size_type>(__new_size)));
1123 __construct_at_end(__first, __last);
1124 }
1125}
1126
1127template <class _Tp, class _Allocator>
1128void
1129vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1130{
1131 if (__n <= capacity())
1132 {
1133 size_type __s = size();
1134 _STD::fill_n(this->__begin_, min(__n, __s), __u);
1135 if (__n > __s)
1136 __construct_at_end(__n - __s, __u);
1137 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001138 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001139 }
1140 else
1141 {
1142 deallocate();
1143 allocate(__recommend(static_cast<size_type>(__n)));
1144 __construct_at_end(__n, __u);
1145 }
1146}
1147
Howard Hinnant324bb032010-08-22 00:02:43 +00001148template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001149_LIBCPP_INLINE_VISIBILITY inline
1150typename vector<_Tp, _Allocator>::iterator
1151vector<_Tp, _Allocator>::__make_iter(pointer __p)
1152{
1153#ifdef _LIBCPP_DEBUG
1154 return iterator(this, __p);
1155#else
1156 return iterator(__p);
1157#endif
1158}
1159
Howard Hinnant324bb032010-08-22 00:02:43 +00001160template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001161_LIBCPP_INLINE_VISIBILITY inline
1162typename vector<_Tp, _Allocator>::const_iterator
1163vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const
1164{
1165#ifdef _LIBCPP_DEBUG
1166 return const_iterator(this, __p);
1167#else
1168 return const_iterator(__p);
1169#endif
1170}
1171
Howard Hinnant324bb032010-08-22 00:02:43 +00001172template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001173_LIBCPP_INLINE_VISIBILITY inline
1174typename vector<_Tp, _Allocator>::iterator
1175vector<_Tp, _Allocator>::begin()
1176{
1177 return __make_iter(this->__begin_);
1178}
1179
Howard Hinnant324bb032010-08-22 00:02:43 +00001180template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001181_LIBCPP_INLINE_VISIBILITY inline
1182typename vector<_Tp, _Allocator>::const_iterator
1183vector<_Tp, _Allocator>::begin() const
1184{
1185 return __make_iter(this->__begin_);
1186}
1187
Howard Hinnant324bb032010-08-22 00:02:43 +00001188template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001189_LIBCPP_INLINE_VISIBILITY inline
1190typename vector<_Tp, _Allocator>::iterator
1191vector<_Tp, _Allocator>::end()
1192{
1193 return __make_iter(this->__end_);
1194}
1195
Howard Hinnant324bb032010-08-22 00:02:43 +00001196template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001197_LIBCPP_INLINE_VISIBILITY inline
1198typename vector<_Tp, _Allocator>::const_iterator
1199vector<_Tp, _Allocator>::end() const
1200{
1201 return __make_iter(this->__end_);
1202}
1203
Howard Hinnant324bb032010-08-22 00:02:43 +00001204template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001205_LIBCPP_INLINE_VISIBILITY inline
1206typename vector<_Tp, _Allocator>::reference
1207vector<_Tp, _Allocator>::operator[](size_type __n)
1208{
1209#ifdef _LIBCPP_DEBUG
1210 assert(__n < size());
1211#endif
1212 return this->__begin_[__n];
1213}
1214
Howard Hinnant324bb032010-08-22 00:02:43 +00001215template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001216_LIBCPP_INLINE_VISIBILITY inline
1217typename vector<_Tp, _Allocator>::const_reference
1218vector<_Tp, _Allocator>::operator[](size_type __n) const
1219{
1220#ifdef _LIBCPP_DEBUG
1221 assert(__n < size());
1222#endif
1223 return this->__begin_[__n];
1224}
1225
Howard Hinnant324bb032010-08-22 00:02:43 +00001226template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001227typename vector<_Tp, _Allocator>::reference
1228vector<_Tp, _Allocator>::at(size_type __n)
1229{
1230 if (__n >= size())
1231 this->__throw_out_of_range();
1232 return this->__begin_[__n];
1233}
1234
Howard Hinnant324bb032010-08-22 00:02:43 +00001235template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001236typename vector<_Tp, _Allocator>::const_reference
1237vector<_Tp, _Allocator>::at(size_type __n) const
1238{
1239 if (__n >= size())
1240 this->__throw_out_of_range();
1241 return this->__begin_[__n];
1242}
1243
1244template <class _Tp, class _Allocator>
1245void
1246vector<_Tp, _Allocator>::reserve(size_type __n)
1247{
1248 if (__n > capacity())
1249 {
1250 allocator_type& __a = this->__alloc();
1251 __split_buffer<value_type, allocator_type&> __v(__n, 0, __a);
1252 __v.__construct_at_end(move_iterator<pointer>(this->__begin_),
1253 move_iterator<pointer>(this->__end_));
1254 clear();
1255 __swap_out_circular_buffer(__v);
1256 }
1257}
1258
1259template <class _Tp, class _Allocator>
1260void
1261vector<_Tp, _Allocator>::shrink_to_fit()
1262{
1263 if (capacity() > size())
1264 {
1265#ifndef _LIBCPP_NO_EXCEPTIONS
1266 try
1267 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001268#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001269 allocator_type& __a = this->__alloc();
1270 __split_buffer<value_type, allocator_type&> __v(size(), 0, __a);
1271 __v.__construct_at_end(move_iterator<pointer>(this->__begin_),
1272 move_iterator<pointer>(this->__end_));
1273 clear();
1274 __swap_out_circular_buffer(__v);
1275#ifndef _LIBCPP_NO_EXCEPTIONS
1276 }
1277 catch (...)
1278 {
1279 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001280#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001281 }
1282}
1283
1284template <class _Tp, class _Allocator>
1285void
1286vector<_Tp, _Allocator>::push_back(const_reference __x)
1287{
1288 if (this->__end_ < this->__end_cap())
1289 {
1290 __alloc_traits::construct(this->__alloc(),
1291 _STD::__to_raw_pointer(this->__end_), __x);
1292 ++this->__end_;
1293 }
1294 else
1295 {
1296 allocator_type& __a = this->__alloc();
1297 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1298 __v.push_back(__x);
1299 __swap_out_circular_buffer(__v);
1300 }
1301}
1302
Howard Hinnant73d21a42010-09-04 23:28:19 +00001303#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001304
1305template <class _Tp, class _Allocator>
1306void
1307vector<_Tp, _Allocator>::push_back(value_type&& __x)
1308{
1309 if (this->__end_ < this->__end_cap())
1310 {
1311 __alloc_traits::construct(this->__alloc(),
1312 _STD::__to_raw_pointer(this->__end_),
1313 _STD::move(__x));
1314 ++this->__end_;
1315 }
1316 else
1317 {
1318 allocator_type& __a = this->__alloc();
1319 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1320 __v.push_back(_STD::move(__x));
1321 __swap_out_circular_buffer(__v);
1322 }
1323}
1324
Howard Hinnant73d21a42010-09-04 23:28:19 +00001325#ifndef _LIBCPP_HAS_NO_VARIADICS
1326
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001327template <class _Tp, class _Allocator>
1328template <class... _Args>
1329void
1330vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1331{
1332 if (this->__end_ < this->__end_cap())
1333 {
1334 __alloc_traits::construct(this->__alloc(),
1335 _STD::__to_raw_pointer(this->__end_),
1336 _STD::forward<_Args>(__args)...);
1337 ++this->__end_;
1338 }
1339 else
1340 {
1341 allocator_type& __a = this->__alloc();
1342 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1343 __v.emplace_back(_STD::forward<_Args>(__args)...);
1344 __swap_out_circular_buffer(__v);
1345 }
1346}
1347
Howard Hinnant73d21a42010-09-04 23:28:19 +00001348#endif // _LIBCPP_HAS_NO_VARIADICS
1349#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001350
1351template <class _Tp, class _Allocator>
1352_LIBCPP_INLINE_VISIBILITY inline
1353void
1354vector<_Tp, _Allocator>::pop_back()
1355{
1356 this->__destruct_at_end(this->__end_ - 1);
1357}
1358
1359template <class _Tp, class _Allocator>
1360_LIBCPP_INLINE_VISIBILITY inline
1361typename vector<_Tp, _Allocator>::iterator
1362vector<_Tp, _Allocator>::erase(const_iterator __position)
1363{
1364 pointer __p = const_cast<pointer>(&*__position);
1365 iterator __r = __make_iter(__p);
1366 this->__destruct_at_end(_STD::move(__p + 1, this->__end_, __p));
1367 return __r;
1368}
1369
1370template <class _Tp, class _Allocator>
1371typename vector<_Tp, _Allocator>::iterator
1372vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1373{
1374 pointer __p = this->__begin_ + (__first - begin());
1375 iterator __r = __make_iter(__p);
1376 this->__destruct_at_end(_STD::move(__p + (__last - __first), this->__end_, __p));
1377 return __r;
1378}
1379
1380template <class _Tp, class _Allocator>
1381void
1382vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1383{
1384 pointer __old_last = this->__end_;
1385 difference_type __n = __old_last - __to;
1386 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1387 __alloc_traits::construct(this->__alloc(),
1388 _STD::__to_raw_pointer(this->__end_),
1389 _STD::move(*__i));
1390 _STD::move_backward(__from_s, __from_s + __n, __old_last);
1391}
1392
1393template <class _Tp, class _Allocator>
1394typename vector<_Tp, _Allocator>::iterator
1395vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1396{
1397 pointer __p = this->__begin_ + (__position - begin());
1398 if (this->__end_ < this->__end_cap())
1399 {
1400 if (__p == this->__end_)
1401 {
1402 __alloc_traits::construct(this->__alloc(),
1403 _STD::__to_raw_pointer(this->__end_), __x);
1404 ++this->__end_;
1405 }
1406 else
1407 {
1408 __move_range(__p, this->__end_, __p + 1);
1409 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1410 if (__p <= __xr && __xr < this->__end_)
1411 ++__xr;
1412 *__p = *__xr;
1413 }
1414 }
1415 else
1416 {
1417 allocator_type& __a = this->__alloc();
1418 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1419 __v.push_back(__x);
1420 __p = __swap_out_circular_buffer(__v, __p);
1421 }
1422 return __make_iter(__p);
1423}
1424
Howard Hinnant73d21a42010-09-04 23:28:19 +00001425#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001426
1427template <class _Tp, class _Allocator>
1428typename vector<_Tp, _Allocator>::iterator
1429vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1430{
1431 pointer __p = this->__begin_ + (__position - begin());
1432 if (this->__end_ < this->__end_cap())
1433 {
1434 if (__p == this->__end_)
1435 {
1436 __alloc_traits::construct(this->__alloc(),
1437 _STD::__to_raw_pointer(this->__end_),
1438 _STD::move(__x));
1439 ++this->__end_;
1440 }
1441 else
1442 {
1443 __move_range(__p, this->__end_, __p + 1);
1444 *__p = _STD::move(__x);
1445 }
1446 }
1447 else
1448 {
1449 allocator_type& __a = this->__alloc();
1450 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1451 __v.push_back(_STD::move(__x));
1452 __p = __swap_out_circular_buffer(__v, __p);
1453 }
1454 return __make_iter(__p);
1455}
1456
Howard Hinnant73d21a42010-09-04 23:28:19 +00001457#ifndef _LIBCPP_HAS_NO_VARIADICS
1458
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001459template <class _Tp, class _Allocator>
1460template <class... _Args>
1461typename vector<_Tp, _Allocator>::iterator
1462vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1463{
1464 pointer __p = this->__begin_ + (__position - begin());
1465 if (this->__end_ < this->__end_cap())
1466 {
1467 if (__p == this->__end_)
1468 {
1469 __alloc_traits::construct(this->__alloc(),
1470 _STD::__to_raw_pointer(this->__end_),
1471 _STD::forward<_Args>(__args)...);
1472 ++this->__end_;
1473 }
1474 else
1475 {
1476 __move_range(__p, this->__end_, __p + 1);
1477 *__p = value_type(_STD::forward<_Args>(__args)...);
1478 }
1479 }
1480 else
1481 {
1482 allocator_type& __a = this->__alloc();
1483 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1484 __v.emplace_back(_STD::forward<_Args>(__args)...);
1485 __p = __swap_out_circular_buffer(__v, __p);
1486 }
1487 return __make_iter(__p);
1488}
1489
Howard Hinnant73d21a42010-09-04 23:28:19 +00001490#endif // _LIBCPP_HAS_NO_VARIADICS
1491#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001492
1493template <class _Tp, class _Allocator>
1494typename vector<_Tp, _Allocator>::iterator
1495vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1496{
1497 pointer __p = this->__begin_ + (__position - begin());
1498 if (__n > 0)
1499 {
1500 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1501 {
1502 size_type __old_n = __n;
1503 pointer __old_last = this->__end_;
1504 if (__n > static_cast<size_type>(this->__end_ - __p))
1505 {
1506 size_type __cx = __n - (this->__end_ - __p);
1507 __construct_at_end(__cx, __x);
1508 __n -= __cx;
1509 }
1510 if (__n > 0)
1511 {
1512 __move_range(__p, __old_last, __p + __old_n);
1513 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1514 if (__p <= __xr && __xr < this->__end_)
1515 __xr += __old_n;
1516 _STD::fill_n(__p, __n, *__xr);
1517 }
1518 }
1519 else
1520 {
1521 allocator_type& __a = this->__alloc();
1522 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1523 __v.__construct_at_end(__n, __x);
1524 __p = __swap_out_circular_buffer(__v, __p);
1525 }
1526 }
1527 return __make_iter(__p);
1528}
1529
1530template <class _Tp, class _Allocator>
1531template <class _InputIterator>
1532typename enable_if
1533<
1534 __is_input_iterator <_InputIterator>::value &&
1535 !__is_forward_iterator<_InputIterator>::value,
1536 typename vector<_Tp, _Allocator>::iterator
1537>::type
1538vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1539{
1540 difference_type __off = __position - begin();
1541 pointer __p = this->__begin_ + __off;
1542 allocator_type& __a = this->__alloc();
1543 pointer __old_last = this->__end_;
1544 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1545 {
1546 __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_),
1547 *__first);
1548 ++this->__end_;
1549 }
1550 __split_buffer<value_type, allocator_type&> __v(__a);
1551 if (__first != __last)
1552 {
1553#ifndef _LIBCPP_NO_EXCEPTIONS
1554 try
1555 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001556#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001557 __v.__construct_at_end(__first, __last);
1558 difference_type __old_size = __old_last - this->__begin_;
1559 difference_type __old_p = __p - this->__begin_;
1560 reserve(__recommend(size() + __v.size()));
1561 __p = this->__begin_ + __old_p;
1562 __old_last = this->__begin_ + __old_size;
1563#ifndef _LIBCPP_NO_EXCEPTIONS
1564 }
1565 catch (...)
1566 {
1567 erase(__make_iter(__old_last), end());
1568 throw;
1569 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001570#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001571 }
1572 __p = _STD::rotate(__p, __old_last, this->__end_);
1573 insert(__make_iter(__p), move_iterator<iterator>(__v.begin()),
1574 move_iterator<iterator>(__v.end()));
1575 return begin() + __off;
1576}
1577
1578template <class _Tp, class _Allocator>
1579template <class _ForwardIterator>
1580typename enable_if
1581<
1582 __is_forward_iterator<_ForwardIterator>::value,
1583 typename vector<_Tp, _Allocator>::iterator
1584>::type
1585vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1586{
1587 pointer __p = this->__begin_ + (__position - begin());
1588 difference_type __n = _STD::distance(__first, __last);
1589 if (__n > 0)
1590 {
1591 if (__n <= this->__end_cap() - this->__end_)
1592 {
1593 size_type __old_n = __n;
1594 pointer __old_last = this->__end_;
1595 _ForwardIterator __m = __last;
1596 difference_type __dx = this->__end_ - __p;
1597 if (__n > __dx)
1598 {
1599 __m = __first;
1600 _STD::advance(__m, this->__end_ - __p);
1601 __construct_at_end(__m, __last);
1602 __n = __dx;
1603 }
1604 if (__n > 0)
1605 {
1606 __move_range(__p, __old_last, __p + __old_n);
1607 _STD::copy(__first, __m, __p);
1608 }
1609 }
1610 else
1611 {
1612 allocator_type& __a = this->__alloc();
1613 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1614 __v.__construct_at_end(__first, __last);
1615 __p = __swap_out_circular_buffer(__v, __p);
1616 }
1617 }
1618 return __make_iter(__p);
1619}
1620
1621template <class _Tp, class _Allocator>
1622void
1623vector<_Tp, _Allocator>::resize(size_type __sz)
1624{
1625 size_type __cs = size();
1626 if (__cs < __sz)
1627 this->__append(__sz - __cs);
1628 else if (__cs > __sz)
1629 this->__destruct_at_end(this->__begin_ + __sz);
1630}
1631
1632template <class _Tp, class _Allocator>
1633void
1634vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
1635{
1636 size_type __cs = size();
1637 if (__cs < __sz)
1638 this->__append(__sz - __cs, __x);
1639 else if (__cs > __sz)
1640 this->__destruct_at_end(this->__begin_ + __sz);
1641}
1642
1643template <class _Tp, class _Allocator>
1644void
1645vector<_Tp, _Allocator>::swap(vector& __x)
1646{
1647 _STD::swap(this->__begin_, __x.__begin_);
1648 _STD::swap(this->__end_, __x.__end_);
1649 _STD::swap(this->__end_cap(), __x.__end_cap());
1650 __base::__swap_alloc(this->__alloc(), __x.__alloc());
1651#ifdef _LIBCPP_DEBUG
1652 iterator::swap(this, &__x);
1653 const_iterator::swap(this, &__x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001654#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655}
1656
Howard Hinnant324bb032010-08-22 00:02:43 +00001657template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001658bool
1659vector<_Tp, _Allocator>::__invariants() const
1660{
1661 if (this->__begin_ == 0)
1662 {
1663 if (this->__end_ != 0 || this->__end_cap() != 0)
1664 return false;
1665 }
1666 else
1667 {
1668 if (this->__begin_ > this->__end_)
1669 return false;
1670 if (this->__begin_ == this->__end_cap())
1671 return false;
1672 if (this->__end_ > this->__end_cap())
1673 return false;
1674 }
1675 return true;
1676}
1677
1678template <class _Tp, class _Allocator>
1679#ifndef _LIBCPP_DEBUG
1680_LIBCPP_INLINE_VISIBILITY inline
1681#endif
1682void
1683vector<_Tp, _Allocator>::__invalidate_all_iterators()
1684{
1685#ifdef _LIBCPP_DEBUG
1686 iterator::__remove_all(this);
1687 const_iterator::__remove_all(this);
Howard Hinnant324bb032010-08-22 00:02:43 +00001688#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001689}
1690
1691// vector<bool>
1692
1693template <class _Allocator> class vector<bool, _Allocator>;
1694
1695template <class _Allocator> struct hash<vector<bool, _Allocator> >;
1696
1697template <class _Allocator>
Howard Hinnant36cdf022010-09-10 16:42:26 +00001698class _LIBCPP_VISIBLE vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001699 : private __vector_base_common<true>
1700{
1701public:
1702 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00001703 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001704 typedef _Allocator allocator_type;
1705 typedef allocator_traits<allocator_type> __alloc_traits;
1706 typedef __bit_reference<vector> reference;
1707 typedef __bit_const_reference<vector> const_reference;
1708 typedef typename __alloc_traits::size_type size_type;
1709 typedef typename __alloc_traits::difference_type difference_type;
1710 typedef __bit_iterator<vector, false> pointer;
1711 typedef __bit_iterator<vector, true> const_pointer;
1712#ifdef _LIBCPP_DEBUG
1713 typedef __debug_iter<vector, pointer> iterator;
1714 typedef __debug_iter<vector, const_pointer> const_iterator;
1715
1716 friend class __debug_iter<vector, pointer>;
1717 friend class __debug_iter<vector, const_pointer>;
1718
1719 pair<iterator*, const_iterator*> __iterator_list_;
1720
1721 _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;}
1722 _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;}
Howard Hinnant324bb032010-08-22 00:02:43 +00001723#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001724 typedef pointer iterator;
1725 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +00001726#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001727 typedef _STD::reverse_iterator<iterator> reverse_iterator;
1728 typedef _STD::reverse_iterator<const_iterator> const_reverse_iterator;
1729
1730private:
1731 typedef size_type __storage_type;
1732 typedef typename __alloc_traits::template
1733#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1734 rebind_alloc<__storage_type>
1735#else
1736 rebind_alloc<__storage_type>::other
1737#endif
1738 __storage_allocator;
1739 typedef allocator_traits<__storage_allocator> __storage_traits;
1740 typedef typename __storage_traits::pointer __storage_pointer;
1741 typedef typename __storage_traits::const_pointer __const_storage_pointer;
1742
1743 __storage_pointer __begin_;
1744 size_type __size_;
1745 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
1746
1747 _LIBCPP_INLINE_VISIBILITY size_type& __cap() {return __cap_alloc_.first();}
1748 _LIBCPP_INLINE_VISIBILITY const size_type& __cap() const {return __cap_alloc_.first();}
1749 _LIBCPP_INLINE_VISIBILITY __storage_allocator& __alloc() {return __cap_alloc_.second();}
1750 _LIBCPP_INLINE_VISIBILITY const __storage_allocator& __alloc() const {return __cap_alloc_.second();}
1751
1752 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
1753
1754 _LIBCPP_INLINE_VISIBILITY static size_type __internal_cap_to_external(size_type __n)
1755 {return __n * __bits_per_word;}
1756 _LIBCPP_INLINE_VISIBILITY static size_type __external_cap_to_internal(size_type __n)
1757 {return (__n - 1) / __bits_per_word + 1;}
1758
1759public:
1760 vector();
1761 explicit vector(const allocator_type& __a);
1762 ~vector();
1763 explicit vector(size_type __n);
1764 vector(size_type __n, const value_type& __v);
1765 vector(size_type __n, const value_type& __v, const allocator_type& __a);
1766 template <class _InputIterator>
1767 vector(_InputIterator __first, _InputIterator __last,
1768 typename enable_if<__is_input_iterator <_InputIterator>::value &&
1769 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
1770 template <class _InputIterator>
1771 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1772 typename enable_if<__is_input_iterator <_InputIterator>::value &&
1773 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
1774 template <class _ForwardIterator>
1775 vector(_ForwardIterator __first, _ForwardIterator __last,
1776 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
1777 template <class _ForwardIterator>
1778 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
1779 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
1780
1781 vector(const vector& __v);
1782 vector(const vector& __v, const allocator_type& __a);
1783 vector& operator=(const vector& __v);
1784 vector(initializer_list<value_type> __il);
1785 vector(initializer_list<value_type> __il, const allocator_type& __a);
1786
Howard Hinnant73d21a42010-09-04 23:28:19 +00001787#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001788 vector(vector&& __v);
1789 vector(vector&& __v, const allocator_type& __a);
1790 vector& operator=(vector&& __v);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001791#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001792 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001793 vector& operator=(initializer_list<value_type> __il)
1794 {assign(__il.begin(), __il.end()); return *this;}
1795
1796 template <class _InputIterator>
1797 typename enable_if
1798 <
1799 __is_input_iterator<_InputIterator>::value &&
1800 !__is_forward_iterator<_InputIterator>::value,
1801 void
1802 >::type
1803 assign(_InputIterator __first, _InputIterator __last);
1804 template <class _ForwardIterator>
1805 typename enable_if
1806 <
1807 __is_forward_iterator<_ForwardIterator>::value,
1808 void
1809 >::type
1810 assign(_ForwardIterator __first, _ForwardIterator __last);
1811
1812 void assign(size_type __n, const value_type& __x);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001813 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001814 void assign(initializer_list<value_type> __il)
1815 {assign(__il.begin(), __il.end());}
1816
1817 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const
1818 {return allocator_type(this->__alloc());}
1819
1820 size_type max_size() const;
1821 _LIBCPP_INLINE_VISIBILITY size_type capacity() const {return __internal_cap_to_external(__cap());}
1822 _LIBCPP_INLINE_VISIBILITY size_type size() const {return __size_;}
1823 _LIBCPP_INLINE_VISIBILITY bool empty() const {return __size_ == 0;}
1824 void reserve(size_type __n);
1825 void shrink_to_fit();
1826
1827 _LIBCPP_INLINE_VISIBILITY iterator begin() {return __make_iter(0);}
1828 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const {return __make_iter(0);}
1829 _LIBCPP_INLINE_VISIBILITY iterator end() {return __make_iter(__size_);}
1830 _LIBCPP_INLINE_VISIBILITY const_iterator end() const {return __make_iter(__size_);}
1831
1832 _LIBCPP_INLINE_VISIBILITY reverse_iterator rbegin() {return reverse_iterator(end());}
1833 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rbegin() const {return const_reverse_iterator(end());}
1834 _LIBCPP_INLINE_VISIBILITY reverse_iterator rend() {return reverse_iterator(begin());}
1835 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rend() const {return const_reverse_iterator(begin());}
1836
1837 _LIBCPP_INLINE_VISIBILITY const_iterator cbegin() const {return __make_iter(0);}
1838 _LIBCPP_INLINE_VISIBILITY const_iterator cend() const {return __make_iter(__size_);}
1839 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crbegin() const {return rbegin();}
1840 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crend() const {return rend();}
1841
1842 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
1843 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
1844 reference at(size_type __n);
1845 const_reference at(size_type __n) const;
1846
1847 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
1848 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
1849 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
1850 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
1851
1852 void push_back(const value_type& __x);
1853 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
1854
1855 iterator insert(const_iterator __position, const value_type& __x);
1856 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
1857 iterator insert(const_iterator __position, size_type __n, const_reference __x);
1858 template <class _InputIterator>
1859 typename enable_if
1860 <
1861 __is_input_iterator <_InputIterator>::value &&
1862 !__is_forward_iterator<_InputIterator>::value,
1863 iterator
1864 >::type
1865 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
1866 template <class _ForwardIterator>
1867 typename enable_if
1868 <
1869 __is_forward_iterator<_ForwardIterator>::value,
1870 iterator
1871 >::type
1872 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001874 iterator insert(const_iterator __position, initializer_list<value_type> __il)
1875 {return insert(__position, __il.begin(), __il.end());}
1876
1877 iterator erase(const_iterator __position);
1878 iterator erase(const_iterator __first, const_iterator __last);
1879
1880 _LIBCPP_INLINE_VISIBILITY void clear() {__size_ = 0;}
1881
1882 void swap(vector&);
1883
1884 void resize(size_type __sz, value_type __x = false);
1885 void flip();
1886
1887 bool __invariants() const;
1888
1889private:
1890 void __invalidate_all_iterators();
1891 void allocate(size_type __n);
1892 void deallocate();
1893 _LIBCPP_INLINE_VISIBILITY static size_type __align(size_type __new_size)
1894 {return __new_size + (__bits_per_word-1) & ~(__bits_per_word-1);};
1895 size_type __recommend(size_type __new_size) const;
1896 void __construct_at_end(size_type __n, bool __x);
1897 template <class _ForwardIterator>
1898 typename enable_if
1899 <
1900 __is_forward_iterator<_ForwardIterator>::value,
1901 void
1902 >::type
1903 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
1904 void __append(size_type __n, const_reference __x);
1905 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_type __pos)
1906 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
1907 _LIBCPP_INLINE_VISIBILITY const_reference __make_ref(size_type __pos) const
1908 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
1909#ifdef _LIBCPP_DEBUG
1910 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_type __pos)
1911 {return iterator(this, pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
1912 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_type __pos) const
1913 {return const_iterator(this, const_pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
1914 _LIBCPP_INLINE_VISIBILITY iterator __const_iterator_cast(const_iterator __p)
1915 {return iterator(this, pointer(const_cast<__storage_pointer>(__p.base().__seg_), __p.base().__ctz_));}
Howard Hinnant324bb032010-08-22 00:02:43 +00001916#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001917 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_type __pos)
1918 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
1919 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_type __pos) const
1920 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
1921 _LIBCPP_INLINE_VISIBILITY iterator __const_iterator_cast(const_iterator __p)
1922 {return iterator(const_cast<__storage_pointer>(__p.__seg_), __p.__ctz_);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001923#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001924
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001925 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001926 void __copy_assign_alloc(const vector& __v)
1927 {__copy_assign_alloc(__v, integral_constant<bool,
1928 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001929 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001930 void __copy_assign_alloc(const vector& __c, true_type)
1931 {
1932 if (__alloc() != __c.__alloc())
1933 deallocate();
1934 __alloc() = __c.__alloc();
1935 }
1936
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001937 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001938 void __copy_assign_alloc(const vector& __c, false_type)
1939 {}
1940
1941 void __move_assign(vector& __c, false_type);
1942 void __move_assign(vector& __c, true_type);
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001943 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001944 void __move_assign_alloc(vector& __c)
1945 {__move_assign_alloc(__c, integral_constant<bool,
1946 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001947 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001948 void __move_assign_alloc(const vector& __c, true_type)
1949 {
1950 __alloc() = _STD::move(__c.__alloc());
1951 }
1952
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001953 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001954 void __move_assign_alloc(const vector& __c, false_type)
1955 {}
1956
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001958 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y)
1959 {__swap_alloc(__x, __y, integral_constant<bool,
1960 __storage_traits::propagate_on_container_swap::value>());}
1961
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001962 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001963 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, true_type)
1964 {
1965 using _STD::swap;
1966 swap(__x, __y);
1967 }
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001968 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001969 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, false_type)
1970 {}
1971
1972 size_t __hash_code() const;
1973
1974 friend class __bit_reference<vector>;
1975 friend class __bit_const_reference<vector>;
1976 friend class __bit_iterator<vector, false>;
1977 friend class __bit_iterator<vector, true>;
1978 friend class __bit_array<vector>;
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001979 friend struct _LIBCPP_VISIBLE hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001980};
1981
1982template <class _Allocator>
1983#ifndef _LIBCPP_DEBUG
1984_LIBCPP_INLINE_VISIBILITY inline
1985#endif
1986void
1987vector<bool, _Allocator>::__invalidate_all_iterators()
1988{
1989#ifdef _LIBCPP_DEBUG
1990 iterator::__remove_all(this);
1991 const_iterator::__remove_all(this);
Howard Hinnant324bb032010-08-22 00:02:43 +00001992#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001993}
1994
1995// Allocate space for __n objects
1996// throws length_error if __n > max_size()
1997// throws (probably bad_alloc) if memory run out
1998// Precondition: __begin_ == __end_ == __cap() == 0
1999// Precondition: __n > 0
2000// Postcondition: capacity() == __n
2001// Postcondition: size() == 0
2002template <class _Allocator>
2003void
2004vector<bool, _Allocator>::allocate(size_type __n)
2005{
2006 if (__n > max_size())
2007 this->__throw_length_error();
2008 __n = __external_cap_to_internal(__n);
2009 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2010 this->__size_ = 0;
2011 this->__cap() = __n;
2012}
2013
2014template <class _Allocator>
2015void
2016vector<bool, _Allocator>::deallocate()
2017{
2018 if (this->__begin_ != 0)
2019 {
2020 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2021 __invalidate_all_iterators();
2022 this->__begin_ = 0;
2023 this->__size_ = this->__cap() = 0;
2024 }
2025}
2026
2027template <class _Allocator>
2028typename vector<bool, _Allocator>::size_type
2029vector<bool, _Allocator>::max_size() const
2030{
2031 size_type __amax = __storage_traits::max_size(__alloc());
2032 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2033 if (__nmax / __bits_per_word <= __amax)
2034 return __nmax;
2035 return __internal_cap_to_external(__amax);
2036}
2037
2038// Precondition: __new_size > capacity()
2039template <class _Allocator>
2040_LIBCPP_INLINE_VISIBILITY inline
2041typename vector<bool, _Allocator>::size_type
2042vector<bool, _Allocator>::__recommend(size_type __new_size) const
2043{
2044 const size_type __ms = max_size();
2045 if (__new_size > __ms)
2046 this->__throw_length_error();
2047 const size_type __cap = capacity();
2048 if (__cap >= __ms / 2)
2049 return __ms;
2050 return _STD::max(2*__cap, __align(__new_size));
2051}
2052
2053// Default constructs __n objects starting at __end_
2054// Precondition: __n > 0
2055// Precondition: size() + __n <= capacity()
2056// Postcondition: size() == size() + __n
2057template <class _Allocator>
2058_LIBCPP_INLINE_VISIBILITY inline
2059void
2060vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2061{
2062 size_type __old_size = this->__size_;
2063 this->__size_ += __n;
2064 _STD::fill_n(__make_iter(__old_size), __n, __x);
2065}
2066
2067template <class _Allocator>
2068template <class _ForwardIterator>
2069typename enable_if
2070<
2071 __is_forward_iterator<_ForwardIterator>::value,
2072 void
2073>::type
2074vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2075{
2076 size_type __old_size = this->__size_;
2077 this->__size_ += _STD::distance(__first, __last);
2078 _STD::copy(__first, __last, __make_iter(__old_size));
2079}
2080
2081template <class _Allocator>
2082_LIBCPP_INLINE_VISIBILITY inline
2083vector<bool, _Allocator>::vector()
2084 : __begin_(0),
2085 __size_(0),
2086 __cap_alloc_(0)
2087{
2088}
2089
2090template <class _Allocator>
2091_LIBCPP_INLINE_VISIBILITY inline
2092vector<bool, _Allocator>::vector(const allocator_type& __a)
2093 : __begin_(0),
2094 __size_(0),
2095 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2096{
2097}
2098
2099template <class _Allocator>
2100vector<bool, _Allocator>::vector(size_type __n)
2101 : __begin_(0),
2102 __size_(0),
2103 __cap_alloc_(0)
2104{
2105 if (__n > 0)
2106 {
2107 allocate(__n);
2108 __construct_at_end(__n, false);
2109 }
2110}
2111
2112template <class _Allocator>
2113vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
2114 : __begin_(0),
2115 __size_(0),
2116 __cap_alloc_(0)
2117{
2118 if (__n > 0)
2119 {
2120 allocate(__n);
2121 __construct_at_end(__n, __x);
2122 }
2123}
2124
2125template <class _Allocator>
2126vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
2127 : __begin_(0),
2128 __size_(0),
2129 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2130{
2131 if (__n > 0)
2132 {
2133 allocate(__n);
2134 __construct_at_end(__n, __x);
2135 }
2136}
2137
2138template <class _Allocator>
2139template <class _InputIterator>
2140vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2141 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2142 !__is_forward_iterator<_InputIterator>::value>::type*)
2143 : __begin_(0),
2144 __size_(0),
2145 __cap_alloc_(0)
2146{
2147#ifndef _LIBCPP_NO_EXCEPTIONS
2148 try
2149 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002150#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002151 for (; __first != __last; ++__first)
2152 push_back(*__first);
2153#ifndef _LIBCPP_NO_EXCEPTIONS
2154 }
2155 catch (...)
2156 {
2157 if (__begin_ != 0)
2158 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2159 __invalidate_all_iterators();
2160 throw;
2161 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002162#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002163}
2164
2165template <class _Allocator>
2166template <class _InputIterator>
2167vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2168 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2169 !__is_forward_iterator<_InputIterator>::value>::type*)
2170 : __begin_(0),
2171 __size_(0),
2172 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2173{
2174#ifndef _LIBCPP_NO_EXCEPTIONS
2175 try
2176 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002177#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002178 for (; __first != __last; ++__first)
2179 push_back(*__first);
2180#ifndef _LIBCPP_NO_EXCEPTIONS
2181 }
2182 catch (...)
2183 {
2184 if (__begin_ != 0)
2185 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2186 __invalidate_all_iterators();
2187 throw;
2188 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002189#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002190}
2191
2192template <class _Allocator>
2193template <class _ForwardIterator>
2194vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2195 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2196 : __begin_(0),
2197 __size_(0),
2198 __cap_alloc_(0)
2199{
2200 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
2201 if (__n > 0)
2202 {
2203 allocate(__n);
2204 __construct_at_end(__first, __last);
2205 }
2206}
2207
2208template <class _Allocator>
2209template <class _ForwardIterator>
2210vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2211 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2212 : __begin_(0),
2213 __size_(0),
2214 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2215{
2216 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
2217 if (__n > 0)
2218 {
2219 allocate(__n);
2220 __construct_at_end(__first, __last);
2221 }
2222}
2223
2224template <class _Allocator>
2225vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
2226 : __begin_(0),
2227 __size_(0),
2228 __cap_alloc_(0)
2229{
2230 size_type __n = static_cast<size_type>(__il.size());
2231 if (__n > 0)
2232 {
2233 allocate(__n);
2234 __construct_at_end(__il.begin(), __il.end());
2235 }
2236}
2237
2238template <class _Allocator>
2239vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
2240 : __begin_(0),
2241 __size_(0),
2242 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2243{
2244 size_type __n = static_cast<size_type>(__il.size());
2245 if (__n > 0)
2246 {
2247 allocate(__n);
2248 __construct_at_end(__il.begin(), __il.end());
2249 }
2250}
2251
2252template <class _Allocator>
2253_LIBCPP_INLINE_VISIBILITY inline
2254vector<bool, _Allocator>::~vector()
2255{
2256 if (__begin_ != 0)
2257 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2258#ifdef _LIBCPP_DEBUG
2259 __invalidate_all_iterators();
2260#endif
2261}
2262
2263template <class _Allocator>
2264vector<bool, _Allocator>::vector(const vector& __v)
2265 : __begin_(0),
2266 __size_(0),
2267 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2268{
2269 if (__v.size() > 0)
2270 {
2271 allocate(__v.size());
2272 __construct_at_end(__v.begin(), __v.end());
2273 }
2274}
2275
2276template <class _Allocator>
2277vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
2278 : __begin_(0),
2279 __size_(0),
2280 __cap_alloc_(0, __a)
2281{
2282 if (__v.size() > 0)
2283 {
2284 allocate(__v.size());
2285 __construct_at_end(__v.begin(), __v.end());
2286 }
2287}
2288
2289template <class _Allocator>
2290vector<bool, _Allocator>&
2291vector<bool, _Allocator>::operator=(const vector& __v)
2292{
2293 if (this != &__v)
2294 {
2295 __copy_assign_alloc(__v);
2296 if (__v.__size_)
2297 {
2298 if (__v.__size_ > capacity())
2299 {
2300 deallocate();
2301 allocate(__v.__size_);
2302 }
2303 _STD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
2304 }
2305 __size_ = __v.__size_;
2306 }
2307 return *this;
2308}
2309
Howard Hinnant73d21a42010-09-04 23:28:19 +00002310#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2311
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002312template <class _Allocator>
2313_LIBCPP_INLINE_VISIBILITY inline
2314vector<bool, _Allocator>::vector(vector&& __v)
2315 : __begin_(__v.__begin_),
2316 __size_(__v.__size_),
2317 __cap_alloc_(__v.__cap_alloc_)
2318{
2319 __v.__begin_ = 0;
2320 __v.__size_ = 0;
2321 __v.__cap() = 0;
2322}
2323
2324template <class _Allocator>
2325vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
2326 : __begin_(0),
2327 __size_(0),
2328 __cap_alloc_(0, __a)
2329{
2330 if (__a == allocator_type(__v.__alloc()))
2331 {
2332 this->__begin_ = __v.__begin_;
2333 this->__size_ = __v.__size_;
2334 this->__cap() = __v.__cap();
2335 __v.__begin_ = nullptr;
2336 __v.__cap() = __v.__size_ = 0;
2337 }
2338 else if (__v.size() > 0)
2339 {
2340 allocate(__v.size());
2341 __construct_at_end(__v.begin(), __v.end());
2342 }
2343}
2344
2345template <class _Allocator>
2346_LIBCPP_INLINE_VISIBILITY inline
2347vector<bool, _Allocator>&
2348vector<bool, _Allocator>::operator=(vector&& __v)
2349{
2350 __move_assign(__v, integral_constant<bool,
2351 __storage_traits::propagate_on_container_move_assignment::value>());
2352}
2353
2354template <class _Allocator>
2355void
2356vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2357{
2358 if (__alloc() != __c.__alloc())
2359 assign(__c.begin(), __c.end());
2360 else
2361 __move_assign(__c, true_type());
2362}
2363
2364template <class _Allocator>
2365void
2366vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
2367{
2368 deallocate();
2369 this->__begin_ = __c.__begin_;
2370 this->__size_ = __c.__size_;
2371 this->__cap() = __c.__cap();
2372 __move_assign_alloc(__c);
2373 __c.__begin_ = nullptr;
2374 __c.__cap() = __c.__size_ = 0;
2375}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002376
2377#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002378
2379template <class _Allocator>
2380void
2381vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2382{
2383 __size_ = 0;
2384 if (__n > 0)
2385 {
2386 size_type __c = capacity();
2387 if (__n <= __c)
2388 __size_ = __n;
2389 else
2390 {
2391 vector __v(__alloc());
2392 __v.reserve(__recommend(__n));
2393 __v.__size_ = __n;
2394 swap(__v);
2395 }
2396 _STD::fill_n(begin(), __n, __x);
2397 }
2398}
2399
2400template <class _Allocator>
2401template <class _InputIterator>
2402typename enable_if
2403<
2404 __is_input_iterator<_InputIterator>::value &&
2405 !__is_forward_iterator<_InputIterator>::value,
2406 void
2407>::type
2408vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2409{
2410 clear();
2411 for (; __first != __last; ++__first)
2412 push_back(*__first);
2413}
2414
2415template <class _Allocator>
2416template <class _ForwardIterator>
2417typename enable_if
2418<
2419 __is_forward_iterator<_ForwardIterator>::value,
2420 void
2421>::type
2422vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2423{
2424 clear();
2425 difference_type __n = _STD::distance(__first, __last);
2426 if (__n)
2427 {
2428 if (__n > capacity())
2429 {
2430 deallocate();
2431 allocate(__n);
2432 }
2433 __construct_at_end(__first, __last);
2434 }
2435}
2436
2437template <class _Allocator>
2438void
2439vector<bool, _Allocator>::reserve(size_type __n)
2440{
2441 if (__n > capacity())
2442 {
2443 vector __v(this->__alloc());
2444 __v.allocate(__n);
2445 __v.__construct_at_end(this->begin(), this->end());
2446 swap(__v);
2447 __invalidate_all_iterators();
2448 }
2449}
2450
2451template <class _Allocator>
2452void
2453vector<bool, _Allocator>::shrink_to_fit()
2454{
2455 if (__external_cap_to_internal(size()) > __cap())
2456 {
2457#ifndef _LIBCPP_NO_EXCEPTIONS
2458 try
2459 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002460#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002461 vector(*this, allocator_type(__alloc())).swap(*this);
2462#ifndef _LIBCPP_NO_EXCEPTIONS
2463 }
2464 catch (...)
2465 {
2466 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002467#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002468 }
2469}
2470
2471template <class _Allocator>
2472typename vector<bool, _Allocator>::reference
2473vector<bool, _Allocator>::at(size_type __n)
2474{
2475 if (__n >= size())
2476 this->__throw_out_of_range();
2477 return (*this)[__n];
2478}
2479
2480template <class _Allocator>
2481typename vector<bool, _Allocator>::const_reference
2482vector<bool, _Allocator>::at(size_type __n) const
2483{
2484 if (__n >= size())
2485 this->__throw_out_of_range();
2486 return (*this)[__n];
2487}
2488
2489template <class _Allocator>
2490void
2491vector<bool, _Allocator>::push_back(const value_type& __x)
2492{
2493 if (this->__size_ == this->capacity())
2494 reserve(__recommend(this->__size_ + 1));
2495 ++this->__size_;
2496 back() = __x;
2497}
2498
2499template <class _Allocator>
2500typename vector<bool, _Allocator>::iterator
2501vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2502{
2503 iterator __r;
2504 if (size() < capacity())
2505 {
2506 const_iterator __old_end = end();
2507 ++__size_;
2508 _STD::copy_backward(__position, __old_end, end());
2509 __r = __const_iterator_cast(__position);
2510 }
2511 else
2512 {
2513 vector __v(__alloc());
2514 __v.reserve(__recommend(__size_ + 1));
2515 __v.__size_ = __size_ + 1;
2516 __r = _STD::copy(cbegin(), __position, __v.begin());
2517 _STD::copy_backward(__position, cend(), __v.end());
2518 swap(__v);
2519 }
2520 *__r = __x;
2521 return __r;
2522}
2523
2524template <class _Allocator>
2525typename vector<bool, _Allocator>::iterator
2526vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
2527{
2528 iterator __r;
2529 size_type __c = capacity();
2530 if (__n <= __c && size() <= __c - __n)
2531 {
2532 const_iterator __old_end = end();
2533 __size_ += __n;
2534 _STD::copy_backward(__position, __old_end, end());
2535 __r = __const_iterator_cast(__position);
2536 }
2537 else
2538 {
2539 vector __v(__alloc());
2540 __v.reserve(__recommend(__size_ + __n));
2541 __v.__size_ = __size_ + __n;
2542 __r = _STD::copy(cbegin(), __position, __v.begin());
2543 _STD::copy_backward(__position, cend(), __v.end());
2544 swap(__v);
2545 }
2546 _STD::fill_n(__r, __n, __x);
2547 return __r;
2548}
2549
2550template <class _Allocator>
2551template <class _InputIterator>
2552typename enable_if
2553<
2554 __is_input_iterator <_InputIterator>::value &&
2555 !__is_forward_iterator<_InputIterator>::value,
2556 typename vector<bool, _Allocator>::iterator
2557>::type
2558vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
2559{
2560 difference_type __off = __position - begin();
2561 iterator __p = __const_iterator_cast(__position);
2562 iterator __old_end = end();
2563 for (; size() != capacity() && __first != __last; ++__first)
2564 {
2565 ++this->__size_;
2566 back() = *__first;
2567 }
2568 vector __v(__alloc());
2569 if (__first != __last)
2570 {
2571#ifndef _LIBCPP_NO_EXCEPTIONS
2572 try
2573 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002574#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002575 __v.assign(__first, __last);
2576 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
2577 difference_type __old_p = __p - begin();
2578 reserve(__recommend(size() + __v.size()));
2579 __p = begin() + __old_p;
2580 __old_end = begin() + __old_size;
2581#ifndef _LIBCPP_NO_EXCEPTIONS
2582 }
2583 catch (...)
2584 {
2585 erase(__old_end, end());
2586 throw;
2587 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002588#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002589 }
2590 __p = _STD::rotate(__p, __old_end, end());
2591 insert(__p, __v.begin(), __v.end());
2592 return begin() + __off;
2593}
2594
2595template <class _Allocator>
2596template <class _ForwardIterator>
2597typename enable_if
2598<
2599 __is_forward_iterator<_ForwardIterator>::value,
2600 typename vector<bool, _Allocator>::iterator
2601>::type
2602vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
2603{
2604 difference_type __n = _STD::distance(__first, __last);
2605 iterator __r;
2606 size_type __c = capacity();
2607 if (__n <= __c && size() <= __c - __n)
2608 {
2609 const_iterator __old_end = end();
2610 __size_ += __n;
2611 _STD::copy_backward(__position, __old_end, end());
2612 __r = __const_iterator_cast(__position);
2613 }
2614 else
2615 {
2616 vector __v(__alloc());
2617 __v.reserve(__recommend(__size_ + __n));
2618 __v.__size_ = __size_ + __n;
2619 __r = _STD::copy(cbegin(), __position, __v.begin());
2620 _STD::copy_backward(__position, cend(), __v.end());
2621 swap(__v);
2622 }
2623 _STD::copy(__first, __last, __r);
2624 return __r;
2625}
2626
2627template <class _Allocator>
2628_LIBCPP_INLINE_VISIBILITY inline
2629typename vector<bool, _Allocator>::iterator
2630vector<bool, _Allocator>::erase(const_iterator __position)
2631{
2632 iterator __r = __const_iterator_cast(__position);
2633 _STD::copy(__position + 1, this->cend(), __r);
2634 --__size_;
2635 return __r;
2636}
2637
2638template <class _Allocator>
2639typename vector<bool, _Allocator>::iterator
2640vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
2641{
2642 iterator __r = __const_iterator_cast(__first);
2643 difference_type __d = __last - __first;
2644 _STD::copy(__last, this->cend(), __r);
2645 __size_ -= __d;
2646 return __r;
2647}
2648
2649template <class _Allocator>
2650void
2651vector<bool, _Allocator>::swap(vector& __x)
2652{
2653 _STD::swap(this->__begin_, __x.__begin_);
2654 _STD::swap(this->__size_, __x.__size_);
2655 _STD::swap(this->__cap(), __x.__cap());
2656 __swap_alloc(this->__alloc(), __x.__alloc());
2657#ifdef _LIBCPP_DEBUG
2658 iterator::swap(this, &__x);
2659 const_iterator::swap(this, &__x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002660#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002661}
2662
Howard Hinnant324bb032010-08-22 00:02:43 +00002663template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002664void
2665vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
2666{
2667 size_type __cs = size();
2668 if (__cs < __sz)
2669 {
2670 iterator __r;
2671 size_type __c = capacity();
2672 size_type __n = __sz - __cs;
2673 if (__n <= __c && __cs <= __c - __n)
2674 {
2675 __r = end();
2676 __size_ += __n;
2677 }
2678 else
2679 {
2680 vector __v(__alloc());
2681 __v.reserve(__recommend(__size_ + __n));
2682 __v.__size_ = __size_ + __n;
2683 __r = _STD::copy(cbegin(), cend(), __v.begin());
2684 swap(__v);
2685 }
2686 _STD::fill_n(__r, __n, __x);
2687 }
2688 else
2689 __size_ = __sz;
2690}
2691
Howard Hinnant324bb032010-08-22 00:02:43 +00002692template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002693void
2694vector<bool, _Allocator>::flip()
2695{
2696 // do middle whole words
2697 size_type __n = __size_;
2698 __storage_pointer __p = __begin_;
2699 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
2700 *__p = ~*__p;
2701 // do last partial word
2702 if (__n > 0)
2703 {
2704 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
2705 __storage_type __b = *__p & __m;
2706 *__p &= ~__m;
2707 *__p |= ~__b & __m;
2708 }
2709}
2710
Howard Hinnant324bb032010-08-22 00:02:43 +00002711template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002712bool
2713vector<bool, _Allocator>::__invariants() const
2714{
2715 if (this->__begin_ == 0)
2716 {
2717 if (this->__size_ != 0 || this->__cap() != 0)
2718 return false;
2719 }
2720 else
2721 {
2722 if (this->__cap() == 0)
2723 return false;
2724 if (this->__size_ > this->capacity())
2725 return false;
2726 }
2727 return true;
2728}
2729
Howard Hinnant324bb032010-08-22 00:02:43 +00002730template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002731size_t
2732vector<bool, _Allocator>::__hash_code() const
2733{
2734 size_t __h = 0;
2735 // do middle whole words
2736 size_type __n = __size_;
2737 __storage_pointer __p = __begin_;
2738 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
2739 __h ^= *__p;
2740 // do last partial word
2741 if (__n > 0)
2742 {
2743 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
2744 __h ^= *__p & __m;
2745 }
2746 return __h;
2747}
2748
2749template <class _Allocator>
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002750struct _LIBCPP_VISIBLE hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002751 : public unary_function<vector<bool, _Allocator>, size_t>
2752{
Howard Hinnantee6ccd02010-09-23 18:58:28 +00002753 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002754 size_t operator()(const vector<bool, _Allocator>& __vec) const
2755 {return __vec.__hash_code();}
2756};
2757
2758template <class _Tp, class _Allocator>
2759struct __is_zero_default_constructible<vector<_Tp, _Allocator> >
2760 : public integral_constant<bool, __is_zero_default_constructible<_Allocator>::value> {};
2761
2762template <class _Tp, class _Allocator>
2763_LIBCPP_INLINE_VISIBILITY inline
2764bool
2765operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2766{
2767 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
2768 return __sz == __y.size() && _STD::equal(__x.begin(), __x.end(), __y.begin());
2769}
2770
2771template <class _Tp, class _Allocator>
2772_LIBCPP_INLINE_VISIBILITY inline
2773bool
2774operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2775{
2776 return !(__x == __y);
2777}
2778
2779template <class _Tp, class _Allocator>
2780_LIBCPP_INLINE_VISIBILITY inline
2781bool
2782operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2783{
2784 return _STD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
2785}
2786
2787template <class _Tp, class _Allocator>
2788_LIBCPP_INLINE_VISIBILITY inline
2789bool
2790operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2791{
2792 return __y < __x;
2793}
2794
2795template <class _Tp, class _Allocator>
2796_LIBCPP_INLINE_VISIBILITY inline
2797bool
2798operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2799{
2800 return !(__x < __y);
2801}
2802
2803template <class _Tp, class _Allocator>
2804_LIBCPP_INLINE_VISIBILITY inline
2805bool
2806operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2807{
2808 return !(__y < __x);
2809}
2810
2811template <class _Tp, class _Allocator>
2812_LIBCPP_INLINE_VISIBILITY inline
2813void
2814swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
2815{
2816 __x.swap(__y);
2817}
2818
2819_LIBCPP_END_NAMESPACE_STD
2820
2821#endif // _LIBCPP_VECTOR