Add a range-based wrapper for std::unique(begin, end, binary_predicate)

This commit is contained in:
David Blaikie 2021-05-24 16:48:41 -07:00
parent 676a789a5b
commit a08673d04a
2 changed files with 19 additions and 0 deletions

View File

@ -1631,6 +1631,11 @@ auto partition_point(R &&Range, Predicate P) {
return std::partition_point(adl_begin(Range), adl_end(Range), P);
}
template<typename Range, typename Predicate>
auto unique(Range &&R, Predicate P) {
return std::unique(adl_begin(R), adl_end(R), P);
}
/// Wrapper function around std::equal to detect if all elements
/// in a container are same.
template <typename R>

View File

@ -711,4 +711,18 @@ TEST(STLExtras, MoveRange) {
EXPECT_EQ(V4.size(), 4U);
EXPECT_TRUE(llvm::all_of(V4, HasVal));
}
TEST(STLExtras, Unique) {
std::vector<int> V = {1, 5, 5, 4, 3, 3, 3};
auto I = llvm::unique(V, [](int a, int b) { return a == b; });
EXPECT_EQ(I, V.begin() + 4);
EXPECT_EQ(1, V[0]);
EXPECT_EQ(5, V[1]);
EXPECT_EQ(4, V[2]);
EXPECT_EQ(3, V[3]);
}
} // namespace