pub struct PopulatedHashSet<T, S = RandomState>(/* private fields */);Expand description
A hash set that is populated i.e. guaranteed to have at least one element.
Implementations§
Source§impl<T: Eq + Hash> PopulatedHashSet<T>
impl<T: Eq + Hash> PopulatedHashSet<T>
Sourcepub fn new(value: T) -> PopulatedHashSet<T>
pub fn new(value: T) -> PopulatedHashSet<T>
Creates a singleton populated hash set i.e. a hash set with a single value.
§Example
use populated::PopulatedHashSet;
use std::num::NonZeroUsize;
let hash_set = PopulatedHashSet::new(42);
assert_eq!(hash_set.len(), NonZeroUsize::new(1).unwrap());Sourcepub fn with_capacity(capacity: NonZeroUsize, value: T) -> PopulatedHashSet<T>
pub fn with_capacity(capacity: NonZeroUsize, value: T) -> PopulatedHashSet<T>
Creates an empty PopulatedHashSet with the specified capacity
and containing the given value.
Note the capacity must be non-zero and a value must be supplied because this is a populated hash set i.e. non-empty hash set.
§Example
use populated::PopulatedHashSet;
use std::num::NonZeroUsize;
let hash_set = PopulatedHashSet::with_capacity(NonZeroUsize::new(1).unwrap(), 42);
assert_eq!(hash_set.len(), NonZeroUsize::new(1).unwrap());Source§impl<T, S> PopulatedHashSet<T, S>
impl<T, S> PopulatedHashSet<T, S>
Sourcepub fn capacity(&self) -> NonZeroUsize
pub fn capacity(&self) -> NonZeroUsize
Returns the number of elements the set can hold without reallocating.
The capacity is returned as a NonZeroUsize because this is a populated hash set.
§Example
use populated::PopulatedHashSet;
let mut hash_set = PopulatedHashSet::new(42);
hash_set.insert(43);
assert!(hash_set.capacity().get() >= 2);Sourcepub fn len(&self) -> NonZeroUsize
pub fn len(&self) -> NonZeroUsize
Returns the number of elements in the set.
The length is returned as a NonZeroUsize because this is a populated hash set.
§Example
use populated::PopulatedHashSet;
let mut hash_set = PopulatedHashSet::new(42);
hash_set.insert(43);
assert_eq!(hash_set.len().get(), 2);Sourcepub fn retain(self, predicate: impl FnMut(&T) -> bool) -> HashSet<T, S>
pub fn retain(self, predicate: impl FnMut(&T) -> bool) -> HashSet<T, S>
Retains only the elements specified by the predicate.
In other words, remove all elements e such that predicate(&e)
returns false.
§Example
use populated::PopulatedHashSet;
let mut hash_set = PopulatedHashSet::new(42);
hash_set.insert(43);
let hash_set = hash_set.retain(|&e| e == 42);
assert_eq!(hash_set.len(), 1);Sourcepub fn clear(self) -> HashSet<T, S>
pub fn clear(self) -> HashSet<T, S>
Clears the set, removing all values.
This method returns the set as a HashSet because after clearing
the set is no longer guaranteed to be populated.
§Example
use populated::PopulatedHashSet;
let mut hash_set = PopulatedHashSet::new(42);
hash_set.insert(43);
let hash_set = hash_set.clear();
assert_eq!(hash_set.len(), 0);pub fn hasher(&self) -> &S
Source§impl<T: Eq + Hash, S: BuildHasher> PopulatedHashSet<T, S>
impl<T: Eq + Hash, S: BuildHasher> PopulatedHashSet<T, S>
Sourcepub fn with_hasher(hash_builder: S, value: T) -> PopulatedHashSet<T, S>
pub fn with_hasher(hash_builder: S, value: T) -> PopulatedHashSet<T, S>
Creates a new PopulatedHashSet with the given hasher and value.
§Example
use populated::PopulatedHashSet;
use std::collections::hash_map::RandomState;
let hash_set = PopulatedHashSet::with_hasher(RandomState::new(), 42);
assert_eq!(hash_set.len().get(), 1);Sourcepub fn with_capacity_and_hasher(
capacity: NonZeroUsize,
hasher: S,
value: T,
) -> PopulatedHashSet<T, S>
pub fn with_capacity_and_hasher( capacity: NonZeroUsize, hasher: S, value: T, ) -> PopulatedHashSet<T, S>
Creates an empty PopulatedHashSet with the specified capacity
and containing the given value.
Note the capacity must be non-zero and a value must be supplied because this is a populated hash set i.e. non-empty hash set.
§Example
use populated::PopulatedHashSet;
use std::collections::hash_map::RandomState;
use std::num::NonZeroUsize;
let hash_set = PopulatedHashSet::with_capacity_and_hasher(NonZeroUsize::new(1).unwrap(), RandomState::new(), 42);
assert_eq!(hash_set.len().get(), 1);Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more elements to be inserted in the set.
Sourcepub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
Tries to reserve capacity for at least additional more elements to be inserted in the set. If the capacity is already sufficient, nothing happens. If allocation is needed and fails, an error is returned.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the set as much as possible.
Sourcepub fn shrink_to(&mut self, min_capacity: usize)
pub fn shrink_to(&mut self, min_capacity: usize)
Shrinks the capacity of the set to the minimum needed to hold the elements.
Sourcepub fn difference<'a>(
&'a self,
other: &'a HashSet<T, S>,
) -> Difference<'a, T, S>
pub fn difference<'a>( &'a self, other: &'a HashSet<T, S>, ) -> Difference<'a, T, S>
Visits the values representing the difference, i.e., the values that are in self but not in other.
Sourcepub fn symmetric_difference<'a>(
&'a self,
other: &'a HashSet<T, S>,
) -> SymmetricDifference<'a, T, S>
pub fn symmetric_difference<'a>( &'a self, other: &'a HashSet<T, S>, ) -> SymmetricDifference<'a, T, S>
Visits the values representing the symmetric difference, i.e., the values that are in self or in other but not in both.
Sourcepub fn intersection<'a>(
&'a self,
other: &'a HashSet<T, S>,
) -> Intersection<'a, T, S>
pub fn intersection<'a>( &'a self, other: &'a HashSet<T, S>, ) -> Intersection<'a, T, S>
Visits the values representing the intersection, i.e., the values
that are both in self and other.
When an equal element is present in self and other then the resulting
Intersection may yield references to one or the other. This can be
relevant if T contains fields which are not compared by its Eq
implementation, and may hold different value between the two equal
copies of T in the two sets.
Sourcepub fn contains<Q: Hash + Eq + ?Sized>(&self, value: &Q) -> boolwhere
T: Borrow<Q>,
pub fn contains<Q: Hash + Eq + ?Sized>(&self, value: &Q) -> boolwhere
T: Borrow<Q>,
Returns true if the populated set contains a value.
The value may be any borrowed form of the set’s value type, but
Hash and Eq on the borrowed form must match those for the
value type.
Sourcepub fn get<Q: Hash + Eq + ?Sized>(&self, value: &Q) -> Option<&T>where
T: Borrow<Q>,
pub fn get<Q: Hash + Eq + ?Sized>(&self, value: &Q) -> Option<&T>where
T: Borrow<Q>,
Returns a reference to the value in the populated set, if any, that is equal to the given value.
The value may be any borrowed form of the set’s value type, but
Hash and Eq on the borrowed form must match those for the
value type.
Sourcepub fn is_disjoint(&self, other: &HashSet<T, S>) -> bool
pub fn is_disjoint(&self, other: &HashSet<T, S>) -> bool
Returns true if self has no elements in common with other. This is equivalent to checking for an empty intersection.
Sourcepub fn is_subset(&self, other: &HashSet<T, S>) -> bool
pub fn is_subset(&self, other: &HashSet<T, S>) -> bool
Returns true if the set is a subset of another, i.e., other contains at least all the values in self.
Sourcepub fn is_superset(&self, other: &HashSet<T, S>) -> bool
pub fn is_superset(&self, other: &HashSet<T, S>) -> bool
Returns true if the set is a superset of another, i.e., self contains at least all the values in other.
Sourcepub fn insert(&mut self, value: T) -> bool
pub fn insert(&mut self, value: T) -> bool
Adds a value to the set.
Returns whether the value was newly inserted. That is:
- If the set did not previously contain this value, true is returned.
- If the set already contained this value, false is returned, and the set is not modified: original value is not replaced, and the value passed as argument is dropped.
Sourcepub fn inserted(
hash_set: HashSet<T, S>,
value: T,
) -> (bool, PopulatedHashSet<T, S>)
pub fn inserted( hash_set: HashSet<T, S>, value: T, ) -> (bool, PopulatedHashSet<T, S>)
Add a value to the hash set, returning the set as a PopulatedHashSet since inserting guarantees a len() > 0.
Returns whether the value was newly inserted. That is:
- If the set did not previously contain this value, true is returned.
- If the set already contained this value, false is returned, and the set is not modified: original value is not replaced, and the value passed as argument is dropped.
This method is useful when you want to ensure that the set is populated after inserting a value.
use std::collections::HashSet;
use populated::PopulatedHashSet;
use std::num::NonZeroUsize;
let mut hash_set = HashSet::from([42]);
let (inserted, populated_hash_set) = PopulatedHashSet::inserted(hash_set, 43);
assert_eq!(populated_hash_set.len(), NonZeroUsize::new(2).unwrap());
assert!(inserted);Sourcepub fn replace(&mut self, value: T) -> Option<T>
pub fn replace(&mut self, value: T) -> Option<T>
Adds a value to the set, replacing the existing value, if any, that is equal to the given one. Returns the replaced value.
Sourcepub fn remove<Q: Hash + Eq + ?Sized>(
self,
value: &Q,
) -> Result<HashSet<T, S>, PopulatedHashSet<T, S>>where
T: Borrow<Q>,
pub fn remove<Q: Hash + Eq + ?Sized>(
self,
value: &Q,
) -> Result<HashSet<T, S>, PopulatedHashSet<T, S>>where
T: Borrow<Q>,
Removes a value from the set. Returns whether the value was present in the set.
The value may be any borrowed form of the set’s value type, but Hash and Eq on the borrowed form must match those for the value
type.
Sourcepub fn take<Q: Hash + Eq + ?Sized>(
self,
value: &Q,
) -> Result<(T, HashSet<T, S>), PopulatedHashSet<T, S>>where
T: Borrow<Q>,
pub fn take<Q: Hash + Eq + ?Sized>(
self,
value: &Q,
) -> Result<(T, HashSet<T, S>), PopulatedHashSet<T, S>>where
T: Borrow<Q>,
Removes and returns the value in the set, if any, that is equal to the given one.
The value may be any borrowed form of the set’s value type, but Hash and Eq on the borrowed form must match those for the value type.
Trait Implementations§
Source§impl<T: Eq + Hash + Clone, S: BuildHasher + Default> BitOr<&HashSet<T, S>> for &PopulatedHashSet<T, S>
impl<T: Eq + Hash + Clone, S: BuildHasher + Default> BitOr<&HashSet<T, S>> for &PopulatedHashSet<T, S>
Source§impl<T: Eq + Hash + Clone, S: BuildHasher + Default> BitOr<&PopulatedHashSet<T, S>> for &HashSet<T, S>
impl<T: Eq + Hash + Clone, S: BuildHasher + Default> BitOr<&PopulatedHashSet<T, S>> for &HashSet<T, S>
Source§type Output = PopulatedHashSet<T, S>
type Output = PopulatedHashSet<T, S>
| operator.Source§impl<T: Eq + Hash + Clone, S: BuildHasher + Default> BitOr for &PopulatedHashSet<T, S>
impl<T: Eq + Hash + Clone, S: BuildHasher + Default> BitOr for &PopulatedHashSet<T, S>
Source§type Output = PopulatedHashSet<T, S>
type Output = PopulatedHashSet<T, S>
| operator.Source§impl<T: Clone, S: Clone> Clone for PopulatedHashSet<T, S>
impl<T: Clone, S: Clone> Clone for PopulatedHashSet<T, S>
Source§fn clone(&self) -> PopulatedHashSet<T, S>
fn clone(&self) -> PopulatedHashSet<T, S>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T, S> From<PopulatedHashSet<T, S>> for HashSet<T, S>
impl<T, S> From<PopulatedHashSet<T, S>> for HashSet<T, S>
Source§fn from(populated_hash_set: PopulatedHashSet<T, S>) -> HashSet<T, S>
fn from(populated_hash_set: PopulatedHashSet<T, S>) -> HashSet<T, S>
Source§impl<T: Eq + Hash> FromPopulatedIterator<T> for PopulatedHashSet<T>
impl<T: Eq + Hash> FromPopulatedIterator<T> for PopulatedHashSet<T>
Source§fn from_populated_iter(iter: impl IntoPopulatedIterator<Item = T>) -> Self
fn from_populated_iter(iter: impl IntoPopulatedIterator<Item = T>) -> Self
PopulatedIterator into Self.Source§impl<'a, T, S> IntoIterator for &'a PopulatedHashSet<T, S>
impl<'a, T, S> IntoIterator for &'a PopulatedHashSet<T, S>
Source§impl<T, S> IntoIterator for PopulatedHashSet<T, S>
impl<T, S> IntoIterator for PopulatedHashSet<T, S>
Source§impl<'a, T, S> IntoPopulatedIterator for &'a PopulatedHashSet<T, S>
impl<'a, T, S> IntoPopulatedIterator for &'a PopulatedHashSet<T, S>
type PopulatedIntoIter = PopulatedIter<'a, T>
Source§fn into_populated_iter(self) -> PopulatedIter<'a, T>
fn into_populated_iter(self) -> PopulatedIter<'a, T>
PopulatedIterator.Source§impl<T, S> IntoPopulatedIterator for PopulatedHashSet<T, S>
impl<T, S> IntoPopulatedIterator for PopulatedHashSet<T, S>
type PopulatedIntoIter = IntoPopulatedIter<T>
Source§fn into_populated_iter(self) -> IntoPopulatedIter<T>
fn into_populated_iter(self) -> IntoPopulatedIter<T>
PopulatedIterator.