Struct advanced_collections::disjoint_set::DisjointSet
source · pub struct DisjointSet<T, S = RandomState>where
T: Eq + Hash,
S: BuildHasher,{ /* private fields */ }
Expand description
where α() - very slowly growing function. α(n) < 4 for any reasonable n. Therefore O(α(n)) ≈ O(1).
Example
extern crate advanced_collections;
use advanced_collections::disjoint_set::DisjointSet;
use std::iter::FromIterator;
fn main(){
let arr = [1,2,3,4,5,6,7];
//creates 7 disjoint sets
let mut ds: DisjointSet<i32> = DisjointSet::from_iter(&arr);
//you can join existing sets
ds.union(1, 2);
//or add elements to existing sets
ds.union(1,8);
//you can check if elements are in the same set
assert!(ds.in_union(&2,&8));
assert!(!ds.in_union(&3,&4));
//or if the element has been previously added to the set
assert!(ds.contains(&7));
assert!(!ds.contains(&10));
//finally, you can access sets and content of sets using iterator
for set in &mut ds {
println!("A new set:");
for elem in set.into_iter() {
print!("{}, ", elem);
}
println!("");
}
}
Implementations
sourceimpl<T, S> DisjointSet<T, S>where
T: Eq + Hash,
S: BuildHasher,
impl<T, S> DisjointSet<T, S>where
T: Eq + Hash,
S: BuildHasher,
sourcepub fn with_capacity(capacity: usize) -> Selfwhere
S: Default,
pub fn with_capacity(capacity: usize) -> Selfwhere
S: Default,
Creates an empty DisjointSet with the specified capacity.
The DisjointSet will be able to hold at least capacity elements without reallocating. If capacity is 0, the DisjointSet will not allocate.
sourcepub fn with_hasher(hash_builder: S) -> Self
pub fn with_hasher(hash_builder: S) -> Self
Creates an empty DisjointSet which will use the given hash builder to hash keys.
The created set has the default initial capacity.
sourcepub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self
Creates an empty Counter with the specified capacity, using hash_builder to hash the keys.
The Counter will be able to hold at least capacity elements without reallocating. If capacity is 0, the Counter will not allocate.
sourcepub fn make_set(&mut self, val: T)
pub fn make_set(&mut self, val: T)
Crates a subset with the provided element.
If the given element already exists, nothing happens.
Complexity:: O(1)
sourcepub fn union(&mut self, a: T, b: T)
pub fn union(&mut self, a: T, b: T)
Joins two subsets using one element from both subsets.
If the provided elements do not exist in the collection when this function is called, a new subset with one element gets created prior to joining.
Complexity: O(α(n)) ≈ O(1)
sourcepub fn contains(&self, val: &T) -> bool
pub fn contains(&self, val: &T) -> bool
Check if the given element has been added to this collection.
Complexity:* O(α(n)) ≈ O(1)
sourcepub fn in_union(&mut self, a: &T, b: &T) -> bool
pub fn in_union(&mut self, a: &T, b: &T) -> bool
Checks if the given two elements are in the same subset.
Complexity:* O(α(n)) ≈ O(1)
pub fn is_empty(&self) -> bool
pub fn len(&self) -> usize
pub fn clear(&mut self)
pub fn reserve(&mut self, additional: usize)
Trait Implementations
sourceimpl<T: Clone, S: Clone> Clone for DisjointSet<T, S>where
T: Eq + Hash,
S: BuildHasher,
impl<T: Clone, S: Clone> Clone for DisjointSet<T, S>where
T: Eq + Hash,
S: BuildHasher,
sourcefn clone(&self) -> DisjointSet<T, S>
fn clone(&self) -> DisjointSet<T, S>
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresourceimpl<T: Debug, S: Debug> Debug for DisjointSet<T, S>where
T: Eq + Hash,
S: BuildHasher,
impl<T: Debug, S: Debug> Debug for DisjointSet<T, S>where
T: Eq + Hash,
S: BuildHasher,
sourceimpl<T, S> Default for DisjointSet<T, S>where
T: Eq + Hash,
S: BuildHasher + Default,
impl<T, S> Default for DisjointSet<T, S>where
T: Eq + Hash,
S: BuildHasher + Default,
sourceimpl<'a, T, S> Extend<&'a T> for DisjointSet<T, S>where
T: Hash + Eq + Copy,
S: BuildHasher,
impl<'a, T, S> Extend<&'a T> for DisjointSet<T, S>where
T: Hash + Eq + Copy,
S: BuildHasher,
sourcefn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
Extends collection using the provided iterator.
Elements become a new subsets with just one element (equivalent to calling make_set() multiple times).
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)sourceimpl<T, S> Extend<T> for DisjointSet<T, S>where
T: Hash + Eq,
S: BuildHasher,
impl<T, S> Extend<T> for DisjointSet<T, S>where
T: Hash + Eq,
S: BuildHasher,
sourcefn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Extends collection using the provided iterator.
Elements become a new subsets with just one element (equivalent to calling make_set() multiple times).
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)sourceimpl<'a, T, S> FromIterator<&'a T> for DisjointSet<T, S>where
T: Hash + Eq + Clone,
S: BuildHasher + Default,
impl<'a, T, S> FromIterator<&'a T> for DisjointSet<T, S>where
T: Hash + Eq + Clone,
S: BuildHasher + Default,
sourcefn from_iter<I: IntoIterator<Item = &'a T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = &'a T>>(iter: I) -> Self
Creates DisjointSet from provided iterator.
Elements become a new subsets with just one element (equivalent to calling make_set() multiple times).
sourceimpl<T, S> FromIterator<T> for DisjointSet<T, S>where
T: Hash + Eq,
S: BuildHasher + Default,
impl<T, S> FromIterator<T> for DisjointSet<T, S>where
T: Hash + Eq,
S: BuildHasher + Default,
sourcefn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Creates DisjointSet from provided iterator.
Elements become a new subsets with just one element (equivalent to calling make_set() multiple times).