[Lemon-commits] [lemon_svn] beckerjc: r638 - hugo/trunk/src/work/johanna

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:40:42 CET 2006


Author: beckerjc
Date: Thu Apr 29 18:45:40 2004
New Revision: 638

Modified:
   hugo/trunk/src/work/johanna/unionfind.h
   hugo/trunk/src/work/johanna/unionfind_test.cc

Log:
Three new methods in UnionFindEnum.
UnionFindEnum completed.


Modified: hugo/trunk/src/work/johanna/unionfind.h
==============================================================================
--- hugo/trunk/src/work/johanna/unionfind.h	(original)
+++ hugo/trunk/src/work/johanna/unionfind.h	Thu Apr 29 18:45:40 2004
@@ -264,6 +264,26 @@
     }
 
     /**
+     * \brief Insert the given element into the component of the others.
+     *
+     * This methods insert the element \e a into the component of the
+     * element \e comp. 
+     */
+
+    void insert(const T &a, const T &comp) {
+      
+      IIter clit = _find(m[comp]);
+      ItemList &c = *clit->my_class;
+      c.push_back(ItemType(a,0));
+      IIter ai = c.end();
+      --ai;
+      ai->parent = clit;
+      m.set(a, ai);
+      ++clit->size;
+    }
+
+
+    /**
      * \brief Find the leader of the component of the given element.
      *
      * The method returns the leader of the component of the given element.
@@ -385,6 +405,58 @@
       la->parent = ia;
     }
 
+    /**
+     * \brief Move the given element to an other component.
+     *
+     * This method moves the element \e a from its component
+     * to the component of \e comp.
+     * If \e a and \e comp are in the same component then
+     * it returns false otherwise it returns true.
+     */
+
+    bool move(const T &a, const T &comp) {
+
+      IIter ai = m[a];
+      IIter lai = _find(ai);
+      IIter clit = _find(m[comp]);
+
+      if (lai == clit)
+	return false;
+
+      ItemList &c = *clit->my_class;
+
+      bool is_leader = (lai == ai);
+      bool singleton = false;
+
+      if (is_leader) {
+	++lai;
+      }
+
+      c.splice(c.end(), *lai->my_class, ai);
+
+      if (is_leader) {
+	if (ai->size == 1) {
+	  classes.erase(ai->my_class);
+	  singleton = true;
+	}
+	else {
+	  lai->size = ai->size; 
+	  lai->my_class = ai->my_class;	
+	}
+      }
+      if (!singleton) {
+	for (IIter i = lai; i != lai->my_class->end(); ++i)
+	  i->parent = lai;
+	--lai->size;
+      }
+
+      ai->parent = clit;
+      ai->my_class = 0;
+      ++clit->size;
+
+      return true;
+    }
+
 
     /**
      * \brief Remove the given element from the structure.

Modified: hugo/trunk/src/work/johanna/unionfind_test.cc
==============================================================================
--- hugo/trunk/src/work/johanna/unionfind_test.cc	(original)
+++ hugo/trunk/src/work/johanna/unionfind_test.cc	Thu Apr 29 18:45:40 2004
@@ -69,15 +69,63 @@
   check(U.join(3,5));
   print(U);
 
+  cout << "Inserting 8 to the component of 5 ..." << endl;
+  U.insert(8,5);
+  print(U);
+
   cout << "size of the class of 4: " << U.size(4) << endl;
   check(U.size(4) == 3);
   cout << "size of the class of 5: " << U.size(5) << endl;
-  check(U.size(5) == 2);
+  check(U.size(5) == 3);
   cout << "size of the class of 6: " << U.size(6) << endl;
   check(U.size(6) == 1);
   cout << "size of the class of 2: " << U.size(2) << endl;
   check(U.size(2) == 3);
 
+  cout << "Inserting 9 ..." << endl;
+  U.insert(9);
+  print(U);
+  cout << "Inserting 10 to the component of 9 ..." << endl;
+  U.insert(10,9);
+  print(U);
+
+  cout << "Joining 8 and 10..." << endl;
+  check(U.join(8,10));
+  print(U);
+
+  cout << "Move 9 to the class of 4 ..." << endl;
+  check(U.move(9,4));
+  print(U);
+
+  cout << "Move 9 to the class of 2 ..." << endl;
+  check(!U.move(9,2));
+  print(U);
+
+  cout << "size of the class of 4: " << U.size(4) << endl;
+  check(U.size(4) == 4);
+  cout << "size of the class of 9: " << U.size(9) << endl;
+  check(U.size(9) == 4);
+  
+  cout << "Move 5 to the class of 6 ..." << endl;
+  check(U.move(5,6));
+  print(U);
+
+  cout << "size of the class of 5: " << U.size(5) << endl;
+  check(U.size(5) == 2);
+  cout << "size of the class of 8: " << U.size(8) << endl;
+  check(U.size(8) == 3);
+
+  cout << "Move 7 to the class of 10 ..." << endl;
+  check(U.move(7,10));
+  print(U);
+
+  cout << "size of the class of 7: " << U.size(7) << endl;
+  check(U.size(7) == 4);
+
+  cout <<"erase 9: " << endl;
+  U.erase(9);
+  print(U);
+
   cout <<"erase 1: " << endl;
   U.erase(1);
   print(U);
@@ -96,8 +144,8 @@
   U.erase(6);
   print(U);
 
-  cout << "split the class of 5: " << endl;
-  U.split(5);
+  cout << "split the class of 8: " << endl;
+  U.split(8);
   print(U);
 
 



More information about the Lemon-commits mailing list