[libcxx-commits] [libcxx] 0218ea4 - [libc++] Implement ranges::ends_with
Zijun Zhao via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Sep 18 11:59:31 PDT 2023
Author: Zijun Zhao
Date: 2023-09-18T11:56:10-07:00
New Revision: 0218ea4aaa5478d4a55c3f73735d90968c8cbace
URL: https://github.com/llvm/llvm-project/commit/0218ea4aaa5478d4a55c3f73735d90968c8cbace
DIFF: https://github.com/llvm/llvm-project/commit/0218ea4aaa5478d4a55c3f73735d90968c8cbace.diff
LOG: [libc++] Implement ranges::ends_with
Reviewed By: #libc, var-const
Differential Revision: https://reviews.llvm.org/D150831
Added:
libcxx/benchmarks/algorithms/ranges_ends_with.bench.cpp
libcxx/include/__algorithm/ranges_ends_with.h
libcxx/test/std/algorithms/alg.nonmodifying/alg.ends_with/ranges.ends_with.pass.cpp
Modified:
libcxx/benchmarks/CMakeLists.txt
libcxx/include/CMakeLists.txt
libcxx/include/algorithm
libcxx/modules/std/algorithm.inc
libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp
libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp
libcxx/test/std/algorithms/ranges_robust_against_differing_projections.pass.cpp
libcxx/test/std/algorithms/ranges_robust_against_nonbool_predicates.pass.cpp
libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp
libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/benchmarks/CMakeLists.txt b/libcxx/benchmarks/CMakeLists.txt
index 66cc2930257643b..ccc98df638ae397 100644
--- a/libcxx/benchmarks/CMakeLists.txt
+++ b/libcxx/benchmarks/CMakeLists.txt
@@ -172,6 +172,7 @@ set(BENCHMARK_TESTS
algorithms/pop_heap.bench.cpp
algorithms/pstl.stable_sort.bench.cpp
algorithms/push_heap.bench.cpp
+ algorithms/ranges_ends_with.bench.cpp
algorithms/ranges_make_heap.bench.cpp
algorithms/ranges_make_heap_then_sort_heap.bench.cpp
algorithms/ranges_pop_heap.bench.cpp
diff --git a/libcxx/benchmarks/algorithms/ranges_ends_with.bench.cpp b/libcxx/benchmarks/algorithms/ranges_ends_with.bench.cpp
new file mode 100644
index 000000000000000..049af7c2e15a357
--- /dev/null
+++ b/libcxx/benchmarks/algorithms/ranges_ends_with.bench.cpp
@@ -0,0 +1,107 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include <algorithm>
+#include <benchmark/benchmark.h>
+#include <iterator>
+
+#include "test_iterators.h"
+#include <vector>
+
+static void bm_ends_with_contiguous_iter(benchmark::State& state) {
+ std::vector<int> a(state.range(), 1);
+ std::vector<int> p(state.range(), 1);
+
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(a);
+ benchmark::DoNotOptimize(p);
+
+ auto begin1 = contiguous_iterator(a.data());
+ auto end1 = contiguous_iterator(a.data() + a.size());
+ auto begin2 = contiguous_iterator(p.data());
+ auto end2 = contiguous_iterator(p.data() + p.size());
+
+ benchmark::DoNotOptimize(std::ranges::ends_with(begin1, end1, begin2, end2));
+ }
+}
+BENCHMARK(bm_ends_with_contiguous_iter)->RangeMultiplier(16)->Range(16, 16 << 20);
+
+static void bm_ends_with_random_iter(benchmark::State& state) {
+ std::vector<int> a(state.range(), 1);
+ std::vector<int> p(state.range(), 1);
+
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(a);
+ benchmark::DoNotOptimize(p);
+
+ auto begin1 = random_access_iterator(a.begin());
+ auto end1 = random_access_iterator(a.end());
+ auto begin2 = random_access_iterator(p.begin());
+ auto end2 = random_access_iterator(p.end());
+
+ benchmark::DoNotOptimize(std::ranges::ends_with(begin1, end1, begin2, end2));
+ }
+}
+BENCHMARK(bm_ends_with_random_iter)->RangeMultiplier(16)->Range(16, 16 << 20);
+
+static void bm_ends_with_bidirectional_iter(benchmark::State& state) {
+ std::vector<int> a(state.range(), 1);
+ std::vector<int> p(state.range(), 1);
+
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(a);
+ benchmark::DoNotOptimize(p);
+
+ auto begin1 = bidirectional_iterator(a.begin());
+ auto end1 = bidirectional_iterator(a.end());
+ auto begin2 = bidirectional_iterator(p.begin());
+ auto end2 = bidirectional_iterator(p.end());
+
+ benchmark::DoNotOptimize(std::ranges::ends_with(begin1, end1, begin2, end2));
+ }
+}
+BENCHMARK(bm_ends_with_bidirectional_iter)->RangeMultiplier(16)->Range(16, 16 << 20);
+
+static void bm_ends_with_forward_iter(benchmark::State& state) {
+ std::vector<int> a(state.range(), 1);
+ std::vector<int> p(state.range(), 1);
+
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(a);
+ benchmark::DoNotOptimize(p);
+
+ auto begin1 = forward_iterator(a.begin());
+ auto end1 = forward_iterator(a.end());
+ auto begin2 = forward_iterator(p.begin());
+ auto end2 = forward_iterator(p.end());
+
+ benchmark::DoNotOptimize(std::ranges::ends_with(begin1, end1, begin2, end2));
+ }
+}
+BENCHMARK(bm_ends_with_forward_iter)->RangeMultiplier(16)->Range(16, 16 << 20);
+
+static void bm_ends_with_forward_iter_with_size_optimization(benchmark::State& state) {
+ std::vector<int> a(state.range(), 1);
+ std::vector<int> p(state.range(), 1);
+ p.push_back(2);
+
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(a);
+ benchmark::DoNotOptimize(p);
+
+ auto begin1 = forward_iterator(a.begin());
+ auto end1 = forward_iterator(a.end());
+ auto begin2 = forward_iterator(p.begin());
+ auto end2 = forward_iterator(p.end());
+
+ benchmark::DoNotOptimize(std::ranges::ends_with(begin1, end1, begin2, end2));
+ }
+}
+BENCHMARK(bm_ends_with_forward_iter_with_size_optimization)->RangeMultiplier(16)->Range(16, 16 << 20);
+
+BENCHMARK_MAIN();
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 70db7c721a6158e..2ec755236dbaee2 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -110,6 +110,7 @@ set(files
__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
index 000000000000000..2afb74bff0f1526
--- /dev/null
+++ b/libcxx/include/__algorithm/ranges_ends_with.h
@@ -0,0 +1,196 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/ranges_equal.h>
+#include <__algorithm/ranges_starts_with.h>
+#include <__config>
+#include <__functional/identity.h>
+#include <__functional/ranges_operations.h>
+#include <__functional/reference_wrapper.h>
+#include <__iterator/advance.h>
+#include <__iterator/concepts.h>
+#include <__iterator/distance.h>
+#include <__iterator/indirectly_comparable.h>
+#include <__iterator/reverse_iterator.h>
+#include <__ranges/access.h>
+#include <__ranges/concepts.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 <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Pred, class _Proj1, class _Proj2>
+ static _LIBCPP_HIDE_FROM_ABI constexpr bool __ends_with_fn_impl_bidirectional(
+ _Iter1 __first1,
+ _Sent1 __last1,
+ _Iter2 __first2,
+ _Sent2 __last2,
+ _Pred& __pred,
+ _Proj1& __proj1,
+ _Proj2& __proj2) {
+ auto __rbegin1 = std::make_reverse_iterator(__last1);
+ auto __rend1 = std::make_reverse_iterator(__first1);
+ auto __rbegin2 = std::make_reverse_iterator(__last2);
+ auto __rend2 = std::make_reverse_iterator(__first2);
+ return ranges::starts_with(
+ __rbegin1, __rend1, __rbegin2, __rend2, std::ref(__pred), std::ref(__proj1), std::ref(__proj2));
+ }
+
+ template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Pred, class _Proj1, class _Proj2>
+ static _LIBCPP_HIDE_FROM_ABI constexpr bool __ends_with_fn_impl(
+ _Iter1 __first1,
+ _Sent1 __last1,
+ _Iter2 __first2,
+ _Sent2 __last2,
+ _Pred& __pred,
+ _Proj1& __proj1,
+ _Proj2& __proj2) {
+ if constexpr (std::bidirectional_iterator<_Sent1> && std::bidirectional_iterator<_Sent2> &&
+ (!std::random_access_iterator<_Sent1>)&&(!std::random_access_iterator<_Sent2>)) {
+ return __ends_with_fn_impl_bidirectional(__first1, __last1, __first2, __last2, __pred, __proj1, __proj2);
+
+ } else {
+ auto __n1 = ranges::distance(__first1, __last1);
+ auto __n2 = ranges::distance(__first2, __last2);
+ if (__n2 == 0)
+ return true;
+ if (__n2 > __n1)
+ return false;
+
+ return __ends_with_fn_impl_with_offset(
+ std::move(__first1),
+ std::move(__last1),
+ std::move(__first2),
+ std::move(__last2),
+ __pred,
+ __proj1,
+ __proj2,
+ __n1 - __n2);
+ }
+ }
+
+ template <class _Iter1,
+ class _Sent1,
+ class _Iter2,
+ class _Sent2,
+ class _Pred,
+ class _Proj1,
+ class _Proj2,
+ class _Offset>
+ static _LIBCPP_HIDE_FROM_ABI constexpr bool __ends_with_fn_impl_with_offset(
+ _Iter1 __first1,
+ _Sent1 __last1,
+ _Iter2 __first2,
+ _Sent2 __last2,
+ _Pred& __pred,
+ _Proj1& __proj1,
+ _Proj2& __proj2,
+ _Offset __offset) {
+ if constexpr (std::bidirectional_iterator<_Sent1> && std::bidirectional_iterator<_Sent2> &&
+ !std::random_access_iterator<_Sent1> && !std::random_access_iterator<_Sent2>) {
+ return __ends_with_fn_impl_bidirectional(
+ std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), __pred, __proj1, __proj2);
+
+ } else {
+ ranges::advance(__first1, __offset);
+ return ranges::equal(
+ std::move(__first1),
+ std::move(__last1),
+ std::move(__first2),
+ std::move(__last2),
+ std::ref(__pred),
+ std::ref(__proj1),
+ std::ref(__proj2));
+ }
+ }
+
+ template <input_iterator _Iter1,
+ sentinel_for<_Iter1> _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 {
+ return __ends_with_fn_impl(
+ std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), __pred, __proj1, __proj2);
+ }
+
+ template <input_range _Range1,
+ input_range _Range2,
+ class _Pred = ranges::equal_to,
+ class _Proj1 = identity,
+ class _Proj2 = identity>
+ requires(forward_range<_Range1> || sized_range<_Range1>) && (forward_range<_Range2> || sized_range<_Range2>) &&
+ indirectly_comparable<iterator_t<_Range1>, 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 {
+ if constexpr (sized_range<_Range1> && sized_range<_Range2>) {
+ auto __n1 = ranges::size(__range1);
+ auto __n2 = ranges::size(__range2);
+ if (__n2 == 0)
+ return true;
+ if (__n2 > __n1)
+ return false;
+ auto __offset = __n1 - __n2;
+
+ return __ends_with_fn_impl_with_offset(
+ ranges::begin(__range1),
+ ranges::end(__range1),
+ ranges::begin(__range2),
+ ranges::end(__range2),
+ __pred,
+ __proj1,
+ __proj2,
+ __offset);
+
+ } else {
+ return __ends_with_fn_impl(
+ ranges::begin(__range1),
+ ranges::end(__range1),
+ ranges::begin(__range2),
+ ranges::end(__range2),
+ __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
index 76e0d22bf73ef85..69ba9537dda6984 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -447,6 +447,22 @@ namespace ranges {
indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
constexpr bool ranges::any_of(R&& r, Pred pred, Proj proj = {}); // since C++20
+ template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2,
+ class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
+ requires (forward_iterator<I1> || sized_sentinel_for<S1, I1>) &&
+ (forward_iterator<I2> || sized_sentinel_for<S2, I2>) &&
+ indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
+ constexpr bool ranges::ends_with(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},
+ Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++23
+
+ template<input_range R1, input_range R2, class Pred = ranges::equal_to, class Proj1 = identity,
+ class Proj2 = identity>
+ requires (forward_range<R1> || sized_range<R1>) &&
+ (forward_range<R2> || sized_range<R2>) &&
+ indirectly_comparable<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
+ constexpr bool ranges::ends_with(R1&& r1, R2&& r2, Pred pred = {},
+ Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++23
+
template<input_iterator I, sentinel_for<I> S, class Proj = identity,
indirect_unary_predicate<projected<I, Proj>> Pred>
constexpr bool ranges::none_of(I first, S last, Pred pred, Proj proj = {}); // since C++20
@@ -1833,6 +1849,7 @@ template <class BidirectionalIterator, class Compare>
#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/modules/std/algorithm.inc b/libcxx/modules/std/algorithm.inc
index c951b2b36c82739..b7900d15c10c2b5 100644
--- a/libcxx/modules/std/algorithm.inc
+++ b/libcxx/modules/std/algorithm.inc
@@ -154,10 +154,10 @@ export namespace std {
// [alg.starts.with], starts with
using std::ranges::starts_with;
-# if 0
// [alg.ends.with], ends with
using std::ranges::ends_with;
+# if 0
// [alg.fold], fold
using std::ranges::fold_left;
using std::ranges::fold_left_first;
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
index 0a70bd2a243467c..eece9833f8a6357 100644
--- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp
+++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp
@@ -102,8 +102,9 @@ constexpr bool all_the_algorithms()
(void)std::ranges::count_if(a, UnaryTrue(&copies)); assert(copies == 0);
(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);
+#if TEST_STD_VER >= 23
+ (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
index 111b0ff655f5342..afbbc224ea8644c 100644
--- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp
+++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp
@@ -86,8 +86,9 @@ constexpr bool all_the_algorithms()
(void)std::ranges::count_if(a, UnaryTrue(), Proj(&copies)); assert(copies == 0);
(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);
+#if TEST_STD_VER >= 23
+ (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/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
index 000000000000000..ce4dffffc4763a5
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.ends_with/ranges.ends_with.pass.cpp
@@ -0,0 +1,275 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2,
+// class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
+// requires indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
+// constexpr bool ranges::ends_with(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},
+// Proj1 proj1 = {}, Proj2 proj2 = {});
+// template<input_range R1, input_range R2, class Pred = ranges::equal_to, class Proj1 = identity,
+// class Proj2 = identity>
+// requires indirectly_comparable<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
+// constexpr bool ranges::ends_with(R1&& r1, R2&& r2, Pred pred = {},
+// Proj1 proj1 = {}, Proj2 proj2 = {});
+
+#include <algorithm>
+#include <array>
+#include <chrono>
+#include <ranges>
+#include "almost_satisfies_types.h"
+#include "test_iterators.h"
+
+using namespace std::chrono;
+
+template <class Iter1, class Sent1 = Iter1, class Iter2 = int*, class Sent2 = Iter2>
+concept HasEndsWithIt = requires(Iter1 first1, Sent1 last1, Iter2 first2, Sent2 last2) {
+ std::ranges::ends_with(first1, last1, first2, last2);
+};
+
+static_assert(HasEndsWithIt<int*>);
+static_assert(!HasEndsWithIt<ForwardIteratorNotDerivedFrom>);
+static_assert(!HasEndsWithIt<ForwardIteratorNotIncrementable>);
+static_assert(HasEndsWithIt<int*, int*>);
+static_assert(!HasEndsWithIt<int*, SentinelForNotSemiregular>);
+static_assert(!HasEndsWithIt<int*, int*, int**>); // not indirectly comparable
+static_assert(!HasEndsWithIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
+static_assert(!HasEndsWithIt<int*, int*, ForwardIteratorNotDerivedFrom>);
+static_assert(!HasEndsWithIt<int*, int*, ForwardIteratorNotIncrementable>);
+static_assert(!HasEndsWithIt<int*, int*, int*, SentinelForNotSemiregular>);
+static_assert(!HasEndsWithIt<int*, int*, int*, SentinelForNotWeaklyEqualityComparableWith>);
+
+template <class Range1, class Range2 = UncheckedRange<int*>>
+concept HasEndsWithR = requires(Range1&& range1, Range2&& range2) {
+ std::ranges::ends_with(std::forward<Range1>(range1), std::forward<Range2>(range2)); };
+
+static_assert(HasEndsWithR<UncheckedRange<int*>>);
+static_assert(!HasEndsWithR<ForwardRangeNotDerivedFrom>);
+static_assert(!HasEndsWithR<ForwardIteratorNotIncrementable>);
+static_assert(!HasEndsWithR<ForwardRangeNotSentinelSemiregular>);
+static_assert(!HasEndsWithR<ForwardRangeNotSentinelEqualityComparableWith>);
+static_assert(HasEndsWithR<UncheckedRange<int*>, UncheckedRange<int*>>);
+static_assert(!HasEndsWithR<UncheckedRange<int*>, UncheckedRange<int**>>); // not indirectly comparable
+static_assert(!HasEndsWithR<UncheckedRange<int*>, ForwardRangeNotDerivedFrom>);
+static_assert(!HasEndsWithR<UncheckedRange<int*>, ForwardRangeNotSentinelSemiregular>);
+
+// clang-format off
+template <class Iter1, class Sent1 = Iter1, class Iter2, class Sent2 = Iter2>
+constexpr void test_iterators() {
+ { // simple tests
+ int a[] = {1, 2, 3, 4, 5, 6};
+ int p[] = {4, 5, 6};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6)));
+ auto suffix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3)));
+ {
+ [[maybe_unused]] std::same_as<bool> decltype(auto) ret = std::ranges::ends_with(whole.begin(), whole.end(), suffix.begin(), suffix.end());
+ assert(ret);
+ }
+ {
+ [[maybe_unused]] std::same_as<bool> decltype(auto) ret = std::ranges::ends_with(whole, suffix);
+ assert(ret);
+ }
+ }
+
+ { // suffix doesn't match
+ 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 suffix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3)));
+ {
+ bool ret = std::ranges::ends_with(whole.begin(), whole.end(), suffix.begin(), suffix.end());
+ assert(!ret);
+ }
+ {
+ bool ret = std::ranges::ends_with(whole, suffix);
+ assert(!ret);
+ }
+ }
+
+ { // range consists of just one element
+ int a[] = {1};
+ int p[] = {1};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 1)));
+ auto suffix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 1)));
+ {
+ bool ret = std::ranges::ends_with(whole.begin(), whole.end(), suffix.begin(), suffix.end());
+ assert(ret);
+ }
+ {
+ bool ret = std::ranges::ends_with(whole, suffix);
+ assert(ret);
+ }
+ }
+
+ { // suffix consists of just one element
+ int a[] = {5, 1, 2, 4, 3};
+ int p[] = {3};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 5)));
+ auto suffix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 1)));
+ {
+ bool ret = std::ranges::ends_with(whole.begin(), whole.end(), suffix.begin(), suffix.end());
+ assert(ret);
+ }
+ {
+ bool ret = std::ranges::ends_with(whole, suffix);
+ assert(ret);
+ }
+ }
+
+ { // range and suffix are identical
+ 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 suffix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 6)));
+ {
+ bool ret = std::ranges::ends_with(whole.begin(), whole.end(), suffix.begin(), suffix.end());
+ assert(ret);
+ }
+ {
+ bool ret = std::ranges::ends_with(whole, suffix);
+ assert(ret);
+ }
+ }
+
+ { // suffix is longer than range
+ 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 suffix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 8)));
+ {
+ bool ret = std::ranges::ends_with(whole.begin(), whole.end(), suffix.begin(), suffix.end());
+ assert(!ret);
+ }
+ {
+ bool ret = std::ranges::ends_with(whole, suffix);
+ assert(!ret);
+ }
+ }
+
+ { // suffix has zero length
+ int a[] = {1, 2, 3, 4, 5, 6};
+ int p[] = {};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6)));
+ auto suffix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p)));
+ {
+ bool ret = std::ranges::ends_with(whole.begin(), whole.end(), suffix.begin(), suffix.end());
+ assert(ret);
+ }
+ {
+ bool ret = std::ranges::ends_with(whole, suffix);
+ assert(ret);
+ }
+ }
+
+ { // range has zero length
+ int a[] = {};
+ int p[] = {1, 2, 3, 4, 5, 6, 7, 8};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a)));
+ auto suffix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 8)));
+ {
+ bool ret = std::ranges::ends_with(whole.begin(), whole.end(), suffix.begin(), suffix.end());
+ assert(!ret);
+ }
+ {
+ bool ret = std::ranges::ends_with(whole, suffix);
+ assert(!ret);
+ }
+ }
+
+ { // subarray
+ int a[] = {0, 3, 5, 10, 7, 3, 5, 89, 3, 5, 2, 1, 8, 6};
+ int p[] = {3, 5};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 13)));
+ auto suffix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 2)));
+ {
+ bool ret = std::ranges::ends_with(whole.begin(), whole.end(), suffix.begin(), suffix.end());
+ assert(!ret);
+ }
+ {
+ bool ret = std::ranges::ends_with(whole, suffix);
+ assert(!ret);
+ }
+ }
+
+ { // repeated suffix
+ int a[] = {8, 6, 3, 5, 1, 2};
+ int p[] = {1, 2, 1, 2};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6)));
+ auto suffix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 4)));
+ {
+ bool ret = std::ranges::ends_with(whole.begin(), whole.end(), suffix.begin(), suffix.end());
+ assert(!ret);
+ }
+ {
+ bool ret = std::ranges::ends_with(whole, suffix);
+ assert(!ret);
+ }
+ }
+
+ { // check that the predicate is used
+ int a[] = {5, 1, 3, 2, 7};
+ int p[] = {-2, -7};
+ auto pred = [](int l, int r) { return l * -1 == r; };
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 5)));
+ auto suffix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 2)));
+ {
+ bool ret = std::ranges::ends_with(whole.begin(), whole.end(), suffix.begin(), suffix.end(), pred);
+ assert(ret);
+ }
+ {
+ bool ret = std::ranges::ends_with(whole, suffix, pred);
+ assert(ret);
+ }
+ }
+
+ { // check that the projections are used
+ int a[] = {1, 3, 15, 1, 2, 1};
+ int p[] = {2, 1, 2};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6)));
+ auto suffix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3)));
+ {
+ bool ret = std::ranges::ends_with(whole.begin(), whole.end(), suffix.begin(), suffix.end(), {},
+ [](int i) { return i - 3; },
+ [](int i) { return i * -1; });
+ assert(ret);
+ }
+ {
+ bool ret = std::ranges::ends_with(whole, suffix, {},
+ [](int i) { return i - 3; },
+ [](int i) { return i * -1; });
+ assert(ret);
+ }
+ }
+}
+
+constexpr bool test() {
+ // This is to test (forward_iterator<_Iter1> || sized_sentinel_for<_Sent1, _Iter1>) condition.
+ types::for_each(types::cpp20_input_iterator_list<int*>{}, []<class Iter2>() {
+ types::for_each(types::cpp20_input_iterator_list<int*>{}, []<class Iter1>() {
+ if constexpr (std::forward_iterator<Iter1> && std::forward_iterator<Iter2>)
+ test_iterators<Iter1, Iter1, Iter2, Iter2>();
+ if constexpr (std::forward_iterator<Iter2>)
+ test_iterators<Iter1, sized_sentinel<Iter1>, Iter2, Iter2>();
+ if constexpr (std::forward_iterator<Iter1>)
+ test_iterators<Iter1, Iter1, Iter2, sized_sentinel<Iter2>>();
+ test_iterators<Iter1, sized_sentinel<Iter1>, Iter2, sized_sentinel<Iter2>>();
+ });
+ });
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
+ return 0;
+}
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_
diff ering_projections.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_
diff ering_projections.pass.cpp
index 4c90387443df7a1..82792249ef5c749 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_
diff ering_projections.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_
diff ering_projections.pass.cpp
@@ -54,6 +54,9 @@ constexpr bool test_all() {
auto proj1 = [](int x) { return x * -1; };
auto proj2 = [](A a) { return a.x * -1; };
+#if TEST_STD_VER >= 23
+ test(std::ranges::ends_with, in, in2, eq, proj1, proj2);
+#endif
test(std::ranges::equal, in, in2, eq, proj1, proj2);
test(std::ranges::lexicographical_compare, in, in2, eq, proj1, proj2);
test(std::ranges::is_permutation, in, in2, eq, proj1, proj2);
@@ -77,7 +80,6 @@ constexpr bool test_all() {
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);
#endif
return true;
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_nonbool_predicates.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_nonbool_predicates.pass.cpp
index 38457201acf70f7..04773f0f5bc802e 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_nonbool_predicates.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_nonbool_predicates.pass.cpp
@@ -22,6 +22,7 @@
#include <ranges>
#include "boolean_testable.h"
+#include "test_macros.h"
constexpr auto unary_pred = [](int i) { return BooleanTestable(i > 0); };
static_assert(!std::same_as<decltype(unary_pred(1)), bool>);
@@ -68,6 +69,9 @@ constexpr bool test_all() {
test(std::ranges::any_of, in, unary_pred);
test(std::ranges::all_of, in, unary_pred);
+#if TEST_STD_VER >= 23
+ test(std::ranges::ends_with, in, in2, binary_pred);
+#endif
test(std::ranges::none_of, in, unary_pred);
test(std::ranges::find_if, in, unary_pred);
test(std::ranges::find_if_not, in, unary_pred);
@@ -118,6 +122,9 @@ constexpr bool test_all() {
test(std::ranges::partition_copy, in, out, out2, unary_pred);
test(std::ranges::partial_sort_copy, in, in2, binary_pred);
test(std::ranges::merge, in, in2, out, binary_pred);
+#if TEST_STD_VER > 20
+ test(std::ranges::starts_with, in, in2, binary_pred);
+#endif
test(std::ranges::set_
diff erence, in, in2, out, binary_pred);
test(std::ranges::set_intersection, in, in2, out, binary_pred);
test(std::ranges::set_symmetric_
diff erence, in, in2, out, binary_pred);
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp
index c5e8502ef2de66b..d17dce00e0b1aed 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp
@@ -19,6 +19,7 @@
#include <initializer_list>
#include <iterator>
#include <ranges>
+#include "test_macros.h"
struct Foo {
int val;
@@ -73,6 +74,9 @@ constexpr bool test_all() {
test(std::ranges::any_of, in, &Foo::unary_pred, &Bar::val);
test(std::ranges::all_of, in, &Foo::unary_pred, &Bar::val);
+#if TEST_STD_VER >= 23
+ test(std::ranges::ends_with, in, in2, &Foo::binary_pred, &Bar::val, &Bar::val);
+#endif
test(std::ranges::none_of, in, &Foo::unary_pred, &Bar::val);
test(std::ranges::find, in, x, &Bar::val);
test(std::ranges::find_if, in, &Foo::unary_pred, &Bar::val);
@@ -143,6 +147,9 @@ constexpr bool test_all() {
test(std::ranges::partition_copy, in, out, out2, &Foo::unary_pred, &Bar::val);
test(std::ranges::partial_sort_copy, in, in2, &Foo::binary_pred, &Bar::val, &Bar::val);
test(std::ranges::merge, in, in2, out, &Foo::binary_pred, &Bar::val, &Bar::val);
+#if TEST_STD_VER > 20
+ test(std::ranges::starts_with, in, in2, &Foo::binary_pred, &Bar::val, &Bar::val);
+#endif
test(std::ranges::set_
diff erence, in, in2, out, &Foo::binary_pred, &Bar::val, &Bar::val);
test(std::ranges::set_intersection, in, in2, out, &Foo::binary_pred, &Bar::val, &Bar::val);
test(std::ranges::set_symmetric_
diff erence, in, in2, out, &Foo::binary_pred, &Bar::val, &Bar::val);
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
index d383e18adbb99a4..5c8aa0153a63c3f 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
@@ -25,6 +25,7 @@
#include "MoveOnly.h"
#include "test_iterators.h"
+#include "test_macros.h"
// (in, ...)
template <class Func, std::ranges::range Input, class ...Args>
@@ -73,6 +74,9 @@ constexpr void run_tests() {
test(std::ranges::any_of, in, unary_pred);
test(std::ranges::all_of, in, unary_pred);
+#if TEST_STD_VER >= 23
+ test(std::ranges::ends_with, in, in2);
+#endif
test(std::ranges::none_of, in, unary_pred);
test(std::ranges::find, in, x);
test(std::ranges::find_if, in, unary_pred);
@@ -129,6 +133,9 @@ constexpr void run_tests() {
test(std::ranges::replace_copy, in, out, x, x);
test(std::ranges::replace_copy_if, in, out, unary_pred, x);
}
+#if TEST_STD_VER > 20
+ test(std::ranges::starts_with, in, in2);
+#endif
test(std::ranges::swap_ranges, in, in2);
if constexpr (std::copyable<T>) {
test(std::ranges::reverse_copy, in, out);
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
index 9e00a57ba80c631..683f88c19f67849 100644
--- 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_if, a, a, odd));
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 >= 23
+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));
More information about the libcxx-commits
mailing list