pub struct MemoMap<K, V, S = RandomState> { /* private fields */ }
Expand description
An insert only, thread safe hash map to memoize values.
Implementations§
source§impl<K, V> MemoMap<K, V, RandomState>
impl<K, V> MemoMap<K, V, RandomState>
sourcepub fn new() -> MemoMap<K, V, RandomState>
pub fn new() -> MemoMap<K, V, RandomState>
Creates an empty MemoMap
.
source§impl<K, V, S> MemoMap<K, V, S>
impl<K, V, S> MemoMap<K, V, S>
sourcepub fn with_hasher(hash_builder: S) -> MemoMap<K, V, S>
pub fn with_hasher(hash_builder: S) -> MemoMap<K, V, S>
Creates an empty MemoMap
which will use the given hash builder to hash
keys.
source§impl<K, V, S> MemoMap<K, V, S>
impl<K, V, S> MemoMap<K, V, S>
sourcepub fn insert(&self, key: K, value: V) -> bool
pub fn insert(&self, key: K, value: V) -> bool
Inserts a value into the memo map.
This inserts a value for a specific key into the memo map. If the
key already exists, this method does nothing and instead returns false
.
Otherwise the value is inserted and true
is returned. It’s generally
recommended to instead use get_or_insert
or
it’s sibling get_or_try_insert
.
sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
sourcepub fn get_or_try_insert<Q, F, E>(&self, key: &Q, creator: F) -> Result<&V, E>
pub fn get_or_try_insert<Q, F, E>(&self, key: &Q, creator: F) -> Result<&V, E>
Returns a reference to the value corresponding to the key or inserts.
This is the preferred way to work with a memo map: if the value has not been in the map yet the creator function is invoked to create the value, otherwise the already stored value is returned. The creator function itself can be falliable and the error is passed through.
If the creator is infallible, get_or_insert
can be used.
sourcepub fn get_or_insert_owned<F>(&self, key: K, creator: F) -> &Vwhere
F: FnOnce() -> V,
pub fn get_or_insert_owned<F>(&self, key: K, creator: F) -> &Vwhere
F: FnOnce() -> V,
Like get_or_insert
but with an owned key.
sourcepub fn get_or_try_insert_owned<F, E>(&self, key: K, creator: F) -> Result<&V, E>
pub fn get_or_try_insert_owned<F, E>(&self, key: K, creator: F) -> Result<&V, E>
Like get_or_try_insert
but with an owned key.
If the creator is infallible, get_or_insert_owned
can be used.
sourcepub fn get_or_insert<Q, F>(&self, key: &Q, creator: F) -> &V
pub fn get_or_insert<Q, F>(&self, key: &Q, creator: F) -> &V
Returns a reference to the value corresponding to the key or inserts.
This is the preferred way to work with a memo map: if the value has not been in the map yet the creator function is invoked to create the value, otherwise the already stored value is returned.
If the creator is fallible, get_or_try_insert
can be used.
§Example
let memo = MemoMap::new();
// first time inserts
let value = memo.get_or_insert("key", || "23");
assert_eq!(*value, "23");
// second time returns old value
let value = memo.get_or_insert("key", || "24");
assert_eq!(*value, "23");
sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<V>
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
Removes a key from the memo map, returning the value at the key if the key was previously in the map.
A key can only be removed if a mutable reference to the memo map exists. In other words a key can not be removed if there can be borrows to the item.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of items in the map.
§Example
let memo = MemoMap::new();
assert_eq!(memo.len(), 0);
memo.insert(1, "a");
memo.insert(2, "b");
memo.insert(2, "not b");
assert_eq!(memo.len(), 2);
sourcepub fn iter(&self) -> Iter<'_, K, V, S> ⓘ
pub fn iter(&self) -> Iter<'_, K, V, S> ⓘ
An iterator visiting all key-value pairs in arbitrary order. The
iterator element type is (&'a K, &'a V)
.
Important note: during iteration the map is locked! This means that you must not perform calls to the map or you will run into deadlocks. This makes the iterator rather useless in practice for a lot of operations.
sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
An iterator visiting all key-value pairs in arbitrary order, with mutable
references to the values. The iterator element type is (&'a K, &'a mut V)
.
This iterator requires a mutable reference to the map.
sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
An iterator visiting all values mutably in arbitrary order. The iterator
element type is &'a mut V
.
This iterator requires a mutable reference to the map.
Trait Implementations§
Auto Trait Implementations§
impl<K, V, S = RandomState> !Freeze for MemoMap<K, V, S>
impl<K, V, S> RefUnwindSafe for MemoMap<K, V, S>
impl<K, V, S> Send for MemoMap<K, V, S>
impl<K, V, S> Sync for MemoMap<K, V, S>
impl<K, V, S> Unpin for MemoMap<K, V, S>
impl<K, V, S> UnwindSafe for MemoMap<K, V, S>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)