Listing the "Blender Foundation" as copyright holder implied the Blender Foundation holds copyright to files which may include work from many developers. While keeping copyright on headers makes sense for isolated libraries, Blender's own code may be refactored or moved between files in a way that makes the per file copyright holders less meaningful. Copyright references to the "Blender Foundation" have been replaced with "Blender Authors", with the exception of `./extern/` since these this contains libraries which are more isolated, any changed to license headers there can be handled on a case-by-case basis. Some directories in `./intern/` have also been excluded: - `./intern/cycles/` it's own `AUTHORS` file is planned. - `./intern/opensubdiv/`. An "AUTHORS" file has been added, using the chromium projects authors file as a template. Design task: #110784 Ref !110783.
94 lines
1.9 KiB
C++
94 lines
1.9 KiB
C++
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
#pragma once
|
|
|
|
/** \file
|
|
* \ingroup bli
|
|
*
|
|
* This implements the disjoint set data structure with path compression and union by rank.
|
|
*/
|
|
|
|
#include "BLI_array.hh"
|
|
#include "BLI_index_range.hh"
|
|
|
|
namespace blender {
|
|
|
|
template<typename T = int64_t> class DisjointSet {
|
|
private:
|
|
Array<T> parents_;
|
|
Array<T> ranks_;
|
|
|
|
public:
|
|
/**
|
|
* Create a new disjoint set with the given size. Initially, every element is in a separate set.
|
|
*/
|
|
DisjointSet(const int64_t size) : parents_(size), ranks_(size, 0)
|
|
{
|
|
BLI_assert(size >= 0);
|
|
for (const int64_t i : IndexRange(size)) {
|
|
parents_[i] = T(i);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Join the sets containing elements x and y. Nothing happens when they have been in the same set
|
|
* before.
|
|
*/
|
|
void join(const T x, const T y)
|
|
{
|
|
T root1 = this->find_root(x);
|
|
T root2 = this->find_root(y);
|
|
|
|
/* 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.
|
|
*/
|
|
bool in_same_set(const T x, const T y)
|
|
{
|
|
T root1 = this->find_root(x);
|
|
T root2 = this->find_root(y);
|
|
return root1 == root2;
|
|
}
|
|
|
|
/**
|
|
* Find the element that represents the set containing x currently.
|
|
*/
|
|
T find_root(const T x)
|
|
{
|
|
/* Find root by following parents. */
|
|
T root = x;
|
|
while (parents_[root] != root) {
|
|
root = parents_[root];
|
|
}
|
|
|
|
/* Compress path. */
|
|
T to_root = x;
|
|
while (parents_[to_root] != root) {
|
|
const T parent = parents_[to_root];
|
|
parents_[to_root] = root;
|
|
to_root = parent;
|
|
}
|
|
|
|
return root;
|
|
}
|
|
};
|
|
|
|
} // namespace blender
|