1 // Debugging vector implementation -*- C++ -*-
3 // Copyright (C) 2003-2019 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
32 #pragma GCC system_header
34 #include <bits/c++config.h>
35 namespace std _GLIBCXX_VISIBILITY(default) { namespace __debug {
36 template<typename _Tp, typename _Allocator> class vector;
37 } } // namespace std::__debug
41 #include <debug/safe_sequence.h>
42 #include <debug/safe_container.h>
43 #include <debug/safe_iterator.h>
47 /** @brief Base class for Debug Mode vector.
49 * Adds information about the guaranteed capacity, which is useful for
50 * detecting code which relies on non-portable implementation details of
51 * the libstdc++ reallocation policy.
53 template<typename _SafeSequence,
54 typename _BaseSequence>
57 typedef typename _BaseSequence::size_type size_type;
60 _M_seq() const { return *static_cast<const _SafeSequence*>(this); }
63 _Safe_vector() _GLIBCXX_NOEXCEPT
64 : _M_guaranteed_capacity(0)
65 { _M_update_guaranteed_capacity(); }
67 _Safe_vector(const _Safe_vector&) _GLIBCXX_NOEXCEPT
68 : _M_guaranteed_capacity(0)
69 { _M_update_guaranteed_capacity(); }
71 _Safe_vector(size_type __n) _GLIBCXX_NOEXCEPT
72 : _M_guaranteed_capacity(__n)
75 #if __cplusplus >= 201103L
76 _Safe_vector(_Safe_vector&& __x) noexcept
78 { __x._M_guaranteed_capacity = 0; }
81 operator=(const _Safe_vector&) noexcept
83 _M_update_guaranteed_capacity();
88 operator=(_Safe_vector&& __x) noexcept
90 _M_update_guaranteed_capacity();
91 __x._M_guaranteed_capacity = 0;
96 size_type _M_guaranteed_capacity;
99 _M_requires_reallocation(size_type __elements) const _GLIBCXX_NOEXCEPT
100 { return __elements > _M_seq().capacity(); }
103 _M_update_guaranteed_capacity() _GLIBCXX_NOEXCEPT
105 if (_M_seq().size() > _M_guaranteed_capacity)
106 _M_guaranteed_capacity = _M_seq().size();
111 namespace std _GLIBCXX_VISIBILITY(default)
115 /// Class std::vector with safety/checking/debug instrumentation.
116 template<typename _Tp,
117 typename _Allocator = std::allocator<_Tp> >
119 : public __gnu_debug::_Safe_container<
120 vector<_Tp, _Allocator>, _Allocator, __gnu_debug::_Safe_sequence>,
121 public _GLIBCXX_STD_C::vector<_Tp, _Allocator>,
122 public __gnu_debug::_Safe_vector<
123 vector<_Tp, _Allocator>,
124 _GLIBCXX_STD_C::vector<_Tp, _Allocator> >
126 typedef _GLIBCXX_STD_C::vector<_Tp, _Allocator> _Base;
127 typedef __gnu_debug::_Safe_container<
128 vector, _Allocator, __gnu_debug::_Safe_sequence> _Safe;
129 typedef __gnu_debug::_Safe_vector<vector, _Base> _Safe_vector;
131 typedef typename _Base::iterator _Base_iterator;
132 typedef typename _Base::const_iterator _Base_const_iterator;
133 typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
135 template<typename _ItT, typename _SeqT, typename _CatT>
136 friend class ::__gnu_debug::_Safe_iterator;
139 typedef typename _Base::reference reference;
140 typedef typename _Base::const_reference const_reference;
142 typedef __gnu_debug::_Safe_iterator<
143 _Base_iterator, vector> iterator;
144 typedef __gnu_debug::_Safe_iterator<
145 _Base_const_iterator, vector> const_iterator;
147 typedef typename _Base::size_type size_type;
148 typedef typename _Base::difference_type difference_type;
150 typedef _Tp value_type;
151 typedef _Allocator allocator_type;
152 typedef typename _Base::pointer pointer;
153 typedef typename _Base::const_pointer const_pointer;
154 typedef std::reverse_iterator<iterator> reverse_iterator;
155 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
157 // 23.2.4.1 construct/copy/destroy:
159 #if __cplusplus < 201103L
160 vector() _GLIBCXX_NOEXCEPT
167 vector(const _Allocator& __a) _GLIBCXX_NOEXCEPT
170 #if __cplusplus >= 201103L
172 vector(size_type __n, const _Allocator& __a = _Allocator())
173 : _Base(__n, __a), _Safe_vector(__n) { }
175 vector(size_type __n, const _Tp& __value,
176 const _Allocator& __a = _Allocator())
177 : _Base(__n, __value, __a) { }
180 vector(size_type __n, const _Tp& __value = _Tp(),
181 const _Allocator& __a = _Allocator())
182 : _Base(__n, __value, __a) { }
185 #if __cplusplus >= 201103L
186 template<class _InputIterator,
187 typename = std::_RequireInputIter<_InputIterator>>
189 template<class _InputIterator>
191 vector(_InputIterator __first, _InputIterator __last,
192 const _Allocator& __a = _Allocator())
193 : _Base(__gnu_debug::__base(
194 __glibcxx_check_valid_constructor_range(__first, __last)),
195 __gnu_debug::__base(__last), __a) { }
197 #if __cplusplus < 201103L
198 vector(const vector& __x)
201 ~vector() _GLIBCXX_NOEXCEPT { }
203 vector(const vector&) = default;
204 vector(vector&&) = default;
206 vector(const vector& __x, const allocator_type& __a)
207 : _Base(__x, __a) { }
209 vector(vector&& __x, const allocator_type& __a)
211 _Base(std::declval<_Base&&>()), std::declval<const allocator_type&>()) )
212 : _Safe(std::move(__x._M_safe()), __a),
213 _Base(std::move(__x._M_base()), __a),
214 _Safe_vector(std::move(__x)) { }
216 vector(initializer_list<value_type> __l,
217 const allocator_type& __a = allocator_type())
218 : _Base(__l, __a) { }
223 /// Construction from a normal-mode vector
224 vector(const _Base& __x)
227 #if __cplusplus < 201103L
229 operator=(const vector& __x)
231 this->_M_safe() = __x;
233 this->_M_update_guaranteed_capacity();
238 operator=(const vector&) = default;
241 operator=(vector&&) = default;
244 operator=(initializer_list<value_type> __l)
247 this->_M_invalidate_all();
248 this->_M_update_guaranteed_capacity();
253 #if __cplusplus >= 201103L
254 template<typename _InputIterator,
255 typename = std::_RequireInputIter<_InputIterator>>
257 template<typename _InputIterator>
260 assign(_InputIterator __first, _InputIterator __last)
262 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
263 __glibcxx_check_valid_range2(__first, __last, __dist);
265 if (__dist.second >= __gnu_debug::__dp_sign)
266 _Base::assign(__gnu_debug::__unsafe(__first),
267 __gnu_debug::__unsafe(__last));
269 _Base::assign(__first, __last);
271 this->_M_invalidate_all();
272 this->_M_update_guaranteed_capacity();
276 assign(size_type __n, const _Tp& __u)
278 _Base::assign(__n, __u);
279 this->_M_invalidate_all();
280 this->_M_update_guaranteed_capacity();
283 #if __cplusplus >= 201103L
285 assign(initializer_list<value_type> __l)
288 this->_M_invalidate_all();
289 this->_M_update_guaranteed_capacity();
293 using _Base::get_allocator;
297 begin() _GLIBCXX_NOEXCEPT
298 { return iterator(_Base::begin(), this); }
301 begin() const _GLIBCXX_NOEXCEPT
302 { return const_iterator(_Base::begin(), this); }
305 end() _GLIBCXX_NOEXCEPT
306 { return iterator(_Base::end(), this); }
309 end() const _GLIBCXX_NOEXCEPT
310 { return const_iterator(_Base::end(), this); }
313 rbegin() _GLIBCXX_NOEXCEPT
314 { return reverse_iterator(end()); }
316 const_reverse_iterator
317 rbegin() const _GLIBCXX_NOEXCEPT
318 { return const_reverse_iterator(end()); }
321 rend() _GLIBCXX_NOEXCEPT
322 { return reverse_iterator(begin()); }
324 const_reverse_iterator
325 rend() const _GLIBCXX_NOEXCEPT
326 { return const_reverse_iterator(begin()); }
328 #if __cplusplus >= 201103L
330 cbegin() const noexcept
331 { return const_iterator(_Base::begin(), this); }
334 cend() const noexcept
335 { return const_iterator(_Base::end(), this); }
337 const_reverse_iterator
338 crbegin() const noexcept
339 { return const_reverse_iterator(end()); }
341 const_reverse_iterator
342 crend() const noexcept
343 { return const_reverse_iterator(begin()); }
346 // 23.2.4.2 capacity:
348 using _Base::max_size;
350 #if __cplusplus >= 201103L
352 resize(size_type __sz)
354 bool __realloc = this->_M_requires_reallocation(__sz);
355 if (__sz < this->size())
356 this->_M_invalidate_after_nth(__sz);
359 this->_M_invalidate_all();
360 this->_M_update_guaranteed_capacity();
364 resize(size_type __sz, const _Tp& __c)
366 bool __realloc = this->_M_requires_reallocation(__sz);
367 if (__sz < this->size())
368 this->_M_invalidate_after_nth(__sz);
369 _Base::resize(__sz, __c);
371 this->_M_invalidate_all();
372 this->_M_update_guaranteed_capacity();
376 resize(size_type __sz, _Tp __c = _Tp())
378 bool __realloc = this->_M_requires_reallocation(__sz);
379 if (__sz < this->size())
380 this->_M_invalidate_after_nth(__sz);
381 _Base::resize(__sz, __c);
383 this->_M_invalidate_all();
384 this->_M_update_guaranteed_capacity();
388 #if __cplusplus >= 201103L
392 if (_Base::_M_shrink_to_fit())
394 this->_M_guaranteed_capacity = _Base::capacity();
395 this->_M_invalidate_all();
401 capacity() const _GLIBCXX_NOEXCEPT
403 #ifdef _GLIBCXX_DEBUG_PEDANTIC
404 return this->_M_guaranteed_capacity;
406 return _Base::capacity();
413 reserve(size_type __n)
415 bool __realloc = this->_M_requires_reallocation(__n);
417 if (__n > this->_M_guaranteed_capacity)
418 this->_M_guaranteed_capacity = __n;
420 this->_M_invalidate_all();
425 operator[](size_type __n) _GLIBCXX_NOEXCEPT
427 __glibcxx_check_subscript(__n);
428 return _M_base()[__n];
432 operator[](size_type __n) const _GLIBCXX_NOEXCEPT
434 __glibcxx_check_subscript(__n);
435 return _M_base()[__n];
441 front() _GLIBCXX_NOEXCEPT
443 __glibcxx_check_nonempty();
444 return _Base::front();
448 front() const _GLIBCXX_NOEXCEPT
450 __glibcxx_check_nonempty();
451 return _Base::front();
455 back() _GLIBCXX_NOEXCEPT
457 __glibcxx_check_nonempty();
458 return _Base::back();
462 back() const _GLIBCXX_NOEXCEPT
464 __glibcxx_check_nonempty();
465 return _Base::back();
468 // _GLIBCXX_RESOLVE_LIB_DEFECTS
469 // DR 464. Suggestion for new member functions in standard containers.
472 // 23.2.4.3 modifiers:
474 push_back(const _Tp& __x)
476 bool __realloc = this->_M_requires_reallocation(this->size() + 1);
477 _Base::push_back(__x);
479 this->_M_invalidate_all();
480 this->_M_update_guaranteed_capacity();
483 #if __cplusplus >= 201103L
484 template<typename _Up = _Tp>
485 typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value,
488 { emplace_back(std::move(__x)); }
490 template<typename... _Args>
491 #if __cplusplus > 201402L
496 emplace_back(_Args&&... __args)
498 bool __realloc = this->_M_requires_reallocation(this->size() + 1);
499 _Base::emplace_back(std::forward<_Args>(__args)...);
501 this->_M_invalidate_all();
502 this->_M_update_guaranteed_capacity();
503 #if __cplusplus > 201402L
510 pop_back() _GLIBCXX_NOEXCEPT
512 __glibcxx_check_nonempty();
513 this->_M_invalidate_if(_Equal(--_Base::end()));
517 #if __cplusplus >= 201103L
518 template<typename... _Args>
520 emplace(const_iterator __position, _Args&&... __args)
522 __glibcxx_check_insert(__position);
523 bool __realloc = this->_M_requires_reallocation(this->size() + 1);
524 difference_type __offset = __position.base() - _Base::cbegin();
525 _Base_iterator __res = _Base::emplace(__position.base(),
526 std::forward<_Args>(__args)...);
528 this->_M_invalidate_all();
530 this->_M_invalidate_after_nth(__offset);
531 this->_M_update_guaranteed_capacity();
532 return { __res, this };
537 #if __cplusplus >= 201103L
538 insert(const_iterator __position, const _Tp& __x)
540 insert(iterator __position, const _Tp& __x)
543 __glibcxx_check_insert(__position);
544 bool __realloc = this->_M_requires_reallocation(this->size() + 1);
545 difference_type __offset = __position.base() - _Base::begin();
546 _Base_iterator __res = _Base::insert(__position.base(), __x);
548 this->_M_invalidate_all();
550 this->_M_invalidate_after_nth(__offset);
551 this->_M_update_guaranteed_capacity();
552 return iterator(__res, this);
555 #if __cplusplus >= 201103L
556 template<typename _Up = _Tp>
557 typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value,
559 insert(const_iterator __position, _Tp&& __x)
560 { return emplace(__position, std::move(__x)); }
563 insert(const_iterator __position, initializer_list<value_type> __l)
564 { return this->insert(__position, __l.begin(), __l.end()); }
567 #if __cplusplus >= 201103L
569 insert(const_iterator __position, size_type __n, const _Tp& __x)
571 __glibcxx_check_insert(__position);
572 bool __realloc = this->_M_requires_reallocation(this->size() + __n);
573 difference_type __offset = __position.base() - _Base::cbegin();
574 _Base_iterator __res = _Base::insert(__position.base(), __n, __x);
576 this->_M_invalidate_all();
578 this->_M_invalidate_after_nth(__offset);
579 this->_M_update_guaranteed_capacity();
580 return { __res, this };
584 insert(iterator __position, size_type __n, const _Tp& __x)
586 __glibcxx_check_insert(__position);
587 bool __realloc = this->_M_requires_reallocation(this->size() + __n);
588 difference_type __offset = __position.base() - _Base::begin();
589 _Base::insert(__position.base(), __n, __x);
591 this->_M_invalidate_all();
593 this->_M_invalidate_after_nth(__offset);
594 this->_M_update_guaranteed_capacity();
598 #if __cplusplus >= 201103L
599 template<class _InputIterator,
600 typename = std::_RequireInputIter<_InputIterator>>
602 insert(const_iterator __position,
603 _InputIterator __first, _InputIterator __last)
605 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
606 __glibcxx_check_insert_range(__position, __first, __last, __dist);
608 /* Hard to guess if invalidation will occur, because __last
609 - __first can't be calculated in all cases, so we just
610 punt here by checking if it did occur. */
611 _Base_iterator __old_begin = _M_base().begin();
612 difference_type __offset = __position.base() - _Base::cbegin();
613 _Base_iterator __res;
614 if (__dist.second >= __gnu_debug::__dp_sign)
615 __res = _Base::insert(__position.base(),
616 __gnu_debug::__unsafe(__first),
617 __gnu_debug::__unsafe(__last));
619 __res = _Base::insert(__position.base(), __first, __last);
621 if (_M_base().begin() != __old_begin)
622 this->_M_invalidate_all();
624 this->_M_invalidate_after_nth(__offset);
625 this->_M_update_guaranteed_capacity();
626 return { __res, this };
629 template<class _InputIterator>
631 insert(iterator __position,
632 _InputIterator __first, _InputIterator __last)
634 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
635 __glibcxx_check_insert_range(__position, __first, __last, __dist);
637 /* Hard to guess if invalidation will occur, because __last
638 - __first can't be calculated in all cases, so we just
639 punt here by checking if it did occur. */
640 _Base_iterator __old_begin = _M_base().begin();
641 difference_type __offset = __position.base() - _Base::begin();
642 if (__dist.second >= __gnu_debug::__dp_sign)
643 _Base::insert(__position.base(), __gnu_debug::__unsafe(__first),
644 __gnu_debug::__unsafe(__last));
646 _Base::insert(__position.base(), __first, __last);
648 if (_M_base().begin() != __old_begin)
649 this->_M_invalidate_all();
651 this->_M_invalidate_after_nth(__offset);
652 this->_M_update_guaranteed_capacity();
657 #if __cplusplus >= 201103L
658 erase(const_iterator __position)
660 erase(iterator __position)
663 __glibcxx_check_erase(__position);
664 difference_type __offset = __position.base() - _Base::begin();
665 _Base_iterator __res = _Base::erase(__position.base());
666 this->_M_invalidate_after_nth(__offset);
667 return iterator(__res, this);
671 #if __cplusplus >= 201103L
672 erase(const_iterator __first, const_iterator __last)
674 erase(iterator __first, iterator __last)
677 // _GLIBCXX_RESOLVE_LIB_DEFECTS
678 // 151. can't currently clear() empty container
679 __glibcxx_check_erase_range(__first, __last);
681 if (__first.base() != __last.base())
683 difference_type __offset = __first.base() - _Base::begin();
684 _Base_iterator __res = _Base::erase(__first.base(),
686 this->_M_invalidate_after_nth(__offset);
687 return iterator(__res, this);
690 #if __cplusplus >= 201103L
691 return { _Base::begin() + (__first.base() - _Base::cbegin()), this };
699 _GLIBCXX_NOEXCEPT_IF( noexcept(declval<_Base&>().swap(__x)) )
703 std::swap(this->_M_guaranteed_capacity, __x._M_guaranteed_capacity);
707 clear() _GLIBCXX_NOEXCEPT
710 this->_M_invalidate_all();
714 _M_base() _GLIBCXX_NOEXCEPT { return *this; }
717 _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
721 _M_invalidate_after_nth(difference_type __n) _GLIBCXX_NOEXCEPT
723 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
724 this->_M_invalidate_if(_After_nth(__n, _Base::begin()));
728 template<typename _Tp, typename _Alloc>
730 operator==(const vector<_Tp, _Alloc>& __lhs,
731 const vector<_Tp, _Alloc>& __rhs)
732 { return __lhs._M_base() == __rhs._M_base(); }
734 template<typename _Tp, typename _Alloc>
736 operator!=(const vector<_Tp, _Alloc>& __lhs,
737 const vector<_Tp, _Alloc>& __rhs)
738 { return __lhs._M_base() != __rhs._M_base(); }
740 template<typename _Tp, typename _Alloc>
742 operator<(const vector<_Tp, _Alloc>& __lhs,
743 const vector<_Tp, _Alloc>& __rhs)
744 { return __lhs._M_base() < __rhs._M_base(); }
746 template<typename _Tp, typename _Alloc>
748 operator<=(const vector<_Tp, _Alloc>& __lhs,
749 const vector<_Tp, _Alloc>& __rhs)
750 { return __lhs._M_base() <= __rhs._M_base(); }
752 template<typename _Tp, typename _Alloc>
754 operator>=(const vector<_Tp, _Alloc>& __lhs,
755 const vector<_Tp, _Alloc>& __rhs)
756 { return __lhs._M_base() >= __rhs._M_base(); }
758 template<typename _Tp, typename _Alloc>
760 operator>(const vector<_Tp, _Alloc>& __lhs,
761 const vector<_Tp, _Alloc>& __rhs)
762 { return __lhs._M_base() > __rhs._M_base(); }
764 template<typename _Tp, typename _Alloc>
766 swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>& __rhs)
767 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
768 { __lhs.swap(__rhs); }
770 #if __cpp_deduction_guides >= 201606
771 template<typename _InputIterator, typename _ValT
772 = typename iterator_traits<_InputIterator>::value_type,
773 typename _Allocator = allocator<_ValT>,
774 typename = _RequireInputIter<_InputIterator>,
775 typename = _RequireAllocator<_Allocator>>
776 vector(_InputIterator, _InputIterator, _Allocator = _Allocator())
777 -> vector<_ValT, _Allocator>;
780 } // namespace __debug
782 _GLIBCXX_BEGIN_NAMESPACE_VERSION
784 #if __cplusplus >= 201103L
786 /// std::hash specialization for vector<bool>.
787 template<typename _Alloc>
788 struct hash<__debug::vector<bool, _Alloc>>
789 : public __hash_base<size_t, __debug::vector<bool, _Alloc>>
792 operator()(const __debug::vector<bool, _Alloc>& __b) const noexcept
793 { return std::hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>()(__b); }
797 template<typename _Iterator, typename _Container, typename _Sequence>
799 __niter_base(const __gnu_debug::_Safe_iterator<
800 __gnu_cxx::__normal_iterator<_Iterator, _Container>,
801 _Sequence, std::random_access_iterator_tag>& __it)
802 { return std::__niter_base(__it.base()); }
804 #if __cplusplus >= 201703L
805 namespace __detail::__variant
807 template<typename> struct _Never_valueless_alt; // see <variant>
809 // Provide the strong exception-safety guarantee when emplacing a
810 // vector into a variant, but only if move assignment cannot throw.
811 template<typename _Tp, typename _Alloc>
812 struct _Never_valueless_alt<__debug::vector<_Tp, _Alloc>>
813 : std::is_nothrow_move_assignable<__debug::vector<_Tp, _Alloc>>
815 } // namespace __detail::__variant
818 _GLIBCXX_END_NAMESPACE_VERSION
821 namespace __gnu_debug
823 template<typename _Tp, typename _Alloc>
824 struct _Is_contiguous_sequence<std::__debug::vector<_Tp, _Alloc> >
828 template<typename _Alloc>
829 struct _Is_contiguous_sequence<std::__debug::vector<bool, _Alloc> >