pub struct PtrWeakKeyHashMap<K, V, S = RandomState>(/* private fields */);
Expand description
A hash map with weak keys, hashed on key pointer.
When a weak pointer expires, its mapping is lazily removed.
§Examples
use weak_table::PtrWeakKeyHashMap;
use std::rc::{Rc, Weak};
type Table = PtrWeakKeyHashMap<Weak<str>, usize>;
let mut map = Table::new();
let a = Rc::<str>::from("hello");
let b = Rc::<str>::from("hello");
map.insert(a.clone(), 5);
assert_eq!( map.get(&a), Some(&5) );
assert_eq!( map.get(&b), None );
map.insert(b.clone(), 7);
assert_eq!( map.get(&a), Some(&5) );
assert_eq!( map.get(&b), Some(&7) );
Implementations§
Source§impl<K: WeakElement, V> PtrWeakKeyHashMap<K, V, RandomState>
impl<K: WeakElement, V> PtrWeakKeyHashMap<K, V, RandomState>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty PtrWeakKeyHashMap
with the given capacity.
O(n) time
Source§impl<K: WeakElement, V, S: BuildHasher> PtrWeakKeyHashMap<K, V, S>
impl<K: WeakElement, V, S: BuildHasher> PtrWeakKeyHashMap<K, V, S>
Sourcepub fn with_hasher(hash_builder: S) -> Self
pub fn with_hasher(hash_builder: S) -> Self
Creates an empty PtrWeakKeyHashMap
with the given capacity and 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 PtrWeakKeyHashMap
with the given capacity and hasher.
O(n) time
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 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.
O(n) time
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity to the minimum allowed to hold the current number of elements.
O(n) time
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Is the map known to be empty?
This could answer false
for an empty map whose keys have
expired but have yet to be collected.
O(1) time
Sourcepub fn load_factor(&self) -> f32
pub fn load_factor(&self) -> f32
The proportion of buckets that are used.
This is an over-approximation because of expired keys.
O(1) time
Sourcepub fn entry(&mut self, key: K::Strong) -> Entry<'_, ByPtr<K>, V>
pub fn entry(&mut self, key: K::Strong) -> Entry<'_, ByPtr<K>, V>
Gets the requested entry.
expected O(1) time; worst-case O(p) time
Sourcepub fn get(&self, key: &K::Strong) -> Option<&V>
pub fn get(&self, key: &K::Strong) -> Option<&V>
Returns a reference to the value corresponding to the key.
expected O(1) time; worst-case O(p) time
Sourcepub fn contains_key(&self, key: &K::Strong) -> bool
pub fn contains_key(&self, key: &K::Strong) -> bool
Returns true if the map contains the specified key.
expected O(1) time; worst-case O(p) time
Sourcepub fn get_mut(&mut self, key: &K::Strong) -> Option<&mut V>
pub fn get_mut(&mut self, key: &K::Strong) -> Option<&mut V>
Returns a mutable reference to the value corresponding to the key.
expected O(1) time; worst-case O(p) time
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. Does not replace the key.
expected O(1) time; worst-case O(p) time
Sourcepub fn remove(&mut self, key: &K::Strong) -> Option<V>
pub fn remove(&mut self, key: &K::Strong) -> 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 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 submap_with<F, S1, V1>(
&self,
other: &PtrWeakKeyHashMap<K, V1, S1>,
value_equal: F,
) -> bool
pub fn submap_with<F, S1, V1>( &self, other: &PtrWeakKeyHashMap<K, V1, S1>, value_equal: F, ) -> bool
Is this map a submap of the other, using the given value comparison.
In particular, all the keys of self must be in other and the values must compare true with value_equal.
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: &PtrWeakKeyHashMap<K, V1, S1>) -> boolwhere
V: PartialEq<V1>,
S1: BuildHasher,
pub fn is_submap<V1, S1>(&self, other: &PtrWeakKeyHashMap<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: &PtrWeakKeyHashMap<K, V1, S1>,
) -> boolwhere
S1: BuildHasher,
pub fn domain_is_subset<V1, S1>(
&self,
other: &PtrWeakKeyHashMap<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> PtrWeakKeyHashMap<K, V, S>
impl<K: WeakElement, V, S> PtrWeakKeyHashMap<K, V, S>
Sourcepub fn iter(&self) -> Iter<'_, ByPtr<K>, V> ⓘ
pub fn iter(&self) -> Iter<'_, ByPtr<K>, V> ⓘ
Gets an iterator over the keys and values.
O(1) time
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, ByPtr<K>, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, ByPtr<K>, V> ⓘ
Gets an iterator over the keys and mutable values.
O(1) time
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, ByPtr<K>, V> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, ByPtr<K>, V> ⓘ
Gets an iterator over the mutable values.
O(1) time
Trait Implementations§
Source§impl<K: Clone, V: Clone, S: Clone> Clone for PtrWeakKeyHashMap<K, V, S>
impl<K: Clone, V: Clone, S: Clone> Clone for PtrWeakKeyHashMap<K, V, S>
Source§fn clone(&self) -> PtrWeakKeyHashMap<K, V, S>
fn clone(&self) -> PtrWeakKeyHashMap<K, V, S>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<K, V: Debug, S> Debug for PtrWeakKeyHashMap<K, V, S>
impl<K, V: Debug, S> Debug for PtrWeakKeyHashMap<K, V, S>
Source§impl<K: WeakElement, V, S: BuildHasher + Default> Default for PtrWeakKeyHashMap<K, V, S>
impl<K: WeakElement, V, S: BuildHasher + Default> Default for PtrWeakKeyHashMap<K, V, S>
Source§impl<'a, K, V, S> Extend<(&'a <K as WeakElement>::Strong, &'a V)> for PtrWeakKeyHashMap<K, V, S>
impl<'a, K, V, S> Extend<(&'a <K as WeakElement>::Strong, &'a V)> for PtrWeakKeyHashMap<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 PtrWeakKeyHashMap<K, V, S>
impl<K, V, S> Extend<(<K as WeakElement>::Strong, V)> for PtrWeakKeyHashMap<K, V, S>
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
)Source§impl<K, V, S> FromIterator<(<K as WeakElement>::Strong, V)> for PtrWeakKeyHashMap<K, V, S>
impl<K, V, S> FromIterator<(<K as WeakElement>::Strong, V)> for PtrWeakKeyHashMap<K, V, S>
Source§impl<'a, K, V, S> Index<&'a <K as WeakElement>::Strong> for PtrWeakKeyHashMap<K, V, S>
impl<'a, K, V, S> Index<&'a <K as WeakElement>::Strong> for PtrWeakKeyHashMap<K, V, S>
Source§impl<'a, K, V, S> IndexMut<&'a <K as WeakElement>::Strong> for PtrWeakKeyHashMap<K, V, S>
impl<'a, K, V, S> IndexMut<&'a <K as WeakElement>::Strong> for PtrWeakKeyHashMap<K, V, S>
Source§impl<'a, K: WeakElement, V, S> IntoIterator for &'a PtrWeakKeyHashMap<K, V, S>
impl<'a, K: WeakElement, V, S> IntoIterator for &'a PtrWeakKeyHashMap<K, V, S>
Source§impl<'a, K: WeakElement, V, S> IntoIterator for &'a mut PtrWeakKeyHashMap<K, V, S>
impl<'a, K: WeakElement, V, S> IntoIterator for &'a mut PtrWeakKeyHashMap<K, V, S>
Source§impl<K: WeakElement, V, S> IntoIterator for PtrWeakKeyHashMap<K, V, S>
impl<K: WeakElement, V, S> IntoIterator for PtrWeakKeyHashMap<K, V, S>
Source§impl<K, V, V1, S, S1> PartialEq<PtrWeakKeyHashMap<K, V1, S1>> for PtrWeakKeyHashMap<K, V, S>
impl<K, V, V1, S, S1> PartialEq<PtrWeakKeyHashMap<K, V1, S1>> for PtrWeakKeyHashMap<K, V, S>
Source§fn eq(&self, other: &PtrWeakKeyHashMap<K, V1, S1>) -> bool
fn eq(&self, other: &PtrWeakKeyHashMap<K, V1, S1>) -> bool
self
and other
values to be equal, and is used by ==
.