pub struct WeakHashSet<T, S = RandomState>(/* private fields */);Expand description
A hash set with weak elements, hashed on element value.
When a weak pointer expires, its mapping is lazily removed.
Implementations§
Source§impl<T> WeakHashSet<T, RandomState>
impl<T> WeakHashSet<T, RandomState>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty WeakHashSet with the given capacity.
O(n) time
Source§impl<T, S: BuildHasher> WeakHashSet<T, S>
impl<T, S: BuildHasher> WeakHashSet<T, S>
Sourcepub fn with_hasher(hash_builder: S) -> Self
pub fn with_hasher(hash_builder: S) -> Self
Creates an empty WeakHashSet with the given hasher.
O(n) time
Sourcepub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self
Creates an empty WeakHashSet with the given capacity and hasher.
O(n) time
Source§impl<T, S> WeakHashSet<T, S>
impl<T, S> WeakHashSet<T, S>
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the set can hold without reallocating.
O(1) time
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns an over-approximation of the number of elements.
(This is an over-approximation because it includes expired elements.)
O(1) time
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Is the set empty?
Note that this may return false even if all elements have expired, if they haven’t been collected yet.
O(1) time
Sourcepub fn load_factor(&self) -> f32
pub fn load_factor(&self) -> f32
Returns proportion of buckets that are used.
This is an over-approximation because of expired elements.
O(1) time
Source§impl<T: WeakKey, S: BuildHasher> WeakHashSet<T, S>
impl<T: WeakKey, S: BuildHasher> WeakHashSet<T, S>
Sourcepub fn remove_expired(&mut self)
pub fn remove_expired(&mut self)
Removes all elements whose keys have expired.
O(n) time
Sourcepub fn reserve(&mut self, additional_capacity: usize)
pub fn reserve(&mut self, additional_capacity: usize)
Reserves room for additional elements.
This method ensures that at least additional_capacity insertions
may be performed without reallocating.
O(n) time
Sourcepub fn try_reserve(
&mut self,
additional_capacity: usize,
) -> Result<(), TryReserveError>
pub fn try_reserve( &mut self, additional_capacity: usize, ) -> Result<(), TryReserveError>
Tries to reserve room for additional elements.
If this method succeeds, then at least additional_capacity insertions
may be performed without reallocating further.
O(n) time
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity to the minimum to hold the current number of elements.
O(n) time
Sourcepub fn shrink_to(&mut self, min_capacity: usize)
pub fn shrink_to(&mut self, min_capacity: usize)
Shrinks the capacity to hold no fewer than min_capacity elements.
May remove expired items if necessary.
Does nothing if the current capacity is already at min_capacity or below.
O(n) time
Sourcepub fn contains<Q>(&self, key: &Q) -> bool
pub fn contains<Q>(&self, key: &Q) -> bool
Returns true if the set contains the specified key.
expected O(1) time; worst-case O(p) time
Sourcepub fn get<Q>(&self, key: &Q) -> Option<T::Strong>
pub fn get<Q>(&self, key: &Q) -> Option<T::Strong>
Gets a strong reference to the given key, if found.
§Examples
use weak_table::WeakHashSet;
use std::rc::{Rc, Weak};
use std::ops::Deref;
let mut set: WeakHashSet<Weak<String>> = WeakHashSet::new();
let a = Rc::new("a".to_owned());
set.insert(a.clone());
let also_a = set.get("a").unwrap();
assert!(Rc::ptr_eq( &a, &also_a ));expected O(1) time; worst-case O(p) time
Sourcepub fn insert(&mut self, key: T::Strong) -> bool
pub fn insert(&mut self, key: T::Strong) -> bool
Unconditionally inserts key into this set,
replacing any previous matching entry.
Returns true if the key was absent before, and false otherwise.
(Note that unlike HashSet::insert, this insert method always replaces
the key.)
expected O(1) time; worst-case O(p) time
Sourcepub fn remove<Q>(&mut self, key: &Q) -> bool
pub fn remove<Q>(&mut self, key: &Q) -> bool
Removes the entry matching the given key, if it exists.
Returns true if an entry was removed.
expected O(1) time; worst-case O(p) time
Sourcepub fn take<Q>(&mut self, key: &Q) -> Option<T::Strong>
pub fn take<Q>(&mut self, key: &Q) -> Option<T::Strong>
Removes the entry matching the given key, if it exists, and return the it.
expected O(1) time; worst-case O(p) time
Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Removes all elements not satisfying the given predicate.
Also removes any expired elements.
O(n) time
Sourcepub fn is_subset<S1>(&self, other: &WeakHashSet<T, S1>) -> boolwhere
S1: BuildHasher,
pub fn is_subset<S1>(&self, other: &WeakHashSet<T, S1>) -> boolwhere
S1: BuildHasher,
Is self a subset of other?
expected O(n) time; worst-case O(nq) time (where n is
self.capacity() and q is the length of the probe sequences
in other)
Sourcepub fn intersection<'a>(
&'a self,
other: &'a WeakHashSet<T, S>,
) -> Intersection<'a, T, S> ⓘ
pub fn intersection<'a>( &'a self, other: &'a WeakHashSet<T, S>, ) -> Intersection<'a, T, S> ⓘ
Returns an iterator that computes the intersection of this set
with other.
This iterator will return exactly the set of elements that are present in both sets.
It is not specified which set the elements will be copied from.
O(1) time to construct the iterator.
Consuming the iterator will run in O(n) expected time and O(n *p) worst-case time, where n is the size of the smaller set, and p is the average probe length in the larger set.
Sourcepub fn difference<'a>(
&'a self,
other: &'a WeakHashSet<T, S>,
) -> Difference<'a, T, S> ⓘ
pub fn difference<'a>( &'a self, other: &'a WeakHashSet<T, S>, ) -> Difference<'a, T, S> ⓘ
Returns an iterator that computes the difference of this set
and other.
This iterator will return exactly the set of elements
that are present in self, but not in other.
O(1) time to construct the iterator.
Consuming the iterator will run in O(n) expected time and O(n p)
worst-case time, where n is the size of self, and p
is the average probe length in other.
Sourcepub fn union<'a>(&'a self, other: &'a WeakHashSet<T, S>) -> Union<'a, T, S> ⓘ
pub fn union<'a>(&'a self, other: &'a WeakHashSet<T, S>) -> Union<'a, T, S> ⓘ
Returns an iterator over that computes the union of this set and
other.
This iterator will return exactly the set of elements that are
present in either of self or other. No element will be returned
more than once.
If an element is present in both sets, it is not specified which set it will be copied from.
O(1) time to construct the iterator.
Consuming the iterator will run in O(n + m) expected time and O(n + m p) worst-case time, where n is the size of the larger set, m is the size of the smaller set, and p is the average probe length.
Sourcepub fn symmetric_difference<'a>(
&'a self,
other: &'a WeakHashSet<T, S>,
) -> SymmetricDifference<'a, T, S> ⓘ
pub fn symmetric_difference<'a>( &'a self, other: &'a WeakHashSet<T, S>, ) -> SymmetricDifference<'a, T, S> ⓘ
Returns an iterator over that computes the symmetric difference of
this set and other.
This iterator will return exactly the set of elements that are
present in exactly one of self or other but not both.
O(1) time to construct the iterator.
Consuming the iterator will run in O(n + m) expected time and
O((n + m)p) worst-case time, where n is the size of self
m is the size of other, and p is the average probe length.
Sourcepub fn is_superset(&self, other: &WeakHashSet<T, S>) -> bool
pub fn is_superset(&self, other: &WeakHashSet<T, S>) -> bool
Returns true if every element of other is a also a member
of this set.
Sourcepub fn is_disjoint(&self, other: &WeakHashSet<T, S>) -> bool
pub fn is_disjoint(&self, other: &WeakHashSet<T, S>) -> bool
Returns true if this set has no members in common with other.
Source§impl<T: WeakElement, S> WeakHashSet<T, S>
impl<T: WeakElement, S> WeakHashSet<T, S>
Sourcepub fn drain(&mut self) -> Drain<'_, T> ⓘ
pub fn drain(&mut self) -> Drain<'_, T> ⓘ
Gets a draining iterator, which removes all the elements but retains the storage.
O(1) time (and O(n) time to dispose of the result)
Sourcepub fn extract_if<'a, F>(&'a mut self, f: F) -> ExtractIf<'a, T, F> ⓘ
pub fn extract_if<'a, F>(&'a mut self, f: F) -> ExtractIf<'a, T, F> ⓘ
Gets an iterator that removes and returns elements matching a given predicate.
Expired elements are also removed.
If this iterator is dropped before it is completed, then no further
elements are removed.
(This is in contrast to the behavior of drain).
O(1) time
Trait Implementations§
Source§impl<T, S: BuildHasher + Default> BitAnd<&WeakHashSet<T, S>> for &WeakHashSet<T, S>where
T: WeakKey,
impl<T, S: BuildHasher + Default> BitAnd<&WeakHashSet<T, S>> for &WeakHashSet<T, S>where
T: WeakKey,
Source§type Output = WeakHashSet<T, S>
type Output = WeakHashSet<T, S>
& operator.Source§impl<T, S: BuildHasher + Default> BitOr<&WeakHashSet<T, S>> for &WeakHashSet<T, S>where
T: WeakKey,
impl<T, S: BuildHasher + Default> BitOr<&WeakHashSet<T, S>> for &WeakHashSet<T, S>where
T: WeakKey,
Source§type Output = WeakHashSet<T, S>
type Output = WeakHashSet<T, S>
| operator.Source§impl<T, S: BuildHasher + Default> BitXor<&WeakHashSet<T, S>> for &WeakHashSet<T, S>where
T: WeakKey,
impl<T, S: BuildHasher + Default> BitXor<&WeakHashSet<T, S>> for &WeakHashSet<T, S>where
T: WeakKey,
Source§type Output = WeakHashSet<T, S>
type Output = WeakHashSet<T, S>
^ operator.Source§impl<T: Clone, S: Clone> Clone for WeakHashSet<T, S>
impl<T: Clone, S: Clone> Clone for WeakHashSet<T, S>
Source§fn clone(&self) -> WeakHashSet<T, S>
fn clone(&self) -> WeakHashSet<T, S>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: WeakElement, S> Debug for WeakHashSet<T, S>
impl<T: WeakElement, S> Debug for WeakHashSet<T, S>
Source§impl<T, S: BuildHasher + Default> Default for WeakHashSet<T, S>
impl<T, S: BuildHasher + Default> Default for WeakHashSet<T, S>
impl<T: WeakKey, S: BuildHasher> Eq for WeakHashSet<T, S>
Source§impl<T: WeakKey, S: BuildHasher> Extend<<T as WeakElement>::Strong> for WeakHashSet<T, S>
impl<T: WeakKey, S: BuildHasher> Extend<<T as WeakElement>::Strong> for WeakHashSet<T, S>
Source§fn extend<I: IntoIterator<Item = T::Strong>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T::Strong>>(&mut self, iter: I)
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<T: WeakKey, const N: usize> From<[<T as WeakElement>::Strong; N]> for WeakHashSet<T, RandomState>
impl<T: WeakKey, const N: usize> From<[<T as WeakElement>::Strong; N]> for WeakHashSet<T, RandomState>
Source§impl<T, S> FromIterator<<T as WeakElement>::Strong> for WeakHashSet<T, S>
impl<T, S> FromIterator<<T as WeakElement>::Strong> for WeakHashSet<T, S>
Source§impl<T: WeakElement, S> IntoIterator for WeakHashSet<T, S>
impl<T: WeakElement, S> IntoIterator for WeakHashSet<T, S>
Source§impl<'a, T: WeakElement, S> IntoIterator for &'a WeakHashSet<T, S>
impl<'a, T: WeakElement, S> IntoIterator for &'a WeakHashSet<T, S>
Source§impl<T, S, S1> PartialEq<WeakHashSet<T, S1>> for WeakHashSet<T, S>
impl<T, S, S1> PartialEq<WeakHashSet<T, S1>> for WeakHashSet<T, S>
Source§impl<T, S: BuildHasher + Default> Sub<&WeakHashSet<T, S>> for &WeakHashSet<T, S>where
T: WeakKey,
impl<T, S: BuildHasher + Default> Sub<&WeakHashSet<T, S>> for &WeakHashSet<T, S>where
T: WeakKey,
Source§type Output = WeakHashSet<T, S>
type Output = WeakHashSet<T, S>
- operator.