2 #ifndef OPENGM_PARTITION_HXX
3 #define OPENGM_PARTITION_HXX
12 template<
class T =
size_t>
21 value_type
find(
const value_type&)
const;
22 value_type
find(value_type);
30 void reset(
const value_type&);
31 void merge(value_type, value_type);
32 void insert(
const value_type&);
35 std::vector<value_type> parents_;
36 std::vector<value_type> ranks_;
37 value_type numberOfElements_;
38 value_type numberOfSets_;
60 : parents_(static_cast<size_t>(size)),
61 ranks_(static_cast<size_t>(size)),
62 numberOfElements_(size),
65 for(T j=0; j<size; ++j) {
66 parents_[
static_cast<size_t>(j)] = j;
81 numberOfElements_ = size;
83 ranks_.resize(static_cast<size_t>(size));
84 parents_.resize(static_cast<size_t>(size));
85 for(T j=0; j<size; ++j) {
86 ranks_[
static_cast<size_t>(j)] = 0;
87 parents_[
static_cast<size_t>(j)] = j;
106 while(parents_[static_cast<size_t>(root)] != root) {
107 root = parents_[
static_cast<size_t>(root)];
127 while(parents_[static_cast<size_t>(root)] != root) {
128 root = parents_[
static_cast<size_t>(root)];
131 while(element != root) {
132 value_type tmp = parents_[
static_cast<size_t>(element)];
133 parents_[
static_cast<size_t>(element)] = root;
153 element1 = find(element1);
154 element2 = find(element2);
155 if(ranks_[static_cast<size_t>(element1)] < ranks_[static_cast<size_t>(element2)]) {
156 parents_[
static_cast<size_t>(element1)] = element2;
159 else if(ranks_[static_cast<size_t>(element1)] > ranks_[
static_cast<size_t>(element2)]) {
160 parents_[
static_cast<size_t>(element2)] = element1;
163 else if(element1 != element2) {
164 parents_[
static_cast<size_t>(element2)] = element1;
165 ++ranks_[
static_cast<size_t>(element1)];
181 ranks_.insert(ranks_.end(),
static_cast<size_t>(number), T(0));
182 parents_.insert(parents_.end(),
static_cast<size_t>(number), T(0));
183 for(
value_type j=numberOfElements_; j<numberOfElements_+number; ++j) {
184 parents_[
static_cast<size_t>(j)] = j;
186 numberOfElements_ += number;
187 numberOfSets_ += number;
195 template<
class Iterator>
202 for(
value_type j=0; j<numberOfElements(); ++j) {
203 if(parents_[static_cast<size_t>(j)] == j) {
218 std::map<value_type, value_type>& out
222 std::vector<value_type> r(static_cast<size_t>(numberOfSets()));
223 representatives(r.begin());
224 for(T j=0; j<numberOfSets(); ++j) {
225 out[ r[
static_cast<size_t>(j)] ] = j;
234 template<
class Iterator>
241 std::map<value_type, value_type> rl;
242 representativeLabeling(rl);
243 for(
value_type j=0; j<numberOfElements(); ++j) {
253 return numberOfElements_;
260 return numberOfSets_;
265 #endif // #ifndef OPENGM_PARTITION_HXX
void elementLabeling(Iterator) const
Output a continuous labeling of all elements.
void reset(const value_type &)
Reset a partition such that each set contains precisely one element.
void merge(value_type, value_type)
Merge two sets.
void representativeLabeling(std::map< value_type, value_type > &) const
Output a continuous labeling of the representative elements.
value_type numberOfSets() const
void insert(const value_type &)
Insert new sets.
value_type find(const value_type &) const
Find the representative element of the set that contains the given element.
void representatives(Iterator) const
Output all elements which are set representatives.
Disjoint set data structure with path compression.
value_type numberOfElements() const
Partition()
Construct a partition.