2023-08-15 16:20:26 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2020-07-08 14:57:31 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
* \ingroup bli
|
|
|
|
*
|
|
|
|
* This implements the disjoint set data structure with path compression and union by rank.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "BLI_array.hh"
|
2022-11-12 14:26:47 +01:00
|
|
|
#include "BLI_index_range.hh"
|
2020-07-08 14:57:31 +02:00
|
|
|
|
|
|
|
namespace blender {
|
|
|
|
|
2022-11-12 14:26:47 +01:00
|
|
|
template<typename T = int64_t> class DisjointSet {
|
2020-07-08 14:57:31 +02:00
|
|
|
private:
|
2022-11-12 14:26:47 +01:00
|
|
|
Array<T> parents_;
|
|
|
|
Array<T> ranks_;
|
2020-07-08 14:57:31 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Create a new disjoint set with the given size. Initially, every element is in a separate set.
|
|
|
|
*/
|
2022-11-12 14:26:47 +01:00
|
|
|
DisjointSet(const int64_t size) : parents_(size), ranks_(size, 0)
|
2020-07-08 14:57:31 +02:00
|
|
|
{
|
2020-07-20 12:16:20 +02:00
|
|
|
BLI_assert(size >= 0);
|
2022-11-12 14:26:47 +01:00
|
|
|
for (const int64_t i : IndexRange(size)) {
|
|
|
|
parents_[i] = T(i);
|
2020-07-08 14:57:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Join the sets containing elements x and y. Nothing happens when they have been in the same set
|
|
|
|
* before.
|
|
|
|
*/
|
2022-11-12 14:26:47 +01:00
|
|
|
void join(const T x, const T y)
|
2020-07-08 14:57:31 +02:00
|
|
|
{
|
2022-11-12 14:26:47 +01:00
|
|
|
T root1 = this->find_root(x);
|
|
|
|
T root2 = this->find_root(y);
|
2020-07-08 14:57:31 +02:00
|
|
|
|
|
|
|
/* x and y are in the same set already. */
|
|
|
|
if (root1 == root2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Implement union by rank heuristic. */
|
|
|
|
if (ranks_[root1] < ranks_[root2]) {
|
|
|
|
std::swap(root1, root2);
|
|
|
|
}
|
|
|
|
parents_[root2] = root1;
|
|
|
|
|
|
|
|
if (ranks_[root1] == ranks_[root2]) {
|
|
|
|
ranks_[root1]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true when x and y are in the same set.
|
|
|
|
*/
|
2022-11-12 14:26:47 +01:00
|
|
|
bool in_same_set(const T x, const T y)
|
2020-07-08 14:57:31 +02:00
|
|
|
{
|
2022-11-12 14:26:47 +01:00
|
|
|
T root1 = this->find_root(x);
|
|
|
|
T root2 = this->find_root(y);
|
2020-07-08 14:57:31 +02:00
|
|
|
return root1 == root2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the element that represents the set containing x currently.
|
|
|
|
*/
|
2022-11-12 14:26:47 +01:00
|
|
|
T find_root(const T x)
|
2020-07-08 14:57:31 +02:00
|
|
|
{
|
|
|
|
/* Find root by following parents. */
|
2022-11-12 14:26:47 +01:00
|
|
|
T root = x;
|
2020-07-08 14:57:31 +02:00
|
|
|
while (parents_[root] != root) {
|
|
|
|
root = parents_[root];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compress path. */
|
2022-11-12 14:26:47 +01:00
|
|
|
T to_root = x;
|
|
|
|
while (parents_[to_root] != root) {
|
|
|
|
const T parent = parents_[to_root];
|
|
|
|
parents_[to_root] = root;
|
|
|
|
to_root = parent;
|
2020-07-08 14:57:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace blender
|