Skip to content

Commit 3931b51

Browse files
kazutakahiratayuxuanchen1997
authored andcommitted
[ADT] Teach set_intersect to erase with iterators (#99569)
Summary: 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. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251415
1 parent c9edf3e commit 3931b51

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)