[libcxx-commits] [libcxx] [libc++][ranges] Implement ranges::contains_subrange (PR #66963)
Konstantin Varlamov via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Oct 13 18:46:33 PDT 2023
================
@@ -0,0 +1,303 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 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(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
----------------
var-const wrote:
These are great test cases -- please also add a test case where both the range and the subrange have zero length (which should return `true`).
https://github.com/llvm/llvm-project/pull/66963
More information about the libcxx-commits
mailing list