blob: 7441cd01af0cc889b2ed14960015e53805eac9ad [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//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
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)
329 {__destruct_at_end(__new_last, has_trivial_destructor<value_type>());}
330 void __destruct_at_end(const_pointer __new_last, false_type);
331 void __destruct_at_end(const_pointer __new_last, true_type);
332
333 void __copy_assign_alloc(const __vector_base& __c)
334 {__copy_assign_alloc(__c, integral_constant<bool,
335 __alloc_traits::propagate_on_container_copy_assignment::value>());}
336
337 void __move_assign_alloc(__vector_base& __c)
338 {__move_assign_alloc(__c, integral_constant<bool,
339 __alloc_traits::propagate_on_container_move_assignment::value>());}
340
341 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
342 {__swap_alloc(__x, __y, integral_constant<bool,
343 __alloc_traits::propagate_on_container_swap::value>());}
344private:
345 void __copy_assign_alloc(const __vector_base& __c, true_type)
346 {
347 if (__alloc() != __c.__alloc())
348 {
349 clear();
350 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
351 __begin_ = __end_ = __end_cap() = nullptr;
352 }
353 __alloc() = __c.__alloc();
354 }
355
356 void __copy_assign_alloc(const __vector_base& __c, false_type)
357 {}
358
359 void __move_assign_alloc(const __vector_base& __c, true_type)
360 {
361 __alloc() = _STD::move(__c.__alloc());
362 }
363
364 void __move_assign_alloc(const __vector_base& __c, false_type)
365 {}
366
367 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
368 {
369 using _STD::swap;
370 swap(__x, __y);
371 }
372 static void __swap_alloc(allocator_type& __x, allocator_type& __y, false_type)
373 {}
374};
375
376template <class _Tp, class _Allocator>
377_LIBCPP_INLINE_VISIBILITY inline
378void
379__vector_base<_Tp, _Allocator>::__destruct_at_end(const_pointer __new_last, false_type)
380{
381 while (__new_last < __end_)
382 __alloc_traits::destroy(__alloc(), const_cast<pointer>(--__end_));
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, true_type)
389{
390 __end_ = const_cast<pointer>(__new_last);
391}
392
393template <class _Tp, class _Allocator>
394_LIBCPP_INLINE_VISIBILITY inline
395__vector_base<_Tp, _Allocator>::__vector_base()
396 : __begin_(0),
397 __end_(0),
398 __end_cap_(0)
399{
400}
401
402template <class _Tp, class _Allocator>
403_LIBCPP_INLINE_VISIBILITY inline
404__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
405 : __begin_(0),
406 __end_(0),
407 __end_cap_(0, __a)
408{
409}
410
411template <class _Tp, class _Allocator>
412__vector_base<_Tp, _Allocator>::~__vector_base()
413{
414 if (__begin_ != 0)
415 {
416 clear();
417 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
418 }
419}
420
421template <class _Tp, class _Allocator = allocator<_Tp> >
422class vector
423 : private __vector_base<_Tp, _Allocator>
424{
425private:
426 typedef __vector_base<_Tp, _Allocator> __base;
Howard Hinnant324bb032010-08-22 00:02:43 +0000427public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000428 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +0000429 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430 typedef _Allocator allocator_type;
431 typedef typename __base::__alloc_traits __alloc_traits;
432 typedef typename __base::reference reference;
433 typedef typename __base::const_reference const_reference;
434 typedef typename __base::size_type size_type;
435 typedef typename __base::difference_type difference_type;
436 typedef typename __base::pointer pointer;
437 typedef typename __base::const_pointer const_pointer;
438#ifdef _LIBCPP_DEBUG
439 typedef __debug_iter<vector, pointer> iterator;
440 typedef __debug_iter<vector, const_pointer> const_iterator;
441
442 friend class __debug_iter<vector, pointer>;
443 friend class __debug_iter<vector, const_pointer>;
444
445 pair<iterator*, const_iterator*> __iterator_list_;
446
447 _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;}
448 _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;}
449#elif defined(_LIBCPP_RAW_ITERATORS)
450 typedef pointer iterator;
451 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000452#else // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000453 typedef __wrap_iter<pointer> iterator;
454 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000455#endif // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000456 typedef _STD::reverse_iterator<iterator> reverse_iterator;
457 typedef _STD::reverse_iterator<const_iterator> const_reverse_iterator;
458
459 _LIBCPP_INLINE_VISIBILITY vector() {}
460 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a) : __base(__a) {}
461 explicit vector(size_type __n);
462 vector(size_type __n, const_reference __x);
463 vector(size_type __n, const_reference __x, const allocator_type& __a);
464 template <class _InputIterator>
465 vector(_InputIterator __first, _InputIterator __last,
466 typename enable_if<__is_input_iterator <_InputIterator>::value &&
467 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
468 template <class _InputIterator>
469 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
470 typename enable_if<__is_input_iterator <_InputIterator>::value &&
471 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
472 template <class _ForwardIterator>
473 vector(_ForwardIterator __first, _ForwardIterator __last,
474 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
475 template <class _ForwardIterator>
476 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
477 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
478 vector(initializer_list<value_type> __il);
479 vector(initializer_list<value_type> __il, const allocator_type& __a);
480#ifdef _LIBCPP_DEBUG
481 ~vector() {__invalidate_all_iterators();}
482#endif
483
484 vector(const vector& __x);
485 vector(const vector& __x, const allocator_type& __a);
486 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000487#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000488 vector(vector&& __x);
489 vector(vector&& __x, const allocator_type& __a);
490 vector& operator=(vector&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000491#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000492 vector& operator=(initializer_list<value_type> __il)
493 {assign(__il.begin(), __il.end()); return *this;}
494
495 template <class _InputIterator>
496 typename enable_if
497 <
498 __is_input_iterator <_InputIterator>::value &&
499 !__is_forward_iterator<_InputIterator>::value,
500 void
501 >::type
502 assign(_InputIterator __first, _InputIterator __last);
503 template <class _ForwardIterator>
504 typename enable_if
505 <
506 __is_forward_iterator<_ForwardIterator>::value,
507 void
508 >::type
509 assign(_ForwardIterator __first, _ForwardIterator __last);
510
511 void assign(size_type __n, const_reference __u);
512 void assign(initializer_list<value_type> __il)
513 {assign(__il.begin(), __il.end());}
514
515 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const {return this->__alloc();}
516
517 iterator begin();
518 const_iterator begin() const;
519 iterator end();
520 const_iterator end() const;
521
522 _LIBCPP_INLINE_VISIBILITY reverse_iterator rbegin() {return reverse_iterator(end());}
523 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rbegin() const {return const_reverse_iterator(end());}
524 _LIBCPP_INLINE_VISIBILITY reverse_iterator rend() {return reverse_iterator(begin());}
525 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rend() const {return const_reverse_iterator(begin());}
526
527 _LIBCPP_INLINE_VISIBILITY const_iterator cbegin() const {return begin();}
528 _LIBCPP_INLINE_VISIBILITY const_iterator cend() const {return end();}
529 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crbegin() const {return rbegin();}
530 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crend() const {return rend();}
531
532 _LIBCPP_INLINE_VISIBILITY size_type size() const {return static_cast<size_type>(this->__end_ - this->__begin_);}
533 _LIBCPP_INLINE_VISIBILITY size_type capacity() const {return __base::capacity();}
534 _LIBCPP_INLINE_VISIBILITY bool empty() const {return this->__begin_ == this->__end_;}
535 size_type max_size() const;
536 void reserve(size_type __n);
537 void shrink_to_fit();
538
539 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
540 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
541 reference at(size_type __n);
542 const_reference at(size_type __n) const;
543
544 _LIBCPP_INLINE_VISIBILITY reference front() {return *this->__begin_;}
545 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return *this->__begin_;}
546 _LIBCPP_INLINE_VISIBILITY reference back() {return *(this->__end_ - 1);}
547 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return *(this->__end_ - 1);}
548
549 _LIBCPP_INLINE_VISIBILITY value_type* data()
550 {return _STD::__to_raw_pointer(this->__begin_);}
551 _LIBCPP_INLINE_VISIBILITY const value_type* data() const
552 {return _STD::__to_raw_pointer(this->__begin_);}
553
554 void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000555#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000556 void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000557#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000558 template <class... _Args>
559 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000560#endif // _LIBCPP_HAS_NO_VARIADICS
561#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000562 void pop_back();
563
564 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000565#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000566 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000567#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000568 template <class... _Args>
569 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000570#endif // _LIBCPP_HAS_NO_VARIADICS
571#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572 iterator insert(const_iterator __position, size_type __n, const_reference __x);
573 template <class _InputIterator>
574 typename enable_if
575 <
576 __is_input_iterator <_InputIterator>::value &&
577 !__is_forward_iterator<_InputIterator>::value,
578 iterator
579 >::type
580 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
581 template <class _ForwardIterator>
582 typename enable_if
583 <
584 __is_forward_iterator<_ForwardIterator>::value,
585 iterator
586 >::type
587 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
588 iterator insert(const_iterator __position, initializer_list<value_type> __il)
589 {return insert(__position, __il.begin(), __il.end());}
590
591 iterator erase(const_iterator __position);
592 iterator erase(const_iterator __first, const_iterator __last);
593
594 _LIBCPP_INLINE_VISIBILITY void clear() {__base::clear();}
595
596 void resize(size_type __sz);
597 void resize(size_type __sz, const_reference __x);
598
599 void swap(vector&);
600
601 bool __invariants() const;
602
603private:
604 void __invalidate_all_iterators();
605 void allocate(size_type __n);
606 void deallocate();
607 size_type __recommend(size_type __new_size) const;
608 void __construct_at_end(size_type __n);
609 void __construct_at_end(size_type __n, false_type);
610 void __construct_at_end(size_type __n, true_type);
611 void __construct_at_end(size_type __n, const_reference __x);
612 void __construct_at_end(size_type __n, const_reference __x, false_type);
613 void __construct_at_end(size_type __n, const_reference __x, true_type);
614 template <class _ForwardIterator>
615 typename enable_if
616 <
617 __is_forward_iterator<_ForwardIterator>::value,
618 void
619 >::type
620 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
621 void __move_construct_at_end(pointer __first, pointer __last);
622 void __append(size_type __n);
623 void __append(size_type __n, const_reference __x);
624 iterator __make_iter(pointer __p);
625 const_iterator __make_iter(const_pointer __p) const;
626 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
627 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
628 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
629 void __move_assign(vector& __c, true_type);
630 void __move_assign(vector& __c, false_type);
631};
632
633template <class _Tp, class _Allocator>
634void
635vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
636{
637 for (pointer __p = this->__end_; this->__begin_ < __p;)
638 __v.push_front(_STD::move(*--__p));
639 _STD::swap(this->__begin_, __v.__begin_);
640 _STD::swap(this->__end_, __v.__end_);
641 _STD::swap(this->__end_cap(), __v.__end_cap());
642 __v.__first_ = __v.__begin_;
643 __invalidate_all_iterators();
644}
645
646template <class _Tp, class _Allocator>
647typename vector<_Tp, _Allocator>::pointer
648vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
649{
650 pointer __r = __v.__begin_;
651 for (pointer __i = __p; this->__begin_ < __i;)
652 __v.push_front(_STD::move(*--__i));
653 for (pointer __i = __p; __i < this->__end_; ++__i)
654 __v.push_back(_STD::move(*__i));
655 _STD::swap(this->__begin_, __v.__begin_);
656 _STD::swap(this->__end_, __v.__end_);
657 _STD::swap(this->__end_cap(), __v.__end_cap());
658 __v.__first_ = __v.__begin_;
659 __invalidate_all_iterators();
660 return __r;
661}
662
663// Allocate space for __n objects
664// throws length_error if __n > max_size()
665// throws (probably bad_alloc) if memory run out
666// Precondition: __begin_ == __end_ == __end_cap() == 0
667// Precondition: __n > 0
668// Postcondition: capacity() == __n
669// Postcondition: size() == 0
670template <class _Tp, class _Allocator>
671void
672vector<_Tp, _Allocator>::allocate(size_type __n)
673{
674 if (__n > max_size())
675 this->__throw_length_error();
676 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
677 this->__end_cap() = this->__begin_ + __n;
678}
679
680template <class _Tp, class _Allocator>
681void
682vector<_Tp, _Allocator>::deallocate()
683{
684 if (this->__begin_ != 0)
685 {
686 clear();
687 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
688 __invalidate_all_iterators();
689 this->__begin_ = this->__end_ = this->__end_cap() = 0;
690 }
691}
692
693template <class _Tp, class _Allocator>
694typename vector<_Tp, _Allocator>::size_type
695vector<_Tp, _Allocator>::max_size() const
696{
697 return _STD::min(__alloc_traits::max_size(this->__alloc()), numeric_limits<size_type>::max() / 2); // end() >= begin(), always
698}
699
700// Precondition: __new_size > capacity()
701template <class _Tp, class _Allocator>
702_LIBCPP_INLINE_VISIBILITY inline
703typename vector<_Tp, _Allocator>::size_type
704vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
705{
706 const size_type __ms = max_size();
707 if (__new_size > __ms)
708 this->__throw_length_error();
709 const size_type __cap = capacity();
710 if (__cap >= __ms / 2)
711 return __ms;
712 return _STD::max(2*__cap, __new_size);
713}
714
715// Default constructs __n objects starting at __end_
716// throws if construction throws
717// Precondition: __n > 0
718// Precondition: size() + __n <= capacity()
719// Postcondition: size() == size() + __n
720template <class _Tp, class _Allocator>
721_LIBCPP_INLINE_VISIBILITY inline
722void
723vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
724{
725 __construct_at_end(__n, __is_zero_default_constructible<value_type>());
726}
727
728template <class _Tp, class _Allocator>
729void
730vector<_Tp, _Allocator>::__construct_at_end(size_type __n, false_type)
731{
732 allocator_type& __a = this->__alloc();
733 do
734 {
735 __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_));
736 ++this->__end_;
737 --__n;
738 } while (__n > 0);
739}
740
741template <class _Tp, class _Allocator>
742_LIBCPP_INLINE_VISIBILITY inline
743void
744vector<_Tp, _Allocator>::__construct_at_end(size_type __n, true_type)
745{
746 _STD::memset(this->__end_, 0, __n*sizeof(value_type));
747 this->__end_ += __n;
748}
749
750// Copy constructs __n objects starting at __end_ from __x
751// throws if construction throws
752// Precondition: __n > 0
753// Precondition: size() + __n <= capacity()
754// Postcondition: size() == old size() + __n
755// Postcondition: [i] == __x for all i in [size() - __n, __n)
756template <class _Tp, class _Allocator>
757_LIBCPP_INLINE_VISIBILITY inline
758void
759vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
760{
761 __construct_at_end(__n, __x, integral_constant<bool, has_trivial_copy_constructor<value_type>::value &&
762 has_trivial_copy_assign<value_type>::value>());
763}
764
765template <class _Tp, class _Allocator>
766void
767vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x, false_type)
768{
769 allocator_type& __a = this->__alloc();
770 do
771 {
772 __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_), __x);
773 ++this->__end_;
774 --__n;
775 } while (__n > 0);
776}
777
778template <class _Tp, class _Allocator>
779_LIBCPP_INLINE_VISIBILITY inline
780void
781vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x, true_type)
782{
783 _STD::fill_n(this->__end_, __n, __x);
784 this->__end_ += __n;
785}
786
787template <class _Tp, class _Allocator>
788template <class _ForwardIterator>
789typename enable_if
790<
791 __is_forward_iterator<_ForwardIterator>::value,
792 void
793>::type
794vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
795{
796 allocator_type& __a = this->__alloc();
797 for (; __first != __last; ++__first)
798 {
799 __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_), *__first);
800 ++this->__end_;
801 }
802}
803
804template <class _Tp, class _Allocator>
805void
806vector<_Tp, _Allocator>::__move_construct_at_end(pointer __first, pointer __last)
807{
808 allocator_type& __a = this->__alloc();
809 for (; __first != __last; ++__first)
810 {
811 __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_),
812 _STD::move(*__first));
813 ++this->__end_;
814 }
815}
816
817// Default constructs __n objects starting at __end_
818// throws if construction throws
819// Postcondition: size() == size() + __n
820// Exception safety: strong but assumes move ctor doesn't throw (copy ctor can)
821template <class _Tp, class _Allocator>
822void
823vector<_Tp, _Allocator>::__append(size_type __n)
824{
825 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
826 this->__construct_at_end(__n);
827 else
828 {
829 allocator_type& __a = this->__alloc();
830 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
831 __v.__construct_at_end(__n);
832 __swap_out_circular_buffer(__v);
833 }
834}
835
836// Default constructs __n objects starting at __end_
837// throws if construction throws
838// Postcondition: size() == size() + __n
839// Exception safety: strong but assumes move ctor doesn't throw (copy ctor can)
840template <class _Tp, class _Allocator>
841void
842vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
843{
844 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
845 this->__construct_at_end(__n, __x);
846 else
847 {
848 allocator_type& __a = this->__alloc();
849 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
850 __v.__construct_at_end(__n, __x);
851 __swap_out_circular_buffer(__v);
852 }
853}
854
855template <class _Tp, class _Allocator>
856vector<_Tp, _Allocator>::vector(size_type __n)
857{
858 if (__n > 0)
859 {
860 allocate(__n);
861 __construct_at_end(__n);
862 }
863}
864
865template <class _Tp, class _Allocator>
866vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
867{
868 if (__n > 0)
869 {
870 allocate(__n);
871 __construct_at_end(__n, __x);
872 }
873}
874
875template <class _Tp, class _Allocator>
876vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
877 : __base(__a)
878{
879 if (__n > 0)
880 {
881 allocate(__n);
882 __construct_at_end(__n, __x);
883 }
884}
885
886template <class _Tp, class _Allocator>
887template <class _InputIterator>
888vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
889 typename enable_if<__is_input_iterator <_InputIterator>::value &&
890 !__is_forward_iterator<_InputIterator>::value>::type*)
891{
892 for (; __first != __last; ++__first)
893 push_back(*__first);
894}
895
896template <class _Tp, class _Allocator>
897template <class _InputIterator>
898vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
899 typename enable_if<__is_input_iterator <_InputIterator>::value &&
900 !__is_forward_iterator<_InputIterator>::value>::type*)
901 : __base(__a)
902{
903 for (; __first != __last; ++__first)
904 push_back(*__first);
905}
906
907template <class _Tp, class _Allocator>
908template <class _ForwardIterator>
909vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
910 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
911{
912 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
913 if (__n > 0)
914 {
915 allocate(__n);
916 __construct_at_end(__first, __last);
917 }
918}
919
920template <class _Tp, class _Allocator>
921template <class _ForwardIterator>
922vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
923 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
924 : __base(__a)
925{
926 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
927 if (__n > 0)
928 {
929 allocate(__n);
930 __construct_at_end(__first, __last);
931 }
932}
933
934template <class _Tp, class _Allocator>
935vector<_Tp, _Allocator>::vector(const vector& __x)
936 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
937{
938 size_type __n = __x.size();
939 if (__n > 0)
940 {
941 allocate(__n);
942 __construct_at_end(__x.__begin_, __x.__end_);
943 }
944}
945
946template <class _Tp, class _Allocator>
947vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
948 : __base(__a)
949{
950 size_type __n = __x.size();
951 if (__n > 0)
952 {
953 allocate(__n);
954 __construct_at_end(__x.__begin_, __x.__end_);
955 }
956}
957
Howard Hinnant73d21a42010-09-04 23:28:19 +0000958#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000959
960template <class _Tp, class _Allocator>
961_LIBCPP_INLINE_VISIBILITY inline
962vector<_Tp, _Allocator>::vector(vector&& __x)
963 : __base(_STD::move(__x.__alloc()))
964{
965 this->__begin_ = __x.__begin_;
966 this->__end_ = __x.__end_;
967 this->__end_cap() = __x.__end_cap();
968 __x.__begin_ = __x.__end_ = __x.__end_cap() = 0;
969 __x.__invalidate_all_iterators();
970}
971
972template <class _Tp, class _Allocator>
973_LIBCPP_INLINE_VISIBILITY inline
974vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
975 : __base(__a)
976{
977 if (__a == __x.__alloc())
978 {
979 this->__begin_ = __x.__begin_;
980 this->__end_ = __x.__end_;
981 this->__end_cap() = __x.__end_cap();
982 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
983 __x.__invalidate_all_iterators();
984 }
985 else
986 {
987 typedef move_iterator<iterator> _I;
988 assign(_I(__x.begin()), _I(__x.end()));
989 }
990}
991
992template <class _Tp, class _Allocator>
993_LIBCPP_INLINE_VISIBILITY inline
994vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
995{
996 if (__il.size() > 0)
997 {
998 allocate(__il.size());
999 __construct_at_end(__il.begin(), __il.end());
1000 }
1001}
1002
1003template <class _Tp, class _Allocator>
1004_LIBCPP_INLINE_VISIBILITY inline
1005vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1006 : __base(__a)
1007{
1008 if (__il.size() > 0)
1009 {
1010 allocate(__il.size());
1011 __construct_at_end(__il.begin(), __il.end());
1012 }
1013}
1014
1015template <class _Tp, class _Allocator>
1016_LIBCPP_INLINE_VISIBILITY inline
1017vector<_Tp, _Allocator>&
1018vector<_Tp, _Allocator>::operator=(vector&& __x)
1019{
1020 __move_assign(__x, integral_constant<bool,
1021 __alloc_traits::propagate_on_container_move_assignment::value>());
1022 return *this;
1023}
1024
1025template <class _Tp, class _Allocator>
1026void
1027vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1028{
1029 if (__base::__alloc() != __c.__alloc())
1030 {
1031 typedef move_iterator<iterator> _I;
1032 assign(_I(__c.begin()), _I(__c.end()));
1033 }
1034 else
1035 __move_assign(__c, true_type());
1036}
1037
1038template <class _Tp, class _Allocator>
1039void
1040vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
1041{
1042 deallocate();
1043 this->__begin_ = __c.__begin_;
1044 this->__end_ = __c.__end_;
1045 this->__end_cap() = __c.__end_cap();
1046 __base::__move_assign_alloc(__c);
1047 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
1048}
1049
Howard Hinnant73d21a42010-09-04 23:28:19 +00001050#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001051
1052template <class _Tp, class _Allocator>
1053_LIBCPP_INLINE_VISIBILITY inline
1054vector<_Tp, _Allocator>&
1055vector<_Tp, _Allocator>::operator=(const vector& __x)
1056{
1057 if (this != &__x)
1058 {
1059 __base::__copy_assign_alloc(__x);
1060 assign(__x.__begin_, __x.__end_);
1061 }
1062 return *this;
1063}
1064
1065template <class _Tp, class _Allocator>
1066template <class _InputIterator>
1067typename enable_if
1068<
1069 __is_input_iterator <_InputIterator>::value &&
1070 !__is_forward_iterator<_InputIterator>::value,
1071 void
1072>::type
1073vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1074{
1075 clear();
1076 for (; __first != __last; ++__first)
1077 push_back(*__first);
1078}
1079
1080template <class _Tp, class _Allocator>
1081template <class _ForwardIterator>
1082typename enable_if
1083<
1084 __is_forward_iterator<_ForwardIterator>::value,
1085 void
1086>::type
1087vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1088{
1089 typename iterator_traits<_ForwardIterator>::difference_type __new_size = _STD::distance(__first, __last);
1090 if (static_cast<size_type>(__new_size) <= capacity())
1091 {
1092 _ForwardIterator __mid = __last;
1093 bool __growing = false;
1094 if (static_cast<size_type>(__new_size) > size())
1095 {
1096 __growing = true;
1097 __mid = __first;
1098 _STD::advance(__mid, size());
1099 }
1100 pointer __m = _STD::copy(__first, __mid, this->__begin_);
1101 if (__growing)
1102 __construct_at_end(__mid, __last);
1103 else
1104 this->__destruct_at_end(__m);
1105 }
1106 else
1107 {
1108 deallocate();
1109 allocate(__recommend(static_cast<size_type>(__new_size)));
1110 __construct_at_end(__first, __last);
1111 }
1112}
1113
1114template <class _Tp, class _Allocator>
1115void
1116vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1117{
1118 if (__n <= capacity())
1119 {
1120 size_type __s = size();
1121 _STD::fill_n(this->__begin_, min(__n, __s), __u);
1122 if (__n > __s)
1123 __construct_at_end(__n - __s, __u);
1124 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001125 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001126 }
1127 else
1128 {
1129 deallocate();
1130 allocate(__recommend(static_cast<size_type>(__n)));
1131 __construct_at_end(__n, __u);
1132 }
1133}
1134
Howard Hinnant324bb032010-08-22 00:02:43 +00001135template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001136_LIBCPP_INLINE_VISIBILITY inline
1137typename vector<_Tp, _Allocator>::iterator
1138vector<_Tp, _Allocator>::__make_iter(pointer __p)
1139{
1140#ifdef _LIBCPP_DEBUG
1141 return iterator(this, __p);
1142#else
1143 return iterator(__p);
1144#endif
1145}
1146
Howard Hinnant324bb032010-08-22 00:02:43 +00001147template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001148_LIBCPP_INLINE_VISIBILITY inline
1149typename vector<_Tp, _Allocator>::const_iterator
1150vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const
1151{
1152#ifdef _LIBCPP_DEBUG
1153 return const_iterator(this, __p);
1154#else
1155 return const_iterator(__p);
1156#endif
1157}
1158
Howard Hinnant324bb032010-08-22 00:02:43 +00001159template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001160_LIBCPP_INLINE_VISIBILITY inline
1161typename vector<_Tp, _Allocator>::iterator
1162vector<_Tp, _Allocator>::begin()
1163{
1164 return __make_iter(this->__begin_);
1165}
1166
Howard Hinnant324bb032010-08-22 00:02:43 +00001167template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001168_LIBCPP_INLINE_VISIBILITY inline
1169typename vector<_Tp, _Allocator>::const_iterator
1170vector<_Tp, _Allocator>::begin() const
1171{
1172 return __make_iter(this->__begin_);
1173}
1174
Howard Hinnant324bb032010-08-22 00:02:43 +00001175template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001176_LIBCPP_INLINE_VISIBILITY inline
1177typename vector<_Tp, _Allocator>::iterator
1178vector<_Tp, _Allocator>::end()
1179{
1180 return __make_iter(this->__end_);
1181}
1182
Howard Hinnant324bb032010-08-22 00:02:43 +00001183template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001184_LIBCPP_INLINE_VISIBILITY inline
1185typename vector<_Tp, _Allocator>::const_iterator
1186vector<_Tp, _Allocator>::end() const
1187{
1188 return __make_iter(this->__end_);
1189}
1190
Howard Hinnant324bb032010-08-22 00:02:43 +00001191template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001192_LIBCPP_INLINE_VISIBILITY inline
1193typename vector<_Tp, _Allocator>::reference
1194vector<_Tp, _Allocator>::operator[](size_type __n)
1195{
1196#ifdef _LIBCPP_DEBUG
1197 assert(__n < size());
1198#endif
1199 return this->__begin_[__n];
1200}
1201
Howard Hinnant324bb032010-08-22 00:02:43 +00001202template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001203_LIBCPP_INLINE_VISIBILITY inline
1204typename vector<_Tp, _Allocator>::const_reference
1205vector<_Tp, _Allocator>::operator[](size_type __n) const
1206{
1207#ifdef _LIBCPP_DEBUG
1208 assert(__n < size());
1209#endif
1210 return this->__begin_[__n];
1211}
1212
Howard Hinnant324bb032010-08-22 00:02:43 +00001213template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001214typename vector<_Tp, _Allocator>::reference
1215vector<_Tp, _Allocator>::at(size_type __n)
1216{
1217 if (__n >= size())
1218 this->__throw_out_of_range();
1219 return this->__begin_[__n];
1220}
1221
Howard Hinnant324bb032010-08-22 00:02:43 +00001222template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001223typename vector<_Tp, _Allocator>::const_reference
1224vector<_Tp, _Allocator>::at(size_type __n) const
1225{
1226 if (__n >= size())
1227 this->__throw_out_of_range();
1228 return this->__begin_[__n];
1229}
1230
1231template <class _Tp, class _Allocator>
1232void
1233vector<_Tp, _Allocator>::reserve(size_type __n)
1234{
1235 if (__n > capacity())
1236 {
1237 allocator_type& __a = this->__alloc();
1238 __split_buffer<value_type, allocator_type&> __v(__n, 0, __a);
1239 __v.__construct_at_end(move_iterator<pointer>(this->__begin_),
1240 move_iterator<pointer>(this->__end_));
1241 clear();
1242 __swap_out_circular_buffer(__v);
1243 }
1244}
1245
1246template <class _Tp, class _Allocator>
1247void
1248vector<_Tp, _Allocator>::shrink_to_fit()
1249{
1250 if (capacity() > size())
1251 {
1252#ifndef _LIBCPP_NO_EXCEPTIONS
1253 try
1254 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001255#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001256 allocator_type& __a = this->__alloc();
1257 __split_buffer<value_type, allocator_type&> __v(size(), 0, __a);
1258 __v.__construct_at_end(move_iterator<pointer>(this->__begin_),
1259 move_iterator<pointer>(this->__end_));
1260 clear();
1261 __swap_out_circular_buffer(__v);
1262#ifndef _LIBCPP_NO_EXCEPTIONS
1263 }
1264 catch (...)
1265 {
1266 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001267#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001268 }
1269}
1270
1271template <class _Tp, class _Allocator>
1272void
1273vector<_Tp, _Allocator>::push_back(const_reference __x)
1274{
1275 if (this->__end_ < this->__end_cap())
1276 {
1277 __alloc_traits::construct(this->__alloc(),
1278 _STD::__to_raw_pointer(this->__end_), __x);
1279 ++this->__end_;
1280 }
1281 else
1282 {
1283 allocator_type& __a = this->__alloc();
1284 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1285 __v.push_back(__x);
1286 __swap_out_circular_buffer(__v);
1287 }
1288}
1289
Howard Hinnant73d21a42010-09-04 23:28:19 +00001290#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001291
1292template <class _Tp, class _Allocator>
1293void
1294vector<_Tp, _Allocator>::push_back(value_type&& __x)
1295{
1296 if (this->__end_ < this->__end_cap())
1297 {
1298 __alloc_traits::construct(this->__alloc(),
1299 _STD::__to_raw_pointer(this->__end_),
1300 _STD::move(__x));
1301 ++this->__end_;
1302 }
1303 else
1304 {
1305 allocator_type& __a = this->__alloc();
1306 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1307 __v.push_back(_STD::move(__x));
1308 __swap_out_circular_buffer(__v);
1309 }
1310}
1311
Howard Hinnant73d21a42010-09-04 23:28:19 +00001312#ifndef _LIBCPP_HAS_NO_VARIADICS
1313
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001314template <class _Tp, class _Allocator>
1315template <class... _Args>
1316void
1317vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1318{
1319 if (this->__end_ < this->__end_cap())
1320 {
1321 __alloc_traits::construct(this->__alloc(),
1322 _STD::__to_raw_pointer(this->__end_),
1323 _STD::forward<_Args>(__args)...);
1324 ++this->__end_;
1325 }
1326 else
1327 {
1328 allocator_type& __a = this->__alloc();
1329 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1330 __v.emplace_back(_STD::forward<_Args>(__args)...);
1331 __swap_out_circular_buffer(__v);
1332 }
1333}
1334
Howard Hinnant73d21a42010-09-04 23:28:19 +00001335#endif // _LIBCPP_HAS_NO_VARIADICS
1336#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001337
1338template <class _Tp, class _Allocator>
1339_LIBCPP_INLINE_VISIBILITY inline
1340void
1341vector<_Tp, _Allocator>::pop_back()
1342{
1343 this->__destruct_at_end(this->__end_ - 1);
1344}
1345
1346template <class _Tp, class _Allocator>
1347_LIBCPP_INLINE_VISIBILITY inline
1348typename vector<_Tp, _Allocator>::iterator
1349vector<_Tp, _Allocator>::erase(const_iterator __position)
1350{
1351 pointer __p = const_cast<pointer>(&*__position);
1352 iterator __r = __make_iter(__p);
1353 this->__destruct_at_end(_STD::move(__p + 1, this->__end_, __p));
1354 return __r;
1355}
1356
1357template <class _Tp, class _Allocator>
1358typename vector<_Tp, _Allocator>::iterator
1359vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1360{
1361 pointer __p = this->__begin_ + (__first - begin());
1362 iterator __r = __make_iter(__p);
1363 this->__destruct_at_end(_STD::move(__p + (__last - __first), this->__end_, __p));
1364 return __r;
1365}
1366
1367template <class _Tp, class _Allocator>
1368void
1369vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1370{
1371 pointer __old_last = this->__end_;
1372 difference_type __n = __old_last - __to;
1373 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1374 __alloc_traits::construct(this->__alloc(),
1375 _STD::__to_raw_pointer(this->__end_),
1376 _STD::move(*__i));
1377 _STD::move_backward(__from_s, __from_s + __n, __old_last);
1378}
1379
1380template <class _Tp, class _Allocator>
1381typename vector<_Tp, _Allocator>::iterator
1382vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1383{
1384 pointer __p = this->__begin_ + (__position - begin());
1385 if (this->__end_ < this->__end_cap())
1386 {
1387 if (__p == this->__end_)
1388 {
1389 __alloc_traits::construct(this->__alloc(),
1390 _STD::__to_raw_pointer(this->__end_), __x);
1391 ++this->__end_;
1392 }
1393 else
1394 {
1395 __move_range(__p, this->__end_, __p + 1);
1396 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1397 if (__p <= __xr && __xr < this->__end_)
1398 ++__xr;
1399 *__p = *__xr;
1400 }
1401 }
1402 else
1403 {
1404 allocator_type& __a = this->__alloc();
1405 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1406 __v.push_back(__x);
1407 __p = __swap_out_circular_buffer(__v, __p);
1408 }
1409 return __make_iter(__p);
1410}
1411
Howard Hinnant73d21a42010-09-04 23:28:19 +00001412#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001413
1414template <class _Tp, class _Allocator>
1415typename vector<_Tp, _Allocator>::iterator
1416vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1417{
1418 pointer __p = this->__begin_ + (__position - begin());
1419 if (this->__end_ < this->__end_cap())
1420 {
1421 if (__p == this->__end_)
1422 {
1423 __alloc_traits::construct(this->__alloc(),
1424 _STD::__to_raw_pointer(this->__end_),
1425 _STD::move(__x));
1426 ++this->__end_;
1427 }
1428 else
1429 {
1430 __move_range(__p, this->__end_, __p + 1);
1431 *__p = _STD::move(__x);
1432 }
1433 }
1434 else
1435 {
1436 allocator_type& __a = this->__alloc();
1437 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1438 __v.push_back(_STD::move(__x));
1439 __p = __swap_out_circular_buffer(__v, __p);
1440 }
1441 return __make_iter(__p);
1442}
1443
Howard Hinnant73d21a42010-09-04 23:28:19 +00001444#ifndef _LIBCPP_HAS_NO_VARIADICS
1445
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001446template <class _Tp, class _Allocator>
1447template <class... _Args>
1448typename vector<_Tp, _Allocator>::iterator
1449vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1450{
1451 pointer __p = this->__begin_ + (__position - begin());
1452 if (this->__end_ < this->__end_cap())
1453 {
1454 if (__p == this->__end_)
1455 {
1456 __alloc_traits::construct(this->__alloc(),
1457 _STD::__to_raw_pointer(this->__end_),
1458 _STD::forward<_Args>(__args)...);
1459 ++this->__end_;
1460 }
1461 else
1462 {
1463 __move_range(__p, this->__end_, __p + 1);
1464 *__p = value_type(_STD::forward<_Args>(__args)...);
1465 }
1466 }
1467 else
1468 {
1469 allocator_type& __a = this->__alloc();
1470 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1471 __v.emplace_back(_STD::forward<_Args>(__args)...);
1472 __p = __swap_out_circular_buffer(__v, __p);
1473 }
1474 return __make_iter(__p);
1475}
1476
Howard Hinnant73d21a42010-09-04 23:28:19 +00001477#endif // _LIBCPP_HAS_NO_VARIADICS
1478#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001479
1480template <class _Tp, class _Allocator>
1481typename vector<_Tp, _Allocator>::iterator
1482vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1483{
1484 pointer __p = this->__begin_ + (__position - begin());
1485 if (__n > 0)
1486 {
1487 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1488 {
1489 size_type __old_n = __n;
1490 pointer __old_last = this->__end_;
1491 if (__n > static_cast<size_type>(this->__end_ - __p))
1492 {
1493 size_type __cx = __n - (this->__end_ - __p);
1494 __construct_at_end(__cx, __x);
1495 __n -= __cx;
1496 }
1497 if (__n > 0)
1498 {
1499 __move_range(__p, __old_last, __p + __old_n);
1500 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1501 if (__p <= __xr && __xr < this->__end_)
1502 __xr += __old_n;
1503 _STD::fill_n(__p, __n, *__xr);
1504 }
1505 }
1506 else
1507 {
1508 allocator_type& __a = this->__alloc();
1509 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1510 __v.__construct_at_end(__n, __x);
1511 __p = __swap_out_circular_buffer(__v, __p);
1512 }
1513 }
1514 return __make_iter(__p);
1515}
1516
1517template <class _Tp, class _Allocator>
1518template <class _InputIterator>
1519typename enable_if
1520<
1521 __is_input_iterator <_InputIterator>::value &&
1522 !__is_forward_iterator<_InputIterator>::value,
1523 typename vector<_Tp, _Allocator>::iterator
1524>::type
1525vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1526{
1527 difference_type __off = __position - begin();
1528 pointer __p = this->__begin_ + __off;
1529 allocator_type& __a = this->__alloc();
1530 pointer __old_last = this->__end_;
1531 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1532 {
1533 __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_),
1534 *__first);
1535 ++this->__end_;
1536 }
1537 __split_buffer<value_type, allocator_type&> __v(__a);
1538 if (__first != __last)
1539 {
1540#ifndef _LIBCPP_NO_EXCEPTIONS
1541 try
1542 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001543#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001544 __v.__construct_at_end(__first, __last);
1545 difference_type __old_size = __old_last - this->__begin_;
1546 difference_type __old_p = __p - this->__begin_;
1547 reserve(__recommend(size() + __v.size()));
1548 __p = this->__begin_ + __old_p;
1549 __old_last = this->__begin_ + __old_size;
1550#ifndef _LIBCPP_NO_EXCEPTIONS
1551 }
1552 catch (...)
1553 {
1554 erase(__make_iter(__old_last), end());
1555 throw;
1556 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001557#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001558 }
1559 __p = _STD::rotate(__p, __old_last, this->__end_);
1560 insert(__make_iter(__p), move_iterator<iterator>(__v.begin()),
1561 move_iterator<iterator>(__v.end()));
1562 return begin() + __off;
1563}
1564
1565template <class _Tp, class _Allocator>
1566template <class _ForwardIterator>
1567typename enable_if
1568<
1569 __is_forward_iterator<_ForwardIterator>::value,
1570 typename vector<_Tp, _Allocator>::iterator
1571>::type
1572vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1573{
1574 pointer __p = this->__begin_ + (__position - begin());
1575 difference_type __n = _STD::distance(__first, __last);
1576 if (__n > 0)
1577 {
1578 if (__n <= this->__end_cap() - this->__end_)
1579 {
1580 size_type __old_n = __n;
1581 pointer __old_last = this->__end_;
1582 _ForwardIterator __m = __last;
1583 difference_type __dx = this->__end_ - __p;
1584 if (__n > __dx)
1585 {
1586 __m = __first;
1587 _STD::advance(__m, this->__end_ - __p);
1588 __construct_at_end(__m, __last);
1589 __n = __dx;
1590 }
1591 if (__n > 0)
1592 {
1593 __move_range(__p, __old_last, __p + __old_n);
1594 _STD::copy(__first, __m, __p);
1595 }
1596 }
1597 else
1598 {
1599 allocator_type& __a = this->__alloc();
1600 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1601 __v.__construct_at_end(__first, __last);
1602 __p = __swap_out_circular_buffer(__v, __p);
1603 }
1604 }
1605 return __make_iter(__p);
1606}
1607
1608template <class _Tp, class _Allocator>
1609void
1610vector<_Tp, _Allocator>::resize(size_type __sz)
1611{
1612 size_type __cs = size();
1613 if (__cs < __sz)
1614 this->__append(__sz - __cs);
1615 else if (__cs > __sz)
1616 this->__destruct_at_end(this->__begin_ + __sz);
1617}
1618
1619template <class _Tp, class _Allocator>
1620void
1621vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
1622{
1623 size_type __cs = size();
1624 if (__cs < __sz)
1625 this->__append(__sz - __cs, __x);
1626 else if (__cs > __sz)
1627 this->__destruct_at_end(this->__begin_ + __sz);
1628}
1629
1630template <class _Tp, class _Allocator>
1631void
1632vector<_Tp, _Allocator>::swap(vector& __x)
1633{
1634 _STD::swap(this->__begin_, __x.__begin_);
1635 _STD::swap(this->__end_, __x.__end_);
1636 _STD::swap(this->__end_cap(), __x.__end_cap());
1637 __base::__swap_alloc(this->__alloc(), __x.__alloc());
1638#ifdef _LIBCPP_DEBUG
1639 iterator::swap(this, &__x);
1640 const_iterator::swap(this, &__x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001641#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001642}
1643
Howard Hinnant324bb032010-08-22 00:02:43 +00001644template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001645bool
1646vector<_Tp, _Allocator>::__invariants() const
1647{
1648 if (this->__begin_ == 0)
1649 {
1650 if (this->__end_ != 0 || this->__end_cap() != 0)
1651 return false;
1652 }
1653 else
1654 {
1655 if (this->__begin_ > this->__end_)
1656 return false;
1657 if (this->__begin_ == this->__end_cap())
1658 return false;
1659 if (this->__end_ > this->__end_cap())
1660 return false;
1661 }
1662 return true;
1663}
1664
1665template <class _Tp, class _Allocator>
1666#ifndef _LIBCPP_DEBUG
1667_LIBCPP_INLINE_VISIBILITY inline
1668#endif
1669void
1670vector<_Tp, _Allocator>::__invalidate_all_iterators()
1671{
1672#ifdef _LIBCPP_DEBUG
1673 iterator::__remove_all(this);
1674 const_iterator::__remove_all(this);
Howard Hinnant324bb032010-08-22 00:02:43 +00001675#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676}
1677
1678// vector<bool>
1679
1680template <class _Allocator> class vector<bool, _Allocator>;
1681
1682template <class _Allocator> struct hash<vector<bool, _Allocator> >;
1683
1684template <class _Allocator>
1685class vector<bool, _Allocator>
1686 : private __vector_base_common<true>
1687{
1688public:
1689 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43 +00001690 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001691 typedef _Allocator allocator_type;
1692 typedef allocator_traits<allocator_type> __alloc_traits;
1693 typedef __bit_reference<vector> reference;
1694 typedef __bit_const_reference<vector> const_reference;
1695 typedef typename __alloc_traits::size_type size_type;
1696 typedef typename __alloc_traits::difference_type difference_type;
1697 typedef __bit_iterator<vector, false> pointer;
1698 typedef __bit_iterator<vector, true> const_pointer;
1699#ifdef _LIBCPP_DEBUG
1700 typedef __debug_iter<vector, pointer> iterator;
1701 typedef __debug_iter<vector, const_pointer> const_iterator;
1702
1703 friend class __debug_iter<vector, pointer>;
1704 friend class __debug_iter<vector, const_pointer>;
1705
1706 pair<iterator*, const_iterator*> __iterator_list_;
1707
1708 _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;}
1709 _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;}
Howard Hinnant324bb032010-08-22 00:02:43 +00001710#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001711 typedef pointer iterator;
1712 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +00001713#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001714 typedef _STD::reverse_iterator<iterator> reverse_iterator;
1715 typedef _STD::reverse_iterator<const_iterator> const_reverse_iterator;
1716
1717private:
1718 typedef size_type __storage_type;
1719 typedef typename __alloc_traits::template
1720#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1721 rebind_alloc<__storage_type>
1722#else
1723 rebind_alloc<__storage_type>::other
1724#endif
1725 __storage_allocator;
1726 typedef allocator_traits<__storage_allocator> __storage_traits;
1727 typedef typename __storage_traits::pointer __storage_pointer;
1728 typedef typename __storage_traits::const_pointer __const_storage_pointer;
1729
1730 __storage_pointer __begin_;
1731 size_type __size_;
1732 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
1733
1734 _LIBCPP_INLINE_VISIBILITY size_type& __cap() {return __cap_alloc_.first();}
1735 _LIBCPP_INLINE_VISIBILITY const size_type& __cap() const {return __cap_alloc_.first();}
1736 _LIBCPP_INLINE_VISIBILITY __storage_allocator& __alloc() {return __cap_alloc_.second();}
1737 _LIBCPP_INLINE_VISIBILITY const __storage_allocator& __alloc() const {return __cap_alloc_.second();}
1738
1739 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
1740
1741 _LIBCPP_INLINE_VISIBILITY static size_type __internal_cap_to_external(size_type __n)
1742 {return __n * __bits_per_word;}
1743 _LIBCPP_INLINE_VISIBILITY static size_type __external_cap_to_internal(size_type __n)
1744 {return (__n - 1) / __bits_per_word + 1;}
1745
1746public:
1747 vector();
1748 explicit vector(const allocator_type& __a);
1749 ~vector();
1750 explicit vector(size_type __n);
1751 vector(size_type __n, const value_type& __v);
1752 vector(size_type __n, const value_type& __v, const allocator_type& __a);
1753 template <class _InputIterator>
1754 vector(_InputIterator __first, _InputIterator __last,
1755 typename enable_if<__is_input_iterator <_InputIterator>::value &&
1756 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
1757 template <class _InputIterator>
1758 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1759 typename enable_if<__is_input_iterator <_InputIterator>::value &&
1760 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
1761 template <class _ForwardIterator>
1762 vector(_ForwardIterator __first, _ForwardIterator __last,
1763 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
1764 template <class _ForwardIterator>
1765 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
1766 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
1767
1768 vector(const vector& __v);
1769 vector(const vector& __v, const allocator_type& __a);
1770 vector& operator=(const vector& __v);
1771 vector(initializer_list<value_type> __il);
1772 vector(initializer_list<value_type> __il, const allocator_type& __a);
1773
Howard Hinnant73d21a42010-09-04 23:28:19 +00001774#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001775 vector(vector&& __v);
1776 vector(vector&& __v, const allocator_type& __a);
1777 vector& operator=(vector&& __v);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001778#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001779 vector& operator=(initializer_list<value_type> __il)
1780 {assign(__il.begin(), __il.end()); return *this;}
1781
1782 template <class _InputIterator>
1783 typename enable_if
1784 <
1785 __is_input_iterator<_InputIterator>::value &&
1786 !__is_forward_iterator<_InputIterator>::value,
1787 void
1788 >::type
1789 assign(_InputIterator __first, _InputIterator __last);
1790 template <class _ForwardIterator>
1791 typename enable_if
1792 <
1793 __is_forward_iterator<_ForwardIterator>::value,
1794 void
1795 >::type
1796 assign(_ForwardIterator __first, _ForwardIterator __last);
1797
1798 void assign(size_type __n, const value_type& __x);
1799 void assign(initializer_list<value_type> __il)
1800 {assign(__il.begin(), __il.end());}
1801
1802 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const
1803 {return allocator_type(this->__alloc());}
1804
1805 size_type max_size() const;
1806 _LIBCPP_INLINE_VISIBILITY size_type capacity() const {return __internal_cap_to_external(__cap());}
1807 _LIBCPP_INLINE_VISIBILITY size_type size() const {return __size_;}
1808 _LIBCPP_INLINE_VISIBILITY bool empty() const {return __size_ == 0;}
1809 void reserve(size_type __n);
1810 void shrink_to_fit();
1811
1812 _LIBCPP_INLINE_VISIBILITY iterator begin() {return __make_iter(0);}
1813 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const {return __make_iter(0);}
1814 _LIBCPP_INLINE_VISIBILITY iterator end() {return __make_iter(__size_);}
1815 _LIBCPP_INLINE_VISIBILITY const_iterator end() const {return __make_iter(__size_);}
1816
1817 _LIBCPP_INLINE_VISIBILITY reverse_iterator rbegin() {return reverse_iterator(end());}
1818 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rbegin() const {return const_reverse_iterator(end());}
1819 _LIBCPP_INLINE_VISIBILITY reverse_iterator rend() {return reverse_iterator(begin());}
1820 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rend() const {return const_reverse_iterator(begin());}
1821
1822 _LIBCPP_INLINE_VISIBILITY const_iterator cbegin() const {return __make_iter(0);}
1823 _LIBCPP_INLINE_VISIBILITY const_iterator cend() const {return __make_iter(__size_);}
1824 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crbegin() const {return rbegin();}
1825 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crend() const {return rend();}
1826
1827 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
1828 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
1829 reference at(size_type __n);
1830 const_reference at(size_type __n) const;
1831
1832 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
1833 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
1834 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
1835 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
1836
1837 void push_back(const value_type& __x);
1838 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
1839
1840 iterator insert(const_iterator __position, const value_type& __x);
1841 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
1842 iterator insert(const_iterator __position, size_type __n, const_reference __x);
1843 template <class _InputIterator>
1844 typename enable_if
1845 <
1846 __is_input_iterator <_InputIterator>::value &&
1847 !__is_forward_iterator<_InputIterator>::value,
1848 iterator
1849 >::type
1850 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
1851 template <class _ForwardIterator>
1852 typename enable_if
1853 <
1854 __is_forward_iterator<_ForwardIterator>::value,
1855 iterator
1856 >::type
1857 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
1858 iterator insert(const_iterator __position, initializer_list<value_type> __il)
1859 {return insert(__position, __il.begin(), __il.end());}
1860
1861 iterator erase(const_iterator __position);
1862 iterator erase(const_iterator __first, const_iterator __last);
1863
1864 _LIBCPP_INLINE_VISIBILITY void clear() {__size_ = 0;}
1865
1866 void swap(vector&);
1867
1868 void resize(size_type __sz, value_type __x = false);
1869 void flip();
1870
1871 bool __invariants() const;
1872
1873private:
1874 void __invalidate_all_iterators();
1875 void allocate(size_type __n);
1876 void deallocate();
1877 _LIBCPP_INLINE_VISIBILITY static size_type __align(size_type __new_size)
1878 {return __new_size + (__bits_per_word-1) & ~(__bits_per_word-1);};
1879 size_type __recommend(size_type __new_size) const;
1880 void __construct_at_end(size_type __n, bool __x);
1881 template <class _ForwardIterator>
1882 typename enable_if
1883 <
1884 __is_forward_iterator<_ForwardIterator>::value,
1885 void
1886 >::type
1887 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
1888 void __append(size_type __n, const_reference __x);
1889 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_type __pos)
1890 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
1891 _LIBCPP_INLINE_VISIBILITY const_reference __make_ref(size_type __pos) const
1892 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
1893#ifdef _LIBCPP_DEBUG
1894 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_type __pos)
1895 {return iterator(this, pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
1896 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_type __pos) const
1897 {return const_iterator(this, const_pointer(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word)));}
1898 _LIBCPP_INLINE_VISIBILITY iterator __const_iterator_cast(const_iterator __p)
1899 {return iterator(this, pointer(const_cast<__storage_pointer>(__p.base().__seg_), __p.base().__ctz_));}
Howard Hinnant324bb032010-08-22 00:02:43 +00001900#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001901 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_type __pos)
1902 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
1903 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_type __pos) const
1904 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
1905 _LIBCPP_INLINE_VISIBILITY iterator __const_iterator_cast(const_iterator __p)
1906 {return iterator(const_cast<__storage_pointer>(__p.__seg_), __p.__ctz_);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001907#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001908
1909 void __copy_assign_alloc(const vector& __v)
1910 {__copy_assign_alloc(__v, integral_constant<bool,
1911 __storage_traits::propagate_on_container_copy_assignment::value>());}
1912 void __copy_assign_alloc(const vector& __c, true_type)
1913 {
1914 if (__alloc() != __c.__alloc())
1915 deallocate();
1916 __alloc() = __c.__alloc();
1917 }
1918
1919 void __copy_assign_alloc(const vector& __c, false_type)
1920 {}
1921
1922 void __move_assign(vector& __c, false_type);
1923 void __move_assign(vector& __c, true_type);
1924 void __move_assign_alloc(vector& __c)
1925 {__move_assign_alloc(__c, integral_constant<bool,
1926 __storage_traits::propagate_on_container_move_assignment::value>());}
1927 void __move_assign_alloc(const vector& __c, true_type)
1928 {
1929 __alloc() = _STD::move(__c.__alloc());
1930 }
1931
1932 void __move_assign_alloc(const vector& __c, false_type)
1933 {}
1934
1935 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y)
1936 {__swap_alloc(__x, __y, integral_constant<bool,
1937 __storage_traits::propagate_on_container_swap::value>());}
1938
1939 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, true_type)
1940 {
1941 using _STD::swap;
1942 swap(__x, __y);
1943 }
1944 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, false_type)
1945 {}
1946
1947 size_t __hash_code() const;
1948
1949 friend class __bit_reference<vector>;
1950 friend class __bit_const_reference<vector>;
1951 friend class __bit_iterator<vector, false>;
1952 friend class __bit_iterator<vector, true>;
1953 friend class __bit_array<vector>;
1954 friend struct hash<vector>;
1955};
1956
1957template <class _Allocator>
1958#ifndef _LIBCPP_DEBUG
1959_LIBCPP_INLINE_VISIBILITY inline
1960#endif
1961void
1962vector<bool, _Allocator>::__invalidate_all_iterators()
1963{
1964#ifdef _LIBCPP_DEBUG
1965 iterator::__remove_all(this);
1966 const_iterator::__remove_all(this);
Howard Hinnant324bb032010-08-22 00:02:43 +00001967#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001968}
1969
1970// Allocate space for __n objects
1971// throws length_error if __n > max_size()
1972// throws (probably bad_alloc) if memory run out
1973// Precondition: __begin_ == __end_ == __cap() == 0
1974// Precondition: __n > 0
1975// Postcondition: capacity() == __n
1976// Postcondition: size() == 0
1977template <class _Allocator>
1978void
1979vector<bool, _Allocator>::allocate(size_type __n)
1980{
1981 if (__n > max_size())
1982 this->__throw_length_error();
1983 __n = __external_cap_to_internal(__n);
1984 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
1985 this->__size_ = 0;
1986 this->__cap() = __n;
1987}
1988
1989template <class _Allocator>
1990void
1991vector<bool, _Allocator>::deallocate()
1992{
1993 if (this->__begin_ != 0)
1994 {
1995 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
1996 __invalidate_all_iterators();
1997 this->__begin_ = 0;
1998 this->__size_ = this->__cap() = 0;
1999 }
2000}
2001
2002template <class _Allocator>
2003typename vector<bool, _Allocator>::size_type
2004vector<bool, _Allocator>::max_size() const
2005{
2006 size_type __amax = __storage_traits::max_size(__alloc());
2007 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2008 if (__nmax / __bits_per_word <= __amax)
2009 return __nmax;
2010 return __internal_cap_to_external(__amax);
2011}
2012
2013// Precondition: __new_size > capacity()
2014template <class _Allocator>
2015_LIBCPP_INLINE_VISIBILITY inline
2016typename vector<bool, _Allocator>::size_type
2017vector<bool, _Allocator>::__recommend(size_type __new_size) const
2018{
2019 const size_type __ms = max_size();
2020 if (__new_size > __ms)
2021 this->__throw_length_error();
2022 const size_type __cap = capacity();
2023 if (__cap >= __ms / 2)
2024 return __ms;
2025 return _STD::max(2*__cap, __align(__new_size));
2026}
2027
2028// Default constructs __n objects starting at __end_
2029// Precondition: __n > 0
2030// Precondition: size() + __n <= capacity()
2031// Postcondition: size() == size() + __n
2032template <class _Allocator>
2033_LIBCPP_INLINE_VISIBILITY inline
2034void
2035vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2036{
2037 size_type __old_size = this->__size_;
2038 this->__size_ += __n;
2039 _STD::fill_n(__make_iter(__old_size), __n, __x);
2040}
2041
2042template <class _Allocator>
2043template <class _ForwardIterator>
2044typename enable_if
2045<
2046 __is_forward_iterator<_ForwardIterator>::value,
2047 void
2048>::type
2049vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2050{
2051 size_type __old_size = this->__size_;
2052 this->__size_ += _STD::distance(__first, __last);
2053 _STD::copy(__first, __last, __make_iter(__old_size));
2054}
2055
2056template <class _Allocator>
2057_LIBCPP_INLINE_VISIBILITY inline
2058vector<bool, _Allocator>::vector()
2059 : __begin_(0),
2060 __size_(0),
2061 __cap_alloc_(0)
2062{
2063}
2064
2065template <class _Allocator>
2066_LIBCPP_INLINE_VISIBILITY inline
2067vector<bool, _Allocator>::vector(const allocator_type& __a)
2068 : __begin_(0),
2069 __size_(0),
2070 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2071{
2072}
2073
2074template <class _Allocator>
2075vector<bool, _Allocator>::vector(size_type __n)
2076 : __begin_(0),
2077 __size_(0),
2078 __cap_alloc_(0)
2079{
2080 if (__n > 0)
2081 {
2082 allocate(__n);
2083 __construct_at_end(__n, false);
2084 }
2085}
2086
2087template <class _Allocator>
2088vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
2089 : __begin_(0),
2090 __size_(0),
2091 __cap_alloc_(0)
2092{
2093 if (__n > 0)
2094 {
2095 allocate(__n);
2096 __construct_at_end(__n, __x);
2097 }
2098}
2099
2100template <class _Allocator>
2101vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
2102 : __begin_(0),
2103 __size_(0),
2104 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2105{
2106 if (__n > 0)
2107 {
2108 allocate(__n);
2109 __construct_at_end(__n, __x);
2110 }
2111}
2112
2113template <class _Allocator>
2114template <class _InputIterator>
2115vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2116 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2117 !__is_forward_iterator<_InputIterator>::value>::type*)
2118 : __begin_(0),
2119 __size_(0),
2120 __cap_alloc_(0)
2121{
2122#ifndef _LIBCPP_NO_EXCEPTIONS
2123 try
2124 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002125#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002126 for (; __first != __last; ++__first)
2127 push_back(*__first);
2128#ifndef _LIBCPP_NO_EXCEPTIONS
2129 }
2130 catch (...)
2131 {
2132 if (__begin_ != 0)
2133 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2134 __invalidate_all_iterators();
2135 throw;
2136 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002137#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002138}
2139
2140template <class _Allocator>
2141template <class _InputIterator>
2142vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2143 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2144 !__is_forward_iterator<_InputIterator>::value>::type*)
2145 : __begin_(0),
2146 __size_(0),
2147 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2148{
2149#ifndef _LIBCPP_NO_EXCEPTIONS
2150 try
2151 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002152#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002153 for (; __first != __last; ++__first)
2154 push_back(*__first);
2155#ifndef _LIBCPP_NO_EXCEPTIONS
2156 }
2157 catch (...)
2158 {
2159 if (__begin_ != 0)
2160 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2161 __invalidate_all_iterators();
2162 throw;
2163 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002164#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002165}
2166
2167template <class _Allocator>
2168template <class _ForwardIterator>
2169vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2170 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2171 : __begin_(0),
2172 __size_(0),
2173 __cap_alloc_(0)
2174{
2175 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
2176 if (__n > 0)
2177 {
2178 allocate(__n);
2179 __construct_at_end(__first, __last);
2180 }
2181}
2182
2183template <class _Allocator>
2184template <class _ForwardIterator>
2185vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2186 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
2187 : __begin_(0),
2188 __size_(0),
2189 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2190{
2191 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
2192 if (__n > 0)
2193 {
2194 allocate(__n);
2195 __construct_at_end(__first, __last);
2196 }
2197}
2198
2199template <class _Allocator>
2200vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
2201 : __begin_(0),
2202 __size_(0),
2203 __cap_alloc_(0)
2204{
2205 size_type __n = static_cast<size_type>(__il.size());
2206 if (__n > 0)
2207 {
2208 allocate(__n);
2209 __construct_at_end(__il.begin(), __il.end());
2210 }
2211}
2212
2213template <class _Allocator>
2214vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
2215 : __begin_(0),
2216 __size_(0),
2217 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2218{
2219 size_type __n = static_cast<size_type>(__il.size());
2220 if (__n > 0)
2221 {
2222 allocate(__n);
2223 __construct_at_end(__il.begin(), __il.end());
2224 }
2225}
2226
2227template <class _Allocator>
2228_LIBCPP_INLINE_VISIBILITY inline
2229vector<bool, _Allocator>::~vector()
2230{
2231 if (__begin_ != 0)
2232 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2233#ifdef _LIBCPP_DEBUG
2234 __invalidate_all_iterators();
2235#endif
2236}
2237
2238template <class _Allocator>
2239vector<bool, _Allocator>::vector(const vector& __v)
2240 : __begin_(0),
2241 __size_(0),
2242 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2243{
2244 if (__v.size() > 0)
2245 {
2246 allocate(__v.size());
2247 __construct_at_end(__v.begin(), __v.end());
2248 }
2249}
2250
2251template <class _Allocator>
2252vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
2253 : __begin_(0),
2254 __size_(0),
2255 __cap_alloc_(0, __a)
2256{
2257 if (__v.size() > 0)
2258 {
2259 allocate(__v.size());
2260 __construct_at_end(__v.begin(), __v.end());
2261 }
2262}
2263
2264template <class _Allocator>
2265vector<bool, _Allocator>&
2266vector<bool, _Allocator>::operator=(const vector& __v)
2267{
2268 if (this != &__v)
2269 {
2270 __copy_assign_alloc(__v);
2271 if (__v.__size_)
2272 {
2273 if (__v.__size_ > capacity())
2274 {
2275 deallocate();
2276 allocate(__v.__size_);
2277 }
2278 _STD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
2279 }
2280 __size_ = __v.__size_;
2281 }
2282 return *this;
2283}
2284
Howard Hinnant73d21a42010-09-04 23:28:19 +00002285#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2286
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002287template <class _Allocator>
2288_LIBCPP_INLINE_VISIBILITY inline
2289vector<bool, _Allocator>::vector(vector&& __v)
2290 : __begin_(__v.__begin_),
2291 __size_(__v.__size_),
2292 __cap_alloc_(__v.__cap_alloc_)
2293{
2294 __v.__begin_ = 0;
2295 __v.__size_ = 0;
2296 __v.__cap() = 0;
2297}
2298
2299template <class _Allocator>
2300vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
2301 : __begin_(0),
2302 __size_(0),
2303 __cap_alloc_(0, __a)
2304{
2305 if (__a == allocator_type(__v.__alloc()))
2306 {
2307 this->__begin_ = __v.__begin_;
2308 this->__size_ = __v.__size_;
2309 this->__cap() = __v.__cap();
2310 __v.__begin_ = nullptr;
2311 __v.__cap() = __v.__size_ = 0;
2312 }
2313 else if (__v.size() > 0)
2314 {
2315 allocate(__v.size());
2316 __construct_at_end(__v.begin(), __v.end());
2317 }
2318}
2319
2320template <class _Allocator>
2321_LIBCPP_INLINE_VISIBILITY inline
2322vector<bool, _Allocator>&
2323vector<bool, _Allocator>::operator=(vector&& __v)
2324{
2325 __move_assign(__v, integral_constant<bool,
2326 __storage_traits::propagate_on_container_move_assignment::value>());
2327}
2328
2329template <class _Allocator>
2330void
2331vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2332{
2333 if (__alloc() != __c.__alloc())
2334 assign(__c.begin(), __c.end());
2335 else
2336 __move_assign(__c, true_type());
2337}
2338
2339template <class _Allocator>
2340void
2341vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
2342{
2343 deallocate();
2344 this->__begin_ = __c.__begin_;
2345 this->__size_ = __c.__size_;
2346 this->__cap() = __c.__cap();
2347 __move_assign_alloc(__c);
2348 __c.__begin_ = nullptr;
2349 __c.__cap() = __c.__size_ = 0;
2350}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002351
2352#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002353
2354template <class _Allocator>
2355void
2356vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2357{
2358 __size_ = 0;
2359 if (__n > 0)
2360 {
2361 size_type __c = capacity();
2362 if (__n <= __c)
2363 __size_ = __n;
2364 else
2365 {
2366 vector __v(__alloc());
2367 __v.reserve(__recommend(__n));
2368 __v.__size_ = __n;
2369 swap(__v);
2370 }
2371 _STD::fill_n(begin(), __n, __x);
2372 }
2373}
2374
2375template <class _Allocator>
2376template <class _InputIterator>
2377typename enable_if
2378<
2379 __is_input_iterator<_InputIterator>::value &&
2380 !__is_forward_iterator<_InputIterator>::value,
2381 void
2382>::type
2383vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2384{
2385 clear();
2386 for (; __first != __last; ++__first)
2387 push_back(*__first);
2388}
2389
2390template <class _Allocator>
2391template <class _ForwardIterator>
2392typename enable_if
2393<
2394 __is_forward_iterator<_ForwardIterator>::value,
2395 void
2396>::type
2397vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2398{
2399 clear();
2400 difference_type __n = _STD::distance(__first, __last);
2401 if (__n)
2402 {
2403 if (__n > capacity())
2404 {
2405 deallocate();
2406 allocate(__n);
2407 }
2408 __construct_at_end(__first, __last);
2409 }
2410}
2411
2412template <class _Allocator>
2413void
2414vector<bool, _Allocator>::reserve(size_type __n)
2415{
2416 if (__n > capacity())
2417 {
2418 vector __v(this->__alloc());
2419 __v.allocate(__n);
2420 __v.__construct_at_end(this->begin(), this->end());
2421 swap(__v);
2422 __invalidate_all_iterators();
2423 }
2424}
2425
2426template <class _Allocator>
2427void
2428vector<bool, _Allocator>::shrink_to_fit()
2429{
2430 if (__external_cap_to_internal(size()) > __cap())
2431 {
2432#ifndef _LIBCPP_NO_EXCEPTIONS
2433 try
2434 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002435#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002436 vector(*this, allocator_type(__alloc())).swap(*this);
2437#ifndef _LIBCPP_NO_EXCEPTIONS
2438 }
2439 catch (...)
2440 {
2441 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002442#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002443 }
2444}
2445
2446template <class _Allocator>
2447typename vector<bool, _Allocator>::reference
2448vector<bool, _Allocator>::at(size_type __n)
2449{
2450 if (__n >= size())
2451 this->__throw_out_of_range();
2452 return (*this)[__n];
2453}
2454
2455template <class _Allocator>
2456typename vector<bool, _Allocator>::const_reference
2457vector<bool, _Allocator>::at(size_type __n) const
2458{
2459 if (__n >= size())
2460 this->__throw_out_of_range();
2461 return (*this)[__n];
2462}
2463
2464template <class _Allocator>
2465void
2466vector<bool, _Allocator>::push_back(const value_type& __x)
2467{
2468 if (this->__size_ == this->capacity())
2469 reserve(__recommend(this->__size_ + 1));
2470 ++this->__size_;
2471 back() = __x;
2472}
2473
2474template <class _Allocator>
2475typename vector<bool, _Allocator>::iterator
2476vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2477{
2478 iterator __r;
2479 if (size() < capacity())
2480 {
2481 const_iterator __old_end = end();
2482 ++__size_;
2483 _STD::copy_backward(__position, __old_end, end());
2484 __r = __const_iterator_cast(__position);
2485 }
2486 else
2487 {
2488 vector __v(__alloc());
2489 __v.reserve(__recommend(__size_ + 1));
2490 __v.__size_ = __size_ + 1;
2491 __r = _STD::copy(cbegin(), __position, __v.begin());
2492 _STD::copy_backward(__position, cend(), __v.end());
2493 swap(__v);
2494 }
2495 *__r = __x;
2496 return __r;
2497}
2498
2499template <class _Allocator>
2500typename vector<bool, _Allocator>::iterator
2501vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
2502{
2503 iterator __r;
2504 size_type __c = capacity();
2505 if (__n <= __c && size() <= __c - __n)
2506 {
2507 const_iterator __old_end = end();
2508 __size_ += __n;
2509 _STD::copy_backward(__position, __old_end, end());
2510 __r = __const_iterator_cast(__position);
2511 }
2512 else
2513 {
2514 vector __v(__alloc());
2515 __v.reserve(__recommend(__size_ + __n));
2516 __v.__size_ = __size_ + __n;
2517 __r = _STD::copy(cbegin(), __position, __v.begin());
2518 _STD::copy_backward(__position, cend(), __v.end());
2519 swap(__v);
2520 }
2521 _STD::fill_n(__r, __n, __x);
2522 return __r;
2523}
2524
2525template <class _Allocator>
2526template <class _InputIterator>
2527typename enable_if
2528<
2529 __is_input_iterator <_InputIterator>::value &&
2530 !__is_forward_iterator<_InputIterator>::value,
2531 typename vector<bool, _Allocator>::iterator
2532>::type
2533vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
2534{
2535 difference_type __off = __position - begin();
2536 iterator __p = __const_iterator_cast(__position);
2537 iterator __old_end = end();
2538 for (; size() != capacity() && __first != __last; ++__first)
2539 {
2540 ++this->__size_;
2541 back() = *__first;
2542 }
2543 vector __v(__alloc());
2544 if (__first != __last)
2545 {
2546#ifndef _LIBCPP_NO_EXCEPTIONS
2547 try
2548 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002549#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002550 __v.assign(__first, __last);
2551 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
2552 difference_type __old_p = __p - begin();
2553 reserve(__recommend(size() + __v.size()));
2554 __p = begin() + __old_p;
2555 __old_end = begin() + __old_size;
2556#ifndef _LIBCPP_NO_EXCEPTIONS
2557 }
2558 catch (...)
2559 {
2560 erase(__old_end, end());
2561 throw;
2562 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002563#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002564 }
2565 __p = _STD::rotate(__p, __old_end, end());
2566 insert(__p, __v.begin(), __v.end());
2567 return begin() + __off;
2568}
2569
2570template <class _Allocator>
2571template <class _ForwardIterator>
2572typename enable_if
2573<
2574 __is_forward_iterator<_ForwardIterator>::value,
2575 typename vector<bool, _Allocator>::iterator
2576>::type
2577vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
2578{
2579 difference_type __n = _STD::distance(__first, __last);
2580 iterator __r;
2581 size_type __c = capacity();
2582 if (__n <= __c && size() <= __c - __n)
2583 {
2584 const_iterator __old_end = end();
2585 __size_ += __n;
2586 _STD::copy_backward(__position, __old_end, end());
2587 __r = __const_iterator_cast(__position);
2588 }
2589 else
2590 {
2591 vector __v(__alloc());
2592 __v.reserve(__recommend(__size_ + __n));
2593 __v.__size_ = __size_ + __n;
2594 __r = _STD::copy(cbegin(), __position, __v.begin());
2595 _STD::copy_backward(__position, cend(), __v.end());
2596 swap(__v);
2597 }
2598 _STD::copy(__first, __last, __r);
2599 return __r;
2600}
2601
2602template <class _Allocator>
2603_LIBCPP_INLINE_VISIBILITY inline
2604typename vector<bool, _Allocator>::iterator
2605vector<bool, _Allocator>::erase(const_iterator __position)
2606{
2607 iterator __r = __const_iterator_cast(__position);
2608 _STD::copy(__position + 1, this->cend(), __r);
2609 --__size_;
2610 return __r;
2611}
2612
2613template <class _Allocator>
2614typename vector<bool, _Allocator>::iterator
2615vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
2616{
2617 iterator __r = __const_iterator_cast(__first);
2618 difference_type __d = __last - __first;
2619 _STD::copy(__last, this->cend(), __r);
2620 __size_ -= __d;
2621 return __r;
2622}
2623
2624template <class _Allocator>
2625void
2626vector<bool, _Allocator>::swap(vector& __x)
2627{
2628 _STD::swap(this->__begin_, __x.__begin_);
2629 _STD::swap(this->__size_, __x.__size_);
2630 _STD::swap(this->__cap(), __x.__cap());
2631 __swap_alloc(this->__alloc(), __x.__alloc());
2632#ifdef _LIBCPP_DEBUG
2633 iterator::swap(this, &__x);
2634 const_iterator::swap(this, &__x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002635#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002636}
2637
Howard Hinnant324bb032010-08-22 00:02:43 +00002638template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002639void
2640vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
2641{
2642 size_type __cs = size();
2643 if (__cs < __sz)
2644 {
2645 iterator __r;
2646 size_type __c = capacity();
2647 size_type __n = __sz - __cs;
2648 if (__n <= __c && __cs <= __c - __n)
2649 {
2650 __r = end();
2651 __size_ += __n;
2652 }
2653 else
2654 {
2655 vector __v(__alloc());
2656 __v.reserve(__recommend(__size_ + __n));
2657 __v.__size_ = __size_ + __n;
2658 __r = _STD::copy(cbegin(), cend(), __v.begin());
2659 swap(__v);
2660 }
2661 _STD::fill_n(__r, __n, __x);
2662 }
2663 else
2664 __size_ = __sz;
2665}
2666
Howard Hinnant324bb032010-08-22 00:02:43 +00002667template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002668void
2669vector<bool, _Allocator>::flip()
2670{
2671 // do middle whole words
2672 size_type __n = __size_;
2673 __storage_pointer __p = __begin_;
2674 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
2675 *__p = ~*__p;
2676 // do last partial word
2677 if (__n > 0)
2678 {
2679 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
2680 __storage_type __b = *__p & __m;
2681 *__p &= ~__m;
2682 *__p |= ~__b & __m;
2683 }
2684}
2685
Howard Hinnant324bb032010-08-22 00:02:43 +00002686template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002687bool
2688vector<bool, _Allocator>::__invariants() const
2689{
2690 if (this->__begin_ == 0)
2691 {
2692 if (this->__size_ != 0 || this->__cap() != 0)
2693 return false;
2694 }
2695 else
2696 {
2697 if (this->__cap() == 0)
2698 return false;
2699 if (this->__size_ > this->capacity())
2700 return false;
2701 }
2702 return true;
2703}
2704
Howard Hinnant324bb032010-08-22 00:02:43 +00002705template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002706size_t
2707vector<bool, _Allocator>::__hash_code() const
2708{
2709 size_t __h = 0;
2710 // do middle whole words
2711 size_type __n = __size_;
2712 __storage_pointer __p = __begin_;
2713 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
2714 __h ^= *__p;
2715 // do last partial word
2716 if (__n > 0)
2717 {
2718 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
2719 __h ^= *__p & __m;
2720 }
2721 return __h;
2722}
2723
2724template <class _Allocator>
2725struct hash<vector<bool, _Allocator> >
2726 : public unary_function<vector<bool, _Allocator>, size_t>
2727{
2728 size_t operator()(const vector<bool, _Allocator>& __vec) const
2729 {return __vec.__hash_code();}
2730};
2731
2732template <class _Tp, class _Allocator>
2733struct __is_zero_default_constructible<vector<_Tp, _Allocator> >
2734 : public integral_constant<bool, __is_zero_default_constructible<_Allocator>::value> {};
2735
2736template <class _Tp, class _Allocator>
2737_LIBCPP_INLINE_VISIBILITY inline
2738bool
2739operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2740{
2741 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
2742 return __sz == __y.size() && _STD::equal(__x.begin(), __x.end(), __y.begin());
2743}
2744
2745template <class _Tp, class _Allocator>
2746_LIBCPP_INLINE_VISIBILITY inline
2747bool
2748operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2749{
2750 return !(__x == __y);
2751}
2752
2753template <class _Tp, class _Allocator>
2754_LIBCPP_INLINE_VISIBILITY inline
2755bool
2756operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2757{
2758 return _STD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
2759}
2760
2761template <class _Tp, class _Allocator>
2762_LIBCPP_INLINE_VISIBILITY inline
2763bool
2764operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2765{
2766 return __y < __x;
2767}
2768
2769template <class _Tp, class _Allocator>
2770_LIBCPP_INLINE_VISIBILITY inline
2771bool
2772operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2773{
2774 return !(__x < __y);
2775}
2776
2777template <class _Tp, class _Allocator>
2778_LIBCPP_INLINE_VISIBILITY inline
2779bool
2780operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
2781{
2782 return !(__y < __x);
2783}
2784
2785template <class _Tp, class _Allocator>
2786_LIBCPP_INLINE_VISIBILITY inline
2787void
2788swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
2789{
2790 __x.swap(__y);
2791}
2792
2793_LIBCPP_END_NAMESPACE_STD
2794
2795#endif // _LIBCPP_VECTOR