src/qvdta/qvdisjointset.cpp

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00024 
00025 #include <qvdta/qvdisjointset.h>
00026 
00027 namespace qvdta
00028 {
00029 QVDisjointSet::QVDisjointSet(uInt numElements): elements(numElements), cols(0), rows(0)
00030         {
00031         Q_ASSERT_X(numElements > 0, "QVDisjointSet", "Number of elements equals 0 in constructor");
00032         allocData();
00033         makeSet();
00034         }
00035 
00036 QVDisjointSet::QVDisjointSet(uInt cols, uInt rows): elements(cols*rows), cols(cols), rows(rows)
00037         {
00038         Q_ASSERT_X(cols > 0, "QVDisjointSet", "Number of columns equals 0 in constructor");
00039         Q_ASSERT_X(rows > 0, "QVDisjointSet", "Number of rows equals 0 in constructor");
00040         allocData();
00041         makeSet();
00042         }
00043 
00044 QVDisjointSet::QVDisjointSet(QVGenericImage &image): elements(cols*rows), cols(image.getCols()), rows(image.getRows())
00045         {
00046         Q_ASSERT_X(cols > 0, "QVDisjointSet", "Number of columns equals 0 in constructor");
00047         Q_ASSERT_X(rows > 0, "QVDisjointSet", "Number of rows equals 0 in constructor");
00048         allocData();
00049         makeSet();
00050         }
00051 
00052 QVDisjointSet::~QVDisjointSet()
00053         {
00054         Q_ASSERT_X(parent != 0, "~QVDisjointSet", "Parent array not allocated");
00055         Q_ASSERT_X(rank != 0, "~QVDisjointSet", "Rank array not allocated");
00056         freeData();
00057         }
00058 
00059 uInt QVDisjointSet::unify(uInt index1, uInt index2)
00060         {
00061         Q_ASSERT_X(index1 < elements, "QVDisjointSet::unify", "First index exceeds number of elements");
00062         Q_ASSERT_X(index2 < elements, "QVDisjointSet::unify", "Second index exceeds number of elements");
00063 
00064         uInt root1 = find(index1), root2 = find(index2);
00065 
00066         // Trivial cases
00067         if ( (index1 == index2) || (root1 == root2) )
00068                 return root1;
00069 
00070         // Addon to union-find algorithm to keep count of number of sets with constant cost
00071         sets--;
00072 
00073         Q_ASSERT_X(sets > 0 , "QVDisjointSet::unify", "Number of sets reached 0");
00074         
00075         if (rank[root1] > rank[root2])
00076                 {
00077                 uInt temp = root1;
00078                 root1 = root2;
00079                 root2 = temp;
00080                 }
00081 
00082         Q_ASSERT_X(rank[root1] == MIN(rank[root1], rank[root2]) ,
00083                 "QVDisjointSet::unify", "first root is minimal");
00084         Q_ASSERT_X(rank[root2] == MAX(rank[root1], rank[root2]) ,
00085                 "QVDisjointSet::unify", "first root is maximal");
00086 
00087         if (rank[root1] == rank[root2])
00088                 rank[root2] = rank[root2] +1;
00089 
00090         parent[root1] = root2;
00091         count[root2] += count[root1];
00092 
00093         Q_ASSERT_X(find(index1) == find(index2) , "QVDisjointSet::unify",
00094                 "Parent for indexes don't coincide, after unify");
00095 
00096         return find(index1);
00097         }
00098 
00099 void QVDisjointSet::makeSet()
00100         {
00101         sets = elements;
00102         for (uInt index = 0; index < elements; index++)
00103                 { 
00104                 rank[index] = 0;
00105                 count[index] = 1;
00106                 parent[index] = index;
00107                 }
00108         }
00109 
00110 void QVDisjointSet::allocData()
00111         {
00112         parent = new uInt[elements];
00113         rank = new uInt[elements];
00114         count = new uInt[elements];
00115         }
00116 
00117 void QVDisjointSet::freeData()
00118         {
00119         delete parent;
00120         delete rank;
00121         delete count;
00122         }
00123 
00124 }

Generated on Fri Dec 7 12:20:59 2007 for QVision by  doxygen 1.5.3