[llvm] [ADT] "Inline" TestAndEraseFromSet into SetVector::remove_if (NFC) (PR #155790)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 28 01:52:55 PDT 2025


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/155790

TestAndEraseFromSet is used only from SetVector::remove_if.  This
patch "inlines" the struct into its sole user in the form of a lambda
function.

FWIW, "git blame" shows that TestAndEraseFromSet dates back to 2012.
Most likely, the lambda function wasn't an option yet back then.


>From 80a46786964ba9e7f1be8ae463a1f9de38e6a7af Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sun, 24 Aug 2025 21:46:21 -0700
Subject: [PATCH] [ADT] "Inline" TestAndEraseFromSet into SetVector::remove_if
 (NFC)

TestAndEraseFromSet is used only from SetVector::remove_if.  This
patch "inlines" the struct into its sole user in the form of a lambda
function.

FWIW, "git blame" shows that TestAndEraseFromSet dates back to 2012.
Most likely, the lambda function wasn't an option yet back then.
---
 llvm/include/llvm/ADT/SetVector.h | 32 +++++++------------------------
 1 file changed, 7 insertions(+), 25 deletions(-)

diff --git a/llvm/include/llvm/ADT/SetVector.h b/llvm/include/llvm/ADT/SetVector.h
index 85d4f2d5ee28a..273a011d1da1b 100644
--- a/llvm/include/llvm/ADT/SetVector.h
+++ b/llvm/include/llvm/ADT/SetVector.h
@@ -250,8 +250,13 @@ class SetVector {
         if (isSmall())
           return llvm::remove_if(vector_, P);
 
-      return llvm::remove_if(vector_,
-                             TestAndEraseFromSet<UnaryPredicate>(P, set_));
+      return llvm::remove_if(vector_, [&](const value_type &V) {
+        if (P(V)) {
+          set_.erase(V);
+          return true;
+        }
+        return false;
+      });
     }();
 
     if (I == vector_.end())
@@ -335,29 +340,6 @@ class SetVector {
   }
 
 private:
-  /// A wrapper predicate designed for use with std::remove_if.
-  ///
-  /// This predicate wraps a predicate suitable for use with std::remove_if to
-  /// call set_.erase(x) on each element which is slated for removal.
-  template <typename UnaryPredicate>
-  class TestAndEraseFromSet {
-    UnaryPredicate P;
-    set_type &set_;
-
-  public:
-    TestAndEraseFromSet(UnaryPredicate P, set_type &set_)
-        : P(std::move(P)), set_(set_) {}
-
-    template <typename ArgumentT>
-    bool operator()(const ArgumentT &Arg) {
-      if (P(Arg)) {
-        set_.erase(Arg);
-        return true;
-      }
-      return false;
-    }
-  };
-
   [[nodiscard]] static constexpr bool canBeSmall() { return N != 0; }
 
   [[nodiscard]] bool isSmall() const { return set_.empty(); }



More information about the llvm-commits mailing list