1 // Debugging vector implementation -*- C++ -*-
3 // Copyright (C) 2003-2013 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file debug/vector
26 * This file is a GNU debug extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_DEBUG_VECTOR
30 #define _GLIBCXX_DEBUG_VECTOR 1
34 #include <debug/safe_sequence.h>
35 #include <debug/safe_iterator.h>
37 namespace std _GLIBCXX_VISIBILITY(default)
41 /// 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>,
46 public __gnu_debug::_Safe_sequence<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;
52 typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
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;
62 typedef __gnu_debug::_Safe_iterator<_Base_iterator,vector>
64 typedef __gnu_debug::_Safe_iterator<_Base_const_iterator,vector>
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;
77 // 23.2.4.1 construct/copy/destroy:
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())
105 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
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()) { }
114 /// Construction from a release-mode vector
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())
159 __glibcxx_check_self_move_assign(__x);
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)
187 __glibcxx_check_valid_range(__first, __last);
188 _Base::assign(__gnu_debug::__base(__first),
189 __gnu_debug::__base(__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()); }
265 // 23.2.4.2 capacity:
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)
346 __glibcxx_check_subscript(__n);
347 return _M_base()[__n];
351 operator[](size_type __n) const
353 __glibcxx_check_subscript(__n);
354 return _M_base()[__n];
362 __glibcxx_check_nonempty();
363 return _Base::front();
369 __glibcxx_check_nonempty();
370 return _Base::front();
376 __glibcxx_check_nonempty();
377 return _Base::back();
383 __glibcxx_check_nonempty();
384 return _Base::back();
387 // _GLIBCXX_RESOLVE_LIB_DEFECTS
388 // DR 464. Suggestion for new member functions in standard containers.
391 // 23.2.4.3 modifiers:
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();
424 __glibcxx_check_nonempty();
425 this->_M_invalidate_if(_Equal(--_Base::end()));
429 #if __cplusplus >= 201103L
430 template<typename... _Args>
432 emplace(const_iterator __position, _Args&&... __args)
434 __glibcxx_check_insert(__position);
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 #if __cplusplus >= 201103L
450 insert(const_iterator __position, const _Tp& __x)
452 insert(iterator __position, const _Tp& __x)
455 __glibcxx_check_insert(__position);
456 bool __realloc = _M_requires_reallocation(this->size() + 1);
457 difference_type __offset = __position.base() - _Base::begin();
458 _Base_iterator __res = _Base::insert(__position.base(), __x);
460 this->_M_invalidate_all();
462 this->_M_invalidate_after_nth(__offset);
463 _M_update_guaranteed_capacity();
464 return iterator(__res, this);
467 #if __cplusplus >= 201103L
468 template<typename _Up = _Tp>
469 typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value,
471 insert(const_iterator __position, _Tp&& __x)
472 { return emplace(__position, std::move(__x)); }
475 insert(const_iterator __position, initializer_list<value_type> __l)
476 { return this->insert(__position, __l.begin(), __l.end()); }
479 #if __cplusplus >= 201103L
481 insert(const_iterator __position, size_type __n, const _Tp& __x)
483 __glibcxx_check_insert(__position);
484 bool __realloc = _M_requires_reallocation(this->size() + __n);
485 difference_type __offset = __position.base() - _Base::cbegin();
486 _Base_iterator __res = _Base::insert(__position.base(), __n, __x);
488 this->_M_invalidate_all();
490 this->_M_invalidate_after_nth(__offset);
491 _M_update_guaranteed_capacity();
492 return iterator(__res, this);
496 insert(iterator __position, size_type __n, const _Tp& __x)
498 __glibcxx_check_insert(__position);
499 bool __realloc = _M_requires_reallocation(this->size() + __n);
500 difference_type __offset = __position.base() - _Base::begin();
501 _Base::insert(__position.base(), __n, __x);
503 this->_M_invalidate_all();
505 this->_M_invalidate_after_nth(__offset);
506 _M_update_guaranteed_capacity();
510 #if __cplusplus >= 201103L
511 template<class _InputIterator,
512 typename = std::_RequireInputIter<_InputIterator>>
514 insert(const_iterator __position,
515 _InputIterator __first, _InputIterator __last)
517 __glibcxx_check_insert_range(__position, __first, __last);
519 /* Hard to guess if invalidation will occur, because __last
520 - __first can't be calculated in all cases, so we just
521 punt here by checking if it did occur. */
522 _Base_iterator __old_begin = _M_base().begin();
523 difference_type __offset = __position.base() - _Base::cbegin();
524 _Base_iterator __res = _Base::insert(__position.base(),
525 __gnu_debug::__base(__first),
526 __gnu_debug::__base(__last));
528 if (_M_base().begin() != __old_begin)
529 this->_M_invalidate_all();
531 this->_M_invalidate_after_nth(__offset);
532 _M_update_guaranteed_capacity();
533 return iterator(__res, this);
536 template<class _InputIterator>
538 insert(iterator __position,
539 _InputIterator __first, _InputIterator __last)
541 __glibcxx_check_insert_range(__position, __first, __last);
543 /* Hard to guess if invalidation will occur, because __last
544 - __first can't be calculated in all cases, so we just
545 punt here by checking if it did occur. */
546 _Base_iterator __old_begin = _M_base().begin();
547 difference_type __offset = __position.base() - _Base::begin();
548 _Base::insert(__position.base(), __gnu_debug::__base(__first),
549 __gnu_debug::__base(__last));
551 if (_M_base().begin() != __old_begin)
552 this->_M_invalidate_all();
554 this->_M_invalidate_after_nth(__offset);
555 _M_update_guaranteed_capacity();
560 #if __cplusplus >= 201103L
561 erase(const_iterator __position)
563 erase(iterator __position)
566 __glibcxx_check_erase(__position);
567 difference_type __offset = __position.base() - _Base::begin();
568 _Base_iterator __res = _Base::erase(__position.base());
569 this->_M_invalidate_after_nth(__offset);
570 return iterator(__res, this);
574 #if __cplusplus >= 201103L
575 erase(const_iterator __first, const_iterator __last)
577 erase(iterator __first, iterator __last)
580 // _GLIBCXX_RESOLVE_LIB_DEFECTS
581 // 151. can't currently clear() empty container
582 __glibcxx_check_erase_range(__first, __last);
584 if (__first.base() != __last.base())
586 difference_type __offset = __first.base() - _Base::begin();
587 _Base_iterator __res = _Base::erase(__first.base(),
589 this->_M_invalidate_after_nth(__offset);
590 return iterator(__res, this);
593 #if __cplusplus >= 201103L
594 return iterator(__first.base()._M_const_cast(), this);
602 #if __cplusplus >= 201103L
603 noexcept(_Alloc_traits::_S_nothrow_swap())
606 #if __cplusplus >= 201103L
607 if (!_Alloc_traits::_S_propagate_on_swap())
608 __glibcxx_check_equal_allocs(__x);
612 std::swap(_M_guaranteed_capacity, __x._M_guaranteed_capacity);
616 clear() _GLIBCXX_NOEXCEPT
619 this->_M_invalidate_all();
620 _M_guaranteed_capacity = 0;
624 _M_base() _GLIBCXX_NOEXCEPT { return *this; }
627 _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
630 size_type _M_guaranteed_capacity;
633 _M_requires_reallocation(size_type __elements)
634 { return __elements > this->capacity(); }
637 _M_update_guaranteed_capacity()
639 if (this->size() > _M_guaranteed_capacity)
640 _M_guaranteed_capacity = this->size();
644 _M_invalidate_after_nth(difference_type __n)
646 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
647 this->_M_invalidate_if(_After_nth(__n, _Base::begin()));
651 template<typename _Tp, typename _Alloc>
653 operator==(const vector<_Tp, _Alloc>& __lhs,
654 const vector<_Tp, _Alloc>& __rhs)
655 { return __lhs._M_base() == __rhs._M_base(); }
657 template<typename _Tp, typename _Alloc>
659 operator!=(const vector<_Tp, _Alloc>& __lhs,
660 const vector<_Tp, _Alloc>& __rhs)
661 { return __lhs._M_base() != __rhs._M_base(); }
663 template<typename _Tp, typename _Alloc>
665 operator<(const vector<_Tp, _Alloc>& __lhs,
666 const vector<_Tp, _Alloc>& __rhs)
667 { return __lhs._M_base() < __rhs._M_base(); }
669 template<typename _Tp, typename _Alloc>
671 operator<=(const vector<_Tp, _Alloc>& __lhs,
672 const vector<_Tp, _Alloc>& __rhs)
673 { return __lhs._M_base() <= __rhs._M_base(); }
675 template<typename _Tp, typename _Alloc>
677 operator>=(const vector<_Tp, _Alloc>& __lhs,
678 const vector<_Tp, _Alloc>& __rhs)
679 { return __lhs._M_base() >= __rhs._M_base(); }
681 template<typename _Tp, typename _Alloc>
683 operator>(const vector<_Tp, _Alloc>& __lhs,
684 const vector<_Tp, _Alloc>& __rhs)
685 { return __lhs._M_base() > __rhs._M_base(); }
687 template<typename _Tp, typename _Alloc>
689 swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>& __rhs)
690 { __lhs.swap(__rhs); }
692 } // namespace __debug
694 #if __cplusplus >= 201103L
696 /// std::hash specialization for vector<bool>.
697 template<typename _Alloc>
698 struct hash<__debug::vector<bool, _Alloc>>
699 : public __hash_base<size_t, __debug::vector<bool, _Alloc>>
702 operator()(const __debug::vector<bool, _Alloc>& __b) const noexcept
703 { return std::hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>()