public static Eqclass <T> Make(T item) { if (!dict.Find(ref item, out var result)) { dict[item] = result = new Eqclass <T>(item); } return(result); }
public static void Main() { var x = Eqclass <int> .Make(3); var y = Eqclass <int> .Make(4); var z = Eqclass <int> .Make(5); x.Union(y); y.Union(z); Console.WriteLine(x.Find().Item); Console.WriteLine(y.Find().Item); Console.WriteLine(z.Find().Item); }
public void Union(Eqclass <T> that) { var thatRep = that.Find(); var thisRep = Find(); if (thatRep != thisRep) { if (thatRep._rank == thisRep._rank) { thisRep._link = thatRep; thatRep._rank++; } else if (thatRep._rank > thisRep._rank) { thisRep._link = thatRep; } else { thatRep._link = thisRep; } } }