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

via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 28 01:53:32 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

<details>
<summary>Changes</summary>

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.


---
Full diff: https://github.com/llvm/llvm-project/pull/155790.diff


1 Files Affected:

- (modified) llvm/include/llvm/ADT/SetVector.h (+7-25) 


``````````diff
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(); }

``````````

</details>


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


More information about the llvm-commits mailing list