File indexing completed on 2026-05-03 08:13:32
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___CXX03___ITERATOR_ADVANCE_H
0011 #define _LIBCPP___CXX03___ITERATOR_ADVANCE_H
0012
0013 #include <__cxx03/__assert>
0014 #include <__cxx03/__concepts/assignable.h>
0015 #include <__cxx03/__concepts/same_as.h>
0016 #include <__cxx03/__config>
0017 #include <__cxx03/__iterator/concepts.h>
0018 #include <__cxx03/__iterator/incrementable_traits.h>
0019 #include <__cxx03/__iterator/iterator_traits.h>
0020 #include <__cxx03/__type_traits/enable_if.h>
0021 #include <__cxx03/__type_traits/is_integral.h>
0022 #include <__cxx03/__utility/convert_to_integral.h>
0023 #include <__cxx03/__utility/declval.h>
0024 #include <__cxx03/__utility/move.h>
0025 #include <__cxx03/__utility/unreachable.h>
0026 #include <__cxx03/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 <__cxx03/__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 namespace __advance {
0080
0081 struct __fn {
0082 private:
0083 template <class _Ip>
0084 _LIBCPP_HIDE_FROM_ABI static constexpr void __advance_forward(_Ip& __i, iter_difference_t<_Ip> __n) {
0085 while (__n > 0) {
0086 --__n;
0087 ++__i;
0088 }
0089 }
0090
0091 template <class _Ip>
0092 _LIBCPP_HIDE_FROM_ABI static constexpr void __advance_backward(_Ip& __i, iter_difference_t<_Ip> __n) {
0093 while (__n < 0) {
0094 ++__n;
0095 --__i;
0096 }
0097 }
0098
0099 public:
0100
0101 template <input_or_output_iterator _Ip>
0102 _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Ip& __i, iter_difference_t<_Ip> __n) const {
0103
0104 _LIBCPP_ASSERT_PEDANTIC(
0105 __n >= 0 || bidirectional_iterator<_Ip>, "If `n < 0`, then `bidirectional_iterator<I>` must be true.");
0106
0107
0108 if constexpr (random_access_iterator<_Ip>) {
0109 __i += __n;
0110 return;
0111 } else if constexpr (bidirectional_iterator<_Ip>) {
0112
0113 __advance_forward(__i, __n);
0114
0115 __advance_backward(__i, __n);
0116 return;
0117 } else {
0118
0119 __advance_forward(__i, __n);
0120 return;
0121 }
0122 }
0123
0124
0125
0126 template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
0127 _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Ip& __i, _Sp __bound_sentinel) const {
0128
0129 if constexpr (assignable_from<_Ip&, _Sp>) {
0130 __i = std::move(__bound_sentinel);
0131 }
0132
0133
0134 else if constexpr (sized_sentinel_for<_Sp, _Ip>) {
0135 (*this)(__i, __bound_sentinel - __i);
0136 }
0137
0138 else {
0139 while (__i != __bound_sentinel) {
0140 ++__i;
0141 }
0142 }
0143 }
0144
0145
0146
0147
0148
0149
0150
0151 template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
0152 _LIBCPP_HIDE_FROM_ABI constexpr iter_difference_t<_Ip>
0153 operator()(_Ip& __i, iter_difference_t<_Ip> __n, _Sp __bound_sentinel) const {
0154
0155 _LIBCPP_ASSERT_PEDANTIC((__n >= 0) || (bidirectional_iterator<_Ip> && same_as<_Ip, _Sp>),
0156 "If `n < 0`, then `bidirectional_iterator<I> && same_as<I, S>` must be true.");
0157
0158 if constexpr (sized_sentinel_for<_Sp, _Ip>) {
0159
0160
0161 auto __magnitude_geq = [](auto __a, auto __b) { return __a == 0 ? __b == 0 : __a > 0 ? __a >= __b : __a <= __b; };
0162 if (const auto __m = __bound_sentinel - __i; __magnitude_geq(__n, __m)) {
0163 (*this)(__i, __bound_sentinel);
0164 return __n - __m;
0165 }
0166
0167
0168 (*this)(__i, __n);
0169 return 0;
0170 } else {
0171
0172
0173 while (__n > 0 && __i != __bound_sentinel) {
0174 ++__i;
0175 --__n;
0176 }
0177
0178
0179 if constexpr (bidirectional_iterator<_Ip> && same_as<_Ip, _Sp>) {
0180 while (__n < 0 && __i != __bound_sentinel) {
0181 --__i;
0182 ++__n;
0183 }
0184 }
0185 return __n;
0186 }
0187
0188 __libcpp_unreachable();
0189 }
0190 };
0191
0192 }
0193
0194 inline namespace __cpo {
0195 inline constexpr auto advance = __advance::__fn{};
0196 }
0197 }
0198
0199 #endif
0200
0201 _LIBCPP_END_NAMESPACE_STD
0202
0203 _LIBCPP_POP_MACROS
0204
0205 #endif