Skip to content

[llvm][ADT] Add wrappers to std::includes #143297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions llvm/include/llvm/ADT/STLExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions llvm/unittests/ADT/STLExtrasTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};

Expand Down
Loading