Пример #1
0
 public FastDisjointSet(ICollection <T> objectSet)
 {
     objectToElement = Generics.NewHashMap();
     foreach (T o in objectSet)
     {
         // create an element
         FastDisjointSet.Element <T> e = new FastDisjointSet.Element <T>(o);
         objectToElement[o] = e;
     }
 }
Пример #2
0
 public virtual T Find(T o)
 {
     FastDisjointSet.Element <T> e = objectToElement[o];
     if (e == null)
     {
         return(null);
     }
     FastDisjointSet.Element <T> element = FindElement(e);
     return(element.@object);
 }
Пример #3
0
 private static FastDisjointSet.Element <TTT> FindElement <Ttt>(FastDisjointSet.Element <TTT> e)
 {
     if (e.parent == e)
     {
         return(e);
     }
     FastDisjointSet.Element <TTT> rep = FindElement(e.parent);
     e.parent = rep;
     return(rep);
 }
Пример #4
0
 public virtual void Union(T a, T b)
 {
     FastDisjointSet.Element <T> e = objectToElement[a];
     FastDisjointSet.Element <T> f = objectToElement[b];
     if (e == null || f == null)
     {
         return;
     }
     if (e == f)
     {
         return;
     }
     LinkElements(FindElement(e), FindElement(f));
 }
Пример #5
0
 private static void LinkElements <Ttt>(FastDisjointSet.Element <TTT> e, FastDisjointSet.Element <TTT> f)
 {
     if (e.rank > f.rank)
     {
         f.parent = e;
     }
     else
     {
         e.parent = f;
         if (e.rank == f.rank)
         {
             f.rank++;
         }
     }
 }
Пример #6
0
 internal Element(TT o)
 {
     @object = o;
     rank    = 0;
     parent  = this;
 }