예제 #1
0
파일: UnionFind.cs 프로젝트: sestoft/C5
    public static Eqclass <T> Make(T item)
    {
        if (!dict.Find(ref item, out var result))
        {
            dict[item] = result = new Eqclass <T>(item);
        }

        return(result);
    }
예제 #2
0
파일: UnionFind.cs 프로젝트: sestoft/C5
    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);
    }
예제 #3
0
파일: UnionFind.cs 프로젝트: sestoft/C5
    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;
            }
        }
    }