00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024
00025 #include <qvdefines.h>
00026 #include <QVDisjointSet>
00027
00028 QVDisjointSet::QVDisjointSet(uInt elements): cols(0), rows(0), elements(elements), parent(elements), rank(elements), count(elements)
00029 {
00030 Q_ASSERT_X(elements > 0, "QVDisjointSet", "Number of elements equals 0 in constructor");
00031
00032 makeSet();
00033 }
00034
00035 QVDisjointSet::QVDisjointSet(const QVDisjointSet &other):
00036 cols(other.cols),
00037 rows(other.rows),
00038 elements(other.elements),
00039 parent(other.parent),
00040 rank(other.rank),
00041 count(other.count)
00042 {
00043 Q_ASSERT_X(elements > 0, "QVDisjointSet", "Number of elements equals 0 in constructor");
00044
00045
00046 }
00047
00048 QVDisjointSet::QVDisjointSet(uInt cols, uInt rows): cols(cols), rows(rows), elements(cols*rows)
00049 {
00050 Q_ASSERT_X(cols > 0, "QVDisjointSet", "Number of columns equals 0 in constructor");
00051 Q_ASSERT_X(rows > 0, "QVDisjointSet", "Number of rows equals 0 in constructor");
00052
00053 makeSet();
00054 }
00055
00056 QVDisjointSet::QVDisjointSet(const QVGenericImage &image): cols(image.getCols()), rows(image.getRows()), elements(cols*rows),
00057 parent(elements), rank(elements), count(elements)
00058 {
00059 Q_ASSERT_X(cols > 0, "QVDisjointSet", "Number of columns equals 0 in constructor");
00060 Q_ASSERT_X(rows > 0, "QVDisjointSet", "Number of rows equals 0 in constructor");
00061
00062 makeSet();
00063 }
00064
00065
00066
00067
00068
00069
00070
00071
00072 uInt QVDisjointSet::unify(uInt index1, uInt index2)
00073 {
00074 Q_ASSERT_X(index1 < elements, "QVDisjointSet::unify", "First index exceeds number of elements");
00075 Q_ASSERT_X(index2 < elements, "QVDisjointSet::unify", "Second index exceeds number of elements");
00076
00077
00078 if ( index1 == index2 )
00079 return index1;
00080
00081 uInt root1 = find(index1), root2 = find(index2);
00082
00083
00084 if ( root1 == root2 )
00085 return root1;
00086
00087
00088 sets--;
00089
00090 Q_ASSERT_X(sets > 0 , "QVDisjointSet::unify", "Number of sets reached 0");
00091
00092 if (rank[root1] > rank[root2])
00093 {
00094 uInt temp = root1;
00095 root1 = root2;
00096 root2 = temp;
00097 }
00098
00099 Q_ASSERT_X(rank[root1] == MIN(rank[root1], rank[root2]), "QVDisjointSet::unify", "first root is minimal");
00100 Q_ASSERT_X(rank[root2] == MAX(rank[root1], rank[root2]), "QVDisjointSet::unify", "first root is maximal");
00101
00102 if (rank[root1] == rank[root2])
00103 rank[root2] = rank[root2] +1;
00104
00105 parent[root1] = root2;
00106
00107
00108 count[root2] += count[root1];
00109
00110 Q_ASSERT_X(find(index1) == find(index2) , "QVDisjointSet::unify", "Parent for indexes don't coincide, after unify");
00111 Q_ASSERT_X(root2 == find(index1) , "QVDisjointSet::unify", "Root2 is not the root for elements.");
00112
00113 return root2;
00114 }
00115
00116 void QVDisjointSet::makeSet()
00117 {
00118 sets = elements;
00119 for (uInt index = 0; index < elements; index++)
00120 {
00121 rank[index] = 0;
00122 count[index] = 1;
00123 parent[index] = index;
00124 }
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140