[llvm] 02cb5bc - [ADT] Teach set_intersect to erase with iterators (#99569)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 19 02:47:50 PDT 2024


Author: Kazu Hirata
Date: 2024-07-19T02:47:47-07:00
New Revision: 02cb5bcab4f5232bbc5ecacb3d5d94fd487baa23

URL: https://github.com/llvm/llvm-project/commit/02cb5bcab4f5232bbc5ecacb3d5d94fd487baa23
DIFF: https://github.com/llvm/llvm-project/commit/02cb5bcab4f5232bbc5ecacb3d5d94fd487baa23.diff

LOG: [ADT] Teach set_intersect to erase with iterators (#99569)

Without this patch, we erase an element in S1 by value even though we
have an interator pointing to it.  This patch tries to use erase(iter)
to avoid redundant lookups.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/SetOperations.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/SetOperations.h b/llvm/include/llvm/ADT/SetOperations.h
index 40e4b05fd7425..86a27b683ebc1 100644
--- a/llvm/include/llvm/ADT/SetOperations.h
+++ b/llvm/include/llvm/ADT/SetOperations.h
@@ -60,11 +60,11 @@ template <class S1Ty, class S2Ty> void set_intersect(S1Ty &S1, const S2Ty &S2) {
   if constexpr (detail::HasMemberRemoveIf<S1Ty, decltype(Pred)>) {
     S1.remove_if(Pred);
   } else {
-    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
+    typename S1Ty::iterator Next;
+    for (typename S1Ty::iterator I = S1.begin(); I != S1.end(); I = Next) {
+      Next = std::next(I);
+      if (!S2.count(*I))
+        S1.erase(I); // Erase element if not in S2
     }
   }
 }


        


More information about the llvm-commits mailing list