Skip to content

Commit 02cb5bc

Browse files
[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.
1 parent 5a45fed commit 02cb5bc

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

llvm/include/llvm/ADT/SetOperations.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ template <class S1Ty, class S2Ty> void set_intersect(S1Ty &S1, const S2Ty &S2) {
6060
if constexpr (detail::HasMemberRemoveIf<S1Ty, decltype(Pred)>) {
6161
S1.remove_if(Pred);
6262
} else {
63-
for (typename S1Ty::iterator I = S1.begin(); I != S1.end();) {
64-
const auto &E = *I;
65-
++I;
66-
if (!S2.count(E))
67-
S1.erase(E); // Erase element if not in S2
63+
typename S1Ty::iterator Next;
64+
for (typename S1Ty::iterator I = S1.begin(); I != S1.end(); I = Next) {
65+
Next = std::next(I);
66+
if (!S2.count(*I))
67+
S1.erase(I); // Erase element if not in S2
6868
}
6969
}
7070
}

0 commit comments

Comments
 (0)