Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
93 lines
2.6 KiB
C++
93 lines
2.6 KiB
C++
#pragma once
|
||
|
||
#include <cstddef>
|
||
#include <numeric>
|
||
#include <stdexcept>
|
||
#include <vector>
|
||
|
||
namespace Void::DisjointSet
|
||
{
|
||
|
||
// ── DisjointSet ───────────────────────────────────────────────────────────────
|
||
// Union-Find with path compression and union by rank.
|
||
// Elements are integers in [0, n). O(α(n)) per operation (inverse Ackermann).
|
||
//
|
||
// DisjointSet ds{5};
|
||
// ds.unite(0, 1);
|
||
// ds.unite(1, 2);
|
||
// ds.connected(0, 2); // true
|
||
// ds.count(); // 3 components (0-1-2, 3, 4)
|
||
|
||
class DisjointSet
|
||
{
|
||
public:
|
||
explicit DisjointSet(std::size_t n)
|
||
: parent_(n), rank_(n, 0), component_count_{n}
|
||
{
|
||
std::iota(parent_.begin(), parent_.end(), std::size_t{0});
|
||
}
|
||
|
||
// Find root of element x (with path compression).
|
||
[[nodiscard]] std::size_t find(std::size_t x)
|
||
{
|
||
check(x);
|
||
if (parent_[x] != x)
|
||
parent_[x] = find(parent_[x]);
|
||
return parent_[x];
|
||
}
|
||
|
||
// Merge the components containing x and y.
|
||
// Returns true if they were in different components.
|
||
bool unite(std::size_t x, std::size_t y)
|
||
{
|
||
check(x); check(y);
|
||
const std::size_t rx = find(x);
|
||
const std::size_t ry = find(y);
|
||
if (rx == ry) return false;
|
||
if (rank_[rx] < rank_[ry]) {
|
||
parent_[rx] = ry;
|
||
} else if (rank_[rx] > rank_[ry]) {
|
||
parent_[ry] = rx;
|
||
} else {
|
||
parent_[ry] = rx;
|
||
++rank_[rx];
|
||
}
|
||
--component_count_;
|
||
return true;
|
||
}
|
||
|
||
[[nodiscard]] bool connected(std::size_t x, std::size_t y)
|
||
{
|
||
return find(x) == find(y);
|
||
}
|
||
|
||
// Number of disjoint components.
|
||
[[nodiscard]] std::size_t count() const noexcept { return component_count_; }
|
||
|
||
// Total number of elements.
|
||
[[nodiscard]] std::size_t size() const noexcept { return parent_.size(); }
|
||
|
||
// Component size of the component containing x.
|
||
[[nodiscard]] std::size_t component_size(std::size_t x)
|
||
{
|
||
const std::size_t root = find(x);
|
||
std::size_t n = 0;
|
||
for (std::size_t i = 0; i < parent_.size(); ++i)
|
||
if (find(i) == root) ++n;
|
||
return n;
|
||
}
|
||
|
||
private:
|
||
std::vector<std::size_t> parent_;
|
||
std::vector<std::size_t> rank_;
|
||
std::size_t component_count_;
|
||
|
||
void check(std::size_t x) const
|
||
{
|
||
if (x >= parent_.size())
|
||
throw std::out_of_range{"DisjointSet: index out of range"};
|
||
}
|
||
};
|
||
|
||
} // namespace Void::DisjointSet
|