DisjointHashSet

Struct DisjointHashSet 

Source
pub struct DisjointHashSet<K> { /* private fields */ }

Implementations§

Source§

impl<K: Eq + Hash> DisjointHashSet<K>

Source

pub fn new() -> Self

Creates an empty DisjointHashSet.

§Example
use disjoint_hash_set ::DisjointHashSet;
let mut djhs: DisjointHashSet<&str> = DisjointHashSet::new();
Source

pub fn contains<T: Borrow<K>>(&self, val: T) -> bool

Check if the value has already been inserted.

§Example
use disjoint_hash_set::DisjointHashSet;

let mut djhs = DisjointHashSet::new();
assert!(!djhs.contains(&"a"));
djhs.insert(&"a");
assert!(djhs.contains(&"a"));
Source

pub fn insert(&mut self, val: K) -> bool

Insert the value as a new disjoint set with a single member. Returns true if the value was not already present.

use disjoint_hash_set::DisjointHashSet;

let mut djhs = DisjointHashSet::new();
assert!(djhs.insert(&"a"));
assert!(!djhs.insert(&"a"));
Source

pub fn is_linked<T: Borrow<K>>(&mut self, val1: T, val2: T) -> bool

Checks if the two keys are members of the same set. This will not implicitly add values that were not already present.

use disjoint_hash_set::DisjointHashSet;

let mut djhs = DisjointHashSet::new();
djhs.link("a", "b");
djhs.link("a", "c");
assert!(djhs.is_linked("b", "c"));
assert!(!djhs.is_linked("a", "d"));

Link the respective sets of the two provided values. This will insert non-existent values in the process.

use disjoint_hash_set::DisjointHashSet;

let mut djhs = DisjointHashSet::new();

djhs.link("a", "b");
assert!(djhs.contains("a"));
assert!(djhs.contains("b"));
assert!(djhs.is_linked("a", "b"));
Source

pub fn sets(self) -> impl Iterator<Item = HashSet<K>>

Consumes the DisjointHashSet and returns an iterator of HashSets for each disjoint set.

use disjoint_hash_set::DisjointHashSet;
use std::{collections::HashSet, iter::FromIterator};

let edges = vec![("a", "a"), ("b", "c"), ("d", "e"), ("e", "f")];
let mut sets = DisjointHashSet::from_iter(edges).sets().collect::<Vec<_>>();
sets.sort_by(|set_a, set_b| set_a.len().cmp(&set_b.len()));

let expected_sets: Vec<HashSet<&str>> = vec![
    HashSet::from_iter(vec!["a"]),
    HashSet::from_iter(vec!["b", "c"]),
    HashSet::from_iter(vec!["d", "e", "f"]),
];

assert_eq!(sets, expected_sets);

Trait Implementations§

Source§

impl<K: Clone> Clone for DisjointHashSet<K>

Source§

fn clone(&self) -> DisjointHashSet<K>

Returns a duplicate 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<K: Debug> Debug for DisjointHashSet<K>

Source§

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

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

impl<V: Eq + Hash> FromIterator<(V, V)> for DisjointHashSet<V>

Source§

fn from_iter<I: IntoIterator<Item = (V, V)>>(links: I) -> Self

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<K> Freeze for DisjointHashSet<K>

§

impl<K> RefUnwindSafe for DisjointHashSet<K>
where K: RefUnwindSafe,

§

impl<K> Send for DisjointHashSet<K>
where K: Send,

§

impl<K> Sync for DisjointHashSet<K>
where K: Sync,

§

impl<K> Unpin for DisjointHashSet<K>
where K: Unpin,

§

impl<K> UnwindSafe for DisjointHashSet<K>
where K: 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, 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> 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.