pub struct HashSet<T: Hash + Eq, B: BuildHasher = RandomState> { /* private fields */ }
Expand description
A set of values that prevents duplicates with the help of the Hash
trait.
Relies on HashMap
internally, see documentation there for additional details.
A custom load factor is not supported at this point, with the default being 4/5.
It is a logic error for keys in a HashSet to be manipulated in a way that changes their hash. Because of this, HashSet’s API prevents mutable access to its keys.
§Time Complexity
See HashMap
with the following additions.
Variables are defined as follows:
n
: The number of items in the HashSet.m
: The number of items in the second HashSet.
Method | Complexity |
---|---|
difference *, - | O(n) |
-= | O(m) |
symmetric_difference *, ^ | O(n+m) |
^= | O(n+m) **, O(m) |
intersection *, & | O(n) |
&= | O(n) |
union *, | | O(n+m) |
|= | O(n+m) **, O(m) |
is_subset | O(m) |
is_superset | O(n) |
In the event of a has collision, all methods will take additional time. This additional time is kept at a minimum and hash collisions are unlikely especially with a large capacity.
* When exhausted.
** If the HashMap already has capacity for the additional items, these methods will take O(m)
instead.
Implementations§
Source§impl<T: Hash + Eq, B: BuildHasher + Default> HashSet<T, B>
impl<T: Hash + Eq, B: BuildHasher + Default> HashSet<T, B>
Source§impl<T: Hash + Eq, B: BuildHasher> HashSet<T, B>
impl<T: Hash + Eq, B: BuildHasher> HashSet<T, B>
Sourcepub fn with_hasher(hasher: B) -> HashSet<T, B>
pub fn with_hasher(hasher: B) -> HashSet<T, B>
Creates a new HashSet with capacity 0 and the provided hasher
.
Sourcepub fn with_cap_and_hasher(cap: usize, hasher: B) -> HashSet<T, B>
pub fn with_cap_and_hasher(cap: usize, hasher: B) -> HashSet<T, B>
Creates a new HashSet with the provided cap
acity and hasher
.
Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the length of the HashSet (the number of elements it contains).
Sourcepub fn insert(&mut self, item: T) -> bool
pub fn insert(&mut self, item: T) -> bool
Inserts the provided item into the HashSet, increasing its capacity if required. If the item was already included, no change is made and the method returns false. In other words, the method returns true if the insertion changes the HashSet.
Sourcepub unsafe fn insert_unchecked(&mut self, item: T) -> bool
pub unsafe fn insert_unchecked(&mut self, item: T) -> bool
Inserts the provided item, without checking if the HashSet has enough capacity. If the item was already included, no change is made and the method returns false.
§Safety
It is the responsibility of the caller to ensure that the HashSet has enough capacity to add
the provided item, using methods like reserve
or
with_cap
.
§Panics
Panics if the HashSet has a capacity of 0, as it isn’t possible to find a bucket associated with the item.
Sourcepub fn get<Q>(&self, item: &Q) -> Option<&T>
pub fn get<Q>(&self, item: &Q) -> Option<&T>
Returns a reference to the contained element equal to the provided item
or None if there
isn’t one.
Sourcepub fn remove<Q>(&mut self, item: &Q) -> Option<T>
pub fn remove<Q>(&mut self, item: &Q) -> Option<T>
Removes item
from the HashSet, returning it if it exists.
Trait Implementations§
Source§impl<T: Hash + Eq, B: BuildHasher> BitAndAssign for HashSet<T, B>
impl<T: Hash + Eq, B: BuildHasher> BitAndAssign for HashSet<T, B>
Source§fn bitand_assign(&mut self, rhs: Self)
fn bitand_assign(&mut self, rhs: Self)
Removes all items not in rhs
from self
to form an intersection in place.
Source§impl<T: Hash + Eq, B: BuildHasher> BitOrAssign for HashSet<T, B>
impl<T: Hash + Eq, B: BuildHasher> BitOrAssign for HashSet<T, B>
Source§fn bitor_assign(&mut self, rhs: Self)
fn bitor_assign(&mut self, rhs: Self)
Adds all items from rhs
to self
to form a union in place.
Source§impl<T: Hash + Eq, B: BuildHasher> BitXorAssign for HashSet<T, B>
impl<T: Hash + Eq, B: BuildHasher> BitXorAssign for HashSet<T, B>
Source§fn bitxor_assign(&mut self, rhs: Self)
fn bitxor_assign(&mut self, rhs: Self)
Removes all items in both rhs
and self
from self
to form the symmetric difference of
the two in place.
Source§impl<T: Hash + Eq, B: BuildHasher> Extend<T> for HashSet<T, B>
impl<T: Hash + Eq, B: BuildHasher> Extend<T> for HashSet<T, B>
Source§fn extend<A: IntoIterator<Item = T>>(&mut self, iter: A)
fn extend<A: IntoIterator<Item = T>>(&mut self, iter: A)
Source§fn extend_one(&mut self, item: T)
fn extend_one(&mut self, item: T)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<T, B, I> From<I> for HashSet<T, B>where
T: Hash + Eq,
B: BuildHasher + Default,
I: Iterator<Item = T> + ExactSizeIterator + TrustedLen,
impl<T, B, I> From<I> for HashSet<T, B>where
T: Hash + Eq,
B: BuildHasher + Default,
I: Iterator<Item = T> + ExactSizeIterator + TrustedLen,
Source§impl<T: Hash + Eq, B: BuildHasher + Default> FromIterator<T> for HashSet<T, B>
impl<T: Hash + Eq, B: BuildHasher + Default> FromIterator<T> for HashSet<T, B>
Source§fn from_iter<I: IntoIterator<Item = T>>(value: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(value: I) -> Self
Source§impl<'a, T: Hash + Eq, B: BuildHasher> IntoIterator for &'a HashSet<T, B>
impl<'a, T: Hash + Eq, B: BuildHasher> IntoIterator for &'a HashSet<T, B>
Source§impl<T: Hash + Eq, B: BuildHasher> IntoIterator for HashSet<T, B>
impl<T: Hash + Eq, B: BuildHasher> IntoIterator for HashSet<T, B>
Source§impl<T: Hash + Eq + Borrow<Q>, B: BuildHasher, Q: Hash + Eq + ?Sized> SetInterface<T, Q> for HashSet<T, B>
impl<T: Hash + Eq + Borrow<Q>, B: BuildHasher, Q: Hash + Eq + ?Sized> SetInterface<T, Q> for HashSet<T, B>
Source§impl<T: Hash + Eq, B: BuildHasher> SetIterator<T> for HashSet<T, B>
impl<T: Hash + Eq, B: BuildHasher> SetIterator<T> for HashSet<T, B>
type Iter<'a> = Iter<'a, T> where Self: 'a
Source§fn iter<'a>(&'a self) -> Self::Iter<'a>
fn iter<'a>(&'a self) -> Self::Iter<'a>
Source§fn into_difference(self, other: Self) -> IntoDifference<Self, T> ⓘ
fn into_difference(self, other: Self) -> IntoDifference<Self, T> ⓘ
self
but not other
. (self \ other
)Source§fn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, Self, T> ⓘ
fn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, Self, T> ⓘ
self
but not other
. (self \ other
)Source§fn into_symmetric_difference(
self,
other: Self,
) -> IntoSymmetricDifference<Self, T> ⓘ
fn into_symmetric_difference( self, other: Self, ) -> IntoSymmetricDifference<Self, T> ⓘ
self
or other
but not both. (self △ other
)Source§fn symmetric_difference<'a>(
&'a self,
other: &'a Self,
) -> SymmetricDifference<'a, Self, T> ⓘ
fn symmetric_difference<'a>( &'a self, other: &'a Self, ) -> SymmetricDifference<'a, Self, T> ⓘ
self
or other
but not both.
(self △ other
)Source§fn into_intersection(self, other: Self) -> IntoIntersection<Self, T> ⓘ
fn into_intersection(self, other: Self) -> IntoIntersection<Self, T> ⓘ
self
and other
. (self ∩ other
)Source§fn intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, Self, T> ⓘ
fn intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, Self, T> ⓘ
self
and other
. (self ∩ other
)Source§fn into_union(self, other: Self) -> IntoUnion<Self, T> ⓘ
fn into_union(self, other: Self) -> IntoUnion<Self, T> ⓘ
self
or other
. (self ∪ other
)Source§fn union<'a>(&'a self, other: &'a Self) -> Union<'a, Self, T> ⓘ
fn union<'a>(&'a self, other: &'a Self) -> Union<'a, Self, T> ⓘ
self
or other
. (self ∪ other
)Source§fn is_subset(&self, other: &Self) -> bool
fn is_subset(&self, other: &Self) -> bool
other
contains all elements of self
. (self ⊆ other
)Source§fn is_superset(&self, other: &Self) -> bool
fn is_superset(&self, other: &Self) -> bool
self
contains all elements of other
. (self ⊇ other
)Source§impl<T: Hash + Eq, B: BuildHasher> SubAssign for HashSet<T, B>
impl<T: Hash + Eq, B: BuildHasher> SubAssign for HashSet<T, B>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Removes all items in rhs
from self
to form the difference of the two in place.