[libcxx-commits] [libcxx] a6b846a - [libc++][ranges] Implement ranges::contains_subrange (#66963)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Feb 13 15:42:41 PST 2024
Author: ZijunZhaoCCK
Date: 2024-02-13T15:42:37-08:00
New Revision: a6b846ae1e58e11160185e427e20a995f6656859
URL: https://github.com/llvm/llvm-project/commit/a6b846ae1e58e11160185e427e20a995f6656859
DIFF: https://github.com/llvm/llvm-project/commit/a6b846ae1e58e11160185e427e20a995f6656859.diff
LOG: [libc++][ranges] Implement ranges::contains_subrange (#66963)
Added:
libcxx/include/__algorithm/ranges_contains_subrange.h
libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains_subrange.pass.cpp
Modified:
libcxx/docs/Status/RangesAlgorithms.csv
libcxx/include/CMakeLists.txt
libcxx/include/algorithm
libcxx/include/libcxx.imp
libcxx/include/module.modulemap.in
libcxx/modules/std/algorithm.inc
libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp
libcxx/test/libcxx/diagnostics/ranges.nodiscard_extensions.compile.pass.cpp
libcxx/test/libcxx/diagnostics/ranges.nodiscard_extensions.verify.cpp
libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/docs/Status/RangesAlgorithms.csv b/libcxx/docs/Status/RangesAlgorithms.csv
index 2fe530bf75fd94..f7a51f732c4b14 100644
--- a/libcxx/docs/Status/RangesAlgorithms.csv
+++ b/libcxx/docs/Status/RangesAlgorithms.csv
@@ -3,13 +3,13 @@ C++20,all C++20 algorithms,N/A,N/A,✅
C++23,`find_last <https://wg21.link/P1223R5>`_,Unassigned,No patch yet,Not started
C++23,`find_last_if <https://wg21.link/P1223R5>`_,Unassigned,No patch yet,Not started
C++23,`find_last_if_not <https://wg21.link/P1223R5>`_,Unassigned,No patch yet,Not started
-C++23,`starts_with <https://wg21.link/P1659R3>`_,Zijun Zhao,`D150735 <https://llvm.org/D150735>`_,✅
-C++23,`ends_with <https://wg21.link/P1659R3>`_,Zijun Zhao,No patch yet,In Progress
+C++23,`starts_with <https://wg21.link/P1659R3>`_,Zijun Zhao,`D150735 <https://llvm.org/D150735>`_,Complete
+C++23,`ends_with <https://wg21.link/P1659R3>`_,Zijun Zhao, `D150831 <https://llvm.org/D150831>`_,Complete
C++23,`shift_left <https://wg21.link/p2440r1>`_,Unassigned,No patch yet,Not started
C++23,`shift_right <https://wg21.link/p2440r1>`_,Unassigned,No patch yet,Not started
C++23,`iota (algorithm) <https://wg21.link/p2440r1>`_,Unassigned,No patch yet,Not started
C++23,`fold <https://wg21.link/p2322r5>`_,Unassigned,No patch yet,Not started
-C++23,`contains <https://wg21.link/p2302r2>`_,Zijun Zhao,No patch yet,In Progress
+C++23,`contains <https://wg21.link/p2302r2>`_,Zijun Zhao, `#65148 <https://github.com/llvm/llvm-project/pull/65148>`_,Complete
C++23,`fold_left_with_iter <https://wg21.link/p2322r6>`_,Christopher Di Bella,N/A,Complete
C++23,`fold_left <https://wg21.link/p2322r6>`_,Christopher Di Bella,N/A,Complete
C++23,`fold_left_first_with_iter <https://wg21.link/p2322r6>`_,Christopher Di Bella,N/A,In progress
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index d55dc66a91bc7d..b44068357e7089 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -110,6 +110,7 @@ set(files
__algorithm/ranges_binary_search.h
__algorithm/ranges_clamp.h
__algorithm/ranges_contains.h
+ __algorithm/ranges_contains_subrange.h
__algorithm/ranges_copy.h
__algorithm/ranges_copy_backward.h
__algorithm/ranges_copy_if.h
diff --git a/libcxx/include/__algorithm/ranges_contains_subrange.h b/libcxx/include/__algorithm/ranges_contains_subrange.h
new file mode 100644
index 00000000000000..4cd03cbb537060
--- /dev/null
+++ b/libcxx/include/__algorithm/ranges_contains_subrange.h
@@ -0,0 +1,99 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_CONTAINS_SUBRANGE_H
+#define _LIBCPP___ALGORITHM_RANGES_CONTAINS_SUBRANGE_H
+
+#include <__algorithm/ranges_search.h>
+#include <__config>
+#include <__functional/identity.h>
+#include <__functional/ranges_operations.h>
+#include <__functional/reference_wrapper.h>
+#include <__iterator/concepts.h>
+#include <__iterator/distance.h>
+#include <__iterator/indirectly_comparable.h>
+#include <__iterator/projected.h>
+#include <__ranges/access.h>
+#include <__ranges/concepts.h>
+#include <__ranges/subrange.h>
+#include <__utility/move.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+#if _LIBCPP_STD_VER >= 23
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+namespace ranges {
+namespace __contains_subrange {
+struct __fn {
+ template <forward_iterator _Iter1,
+ sentinel_for<_Iter1> _Sent1,
+ forward_iterator _Iter2,
+ sentinel_for<_Iter2> _Sent2,
+ class _Pred = ranges::equal_to,
+ class _Proj1 = identity,
+ class _Proj2 = identity>
+ requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
+ _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool static operator()(
+ _Iter1 __first1,
+ _Sent1 __last1,
+ _Iter2 __first2,
+ _Sent2 __last2,
+ _Pred __pred = {},
+ _Proj1 __proj1 = {},
+ _Proj2 __proj2 = {}) {
+ auto __n2 = ranges::distance(__first2, __last2);
+ if (__n2 == 0)
+ return true;
+
+ auto __ret = ranges::search(
+ std::move(__first1), __last1, std::move(__first2), __last2, __pred, std::ref(__proj1), std::ref(__proj2));
+ return __ret.empty() == false;
+ }
+
+ template <forward_range _Range1,
+ forward_range _Range2,
+ class _Pred = ranges::equal_to,
+ class _Proj1 = identity,
+ class _Proj2 = identity>
+ requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>, _Pred, _Proj1, _Proj2>
+ _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool static
+ operator()(_Range1&& __range1, _Range2&& __range2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) {
+ auto __n2 = 0;
+ if constexpr (sized_range<_Range2>) {
+ __n2 = ranges::size(__range2);
+ } else {
+ __n2 = std::distance(cbegin(__range2), cend(__range2));
+ }
+ if (__n2 == 0)
+ return true;
+
+ auto __ret = ranges::search(__range1, __range2, __pred, std::ref(__proj1), std::ref(__proj2));
+ return __ret.empty() == false;
+ }
+};
+} // namespace __contains_subrange
+
+inline namespace __cpo {
+inline constexpr auto contains_subrange = __contains_subrange::__fn{};
+} // namespace __cpo
+} // namespace ranges
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_STD_VER >= 23
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___ALGORITHM_RANGES_CONTAINS_SUBRANGE_H
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index 1176602a2b6951..70e30bc87e8128 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -217,6 +217,19 @@ namespace ranges {
constexpr ranges::minmax_element_result<borrowed_iterator_t<R>>
minmax_element(R&& r, Comp comp = {}, Proj proj = {}); // since C++20
+ template<forward_iterator I1, sentinel_for<I1> S1,
+ forward_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 contains_subrange(I1 first1, S1 last1, I2 first2, S2 last2,
+ Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++23
+
+ template<forward_range R1, forward_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 contains_subrange(R1&& r1, R2&& r2, Pred pred = {},
+ Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++23
+
template<class I, class O>
using copy_result = in_out_result<I, O>; // since C++20
@@ -1875,6 +1888,7 @@ template <class BidirectionalIterator, class Compare>
#include <__algorithm/ranges_binary_search.h>
#include <__algorithm/ranges_clamp.h>
#include <__algorithm/ranges_contains.h>
+#include <__algorithm/ranges_contains_subrange.h>
#include <__algorithm/ranges_copy.h>
#include <__algorithm/ranges_copy_backward.h>
#include <__algorithm/ranges_copy_if.h>
diff --git a/libcxx/include/libcxx.imp b/libcxx/include/libcxx.imp
index 69de4705f37886..3f056d418f47cf 100644
--- a/libcxx/include/libcxx.imp
+++ b/libcxx/include/libcxx.imp
@@ -110,6 +110,7 @@
{ include: [ "<__algorithm/ranges_binary_search.h>", "private", "<algorithm>", "public" ] },
{ include: [ "<__algorithm/ranges_clamp.h>", "private", "<algorithm>", "public" ] },
{ include: [ "<__algorithm/ranges_contains.h>", "private", "<algorithm>", "public" ] },
+ { include: [ "<__algorithm/ranges_contains_subrange.h>", "private", "<algorithm>", "public" ] },
{ include: [ "<__algorithm/ranges_copy.h>", "private", "<algorithm>", "public" ] },
{ include: [ "<__algorithm/ranges_copy_backward.h>", "private", "<algorithm>", "public" ] },
{ include: [ "<__algorithm/ranges_copy_if.h>", "private", "<algorithm>", "public" ] },
diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index f3246f808b9d02..63af3a90d88b9b 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -778,6 +778,7 @@ module std_private_algorithm_ranges_clamp [system
export std_private_functional_ranges_operations
}
module std_private_algorithm_ranges_contains [system] { header "__algorithm/ranges_contains.h" }
+module std_private_algorithm_ranges_contains_subrange [system] { header "__algorithm/ranges_contains_subrange.h" }
module std_private_algorithm_ranges_copy [system] {
header "__algorithm/ranges_copy.h"
export std_private_algorithm_in_out_result
diff --git a/libcxx/modules/std/algorithm.inc b/libcxx/modules/std/algorithm.inc
index 75e8a3af78dea2..e7796bfa26af81 100644
--- a/libcxx/modules/std/algorithm.inc
+++ b/libcxx/modules/std/algorithm.inc
@@ -46,9 +46,7 @@ export namespace std {
// [alg.contains], contains
namespace ranges {
using std::ranges::contains;
-#if 0
using std::ranges::contains_subrange;
-#endif
} // namespace ranges
#endif // _LIBCPP_STD_VER >= 23
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 e96a57f4005e04..71823d9afc1a4b 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,6 +86,10 @@ constexpr bool all_the_algorithms()
assert(copies == 0);
(void)std::ranges::contains(a, value, Proj(&copies));
assert(copies == 0);
+ (void)std::ranges::contains_subrange(first, last, first2, last2, Equal(), Proj(&copies), Proj(&copies));
+ assert(copies == 0);
+ (void)std::ranges::contains_subrange(a, b, Equal(), Proj(&copies), Proj(&copies));
+ assert(copies == 0);
#endif
(void)std::ranges::count(first, last, value, Proj(&copies)); assert(copies == 0);
(void)std::ranges::count(a, value, Proj(&copies)); assert(copies == 0);
diff --git a/libcxx/test/libcxx/diagnostics/ranges.nodiscard_extensions.compile.pass.cpp b/libcxx/test/libcxx/diagnostics/ranges.nodiscard_extensions.compile.pass.cpp
index 19e07b83079c78..12f6b271583693 100644
--- a/libcxx/test/libcxx/diagnostics/ranges.nodiscard_extensions.compile.pass.cpp
+++ b/libcxx/test/libcxx/diagnostics/ranges.nodiscard_extensions.compile.pass.cpp
@@ -33,6 +33,8 @@ void test() {
#if TEST_STD_VER >= 23
std::ranges::contains(range, 1);
std::ranges::contains(iter, iter, 1);
+ std::ranges::contains_subrange(range, range);
+ std::ranges::contains_subrange(iter, iter, iter, iter);
#endif
std::ranges::count_if(range, pred);
std::ranges::count_if(iter, iter, pred);
diff --git a/libcxx/test/libcxx/diagnostics/ranges.nodiscard_extensions.verify.cpp b/libcxx/test/libcxx/diagnostics/ranges.nodiscard_extensions.verify.cpp
index 5e45ad086cbd08..57ce6ab1e6b11c 100644
--- a/libcxx/test/libcxx/diagnostics/ranges.nodiscard_extensions.verify.cpp
+++ b/libcxx/test/libcxx/diagnostics/ranges.nodiscard_extensions.verify.cpp
@@ -95,6 +95,10 @@ void test() {
// expected-warning at -1{{ignoring return value of function declared with 'nodiscard' attribute}}
std::ranges::contains(iter, iter, 1);
// expected-warning at -1{{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::contains_subrange(range, range);
+ // expected-warning at -1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::contains_subrange(iter, iter, iter, iter);
+ // expected-warning at -1 {{ignoring return value of function declared with 'nodiscard' attribute}}
std::ranges::fold_left(range, 0, std::plus());
// expected-warning at -1{{ignoring return value of function declared with 'nodiscard' attribute}}
std::ranges::fold_left(iter, iter, 0, std::plus());
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains_subrange.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains_subrange.pass.cpp
new file mode 100644
index 00000000000000..d48ee9e4e7e02e
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains_subrange.pass.cpp
@@ -0,0 +1,320 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=2000000
+
+// template<forward_iterator I1, sentinel_for<I1> S1,
+// forward_iterator I2, sentinel_for<I2> S2, class Proj = identity>
+// requires indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
+// constexpr bool ranges::contains_subrange(I1 first1, S1 last1, I2 first2, S2 last2,
+// Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++23
+
+// template<forward_range R1, forward_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::contains_subrange(R1&& r1, R2&& r2, Pred pred = {},
+// Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++23
+
+#include <algorithm>
+#include <cassert>
+#include <concepts>
+#include <ranges>
+#include <utility>
+
+#include "almost_satisfies_types.h"
+#include "test_iterators.h"
+
+struct NotEqualityComparable {};
+
+template <class Iter1, class Sent1 = Iter1, class Iter2 = int*, class Sent2 = Iter2>
+concept HasContainsSubrangeIt = requires(Iter1 first1, Sent1 last1, Iter2 first2, Sent2 last2) {
+ std::ranges::contains_subrange(first1, last1, first2, last2);
+};
+
+static_assert(HasContainsSubrangeIt<int*>);
+static_assert(!HasContainsSubrangeIt<ForwardIteratorNotDerivedFrom>);
+static_assert(!HasContainsSubrangeIt<ForwardIteratorNotIncrementable>);
+static_assert(!HasContainsSubrangeIt<int*, SentinelForNotSemiregular>);
+static_assert(!HasContainsSubrangeIt<int*, int*, int**>); // not indirectly comparable
+static_assert(!HasContainsSubrangeIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
+static_assert(!HasContainsSubrangeIt<int*, int*, ForwardIteratorNotDerivedFrom>);
+static_assert(!HasContainsSubrangeIt<int*, int*, ForwardIteratorNotIncrementable>);
+static_assert(!HasContainsSubrangeIt<int*, int*, int*, SentinelForNotSemiregular>);
+static_assert(!HasContainsSubrangeIt<int*, int*, int*, SentinelForNotWeaklyEqualityComparableWith>);
+
+template <class Range1, class Range2 = UncheckedRange<int*>>
+concept HasContainsSubrangeR = requires(Range1&& range1, Range2&& range2) {
+ std::ranges::contains_subrange(std::forward<Range1>(range1), std::forward<Range2>(range2));
+};
+
+static_assert(HasContainsSubrangeR<UncheckedRange<int*>>);
+static_assert(!HasContainsSubrangeR<ForwardRangeNotDerivedFrom>);
+static_assert(!HasContainsSubrangeR<ForwardIteratorNotIncrementable>);
+static_assert(!HasContainsSubrangeR<ForwardRangeNotSentinelSemiregular>);
+static_assert(!HasContainsSubrangeR<ForwardRangeNotSentinelEqualityComparableWith>);
+static_assert(!HasContainsSubrangeR<UncheckedRange<int*>, UncheckedRange<int**>>); // not indirectly comparable
+static_assert(!HasContainsSubrangeR<UncheckedRange<int*>, ForwardRangeNotDerivedFrom>);
+static_assert(!HasContainsSubrangeR<UncheckedRange<int*>, ForwardRangeNotIncrementable>);
+static_assert(!HasContainsSubrangeR<UncheckedRange<int*>, ForwardRangeNotSentinelSemiregular>);
+static_assert(!HasContainsSubrangeR<UncheckedRange<int*>, ForwardRangeNotSentinelEqualityComparableWith>);
+
+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[] = {3, 4, 5};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(std::end(a))));
+ auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(std::end(p))));
+ {
+ std::same_as<bool> decltype(auto) ret =
+ std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end());
+ assert(ret);
+ }
+ {
+ std::same_as<bool> decltype(auto) ret = std::ranges::contains_subrange(whole, subrange);
+ assert(ret);
+ }
+ }
+
+ { // no match
+ int a[] = {1, 2, 3, 4, 5, 6};
+ int p[] = {3, 4, 2};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(std::end(a))));
+ auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(std::end(p))));
+ {
+ bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end());
+ assert(!ret);
+ }
+ {
+ bool ret = std::ranges::contains_subrange(whole, subrange);
+ assert(!ret);
+ }
+ }
+
+ { // range consists of just one element
+ int a[] = {3};
+ int p[] = {3, 4, 2};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(std::end(a))));
+ auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(std::end(p))));
+ {
+ bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end());
+ assert(!ret);
+ }
+ {
+ bool ret = std::ranges::contains_subrange(whole, subrange);
+ assert(!ret);
+ }
+ }
+
+ { // subrange consists of just one element
+ int a[] = {23, 1, 20, 3, 54, 2};
+ int p[] = {3};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(std::end(a))));
+ auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(std::end(p))));
+ {
+ bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end());
+ assert(ret);
+ }
+ {
+ bool ret = std::ranges::contains_subrange(whole, subrange);
+ assert(ret);
+ }
+ }
+
+ { // range has zero length
+ int a[] = {};
+ int p[] = {3, 4, 2};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a)));
+ auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(std::end(p))));
+ {
+ bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end());
+ assert(!ret);
+ }
+ {
+ bool ret = std::ranges::contains_subrange(whole, subrange);
+ assert(!ret);
+ }
+ }
+
+ { // subrange has zero length
+ int a[] = {3, 4, 2};
+ int p[] = {};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(std::end(a))));
+ auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p)));
+ {
+ bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end());
+ assert(ret);
+ }
+ {
+ bool ret = std::ranges::contains_subrange(whole, subrange);
+ assert(ret);
+ }
+ }
+
+ { // range and subrange both have zero length
+ int a[] = {};
+ int p[] = {};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a)));
+ auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p)));
+ {
+ bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end());
+ assert(ret);
+ }
+ {
+ bool ret = std::ranges::contains_subrange(whole, subrange);
+ assert(ret);
+ }
+ }
+
+ { // range and subrange are identical
+ int a[] = {3, 4, 11, 32, 54, 2};
+ int p[] = {3, 4, 11, 32, 54, 2};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(std::end(a))));
+ auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(std::end(p))));
+ {
+ bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end());
+ assert(ret);
+ }
+ {
+ bool ret = std::ranges::contains_subrange(whole, subrange);
+ assert(ret);
+ }
+ }
+
+ { // subrange is longer than range
+ int a[] = {3, 4, 2};
+ int p[] = {23, 3, 4, 2, 11, 32, 54, 2};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(std::end(a))));
+ auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(std::end(p))));
+ {
+ bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end());
+ assert(!ret);
+ }
+ {
+ bool ret = std::ranges::contains_subrange(whole, subrange);
+ assert(!ret);
+ }
+ }
+
+ { // subrange is the prefix
+ int a[] = {3, 43, 5, 100, 433, 278, 6457, 900};
+ int p[] = {3, 43, 5};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(std::end(a))));
+ auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(std::end(p))));
+ {
+ bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end());
+ assert(ret);
+ }
+ {
+ bool ret = std::ranges::contains_subrange(whole, subrange);
+ assert(ret);
+ }
+ }
+
+ { // subrange is the suffix
+ int a[] = {3, 43, 5, 7, 68, 100, 433, 900};
+ int p[] = {100, 433, 900};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(std::end(a))));
+ auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(std::end(p))));
+ {
+ bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end());
+ assert(ret);
+ }
+ {
+ bool ret = std::ranges::contains_subrange(whole, subrange);
+ assert(ret);
+ }
+ }
+
+ { // subrange is a subsequence
+ int a[] = {23, 1, 0, 54, 2};
+ int p[] = {1, 0, 2};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(std::end(a))));
+ auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(std::end(p))));
+ {
+ bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end());
+ assert(!ret);
+ }
+ {
+ bool ret = std::ranges::contains_subrange(whole, subrange);
+ assert(!ret);
+ }
+ }
+
+ { // repeated subrange
+ int a[] = {23, 1, 0, 2, 54, 1, 0, 2, 23, 33};
+ int p[] = {1, 0, 2};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(std::end(a))));
+ auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(std::end(p))));
+ {
+ bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end());
+ assert(ret);
+ }
+ {
+ bool ret = std::ranges::contains_subrange(whole, subrange);
+ assert(ret);
+ }
+ }
+
+ { // check that the predicate is used
+ int a[] = {23, 81, 61, 0, 42, 25, 1, 2, 1, 29, 2};
+ int p[] = {-1, -2, -1};
+ auto pred = [](int l, int r) { return l * -1 == r; };
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(std::end(a))));
+ auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(std::end(p))));
+ {
+ bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end(), pred);
+ assert(ret);
+ }
+ {
+ bool ret = std::ranges::contains_subrange(whole, subrange, pred);
+ assert(ret);
+ }
+ }
+
+ { // check that the projections are used
+ int a[] = {1, 3, 15, 1, 2, 1, 8};
+ int p[] = {2, 1, 2};
+ auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(std::end(a))));
+ auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(std::end(p))));
+ auto proj1 = [](int i) { return i - 3; };
+ auto proj2 = [](int i) { return i * -1; };
+ {
+ bool ret = std::ranges::contains_subrange(
+ whole.begin(), whole.end(), subrange.begin(), subrange.end(), {}, proj1, proj2);
+ assert(ret);
+ }
+ {
+ bool ret = std::ranges::contains_subrange(whole, subrange, {}, proj1, proj2);
+ assert(ret);
+ }
+ }
+}
+
+constexpr bool test() {
+ types::for_each(types::forward_iterator_list<int*>{}, []<class Iter1> {
+ types::for_each(types::forward_iterator_list<int*>{}, []<class Iter2> {
+ test_iterators<Iter1, Iter1, Iter2, Iter2>();
+ test_iterators<Iter1, Iter1, Iter2, sized_sentinel<Iter2>>();
+ test_iterators<Iter1, sized_sentinel<Iter1>, Iter2, 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/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 494e9fd19c3545..9506ca1c768bd7 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
@@ -67,6 +67,7 @@ static_assert(test(std::ranges::binary_search, a, 42));
static_assert(test(std::ranges::clamp, 42, 42, 42));
#if TEST_STD_VER >= 23
static_assert(test(std::ranges::contains, a, 42));
+static_assert(test(std::ranges::contains_subrange, a, a));
#endif
static_assert(test(std::ranges::copy, a, a));
static_assert(test(std::ranges::copy_backward, a, a));
More information about the libcxx-commits
mailing list