29 #ifndef _GLIBCXX_DEBUG_FORWARD_LIST
30 #define _GLIBCXX_DEBUG_FORWARD_LIST 1
32 #pragma GCC system_header
35 namespace std _GLIBCXX_VISIBILITY(default) {
namespace __debug {
45 #define __glibcxx_check_valid_fl_range(_First,_Last,_Dist) \
46 _GLIBCXX_DEBUG_VERIFY(_First._M_valid_range(_Last, _Dist, false), \
47 _M_message(__gnu_debug::__msg_valid_range) \
48 ._M_iterator(_First, #_First) \
49 ._M_iterator(_Last, #_Last))
55 template<
typename _SafeSequence>
61 {
return *
static_cast<_SafeSequence*
>(
this); }
75 using _Base_const_iterator = __decltype(_M_this()._M_base().
cend());
78 return __it != _M_this()._M_base().cbefore_begin()
79 && __it != _M_this()._M_base().cend(); });
85 template<
typename _SafeSequence>
93 using const_iterator =
typename _SafeSequence::const_iterator;
96 _SafeSequence& __rseq =
static_cast<_SafeSequence&
>(__rhs);
102 const_iterator* __victim =
103 static_cast<const_iterator*
>(__victim_base);
105 if (__victim->base() == __rseq._M_base().cbefore_begin())
108 if (__lhs_iterators == __victim_base)
109 __lhs_iterators = __victim_base->
_M_next;
112 __victim_base->
_M_next = __bbegin_its;
113 __bbegin_its->
_M_prior = __victim_base;
116 __last_bbegin = __victim_base;
117 __bbegin_its = __victim_base;
127 __rhs_iterators->
_M_prior = __last_bbegin;
128 __last_bbegin->
_M_next = __rhs_iterators;
130 __rhs_iterators = __bbegin_its;
134 template<
typename _SafeSequence>
136 _Safe_forward_list<_SafeSequence>::
137 _M_swap_single(_Safe_sequence_base& __other) noexcept
139 std::swap(_M_this()._M_iterators, __other._M_iterators);
140 std::swap(_M_this()._M_const_iterators, __other._M_const_iterators);
143 _Safe_iterator_base* __this_its = _M_this()._M_iterators;
144 _M_swap_aux(__other, __other._M_iterators,
145 _M_this(), _M_this()._M_iterators);
146 _Safe_iterator_base* __this_const_its = _M_this()._M_const_iterators;
147 _M_swap_aux(__other, __other._M_const_iterators,
148 _M_this(), _M_this()._M_const_iterators);
149 _M_swap_aux(_M_this(), __this_its,
150 __other, __other._M_iterators);
151 _M_swap_aux(_M_this(), __this_const_its,
152 __other, __other._M_const_iterators);
157 template<
typename _SafeSequence>
159 _Safe_forward_list<_SafeSequence>::
160 _M_swap(_Safe_sequence_base& __other) noexcept
163 using namespace __gnu_cxx;
164 __mutex *__this_mutex = &_M_this()._M_get_mutex();
165 __mutex *__other_mutex =
166 &
static_cast<_SafeSequence&
>(__other)._M_get_mutex();
167 if (__this_mutex == __other_mutex)
170 _M_swap_single(__other);
175 ? *__this_mutex : *__other_mutex);
177 ? *__other_mutex : *__this_mutex);
178 _M_swap_single(__other);
183 namespace std _GLIBCXX_VISIBILITY(default)
188 template<
typename _Tp,
typename _Alloc = std::allocator<_Tp> >
191 forward_list<_Tp, _Alloc>, _Alloc, __gnu_debug::_Safe_forward_list>,
192 public _GLIBCXX_STD_C::forward_list<_Tp, _Alloc>
194 typedef _GLIBCXX_STD_C::forward_list<_Tp, _Alloc> _Base;
198 typedef typename _Base::iterator _Base_iterator;
199 typedef typename _Base::const_iterator _Base_const_iterator;
201 template<
typename _ItT,
typename _SeqT,
typename _CatT>
202 friend class ::__gnu_debug::_Safe_iterator;
205 typedef typename _Base::reference reference;
206 typedef typename _Base::const_reference const_reference;
209 _Base_iterator, forward_list> iterator;
211 _Base_const_iterator, forward_list> const_iterator;
213 typedef typename _Base::size_type size_type;
214 typedef typename _Base::difference_type difference_type;
216 typedef _Tp value_type;
217 typedef typename _Base::allocator_type allocator_type;
218 typedef typename _Base::pointer pointer;
219 typedef typename _Base::const_pointer const_pointer;
223 forward_list() =
default;
226 forward_list(
const allocator_type& __al) noexcept
229 forward_list(
const forward_list& __list,
const allocator_type& __al)
230 : _Base(__list, __al)
233 forward_list(forward_list&& __list,
const allocator_type& __al)
234 : _Safe(std::
move(__list._M_safe()), __al),
235 _Base(std::
move(__list._M_base()), __al)
239 forward_list(size_type __n,
const allocator_type& __al = allocator_type())
243 forward_list(size_type __n,
const _Tp& __value,
244 const allocator_type& __al = allocator_type())
245 : _Base(__n, __value, __al)
248 template<
typename _InputIterator,
249 typename = std::_RequireInputIter<_InputIterator>>
250 forward_list(_InputIterator __first, _InputIterator __last,
251 const allocator_type& __al = allocator_type())
252 : _Base(__gnu_debug::
__base(
253 __glibcxx_check_valid_constructor_range(__first, __last)),
254 __gnu_debug::
__base(__last), __al)
257 forward_list(
const forward_list&) =
default;
259 forward_list(forward_list&&) =
default;
262 const allocator_type& __al = allocator_type())
266 ~forward_list() =
default;
269 operator=(
const forward_list&) =
default;
272 operator=(forward_list&&) =
default;
278 this->_M_invalidate_all();
282 template<
typename _InputIterator,
283 typename = std::_RequireInputIter<_InputIterator>>
285 assign(_InputIterator __first, _InputIterator __last)
288 __glibcxx_check_valid_range2(__first, __last, __dist);
290 if (__dist.
second >= __gnu_debug::__dp_sign)
291 _Base::assign(__gnu_debug::__unsafe(__first),
292 __gnu_debug::__unsafe(__last));
294 _Base::assign(__first, __last);
296 this->_M_invalidate_all();
300 assign(size_type __n,
const _Tp& __val)
302 _Base::assign(__n, __val);
303 this->_M_invalidate_all();
310 this->_M_invalidate_all();
313 using _Base::get_allocator;
318 before_begin() noexcept
319 {
return { _Base::before_begin(),
this }; }
322 before_begin() const noexcept
323 {
return { _Base::before_begin(),
this }; }
330 begin() const noexcept
346 cbefore_begin() const noexcept
347 {
return { _Base::cbefore_begin(),
this }; }
350 cend() const noexcept
354 using _Base::max_size;
361 __glibcxx_check_nonempty();
362 return _Base::front();
368 __glibcxx_check_nonempty();
369 return _Base::front();
374 using _Base::emplace_front;
375 using _Base::push_front;
380 __glibcxx_check_nonempty();
381 this->_M_invalidate_if([
this](_Base_const_iterator __it)
382 {
return __it == this->_M_base().cbegin(); });
386 template<
typename... _Args>
388 emplace_after(const_iterator __pos, _Args&&... __args)
391 return { _Base::emplace_after(__pos.base(),
392 std::forward<_Args>(__args)...),
397 insert_after(const_iterator __pos,
const _Tp& __val)
400 return { _Base::insert_after(__pos.base(), __val),
this };
404 insert_after(const_iterator __pos, _Tp&& __val)
407 return { _Base::insert_after(__pos.base(),
std::move(__val)),
this };
411 insert_after(const_iterator __pos, size_type __n,
const _Tp& __val)
414 return { _Base::insert_after(__pos.base(), __n, __val),
this };
417 template<
typename _InputIterator,
418 typename = std::_RequireInputIter<_InputIterator>>
420 insert_after(const_iterator __pos,
421 _InputIterator __first, _InputIterator __last)
426 if (__dist.
second >= __gnu_debug::__dp_sign)
429 _Base::insert_after(__pos.base(),
430 __gnu_debug::__unsafe(__first),
431 __gnu_debug::__unsafe(__last)),
435 return { _Base::insert_after(__pos.base(), __first, __last),
this };
442 return { _Base::insert_after(__pos.base(), __il),
this };
447 _M_erase_after(_Base_const_iterator __pos)
449 _Base_const_iterator __next = std::next(__pos);
450 this->_M_invalidate_if([__next](_Base_const_iterator __it)
451 {
return __it == __next; });
452 return _Base::erase_after(__pos);
456 erase_after(const_iterator __pos)
459 return { _M_erase_after(__pos.base()),
this };
463 erase_after(const_iterator __pos, const_iterator __last)
466 for (_Base_const_iterator __victim = std::next(__pos.base());
467 __victim != __last.base(); ++__victim)
470 _M_message(__gnu_debug::__msg_valid_range2)
471 ._M_sequence(*
this,
"this")
472 ._M_iterator(__pos,
"pos")
473 ._M_iterator(__last,
"last"));
474 this->_M_invalidate_if([__victim](_Base_const_iterator __it)
475 {
return __it == __victim; });
478 return { _Base::erase_after(__pos.base(), __last.base()),
this };
482 swap(forward_list& __list)
483 noexcept( noexcept(declval<_Base&>().swap(__list)) )
485 _Safe::_M_swap(__list);
490 resize(size_type __sz)
492 this->_M_detach_singular();
497 for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
500 for (; __victim != __end; ++__victim)
502 this->_M_invalidate_if([__victim](_Base_const_iterator __it)
503 {
return __it == __victim; });
512 this->_M_revalidate_singular();
513 __throw_exception_again;
518 resize(size_type __sz,
const value_type& __val)
520 this->_M_detach_singular();
525 for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
528 for (; __victim != __end; ++__victim)
530 this->_M_invalidate_if([__victim](_Base_const_iterator __it)
531 {
return __it == __victim; });
536 _Base::resize(__sz, __val);
540 this->_M_revalidate_singular();
541 __throw_exception_again;
549 this->_M_invalidate_all();
554 splice_after(const_iterator __pos, forward_list&& __list)
558 _M_message(__gnu_debug::__msg_self_splice)
559 ._M_sequence(*
this,
"this"));
560 _GLIBCXX_DEBUG_VERIFY(__list.get_allocator() == this->get_allocator(),
561 _M_message(__gnu_debug::__msg_splice_alloc)
563 ._M_sequence(__list,
"__list"));
564 this->_M_transfer_from_if(__list, [&__list](_Base_const_iterator __it)
566 return __it != __list._M_base().cbefore_begin()
567 && __it != __list._M_base().end();
569 _Base::splice_after(__pos.base(),
std::move(__list._M_base()));
573 splice_after(const_iterator __pos, forward_list& __list)
574 { splice_after(__pos,
std::move(__list)); }
577 splice_after(const_iterator __pos, forward_list&& __list,
581 _GLIBCXX_DEBUG_VERIFY(__i._M_before_dereferenceable(),
582 _M_message(__gnu_debug::__msg_splice_bad)
583 ._M_iterator(__i,
"__i"));
585 _M_message(__gnu_debug::__msg_splice_other)
586 ._M_iterator(__i,
"__i")
587 ._M_sequence(__list,
"__list"));
588 _GLIBCXX_DEBUG_VERIFY(__list.get_allocator() == this->get_allocator(),
589 _M_message(__gnu_debug::__msg_splice_alloc)
591 ._M_sequence(__list,
"__list"));
595 _Base_const_iterator __next = std::next(__i.base());
596 this->_M_transfer_from_if(__list, [__next](_Base_const_iterator __it)
597 {
return __it == __next; });
598 _Base::splice_after(__pos.base(),
std::move(__list._M_base()),
603 splice_after(const_iterator __pos, forward_list& __list,
605 { splice_after(__pos,
std::move(__list), __i); }
608 splice_after(const_iterator __pos, forward_list&& __list,
609 const_iterator __before, const_iterator __last)
614 __glibcxx_check_valid_fl_range(__before, __last, __dist);
615 _GLIBCXX_DEBUG_VERIFY(__before._M_attached_to(__listptr),
616 _M_message(__gnu_debug::__msg_splice_other)
617 ._M_sequence(__list,
"list")
618 ._M_iterator(__before,
"before"));
619 _GLIBCXX_DEBUG_VERIFY(__before._M_dereferenceable()
620 || __before._M_is_before_begin(),
621 _M_message(__gnu_debug::__msg_valid_range2)
622 ._M_sequence(__list,
"list")
623 ._M_iterator(__before,
"before")
624 ._M_iterator(__last,
"last"));
625 _GLIBCXX_DEBUG_VERIFY(__before != __last,
626 _M_message(__gnu_debug::__msg_valid_range2)
627 ._M_sequence(__list,
"list")
628 ._M_iterator(__before,
"before")
629 ._M_iterator(__last,
"last"));
630 _GLIBCXX_DEBUG_VERIFY(__list.get_allocator() == this->get_allocator(),
631 _M_message(__gnu_debug::__msg_splice_alloc)
633 ._M_sequence(__list,
"__list"));
635 for (_Base_const_iterator __tmp = std::next(__before.base());
636 __tmp != __last.base(); ++__tmp)
638 _GLIBCXX_DEBUG_VERIFY(__tmp != __list._M_base().end(),
639 _M_message(__gnu_debug::__msg_valid_range2)
640 ._M_sequence(__list,
"list")
641 ._M_iterator(__before,
"before")
642 ._M_iterator(__last,
"last"));
643 _GLIBCXX_DEBUG_VERIFY(__listptr !=
this || __tmp != __pos.base(),
644 _M_message(__gnu_debug::__msg_splice_overlap)
645 ._M_iterator(__tmp,
"position")
646 ._M_iterator(__before,
"before")
647 ._M_iterator(__last,
"last"));
650 this->_M_transfer_from_if(__list, [__tmp](_Base_const_iterator __it)
651 {
return __it == __tmp; });
654 _Base::splice_after(__pos.base(),
std::move(__list._M_base()),
655 __before.base(), __last.base());
659 splice_after(const_iterator __pos, forward_list& __list,
660 const_iterator __before, const_iterator __last)
661 { splice_after(__pos,
std::move(__list), __before, __last); }
664 #if __cplusplus > 201703L
665 using __remove_return_type = size_type;
666 # define _GLIBCXX_FWDLIST_REMOVE_RETURN_TYPE_TAG \
667 __attribute__((__abi_tag__("__cxx20")))
668 # define _GLIBCXX20_ONLY(__expr) __expr
670 using __remove_return_type = void;
671 # define _GLIBCXX_FWDLIST_REMOVE_RETURN_TYPE_TAG
672 # define _GLIBCXX20_ONLY(__expr)
676 _GLIBCXX_FWDLIST_REMOVE_RETURN_TYPE_TAG
678 remove(
const _Tp& __val)
680 if (!this->_M_iterators && !this->_M_const_iterators)
681 return _Base::remove(__val);
683 size_type __removed __attribute__((__unused__)) = 0;
684 _Base_iterator __x = _Base::before_begin();
685 _Base_iterator __old = __x++;
693 __x = _M_erase_after(__old);
694 _GLIBCXX20_ONLY( __removed++ );
705 this->_M_erase_after(__extra);
706 _GLIBCXX20_ONLY( __removed++ );
709 return _GLIBCXX20_ONLY( __removed );
712 template<
typename _Pred>
714 remove_if(_Pred __pred)
716 if (!this->_M_iterators && !this->_M_const_iterators)
717 return _Base::remove_if(__pred);
719 size_type __removed __attribute__((__unused__)) = 0;
720 _Base_iterator __x = _Base::before_begin();
721 _Base_iterator __old = __x++;
725 __x = _M_erase_after(__old);
726 _GLIBCXX20_ONLY( __removed++ );
731 return _GLIBCXX20_ONLY( __removed );
734 _GLIBCXX_FWDLIST_REMOVE_RETURN_TYPE_TAG
739 template<
typename _BinPred>
741 unique(_BinPred __binary_pred)
743 if (!this->_M_iterators && !this->_M_const_iterators)
744 return _Base::unique(__binary_pred);
748 if (__first == __last)
749 return _GLIBCXX20_ONLY(0);
751 size_type __removed __attribute__((__unused__)) = 0;
752 _Base_iterator __next = std::next(__first);
753 while (__next != __last)
755 if (__binary_pred(*__first, *__next))
757 __next = _M_erase_after(__first);
758 _GLIBCXX20_ONLY( __removed++ );
764 return _GLIBCXX20_ONLY( __removed );
767 #undef _GLIBCXX_FWDLIST_REMOVE_RETURN_TYPE_TAG
768 #undef _GLIBCXX20_ONLY
771 merge(forward_list&& __list)
776 __glibcxx_check_sorted(__list._M_base().begin(),
777 __list._M_base().end());
778 this->_M_transfer_from_if(__list, [&__list](_Base_const_iterator __it)
780 return __it != __list._M_base().cbefore_begin()
781 && __it != __list._M_base().cend();
783 _Base::merge(
std::move(__list._M_base()));
788 merge(forward_list& __list)
791 template<
typename _Comp>
793 merge(forward_list&& __list, _Comp __comp)
799 __list._M_base().end(), __comp);
800 this->_M_transfer_from_if(__list,
801 [&__list](_Base_const_iterator __it)
803 return __it != __list._M_base().cbefore_begin()
804 && __it != __list._M_base().cend();
806 _Base::merge(
std::move(__list._M_base()), __comp);
810 template<
typename _Comp>
812 merge(forward_list& __list, _Comp __comp)
816 using _Base::reverse;
819 _M_base() noexcept {
return *
this; }
822 _M_base() const noexcept {
return *
this; }
825 #if __cpp_deduction_guides >= 201606
826 template<
typename _InputIterator,
typename _ValT
827 =
typename iterator_traits<_InputIterator>::value_type,
828 typename _Allocator = allocator<_ValT>,
829 typename = _RequireInputIter<_InputIterator>,
830 typename = _RequireAllocator<_Allocator>>
831 forward_list(_InputIterator, _InputIterator, _Allocator = _Allocator())
832 -> forward_list<_ValT, _Allocator>;
835 template<
typename _Tp,
typename _Alloc>
837 operator==(
const forward_list<_Tp, _Alloc>& __lx,
838 const forward_list<_Tp, _Alloc>& __ly)
839 {
return __lx._M_base() == __ly._M_base(); }
841 #if __cpp_lib_three_way_comparison
842 template<
typename _Tp,
typename _Alloc>
843 constexpr __detail::__synth3way_t<_Tp>
844 operator<=>(
const forward_list<_Tp, _Alloc>& __x,
845 const forward_list<_Tp, _Alloc>& __y)
846 {
return __x._M_base() <=> __y._M_base(); }
848 template<
typename _Tp,
typename _Alloc>
850 operator<(const forward_list<_Tp, _Alloc>& __lx,
851 const forward_list<_Tp, _Alloc>& __ly)
852 {
return __lx._M_base() < __ly._M_base(); }
854 template<
typename _Tp,
typename _Alloc>
856 operator!=(
const forward_list<_Tp, _Alloc>& __lx,
857 const forward_list<_Tp, _Alloc>& __ly)
858 {
return !(__lx == __ly); }
861 template<
typename _Tp,
typename _Alloc>
865 {
return (__ly < __lx); }
868 template<
typename _Tp,
typename _Alloc>
872 {
return !(__lx < __ly); }
875 template<
typename _Tp,
typename _Alloc>
877 operator<=(const forward_list<_Tp, _Alloc>& __lx,
879 {
return !(__ly < __lx); }
880 #endif // three-way comparison
883 template<
typename _Tp,
typename _Alloc>
886 noexcept(noexcept(__lx.swap(__ly)))
892 namespace __gnu_debug
894 template<
typename _Tp,
typename _Alloc>
895 struct _BeforeBeginHelper<std::__debug::forward_list<_Tp, _Alloc> >
899 template<
typename _Iterator>
901 _S_Is(
const _Safe_iterator<_Iterator, _Sequence>& __it)
904 __it.base() == __it._M_get_sequence()->_M_base().before_begin();
907 template<
typename _Iterator>
909 _S_Is_Beginnest(
const _Safe_iterator<_Iterator, _Sequence>& __it)
910 {
return _S_Is(__it); }
913 template<
typename _Tp,
typename _Alloc>
914 struct _Sequence_traits<std::__debug::forward_list<_Tp, _Alloc> >
918 static typename _Distance_traits<_It>::__type
922 ? std::make_pair(0, __dp_exact) : std::make_pair(1, __dp_sign);
926 #ifndef _GLIBCXX_DEBUG_PEDANTIC
927 template<
class _Tp,
class _Alloc>
928 struct _Insert_range_from_self_is_safe<
929 std::__debug::forward_list<_Tp, _Alloc> >
930 {
enum { __value = 1 }; };
#define __glibcxx_check_sorted_pred(_First, _Last, _Pred)
_Tp * begin(valarray< _Tp > &__va)
Return an iterator pointing to the first element of the valarray.
#define __glibcxx_check_erase_range_after(_First, _Last)
One of the comparison functors.
Base class for constructing a safe sequence type that tracks iterators that reference it...
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
#define __glibcxx_check_erase_after(_Position)
constexpr auto cbegin(const _Container &__cont) noexcept(noexcept(std::begin(__cont))) -> decltype(std::begin(__cont))
Return an iterator pointing to the first element of the const container.
A standard container with linear time access to elements, and fixed time insertion/deletion at any po...
Special iterators swap and invalidation for forward_list because of the before_begin iterator...
_Safe_iterator_base * _M_next
void _M_invalidate_if(_Predicate __pred)
_Safe_sequence_base * _M_sequence
_T2 second
The second member.
_Safe_iterator_base * _M_prior
constexpr _Iterator __base(_Iterator __it)
Basic functionality for a safe iterator.
Class std::forward_list with safety/checking/debug instrumentation.
_Tp * end(valarray< _Tp > &__va)
Return an iterator pointing to one past the last element of the valarray.
Safe class dealing with some allocator dependent operations.
#define __glibcxx_check_insert_after(_Position)
#define __glibcxx_check_insert_range_after(_Position, _First, _Last, _Dist)
constexpr auto cend(const _Container &__cont) noexcept(noexcept(std::end(__cont))) -> decltype(std::end(__cont))
Return an iterator pointing to one past the last element of the const container.
Struct holding two objects of arbitrary type.
Base class that supports tracking of iterators that reference a sequence.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.