diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -97,6 +97,7 @@ __algorithm/ranges_copy_n.h __algorithm/ranges_count.h __algorithm/ranges_count_if.h + __algorithm/ranges_ends_with.h __algorithm/ranges_equal.h __algorithm/ranges_equal_range.h __algorithm/ranges_fill.h diff --git a/libcxx/include/__algorithm/ranges_ends_with.h b/libcxx/include/__algorithm/ranges_ends_with.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/ranges_ends_with.h @@ -0,0 +1,108 @@ +//===----------------------------------------------------------------------===// +// +// 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___ALGORITHM_RANGES_ENDS_WITH_H +#define _LIBCPP___ALGORITHM_RANGES_ENDS_WITH_H + +#include <__algorithm/in_in_result.h> +#include <__algorithm/ranges_equal.h> +#include <__config> +#include <__functional/identity.h> +#include <__functional/ranges_operations.h> +#include <__iterator/concepts.h> +#include <__iterator/distance.h> +#include <__iterator/indirectly_comparable.h> +#include <__ranges/access.h> +#include <__ranges/concepts.h> +#include <__ranges/drop_view.h> +#include <__ranges/ref_view.h> +#include <__utility/move.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if _LIBCPP_STD_VER >= 23 + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace ranges { +namespace __ends_with { +struct __fn { + template _Sent1, + input_iterator _Iter2, + sentinel_for<_Iter2> _Sent2, + class _Pred = ranges::equal_to, + class _Proj1 = identity, + class _Proj2 = identity> + requires(forward_iterator<_Iter1> || sized_sentinel_for<_Sent1, _Iter1>) && + (forward_iterator<_Iter2> || sized_sentinel_for<_Sent2, _Iter2>) && + indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2> + _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool operator()( + _Iter1 __first1, + _Sent1 __last1, + _Iter2 __first2, + _Sent2 __last2, + _Pred __pred = {}, + _Proj1 __proj1 = {}, + _Proj2 __proj2 = {}) const { + auto N1 = ranges::distance(__first1, __last1); + auto N2 = ranges::distance(__first2, __last2); + if (N2 > N1) + return false; + std::advance(__first1, (N1 - N2)); + auto __unwrapped1 = std::__unwrap_range(std::move(__first1), std::move(__last1)); + auto __unwrapped2 = std::__unwrap_range(std::move(__first2), std::move(__last2)); + return std::__equal_impl( + std::move(__unwrapped1.first), + std::move(__unwrapped1.second), + std::move(__unwrapped2.first), + std::move(__unwrapped2.second), + __pred, + __proj1, + __proj2); + } + + template + requires(forward_range<_Range1> || sized_range<_Range1>) && (forward_range<_Range2> || sized_range<_Range2>) && + indirectly_comparable, iterator_t<_Range2>, _Pred, _Proj1, _Proj2> + _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool operator()( + _Range1&& __range1, _Range2&& __range2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const { + auto N1 = ranges::distance(__range1); + auto N2 = ranges::distance(__range2); + if (N2 > N1) + return false; + auto __new_range = ranges::drop_view(ranges::ref_view(__range1), N1 - N2); + auto __unwrapped1 = std::__unwrap_range(ranges::begin(__new_range), ranges::end(__new_range)); + auto __unwrapped2 = std::__unwrap_range(ranges::begin(__range2), ranges::end(__range2)); + return std::__equal_impl( + std::move(__unwrapped1.first), + std::move(__unwrapped1.second), + std::move(__unwrapped2.first), + std::move(__unwrapped2.second), + __pred, + __proj1, + __proj2); + } +}; +} // namespace __ends_with +inline namespace __cpo { +inline constexpr auto ends_with = __ends_with::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER >= 23 + +#endif // _LIBCPP___ALGORITHM_RANGES_ENDS_WITH_H diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -437,6 +437,22 @@ template, Proj>> Pred> constexpr bool ranges::any_of(R&& r, Pred pred, Proj proj = {}); // since C++20 + + template S1, input_iterator I2, sentinel_for S2, + class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity> + requires (forward_iterator || sized_sentinel_for) && + (forward_iterator || sized_sentinel_for) && + indirectly_comparable + constexpr bool ranges::ends_with(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, + Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++23 + + template + requires (forward_range || sized_range) && + (forward_range || sized_range) && + indirectly_comparable, iterator_t, Pred, Proj1, Proj2> + constexpr bool ranges::ends_with(R1&& r1, R2&& r2, Pred pred = {}, + Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++23 template S, class Proj = identity, indirect_unary_predicate> Pred> @@ -1818,6 +1834,7 @@ #include <__algorithm/ranges_copy_n.h> #include <__algorithm/ranges_count.h> #include <__algorithm/ranges_count_if.h> +#include <__algorithm/ranges_ends_with.h> #include <__algorithm/ranges_equal.h> #include <__algorithm/ranges_equal_range.h> #include <__algorithm/ranges_fill.h> diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in --- a/libcxx/include/module.modulemap.in +++ b/libcxx/include/module.modulemap.in @@ -374,6 +374,7 @@ } module ranges_count { private header "__algorithm/ranges_count.h" } module ranges_count_if { private header "__algorithm/ranges_count_if.h" } + module ranges_ends_with { private header "__algorithm/ranges_ends_with.h" } module ranges_equal { private header "__algorithm/ranges_equal.h" } module ranges_equal_range { private header "__algorithm/ranges_equal_range.h" diff --git a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp --- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp +++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp @@ -103,7 +103,8 @@ (void)std::ranges::copy_if(first, last, first2, UnaryTrue(&copies)); assert(copies == 0); (void)std::ranges::copy_if(a, first2, UnaryTrue(&copies)); assert(copies == 0); #if TEST_STD_VER > 20 - //(void)std::ranges::ends_with(first, last, first2, last2, Equal(&copies)); assert(copies == 0); + (void)std::ranges::ends_with(first, last, first2, last2, Equal(&copies)); assert(copies == 0); + (void)std::ranges::ends_with(a, b, Equal(&copies)); assert(copies == 0); #endif (void)std::ranges::equal(first, last, first2, last2, Equal(&copies)); assert(copies == 0); (void)std::ranges::equal(a, b, Equal(&copies)); assert(copies == 0); diff --git a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp --- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp +++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp @@ -87,7 +87,8 @@ (void)std::ranges::copy_if(first, last, first2, UnaryTrue(), Proj(&copies)); assert(copies == 0); (void)std::ranges::copy_if(a, first2, UnaryTrue(), Proj(&copies)); assert(copies == 0); #if TEST_STD_VER > 20 - //(void)std::ranges::ends_with(first, last, first2, last2, Equal(), Proj(&copies), Proj(&copies)); assert(copies == 0); + (void)std::ranges::ends_with(first, last, first2, last2, Equal(), Proj(&copies), Proj(&copies)); assert(copies == 0); + (void)std::ranges::ends_with(a, b, Equal(), Proj(&copies), Proj(&copies)); assert(copies == 0); #endif (void)std::ranges::equal(first, last, first2, last2, Equal(), Proj(&copies), Proj(&copies)); assert(copies == 0); (void)std::ranges::equal(a, b, Equal(), Proj(&copies), Proj(&copies)); assert(copies == 0); diff --git a/libcxx/test/libcxx/private_headers.verify.cpp b/libcxx/test/libcxx/private_headers.verify.cpp --- a/libcxx/test/libcxx/private_headers.verify.cpp +++ b/libcxx/test/libcxx/private_headers.verify.cpp @@ -134,6 +134,7 @@ #include <__algorithm/ranges_copy_n.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_copy_n.h'}} #include <__algorithm/ranges_count.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_count.h'}} #include <__algorithm/ranges_count_if.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_count_if.h'}} +#include <__algorithm/ranges_ends_with.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_ends_with.h'}} #include <__algorithm/ranges_equal.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_equal.h'}} #include <__algorithm/ranges_equal_range.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_equal_range.h'}} #include <__algorithm/ranges_fill.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_fill.h'}} diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.ends_with/ranges.ends_with.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.ends_with/ranges.ends_with.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.ends_with/ranges.ends_with.pass.cpp @@ -0,0 +1,257 @@ +//===----------------------------------------------------------------------===// +// +// 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, c++20 + +// template S1, input_iterator I2, sentinel_for S2, +// class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity> +// requires indirectly_comparable +// constexpr bool ranges::ends_with(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, +// Proj1 proj1 = {}, Proj2 proj2 = {}); +// template +// requires indirectly_comparable, iterator_t, Pred, Proj1, Proj2> +// constexpr bool ranges::ends_with(R1&& r1, R2&& r2, Pred pred = {}, +// Proj1 proj1 = {}, Proj2 proj2 = {}); + +#include +#include +#include + +#include "almost_satisfies_types.h" +#include "test_iterators.h" + +template +concept HasEndsWithIt = requires (Iter1 first1, Sent1 last1, Iter2 first2, Sent2 last2) { + std::ranges::ends_with(first1, last1, first2, last2); +}; + +static_assert(HasEndsWithIt); +static_assert(!HasEndsWithIt); +static_assert(!HasEndsWithIt); +static_assert(!HasEndsWithIt); +static_assert(!HasEndsWithIt); // not indirectly comparable +static_assert(!HasEndsWithIt); +static_assert(!HasEndsWithIt); +static_assert(!HasEndsWithIt); +static_assert(!HasEndsWithIt); +static_assert(!HasEndsWithIt); + +template > +concept HasEndsWithR = requires (Range1 range1, Range2 range2) { + std::ranges::ends_with(range1, range2); +}; + +static_assert(HasEndsWithR>); +static_assert(!HasEndsWithR); +static_assert(!HasEndsWithR); +static_assert(!HasEndsWithR); +static_assert(!HasEndsWithR); +static_assert(!HasEndsWithR, UncheckedRange>); // not indirectly comparable +static_assert(!HasEndsWithR, ForwardRangeNotDerivedFrom>); +static_assert(!HasEndsWithR, ForwardRangeNotIncrementable>); +static_assert(!HasEndsWithR, ForwardRangeNotSentinelSemiregular>); +static_assert(!HasEndsWithR, ForwardRangeNotSentinelEqualityComparableWith>); + +// clang-format off +template +constexpr void test_iterators() { + {// simply tests + {int a[] = {1, 2, 3, 4, 5, 6}; + int p[] = {5, 6}; + std::same_as decltype(auto) ret = + std::ranges::ends_with(Iter1(a), Sent1(Iter1(a + 6)), Iter2(p), Sent2(Iter2(p + 2))); + assert(ret); + } + { + int a[] = {1, 2, 3, 4, 5, 6}; + int p[] = {5, 6}; + auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6))); + auto prefix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 2))); + std::same_as decltype(auto) ret = std::ranges::ends_with(whole, prefix); + assert(ret); + } + } + + {// prefix doesn't match + { + int a[] = {1, 2, 3, 4, 5, 6}; + int p[] = {1, 2, 3}; + std::same_as decltype(auto) ret = + std::ranges::ends_with(Iter1(a), Sent1(Iter1(a + 6)), Iter2(p), Sent2(Iter2(p + 3))); + assert(!ret); + } + { + int a[] = {1, 2, 3, 4, 5, 6}; + int p[] = {1, 2, 3}; + auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6))); + auto prefix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3))); + std::same_as decltype(auto) ret = std::ranges::ends_with(whole, prefix); + assert(!ret); + } + } + + {// range and prefix are identical + { + int a[] = {1, 2, 3, 4, 5, 6}; + int p[] = {1, 2, 3, 4, 5, 6}; + std::same_as decltype(auto) ret = + std::ranges::ends_with(Iter1(a), Sent1(Iter1(a + 6)), Iter2(p), Sent2(Iter2(p + 6))); + assert(ret); + } + { + int a[] = {1, 2, 3, 4, 5, 6}; + int p[] = {1, 2, 3, 4, 5, 6}; + auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6))); + auto prefix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 6))); + std::same_as decltype(auto) ret = std::ranges::ends_with(whole, prefix); + assert(ret); + } + } + + {// prefix is longer than range + { + int a[] = {3, 4, 5, 6, 7, 8}; + int p[] = {1, 2, 3, 4, 5, 6, 7, 8}; + std::same_as decltype(auto) ret = + std::ranges::ends_with(Iter1(a), Sent1(Iter1(a + 6)), Iter2(p), Sent2(Iter2(p + 8))); + assert(!ret); + } + { + int a[] = {3, 4, 5, 6, 7, 8}; + int p[] = {1, 2, 3, 4, 5, 6, 7, 8}; + auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6))); + auto prefix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 8))); + std::same_as decltype(auto) ret = std::ranges::ends_with(whole, prefix); + assert(!ret); + } + } + + {// prefix has zero length + { + int a[] = {1, 2, 3, 4, 5, 6}; + int p[] = {}; + std::same_as decltype(auto) ret = + std::ranges::ends_with(Iter1(a), Sent1(Iter1(a + 6)), Iter2(p), Sent2(Iter2(p))); + assert(ret); + } + { + int a[] = {1, 2, 3, 4, 5, 6}; + int p[] = {}; + auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6))); + auto prefix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p))); + std::same_as decltype(auto) ret = std::ranges::ends_with(whole, prefix); + assert(ret); + } + } + + {// range has zero length + { + int a[] = {}; + int p[] = {1, 2, 3, 4, 5, 6, 7, 8}; + std::same_as decltype(auto) ret = + std::ranges::ends_with(Iter1(a), Sent1(Iter1(a)), Iter2(p), Sent2(Iter2(p + 8))); + assert(!ret); + } + { + int a[] = {}; + int p[] = {1, 2, 3, 4, 5, 6, 7, 8}; + auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a))); + auto prefix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 8))); + std::same_as decltype(auto) ret = std::ranges::ends_with(whole, prefix); + assert(!ret); + } + } + + {// check that the predicate is used + { + int a[] = {3, 4, 0, 6, 11, 8}; + int p[] = {1, 12}; + std::same_as decltype(auto) ret = std::ranges::ends_with( + Iter1(a), Sent1(Iter1(a + 6)), Iter2(p), Sent2(Iter2(p + 2)), [](int l, int r) { return l > r; }); + assert(!ret); + } + { + int a[] = {3, 4, 0, 6, 11, 8}; + int p[] = {1, 12}; + auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6))); + auto prefix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 2))); + std::same_as decltype(auto) ret = std::ranges::ends_with(whole, prefix, [](int l, int r) { return l > r; }); + assert(!ret); + } + } + + {// check that the projections are used + { + int a[] = {1, 3, 15, 1, 3, 5}; + int p[] = {2, 3, 4}; + std::same_as decltype(auto) ret = std::ranges::ends_with( + Iter1(a), + Sent1(Iter1(a + 6)), + Iter2(p), + Sent2(Iter2(p + 3)), + {}, + [](int i) { return i + 3; }, + [](int i) { return i * 2; }); + assert(ret); + } + { + int a[] = {1, 3, 15, 1, 3, 5}; + int p[] = {2, 3, 4}; + auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6))); + auto prefix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3))); + std::same_as decltype(auto) ret = std::ranges::ends_with( + whole, prefix, {}, [](int i) { return i + 3; }, [](int i) { return i * 2; }); + assert(ret); + } + } +} + +constexpr bool test() { + types::for_each(types::forward_iterator_list{}, []() { + types::for_each(types::forward_iterator_list{}, []() { + test_iterators(); + test_iterators, I2, I2>(); + test_iterators>(); + test_iterators, I2, sized_sentinel>(); + }); + }); + + { // check that std::invoke is used + struct S { + int i; + + constexpr S identity() { return *this; } + + constexpr bool compare(const S& s) { return i == s.i; } + }; + { + S a[] = {{1}, {2}, {3}, {4}}; + S p[] = {{3}, {4}}; + auto ret = std::ranges::ends_with(a, a + 4, p, p + 2, &S::compare, &S::identity, &S::identity); + assert(ret); + } + { + S a[] = {{1}, {2}, {3}, {4}}; + S p[] = {{3}, {4}}; + auto ret = std::ranges::ends_with(a, p, &S::compare, &S::identity, &S::identity); + assert(ret); + } + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/algorithms/ranges_robust_against_differing_projections.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_differing_projections.pass.cpp --- a/libcxx/test/std/algorithms/ranges_robust_against_differing_projections.pass.cpp +++ b/libcxx/test/std/algorithms/ranges_robust_against_differing_projections.pass.cpp @@ -77,7 +77,7 @@ test(std::ranges::set_union, in, in2, out2, less, proj1, proj2); #if TEST_STD_VER > 20 test(std::ranges::starts_with, in, in2, eq, proj1, proj2); - // test(std::ranges::ends_with, in, in2, eq, proj1, proj2); + test(std::ranges::ends_with, in, in2, eq, proj1, proj2); #endif return true; diff --git a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp --- a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp +++ b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp @@ -71,7 +71,9 @@ static_assert(test(std::ranges::copy_n, a, 10, a)); static_assert(test(std::ranges::count, a, 42)); static_assert(test(std::ranges::count_if, a, odd)); -//static_assert(test(std::ranges::ends_with, a, a)); +#if TEST_STD_VER > 20 +static_assert(test(std::ranges::ends_with, a, a)); +#endif static_assert(test(std::ranges::equal, a, a)); static_assert(test(std::ranges::equal_range, a, 42)); static_assert(test(std::ranges::fill, a, 42));