UnionFind

Struct UnionFind 

Source
pub struct UnionFind { /* private fields */ }
Expand description

Implement union-find for elements with indices [0, …, N-1].

Implementations§

Source§

impl UnionFind

Source

pub fn new(size: i32) -> UnionFind

Create new union-find data where initially each element is a separate component.

Source

pub fn representative(&mut self, element: i32) -> i32

Return the index of the current representative of the component the element belong to.

This mutates the data, as it needs to modify the representatives to short-cut further lookups.

Source

pub fn merge(&mut self, left: i32, right: i32)

Merge the two distinct components the left and right elements belong to.

Source

pub fn components(&self) -> i32

The current number of distinct components.

Source

pub fn finalize(&mut self)

Collect the final root representatives of the components.

This must be called once, after all merging were done. Once it was called, it is no longer possible to perform any more merged.

Source

pub fn component(&mut self, element: i32) -> i32

Return the index of the component the element belongs to.

Component indices are [0, …, M-1] for some small M, as opposed to representatives This may only be invoked after finalize has been called.

Auto Trait Implementations§

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.