HashMap

Struct HashMap 

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

Source

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

pub fn with_capacity(capacity: usize, policy: P) -> Self

Source§

impl<K, V, P, H> HashMap<K, V, P, H>
where P: ExpirePolicy, K: Hash + Eq, H: BuildHasher,

Source

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

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

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

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

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

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());
Source

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

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

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

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

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

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

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

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

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

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

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

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,

Source

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());
Source

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

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

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

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());
Source

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);
}
Source

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);
}
Source

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);
}
Source

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);
}
Source

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

Trait Implementations§

Source§

impl<K, V, P, H> RefUnwindSafe for HashMap<K, V, P, H>

Source§

impl<K, V, P, H> Send for HashMap<K, V, P, H>
where P: ExpirePolicy + Send, K: Send, V: Send, H: Send,

Source§

impl<K, V, P, H> Sync for HashMap<K, V, P, H>
where P: ExpirePolicy + Sync, K: Sync, V: Sync, H: Sync,

Source§

impl<K, V, P, H> Unpin for HashMap<K, V, P, H>
where P: ExpirePolicy + Unpin, K: Unpin, V: Unpin, H: Unpin,

Source§

impl<K, V, P, H> UnwindSafe for HashMap<K, V, P, H>

Auto Trait Implementations§

§

impl<K, V, P, H = RandomState> !Freeze for HashMap<K, V, P, H>

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> 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.