pub struct IterableMap<K, V> { /* private fields */ }Expand description
A map over global state that allows iteration. Each entry at key K_n stores (K_{n}, V, K_{n-1}), where V is the value and K_{n-1} is the key hash of the previous entry.
This creates a constant spatial overhead; every entry stores a pointer to the one inserted before it.
Enables iteration without a guaranteed ordering; updating an existing key does not affect position.
Under the hood, this is a singly-linked HashMap with linear probing for collision resolution. Supports full traversal, typically in reverse-insertion order.
Implementations§
Source§impl<K, V> IterableMap<K, V>
impl<K, V> IterableMap<K, V>
Sourcepub fn new<S: Into<String>>(prefix: S) -> Self
pub fn new<S: Into<String>>(prefix: S) -> Self
Creates an empty IterableMap with the given prefix.
Sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts a key-value pair into the map.
If the map did not have this key present, None is returned.
If the map did have this key present, the value is updated, and the old value is returned.
This has an amortized complexity of O(1), with a worst-case of O(n) when running into collisions.
Sourcepub fn remove(&mut self, key: &K) -> Option<V>
pub fn remove(&mut self, key: &K) -> Option<V>
Removes a key from the map. Returns the associated value if the key exists.
Has a worst-case runtime of O(n).
Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Returns true if the map contains a value for the specified key.
Sourcepub fn keys(&self) -> impl Iterator<Item = K> + '_
pub fn keys(&self) -> impl Iterator<Item = K> + '_
Creates an iterator visiting all the values in arbitrary order.
Sourcepub fn values(&self) -> impl Iterator<Item = V> + '_
pub fn values(&self) -> impl Iterator<Item = V> + '_
Creates an iterator visiting all the values in arbitrary order.
pub fn is_empty(&self) -> bool
Sourcepub fn iter(&self) -> IterableMapIter<'_, K, V> ⓘ
pub fn iter(&self) -> IterableMapIter<'_, K, V> ⓘ
Returns an iterator over the entries in the map.
Traverses entries in reverse-insertion order. Each item is a tuple of the hashed key and the value.
Trait Implementations§
Source§impl<K, V> BorshDeserialize for IterableMap<K, V>
impl<K, V> BorshDeserialize for IterableMap<K, V>
fn deserialize_reader<__R: Read>(reader: &mut __R) -> Result<Self, Error>
Source§fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
Source§fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>where
R: Read,
Source§impl<K, V> BorshSerialize for IterableMap<K, V>
impl<K, V> BorshSerialize for IterableMap<K, V>
Source§impl<K: Clone, V: Clone> Clone for IterableMap<K, V>
impl<K: Clone, V: Clone> Clone for IterableMap<K, V>
Source§fn clone(&self) -> IterableMap<K, V>
fn clone(&self) -> IterableMap<K, V>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more