[llvm] e30cba5 - [llvm][ADT] Add wrappers to `std::includes` (#143297)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 9 20:15:25 PDT 2025
Author: Longsheng Mou
Date: 2025-06-10T11:15:22+08:00
New Revision: e30cba54641d593fdc0e870cb589c7ae265703fe
URL: https://github.com/llvm/llvm-project/commit/e30cba54641d593fdc0e870cb589c7ae265703fe
DIFF: https://github.com/llvm/llvm-project/commit/e30cba54641d593fdc0e870cb589c7ae265703fe.diff
LOG: [llvm][ADT] Add wrappers to `std::includes` (#143297)
Add `llvm::includes` that accepts a range instead of start/end iterator.
Added:
Modified:
llvm/include/llvm/ADT/STLExtras.h
llvm/unittests/ADT/STLExtrasTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 897dc76a420b2..eea06cfb99ba2 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -1940,6 +1940,28 @@ template <typename R> bool is_sorted(R &&Range) {
return std::is_sorted(adl_begin(Range), adl_end(Range));
}
+/// Provide wrappers to std::includes which take ranges instead of having to
+/// pass begin/end explicitly.
+/// This function checks if the sorted range \p R2 is a subsequence of the
+/// sorted range \p R1. The ranges must be sorted in non-descending order.
+template <typename R1, typename R2> bool includes(R1 &&Range1, R2 &&Range2) {
+ assert(is_sorted(Range1) && "Range1 must be sorted in non-descending order");
+ assert(is_sorted(Range2) && "Range2 must be sorted in non-descending order");
+ return std::includes(adl_begin(Range1), adl_end(Range1), adl_begin(Range2),
+ adl_end(Range2));
+}
+
+/// This function checks if the sorted range \p R2 is a subsequence of the
+/// sorted range \p R1. The ranges must be sorted with respect to a comparator
+/// \p C.
+template <typename R1, typename R2, typename Compare>
+bool includes(R1 &&Range1, R2 &&Range2, Compare &&C) {
+ assert(is_sorted(Range1, C) && "Range1 must be sorted with respect to C");
+ assert(is_sorted(Range2, C) && "Range2 must be sorted with respect to C");
+ return std::includes(adl_begin(Range1), adl_end(Range1), adl_begin(Range2),
+ adl_end(Range2), std::forward<Compare>(C));
+}
+
/// Wrapper function around std::count to count the number of times an element
/// \p Element occurs in the given range \p Range.
template <typename R, typename E> auto count(R &&Range, const E &Element) {
diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp
index 0101be47a6869..286cfa745fd14 100644
--- a/llvm/unittests/ADT/STLExtrasTest.cpp
+++ b/llvm/unittests/ADT/STLExtrasTest.cpp
@@ -1567,6 +1567,30 @@ TEST(STLExtrasTest, Mismatch) {
}
}
+TEST(STLExtrasTest, Includes) {
+ {
+ std::vector<int> V1 = {1, 2};
+ std::vector<int> V2;
+ EXPECT_TRUE(includes(V1, V2));
+ EXPECT_FALSE(includes(V2, V1));
+ V2.push_back(1);
+ EXPECT_TRUE(includes(V1, V2));
+ V2.push_back(3);
+ EXPECT_FALSE(includes(V1, V2));
+ }
+
+ {
+ std::vector<int> V1 = {3, 2, 1};
+ std::vector<int> V2;
+ EXPECT_TRUE(includes(V1, V2, std::greater<>()));
+ EXPECT_FALSE(includes(V2, V1, std::greater<>()));
+ V2.push_back(3);
+ EXPECT_TRUE(includes(V1, V2, std::greater<>()));
+ V2.push_back(0);
+ EXPECT_FALSE(includes(V1, V2, std::greater<>()));
+ }
+}
+
struct Foo;
struct Bar {};
More information about the llvm-commits
mailing list