Struct memo_map::MemoMap

source ·
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>

source

pub fn new() -> MemoMap<K, V, RandomState>

Creates an empty MemoMap.

source§

impl<K, V, S> MemoMap<K, V, S>

source

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>
where K: Eq + Hash, S: BuildHasher,

source

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.

source

pub fn replace(&mut self, key: K, value: V)

Inserts a value into the memo map replacing the old value.

This has the same restrictions as remove and clear in that it requires a mutable reference to the map.

source

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

Returns true if the map contains a value for the specified key.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

source

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

Returns a reference to the value corresponding to the key.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

source

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where Q: Hash + Eq + ?Sized, K: Borrow<Q>,

Returns a mutable reference to the value corresponding to the key.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

source

pub fn get_or_try_insert<Q, F, E>(&self, key: &Q, creator: F) -> Result<&V, E>
where Q: Hash + Eq + ToOwned<Owned = K> + ?Sized, K: Borrow<Q>, F: FnOnce() -> 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.

source

pub fn get_or_insert_owned<F>(&self, key: K, creator: F) -> &V
where F: FnOnce() -> V,

Like get_or_insert but with an owned key.

source

pub fn get_or_try_insert_owned<F, E>(&self, key: K, creator: F) -> Result<&V, E>
where F: FnOnce() -> 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.

source

pub fn get_or_insert<Q, F>(&self, key: &Q, creator: F) -> &V
where Q: Hash + Eq + ToOwned<Owned = K> + ?Sized, K: Borrow<Q>, F: FnOnce() -> 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");
source

pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where Q: Hash + Eq + ?Sized, K: Borrow<Q>,

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.

source

pub fn clear(&mut self)

Clears the map, removing all elements.

source

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);
source

pub fn is_empty(&self) -> bool

Returns true if the memo map contains no items.

source

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.

source

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.

source

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.

source

pub fn keys(&self) -> Keys<'_, K, V, S>

An iterator visiting all keys in arbitrary order. The iterator element type is &'a K.

Trait Implementations§

source§

impl<K: Clone, V: Clone, S: Clone> Clone for MemoMap<K, V, S>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<K: Debug, V: Debug, S: Debug> Debug for MemoMap<K, V, S>

source§

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

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

impl<K, V, S: Default> Default for MemoMap<K, V, S>

source§

fn default() -> Self

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

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>
where S: Send, K: Send, V: Send,

§

impl<K, V, S> Sync for MemoMap<K, V, S>
where S: Send, K: Send, V: Send,

§

impl<K, V, S> Unpin for MemoMap<K, V, S>
where S: Unpin, K: Unpin,

§

impl<K, V, S> UnwindSafe for MemoMap<K, V, S>

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§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

§

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

§

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

§

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.