pub struct SHashMap<K: StableType + AsFixedSizeBytes + Hash + Eq, V: StableType + AsFixedSizeBytes> { /* private fields */ }Expand description
Reallocating, open addressing, linear probing, eager removes hash map
Conceptually the same thing as std::collections::HashMap, but with a couple of twists:
- zwohash is used, instead of
SipHash, to make hashes faster and deterministic between canister upgrades. - eager removes (no tombstones) are performed in order to prevent performance degradation.
This is a “finite” data structure - it can only handle up to u32::MAX / (1 + K::SIZE + V::SIZE)
elements total. Putting more elements inside will panic.
Both K and V have to implement StableType and AsFixedSizeBytes traits. SHashMap also
implements these traits itself, so you can nest it inside other stable structures.
Implementations§
Source§impl<K: StableType + AsFixedSizeBytes + Hash + Eq, V: StableType + AsFixedSizeBytes> SHashMap<K, V>
impl<K: StableType + AsFixedSizeBytes + Hash + Eq, V: StableType + AsFixedSizeBytes> SHashMap<K, V>
Sourcepub fn new_with_capacity(capacity: usize) -> Result<Self, OutOfMemory>
pub fn new_with_capacity(capacity: usize) -> Result<Self, OutOfMemory>
Creates a SHashMap of requested capacity.
Does allocate stable memory, returning OutOfMemory if there is not enough of it.
If this function returns Ok, you are guaranteed to have enough stable memory to store at
least capacity entries in it.
§Example
let mut at_least_10_number_pairs = SHashMap::<u64, u64>::new_with_capacity(10)
.expect("Out of memory");Sourcepub fn insert(&mut self, key: K, value: V) -> Result<Option<V>, (K, V)>
pub fn insert(&mut self, key: K, value: V) -> Result<Option<V>, (K, V)>
Inserts a key-value pair in this SHashMap
Will try to reallocate, if length == capacity * 3/4 and there is no key-value pair stored by the
same key. If the canister is out of stable memory, will return Err with the key-value pair
that was about to get inserted.
If the insertion was successful, returns Option with a previous value stored by this key, if there was one.
Reallocation triggers a process of complete rehashing of keys.
§Example
let mut map = SHashMap::new();
match map.insert(1, 10) {
Ok(prev) => println!("Success! Previous value == {prev:?}"),
Err((k, v)) => println!("Out of memory. Unable to insert: {k}, {v}"),
};Sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<V>
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
Removes a key-value pair by the provided key
Returns None if no pair was found by this key
§Examples
let mut map = SHashMap::new();
map.insert(1, 10).expect("Out of memory");
assert_eq!(map.remove(&1).unwrap(), 10);Borrowed type is also accepted. If your key type is, for example, [SBox] of String, then you can remove the pair by String.
let mut map = SHashMap::new();
let str_key = String::from("The key");
let key = SBox::new(str_key.clone()).expect("Out of memory");
map.insert(key, 10).expect("Out of memory");
assert_eq!(map.remove(&str_key).unwrap(), 10);Sourcepub fn get<Q>(&self, key: &Q) -> Option<SRef<'_, V>>
pub fn get<Q>(&self, key: &Q) -> Option<SRef<'_, V>>
Returns an immutable reference SRef to a value stored by the key
See also SHashMap::get_mut.
If no such key-value pair is found, returns None
Borrowed type is also accepted. If your key type is, for example, [SBox] of String, then you can get the value by String.
§Example
let mut map = SHashMap::new();
let str_key = String::from("The key");
let key = SBox::new(str_key.clone()).expect("Out of memory");
map.insert(key, 10).expect("Out of memory");
assert_eq!(*map.get(&str_key).unwrap(), 10);Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<SRefMut<'_, V>>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<SRefMut<'_, V>>
Returns a mutable reference SRefMut to a value stored by the key
See also SHashMap::get.
If no such key-value pair is found, returns None
Borrowed type is also accepted. If your key type is, for example, [SBox] of String, then you can get the value by String.
Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
Sourcepub const fn max_capacity() -> usize
pub const fn max_capacity() -> usize
Returns the maximum possible capacity of this SHashMap
Sourcepub const fn is_full(&self) -> bool
pub const fn is_full(&self) -> bool
Returns true if the next unique key insert will trigger the reallocation and rehashing
Sourcepub fn debug_print(&self)
pub fn debug_print(&self)
Prints byte representation of this SHashMap
Useful for tests
Trait Implementations§
Source§impl<K: StableType + AsFixedSizeBytes + Hash + Eq, V: StableType + AsFixedSizeBytes> AsFixedSizeBytes for SHashMap<K, V>
impl<K: StableType + AsFixedSizeBytes + Hash + Eq, V: StableType + AsFixedSizeBytes> AsFixedSizeBytes for SHashMap<K, V>
Source§fn as_fixed_size_bytes(&self, buf: &mut [u8])
fn as_fixed_size_bytes(&self, buf: &mut [u8])
Source§fn from_fixed_size_bytes(buf: &[u8]) -> Self
fn from_fixed_size_bytes(buf: &[u8]) -> Self
Source§fn as_new_fixed_size_bytes(&self) -> Self::Buf
fn as_new_fixed_size_bytes(&self) -> Self::Buf
Source§impl<K: StableType + AsFixedSizeBytes + Hash + Eq + Debug, V: StableType + AsFixedSizeBytes + Debug> Debug for SHashMap<K, V>
impl<K: StableType + AsFixedSizeBytes + Hash + Eq + Debug, V: StableType + AsFixedSizeBytes + Debug> Debug for SHashMap<K, V>
Source§impl<K: StableType + AsFixedSizeBytes + Hash + Eq, V: StableType + AsFixedSizeBytes> Default for SHashMap<K, V>
impl<K: StableType + AsFixedSizeBytes + Hash + Eq, V: StableType + AsFixedSizeBytes> Default for SHashMap<K, V>
Source§impl<K: StableType + AsFixedSizeBytes + Hash + Eq, V: StableType + AsFixedSizeBytes> Drop for SHashMap<K, V>
impl<K: StableType + AsFixedSizeBytes + Hash + Eq, V: StableType + AsFixedSizeBytes> Drop for SHashMap<K, V>
Source§impl<K: StableType + AsFixedSizeBytes + Hash + Eq, V: StableType + AsFixedSizeBytes> StableType for SHashMap<K, V>
impl<K: StableType + AsFixedSizeBytes + Hash + Eq, V: StableType + AsFixedSizeBytes> StableType for SHashMap<K, V>
Source§unsafe fn stable_drop_flag_off(&mut self)
unsafe fn stable_drop_flag_off(&mut self)
off position, if it is applicable Read moreSource§unsafe fn stable_drop_flag_on(&mut self)
unsafe fn stable_drop_flag_on(&mut self)
on position, if it is applicable Read more