Class std::vector with safety/checking/debug instrumentation.
42 template<
typename _Tp,
43 typename _Allocator = std::allocator<_Tp> >
45 :
public _GLIBCXX_STD_C::vector<_Tp, _Allocator>,
48 typedef _GLIBCXX_STD_C::vector<_Tp, _Allocator> _Base;
50 typedef typename _Base::iterator _Base_iterator;
51 typedef typename _Base::const_iterator _Base_const_iterator;
54 #if __cplusplus >= 201103L
55 typedef __gnu_cxx::__alloc_traits<_Allocator> _Alloc_traits;
59 typedef typename _Base::reference reference;
60 typedef typename _Base::const_reference const_reference;
67 typedef typename _Base::size_type size_type;
68 typedef typename _Base::difference_type difference_type;
70 typedef _Tp value_type;
71 typedef _Allocator allocator_type;
72 typedef typename _Base::pointer pointer;
73 typedef typename _Base::const_pointer const_pointer;
74 typedef std::reverse_iterator<iterator> reverse_iterator;
75 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
79 vector(
const _Allocator& __a = _Allocator())
80 : _Base(__a), _M_guaranteed_capacity(0) { }
82 #if __cplusplus >= 201103L
84 vector(size_type __n,
const _Allocator& __a = _Allocator())
85 : _Base(__n, __a), _M_guaranteed_capacity(__n) { }
87 vector(size_type __n,
const _Tp& __value,
88 const _Allocator& __a = _Allocator())
89 : _Base(__n, __value, __a), _M_guaranteed_capacity(__n) { }
92 vector(size_type __n,
const _Tp& __value = _Tp(),
93 const _Allocator& __a = _Allocator())
94 : _Base(__n, __value, __a), _M_guaranteed_capacity(__n) { }
97 #if __cplusplus >= 201103L
98 template<
class _InputIterator,
99 typename = std::_RequireInputIter<_InputIterator>>
101 template<
class _InputIterator>
103 vector(_InputIterator __first, _InputIterator __last,
104 const _Allocator& __a = _Allocator())
107 __gnu_debug::
__base(__last), __a),
108 _M_guaranteed_capacity(0)
109 { _M_update_guaranteed_capacity(); }
111 vector(
const vector& __x)
112 : _Base(__x), _M_guaranteed_capacity(__x.size()) { }
115 vector(
const _Base& __x)
116 : _Base(__x), _M_guaranteed_capacity(__x.size()) { }
118 #if __cplusplus >= 201103L
119 vector(vector&& __x) noexcept
120 : _Base(std::move(__x)),
121 _M_guaranteed_capacity(this->size())
124 __x._M_guaranteed_capacity = 0;
127 vector(
const vector& __x,
const allocator_type& __a)
128 : _Base(__x, __a), _M_guaranteed_capacity(__x.size()) { }
130 vector(vector&& __x,
const allocator_type& __a)
131 : _Base(std::move(__x), __a),
132 _M_guaranteed_capacity(this->size())
134 __x._M_invalidate_all();
135 __x._M_guaranteed_capacity = 0;
138 vector(initializer_list<value_type> __l,
139 const allocator_type& __a = allocator_type())
141 _M_guaranteed_capacity(__l.size()) { }
144 ~vector() _GLIBCXX_NOEXCEPT { }
147 operator=(
const vector& __x)
149 static_cast<_Base&
>(*this) = __x;
150 this->_M_invalidate_all();
151 _M_update_guaranteed_capacity();
155 #if __cplusplus >= 201103L
157 operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
160 _Base::operator=(std::move(__x));
161 this->_M_invalidate_all();
162 _M_update_guaranteed_capacity();
163 __x._M_invalidate_all();
164 __x._M_guaranteed_capacity = 0;
169 operator=(initializer_list<value_type> __l)
171 static_cast<_Base&
>(*this) = __l;
172 this->_M_invalidate_all();
173 _M_update_guaranteed_capacity();
178 #if __cplusplus >= 201103L
179 template<
typename _InputIterator,
180 typename = std::_RequireInputIter<_InputIterator>>
182 template<
typename _InputIterator>
185 assign(_InputIterator __first, _InputIterator __last)
190 this->_M_invalidate_all();
191 _M_update_guaranteed_capacity();
195 assign(size_type __n,
const _Tp& __u)
197 _Base::assign(__n, __u);
198 this->_M_invalidate_all();
199 _M_update_guaranteed_capacity();
202 #if __cplusplus >= 201103L
204 assign(initializer_list<value_type> __l)
207 this->_M_invalidate_all();
208 _M_update_guaranteed_capacity();
212 using _Base::get_allocator;
216 begin() _GLIBCXX_NOEXCEPT
217 {
return iterator(_Base::begin(),
this); }
220 begin() const _GLIBCXX_NOEXCEPT
221 {
return const_iterator(_Base::begin(),
this); }
224 end() _GLIBCXX_NOEXCEPT
225 {
return iterator(_Base::end(),
this); }
228 end() const _GLIBCXX_NOEXCEPT
229 {
return const_iterator(_Base::end(),
this); }
232 rbegin() _GLIBCXX_NOEXCEPT
233 {
return reverse_iterator(end()); }
235 const_reverse_iterator
236 rbegin() const _GLIBCXX_NOEXCEPT
237 {
return const_reverse_iterator(end()); }
240 rend() _GLIBCXX_NOEXCEPT
241 {
return reverse_iterator(begin()); }
243 const_reverse_iterator
244 rend() const _GLIBCXX_NOEXCEPT
245 {
return const_reverse_iterator(begin()); }
247 #if __cplusplus >= 201103L
249 cbegin() const noexcept
250 {
return const_iterator(_Base::begin(),
this); }
253 cend() const noexcept
254 {
return const_iterator(_Base::end(),
this); }
256 const_reverse_iterator
257 crbegin() const noexcept
258 {
return const_reverse_iterator(end()); }
260 const_reverse_iterator
261 crend() const noexcept
262 {
return const_reverse_iterator(begin()); }
267 using _Base::max_size;
269 #if __cplusplus >= 201103L
271 resize(size_type __sz)
273 bool __realloc = _M_requires_reallocation(__sz);
274 if (__sz < this->size())
275 this->_M_invalidate_after_nth(__sz);
278 this->_M_invalidate_all();
279 _M_update_guaranteed_capacity();
283 resize(size_type __sz,
const _Tp& __c)
285 bool __realloc = _M_requires_reallocation(__sz);
286 if (__sz < this->size())
287 this->_M_invalidate_after_nth(__sz);
288 _Base::resize(__sz, __c);
290 this->_M_invalidate_all();
291 _M_update_guaranteed_capacity();
295 resize(size_type __sz, _Tp __c = _Tp())
297 bool __realloc = _M_requires_reallocation(__sz);
298 if (__sz < this->size())
299 this->_M_invalidate_after_nth(__sz);
300 _Base::resize(__sz, __c);
302 this->_M_invalidate_all();
303 _M_update_guaranteed_capacity();
307 #if __cplusplus >= 201103L
311 if (_Base::_M_shrink_to_fit())
313 _M_guaranteed_capacity = _Base::capacity();
314 this->_M_invalidate_all();
320 capacity() const _GLIBCXX_NOEXCEPT
322 #ifdef _GLIBCXX_DEBUG_PEDANTIC
323 return _M_guaranteed_capacity;
325 return _Base::capacity();
332 reserve(size_type __n)
334 bool __realloc = _M_requires_reallocation(__n);
336 if (__n > _M_guaranteed_capacity)
337 _M_guaranteed_capacity = __n;
339 this->_M_invalidate_all();
344 operator[](size_type __n)
347 return _M_base()[__n];
351 operator[](size_type __n)
const
354 return _M_base()[__n];
363 return _Base::front();
370 return _Base::front();
377 return _Base::back();
384 return _Base::back();
393 push_back(
const _Tp& __x)
395 bool __realloc = _M_requires_reallocation(this->size() + 1);
396 _Base::push_back(__x);
398 this->_M_invalidate_all();
399 _M_update_guaranteed_capacity();
402 #if __cplusplus >= 201103L
403 template<
typename _Up = _Tp>
404 typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value,
407 { emplace_back(std::move(__x)); }
409 template<
typename... _Args>
411 emplace_back(_Args&&... __args)
413 bool __realloc = _M_requires_reallocation(this->size() + 1);
414 _Base::emplace_back(std::forward<_Args>(__args)...);
416 this->_M_invalidate_all();
417 _M_update_guaranteed_capacity();
425 this->_M_invalidate_if(_Equal(--_Base::end()));
429 #if __cplusplus >= 201103L
430 template<
typename... _Args>
432 emplace(iterator __position, _Args&&... __args)
435 bool __realloc = _M_requires_reallocation(this->size() + 1);
436 difference_type __offset = __position.base() - _Base::begin();
437 _Base_iterator __res = _Base::emplace(__position.base(),
438 std::forward<_Args>(__args)...);
440 this->_M_invalidate_all();
442 this->_M_invalidate_after_nth(__offset);
443 _M_update_guaranteed_capacity();
444 return iterator(__res,
this);
449 insert(iterator __position,
const _Tp& __x)
452 bool __realloc = _M_requires_reallocation(this->size() + 1);
453 difference_type __offset = __position.base() - _Base::begin();
454 _Base_iterator __res = _Base::insert(__position.base(), __x);
456 this->_M_invalidate_all();
458 this->_M_invalidate_after_nth(__offset);
459 _M_update_guaranteed_capacity();
460 return iterator(__res,
this);
463 #if __cplusplus >= 201103L
464 template<
typename _Up = _Tp>
465 typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value,
467 insert(iterator __position, _Tp&& __x)
468 {
return emplace(__position, std::move(__x)); }
471 insert(iterator __position, initializer_list<value_type> __l)
472 { this->insert(__position, __l.begin(), __l.end()); }
476 insert(iterator __position, size_type __n,
const _Tp& __x)
479 bool __realloc = _M_requires_reallocation(this->size() + __n);
480 difference_type __offset = __position.base() - _Base::begin();
481 _Base::insert(__position.base(), __n, __x);
483 this->_M_invalidate_all();
485 this->_M_invalidate_after_nth(__offset);
486 _M_update_guaranteed_capacity();
489 #if __cplusplus >= 201103L
490 template<
class _InputIterator,
491 typename = std::_RequireInputIter<_InputIterator>>
493 template<
class _InputIterator>
496 insert(iterator __position,
497 _InputIterator __first, _InputIterator __last)
504 _Base_iterator __old_begin = _M_base().begin();
505 difference_type __offset = __position.base() - _Base::begin();
509 if (_M_base().begin() != __old_begin)
510 this->_M_invalidate_all();
512 this->_M_invalidate_after_nth(__offset);
513 _M_update_guaranteed_capacity();
517 erase(iterator __position)
520 difference_type __offset = __position.base() - _Base::begin();
521 _Base_iterator __res = _Base::erase(__position.base());
522 this->_M_invalidate_after_nth(__offset);
523 return iterator(__res,
this);
527 erase(iterator __first, iterator __last)
533 if (__first.base() != __last.base())
535 difference_type __offset = __first.base() - _Base::begin();
536 _Base_iterator __res = _Base::erase(__first.base(),
538 this->_M_invalidate_after_nth(__offset);
539 return iterator(__res,
this);
547 #if __cplusplus >= 201103L
548 noexcept(_Alloc_traits::_S_nothrow_swap())
551 #if __cplusplus >= 201103L
552 if (!_Alloc_traits::_S_propagate_on_swap())
557 std::swap(_M_guaranteed_capacity, __x._M_guaranteed_capacity);
561 clear() _GLIBCXX_NOEXCEPT
564 this->_M_invalidate_all();
565 _M_guaranteed_capacity = 0;
569 _M_base() _GLIBCXX_NOEXCEPT {
return *
this; }
572 _M_base() const _GLIBCXX_NOEXCEPT {
return *
this; }
575 size_type _M_guaranteed_capacity;
578 _M_requires_reallocation(size_type __elements)
579 {
return __elements > this->capacity(); }
582 _M_update_guaranteed_capacity()
584 if (this->size() > _M_guaranteed_capacity)
585 _M_guaranteed_capacity = this->size();
589 _M_invalidate_after_nth(difference_type __n)
592 this->_M_invalidate_if(_After_nth(__n, _Base::begin()));
596 template<
typename _Tp,
typename _Alloc>
599 const vector<_Tp, _Alloc>& __rhs)
600 {
return __lhs._M_base() == __rhs._M_base(); }
602 template<
typename _Tp,
typename _Alloc>
605 const vector<_Tp, _Alloc>& __rhs)
606 {
return __lhs._M_base() != __rhs._M_base(); }
608 template<
typename _Tp,
typename _Alloc>
610 operator<(const vector<_Tp, _Alloc>& __lhs,
611 const vector<_Tp, _Alloc>& __rhs)
612 {
return __lhs._M_base() < __rhs._M_base(); }
614 template<
typename _Tp,
typename _Alloc>
616 operator<=(const vector<_Tp, _Alloc>& __lhs,
617 const vector<_Tp, _Alloc>& __rhs)
618 {
return __lhs._M_base() <= __rhs._M_base(); }
620 template<
typename _Tp,
typename _Alloc>
623 const vector<_Tp, _Alloc>& __rhs)
624 {
return __lhs._M_base() >= __rhs._M_base(); }
626 template<
typename _Tp,
typename _Alloc>
628 operator>(
const vector<_Tp, _Alloc>& __lhs,
629 const vector<_Tp, _Alloc>& __rhs)
630 {
return __lhs._M_base() > __rhs._M_base(); }
632 template<
typename _Tp,
typename _Alloc>
634 swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>& __rhs)
635 { __lhs.swap(__rhs); }
639 #if __cplusplus >= 201103L
642 template<
typename _Alloc>
643 struct hash<__debug::vector<bool, _Alloc>>
644 :
public __hash_base<size_t, __debug::vector<bool, _Alloc>>
647 operator()(
const __debug::vector<bool, _Alloc>& __b)
const noexcept
648 {
return std::hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>()
bool operator>=(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:644
Definition: safe_sequence.h:62
_Siter_base< _Iterator >::iterator_type __base(_Iterator __it)
Definition: functions.h:446
bool operator==(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
Definition: safe_sequence.h:77
#define __glibcxx_check_erase(_Position)
Definition: macros.h:135
#define __glibcxx_check_insert(_Position)
Definition: macros.h:73
#define __glibcxx_check_subscript(_N)
Definition: macros.h:202
#define __glibcxx_check_valid_range(_First, _Last)
Definition: macros.h:53
_InputIterator __check_valid_range(const _InputIterator &__first, const _InputIterator &__last __attribute__((__unused__)))
Definition: functions.h:157
#define __glibcxx_check_insert_range(_Position, _First, _Last)
Definition: macros.h:110
bool operator>(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:612
#define __glibcxx_check_self_move_assign(_Other)
Definition: macros.h:328
bool operator!=(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
Base class for constructing a safe sequence type that tracks iterators that reference it...
Definition: formatter.h:52
#define __glibcxx_check_erase_range(_First, _Last)
Definition: macros.h:163
#define __glibcxx_check_nonempty()
Definition: macros.h:218
#define __glibcxx_check_equal_allocs(_Other)
Definition: macros.h:339
Safe iterator wrapper.
Definition: formatter.h:46
void swap(exception_ptr &__lhs, exception_ptr &__rhs)
Definition: exception_ptr.h:160