[llvm] [SetOperations] Support set containers with remove_if (PR #96613)

Jakub Kuderski via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 25 06:55:22 PDT 2024


================
@@ -36,11 +48,16 @@ template <class S1Ty, class S2Ty> bool set_union(S1Ty &S1, const S2Ty &S2) {
 /// elements that are not contained in S2.
 ///
 template <class S1Ty, class S2Ty> void set_intersect(S1Ty &S1, const S2Ty &S2) {
-  for (typename S1Ty::iterator I = S1.begin(); I != S1.end();) {
-    const auto &E = *I;
-    ++I;
-    if (!S2.count(E))
-      S1.erase(E); // Erase element if not in S2
+  if constexpr (detail::HasMemberRemoveIf<S1Ty,
+                                          bool (*)(decltype(*S2.begin()))>) {
+    S1.remove_if([S2](const auto &E) { return !S2.count(E); });
----------------
kuhar wrote:

I'd put the lambda in a local variable and then `decltype` in the `if constexpr` condition. This way the actual `Fn` type matches what is being used for detection.

Also, shouldn't we be capturing `S2` by ref?

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


More information about the llvm-commits mailing list