From a08673d04a99efe200fb53f3ef57b5cfb8e513bb Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Mon, 24 May 2021 16:48:41 -0700 Subject: [PATCH] Add a range-based wrapper for std::unique(begin, end, binary_predicate) --- llvm/include/llvm/ADT/STLExtras.h | 5 +++++ llvm/unittests/ADT/STLExtrasTest.cpp | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h index 182451c2..430da0f 100644 --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -1631,6 +1631,11 @@ auto partition_point(R &&Range, Predicate P) { return std::partition_point(adl_begin(Range), adl_end(Range), P); } +template +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 diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp index 8626530..582f182 100644 --- a/llvm/unittests/ADT/STLExtrasTest.cpp +++ b/llvm/unittests/ADT/STLExtrasTest.cpp @@ -711,4 +711,18 @@ TEST(STLExtras, MoveRange) { EXPECT_EQ(V4.size(), 4U); EXPECT_TRUE(llvm::all_of(V4, HasVal)); } + +TEST(STLExtras, Unique) { + std::vector 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 -- 2.7.4