PopulatedHashSet

Struct PopulatedHashSet 

Source
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>

Source

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());
Source

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>

Source

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);
Source

pub fn iter(&self) -> Iter<'_, T>

Returns an iterator over the set.

Source

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);
Source

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);
Source

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);
Source

pub fn hasher(&self) -> &S

Source§

impl<T: Eq + Hash, S: BuildHasher> PopulatedHashSet<T, S>

Source

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);
Source

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);
Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more elements to be inserted in the set.

Source

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.

Source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the set as much as possible.

Source

pub fn shrink_to(&mut self, min_capacity: usize)

Shrinks the capacity of the set to the minimum needed to hold the elements.

Source

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.

Source

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.

Source

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.

Source

pub fn contains<Q: Hash + Eq + ?Sized>(&self, value: &Q) -> bool
where 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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.
Source

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);
Source

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.

Source

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.

Source

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>

Source§

type Output = PopulatedHashSet<T, S>

The resulting type after applying the | operator.
Source§

fn bitor(self, other: &HashSet<T, S>) -> Self::Output

Performs the | operation. Read more
Source§

impl<T: Eq + Hash + Clone, S: BuildHasher + Default> BitOr<&PopulatedHashSet<T, S>> for &HashSet<T, S>

Source§

type Output = PopulatedHashSet<T, S>

The resulting type after applying the | operator.
Source§

fn bitor(self, other: &PopulatedHashSet<T, S>) -> Self::Output

Performs the | operation. Read more
Source§

impl<T: Eq + Hash + Clone, S: BuildHasher + Default> BitOr for &PopulatedHashSet<T, S>

Source§

type Output = PopulatedHashSet<T, S>

The resulting type after applying the | operator.
Source§

fn bitor(self, other: &PopulatedHashSet<T, S>) -> Self::Output

Performs the | operation. Read more
Source§

impl<T: Clone, S: Clone> Clone for PopulatedHashSet<T, S>

Source§

fn clone(&self) -> PopulatedHashSet<T, S>

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<T: Debug, S: Debug> Debug for PopulatedHashSet<T, S>

Source§

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

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

impl<T, S> From<PopulatedHashSet<T, S>> for HashSet<T, S>

Source§

fn from(populated_hash_set: PopulatedHashSet<T, S>) -> HashSet<T, S>

Converts to this type from the input type.
Source§

impl<T: Eq + Hash> FromPopulatedIterator<T> for PopulatedHashSet<T>

Source§

fn from_populated_iter(iter: impl IntoPopulatedIterator<Item = T>) -> Self

Converts a PopulatedIterator into Self.
Source§

impl<'a, T, S> IntoIterator for &'a PopulatedHashSet<T, S>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T, S> IntoIterator for PopulatedHashSet<T, S>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T, S> IntoPopulatedIterator for &'a PopulatedHashSet<T, S>

Source§

type PopulatedIntoIter = PopulatedIter<'a, T>

Source§

fn into_populated_iter(self) -> PopulatedIter<'a, T>

Converts the type into a PopulatedIterator.
Source§

impl<T, S> IntoPopulatedIterator for PopulatedHashSet<T, S>

Source§

type PopulatedIntoIter = IntoPopulatedIter<T>

Source§

fn into_populated_iter(self) -> IntoPopulatedIter<T>

Converts the type into a PopulatedIterator.
Source§

impl<T: Eq + Hash, S: BuildHasher> PartialEq<HashSet<T, S>> for PopulatedHashSet<T, S>

Source§

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

Tests for self and other values to be equal, and is used by ==.
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: Eq + Hash, S: BuildHasher> PartialEq<PopulatedHashSet<T, S>> for HashSet<T, S>

Source§

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

Tests for self and other values to be equal, and is used by ==.
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: Eq + Hash, S: BuildHasher> PartialEq for PopulatedHashSet<T, S>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
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, S> TryFrom<HashSet<T, S>> for PopulatedHashSet<T, S>

Source§

type Error = HashSet<T, S>

The type returned in the event of a conversion error.
Source§

fn try_from( hash_set: HashSet<T, S>, ) -> Result<PopulatedHashSet<T, S>, Self::Error>

Performs the conversion.
Source§

impl<T: Eq + Hash, S: BuildHasher> Eq for PopulatedHashSet<T, S>

Auto Trait Implementations§

§

impl<T, S> Freeze for PopulatedHashSet<T, S>
where S: Freeze,

§

impl<T, S> RefUnwindSafe for PopulatedHashSet<T, S>

§

impl<T, S> Send for PopulatedHashSet<T, S>
where S: Send, T: Send,

§

impl<T, S> Sync for PopulatedHashSet<T, S>
where S: Sync, T: Sync,

§

impl<T, S> Unpin for PopulatedHashSet<T, S>
where S: Unpin, T: Unpin,

§

impl<T, S> UnwindSafe for PopulatedHashSet<T, S>
where S: UnwindSafe, 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> 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.