Skip to content

Conversation

kazutakahirata
Copy link
Contributor

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.

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.
@llvmbot
Copy link
Member

llvmbot commented Aug 28, 2025

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

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

@kazutakahirata kazutakahirata merged commit 60bea3d into llvm:main Aug 28, 2025
11 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20250827_SetVector_remove_if branch August 28, 2025 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants