Skip to main content

WeakHashSet

Struct WeakHashSet 

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

Source

pub fn new() -> Self

Creates an empty WeakHashSet.

O(1) time

Source

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>

Source

pub fn with_hasher(hash_builder: S) -> Self

Creates an empty WeakHashSet with the given hasher.

O(n) time

Source

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>

Source

pub fn hasher(&self) -> &S

Returns a reference to the set’s BuildHasher.

O(1) time

Source

pub fn capacity(&self) -> usize

Returns the number of elements the set can hold without reallocating.

O(1) time

Source

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

Source

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

Source

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

pub fn clear(&mut self)

Remove all elements from the set.

O(n) time

Source§

impl<T: WeakKey, S: BuildHasher> WeakHashSet<T, S>

Source

pub fn remove_expired(&mut self)

Removes all elements whose keys have expired.

O(n) time

Source

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

Source

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

Source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity to the minimum to hold the current number of elements.

O(n) time

Source

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

Source

pub fn contains<Q>(&self, key: &Q) -> bool
where Q: ?Sized + Eq + Hash, T::Key: Borrow<Q>,

Returns true if the set contains the specified key.

expected O(1) time; worst-case O(p) time

Source

pub fn get<Q>(&self, key: &Q) -> Option<T::Strong>
where Q: ?Sized + Eq + Hash, T::Key: Borrow<Q>,

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

Source

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

Source

pub fn remove<Q>(&mut self, key: &Q) -> bool
where Q: ?Sized + Eq + Hash, T::Key: Borrow<Q>,

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

Source

pub fn take<Q>(&mut self, key: &Q) -> Option<T::Strong>
where Q: ?Sized + Eq + Hash, T::Key: Borrow<Q>,

Removes the entry matching the given key, if it exists, and return the it.

expected O(1) time; worst-case O(p) time

Source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(T::Strong) -> bool,

Removes all elements not satisfying the given predicate.

Also removes any expired elements.

O(n) time

Source

pub fn is_subset<S1>(&self, other: &WeakHashSet<T, S1>) -> bool
where 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)

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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>

Source

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

Gets an iterator over the elements of this set.

O(1) time

Source

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)

Source

pub fn extract_if<'a, F>(&'a mut self, f: F) -> ExtractIf<'a, T, F>
where F: FnMut(T::Strong) -> bool + 'a,

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,

Source§

type Output = WeakHashSet<T, S>

The resulting type after applying the & operator.
Source§

fn bitand(self, other: &WeakHashSet<T, S>) -> Self::Output

Performs the & operation. Read more
Source§

impl<T, S: BuildHasher + Default> BitOr<&WeakHashSet<T, S>> for &WeakHashSet<T, S>
where T: WeakKey,

Source§

type Output = WeakHashSet<T, S>

The resulting type after applying the | operator.
Source§

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

Performs the | operation. Read more
Source§

impl<T, S: BuildHasher + Default> BitXor<&WeakHashSet<T, S>> for &WeakHashSet<T, S>
where T: WeakKey,

Source§

type Output = WeakHashSet<T, S>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, other: &WeakHashSet<T, S>) -> Self::Output

Performs the ^ operation. Read more
Source§

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

Source§

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

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: WeakElement, S> Debug for WeakHashSet<T, S>
where T::Strong: Debug,

Source§

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

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

impl<T, S: BuildHasher + Default> Default for WeakHashSet<T, S>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: WeakKey, S: BuildHasher> Eq for WeakHashSet<T, S>
where T::Key: Eq,

Source§

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)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T: WeakKey, const N: usize> From<[<T as WeakElement>::Strong; N]> for WeakHashSet<T, RandomState>

Source§

fn from(value: [T::Strong; N]) -> Self

Converts an array of elements into a set.

If any entries in the array are equal, all but one of the corresponding values will be dropped.

Source§

impl<T, S> FromIterator<<T as WeakElement>::Strong> for WeakHashSet<T, S>
where T: WeakKey, S: BuildHasher + Default,

Source§

fn from_iter<I: IntoIterator<Item = T::Strong>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T: WeakElement, S> IntoIterator for WeakHashSet<T, S>

Source§

fn into_iter(self) -> Self::IntoIter

Creates an owning iterator from self.

O(1) time (and O(n) time to dispose of the result)

Source§

type Item = <T as WeakElement>::Strong

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

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

impl<'a, T: WeakElement, S> IntoIterator for &'a WeakHashSet<T, S>

Source§

fn into_iter(self) -> Self::IntoIter

Creates a borrowing iterator from self.

O(1) time

Source§

type Item = <T as WeakElement>::Strong

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

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

impl<T, S, S1> PartialEq<WeakHashSet<T, S1>> for WeakHashSet<T, S>
where T: WeakKey, S: BuildHasher, S1: BuildHasher,

Source§

fn eq(&self, other: &WeakHashSet<T, S1>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T, S: BuildHasher + Default> Sub<&WeakHashSet<T, S>> for &WeakHashSet<T, S>
where T: WeakKey,

Source§

type Output = WeakHashSet<T, S>

The resulting type after applying the - operator.
Source§

fn sub(self, other: &WeakHashSet<T, S>) -> Self::Output

Performs the - operation. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T, S> UnsafeUnpin for WeakHashSet<T, S>
where S: UnsafeUnpin,

§

impl<T, S> UnwindSafe for WeakHashSet<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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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.