Struct UnionFind

Source
pub struct UnionFind<T: NumericId> { /* private fields */ }
Expand description

A thread-safe implementation of a union-find datastructure.

This implementation supports concurrent finds and merges, with path compression. Importantly, this implementation supports dynamically resizing the underlying array when new ids appear. This allows callers to generate Ids more flexibly, though huge amounts of resizing will cause contention as callers wait for resizes to complete.

The Clone implementation for this type is shallow: copies of the data-structure see one another’s updates.

Implementations§

Source§

impl<T: NumericId> UnionFind<T>
where T::Atomic: AtomicInt<Underlying = T::Rep>,

Source

pub fn reset(&self)

Reset the union-find datastructure, setting each element to point to itself.

This method blocks until all threads have finished their current operations.

Source

pub fn deep_copy(&self) -> Self

Create a deep copy of the union-find datastructure: subsequent unions on the returned copy will not affect the original.

Source

pub fn with_capacity(capacity: usize) -> Self

Initialize a union-find with capacity elements pointing to themselves.

Source

pub fn find(&self, elt: T) -> T

Get the canonical value associated with elt.

Note that it may be the case that find(x) != find(y) even if x and y belong to the same equivalence class, as the canonical value for the class may have changed between the find(x) and find(y) calls. To get a “ground truth” view on this, use the same_set method.

We expose find because it does work if you know that there are not any concurrent merge operations on the data-structure: this is common enough in egglog. It also makes some tests easier to write.

Source

pub fn same_set(&self, l: T, r: T) -> bool

Check if l and r belong to the same equivalence class.

Source

pub fn union(&self, l: T, r: T) -> (T, T)

Merge the equivalence classes of l and r, returning the new parent and new child classes. If l and r are already in the same class, then their canonical representative is returned twice.

Trait Implementations§

Source§

impl<T: NumericId> Clone for UnionFind<T>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: NumericId> Default for UnionFind<T>
where T::Atomic: AtomicInt,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for UnionFind<T>

§

impl<T> !RefUnwindSafe for UnionFind<T>

§

impl<T> Send for UnionFind<T>
where <T as NumericId>::Atomic: Send,

§

impl<T> Sync for UnionFind<T>
where <T as NumericId>::Atomic: Send,

§

impl<T> Unpin for UnionFind<T>

§

impl<T> !UnwindSafe for UnionFind<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.