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

Creates a new, empty DisjointSet.

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.

Creates an empty DisjointSet which will use the given hash builder to hash keys.

The created set has the default initial capacity.

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.

Crates a subset with the provided element.

If the given element already exists, nothing happens.

Complexity:: O(1)

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)

Check if the given element has been added to this collection.

Complexity:* O(α(n)) ≈ O(1)

Checks if the given two elements are in the same subset.

Complexity:* O(α(n)) ≈ O(1)

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Extends collection using the provided iterator.

Elements become a new subsets with just one element (equivalent to calling make_set() multiple times).

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more

Extends collection using the provided iterator.

Elements become a new subsets with just one element (equivalent to calling make_set() multiple times).

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more

Creates DisjointSet from provided iterator.

Elements become a new subsets with just one element (equivalent to calling make_set() multiple times).

Creates DisjointSet from provided iterator.

Elements become a new subsets with just one element (equivalent to calling make_set() multiple times).

The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.