pub struct HashMap<K, V, P, H = DefaultHashBuilder>where
P: ExpirePolicy,{ /* private fields */ }Expand description
A hash map implemented with hashbrown internal.
HashMap supports both Standard-like and hashbrown-like interfaces.
Implementations§
Source§impl<K, V, P> HashMap<K, V, P, DefaultHashBuilder>where
P: ExpirePolicy,
impl<K, V, P> HashMap<K, V, P, DefaultHashBuilder>where
P: ExpirePolicy,
Sourcepub fn new(policy: P) -> Self
pub fn new(policy: P) -> Self
Creates an empty HashMap with specified expire policy.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::time::Duration;
let mut cache = HashMap::<u32, u32, _>::new(LazyFixedTTLPolicy::new(Duration::from_secs(30)));pub fn with_capacity(capacity: usize, policy: P) -> Self
Source§impl<K, V, P, H> HashMap<K, V, P, H>
impl<K, V, P, H> HashMap<K, V, P, H>
Sourcepub fn get<Q>(&self, k: &Q) -> Option<&V>
pub fn get<Q>(&self, k: &Q) -> Option<&V>
Returns a reference to the value corresponding to the key.
If the entry is expired, returns None
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::thread::sleep;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache.insert(0, "0", ());
assert_eq!(cache.get(&0), Some(&"0"));
assert_eq!(cache.get(&1), None);
sleep(Duration::from_millis(10));
assert_eq!(cache.get(&0), None);Sourcepub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)>
Returns the key-value pair corresponding to the supplied key.
If the entry is expired, returns None
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::thread::sleep;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache.insert(0, "0", ());
assert_eq!(cache.get_key_value(&0), Some((&0, &"0")));
assert_eq!(cache.get_key_value(&1), None);
sleep(Duration::from_millis(10));
assert_eq!(cache.get_key_value(&0), None);Sourcepub fn get_mut<Q>(&self, k: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&self, k: &Q) -> Option<&mut V>
Returns a mutable reference to the value corresponding to the key.
If the entry is expired, returns None
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::thread::sleep;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache.insert(0, "0", ());
if let Some(x) = cache.get_mut(&0) {
*x = "1";
sleep(Duration::from_millis(10));
assert_eq!(*x, "1");
}
assert!(cache.get(&0).is_none());Sourcepub fn contains_key<Q>(&self, k: &Q) -> bool
pub fn contains_key<Q>(&self, k: &Q) -> bool
Returns true if the HashMap contains a value for the specified key.
If the entry is expired, returns false
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::thread::sleep;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache.insert(0, "0", ());
assert!(cache.contains_key(&0));
sleep(Duration::from_millis(10));
assert!(!cache.contains_key(&0));Sourcepub fn insert(&mut self, k: K, v: V, init: P::Info) -> Option<V>
pub fn insert(&mut self, k: K, v: V, init: P::Info) -> Option<V>
Inserts a key-value pair into the HashMap.
If the HashMap did not have this key present, None is returned.
If the HashMap did have this key present, the value is updated, and the old value is returned.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::thread::sleep;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
assert_eq!(cache.insert(0, "a", ()), None);
assert!(!cache.is_empty());
assert_eq!(cache.insert(0, "b", ()), Some("a"));
sleep(Duration::from_millis(10));
assert_eq!(cache.insert(0, "c", ()), None);Sourcepub fn remove<Q>(&mut self, k: &Q) -> Option<V>
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
Removes a key from the HashMap, returning the value at the key if the key was previously in the HashMap.
If the HashMap did not have this key present, None is returned.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::thread::sleep;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache.insert(0, "a", ());
cache.insert(1, "b", ());
assert_eq!(cache.remove(&1), Some("b"));
sleep(Duration::from_millis(15));
assert_eq!(cache.remove(&0), None);Sourcepub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
pub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
Removes a key from the HashMap, returning the stored key and value if the key was previously in the HashMap.
If the HashMap did not have this key present, None is returned.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::thread::sleep;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache.insert(0, "a", ());
cache.insert(1, "b", ());
assert_eq!(cache.remove_entry(&1), Some((1, "b")));
sleep(Duration::from_millis(15));
assert_eq!(cache.remove_entry(&0), None);Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more elements to be inserted in the HashMap. The collection may reserve more space to avoid frequent reallocations.
This function updates internal bucket id which tracks EntryId because of bucket relocation.
§Panics
Panics if the new allocation size overflows usize or out of memory.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::time::Duration;
let mut cache = HashMap::<u32, u32, LazyFixedTTLPolicy>::new(LazyFixedTTLPolicy::new(
Duration::from_secs(30),
));
cache.reserve(10);Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the HashMap as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.
This function updates internal bucket id which tracks EntryId because of bucket relocation.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::thread::sleep;
use std::time::Duration;
let mut cache = HashMap::with_capacity(20, LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache.insert(0, "a", ());
cache.insert(1, "b", ());
assert!(cache.capacity() >= 20);
cache.shrink_to_fit();
assert!(cache.capacity() >= 2);Sourcepub fn shrink_to(&mut self, min_capacity: usize)
pub fn shrink_to(&mut self, min_capacity: usize)
Shrinks the capacity of the HashMap with a lower limit. It will drop down no lower than the supplied limit while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.
This function updates internal bucket id which tracks EntryId because of bucket relocation.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::thread::sleep;
use std::time::Duration;
let mut cache = HashMap::with_capacity(20, LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache.insert(0, "a", ());
cache.insert(1, "b", ());
assert!(cache.capacity() >= 20);
cache.shrink_to(10);
assert!(cache.capacity() >= 10);
cache.shrink_to(0);
assert!(cache.capacity() >= 2);Sourcepub fn entry(&mut self, key: K) -> Entry<'_, K, V, P, H>
pub fn entry(&mut self, key: K) -> Entry<'_, K, V, P, H>
Gets the given key’s corresponding entry in the HashMap for in-place manipulation.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::time::Duration;
let mut letters = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_secs(10)));
for ch in "an easy-to-use cache library".chars() {
let counter = letters.entry(ch).or_insert(0, ());
*counter += 1;
}
assert_eq!(letters.get(&'a'), Some(&4));
assert_eq!(letters.get(&'r'), Some(&2));
assert_eq!(letters.get(&'t'), Some(&1));
assert_eq!(letters.get(&'b'), Some(&1));
assert_eq!(letters.get(&'l'), Some(&1));
assert_eq!(letters.get(&'n'), Some(&1));
assert_eq!(letters.get(&'d'), None);Sourcepub fn drain(&mut self) -> Drain<'_, K, V, P> ⓘ
pub fn drain(&mut self) -> Drain<'_, K, V, P> ⓘ
Clears the HashMap, returning all key-value pairs as an iterator. Keeps the allocated memory for reuse.
When drop, this function also triggers internal ExpirePolicy::clear().
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::thread::sleep;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache.insert(0, "a", ());
cache.insert(1, "b", ());
for (k, v) in cache.drain() {
assert!(k == 0 || k == 1);
assert!(v == "a" || v == "b");
}
assert!(cache.is_empty());Source§impl<K, V, P, H> HashMap<K, V, P, H>where
P: ExpirePolicy,
impl<K, V, P, H> HashMap<K, V, P, H>where
P: ExpirePolicy,
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the HashMap, removing all key-value pairs. Keeps the allocated memory for reuse.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::thread::sleep;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache.insert(0, "a", ());
cache.clear();
assert!(cache.is_empty());Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the exact number of elements in the HashMap.
This functions is accurate than len_approx but uses slower algorithm O(n).
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::thread::sleep;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
assert_eq!(cache.len(), 0);
cache.insert(0, "a", ());
assert_eq!(cache.len(), 1);Sourcepub fn len_approx(&self) -> usize
pub fn len_approx(&self) -> usize
Returns the number of elements in the HashMap.
This function may contain count of elements in the HashMap that is not actually removed from internal table.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::thread::sleep;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
assert_eq!(cache.len_approx(), 0);
cache.insert(0, "a", ());
assert_eq!(cache.len_approx(), 1);
sleep(Duration::from_millis(10));
assert_eq!(cache.len(), 0);
assert_eq!(cache.len_approx(), 1);Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::time::Duration;
let mut cache = HashMap::<u32, u32, _>::with_capacity(
20,
LazyFixedTTLPolicy::new(Duration::from_millis(10)),
);
assert!(cache.capacity() >= 20);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the HashMap contains no elements.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::thread::sleep;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
assert!(cache.is_empty());
cache.insert(0, "a", ());
assert!(!cache.is_empty());
sleep(Duration::from_millis(10));
assert!(cache.is_empty());Sourcepub fn iter(&self) -> Iter<'_, K, V, P> ⓘ
pub fn iter(&self) -> Iter<'_, K, V, P> ⓘ
An iterator visiting all key-value pairs in arbitrary order. The iterator element type is (&'a K, &'a V).
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache.insert(0, "a", ());
cache.insert(1, "b", ());
cache.insert(2, "c", ());
for (k, v) in cache.iter() {
println!("key: {}, val: {}", k, v);
}Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V, P> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V, P> ⓘ
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).
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache.insert(0, 0, ());
cache.insert(1, 1, ());
cache.insert(2, 2, ());
for (_, v) in cache.iter_mut() {
*v *= 2;
}
for (k, v) in cache.iter() {
println!("key: {}, val: {}", k, v);
}Sourcepub fn values(&self) -> Values<'_, K, V, P> ⓘ
pub fn values(&self) -> Values<'_, K, V, P> ⓘ
An iterator visiting all values in arbitrary order. The iterator element type is &'a V.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache.insert(0, "a", ());
cache.insert(1, "b", ());
cache.insert(2, "c", ());
for v in cache.values() {
println!("{}", v);
}Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V, P> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V, P> ⓘ
An iterator visiting all values mutably in arbitrary order. The iterator element type is &'a mut V.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache.insert(0, 0, ());
cache.insert(1, 1, ());
cache.insert(2, 2, ());
for v in cache.values_mut() {
*v = *v + 6;
}
for v in cache.values() {
println!("{}", v);
}Sourcepub fn keys(&self) -> Keys<'_, K, V, P> ⓘ
pub fn keys(&self) -> Keys<'_, K, V, P> ⓘ
An iterator visiting all keys in arbitrary order. The iterator element type is &'a K.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache.insert(0, "a", ());
cache.insert(1, "b", ());
cache.insert(2, "c", ());
for v in cache.keys() {
println!("{}", v);
}