@@ -222,38 +222,44 @@ template <class ElemTy> class EquivalenceClasses {
222222
223223 // / erase - Erase a value from the union/find set, return if erase succeed.
224224 bool erase (const ElemTy &V) {
225- if (!TheMapping.contains (V)) return false ;
226- const ECValue *cur = TheMapping[V];
227- const ECValue *next = cur->getNext ();
228- if (cur->isLeader ()) {
229- if (next) {
230- next->Leader = cur->Leader ;
231- auto nn = next->Next ;
232- next->Next = (const ECValue*)((intptr_t )nn | (intptr_t )1 );
225+ if (!TheMapping.contains (V))
226+ return false ;
227+ const ECValue *Cur = TheMapping[V];
228+ const ECValue *Next = Cur->getNext ();
229+ if (Cur->isLeader ()) {
230+ if (Next) {
231+ Next->Leader = Cur->Leader ;
232+ Next->Next = (const ECValue *)((intptr_t )Next->Next | (intptr_t )1 );
233+ const ECValue *newLeader = Next;
234+ while ((Next = Next->getNext ())) {
235+ Next->Leader = newLeader;
236+ }
233237 }
234238 } else {
235- const ECValue *leader = findLeader (V).Node ;
236- const ECValue *pre = leader ;
237- while (pre ->getNext () != cur ) {
238- pre = pre ->getNext ();
239+ const ECValue *Leader = findLeader (V).Node ;
240+ const ECValue *Pre = Leader ;
241+ while (Pre ->getNext () != Cur ) {
242+ Pre = Pre ->getNext ();
239243 }
240- if (!next ) {
241- pre ->Next = nullptr ;
242- leader ->Leader = pre ;
244+ if (!Next ) {
245+ Pre ->Next = nullptr ;
246+ Leader ->Leader = Pre ;
243247 } else {
244- pre ->Next = (const ECValue*)((intptr_t )next | (intptr_t )pre ->isLeader ());
245- next->Leader = pre ;
248+ Pre->Next =
249+ (const ECValue *)((intptr_t )Next | (intptr_t )Pre->isLeader ());
250+ Next->Leader = Pre;
246251 }
247252 }
248253 TheMapping.erase (V);
249254 for (auto I = Members.begin (); I != Members.end (); I++) {
250- if (*I == cur ) {
255+ if (*I == Cur ) {
251256 Members.erase (I);
252257 break ;
253258 }
254259 }
255260 return true ;
256261 }
262+
257263 // / findLeader - Given a value in the set, return a member iterator for the
258264 // / equivalence class it is in. This does the path-compression part that
259265 // / makes union-find "union findy". This returns an end iterator if the value
@@ -281,18 +287,16 @@ template <class ElemTy> class EquivalenceClasses {
281287 // Otherwise, this is a real union operation. Set the end of the L1 list to
282288 // point to the L2 leader node.
283289 const ECValue &L1LV = *L1.Node , &L2LV = *L2.Node ;
284- const ECValue *L1LastV = L1LV.getEndOfList ();
285-
286- L1LastV->setNext (&L2LV);
290+ L1LV.getEndOfList ()->setNext (&L2LV);
287291
288292 // Update L1LV's end of list pointer.
289293 L1LV.Leader = L2LV.getEndOfList ();
290294
291295 // Clear L2's leader flag:
292296 L2LV.Next = L2LV.getNext ();
293297
294- // L2's leader is now last value of L1.
295- L2LV.Leader = L1LastV ;
298+ // L2's leader is now L1.
299+ L2LV.Leader = &L1LV ;
296300 return L1;
297301 }
298302
0 commit comments