pub struct DisjointSet<K: Copy + Eq + Hash> { /* private fields */ }Expand description
A Union-Find data structure for grouping items into disjoint sets.
Corresponds to TS DisjointSet<T> in src/Utils/DisjointSet.ts.
Uses IndexMap to preserve insertion order (matching TS Map behavior).
Implementations§
Source§impl<K: Copy + Eq + Hash> DisjointSet<K>
impl<K: Copy + Eq + Hash> DisjointSet<K>
pub fn new() -> Self
Sourcepub fn union(&mut self, items: &[K])
pub fn union(&mut self, items: &[K])
Updates the graph to reflect that the given items form a set, linking any previous sets that the items were part of into a single set.
Corresponds to TS union(items: Array<T>): void.
Sourcepub fn find(&mut self, item: K) -> K
pub fn find(&mut self, item: K) -> K
Find the root of the set containing item, with path compression.
If item is not in the set, it is inserted as its own root.
Note: callers that need null/None semantics for missing items should
use find_opt() instead.
Sourcepub fn find_opt(&mut self, item: K) -> Option<K>
pub fn find_opt(&mut self, item: K) -> Option<K>
Find the root of the set containing item, returning None if the item
was never added to the set.
Corresponds to TS find(item: T): T | null.
Sourcepub fn has(&self, item: K) -> bool
pub fn has(&self, item: K) -> bool
Returns true if the item is present in the set.
Corresponds to TS has(item: T): boolean.
Sourcepub fn canonicalize(&mut self) -> IndexMap<K, K>
pub fn canonicalize(&mut self) -> IndexMap<K, K>
Forces the set into canonical form (all items pointing directly to their root) and returns a map of items to their roots.
Corresponds to TS canonicalize(): Map<T, T>.
Sourcepub fn for_each<F>(&mut self, f: F)where
F: FnMut(K, K),
pub fn for_each<F>(&mut self, f: F)where
F: FnMut(K, K),
Calls the provided callback once for each item in the disjoint set, passing the item and the group root to which it belongs.
Corresponds to TS forEach(fn: (item: T, group: T) => void): void.
Sourcepub fn build_sets(&mut self) -> Vec<HashSet<K>>
pub fn build_sets(&mut self) -> Vec<HashSet<K>>
Groups all items by their root and returns the groups as a list of sets.
Corresponds to TS buildSets(): Array<Set<T>>.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of items in the set.
Corresponds to TS get size(): number.