[llvm] r290555 - [ADT] Add an llvm::erase_if utility to make the standard erase+remove_if

Chandler Carruth via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 26 15:30:44 PST 2016


Author: chandlerc
Date: Mon Dec 26 17:30:44 2016
New Revision: 290555

URL: http://llvm.org/viewvc/llvm-project?rev=290555&view=rev
Log:
[ADT] Add an llvm::erase_if utility to make the standard erase+remove_if
pattern easier to write.

Differential Revision: https://reviews.llvm.org/D28120

Modified:
    llvm/trunk/include/llvm/ADT/STLExtras.h
    llvm/trunk/unittests/ADT/STLExtrasTest.cpp

Modified: llvm/trunk/include/llvm/ADT/STLExtras.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/STLExtras.h?rev=290555&r1=290554&r2=290555&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/STLExtras.h (original)
+++ llvm/trunk/include/llvm/ADT/STLExtras.h Mon Dec 26 17:30:44 2016
@@ -815,6 +815,18 @@ auto partition(R &&Range, UnaryPredicate
   return std::partition(std::begin(Range), std::end(Range), P);
 }
 
+/// Provide a container algorithm similar to C++ Library Fundamentals v2's
+/// `erase_if` which is equivalent to:
+///
+///   C.erase(remove_if(C, pred), C.end());
+///
+/// This version works for any container with an erase method call accepting
+/// two iterators.
+template <typename Container, typename UnaryPredicate>
+void erase_if(Container &C, UnaryPredicate P) {
+  C.erase(remove_if(C, P), C.end());
+}
+
 //===----------------------------------------------------------------------===//
 //     Extra additions to <memory>
 //===----------------------------------------------------------------------===//

Modified: llvm/trunk/unittests/ADT/STLExtrasTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/STLExtrasTest.cpp?rev=290555&r1=290554&r2=290555&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/STLExtrasTest.cpp (original)
+++ llvm/trunk/unittests/ADT/STLExtrasTest.cpp Mon Dec 26 17:30:44 2016
@@ -297,4 +297,15 @@ TEST(STLExtrasTest, PartitionAdaptor) {
   EXPECT_EQ(7, V[7]);
 }
 
+TEST(STLExtrasTest, EraseIf) {
+  std::vector<int> V = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  erase_if(V, [](int i) { return i % 2 == 0; });
+  EXPECT_EQ(4u, V.size());
+  EXPECT_EQ(1, V[0]);
+  EXPECT_EQ(3, V[1]);
+  EXPECT_EQ(5, V[2]);
+  EXPECT_EQ(7, V[3]);
+}
+
 }




More information about the llvm-commits mailing list