pub struct WeakKeyHashMap<K, V, S = RandomState>(/* private fields */);Expand description
A hash map with weak keys, hashed on key value.
When a weak pointer expires, its mapping is lazily removed.
Implementations§
Source§impl<K, V> WeakKeyHashMap<K, V, RandomState>
impl<K, V> WeakKeyHashMap<K, V, RandomState>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty WeakKeyHashMap with the given capacity.
O(n) time
Source§impl<K, V, S: BuildHasher> WeakKeyHashMap<K, V, S>
impl<K, V, S: BuildHasher> WeakKeyHashMap<K, V, S>
Sourcepub fn with_hasher(hash_builder: S) -> Self
pub fn with_hasher(hash_builder: S) -> Self
Creates an empty WeakKeyHashMap 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 WeakKeyHashMap with the given capacity and hasher.
O(n) time
Source§impl<K, V, S> WeakKeyHashMap<K, V, S>
impl<K, V, S> WeakKeyHashMap<K, V, S>
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the map 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 map 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<K: WeakKey, V, S: BuildHasher> WeakKeyHashMap<K, V, S>
impl<K: WeakKey, V, S: BuildHasher> WeakKeyHashMap<K, V, S>
Sourcepub fn remove_expired(&mut self)
pub fn remove_expired(&mut self)
Removes all mappings 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 mappings.
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 mappings.
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 mappings.
May remove expired items if necessary.
Does nothing if the current capacity is already at min_capacity or below.
O(n) time
Sourcepub fn entry(&mut self, key: K::Strong) -> Entry<'_, K, V>
pub fn entry(&mut self, key: K::Strong) -> Entry<'_, K, V>
Gets the requested entry.
expected O(1) time; worst-case O(p) time
Sourcepub fn get<Q>(&self, key: &Q) -> Option<&V>
pub fn get<Q>(&self, key: &Q) -> Option<&V>
Returns a reference to the value corresponding to the key.
Returns None if no matching key is found.
expected O(1) time; worst-case O(p) time
Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
Returns true if the map contains the specified key.
expected O(1) time; worst-case O(p) time
Sourcepub fn get_key<Q>(&self, key: &Q) -> Option<K::Strong>
pub fn get_key<Q>(&self, key: &Q) -> Option<K::Strong>
Returns a strong reference to the key, if found.
expected O(1) time; worst-case O(p) time
Sourcepub fn get_both<Q>(&self, key: &Q) -> Option<(K::Strong, &V)>
pub fn get_both<Q>(&self, key: &Q) -> Option<(K::Strong, &V)>
Returns a pair of a strong reference to the key, and a reference to the value, if present.
expected O(1) time; worst-case O(p) time
Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
Returns a mutable reference to the value corresponding to the key.
Returns None if no matching key is found.
expected O(1) time; worst-case O(p) time
Sourcepub fn get_both_mut<Q>(&mut self, key: &Q) -> Option<(K::Strong, &mut V)>
pub fn get_both_mut<Q>(&mut self, key: &Q) -> Option<(K::Strong, &mut V)>
Returns a pair of a strong reference to the key, and a mutable reference to the value, if present.
expected O(1) time; worst-case O(p) time
Sourcepub fn get_disjoint_mut<Q, const N: usize>(
&mut self,
ks: [&Q; N],
) -> [Option<&mut V>; N]
pub fn get_disjoint_mut<Q, const N: usize>( &mut self, ks: [&Q; N], ) -> [Option<&mut V>; N]
Looks up mutable references to the values corresponding to several keys at a time.
(Because of borrowing rules, Rust doesn’t allow you to call get_mut() again
while the result of a previous get_mut() is still live. This method exists
to work around that limitation.)
Only one mutable reference can exist to any given value at a time. Therefore, all keys must refer to different values, or this method will panic.
expected O(1 N^2) time; worst-case O(p N^2) time,
where N is the length of the array.
§Panics
Panics if any keys refer to the same value.
Sourcepub fn get_both_disjoint_mut<Q, const N: usize>(
&mut self,
ks: [&Q; N],
) -> [Option<(K::Strong, &mut V)>; N]
pub fn get_both_disjoint_mut<Q, const N: usize>( &mut self, ks: [&Q; N], ) -> [Option<(K::Strong, &mut V)>; N]
Looks up mutable references to the values corresponding to several keys at a time. Returns those references along with their keys as stored in the table.
(Because of borrowing rules, Rust doesn’t allow you to call get_mut() again
while the result of a previous get_mut() is still live. This method exists
to work around that limitation.)
Only one mutable reference can exist to any given value at a time. Therefore, all keys must refer to different values, or this method will panic.
expected O(1 N^2) time; worst-case O(p N^2) time,
where N is the length of the array.
§Panics
Panics if any keys refer to the same value.
Sourcepub fn insert(&mut self, key: K::Strong, value: V) -> Option<V>
pub fn insert(&mut self, key: K::Strong, value: V) -> Option<V>
Unconditionally inserts the value, returning the old value if already present.
Unlike std::collections::HashMap, this replaces the key even the entry was occupied.
expected O(1) time; worst-case O(p) time
Sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<V>
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
Removes the entry with the given key, if it exists, and returns the value.
expected O(1) time; worst-case O(p) time
Sourcepub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K::Strong, V)>
pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K::Strong, V)>
Removes the entry with the given key, if it exists, and returns both the key and value.
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 mappings not satisfying the given predicate.
Also removes any expired mappings.
O(n) time
Sourcepub fn is_submap_with<F, S1, V1>(
&self,
other: &WeakKeyHashMap<K, V1, S1>,
cmp: F,
) -> bool
pub fn is_submap_with<F, S1, V1>( &self, other: &WeakKeyHashMap<K, V1, S1>, cmp: F, ) -> bool
Is this map a submap of the other under the given value comparison cmp?
In particular, for every key k of self,
kmust also be a key ofotherandcmp(self[k], other[k])must hold.
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 is_submap<V1, S1>(&self, other: &WeakKeyHashMap<K, V1, S1>) -> boolwhere
V: PartialEq<V1>,
S1: BuildHasher,
pub fn is_submap<V1, S1>(&self, other: &WeakKeyHashMap<K, V1, S1>) -> boolwhere
V: PartialEq<V1>,
S1: BuildHasher,
Is self a submap 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 domain_is_subset<V1, S1>(
&self,
other: &WeakKeyHashMap<K, V1, S1>,
) -> boolwhere
S1: BuildHasher,
pub fn domain_is_subset<V1, S1>(
&self,
other: &WeakKeyHashMap<K, V1, S1>,
) -> boolwhere
S1: BuildHasher,
Are the keys of self a subset of the keys 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§impl<K: WeakElement, V, S> WeakKeyHashMap<K, V, S>
impl<K: WeakElement, V, S> WeakKeyHashMap<K, V, S>
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
Gets an iterator over the keys and mutable values.
O(1) time
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
Gets an iterator over the mutable values.
O(1) time
Sourcepub fn drain(&mut self) -> Drain<'_, K, V> ⓘ
pub fn drain(&mut self) -> Drain<'_, K, V> ⓘ
Gets a draining iterator, which removes all the values but retains the storage.
O(1) time (and O(n) time to dispose of the result)
Sourcepub fn into_keys(self) -> IntoKeys<K, V> ⓘ
pub fn into_keys(self) -> IntoKeys<K, V> ⓘ
Returns an iterator that owns and consumes this map, returning the keys in the map.
O(1)
Sourcepub fn into_values(self) -> IntoValues<K, V> ⓘ
pub fn into_values(self) -> IntoValues<K, V> ⓘ
Returns an iterator that owns and consumes this map, returning the keys in the map.
O(1)
Sourcepub fn extract_if<'a, F>(&'a mut self, f: F) -> ExtractIf<'a, K, V, F> ⓘ
pub fn extract_if<'a, F>(&'a mut self, f: F) -> ExtractIf<'a, K, V, 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<K: Clone, V: Clone, S: Clone> Clone for WeakKeyHashMap<K, V, S>
impl<K: Clone, V: Clone, S: Clone> Clone for WeakKeyHashMap<K, V, S>
Source§fn clone(&self) -> WeakKeyHashMap<K, V, S>
fn clone(&self) -> WeakKeyHashMap<K, V, 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<K: WeakElement, V: Debug, S> Debug for WeakKeyHashMap<K, V, S>
impl<K: WeakElement, V: Debug, S> Debug for WeakKeyHashMap<K, V, S>
Source§impl<K, V, S: BuildHasher + Default> Default for WeakKeyHashMap<K, V, S>
impl<K, V, S: BuildHasher + Default> Default for WeakKeyHashMap<K, V, S>
impl<K: WeakKey, V: Eq, S: BuildHasher> Eq for WeakKeyHashMap<K, V, S>
Source§impl<'a, K, V, S> Extend<(&'a <K as WeakElement>::Strong, &'a V)> for WeakKeyHashMap<K, V, S>
impl<'a, K, V, S> Extend<(&'a <K as WeakElement>::Strong, &'a V)> for WeakKeyHashMap<K, V, S>
Source§fn extend<T: IntoIterator<Item = (&'a K::Strong, &'a V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (&'a K::Strong, &'a V)>>(&mut self, iter: T)
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<K, V, S> Extend<(<K as WeakElement>::Strong, V)> for WeakKeyHashMap<K, V, S>where
K: WeakKey,
S: BuildHasher,
impl<K, V, S> Extend<(<K as WeakElement>::Strong, V)> for WeakKeyHashMap<K, V, S>where
K: WeakKey,
S: BuildHasher,
Source§fn extend<T: IntoIterator<Item = (K::Strong, V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K::Strong, V)>>(&mut self, iter: T)
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)