diff --git a/libcxx/include/__iterator/advance.h b/libcxx/include/__iterator/advance.h --- a/libcxx/include/__iterator/advance.h +++ b/libcxx/include/__iterator/advance.h @@ -67,126 +67,130 @@ #if !defined(_LIBCPP_HAS_NO_RANGES) -namespace ranges { // [range.iter.op.advance] -// TODO(varconst): rename `__advance_fn` to `__fn`. -struct __advance_fn final : private __function_like { -private: - template - _LIBCPP_HIDE_FROM_ABI - static constexpr _Tp __magnitude_geq(_Tp __a, _Tp __b) noexcept { - return __a < 0 ? (__a <= __b) : (__a >= __b); - } - - template - _LIBCPP_HIDE_FROM_ABI - static constexpr void __advance_forward(_Ip& __i, iter_difference_t<_Ip> __n) { - while (__n > 0) { - --__n; - ++__i; - } - } - template - _LIBCPP_HIDE_FROM_ABI - static constexpr void __advance_backward(_Ip& __i, iter_difference_t<_Ip> __n) { - while (__n < 0) { - ++__n; - --__i; - } - } - -public: - constexpr explicit __advance_fn(__tag __x) noexcept : __function_like(__x) {} - - // Preconditions: If `I` does not model `bidirectional_iterator`, `n` is not negative. - template - _LIBCPP_HIDE_FROM_ABI - constexpr void operator()(_Ip& __i, iter_difference_t<_Ip> __n) const { - _LIBCPP_ASSERT(__n >= 0 || bidirectional_iterator<_Ip>, - "If `n < 0`, then `bidirectional_iterator` must be true."); - - // If `I` models `random_access_iterator`, equivalent to `i += n`. - if constexpr (random_access_iterator<_Ip>) { - __i += __n; - return; - } else if constexpr (bidirectional_iterator<_Ip>) { - // Otherwise, if `n` is non-negative, increments `i` by `n`. - __advance_forward(__i, __n); - // Otherwise, decrements `i` by `-n`. - __advance_backward(__i, __n); - return; - } else { - // Otherwise, if `n` is non-negative, increments `i` by `n`. - __advance_forward(__i, __n); - return; - } - } - - // Preconditions: Either `assignable_from || sized_sentinel_for` is modeled, or [i, bound) denotes a range. - template _Sp> - _LIBCPP_HIDE_FROM_ABI - constexpr void operator()(_Ip& __i, _Sp __bound) const { - // If `I` and `S` model `assignable_from`, equivalent to `i = std::move(bound)`. - if constexpr (assignable_from<_Ip&, _Sp>) { - __i = _VSTD::move(__bound); - } - // Otherwise, if `S` and `I` model `sized_sentinel_for`, equivalent to `ranges::advance(i, bound - i)`. - else if constexpr (sized_sentinel_for<_Sp, _Ip>) { - (*this)(__i, __bound - __i); +namespace ranges { +namespace __advance { + struct __fn final : private __function_like { + private: + template + _LIBCPP_HIDE_FROM_ABI + static constexpr _Tp __magnitude_geq(_Tp __a, _Tp __b) noexcept { + return __a < 0 ? (__a <= __b) : (__a >= __b); } - // Otherwise, while `bool(i != bound)` is true, increments `i`. - else { - while (__i != __bound) { + + template + _LIBCPP_HIDE_FROM_ABI + static constexpr void __advance_forward(_Ip& __i, iter_difference_t<_Ip> __n) { + while (__n > 0) { + --__n; ++__i; } } - } - - // Preconditions: - // * If `n > 0`, [i, bound) denotes a range. - // * If `n == 0`, [i, bound) or [bound, i) denotes a range. - // * If `n < 0`, [bound, i) denotes a range, `I` models `bidirectional_iterator`, and `I` and `S` model `same_as`. - // Returns: `n - M`, where `M` is the difference between the the ending and starting position. - template _Sp> - _LIBCPP_HIDE_FROM_ABI - constexpr iter_difference_t<_Ip> operator()(_Ip& __i, iter_difference_t<_Ip> __n, _Sp __bound) const { - _LIBCPP_ASSERT((__n >= 0) || (bidirectional_iterator<_Ip> && same_as<_Ip, _Sp>), - "If `n < 0`, then `bidirectional_iterator && same_as` must be true."); - // If `S` and `I` model `sized_sentinel_for`: - if constexpr (sized_sentinel_for<_Sp, _Ip>) { - // If |n| >= |bound - i|, equivalent to `ranges::advance(i, bound)`. - if (const auto __M = __bound - __i; __magnitude_geq(__n, __M)) { - (*this)(__i, __bound); - return __n - __M; + + template + _LIBCPP_HIDE_FROM_ABI + static constexpr void __advance_backward(_Ip& __i, iter_difference_t<_Ip> __n) { + while (__n < 0) { + ++__n; + --__i; } + } - // Otherwise, equivalent to `ranges::advance(i, n)`. - (*this)(__i, __n); - return 0; - } else { - // Otherwise, if `n` is non-negative, while `bool(i != bound)` is true, increments `i` but at - // most `n` times. - while (__i != __bound && __n > 0) { - ++__i; - --__n; + public: + constexpr explicit __fn(__tag __x) noexcept : __function_like(__x) {} + + // Preconditions: If `I` does not model `bidirectional_iterator`, `n` is not negative. + template + _LIBCPP_HIDE_FROM_ABI + constexpr void operator()(_Ip& __i, iter_difference_t<_Ip> __n) const { + _LIBCPP_ASSERT(__n >= 0 || bidirectional_iterator<_Ip>, + "If `n < 0`, then `bidirectional_iterator` must be true."); + + // If `I` models `random_access_iterator`, equivalent to `i += n`. + if constexpr (random_access_iterator<_Ip>) { + __i += __n; + return; + } else if constexpr (bidirectional_iterator<_Ip>) { + // Otherwise, if `n` is non-negative, increments `i` by `n`. + __advance_forward(__i, __n); + // Otherwise, decrements `i` by `-n`. + __advance_backward(__i, __n); + return; + } else { + // Otherwise, if `n` is non-negative, increments `i` by `n`. + __advance_forward(__i, __n); + return; } + } - // Otherwise, while `bool(i != bound)` is true, decrements `i` but at most `-n` times. - if constexpr (bidirectional_iterator<_Ip> && same_as<_Ip, _Sp>) { - while (__i != __bound && __n < 0) { - --__i; - ++__n; + // Preconditions: Either `assignable_from || sized_sentinel_for` is modeled, or [i, bound) denotes a range. + template _Sp> + _LIBCPP_HIDE_FROM_ABI + constexpr void operator()(_Ip& __i, _Sp __bound) const { + // If `I` and `S` model `assignable_from`, equivalent to `i = std::move(bound)`. + if constexpr (assignable_from<_Ip&, _Sp>) { + __i = _VSTD::move(__bound); + } + // Otherwise, if `S` and `I` model `sized_sentinel_for`, equivalent to `ranges::advance(i, bound - i)`. + else if constexpr (sized_sentinel_for<_Sp, _Ip>) { + (*this)(__i, __bound - __i); + } + // Otherwise, while `bool(i != bound)` is true, increments `i`. + else { + while (__i != __bound) { + ++__i; } } - return __n; } - _LIBCPP_UNREACHABLE(); - } -}; + // Preconditions: + // * If `n > 0`, [i, bound) denotes a range. + // * If `n == 0`, [i, bound) or [bound, i) denotes a range. + // * If `n < 0`, [bound, i) denotes a range, `I` models `bidirectional_iterator`, and `I` and `S` model `same_as`. + // Returns: `n - M`, where `M` is the difference between the the ending and starting position. + template _Sp> + _LIBCPP_HIDE_FROM_ABI + constexpr iter_difference_t<_Ip> operator()(_Ip& __i, iter_difference_t<_Ip> __n, _Sp __bound) const { + _LIBCPP_ASSERT((__n >= 0) || (bidirectional_iterator<_Ip> && same_as<_Ip, _Sp>), + "If `n < 0`, then `bidirectional_iterator && same_as` must be true."); + // If `S` and `I` model `sized_sentinel_for`: + if constexpr (sized_sentinel_for<_Sp, _Ip>) { + // If |n| >= |bound - i|, equivalent to `ranges::advance(i, bound)`. + if (const auto __M = __bound - __i; __magnitude_geq(__n, __M)) { + (*this)(__i, __bound); + return __n - __M; + } + + // Otherwise, equivalent to `ranges::advance(i, n)`. + (*this)(__i, __n); + return 0; + } else { + // Otherwise, if `n` is non-negative, while `bool(i != bound)` is true, increments `i` but at + // most `n` times. + while (__i != __bound && __n > 0) { + ++__i; + --__n; + } + + // Otherwise, while `bool(i != bound)` is true, decrements `i` but at most `-n` times. + if constexpr (bidirectional_iterator<_Ip> && same_as<_Ip, _Sp>) { + while (__i != __bound && __n < 0) { + --__i; + ++__n; + } + } + return __n; + } + + _LIBCPP_UNREACHABLE(); + } + }; +} -inline constexpr auto advance = __advance_fn(__function_like::__tag()); +inline namespace __cpo { + inline constexpr auto advance = __advance::__fn(__function_like::__tag()); +} // namespace __cpo } // namespace ranges #endif // !defined(_LIBCPP_HAS_NO_RANGES) diff --git a/libcxx/include/__iterator/next.h b/libcxx/include/__iterator/next.h --- a/libcxx/include/__iterator/next.h +++ b/libcxx/include/__iterator/next.h @@ -38,42 +38,47 @@ #if !defined(_LIBCPP_HAS_NO_RANGES) +// [range.iter.op.next] + namespace ranges { -// TODO(varconst): rename `__next_fn` to `__fn`. -struct __next_fn final : private __function_like { - _LIBCPP_HIDE_FROM_ABI - constexpr explicit __next_fn(__tag __x) noexcept : __function_like(__x) {} - - template - _LIBCPP_HIDE_FROM_ABI - constexpr _Ip operator()(_Ip __x) const { - ++__x; - return __x; - } - - template - _LIBCPP_HIDE_FROM_ABI - constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n) const { - ranges::advance(__x, __n); - return __x; - } - - template _Sp> - _LIBCPP_HIDE_FROM_ABI - constexpr _Ip operator()(_Ip __x, _Sp __bound) const { - ranges::advance(__x, __bound); - return __x; - } - - template _Sp> - _LIBCPP_HIDE_FROM_ABI - constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n, _Sp __bound) const { - ranges::advance(__x, __n, __bound); - return __x; - } -}; - -inline constexpr auto next = __next_fn(__function_like::__tag()); +namespace __next { + struct __fn final : private __function_like { + _LIBCPP_HIDE_FROM_ABI + constexpr explicit __fn(__tag __x) noexcept : __function_like(__x) {} + + template + _LIBCPP_HIDE_FROM_ABI + constexpr _Ip operator()(_Ip __x) const { + ++__x; + return __x; + } + + template + _LIBCPP_HIDE_FROM_ABI + constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n) const { + ranges::advance(__x, __n); + return __x; + } + + template _Sp> + _LIBCPP_HIDE_FROM_ABI + constexpr _Ip operator()(_Ip __x, _Sp __bound) const { + ranges::advance(__x, __bound); + return __x; + } + + template _Sp> + _LIBCPP_HIDE_FROM_ABI + constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n, _Sp __bound) const { + ranges::advance(__x, __n, __bound); + return __x; + } + }; +} + +inline namespace __cpo { + inline constexpr auto next = __next::__fn(__function_like::__tag()); +} // namespace __cpo } // namespace ranges #endif // !defined(_LIBCPP_HAS_NO_RANGES) diff --git a/libcxx/include/__iterator/prev.h b/libcxx/include/__iterator/prev.h --- a/libcxx/include/__iterator/prev.h +++ b/libcxx/include/__iterator/prev.h @@ -37,35 +37,40 @@ #if !defined(_LIBCPP_HAS_NO_RANGES) +// [range.iter.op.prev] + namespace ranges { -// TODO(varconst): rename `__prev_fn` to `__fn`. -struct __prev_fn final : private __function_like { - _LIBCPP_HIDE_FROM_ABI - constexpr explicit __prev_fn(__tag __x) noexcept : __function_like(__x) {} +namespace __prev { + struct __fn final : private __function_like { + _LIBCPP_HIDE_FROM_ABI + constexpr explicit __fn(__tag __x) noexcept : __function_like(__x) {} - template - _LIBCPP_HIDE_FROM_ABI - constexpr _Ip operator()(_Ip __x) const { - --__x; - return __x; - } + template + _LIBCPP_HIDE_FROM_ABI + constexpr _Ip operator()(_Ip __x) const { + --__x; + return __x; + } - template - _LIBCPP_HIDE_FROM_ABI - constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n) const { - ranges::advance(__x, -__n); - return __x; - } + template + _LIBCPP_HIDE_FROM_ABI + constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n) const { + ranges::advance(__x, -__n); + return __x; + } - template - _LIBCPP_HIDE_FROM_ABI - constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n, _Ip __bound) const { - ranges::advance(__x, -__n, __bound); - return __x; - } -}; + template + _LIBCPP_HIDE_FROM_ABI + constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n, _Ip __bound) const { + ranges::advance(__x, -__n, __bound); + return __x; + } + }; +} -inline constexpr auto prev = __prev_fn(__function_like::__tag()); +inline namespace __cpo { + inline constexpr auto prev = __prev::__fn(__function_like::__tag()); +} // namespace __cpo } // namespace ranges #endif // !defined(_LIBCPP_HAS_NO_RANGES)