Skip to content

Commit 6e87f5a

Browse files
committed
[llvm][ADT] Add wrappers to std::includes
Add `llvm::includes` that accepts a range instead of start/end iterator.
1 parent cb647ec commit 6e87f5a

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

llvm/include/llvm/ADT/STLExtras.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,6 +1940,28 @@ template <typename R> bool is_sorted(R &&Range) {
19401940
return std::is_sorted(adl_begin(Range), adl_end(Range));
19411941
}
19421942

1943+
/// Provide wrappers to std::includes which take ranges instead of having to
1944+
/// pass begin/end explicitly.
1945+
/// This function checks if the sorted range \p R2 is a subsequence of the
1946+
/// sorted range \p R1. The ranges must be sorted in non-descending order.
1947+
template <typename R1, typename R2> bool includes(R1 &&Range1, R2 &&Range2) {
1948+
assert(is_sorted(Range1) && "Range1 must be sorted in non-descending order");
1949+
assert(is_sorted(Range2) && "Range2 must be sorted in non-descending order");
1950+
return std::includes(adl_begin(Range1), adl_end(Range1), adl_begin(Range2),
1951+
adl_end(Range2));
1952+
}
1953+
1954+
/// This function checks if the sorted range \p R2 is a subsequence of the
1955+
/// sorted range \p R1. The ranges must be sorted with respect to a comparator
1956+
/// \p C.
1957+
template <typename R1, typename R2, typename Compare>
1958+
bool includes(R1 &&Range1, R2 &&Range2, Compare &&C) {
1959+
assert(is_sorted(Range1, C) && "Range1 must be sorted with respect to C");
1960+
assert(is_sorted(Range2, C) && "Range2 must be sorted with respect to C");
1961+
return std::includes(adl_begin(Range1), adl_end(Range1), adl_begin(Range2),
1962+
adl_end(Range2), std::forward<Compare>(C));
1963+
}
1964+
19431965
/// Wrapper function around std::count to count the number of times an element
19441966
/// \p Element occurs in the given range \p Range.
19451967
template <typename R, typename E> auto count(R &&Range, const E &Element) {

llvm/unittests/ADT/STLExtrasTest.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,6 +1567,30 @@ TEST(STLExtrasTest, Mismatch) {
15671567
}
15681568
}
15691569

1570+
TEST(STLExtrasTest, Includes) {
1571+
{
1572+
std::vector<int> V1 = {1, 2};
1573+
std::vector<int> V2;
1574+
EXPECT_TRUE(includes(V1, V2));
1575+
EXPECT_FALSE(includes(V2, V1));
1576+
V2.push_back(1);
1577+
EXPECT_TRUE(includes(V1, V2));
1578+
V2.push_back(3);
1579+
EXPECT_FALSE(includes(V1, V2));
1580+
}
1581+
1582+
{
1583+
std::vector<int> V1 = {3, 2, 1};
1584+
std::vector<int> V2;
1585+
EXPECT_TRUE(includes(V1, V2, deref<std::greater<>>()));
1586+
EXPECT_FALSE(includes(V2, V1, deref<std::greater<>>()));
1587+
V2.push_back(3);
1588+
EXPECT_TRUE(includes(V1, V2, deref<std::greater<>>()));
1589+
V2.push_back(0);
1590+
EXPECT_FALSE(includes(V1, V2, deref<std::greater<>>()));
1591+
}
1592+
}
1593+
15701594
struct Foo;
15711595
struct Bar {};
15721596

0 commit comments

Comments
 (0)