Skip to main content

DisjointSet

Struct DisjointSet 

Source
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>

Source

pub fn new() -> Self

Source

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.

Source

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.

Source

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.

Source

pub fn has(&self, item: K) -> bool

Returns true if the item is present in the set.

Corresponds to TS has(item: T): boolean.

Source

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>.

Source

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.

Source

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>>.

Source

pub fn len(&self) -> usize

Returns the number of items in the set.

Corresponds to TS get size(): number.

Source

pub fn is_empty(&self) -> bool

Auto Trait Implementations§

§

impl<K> Freeze for DisjointSet<K>

§

impl<K> RefUnwindSafe for DisjointSet<K>
where K: RefUnwindSafe,

§

impl<K> Send for DisjointSet<K>
where K: Send,

§

impl<K> Sync for DisjointSet<K>
where K: Sync,

§

impl<K> Unpin for DisjointSet<K>
where K: Unpin,

§

impl<K> UnsafeUnpin for DisjointSet<K>

§

impl<K> UnwindSafe for DisjointSet<K>
where K: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.