protected T DoFind(T item) { SetItem coreItem = GetItemCore(item); if (coreItem.Parent.Equals(item)) { return(item); } var result = DoFind(coreItem.Parent); coreItem.UpdateParent(result); return(result); }
public T Union(T x, T y) { if (HandleIsReferenceType) { Guard.IsNotNull(x, nameof(x)); Guard.IsNotNull(y, nameof(y)); } if (!Values.ContainsKey(x)) { ThrowHandleIsUnknown(nameof(x)); } if (!Values.ContainsKey(y)) { ThrowHandleIsUnknown(nameof(y)); } T xSetId = Find(x); T ySetId = Find(y); if (xSetId.Equals(ySetId)) { return(xSetId); } SetItem xSetIdCore = GetItemCore(xSetId); SetItem ySetIdCore = GetItemCore(ySetId); T result; if (xSetIdCore.Rank < ySetIdCore.Rank) { xSetIdCore.UpdateParent(ySetId); result = ySetId; } else { ySetIdCore.UpdateParent(xSetId); if (xSetIdCore.Rank == ySetIdCore.Rank) { xSetIdCore.PromoteRank(); } result = xSetId; } return(result); }