[llvm] 317f782 - [ADT] Return bool from SmallPtrSet::remove_if()

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 26 05:29:29 PDT 2024


Author: Nikita Popov
Date: 2024-06-26T14:29:21+02:00
New Revision: 317f782ef2366950e6cdc0de9d35df7fcc15ec5d

URL: https://github.com/llvm/llvm-project/commit/317f782ef2366950e6cdc0de9d35df7fcc15ec5d
DIFF: https://github.com/llvm/llvm-project/commit/317f782ef2366950e6cdc0de9d35df7fcc15ec5d.diff

LOG: [ADT] Return bool from SmallPtrSet::remove_if()

Return whether anything was removed. This matches the API of
SetVector::remove_if() and is convenient for some future uses.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/SmallPtrSet.h
    llvm/unittests/ADT/SmallPtrSetTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index ed78c3da82fd5..7bf3d825fd03c 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -357,9 +357,11 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
     return erase_imp(PtrTraits::getAsVoidPointer(Ptr));
   }
 
-  /// Remove elements that match the given predicate.
+  /// Remove elements that match the given predicate. Returns whether anything
+  /// was removed.
   template <typename UnaryPredicate>
-  void remove_if(UnaryPredicate P) {
+  bool remove_if(UnaryPredicate P) {
+    bool Removed = false;
     for (const void **APtr = CurArray, **E = EndPointer(); APtr != E; ++APtr) {
       const void *Value = *APtr;
       if (Value == getTombstoneMarker() || Value == getEmptyMarker())
@@ -368,8 +370,10 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
       if (P(Ptr)) {
         *APtr = getTombstoneMarker();
         ++NumTombstones;
+        Removed = true;
       }
     }
+    return Removed;
   }
 
   /// count - Return 1 if the specified pointer is in the set, 0 otherwise.

diff  --git a/llvm/unittests/ADT/SmallPtrSetTest.cpp b/llvm/unittests/ADT/SmallPtrSetTest.cpp
index 6aced96c217ca..1ed9133c2b551 100644
--- a/llvm/unittests/ADT/SmallPtrSetTest.cpp
+++ b/llvm/unittests/ADT/SmallPtrSetTest.cpp
@@ -422,8 +422,9 @@ TEST(SmallPtrSetTest, RemoveIf) {
   Set.erase(&Vals[0]); // Leave a tombstone.
 
   // Remove odd elements.
-  Set.remove_if([](int *Ptr) { return *Ptr % 2 != 0; });
+  bool Removed = Set.remove_if([](int *Ptr) { return *Ptr % 2 != 0; });
   // We should only have element 2 left now.
+  EXPECT_TRUE(Removed);
   EXPECT_EQ(Set.size(), 1u);
   EXPECT_TRUE(Set.contains(&Vals[2]));
 
@@ -436,9 +437,13 @@ TEST(SmallPtrSetTest, RemoveIf) {
   Set.erase(&Vals[0]); // Leave a tombstone.
 
   // Remove odd elements.
-  Set.remove_if([](int *Ptr) { return *Ptr % 2 != 0; });
+  Removed = Set.remove_if([](int *Ptr) { return *Ptr % 2 != 0; });
   // We should only have elements 2 and 4 left now.
+  EXPECT_TRUE(Removed);
   EXPECT_EQ(Set.size(), 2u);
   EXPECT_TRUE(Set.contains(&Vals[2]));
   EXPECT_TRUE(Set.contains(&Vals[4]));
+
+  Removed = Set.remove_if([](int *Ptr) { return false; });
+  EXPECT_FALSE(Removed);
 }


        


More information about the llvm-commits mailing list