1// Debugging vector implementation -*- C++ -*-
3// Copyright (C) 2003-2022 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/>.
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>
35namespace std _GLIBCXX_VISIBILITY(default) { namespace __debug {
36 template<typename _Tp, typename _Allocator> class vector;
37} } // namespace std::__debug
40#include <debug/safe_sequence.h>
41#include <debug/safe_container.h>
42#include <debug/safe_iterator.h>
46 /** @brief Base class for Debug Mode vector.
48 * Adds information about the guaranteed capacity, which is useful for
49 * detecting code which relies on non-portable implementation details of
50 * the libstdc++ reallocation policy.
52 template<typename _SafeSequence,
53 typename _BaseSequence>
56 typedef typename _BaseSequence::size_type size_type;
59 _M_seq() const { return *static_cast<const _SafeSequence*>(this); }
62 _Safe_vector() _GLIBCXX_NOEXCEPT
63 : _M_guaranteed_capacity(0)
64 { _M_update_guaranteed_capacity(); }
66 _Safe_vector(const _Safe_vector&) _GLIBCXX_NOEXCEPT
67 : _M_guaranteed_capacity(0)
68 { _M_update_guaranteed_capacity(); }
70 _Safe_vector(size_type __n) _GLIBCXX_NOEXCEPT
71 : _M_guaranteed_capacity(__n)
75 operator=(const _Safe_vector&) _GLIBCXX_NOEXCEPT
77 _M_update_guaranteed_capacity();
81#if __cplusplus >= 201103L
82 _Safe_vector(_Safe_vector&& __x) noexcept
84 { __x._M_guaranteed_capacity = 0; }
87 operator=(_Safe_vector&& __x) noexcept
89 _M_update_guaranteed_capacity();
90 __x._M_guaranteed_capacity = 0;
95 size_type _M_guaranteed_capacity;
98 _M_requires_reallocation(size_type __elements) const _GLIBCXX_NOEXCEPT
99 { return __elements > _M_seq().capacity(); }
102 _M_update_guaranteed_capacity() _GLIBCXX_NOEXCEPT
104 if (_M_seq().size() > _M_guaranteed_capacity)
105 _M_guaranteed_capacity = _M_seq().size();
110namespace std _GLIBCXX_VISIBILITY(default)
114 /// Class std::vector with safety/checking/debug instrumentation.
115 template<typename _Tp,
116 typename _Allocator = std::allocator<_Tp> >
118 : public __gnu_debug::_Safe_container<
119 vector<_Tp, _Allocator>, _Allocator, __gnu_debug::_Safe_sequence>,
120 public _GLIBCXX_STD_C::vector<_Tp, _Allocator>,
121 public __gnu_debug::_Safe_vector<
122 vector<_Tp, _Allocator>,
123 _GLIBCXX_STD_C::vector<_Tp, _Allocator> >
125 typedef _GLIBCXX_STD_C::vector<_Tp, _Allocator> _Base;
126 typedef __gnu_debug::_Safe_container<
127 vector, _Allocator, __gnu_debug::_Safe_sequence> _Safe;
128 typedef __gnu_debug::_Safe_vector<vector, _Base> _Safe_vector;
130 typedef typename _Base::iterator _Base_iterator;
131 typedef typename _Base::const_iterator _Base_const_iterator;
132 typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
134 template<typename _ItT, typename _SeqT, typename _CatT>
135 friend class ::__gnu_debug::_Safe_iterator;
137 // Reference wrapper for base class. Disambiguates vector(const _Base&)
138 // from copy constructor by requiring a user-defined conversion.
139 // See PR libstdc++/90102.
142 _Base_ref(const _Base& __r) : _M_ref(__r) { }
148 typedef typename _Base::reference reference;
149 typedef typename _Base::const_reference const_reference;
151 typedef __gnu_debug::_Safe_iterator<
152 _Base_iterator, vector> iterator;
153 typedef __gnu_debug::_Safe_iterator<
154 _Base_const_iterator, vector> const_iterator;
156 typedef typename _Base::size_type size_type;
157 typedef typename _Base::difference_type difference_type;
159 typedef _Tp value_type;
160 typedef _Allocator allocator_type;
161 typedef typename _Base::pointer pointer;
162 typedef typename _Base::const_pointer const_pointer;
163 typedef std::reverse_iterator<iterator> reverse_iterator;
164 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
166 // 23.2.4.1 construct/copy/destroy:
168#if __cplusplus < 201103L
169 vector() _GLIBCXX_NOEXCEPT
176 vector(const _Allocator& __a) _GLIBCXX_NOEXCEPT
179#if __cplusplus >= 201103L
181 vector(size_type __n, const _Allocator& __a = _Allocator())
182 : _Base(__n, __a), _Safe_vector(__n) { }
184 vector(size_type __n, const __type_identity_t<_Tp>& __value,
185 const _Allocator& __a = _Allocator())
186 : _Base(__n, __value, __a) { }
189 vector(size_type __n, const _Tp& __value = _Tp(),
190 const _Allocator& __a = _Allocator())
191 : _Base(__n, __value, __a) { }
194#if __cplusplus >= 201103L
195 template<class _InputIterator,
196 typename = std::_RequireInputIter<_InputIterator>>
198 template<class _InputIterator>
200 vector(_InputIterator __first, _InputIterator __last,
201 const _Allocator& __a = _Allocator())
202 : _Base(__gnu_debug::__base(
203 __glibcxx_check_valid_constructor_range(__first, __last)),
204 __gnu_debug::__base(__last), __a) { }
206#if __cplusplus < 201103L
207 vector(const vector& __x)
210 ~vector() _GLIBCXX_NOEXCEPT { }
212 vector(const vector&) = default;
213 vector(vector&&) = default;
215 vector(const vector& __x, const __type_identity_t<allocator_type>& __a)
216 : _Base(__x, __a) { }
218 vector(vector&& __x, const __type_identity_t<allocator_type>& __a)
220 std::is_nothrow_constructible<_Base,
221 _Base, const allocator_type&>::value )
222 : _Safe(std::move(__x), __a),
223 _Base(std::move(__x), __a),
224 _Safe_vector(std::move(__x)) { }
226 vector(initializer_list<value_type> __l,
227 const allocator_type& __a = allocator_type())
228 : _Base(__l, __a) { }
233 /// Construction from a normal-mode vector
234 vector(_Base_ref __x)
235 : _Base(__x._M_ref) { }
237#if __cplusplus >= 201103L
239 operator=(const vector&) = default;
242 operator=(vector&&) = default;
245 operator=(initializer_list<value_type> __l)
247 _Base::operator=(__l);
248 this->_M_invalidate_all();
249 this->_M_update_guaranteed_capacity();
254#if __cplusplus >= 201103L
255 template<typename _InputIterator,
256 typename = std::_RequireInputIter<_InputIterator>>
258 template<typename _InputIterator>
261 assign(_InputIterator __first, _InputIterator __last)
263 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
264 __glibcxx_check_valid_range2(__first, __last, __dist);
266 if (__dist.second >= __gnu_debug::__dp_sign)
267 _Base::assign(__gnu_debug::__unsafe(__first),
268 __gnu_debug::__unsafe(__last));
270 _Base::assign(__first, __last);
272 this->_M_invalidate_all();
273 this->_M_update_guaranteed_capacity();
277 assign(size_type __n, const _Tp& __u)
279 _Base::assign(__n, __u);
280 this->_M_invalidate_all();
281 this->_M_update_guaranteed_capacity();
284#if __cplusplus >= 201103L
286 assign(initializer_list<value_type> __l)
289 this->_M_invalidate_all();
290 this->_M_update_guaranteed_capacity();
294 using _Base::get_allocator;
299 begin() _GLIBCXX_NOEXCEPT
300 { return iterator(_Base::begin(), this); }
304 begin() const _GLIBCXX_NOEXCEPT
305 { return const_iterator(_Base::begin(), this); }
309 end() _GLIBCXX_NOEXCEPT
310 { return iterator(_Base::end(), this); }
314 end() const _GLIBCXX_NOEXCEPT
315 { return const_iterator(_Base::end(), this); }
319 rbegin() _GLIBCXX_NOEXCEPT
320 { return reverse_iterator(end()); }
323 const_reverse_iterator
324 rbegin() const _GLIBCXX_NOEXCEPT
325 { return const_reverse_iterator(end()); }
329 rend() _GLIBCXX_NOEXCEPT
330 { return reverse_iterator(begin()); }
333 const_reverse_iterator
334 rend() const _GLIBCXX_NOEXCEPT
335 { return const_reverse_iterator(begin()); }
337#if __cplusplus >= 201103L
340 cbegin() const noexcept
341 { return const_iterator(_Base::begin(), this); }
345 cend() const noexcept
346 { return const_iterator(_Base::end(), this); }
349 const_reverse_iterator
350 crbegin() const noexcept
351 { return const_reverse_iterator(end()); }
354 const_reverse_iterator
355 crend() const noexcept
356 { return const_reverse_iterator(begin()); }
359 // 23.2.4.2 capacity:
361 using _Base::max_size;
363#if __cplusplus >= 201103L
365 resize(size_type __sz)
367 bool __realloc = this->_M_requires_reallocation(__sz);
368 if (__sz < this->size())
369 this->_M_invalidate_after_nth(__sz);
372 this->_M_invalidate_all();
373 this->_M_update_guaranteed_capacity();
377 resize(size_type __sz, const _Tp& __c)
379 bool __realloc = this->_M_requires_reallocation(__sz);
380 if (__sz < this->size())
381 this->_M_invalidate_after_nth(__sz);
382 _Base::resize(__sz, __c);
384 this->_M_invalidate_all();
385 this->_M_update_guaranteed_capacity();
389 resize(size_type __sz, _Tp __c = _Tp())
391 bool __realloc = this->_M_requires_reallocation(__sz);
392 if (__sz < this->size())
393 this->_M_invalidate_after_nth(__sz);
394 _Base::resize(__sz, __c);
396 this->_M_invalidate_all();
397 this->_M_update_guaranteed_capacity();
401#if __cplusplus >= 201103L
405 if (_Base::_M_shrink_to_fit())
407 this->_M_guaranteed_capacity = _Base::capacity();
408 this->_M_invalidate_all();
415 capacity() const _GLIBCXX_NOEXCEPT
417#ifdef _GLIBCXX_DEBUG_PEDANTIC
418 return this->_M_guaranteed_capacity;
420 return _Base::capacity();
427 reserve(size_type __n)
429 bool __realloc = this->_M_requires_reallocation(__n);
431 if (__n > this->_M_guaranteed_capacity)
432 this->_M_guaranteed_capacity = __n;
434 this->_M_invalidate_all();
440 operator[](size_type __n) _GLIBCXX_NOEXCEPT
442 __glibcxx_check_subscript(__n);
443 return _Base::operator[](__n);
448 operator[](size_type __n) const _GLIBCXX_NOEXCEPT
450 __glibcxx_check_subscript(__n);
451 return _Base::operator[](__n);
458 front() _GLIBCXX_NOEXCEPT
460 __glibcxx_check_nonempty();
461 return _Base::front();
466 front() const _GLIBCXX_NOEXCEPT
468 __glibcxx_check_nonempty();
469 return _Base::front();
474 back() _GLIBCXX_NOEXCEPT
476 __glibcxx_check_nonempty();
477 return _Base::back();
482 back() const _GLIBCXX_NOEXCEPT
484 __glibcxx_check_nonempty();
485 return _Base::back();
488 // _GLIBCXX_RESOLVE_LIB_DEFECTS
489 // DR 464. Suggestion for new member functions in standard containers.
492 // 23.2.4.3 modifiers:
494 push_back(const _Tp& __x)
496 bool __realloc = this->_M_requires_reallocation(this->size() + 1);
497 _Base::push_back(__x);
499 this->_M_invalidate_all();
500 this->_M_update_guaranteed_capacity();
503#if __cplusplus >= 201103L
504 template<typename _Up = _Tp>
505 typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value,
508 { emplace_back(std::move(__x)); }
510 template<typename... _Args>
511#if __cplusplus > 201402L
516 emplace_back(_Args&&... __args)
518 bool __realloc = this->_M_requires_reallocation(this->size() + 1);
519 _Base::emplace_back(std::forward<_Args>(__args)...);
521 this->_M_invalidate_all();
522 this->_M_update_guaranteed_capacity();
523#if __cplusplus > 201402L
530 pop_back() _GLIBCXX_NOEXCEPT
532 __glibcxx_check_nonempty();
533 this->_M_invalidate_if(_Equal(--_Base::end()));
537#if __cplusplus >= 201103L
538 template<typename... _Args>
540 emplace(const_iterator __position, _Args&&... __args)
542 __glibcxx_check_insert(__position);
543 bool __realloc = this->_M_requires_reallocation(this->size() + 1);
544 difference_type __offset = __position.base() - _Base::cbegin();
545 _Base_iterator __res = _Base::emplace(__position.base(),
546 std::forward<_Args>(__args)...);
548 this->_M_invalidate_all();
550 this->_M_invalidate_after_nth(__offset);
551 this->_M_update_guaranteed_capacity();
552 return { __res, this };
557#if __cplusplus >= 201103L
558 insert(const_iterator __position, const _Tp& __x)
560 insert(iterator __position, const _Tp& __x)
563 __glibcxx_check_insert(__position);
564 bool __realloc = this->_M_requires_reallocation(this->size() + 1);
565 difference_type __offset = __position.base() - _Base::begin();
566 _Base_iterator __res = _Base::insert(__position.base(), __x);
568 this->_M_invalidate_all();
570 this->_M_invalidate_after_nth(__offset);
571 this->_M_update_guaranteed_capacity();
572 return iterator(__res, this);
575#if __cplusplus >= 201103L
576 template<typename _Up = _Tp>
577 typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value,
579 insert(const_iterator __position, _Tp&& __x)
580 { return emplace(__position, std::move(__x)); }
583 insert(const_iterator __position, initializer_list<value_type> __l)
584 { return this->insert(__position, __l.begin(), __l.end()); }
587#if __cplusplus >= 201103L
589 insert(const_iterator __position, size_type __n, const _Tp& __x)
591 __glibcxx_check_insert(__position);
592 bool __realloc = this->_M_requires_reallocation(this->size() + __n);
593 difference_type __offset = __position.base() - _Base::cbegin();
594 _Base_iterator __res = _Base::insert(__position.base(), __n, __x);
596 this->_M_invalidate_all();
598 this->_M_invalidate_after_nth(__offset);
599 this->_M_update_guaranteed_capacity();
600 return { __res, this };
604 insert(iterator __position, size_type __n, const _Tp& __x)
606 __glibcxx_check_insert(__position);
607 bool __realloc = this->_M_requires_reallocation(this->size() + __n);
608 difference_type __offset = __position.base() - _Base::begin();
609 _Base::insert(__position.base(), __n, __x);
611 this->_M_invalidate_all();
613 this->_M_invalidate_after_nth(__offset);
614 this->_M_update_guaranteed_capacity();
618#if __cplusplus >= 201103L
619 template<class _InputIterator,
620 typename = std::_RequireInputIter<_InputIterator>>
622 insert(const_iterator __position,
623 _InputIterator __first, _InputIterator __last)
625 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
626 __glibcxx_check_insert_range(__position, __first, __last, __dist);
628 /* Hard to guess if invalidation will occur, because __last
629 - __first can't be calculated in all cases, so we just
630 punt here by checking if it did occur. */
631 _Base_iterator __old_begin = _M_base().begin();
632 difference_type __offset = __position.base() - _Base::cbegin();
633 _Base_iterator __res;
634 if (__dist.second >= __gnu_debug::__dp_sign)
635 __res = _Base::insert(__position.base(),
636 __gnu_debug::__unsafe(__first),
637 __gnu_debug::__unsafe(__last));
639 __res = _Base::insert(__position.base(), __first, __last);
641 if (_M_base().begin() != __old_begin)
642 this->_M_invalidate_all();
644 this->_M_invalidate_after_nth(__offset);
645 this->_M_update_guaranteed_capacity();
646 return { __res, this };
649 template<class _InputIterator>
651 insert(iterator __position,
652 _InputIterator __first, _InputIterator __last)
654 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
655 __glibcxx_check_insert_range(__position, __first, __last, __dist);
657 /* Hard to guess if invalidation will occur, because __last
658 - __first can't be calculated in all cases, so we just
659 punt here by checking if it did occur. */
660 _Base_iterator __old_begin = _M_base().begin();
661 difference_type __offset = __position.base() - _Base::begin();
662 if (__dist.second >= __gnu_debug::__dp_sign)
663 _Base::insert(__position.base(), __gnu_debug::__unsafe(__first),
664 __gnu_debug::__unsafe(__last));
666 _Base::insert(__position.base(), __first, __last);
668 if (_M_base().begin() != __old_begin)
669 this->_M_invalidate_all();
671 this->_M_invalidate_after_nth(__offset);
672 this->_M_update_guaranteed_capacity();
677#if __cplusplus >= 201103L
678 erase(const_iterator __position)
680 erase(iterator __position)
683 __glibcxx_check_erase(__position);
684 difference_type __offset = __position.base() - _Base::begin();
685 _Base_iterator __res = _Base::erase(__position.base());
686 this->_M_invalidate_after_nth(__offset);
687 return iterator(__res, this);
691#if __cplusplus >= 201103L
692 erase(const_iterator __first, const_iterator __last)
694 erase(iterator __first, iterator __last)
697 // _GLIBCXX_RESOLVE_LIB_DEFECTS
698 // 151. can't currently clear() empty container
699 __glibcxx_check_erase_range(__first, __last);
701 if (__first.base() != __last.base())
703 difference_type __offset = __first.base() - _Base::begin();
704 _Base_iterator __res = _Base::erase(__first.base(),
706 this->_M_invalidate_after_nth(__offset);
707 return iterator(__res, this);
710#if __cplusplus >= 201103L
711 return { _Base::begin() + (__first.base() - _Base::cbegin()), this };
719 _GLIBCXX_NOEXCEPT_IF( noexcept(declval<_Base&>().swap(__x)) )
723 std::swap(this->_M_guaranteed_capacity, __x._M_guaranteed_capacity);
727 clear() _GLIBCXX_NOEXCEPT
730 this->_M_invalidate_all();
734 _M_base() _GLIBCXX_NOEXCEPT { return *this; }
737 _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
741 _M_invalidate_after_nth(difference_type __n) _GLIBCXX_NOEXCEPT
743 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
744 this->_M_invalidate_if(_After_nth(__n, _Base::begin()));
748 template<typename _Tp, typename _Alloc>
750 operator==(const vector<_Tp, _Alloc>& __lhs,
751 const vector<_Tp, _Alloc>& __rhs)
752 { return __lhs._M_base() == __rhs._M_base(); }
754#if __cpp_lib_three_way_comparison
755 template<typename _Tp, typename _Alloc>
756 constexpr __detail::__synth3way_t<_Tp>
757 operator<=>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
758 { return __x._M_base() <=> __y._M_base(); }
760 template<typename _Tp, typename _Alloc>
762 operator!=(const vector<_Tp, _Alloc>& __lhs,
763 const vector<_Tp, _Alloc>& __rhs)
764 { return __lhs._M_base() != __rhs._M_base(); }
766 template<typename _Tp, typename _Alloc>
768 operator<(const vector<_Tp, _Alloc>& __lhs,
769 const vector<_Tp, _Alloc>& __rhs)
770 { return __lhs._M_base() < __rhs._M_base(); }
772 template<typename _Tp, typename _Alloc>
774 operator<=(const vector<_Tp, _Alloc>& __lhs,
775 const vector<_Tp, _Alloc>& __rhs)
776 { return __lhs._M_base() <= __rhs._M_base(); }
778 template<typename _Tp, typename _Alloc>
780 operator>=(const vector<_Tp, _Alloc>& __lhs,
781 const vector<_Tp, _Alloc>& __rhs)
782 { return __lhs._M_base() >= __rhs._M_base(); }
784 template<typename _Tp, typename _Alloc>
786 operator>(const vector<_Tp, _Alloc>& __lhs,
787 const vector<_Tp, _Alloc>& __rhs)
788 { return __lhs._M_base() > __rhs._M_base(); }
789#endif // three-way comparison
791 template<typename _Tp, typename _Alloc>
793 swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>& __rhs)
794 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
795 { __lhs.swap(__rhs); }
797#if __cpp_deduction_guides >= 201606
798 template<typename _InputIterator, typename _ValT
799 = typename iterator_traits<_InputIterator>::value_type,
800 typename _Allocator = allocator<_ValT>,
801 typename = _RequireInputIter<_InputIterator>,
802 typename = _RequireAllocator<_Allocator>>
803 vector(_InputIterator, _InputIterator, _Allocator = _Allocator())
804 -> vector<_ValT, _Allocator>;
806 template<typename _Tp, typename _Allocator = allocator<_Tp>,
807 typename = _RequireAllocator<_Allocator>>
808 vector(size_t, _Tp, _Allocator = _Allocator())
809 -> vector<_Tp, _Allocator>;
812} // namespace __debug
814_GLIBCXX_BEGIN_NAMESPACE_VERSION
816#if __cplusplus >= 201103L
818 /// std::hash specialization for vector<bool>.
819 template<typename _Alloc>
820 struct hash<__debug::vector<bool, _Alloc>>
821 : public __hash_base<size_t, __debug::vector<bool, _Alloc>>
824 operator()(const __debug::vector<bool, _Alloc>& __b) const noexcept
825 { return std::hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>()(__b); }
829#if __cplusplus >= 201703L
830 namespace __detail::__variant
832 template<typename> struct _Never_valueless_alt; // see <variant>
834 // Provide the strong exception-safety guarantee when emplacing a
835 // vector into a variant, but only if move assignment cannot throw.
836 template<typename _Tp, typename _Alloc>
837 struct _Never_valueless_alt<__debug::vector<_Tp, _Alloc>>
838 : std::is_nothrow_move_assignable<__debug::vector<_Tp, _Alloc>>
840 } // namespace __detail::__variant
843_GLIBCXX_END_NAMESPACE_VERSION
848 template<typename _Tp, typename _Alloc>
849 struct _Is_contiguous_sequence<std::__debug::vector<_Tp, _Alloc> >
853 template<typename _Alloc>
854 struct _Is_contiguous_sequence<std::__debug::vector<bool, _Alloc> >