pub struct HashSet<A, S = RandomState> { /* private fields */ }
Expand description
An unordered set.
An immutable hash set using [hash array mapped tries] 1.
Most operations on this set are O(logx n) for a
suitably high x that it should be nearly O(1) for most sets.
Because of this, it’s a great choice for a generic set as long as
you don’t mind that values will need to implement
Hash
and Eq
.
Values will have a predictable order based on the hasher
being used. Unless otherwise specified, this will be the standard
RandomState
hasher.
Implementations§
source§impl<A> HashSet<A, RandomState>
impl<A> HashSet<A, RandomState>
sourcepub fn new() -> HashSet<A, RandomState>
pub fn new() -> HashSet<A, RandomState>
Construct an empty set.
source§impl<A> HashSet<A, RandomState>where
A: Hash + Eq + Clone,
impl<A> HashSet<A, RandomState>where A: Hash + Eq + Clone,
sourcepub fn unit(a: A) -> HashSet<A, RandomState>
pub fn unit(a: A) -> HashSet<A, RandomState>
Construct a set with a single value.
Examples
let set = HashSet::unit(123);
assert!(set.contains(&123));
source§impl<A, S> HashSet<A, S>
impl<A, S> HashSet<A, S>
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Test whether a set is empty.
Time: O(1)
Examples
assert!(
!hashset![1, 2, 3].is_empty()
);
assert!(
HashSet::<i32>::new().is_empty()
);
sourcepub fn ptr_eq(&self, other: &HashSet<A, S>) -> bool
pub fn ptr_eq(&self, other: &HashSet<A, S>) -> bool
Test whether two sets refer to the same content in memory.
This is true if the two sides are references to the same set, or if the two sets refer to the same root node.
This would return true if you’re comparing a set to itself, or if you’re comparing a set to a fresh clone of itself.
Time: O(1)
sourcepub fn with_hasher<RS>(hasher: RS) -> HashSet<A, S>where
Arc<S>: From<RS>,
pub fn with_hasher<RS>(hasher: RS) -> HashSet<A, S>where Arc<S>: From<RS>,
Construct an empty hash set using the provided hasher.
sourcepub fn hasher(&self) -> &Arc<S>
pub fn hasher(&self) -> &Arc<S>
Get a reference to the set’s BuildHasher
.
sourcepub fn new_from<A1>(&self) -> HashSet<A1, S>where
A1: Hash + Eq + Clone,
pub fn new_from<A1>(&self) -> HashSet<A1, S>where A1: Hash + Eq + Clone,
Construct an empty hash set using the same hasher as the current hash set.
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Discard all elements from the set.
This leaves you with an empty set, and all elements that were previously inside it are dropped.
Time: O(n)
Examples
let mut set = hashset![1, 2, 3];
set.clear();
assert!(set.is_empty());
sourcepub fn iter(&self) -> Iter<'_, A> ⓘ
pub fn iter(&self) -> Iter<'_, A> ⓘ
Get an iterator over the values in a hash set.
Please note that the order is consistent between sets using the same hasher, but no other ordering guarantee is offered. Items will not come out in insertion order or sort order. They will, however, come out in the same order every time for the same set.
source§impl<A, S> HashSet<A, S>where
A: Hash + Eq,
S: BuildHasher,
impl<A, S> HashSet<A, S>where A: Hash + Eq, S: BuildHasher,
sourcepub fn contains<BA>(&self, a: &BA) -> boolwhere
BA: Hash + Eq + ?Sized,
A: Borrow<BA>,
pub fn contains<BA>(&self, a: &BA) -> boolwhere BA: Hash + Eq + ?Sized, A: Borrow<BA>,
Test if a value is part of a set.
Time: O(log n)
sourcepub fn is_subset<RS>(&self, other: RS) -> boolwhere
RS: Borrow<HashSet<A, S>>,
pub fn is_subset<RS>(&self, other: RS) -> boolwhere RS: Borrow<HashSet<A, S>>,
Test whether a set is a subset of another set, meaning that all values in our set must also be in the other set.
Time: O(n log n)
sourcepub fn is_proper_subset<RS>(&self, other: RS) -> boolwhere
RS: Borrow<HashSet<A, S>>,
pub fn is_proper_subset<RS>(&self, other: RS) -> boolwhere RS: Borrow<HashSet<A, S>>,
Test whether a set is a proper subset of another set, meaning that all values in our set must also be in the other set. A proper subset must also be smaller than the other set.
Time: O(n log n)
source§impl<A, S> HashSet<A, S>where
A: Hash + Eq + Clone,
S: BuildHasher,
impl<A, S> HashSet<A, S>where A: Hash + Eq + Clone, S: BuildHasher,
sourcepub fn remove<BA>(&mut self, a: &BA) -> Option<A>where
BA: Hash + Eq + ?Sized,
A: Borrow<BA>,
pub fn remove<BA>(&mut self, a: &BA) -> Option<A>where BA: Hash + Eq + ?Sized, A: Borrow<BA>,
Remove a value from a set if it exists.
Time: O(log n)
sourcepub fn update(&self, a: A) -> HashSet<A, S>
pub fn update(&self, a: A) -> HashSet<A, S>
Construct a new set from the current set with the given value added.
Time: O(log n)
Examples
let set = hashset![123];
assert_eq!(
set.update(456),
hashset![123, 456]
);
sourcepub fn without<BA>(&self, a: &BA) -> HashSet<A, S>where
BA: Hash + Eq + ?Sized,
A: Borrow<BA>,
pub fn without<BA>(&self, a: &BA) -> HashSet<A, S>where BA: Hash + Eq + ?Sized, A: Borrow<BA>,
Construct a new set with the given value removed if it’s in the set.
Time: O(log n)
sourcepub fn retain<F>(&mut self, f: F)where
F: FnMut(&A) -> bool,
pub fn retain<F>(&mut self, f: F)where F: FnMut(&A) -> bool,
Filter out values from a set which don’t satisfy a predicate.
This is slightly more efficient than filtering using an iterator, in that it doesn’t need to rehash the retained values, but it still needs to reconstruct the entire tree structure of the set.
Time: O(n log n)
Examples
let mut set = hashset![1, 2, 3];
set.retain(|v| *v > 1);
let expected = hashset![2, 3];
assert_eq!(expected, set);
sourcepub fn union(self, other: HashSet<A, S>) -> HashSet<A, S>
pub fn union(self, other: HashSet<A, S>) -> HashSet<A, S>
Construct the union of two sets.
Time: O(n log n)
Examples
let set1 = hashset!{1, 2};
let set2 = hashset!{2, 3};
let expected = hashset!{1, 2, 3};
assert_eq!(expected, set1.union(set2));
sourcepub fn unions<I>(i: I) -> HashSet<A, S>where
I: IntoIterator<Item = HashSet<A, S>>,
S: Default,
pub fn unions<I>(i: I) -> HashSet<A, S>where I: IntoIterator<Item = HashSet<A, S>>, S: Default,
Construct the union of multiple sets.
Time: O(n log n)
sourcepub fn difference(self, other: HashSet<A, S>) -> HashSet<A, S>
pub fn difference(self, other: HashSet<A, S>) -> HashSet<A, S>
Construct the symmetric difference between two sets.
This is an alias for the
symmetric_difference
method.
Time: O(n log n)
Examples
let set1 = hashset!{1, 2};
let set2 = hashset!{2, 3};
let expected = hashset!{1, 3};
assert_eq!(expected, set1.difference(set2));
sourcepub fn symmetric_difference(self, other: HashSet<A, S>) -> HashSet<A, S>
pub fn symmetric_difference(self, other: HashSet<A, S>) -> HashSet<A, S>
Construct the symmetric difference between two sets.
Time: O(n log n)
Examples
let set1 = hashset!{1, 2};
let set2 = hashset!{2, 3};
let expected = hashset!{1, 3};
assert_eq!(expected, set1.symmetric_difference(set2));
sourcepub fn relative_complement(self, other: HashSet<A, S>) -> HashSet<A, S>
pub fn relative_complement(self, other: HashSet<A, S>) -> HashSet<A, S>
Construct the relative complement between two sets, that is the set
of values in self
that do not occur in other
.
Time: O(m log n) where m is the size of the other set
Examples
let set1 = ordset!{1, 2};
let set2 = ordset!{2, 3};
let expected = ordset!{1};
assert_eq!(expected, set1.relative_complement(set2));
sourcepub fn intersection(self, other: HashSet<A, S>) -> HashSet<A, S>
pub fn intersection(self, other: HashSet<A, S>) -> HashSet<A, S>
Construct the intersection of two sets.
Time: O(n log n)
Examples
let set1 = hashset!{1, 2};
let set2 = hashset!{2, 3};
let expected = hashset!{2};
assert_eq!(expected, set1.intersection(set2));
Trait Implementations§
source§impl<'a, A, S> Add<&'a HashSet<A, S>> for &'a HashSet<A, S>where
A: Hash + Eq + Clone,
S: BuildHasher,
impl<'a, A, S> Add<&'a HashSet<A, S>> for &'a HashSet<A, S>where A: Hash + Eq + Clone, S: BuildHasher,
source§impl<A, S, R> Extend<R> for HashSet<A, S>where
A: Hash + Eq + Clone + From<R>,
S: BuildHasher,
impl<A, S, R> Extend<R> for HashSet<A, S>where A: Hash + Eq + Clone + From<R>, S: BuildHasher,
source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = R>,
fn extend<I>(&mut self, iter: I)where I: IntoIterator<Item = R>,
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl<'a, A, S> From<&'a [A]> for HashSet<A, S>where
A: Hash + Eq + Clone,
S: BuildHasher + Default,
impl<'a, A, S> From<&'a [A]> for HashSet<A, S>where A: Hash + Eq + Clone, S: BuildHasher + Default,
source§impl<'a, A, S> From<&'a BTreeSet<A, Global>> for HashSet<A, S>where
A: Hash + Eq + Clone,
S: BuildHasher + Default,
impl<'a, A, S> From<&'a BTreeSet<A, Global>> for HashSet<A, S>where A: Hash + Eq + Clone, S: BuildHasher + Default,
source§impl<'a, A, S> From<&'a HashSet<A, RandomState>> for HashSet<A, S>where
A: Eq + Hash + Clone,
S: BuildHasher + Default,
impl<'a, A, S> From<&'a HashSet<A, RandomState>> for HashSet<A, S>where A: Eq + Hash + Clone, S: BuildHasher + Default,
source§impl<'a, A, S> From<&'a HashSet<A, S>> for OrdSet<A>where
A: Hash + Eq + Ord + Clone,
S: BuildHasher,
impl<'a, A, S> From<&'a HashSet<A, S>> for OrdSet<A>where A: Hash + Eq + Ord + Clone, S: BuildHasher,
source§impl<'a, A, S> From<&'a OrdSet<A>> for HashSet<A, S>where
A: Ord + Hash + Eq + Clone,
S: BuildHasher + Default,
impl<'a, A, S> From<&'a OrdSet<A>> for HashSet<A, S>where A: Ord + Hash + Eq + Clone, S: BuildHasher + Default,
source§impl<'a, A, S> From<&'a Vec<A, Global>> for HashSet<A, S>where
A: Hash + Eq + Clone,
S: BuildHasher + Default,
impl<'a, A, S> From<&'a Vec<A, Global>> for HashSet<A, S>where A: Hash + Eq + Clone, S: BuildHasher + Default,
source§impl<'a, A, S> From<&'a Vector<A>> for HashSet<A, S>where
A: Hash + Eq + Clone,
S: BuildHasher + Default,
impl<'a, A, S> From<&'a Vector<A>> for HashSet<A, S>where A: Hash + Eq + Clone, S: BuildHasher + Default,
source§impl<'s, 'a, A, OA, SA, SB> From<&'s HashSet<&'a A, SA>> for HashSet<OA, SB>where
A: ToOwned<Owned = OA> + Hash + Eq + ?Sized,
OA: Borrow<A> + Hash + Eq + Clone,
SA: BuildHasher,
SB: BuildHasher + Default,
impl<'s, 'a, A, OA, SA, SB> From<&'s HashSet<&'a A, SA>> for HashSet<OA, SB>where A: ToOwned<Owned = OA> + Hash + Eq + ?Sized, OA: Borrow<A> + Hash + Eq + Clone, SA: BuildHasher, SB: BuildHasher + Default,
source§impl<A, S> From<HashSet<A, RandomState>> for HashSet<A, S>where
A: Eq + Hash + Clone,
S: BuildHasher + Default,
impl<A, S> From<HashSet<A, RandomState>> for HashSet<A, S>where A: Eq + Hash + Clone, S: BuildHasher + Default,
source§impl<A, S> From<HashSet<A, S>> for OrdSet<A>where
A: Hash + Eq + Ord + Clone,
S: BuildHasher,
impl<A, S> From<HashSet<A, S>> for OrdSet<A>where A: Hash + Eq + Ord + Clone, S: BuildHasher,
source§impl<A, S> From<OrdSet<A>> for HashSet<A, S>where
A: Ord + Hash + Eq + Clone,
S: BuildHasher + Default,
impl<A, S> From<OrdSet<A>> for HashSet<A, S>where A: Ord + Hash + Eq + Clone, S: BuildHasher + Default,
source§impl<A, S> From<Vec<A, Global>> for HashSet<A, S>where
A: Hash + Eq + Clone,
S: BuildHasher + Default,
impl<A, S> From<Vec<A, Global>> for HashSet<A, S>where A: Hash + Eq + Clone, S: BuildHasher + Default,
source§impl<A, S> From<Vector<A>> for HashSet<A, S>where
A: Hash + Eq + Clone,
S: BuildHasher + Default,
impl<A, S> From<Vector<A>> for HashSet<A, S>where A: Hash + Eq + Clone, S: BuildHasher + Default,
source§impl<A, RA, S> FromIterator<RA> for HashSet<A, S>where
A: Hash + Eq + Clone + From<RA>,
S: BuildHasher + Default,
impl<A, RA, S> FromIterator<RA> for HashSet<A, S>where A: Hash + Eq + Clone + From<RA>, S: BuildHasher + Default,
source§impl<'a, A, S> IntoIterator for &'a HashSet<A, S>where
A: Hash + Eq,
S: BuildHasher,
impl<'a, A, S> IntoIterator for &'a HashSet<A, S>where A: Hash + Eq, S: BuildHasher,
source§impl<A, S> IntoIterator for HashSet<A, S>where
A: Hash + Eq + Clone,
S: BuildHasher,
impl<A, S> IntoIterator for HashSet<A, S>where A: Hash + Eq + Clone, S: BuildHasher,
§type IntoIter = ConsumingIter<<HashSet<A, S> as IntoIterator>::Item>
type IntoIter = ConsumingIter<<HashSet<A, S> as IntoIterator>::Item>
source§impl<'a, A, S> Mul<&'a HashSet<A, S>> for &'a HashSet<A, S>where
A: Hash + Eq + Clone,
S: BuildHasher,
impl<'a, A, S> Mul<&'a HashSet<A, S>> for &'a HashSet<A, S>where A: Hash + Eq + Clone, S: BuildHasher,
source§impl<A, S> Ord for HashSet<A, S>where
A: Hash + Eq + Clone + Ord,
S: BuildHasher + Default,
impl<A, S> Ord for HashSet<A, S>where A: Hash + Eq + Clone + Ord, S: BuildHasher + Default,
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
source§impl<A, S> PartialEq<HashSet<A, S>> for HashSet<A, S>where
A: Hash + Eq,
S: BuildHasher + Default,
impl<A, S> PartialEq<HashSet<A, S>> for HashSet<A, S>where A: Hash + Eq, S: BuildHasher + Default,
source§impl<A, S> PartialOrd<HashSet<A, S>> for HashSet<A, S>where
A: Hash + Eq + Clone + PartialOrd<A>,
S: BuildHasher + Default,
impl<A, S> PartialOrd<HashSet<A, S>> for HashSet<A, S>where A: Hash + Eq + Clone + PartialOrd<A>, S: BuildHasher + Default,
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more