rust_algorithm_club::collections

Struct HashSet

Source
pub struct HashSet<T>
where T: Hash + Eq,
{ /* private fields */ }
Expand description

A hash set implementation based on HashMap.

References:

Implementations§

Source§

impl<T> HashSet<T>
where T: Hash + Eq,

Source

pub fn new() -> Self

Creates an empty set.

Source

pub fn len(&self) -> usize

Gets the number of non-repetitive elements, equivalently to the cardinality of a set.

§Complexity

Constant.

Source

pub fn is_empty(&self) -> bool

Returns whether there is no any element in the set.

§Complexity

Constant.

Source

pub fn insert(&mut self, value: T) -> bool

Inserts an element into the set.

Returns true if there were no such element in the set; returns false if an identical element is already in the set.

§Parameters
  • value - Element to be inserted.
§Complexity

Constant.

Source

pub fn contains<Q>(&self, value: &Q) -> bool
where T: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns whether an element is present in the set.

This is equivalent to “belongs to ∈” relation in mathematics.

§Parameters
  • value - Element to be checked whether is in the set.
§Complexity

Constant.

Source

pub fn remove<Q>(&mut self, value: &Q) -> bool
where T: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes an element from the set.

Returns true if such item was present and removed; returns false if no such item was found in the set.

§Parameters
  • value - Element to be removed.
§Complexity

Constant.

Source

pub fn iter(&self) -> impl Iterator<Item = &T>

Creates an iterator yielding immutable reference of each item in arbitrary order.

Source

pub fn union<'a>(&'a self, other: &'a HashSet<T>) -> impl Iterator<Item = &T>

Returns an iterator visiting items that exists in self, in other, or in both self and other

This is equivalent to self ∪ other in mathematics.

§Parameters
  • other - The other set.
Source

pub fn difference<'a>( &'a self, other: &'a HashSet<T>, ) -> impl Iterator<Item = &T>

Returns an iterator visiting items that exists in self but not in other.

This is equivalent to self \ other in mathematics.

§Parameters
  • other - The other set.
Source

pub fn symmetric_difference<'a>( &'a self, other: &'a HashSet<T>, ) -> impl Iterator<Item = &T>

Returns an iterator visiting items that only exists in either self or other but not in their intersection.

This is equivalent to self △ other in mathematics.

§Parameters
  • other - The other set.
Source

pub fn intersection<'a>( &'a self, other: &'a HashSet<T>, ) -> impl Iterator<Item = &T>

Returns an iterator visiting items that exists in both self and other.

This is equivalent to self ∩ other in mathematics.

§Parameters
  • other - The other set.
Source

pub fn is_disjoint(&self, other: &HashSet<T>) -> bool

Returns true if self has no elements in common with other.

This is equivalent to checking for an empty intersection, which means their intersection is the empty set ∅.

§Parameters
  • other - The other set.
§Complexity

Linear in the size of self.

Source

pub fn is_subset(&self, other: &HashSet<T>) -> bool

Returns true if other contains at least all elements in self.

This is equivalent to self ⊆ other in mathematics.

§Parameters
  • other - The other set.
§Complexity

Linear in the size of self.

Source

pub fn is_superset(&self, other: &HashSet<T>) -> bool

Returns true if self contains at least all elements in other.

This is equivalent to self ⊇ other in mathematics.

§Parameters
  • other - The other set.
§Complexity

Linear in the size of other.

Trait Implementations§

Source§

impl<'a, 'b, T> BitAnd<&'b HashSet<T>> for &'a HashSet<T>
where T: Hash + Eq + Clone,

The bit_and operator &, as an alias of intersection().

Source§

type Output = HashSet<T>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: &'b HashSet<T>) -> Self::Output

Performs the & operation. Read more
Source§

impl<'a, 'b, T> BitOr<&'b HashSet<T>> for &'a HashSet<T>
where T: Hash + Eq + Clone,

The bitor operator |, as an alias of union().

Source§

type Output = HashSet<T>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: &'b HashSet<T>) -> Self::Output

Performs the | operation. Read more
Source§

impl<'a, 'b, T> BitXor<&'b HashSet<T>> for &'a HashSet<T>
where T: Hash + Eq + Clone,

The bitxor operator ^, as an alias of symmetric_difference().

Source§

type Output = HashSet<T>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: &'b HashSet<T>) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<T> Default for HashSet<T>
where T: Hash + Eq,

Source§

fn default() -> Self

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

impl<T> FromIterator<T> for HashSet<T>
where T: Hash + Eq,

Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = T>,

Creates a value from an iterator. Read more
Source§

impl<T> PartialEq for HashSet<T>
where T: Hash + Eq,

Source§

fn eq(&self, other: &HashSet<T>) -> bool

Checks the equality of sets.

Two sets are defined to be equal if they contain the same elements and their cardinality are equal.

Set theory definition: x = y ⇒ ∀z, (z ∈ x ⇔ z ∈ y)

§Parameters
  • other - The other set.
§Complexity

Linear in the size of self.

1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> PartialOrd for HashSet<T>
where T: Hash + Eq,

Source§

fn partial_cmp(&self, other: &HashSet<T>) -> Option<Ordering>

Compares sets to determine whether one is a subset of the other or not.

§Parameters
  • other - The other set.
§Complexity

Linear in the size of max(self, other).

1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a, 'b, T> Sub<&'b HashSet<T>> for &'a HashSet<T>
where T: Hash + Eq + Clone,

The sub operator -, as an alias of difference().

Source§

type Output = HashSet<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &'b HashSet<T>) -> Self::Output

Performs the - operation. Read more
Source§

impl<T> Eq for HashSet<T>
where T: Hash + Eq,

A set is reflecxively equal to itself.

Auto Trait Implementations§

§

impl<T> Freeze for HashSet<T>

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for HashSet<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> 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, 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.