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>
impl<T: Copy + Debug + Eq + Hash> DisjointSets<T>
sourcepub fn find_mut(&mut self, x: T) -> Option<T>
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);
sourcepub fn find(&self, x: T) -> Option<T>
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);
sourcepub fn merge(&mut self, x: T, y: T)
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.
sourcepub fn in_same_set(&self, x: T, y: T) -> bool
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));
sourcepub fn remove_set_of(&mut self, x: T) -> Vec<T>where
T: Ord,
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());
Trait Implementations§
source§impl<T: Clone> Clone for DisjointSets<T>
impl<T: Clone> Clone for DisjointSets<T>
source§fn clone(&self) -> DisjointSets<T>
fn clone(&self) -> DisjointSets<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<T: Debug> Debug for DisjointSets<T>
impl<T: Debug> Debug for DisjointSets<T>
source§impl<T: Default> Default for DisjointSets<T>
impl<T: Default> Default for DisjointSets<T>
source§fn default() -> DisjointSets<T>
fn default() -> DisjointSets<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)