File indexing completed on 2026-05-03 08:13:52
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___ITERATOR_ADVANCE_H
0011 #define _LIBCPP___ITERATOR_ADVANCE_H
0012
0013 #include <__assert>
0014 #include <__concepts/assignable.h>
0015 #include <__concepts/same_as.h>
0016 #include <__config>
0017 #include <__iterator/concepts.h>
0018 #include <__iterator/incrementable_traits.h>
0019 #include <__iterator/iterator_traits.h>
0020 #include <__type_traits/enable_if.h>
0021 #include <__type_traits/is_integral.h>
0022 #include <__utility/convert_to_integral.h>
0023 #include <__utility/declval.h>
0024 #include <__utility/move.h>
0025 #include <__utility/unreachable.h>
0026 #include <limits>
0027
0028 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0029 # pragma GCC system_header
0030 #endif
0031
0032 _LIBCPP_PUSH_MACROS
0033 #include <__undef_macros>
0034
0035 _LIBCPP_BEGIN_NAMESPACE_STD
0036
0037 template <class _InputIter>
0038 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 void
0039 __advance(_InputIter& __i, typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag) {
0040 for (; __n > 0; --__n)
0041 ++__i;
0042 }
0043
0044 template <class _BiDirIter>
0045 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 void
0046 __advance(_BiDirIter& __i, typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag) {
0047 if (__n >= 0)
0048 for (; __n > 0; --__n)
0049 ++__i;
0050 else
0051 for (; __n < 0; ++__n)
0052 --__i;
0053 }
0054
0055 template <class _RandIter>
0056 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 void
0057 __advance(_RandIter& __i, typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag) {
0058 __i += __n;
0059 }
0060
0061 template < class _InputIter,
0062 class _Distance,
0063 class _IntegralDistance = decltype(std::__convert_to_integral(std::declval<_Distance>())),
0064 __enable_if_t<is_integral<_IntegralDistance>::value, int> = 0>
0065 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 void advance(_InputIter& __i, _Distance __orig_n) {
0066 typedef typename iterator_traits<_InputIter>::difference_type _Difference;
0067 _Difference __n = static_cast<_Difference>(std::__convert_to_integral(__orig_n));
0068
0069 _LIBCPP_ASSERT_PEDANTIC(__n >= 0 || __has_bidirectional_iterator_category<_InputIter>::value,
0070 "Attempt to advance(it, n) with negative n on a non-bidirectional iterator");
0071 std::__advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
0072 }
0073
0074 #if _LIBCPP_STD_VER >= 20
0075
0076
0077
0078 namespace ranges {
0079 struct __advance {
0080 private:
0081 template <class _Ip>
0082 _LIBCPP_HIDE_FROM_ABI static constexpr void __advance_forward(_Ip& __i, iter_difference_t<_Ip> __n) {
0083 while (__n > 0) {
0084 --__n;
0085 ++__i;
0086 }
0087 }
0088
0089 template <class _Ip>
0090 _LIBCPP_HIDE_FROM_ABI static constexpr void __advance_backward(_Ip& __i, iter_difference_t<_Ip> __n) {
0091 while (__n < 0) {
0092 ++__n;
0093 --__i;
0094 }
0095 }
0096
0097 public:
0098
0099 template <input_or_output_iterator _Ip>
0100 _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Ip& __i, iter_difference_t<_Ip> __n) const {
0101
0102 _LIBCPP_ASSERT_PEDANTIC(
0103 __n >= 0 || bidirectional_iterator<_Ip>, "If `n < 0`, then `bidirectional_iterator<I>` must be true.");
0104
0105
0106 if constexpr (random_access_iterator<_Ip>) {
0107 __i += __n;
0108 return;
0109 } else if constexpr (bidirectional_iterator<_Ip>) {
0110
0111 __advance_forward(__i, __n);
0112
0113 __advance_backward(__i, __n);
0114 return;
0115 } else {
0116
0117 __advance_forward(__i, __n);
0118 return;
0119 }
0120 }
0121
0122
0123
0124 template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
0125 _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Ip& __i, _Sp __bound_sentinel) const {
0126
0127 if constexpr (assignable_from<_Ip&, _Sp>) {
0128 __i = std::move(__bound_sentinel);
0129 }
0130
0131
0132 else if constexpr (sized_sentinel_for<_Sp, _Ip>) {
0133 (*this)(__i, __bound_sentinel - __i);
0134 }
0135
0136 else {
0137 while (__i != __bound_sentinel) {
0138 ++__i;
0139 }
0140 }
0141 }
0142
0143
0144
0145
0146
0147
0148
0149 template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
0150 _LIBCPP_HIDE_FROM_ABI constexpr iter_difference_t<_Ip>
0151 operator()(_Ip& __i, iter_difference_t<_Ip> __n, _Sp __bound_sentinel) const {
0152
0153 _LIBCPP_ASSERT_PEDANTIC((__n >= 0) || (bidirectional_iterator<_Ip> && same_as<_Ip, _Sp>),
0154 "If `n < 0`, then `bidirectional_iterator<I> && same_as<I, S>` must be true.");
0155
0156 if constexpr (sized_sentinel_for<_Sp, _Ip>) {
0157
0158
0159 auto __magnitude_geq = [](auto __a, auto __b) { return __a == 0 ? __b == 0 : __a > 0 ? __a >= __b : __a <= __b; };
0160 if (const auto __m = __bound_sentinel - __i; __magnitude_geq(__n, __m)) {
0161 (*this)(__i, __bound_sentinel);
0162 return __n - __m;
0163 }
0164
0165
0166 (*this)(__i, __n);
0167 return 0;
0168 } else {
0169
0170
0171 while (__n > 0 && __i != __bound_sentinel) {
0172 ++__i;
0173 --__n;
0174 }
0175
0176
0177 if constexpr (bidirectional_iterator<_Ip> && same_as<_Ip, _Sp>) {
0178 while (__n < 0 && __i != __bound_sentinel) {
0179 --__i;
0180 ++__n;
0181 }
0182 }
0183 return __n;
0184 }
0185
0186 __libcpp_unreachable();
0187 }
0188 };
0189
0190 inline namespace __cpo {
0191 inline constexpr auto advance = __advance{};
0192 }
0193 }
0194
0195 #endif
0196
0197 _LIBCPP_END_NAMESPACE_STD
0198
0199 _LIBCPP_POP_MACROS
0200
0201 #endif