diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -7,10 +7,12 @@ __config __debug __errc + __function_like.h __functional_03 __functional_base __functional_base_03 __hash_table + __iterator/advance.h __iterator/concepts.h __iterator/incrementable_traits.h __iterator/iter_move.h diff --git a/libcxx/include/__function_like.h b/libcxx/include/__function_like.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__function_like.h @@ -0,0 +1,56 @@ +// -*- 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_FUNCTION_LIKE_H +#define _LIBCPP___ITERATOR_FUNCTION_LIKE_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 `__function_like` 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 __function_like { + __function_like() = delete; + __function_like(__function_like const&) = delete; + __function_like& operator=(__function_like const&) = delete; + + void operator&() const = delete; + + struct __tag { }; + +protected: + constexpr explicit __function_like(__tag) noexcept {} + ~__function_like() = default; +}; +} // namespace ranges + +#endif // !defined(_LIBCPP_HAS_NO_RANGES) + +_LIBCPP_END_NAMESPACE_STD + +_LIBCPP_POP_MACROS + +#endif // _LIBCPP___ITERATOR_FUNCTION_LIKE_H diff --git a/libcxx/include/__iterator/advance.h b/libcxx/include/__iterator/advance.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__iterator/advance.h @@ -0,0 +1,157 @@ +// -*- 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_ADVANCE_H +#define _LIBCPP___ITERATOR_ADVANCE_H + +#include <__config> +#include <__debug> +#include <__function_like.h> +#include <__iterator/concepts.h> +#include <__iterator/incrementable_traits.h> +#include +#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 : __function_like { +private: + template + static constexpr make_unsigned_t<_Tp> __abs(_Tp const __n) noexcept { + auto const __unsigned_n = __to_unsigned_like(__n); + auto const __complement = ~__unsigned_n + 1; + return __n < 0 ? __complement : __unsigned_n; + } + + template + static constexpr void __advance_forward(_Ip& __i, iter_difference_t<_Ip> __n) { + while (__n > 0) { + --__n; + ++__i; + } + } + + template + 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 + 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> + 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> + 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; + } + + _LIBCPP_UNREACHABLE(); + } +}; + +inline constexpr auto advance = __advance_fn(__function_like::__tag()); +} // namespace ranges + +#endif // !defined(_LIBCPP_HAS_NO_RANGES) + +_LIBCPP_END_NAMESPACE_STD + +_LIBCPP_POP_MACROS + +#endif // _LIBCPP___ITERATOR_ADVANCE_H diff --git a/libcxx/include/iterator b/libcxx/include/iterator --- a/libcxx/include/iterator +++ b/libcxx/include/iterator @@ -124,6 +124,17 @@ constexpr BidirectionalIterator prev(BidirectionalIterator x, typename iterator_traits::difference_type n = 1); +// [range.iter.ops], range iterator operations +namespace ranges { + // [range.iter.op.advance], ranges::advance + template + constexpr void advance(I& i, iter_difference_t n); // since C++20 + template S> + constexpr void advance(I& i, S bound); // since C++20 + template S> + constexpr iter_difference_t advance(I& i, iter_difference_t n, S bound); // since C++20 +} + template class reverse_iterator : public iterator::iterator_category, @@ -472,6 +483,7 @@ #include <__config> #include <__debug> #include <__functional_base> +#include <__iterator/advance.h> #include <__iterator/concepts.h> #include <__iterator/incrementable_traits.h> #include <__iterator/iter_move.h> @@ -534,7 +546,8 @@ __i += __n; } -template +template ()))>::value>::type> inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 void advance(_InputIter& __i, _Distance __orig_n) { 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,272 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +#include +#include + +#include "test_standard_function.h" +#include "test_iterators.h" + +static_assert(is_function_like()); + +using range_t = std::array; + +[[nodiscard]] constexpr bool operator==(output_iterator const x, output_iterator const y) { + return x.base() == y.base(); +} + +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.stride_count() == distance); + assert(i.stride_displacement() == n); +} + +template +constexpr void check_round_trip(stride_counting_iterator const& i, std::ptrdiff_t const n) { + assert(i.stride_count() == 0 || i.stride_count() == 1); + assert(i.stride_displacement() == n < 0 ? -1 : 1); +} + +namespace iterator_count { +template +constexpr void check_move_forward(std::ptrdiff_t const n) { + auto range = range_t{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + 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 range = range_t{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + 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_forward >(7); + + 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 >(0); + check_move_forward >(0); + check_move_forward >(0); + check_move_forward >(0); + check_move_forward >(0); + check_move_forward >(0); + check_move_backward >(0); + check_move_backward >(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_or_output_iterator auto const&) const { + assert(false && "difference op should take precedence"); + return false; + } + + [[nodiscard]] constexpr friend std::ptrdiff_t operator-(std::input_or_output_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_or_output_iterator auto const&) { + return x.count_; + } + +private: + std::ptrdiff_t count_ = 0; +}; + +namespace iterator_sentinel { +template S = I> +constexpr void check_assignable_case(std::ptrdiff_t const n) { + auto range = range_t{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + 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.stride_count() == 0); // always zero, so don't use `check_round_trip` +} + +template +constexpr void check_sized_sentinel_case(std::ptrdiff_t const n) { + auto range = range_t{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + 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 range = range_t{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + 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.stride_count() == 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_assignable_case >(7); + + check_sized_sentinel_case >(7); + check_sized_sentinel_case >(6); + check_sized_sentinel_case >(5); + check_sized_sentinel_case >(4); + check_sized_sentinel_case >(3); + check_sized_sentinel_case >(2); + check_sized_sentinel_case >(1); + + 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); + check_sentinel_case >(7); + 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, range_t& range) { + 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, range_t& range) { + 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, range_t& range) { + 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.stride_count() == n - expected.result); +} + +template +constexpr void check_backward_case(std::ptrdiff_t const n, expected_t const expected, range_t& range) { + 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.stride_count() == n + expected.result); + assert(current.stride_count() == -current.stride_displacement()); +} + +[[nodiscard]] constexpr bool test() { + auto range = range_t{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + check_forward_sized_sentinel_case >(1, {range.begin() + 1, 0}, range); + // cpp20_input_iterator not copyable, so is omitted + check_forward_sized_sentinel_case >(3, {range.begin() + 3, 0}, range); + check_forward_sized_sentinel_case >(4, {range.begin() + 4, 0}, range); + check_forward_sized_sentinel_case >(5, {range.begin() + 5, 0}, range); + check_forward_sized_sentinel_case >(6, {range.begin() + 6, 0}, range); + check_forward_sized_sentinel_case >(7, {range.begin() + 7, 0}, range); + + // bidirectional_iterator omitted because `n < 0` case requires `same_as` + check_backward_sized_sentinel_case >(5, {range.begin() + 5, 0}, + range); + check_backward_sized_sentinel_case >(6, {range.begin() + 4, 0}, range); + + // disntance == range.size() + check_forward_sized_sentinel_case >(10, {range.end(), 0}, range); + check_forward_sized_sentinel_case >(10, {range.end(), 0}, range); + check_backward_sized_sentinel_case >(10, {range.begin(), 0}, range); + + // distance > range.size() + check_forward_sized_sentinel_case >(1000, {range.end(), 990}, range); + check_forward_sized_sentinel_case >(1000, {range.end(), 990}, range); + check_backward_sized_sentinel_case >(1000, {range.begin(), -990}, + range); + + check_forward_case >(1, {range.begin() + 1, 0}, range); + check_forward_case >(3, {range.begin() + 3, 0}, range); + check_forward_case >(4, {range.begin() + 4, 0}, range); + check_forward_case >(5, {range.begin() + 5, 0}, range); + check_forward_case >(6, {range.begin() + 6, 0}, range); + check_forward_case >(7, {range.begin() + 7, 0}, range); + check_backward_case >(8, {range.begin() + 2, 0}, range); + + // disntance == range.size() + check_forward_case >(10, {range.end(), 0}, range); + check_forward_case >(10, {range.end(), 0}, range); + check_backward_case >(10, {range.begin(), 0}, range); + + // distance > range.size() + check_forward_case >(1000, {range.end(), 990}, range); + check_forward_case >(1000, {range.end(), 990}, range); + check_backward_case >(1000, {range.begin(), -990}, range); + + 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/constraints.verify.cpp b/libcxx/test/std/iterators/iterator.primitives/range.iter.ops/range.iter.ops.advance/constraints.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/iterators/iterator.primitives/range.iter.ops/range.iter.ops.advance/constraints.verify.cpp @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// 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::next + +#include + +#include + +#include "test_iterators.h" + +void proper_constraints() { + auto p = std::unique_ptr(); + std::ranges::advance(p, 5); // expected-error {{no matching function for call}} + std::ranges::advance(p, p); // expected-error {{no matching function for call}} + std::ranges::advance(p, 5, p); // expected-error {{no matching function for call}} +} diff --git a/libcxx/test/std/iterators/iterator.primitives/range.iter.ops/range.iter.ops.advance/special_function.compile.pass.cpp b/libcxx/test/std/iterators/iterator.primitives/range.iter.ops/range.iter.ops.advance/special_function.compile.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/iterators/iterator.primitives/range.iter.ops/range.iter.ops.advance/special_function.compile.pass.cpp @@ -0,0 +1,90 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// FIXME: We're bending the rules here by adding a new type to namespace std::ranges. Since this is +// the standard library's test suite, this should be fine (we *are* the implementation), but it's +// necessary at the time of writing since there aren't any iterators in std::ranges that we can +// borrow for this test. +namespace std::ranges { +class fake_forward_iterator { +public: + using value_type = int; + using difference_type = std::ptrdiff_t; + using iterator_category = std::forward_iterator_tag; + + fake_forward_iterator() = default; + + value_type operator*() const; + fake_forward_iterator& operator++(); + fake_forward_iterator operator++(int); + + bool operator==(fake_forward_iterator const&) const = default; +}; +} // namespace std::ranges + +template +constexpr bool unqualified_lookup_works = requires(I i, Args... args) { + advance(i, args...); +}; + +static_assert(!unqualified_lookup_works); +static_assert(!unqualified_lookup_works); +static_assert( + !unqualified_lookup_works); + +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); + advance(x, 0, x); +} 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,226 @@ 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::output_iterator_tag; +}; + +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; +}; + +template +struct iterator_concept { + using type = std::contiguous_iterator_tag; +}; + +template +using iterator_concept_t = typename iterator_concept::type; + +template +struct iter_value_or_void { using type = void; }; + +template +struct iter_value_or_void { + using type = std::iter_value_t; +}; + +// Iterator adaptor that counts the number of times the iterator has had a successor/predecessor +// operation called. Has two recorders: +// * `stride_count`, which records the total number of calls to an op++, op--, op+=, or op-=. +// * `stride_displacement`, which records the displacement of the calls. This means that both +// op++/op+= will increase the displacement counter by 1, and op--/op-= will decrease the +// displacement counter by 1. +template +class stride_counting_iterator { +public: + using value_type = typename iter_value_or_void::type; + 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 const& base() const& requires std::copyable { return base_; } + + [[nodiscard]] constexpr I base() && { return std::move(base_); } + + constexpr difference_type stride_count() const { return stride_count_; } + + constexpr difference_type stride_displacement() const { return stride_displacement_; } + + constexpr decltype(auto) operator*() const { return *base_; } + + constexpr decltype(auto) operator[](difference_type const n) const { return base_[n]; } + + constexpr stride_counting_iterator& operator++() + { + ++base_; + ++stride_count_; + ++stride_displacement_; + return *this; + } + + constexpr void operator++(int) { ++*this; } + + 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_; + ++stride_count_; + --stride_displacement_; + return *this; + } + + 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; + ++stride_count_; + ++stride_displacement_; + return *this; + } + + constexpr stride_counting_iterator& operator-=(difference_type const n) + requires std::random_access_iterator + { + base_ -= n; + ++stride_count_; + --stride_displacement_; + return *this; + } + + constexpr friend stride_counting_iterator operator+(stride_counting_iterator i, difference_type const n) + requires std::random_access_iterator + { + return i += n; + } + + constexpr friend stride_counting_iterator operator+(difference_type const n, stride_counting_iterator i) + requires std::random_access_iterator + { + return i += n; + } + + constexpr friend stride_counting_iterator operator-(stride_counting_iterator i, difference_type const n) + requires std::random_access_iterator + { + return i -= n; + } + + 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(); + } + + constexpr bool operator==(stride_counting_iterator const& other) const + requires std::sentinel_for + { + return base_ == other.base_; + } + + template S> + constexpr bool operator==(S const last) const + { + return base_ == last; + } + + constexpr friend bool operator<(stride_counting_iterator const& x, stride_counting_iterator const& y) + requires std::random_access_iterator + { + return x.base_ < y.base_; + } + + constexpr friend bool operator>(stride_counting_iterator const& x, stride_counting_iterator const& y) + requires std::random_access_iterator + { + return y < x; + } + + constexpr friend bool operator<=(stride_counting_iterator const& x, stride_counting_iterator const& y) + requires std::random_access_iterator + { + return !(y < x); + } + + 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 stride_count_ = 0; + difference_type stride_displacement_ = 0; +}; + +template +class sentinel_wrapper { +public: + sentinel_wrapper() = default; + constexpr explicit sentinel_wrapper(I base) : base_(std::move(base)) {} + + constexpr bool operator==(I const& other) const requires std::equality_comparable { + return base_ == other; + } + +private: + I base_ = I(); +}; + +// clang-format on + #endif // TEST_STD_VER > 17 && defined(__cpp_lib_concepts) #undef DELETE_FUNCTION diff --git a/libcxx/test/support/test_standard_function.h b/libcxx/test/support/test_standard_function.h new file mode 100644 --- /dev/null +++ b/libcxx/test/support/test_standard_function.h @@ -0,0 +1,39 @@ +//===----------------------------------------------------------------------===// +// +// 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 LIBCXX_TEST_SUPPORT_TEST_STANDARD_FUNCTION_H +#define LIBCXX_TEST_SUPPORT_TEST_STANDARD_FUNCTION_H + +#include "test_macros.h" + +#if TEST_STD_VER >= 20 +template +constexpr bool is_addressable = requires(T t) { + &t; +}; + +template +[[nodiscard]] constexpr bool is_function_like() { + using X = std::remove_cvref_t; + static_assert(!is_addressable); + static_assert(!is_addressable); + + static_assert(std::destructible && !std::default_initializable); + + static_assert(!std::move_constructible); + static_assert(!std::assignable_from); + + static_assert(!std::copy_constructible); + static_assert(!std::assignable_from); + static_assert(!std::assignable_from); + static_assert(!std::assignable_from); + static_assert(std::is_final_v); + return true; +} +#endif + +#endif // LIBCXX_TEST_SUPPORT_TEST_STANDARD_FUNCTION_H