cranelift_isle::disjointsets

Struct DisjointSets

source
pub struct DisjointSets<T> { /* private fields */ }
Expand description

Stores disjoint sets and provides efficient operations to merge two sets, and to find a representative member of a set given any member of that set. In this implementation, sets always have at least two members, and can only be formed by the merge operation.

Implementations§

source§

impl<T: Copy + Debug + Eq + Hash> DisjointSets<T>

source

pub fn find_mut(&mut self, x: T) -> Option<T>

Find a representative member of the set containing x. If x has not been merged with any other items using merge, returns None. This method updates the data structure to make future queries faster, and takes amortized constant time.

let mut sets = cranelift_isle::disjointsets::DisjointSets::default();
sets.merge(1, 2);
sets.merge(1, 3);
sets.merge(2, 4);
assert_eq!(sets.find_mut(3).unwrap(), sets.find_mut(4).unwrap());
assert_eq!(sets.find_mut(10), None);
source

pub fn find(&self, x: T) -> Option<T>

Find a representative member of the set containing x. If x has not been merged with any other items using merge, returns None. This method does not update the data structure to make future queries faster, so find_mut should be preferred.

let mut sets = cranelift_isle::disjointsets::DisjointSets::default();
sets.merge(1, 2);
sets.merge(1, 3);
sets.merge(2, 4);
assert_eq!(sets.find(3).unwrap(), sets.find(4).unwrap());
assert_eq!(sets.find(10), None);
source

pub fn merge(&mut self, x: T, y: T)

Merge the set containing x with the set containing y. This method takes amortized constant time.

source

pub fn in_same_set(&self, x: T, y: T) -> bool

Returns whether the given items have both been merged into the same set. If either is not part of any set, returns false.

let mut sets = cranelift_isle::disjointsets::DisjointSets::default();
sets.merge(1, 2);
sets.merge(1, 3);
sets.merge(2, 4);
sets.merge(5, 6);
assert!(sets.in_same_set(2, 3));
assert!(sets.in_same_set(1, 4));
assert!(sets.in_same_set(3, 4));
assert!(!sets.in_same_set(4, 5));
source

pub fn remove_set_of(&mut self, x: T) -> Vec<T>
where T: Ord,

Remove the set containing the given item, and return all members of that set. The set is returned in sorted order. This method takes time linear in the total size of all sets.

let mut sets = cranelift_isle::disjointsets::DisjointSets::default();
sets.merge(1, 2);
sets.merge(1, 3);
sets.merge(2, 4);
assert_eq!(sets.remove_set_of(4), &[1, 2, 3, 4]);
assert_eq!(sets.remove_set_of(1), &[]);
assert!(sets.is_empty());
source

pub fn is_empty(&self) -> bool

Returns true if there are no sets. This method takes constant time.

let mut sets = cranelift_isle::disjointsets::DisjointSets::default();
assert!(sets.is_empty());
sets.merge(1, 2);
assert!(!sets.is_empty());
source

pub fn len(&self) -> usize

Returns the total number of elements in all sets. This method takes constant time.

let mut sets = cranelift_isle::disjointsets::DisjointSets::default();
sets.merge(1, 2);
assert_eq!(sets.len(), 2);
sets.merge(3, 4);
sets.merge(3, 5);
assert_eq!(sets.len(), 5);

Trait Implementations§

source§

impl<T: Clone> Clone for DisjointSets<T>

source§

fn clone(&self) -> DisjointSets<T>

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: Debug> Debug for DisjointSets<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Default> Default for DisjointSets<T>

source§

fn default() -> DisjointSets<T>

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

Auto Trait Implementations§

§

impl<T> Freeze for DisjointSets<T>

§

impl<T> RefUnwindSafe for DisjointSets<T>
where T: RefUnwindSafe,

§

impl<T> Send for DisjointSets<T>
where T: Send,

§

impl<T> Sync for DisjointSets<T>
where T: Sync,

§

impl<T> Unpin for DisjointSets<T>
where T: Unpin,

§

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

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

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