diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -15,6 +15,8 @@ __iterator/incrementable_traits.h __iterator/iter_move.h __iterator/iterator_traits.h + __iterator/functionish.h + __iterator/primitives.h __iterator/readable_traits.h __libcpp_version __locale diff --git a/libcxx/include/__iterator/functionish.h b/libcxx/include/__iterator/functionish.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__iterator/functionish.h @@ -0,0 +1,54 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___ITERATOR_FUNCTIONISH_H +#define _LIBCPP___ITERATOR_FUNCTIONISH_H + +#include <__config> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_PUSH_MACROS +#include <__undef_macros> + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if !defined(_LIBCPP_HAS_NO_RANGES) + +namespace ranges { +// Per [range.iter.ops.general] and [algorithms.requirements], functions in namespace std::ranges +// can't be found by ADL and inhibit ADL when found by unqualified lookup. The easiest way to +// facilitate this is to use function objects. +// +// Since these are still standard library functions, we use `__functionish` to eliminate most of +// the properties that function objects get by default (e.g. semiregularity, addressability), to +// limit the surface area of the unintended public interface, so as to curb the effect of Hyrum's +// law. +struct __functionish { + __functionish() = delete; + __functionish(__functionish const&) = delete; + __functionish& operator=(__functionish const&) = delete; + + void operator&() const = delete; + + enum __tag { __value }; + +protected: + constexpr explicit __functionish(__tag) noexcept {} + ~__functionish() = default; +}; +} // namespace ranges + +#endif // !defined(_LIBCPP_HAS_NO_RANGES) + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___ITERATOR_FUNCTIONISH_H diff --git a/libcxx/include/__iterator/primitives.h b/libcxx/include/__iterator/primitives.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__iterator/primitives.h @@ -0,0 +1,139 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___ITERATOR_PRIMITIVES_H +#define _LIBCPP___ITERATOR_PRIMITIVES_H + +#include <__config> +#include <__debug> +#include <__iterator/concepts.h> +#include <__iterator/functionish.h> +#include <__iterator/incrementable_traits.h> +#include +#include +#include + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_PUSH_MACROS +#include <__undef_macros> + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if !defined(_LIBCPP_HAS_NO_RANGES) + +namespace ranges { +// [range.iter.op.advance] +struct __advance_fn final : __functionish { +private: + template + [[nodiscard]] static constexpr auto __abs(_Tp const __n) noexcept { + _LIBCPP_ASSERT(__n != numeric_limits<_Tp>::min(), "`-numeric_limits::min()` is undefined"); + return __n < 0 ? -__n : __n; + } + +public: + constexpr explicit __advance_fn(__tag __x) noexcept : __functionish(__x) {} + + // Preconditions: If `I` does not model `bidirectional_iterator`, `n` is not negative. + template + 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 { + // Otherwise, if `n` is non-negative, increments `i` by `n`. + while (__n > 0) { + --__n; + ++__i; + } + + // Otherwise, decrements `i` by `-n`. + if constexpr (bidirectional_iterator<_Ip>) { + while (__n < 0) { + ++__n; + --__i; + } + } + } + } + + // Preconditions: Either `assignable_from || sized_sentinel_for` is modeled, or [i, bound) denotes a range. + template _Sp> + 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 = std::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; + } + } + } + + // 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> + [[nodiscard]] 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; __abs(__n) >= __abs(__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; + } + } +}; + +inline constexpr auto advance = __advance_fn(__functionish::__value); +} // namespace ranges + +#endif // !defined(_LIBCPP_HAS_NO_RANGES) + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___ITERATOR_PRIMITIVES_H diff --git a/libcxx/include/iterator b/libcxx/include/iterator --- a/libcxx/include/iterator +++ b/libcxx/include/iterator @@ -476,6 +476,7 @@ #include <__iterator/incrementable_traits.h> #include <__iterator/iter_move.h> #include <__iterator/iterator_traits.h> +#include <__iterator/primitives.h> #include <__iterator/readable_traits.h> #include <__memory/addressof.h> #include <__memory/pointer_traits.h> diff --git a/libcxx/test/std/iterators/iterator.primitives/range.iter.ops/range.iter.ops.advance/advance.pass.cpp b/libcxx/test/std/iterators/iterator.primitives/range.iter.ops/range.iter.ops.advance/advance.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/iterators/iterator.primitives/range.iter.ops/range.iter.ops.advance/advance.pass.cpp @@ -0,0 +1,258 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// ranges::advance + +#define _LIBCPP_DEBUG_LEVEL 2 +#include + +#include +#include + +#include "test_iterators.h" + +using range_t = std::array; +constexpr auto range = range_t{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + +template +constexpr void check_round_trip(stride_counting_iterator const& i, std::ptrdiff_t const n) { + auto const distance = n < 0 ? -n : n; + assert(i.distance_traversed() == distance); + assert(i.displacement() == n); +} + +template +constexpr void check_round_trip(stride_counting_iterator const& i, std::ptrdiff_t const n) { + assert(i.distance_traversed() <= 1); + assert(i.displacement() == n < 0 ? -1 : 1); +} + +namespace iterator_count { +template +constexpr void check_move_forward(std::ptrdiff_t const n) { + auto first = stride_counting_iterator(I(range.begin())); + std::ranges::advance(first, n); + assert(std::move(first).base().base() == range.begin() + n); + check_round_trip(first, n); +} + +template +constexpr void check_move_backward(std::ptrdiff_t const n) { + auto first = stride_counting_iterator(I(range.begin() + n)); + std::ranges::advance(first, -n); + assert(std::move(first).base().base() == range.begin()); + check_round_trip(first, -n); +} + +[[nodiscard]] constexpr bool test() { + check_move_forward >(1); + check_move_forward >(2); + check_move_forward >(3); + check_move_forward >(4); + check_move_forward >(5); + check_move_forward >(6); + + check_move_backward >(4); + check_move_backward >(5); + check_move_backward >(6); + + // Zero should be checked for each case and each overload + check_move_forward::const_iterator> >(0); + check_move_forward::const_iterator> >(0); + check_move_backward::const_iterator> >(0); + check_move_backward::const_iterator> >(0); + + return true; +} +} // namespace iterator_count + +class distance_apriori_sentinel { +public: + distance_apriori_sentinel() = default; + constexpr explicit distance_apriori_sentinel(std::ptrdiff_t const count) : count_(count) {} + + [[nodiscard]] constexpr bool operator==(std::input_iterator auto const&) const { + assert(false && "difference op should take precedence"); + return false; + } + + [[nodiscard]] constexpr friend std::ptrdiff_t operator-(std::input_iterator auto const&, + distance_apriori_sentinel const y) { + return -y.count_; + } + + [[nodiscard]] constexpr friend std::ptrdiff_t operator-(distance_apriori_sentinel const x, + std::input_iterator auto const&) { + return x.count_; + } + +private: + std::ptrdiff_t count_ = 0; +}; + +template +class sentinel_wrapper { +public: + sentinel_wrapper() = default; + constexpr explicit sentinel_wrapper(I base) : base_(std::move(base)) {} + + [[nodiscard]] constexpr bool operator==(I const& other) const requires std::equality_comparable { + return base_ == other; + } + +private: + I base_ = I(); +}; + +namespace iterator_sentinel { +template S = I> +constexpr void check_assignable_case(std::ptrdiff_t const n) { + auto first = stride_counting_iterator(I(range.begin())); + std::ranges::advance(first, stride_counting_iterator(S(I(range.begin() + n)))); + assert(std::move(first).base().base() == range.begin() + n); + assert(first.distance_traversed() == 0); // always zero, so don't use `check_round_trip` +} + +template +constexpr void check_sized_sentinel_case(std::ptrdiff_t const n) { + auto first = stride_counting_iterator(I(range.begin())); + std::ranges::advance(first, distance_apriori_sentinel(n)); + assert(std::move(first).base().base() == range.begin() + n); + check_round_trip(first, n); +} + +template +constexpr void check_sentinel_case(std::ptrdiff_t const n) { + auto first = stride_counting_iterator(I(range.begin())); + auto const last = I(range.begin() + n); + std::ranges::advance(first, sentinel_wrapper(last)); + assert(first.base() == last); + assert(first.distance_traversed() == n); // always `n`, so don't use `check_round_trip` +} + +[[nodiscard]] constexpr bool test() { + check_assignable_case >(1); + check_assignable_case >(3); + check_assignable_case >(4); + check_assignable_case >(5); + check_assignable_case >(6); + + check_sized_sentinel_case >(1); + check_sized_sentinel_case >(2); + check_sized_sentinel_case >(3); + check_sized_sentinel_case >(4); + check_sized_sentinel_case >(5); + check_sized_sentinel_case >(6); + + check_sentinel_case >(1); + // cpp20_input_iterator not copyable, so is omitted + check_sentinel_case >(3); + check_sentinel_case >(4); + check_sentinel_case >(5); + check_sentinel_case >(6); + return true; +} +} // namespace iterator_sentinel + +namespace iterator_count_sentinel { +struct expected_t { + range_t::const_iterator coordinate; + std::ptrdiff_t result; +}; + +template +constexpr void check_forward_sized_sentinel_case(std::ptrdiff_t const n, expected_t const expected) { + auto current = stride_counting_iterator(I(range.begin())); + auto const result = std::ranges::advance(current, n, distance_apriori_sentinel(range.size())); + assert(current.base().base() == expected.coordinate); + assert(result == expected.result); + check_round_trip(current, n - expected.result); +} + +template +constexpr void check_backward_sized_sentinel_case(std::ptrdiff_t const n, expected_t const expected) { + auto current = stride_counting_iterator(I(range.end())); + auto const result = std::ranges::advance(current, -n, stride_counting_iterator(I(range.begin()))); + assert(current.base().base() == expected.coordinate); + assert(result == expected.result); + check_round_trip(current, n - expected.result); +} + +template +constexpr void check_forward_case(std::ptrdiff_t const n, expected_t const expected) { + auto current = stride_counting_iterator(I(range.begin())); + auto const result = std::ranges::advance(current, n, sentinel_wrapper(I(range.end()))); + assert(current.base().base() == expected.coordinate); + assert(result == expected.result); + assert(current.distance_traversed() == n - expected.result); +} + +template +constexpr void check_backward_case(std::ptrdiff_t const n, expected_t const expected) { + auto current = stride_counting_iterator(I(range.end())); + auto const result = std::ranges::advance(current, -n, stride_counting_iterator(I(range.begin()))); + assert(current.base().base() == expected.coordinate); + assert(result == expected.result); + assert(current.distance_traversed() == n + expected.result); + assert(current.distance_traversed() == -current.displacement()); +} + +[[nodiscard]] constexpr bool test() { + check_forward_sized_sentinel_case >(1, {range.begin() + 1, 0}); + check_forward_sized_sentinel_case >(3, {range.begin() + 3, 0}); + check_forward_sized_sentinel_case >(4, {range.begin() + 4, 0}); + check_forward_sized_sentinel_case >(5, {range.begin() + 5, 0}); + check_forward_sized_sentinel_case >(6, {range.begin() + 6, 0}); + + // bidirectional_iterator omitted because `n < 0` case requires `same_as` + check_backward_sized_sentinel_case >(5, {range.begin() + 5, 0}); + check_backward_sized_sentinel_case >(6, {range.begin() + 4, 0}); + + // disntance == range.size() + check_forward_sized_sentinel_case >(10, {range.end(), 0}); + check_backward_sized_sentinel_case >(10, {range.begin(), 0}); + + // distance > range.size() + check_forward_sized_sentinel_case >(1000, {range.end(), 990}); + check_backward_sized_sentinel_case >(1000, {range.begin(), -990}); + + check_forward_case >(1, {range.begin() + 1, 0}); + check_forward_case >(3, {range.begin() + 3, 0}); + check_forward_case >(4, {range.begin() + 4, 0}); + check_forward_case >(5, {range.begin() + 5, 0}); + check_forward_case >(6, {range.begin() + 6, 0}); + check_backward_case >(7, {range.begin() + 3, 0}); + + // disntance == range.size() + check_forward_case >(10, {range.end(), 0}); + check_backward_case >(10, {range.begin(), 0}); + + // distance > range.size() + check_forward_case >(1000, {range.end(), 990}); + check_backward_case >(1000, {range.begin(), -990}); + + return true; +} +} // namespace iterator_count_sentinel + +int main(int, char**) { + static_assert(iterator_count::test()); + assert(iterator_count::test()); + + static_assert(iterator_sentinel::test()); + assert(iterator_sentinel::test()); + + static_assert(iterator_count_sentinel::test()); + assert(iterator_count_sentinel::test()); + + return 0; +} diff --git a/libcxx/test/std/iterators/iterator.primitives/range.iter.ops/range.iter.ops.advance/advance.verify.cpp b/libcxx/test/std/iterators/iterator.primitives/range.iter.ops/range.iter.ops.advance/advance.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/iterators/iterator.primitives/range.iter.ops/range.iter.ops.advance/advance.verify.cpp @@ -0,0 +1,95 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// ranges::advance + +#include + +namespace std::ranges { +class forward_iterator { +public: + using value_type = int; + using difference_type = std::ptrdiff_t; + using iterator_category = std::forward_iterator_tag; + + forward_iterator() = default; + + value_type operator*() const; + forward_iterator& operator++(); + forward_iterator operator++(int); + + bool operator==(forward_iterator const&) const = default; +}; +} // namespace std::ranges + +// The function templates defined in [range.iter.ops] are not found by argument-dependent name lookup ([basic.lookup.argdep]). +void no_adl_participation() { + std::ranges::forward_iterator x; + advance(x, 0); // expected-error{{use of undeclared identifier 'advance'}} + advance(x, x); // expected-error {{use of undeclared identifier 'advance'}} + // expected-error@*:*{{no matching function for call to '__convert_to_integral'}} + advance(x, 0, x); // expected-error {{use of undeclared identifier 'advance'}} +} + +namespace test { +template +class forward_iterator { +public: + using value_type = int; + using difference_type = std::ptrdiff_t; + using iterator_category = std::forward_iterator_tag; + + forward_iterator() = default; + + value_type operator*() const; + forward_iterator& operator++(); + forward_iterator operator++(int); + + bool operator==(forward_iterator const&) const = default; +}; + +template +void advance(forward_iterator&, std::ptrdiff_t) { + static_assert(std::same_as); +} + +template +void advance(forward_iterator&, forward_iterator) { + static_assert(std::same_as); +} + +template +void advance(forward_iterator&, std::ptrdiff_t, forward_iterator) { + static_assert(std::same_as); +} +} // namespace test + +// When found by unqualified ([basic.lookup.unqual]) name lookup for the postfix-expression in a +// function call ([expr.call]), they inhibit argument-dependent name lookup. +void adl_inhibition() { + test::forward_iterator x; + + using std::ranges::advance; + advance(x, 0); + advance(x, x); + (void)advance(x, 0, x); +} + +void standard_function_properties() { + &std::ranges::advance; // expected-error {{overload resolution selected deleted operator '&'}} + auto move = std::move(std::ranges::advance); + // expected-error@-1{{call to implicitly-deleted copy constructor of 'std::__1::ranges::__advance_fn'}} + auto copy = std::ranges::advance; + // expected-error@-1{{call to implicitly-deleted copy constructor of 'std::__1::ranges::__advance_fn'}} + decltype(std::ranges::advance) x; // expected-error {{}} + struct advance2 : decltype(std::ranges::advance) {}; // expected-error {{base '__advance_fn' is marked 'final'}} +} diff --git a/libcxx/test/support/test_iterators.h b/libcxx/test/support/test_iterators.h --- a/libcxx/test/support/test_iterators.h +++ b/libcxx/test/support/test_iterators.h @@ -636,6 +636,8 @@ #ifdef TEST_SUPPORTS_RANGES +// clang-format off + template struct cpp20_input_iterator { using value_type = std::iter_value_t; @@ -654,21 +656,198 @@ constexpr decltype(auto) operator*() const { return *base_; } - cpp20_input_iterator& operator++() { + constexpr cpp20_input_iterator& operator++() { ++base_; return *this; } - void operator++(int) { ++base_; } + constexpr void operator++(int) { ++base_; } - [[nodiscard]] I const& base() const& { return base_; } + [[nodiscard]] constexpr I const& base() const& { return base_; } - [[nodiscard]] I base() && { return std::move(base_); } + [[nodiscard]] constexpr I base() && { return std::move(base_); } private: I base_ = I(); }; +template +struct iterator_concept { + using type = std::input_iterator_tag; +}; + +template +struct iterator_concept { + using type = std::forward_iterator_tag; +}; + +template +struct iterator_concept { + using type = std::bidirectional_iterator_tag; +}; + +template +struct iterator_concept { + using type = std::random_access_iterator_tag; +}; + +// TODO: uncomment once contiguous_iterator is merged. +// template +// struct iterator_concept { +// using type = std::contiguous_iterator_tag; +// }; + +template +using iterator_concept_t = typename iterator_concept::type; + +template +class stride_counting_iterator { +public: + using value_type = std::iter_value_t; + using difference_type = std::iter_difference_t; + using iterator_concept = iterator_concept_t; + + stride_counting_iterator() = default; + + constexpr explicit stride_counting_iterator(I current) : base_(std::move(current)) {} + + [[nodiscard]] constexpr I base() const& requires std::copyable { return base_; } + + [[nodiscard]] constexpr I base() && { return std::move(base_); } + + // returns number of steps taken in any direction + // ++i, --i -> returns 2 + [[nodiscard]] constexpr difference_type distance_traversed() const { return distance_traversed_; } + + // returns number of steps from starting point + // ++i, --i -> returns 0 + [[nodiscard]] constexpr difference_type displacement() const { return displacement_; } + + [[nodiscard]] constexpr decltype(auto) operator*() const { return *base_; } + + [[nodiscard]] constexpr decltype(auto) operator[](difference_type const n) const { return base_[n]; } + + constexpr stride_counting_iterator& operator++() + { + ++base_; + ++distance_traversed_; + ++displacement_; + return *this; + } + + constexpr void operator++(int) { ++*this; } + + [[nodiscard]] constexpr stride_counting_iterator operator++(int) + requires std::forward_iterator + { + auto temp = *this; + ++*this; + return temp; + } + + constexpr stride_counting_iterator& operator--() + requires std::bidirectional_iterator + { + --base_; + ++distance_traversed_; + --displacement_; + return *this; + } + + [[nodiscard]] constexpr stride_counting_iterator operator--(int) + requires std::bidirectional_iterator + { + auto temp = *this; + --*this; + return temp; + } + + constexpr stride_counting_iterator& operator+=(difference_type const n) + requires std::random_access_iterator + { + base_ += n; + ++distance_traversed_; + ++displacement_; + return *this; + } + + constexpr stride_counting_iterator& operator-=(difference_type const n) + requires std::random_access_iterator + { + base_ -= n; + ++distance_traversed_; + --displacement_; + return *this; + } + + [[nodiscard]] constexpr friend stride_counting_iterator operator+(stride_counting_iterator i, difference_type const n) + requires std::random_access_iterator + { + return i += n; + } + + [[nodiscard]] constexpr friend stride_counting_iterator operator+(difference_type const n, stride_counting_iterator i) + requires std::random_access_iterator + { + return i += n; + } + + [[nodiscard]] constexpr friend stride_counting_iterator operator-(stride_counting_iterator i, difference_type const n) + requires std::random_access_iterator + { + return i -= n; + } + + [[nodiscard]] constexpr friend difference_type operator-(stride_counting_iterator const& x, stride_counting_iterator const& y) + requires std::sized_sentinel_for + { + return x.base() - y.base(); + } + + [[nodiscard]] constexpr bool operator==(stride_counting_iterator const& other) const + requires std::sentinel_for + { + return base_ == other.base_; + } + + template S> + [[nodiscard]] constexpr bool operator==(S const last) const + { + return base_ == last; + } + + [[nodiscard]] constexpr friend bool operator<(stride_counting_iterator const& x, stride_counting_iterator const& y) + requires std::random_access_iterator + { + return x.base_ < y.base_; + } + + [[nodiscard]] constexpr friend bool operator>(stride_counting_iterator const& x, stride_counting_iterator const& y) + requires std::random_access_iterator + { + return y < x; + } + + [[nodiscard]] constexpr friend bool operator<=(stride_counting_iterator const& x, stride_counting_iterator const& y) + requires std::random_access_iterator + { + return !(y < x); + } + + [[nodiscard]] constexpr friend bool operator>=(stride_counting_iterator const& x, stride_counting_iterator const& y) + requires std::random_access_iterator + { + return !(x < y); + } + +private: + I base_; + difference_type distance_traversed_ = 0; + difference_type displacement_ = 0; +}; + +// clang-format on + #endif // TEST_STD_VER > 17 && defined(__cpp_lib_concepts) #undef DELETE_FUNCTION