https://github.com/ZijunZhaoCCK updated https://github.com/llvm/llvm-project/pull/66963
>From 5a2c930770cf548c5e3f3451e76b48cb067e6762 Mon Sep 17 00:00:00 2001 From: Zijun Zhao <zijunz...@google.com> Date: Wed, 13 Sep 2023 14:26:01 -0700 Subject: [PATCH 1/5] [libc++] Implement ranges::contains_subrange --- libcxx/include/CMakeLists.txt | 1 + .../__algorithm/ranges_contains_subrange.h | 145 +++++++++ libcxx/include/algorithm | 14 + ...obust_against_copying_projections.pass.cpp | 4 + .../ranges.contains_subrange.pass.cpp | 293 ++++++++++++++++++ .../niebloid.compile.pass.cpp | 3 + 6 files changed, 460 insertions(+) create mode 100644 libcxx/include/__algorithm/ranges_contains_subrange.h create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains_subrange.pass.cpp diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt index 2ec755236dbaee2..b096259f85f6ac8 100644 --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -104,6 +104,7 @@ set(files __algorithm/ranges_any_of.h __algorithm/ranges_binary_search.h __algorithm/ranges_clamp.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 000000000000000..16de6c29cb2a1a4 --- /dev/null +++ b/libcxx/include/__algorithm/ranges_contains_subrange.h @@ -0,0 +1,145 @@ +//===----------------------------------------------------------------------===// +// +// 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_starts_with.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 <__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 __contains_subrange { +struct __fn { + template <input_iterator _Iter1, + sentinel_for<_Iter1> _Sent1, + input_iterator _Iter2, + sentinel_for<_Iter2> _Sent2, + class _Pred, + class _Proj1, + class _Proj2, + class _Offset> + static _LIBCPP_HIDE_FROM_ABI constexpr bool __contains_subrange_fn_impl( + _Iter1 __first1, + _Sent1 __last1, + _Iter2 __first2, + _Sent2 __last2, + _Pred& __pred, + _Proj1& __proj1, + _Proj2& __proj2, + _Offset __offset) { + if (__offset < 0) + return false; + else { + for (; __offset >= 0; __offset--, __first1++) { + auto result = ranges::starts_with( + std::move(__first1), + std::move(__last1), + std::move(__first2), + std::move(__last2), + std::ref(__pred), + std::ref(__proj1), + std::ref(__proj2)); + if (result) + return true; + } + return false; + } + } + + 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 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); + auto __offset = __n1 - __n2; + + return __contains_subrange_fn_impl( + std::move(__first1), + std::move(__last1), + std::move(__first2), + std::move(__last2), + __pred, + __proj1, + __proj2, + std::move(__offset)); + } + + template <input_range _Range1, + input_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 operator()( + _Range1&& __range1, _Range2&& __range2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const { + auto __n1 = 0; + auto __n2 = 0; + + if constexpr (sized_range<_Range1> && sized_range<_Range2>) { + __n1 = ranges::size(__range1); + __n2 = ranges::size(__range2); + } else { + __n1 = ranges::distance(ranges::begin(__range1), ranges::end(__range1)); + __n2 = ranges::distance(ranges::begin(__range2), ranges::end(__range2)); + } + + auto __offset = __n1 - __n2; + return __contains_subrange_fn_impl( + ranges::begin(__range1), + ranges::end(__range1), + ranges::begin(__range2), + ranges::end(__range2), + __pred, + __proj1, + __proj2, + __offset); + } +}; +} // 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 + +#endif // _LIBCPP___ALGORITHM_RANGES_CONTAINS_SUBRANGE_H \ No newline at end of file diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm index 69ba9537dda6984..bb75d51300356d5 100644 --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -214,6 +214,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 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 + template<class I, class O> using copy_result = in_out_result<I, O>; // since C++20 @@ -1843,6 +1856,7 @@ template <class BidirectionalIterator, class Compare> #include <__algorithm/ranges_any_of.h> #include <__algorithm/ranges_binary_search.h> #include <__algorithm/ranges_clamp.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/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp index afbbc224ea8644c..3c607420726218d 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 @@ -80,6 +80,10 @@ constexpr bool all_the_algorithms() (void)std::ranges::binary_search(first, last, value, Less(), Proj(&copies)); assert(copies == 0); (void)std::ranges::binary_search(a, value, Less(), Proj(&copies)); assert(copies == 0); (void)std::ranges::clamp(T(), T(), T(), Less(), Proj(&copies)); assert(copies == 0); +#if TEST_STD_VER >= 23 + (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); (void)std::ranges::count_if(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0); 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 000000000000000..fd4d858b255db76 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains_subrange.pass.cpp @@ -0,0 +1,293 @@ +//===----------------------------------------------------------------------===// +// +// 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<input_iterator I, sentinel_for<I> S, class T, class Proj = identity> +// requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*> +// constexpr bool ranges::contains(I first, S last, const T& value, Proj proj = {}); // since C++23 + +// template<input_range R, class T, class Proj = identity> +// requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*> +// constexpr bool ranges::contains(R&& r, const T& value, Proj proj = {}); // since C++23 + +#include <algorithm> +#include <array> +#include <cassert> +#include <ranges> +#include <vector> + +#include "almost_satisfies_types.h" +#include "boolean_testable.h" +#include "test_iterators.h" + +struct NotEqualityComparable {}; + +template <class Iter1, class Sent1 = Iter1, class Iter2 = int*, class Sent2 = Iter2> +concept HasContainsSubrangeSubrangeIt = requires(Iter1 first1, Sent1 last1, Iter2 first2, Sent2 last2) { + std::ranges::contains_subrange(first1, last1, first2, last2); +}; + +static_assert(HasContainsSubrangeSubrangeIt<int*>); +static_assert(!HasContainsSubrangeSubrangeIt<NotEqualityComparable*>); +static_assert(!HasContainsSubrangeSubrangeIt<InputIteratorNotDerivedFrom>); +static_assert(!HasContainsSubrangeSubrangeIt<InputIteratorNotIndirectlyReadable>); +static_assert(!HasContainsSubrangeSubrangeIt<InputIteratorNotInputOrOutputIterator>); +static_assert(!HasContainsSubrangeSubrangeIt<cpp20_input_iterator<int*>, SentinelForNotSemiregular>); +static_assert(!HasContainsSubrangeSubrangeIt<cpp20_input_iterator<int*>, InputRangeNotSentinelEqualityComparableWith>); +static_assert(!HasContainsSubrangeSubrangeIt<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>); + +static_assert(!HasContainsSubrangeSubrangeIt<int*, int>); +static_assert(!HasContainsSubrangeSubrangeIt<int, int*>); +static_assert(HasContainsSubrangeSubrangeIt<int*, int*>); + +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>); + +static std::vector<int> comparable_data; + +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(a + 6))); + auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3))); + { + [[maybe_unused]] std::same_as<bool> decltype(auto) ret = + std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end()); + assert(ret); + } + { + [[maybe_unused]] 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(a + 6))); + auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3))); + { + 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(a + 1))); + auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3))); + { + 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(a + 6))); + auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 1))); + { + 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(p + 3))); + { + 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(a + 3))); + 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(a + 6))); + auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 6))); + { + 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(a + 3))); + auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 8))); + { + 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 subsequence + int a[] = {23, 1, 0, 54, 2}; + int p[] = {1, 0, 2}; + auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 5))); + auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3))); + { + 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(a + 10))); + auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3))); + { + 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(a + 11))); + auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3))); + { + 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(a + 7))); + auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3))); + { + bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end(), {}, + [](int i) { return i - 3; }, + [](int i) { return i * -1; }); + assert(ret); + } + { + bool ret = std::ranges::contains_subrange(whole, subrange, {}, + [](int i) { return i - 3; }, + [](int i) { return i * -1; }); + assert(ret); + } + } + + { // check the nodiscard extension + // use #pragma around to suppress error: ignoring return value of function + // declared with 'nodiscard' attribute [-Werror,-Wunused-result] + #pragma clang diagnostic push + #pragma clang diagnostic ignored "-Wunused-result" + int a[] = {1, 9, 0, 13, 25}; + int p[] = {1, 9, 0}; + auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 5))); + auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3))); + std::ranges::contains_subrange(whole, subrange); + #pragma clang diagnostic pop + } +} + +constexpr bool test() { + 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; +} \ No newline at end of file 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 683f88c19f67849..cdab951547a3f31 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 @@ -65,6 +65,9 @@ static_assert(test(std::ranges::all_of, a, odd)); static_assert(test(std::ranges::any_of, a, odd)); 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_subrange, a, a)); +#endif static_assert(test(std::ranges::copy, a, a)); static_assert(test(std::ranges::copy_backward, a, a)); static_assert(test(std::ranges::copy_if, a, a, odd)); >From 13ec75cbfea510ca89a91c10875b061dc476df44 Mon Sep 17 00:00:00 2001 From: Zijun Zhao <zijunz...@google.com> Date: Wed, 20 Sep 2023 18:41:04 -0700 Subject: [PATCH 2/5] format --- libcxx/include/__algorithm/ranges_contains_subrange.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libcxx/include/__algorithm/ranges_contains_subrange.h b/libcxx/include/__algorithm/ranges_contains_subrange.h index 16de6c29cb2a1a4..f8e885d3259be69 100644 --- a/libcxx/include/__algorithm/ranges_contains_subrange.h +++ b/libcxx/include/__algorithm/ranges_contains_subrange.h @@ -129,7 +129,7 @@ struct __fn { __proj1, __proj2, __offset); - } + } }; } // namespace __contains_subrange >From f8040f124b37c52790b6f767cd1febdfbc8dcc3f Mon Sep 17 00:00:00 2001 From: Zijun Zhao <zijunz...@google.com> Date: Thu, 21 Sep 2023 11:06:02 -0700 Subject: [PATCH 3/5] Add a newline --- libcxx/include/__algorithm/ranges_contains_subrange.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libcxx/include/__algorithm/ranges_contains_subrange.h b/libcxx/include/__algorithm/ranges_contains_subrange.h index f8e885d3259be69..8fdd460c8d6f345 100644 --- a/libcxx/include/__algorithm/ranges_contains_subrange.h +++ b/libcxx/include/__algorithm/ranges_contains_subrange.h @@ -142,4 +142,4 @@ _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP_STD_VER >= 23 -#endif // _LIBCPP___ALGORITHM_RANGES_CONTAINS_SUBRANGE_H \ No newline at end of file +#endif // _LIBCPP___ALGORITHM_RANGES_CONTAINS_SUBRANGE_H >From 5f33fa63e76b5d0c066773113447749c3428aa99 Mon Sep 17 00:00:00 2001 From: Zijun Zhao <zijunz...@google.com> Date: Tue, 26 Sep 2023 16:56:38 -0700 Subject: [PATCH 4/5] 1. call distance(r) directly 2. replace auto with int to avoid potential errors --- .../__algorithm/ranges_contains_subrange.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/libcxx/include/__algorithm/ranges_contains_subrange.h b/libcxx/include/__algorithm/ranges_contains_subrange.h index 8fdd460c8d6f345..6dd8324c5dfa699 100644 --- a/libcxx/include/__algorithm/ranges_contains_subrange.h +++ b/libcxx/include/__algorithm/ranges_contains_subrange.h @@ -40,7 +40,7 @@ struct __fn { class _Pred, class _Proj1, class _Proj2, - class _Offset> + class _Offset = int> static _LIBCPP_HIDE_FROM_ABI constexpr bool __contains_subrange_fn_impl( _Iter1 __first1, _Sent1 __last1, @@ -85,9 +85,9 @@ struct __fn { _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const { - auto __n1 = ranges::distance(__first1, __last1); - auto __n2 = ranges::distance(__first2, __last2); - auto __offset = __n1 - __n2; + int __n1 = ranges::distance(__first1, __last1); + int __n2 = ranges::distance(__first2, __last2); + int __offset = __n1 - __n2; return __contains_subrange_fn_impl( std::move(__first1), @@ -108,18 +108,18 @@ struct __fn { requires 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 { - auto __n1 = 0; - auto __n2 = 0; + int __n1 = 0; + int __n2 = 0; if constexpr (sized_range<_Range1> && sized_range<_Range2>) { __n1 = ranges::size(__range1); __n2 = ranges::size(__range2); } else { - __n1 = ranges::distance(ranges::begin(__range1), ranges::end(__range1)); - __n2 = ranges::distance(ranges::begin(__range2), ranges::end(__range2)); + __n1 = ranges::distance(__range1); + __n2 = ranges::distance(__range2); } - auto __offset = __n1 - __n2; + int __offset = __n1 - __n2; return __contains_subrange_fn_impl( ranges::begin(__range1), ranges::end(__range1), >From 4caf13d3bf3f35b9f653f7d121c58f93f49ace27 Mon Sep 17 00:00:00 2001 From: Zijun Zhao <zijunz...@google.com> Date: Sun, 1 Oct 2023 22:41:08 -0700 Subject: [PATCH 5/5] Reformat --- .../ranges.contains_subrange.pass.cpp | 115 +++++++++--------- 1 file changed, 59 insertions(+), 56 deletions(-) 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 index fd4d858b255db76..172c30ad8b979c7 100644 --- 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 @@ -51,7 +51,7 @@ static_assert(HasContainsSubrangeSubrangeIt<int*, int*>); 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)); }; + std::ranges::contains_subrange(std::forward<Range1>(range1), std::forward<Range2>(range2)); }; static_assert(HasContainsSubrangeR<UncheckedRange<int*>>); static_assert(HasContainsSubrangeR<ForwardRangeNotDerivedFrom>); @@ -69,26 +69,25 @@ static std::vector<int> comparable_data; 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(a + 6))); + int a[] = {1, 2, 3, 4, 5, 6}; + int p[] = {3, 4, 5}; + auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6))); auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3))); { [[maybe_unused]] std::same_as<bool> decltype(auto) ret = - std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end()); + std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end()); assert(ret); } { - [[maybe_unused]] std::same_as<bool> decltype(auto) ret = - std::ranges::contains_subrange(whole, subrange); + [[maybe_unused]] 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(a + 6))); + int a[] = {1, 2, 3, 4, 5, 6}; + int p[] = {3, 4, 2}; + auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6))); auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3))); { bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end()); @@ -101,9 +100,9 @@ constexpr void test_iterators() { } { // range consists of just one element - int a[] = {3}; - int p[] = {3, 4, 2}; - auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 1))); + int a[] = {3}; + int p[] = {3, 4, 2}; + auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 1))); auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3))); { bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end()); @@ -116,9 +115,9 @@ constexpr void test_iterators() { } { // 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(a + 6))); + int a[] = {23, 1, 20, 3, 54, 2}; + int p[] = {3}; + auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6))); auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 1))); { bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end()); @@ -131,9 +130,9 @@ constexpr void test_iterators() { } { // range has zero length - int a[] = {}; - int p[] = {3, 4, 2}; - auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a))); + 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(p + 3))); { bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end()); @@ -146,9 +145,9 @@ constexpr void test_iterators() { } { // subrange has zero length - int a[] = {3, 4, 2}; - int p[] = {}; - auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 3))); + int a[] = {3, 4, 2}; + int p[] = {}; + auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 3))); auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p))); { bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end()); @@ -161,9 +160,9 @@ constexpr void test_iterators() { } { // 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(a + 6))); + 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(a + 6))); auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 6))); { bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end()); @@ -176,9 +175,9 @@ constexpr void test_iterators() { } { // 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(a + 3))); + int a[] = {3, 4, 2}; + int p[] = {23, 3, 4, 2, 11, 32, 54, 2}; + auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 3))); auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 8))); { bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end()); @@ -191,9 +190,9 @@ constexpr void test_iterators() { } { // subrange is subsequence - int a[] = {23, 1, 0, 54, 2}; - int p[] = {1, 0, 2}; - auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 5))); + int a[] = {23, 1, 0, 54, 2}; + int p[] = {1, 0, 2}; + auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 5))); auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3))); { bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end()); @@ -206,9 +205,9 @@ constexpr void test_iterators() { } { // 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(a + 10))); + 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(a + 10))); auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3))); { bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end()); @@ -221,10 +220,10 @@ constexpr void test_iterators() { } { // 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(a + 11))); + 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(a + 11))); auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3))); { bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end(), pred); @@ -237,40 +236,44 @@ constexpr void test_iterators() { } { // 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(a + 7))); + int a[] = {1, 3, 15, 1, 2, 1, 8}; + int p[] = {2, 1, 2}; + auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 7))); auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3))); { - bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end(), {}, - [](int i) { return i - 3; }, - [](int i) { return i * -1; }); + bool ret = std::ranges::contains_subrange( + whole.begin(), + whole.end(), + subrange.begin(), + subrange.end(), + {}, + [](int i) { return i - 3; }, + [](int i) { return i * -1; }); assert(ret); } { - bool ret = std::ranges::contains_subrange(whole, subrange, {}, - [](int i) { return i - 3; }, - [](int i) { return i * -1; }); + bool ret = std::ranges::contains_subrange( + whole, subrange, {}, [](int i) { return i - 3; }, [](int i) { return i * -1; }); assert(ret); } } { // check the nodiscard extension - // use #pragma around to suppress error: ignoring return value of function - // declared with 'nodiscard' attribute [-Werror,-Wunused-result] - #pragma clang diagnostic push - #pragma clang diagnostic ignored "-Wunused-result" - int a[] = {1, 9, 0, 13, 25}; - int p[] = {1, 9, 0}; - auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 5))); +// use #pragma around to suppress error: ignoring return value of function +// declared with 'nodiscard' attribute [-Werror,-Wunused-result] +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunused-result" + int a[] = {1, 9, 0, 13, 25}; + int p[] = {1, 9, 0}; + auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 5))); auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3))); std::ranges::contains_subrange(whole, subrange); - #pragma clang diagnostic pop +#pragma clang diagnostic pop } } constexpr bool test() { - types::for_each(types::cpp20_input_iterator_list<int*>{}, []<class Iter2>() { + 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>(); _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits