Struct ic_stable_structures::btreemap::BTreeMap
source · [−]Expand description
A “stable” map based on a B-tree.
The implementation is based on the algorithm outlined in “Introduction to Algorithms” by Cormen et al.
Implementations
sourceimpl<M: Memory + Clone, K: Storable, V: Storable> BTreeMap<M, K, V>
impl<M: Memory + Clone, K: Storable, V: Storable> BTreeMap<M, K, V>
sourcepub fn init(memory: M, max_key_size: u32, max_value_size: u32) -> Self
pub fn init(memory: M, max_key_size: u32, max_value_size: u32) -> Self
Initializes a BTreeMap
.
If the memory provided already contains a BTreeMap
, then that
map is loaded. Otherwise, a new BTreeMap
instance is created.
sourcepub fn new(memory: M, max_key_size: u32, max_value_size: u32) -> Self
pub fn new(memory: M, max_key_size: u32, max_value_size: u32) -> Self
Creates a new instance a BTreeMap
.
The given memory
is assumed to be exclusively reserved for this data
structure and that it starts at address zero. Typically memory
will
be an instance of RestrictedMemory
.
When initialized, the data structure has the following memory layout:
| BTreeHeader | Allocator | … free memory for nodes |
See Allocator
for more details on its own memory layout.
sourcepub fn insert(&mut self, key: K, value: V) -> Result<Option<V>, InsertError>
pub fn insert(&mut self, key: K, value: V) -> Result<Option<V>, InsertError>
Inserts a key-value pair into the map.
The previous value of the key, if present, is returned.
The size of the key/value must be <= the max key/value sizes configured
for the map. Otherwise, an InsertError
is returned.
sourcepub fn get(&self, key: &K) -> Option<V>
pub fn get(&self, key: &K) -> Option<V>
Returns the value associated with the given key if it exists.
sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Returns true
if the key exists in the map, false
otherwise.
sourcepub fn get_memory(&self) -> M
pub fn get_memory(&self) -> M
Returns a reference to the memory used by the map.
sourcepub fn remove(&mut self, key: &K) -> Option<V>
pub fn remove(&mut self, key: &K) -> Option<V>
Removes a key from the map, returning the previous value at the key if it exists.
sourcepub fn iter(&self) -> Iter<'_, M, K, V>ⓘNotable traits for Iter<'_, M, K, V>impl<M: Memory + Clone, K: Storable, V: Storable> Iterator for Iter<'_, M, K, V> type Item = (K, V);
pub fn iter(&self) -> Iter<'_, M, K, V>ⓘNotable traits for Iter<'_, M, K, V>impl<M: Memory + Clone, K: Storable, V: Storable> Iterator for Iter<'_, M, K, V> type Item = (K, V);
Returns an iterator over the entries of the map, sorted by key.
sourcepub fn range(&self, prefix: Vec<u8>, offset: Option<Vec<u8>>) -> Iter<'_, M, K, V>ⓘNotable traits for Iter<'_, M, K, V>impl<M: Memory + Clone, K: Storable, V: Storable> Iterator for Iter<'_, M, K, V> type Item = (K, V);
pub fn range(&self, prefix: Vec<u8>, offset: Option<Vec<u8>>) -> Iter<'_, M, K, V>ⓘNotable traits for Iter<'_, M, K, V>impl<M: Memory + Clone, K: Storable, V: Storable> Iterator for Iter<'_, M, K, V> type Item = (K, V);
Returns an iterator over the entries in the map where keys begin with the given prefix
.
If the optional offset
is set, the iterator returned will start from the entry that
contains this offset
(while still iterating over all remaining entries that begin
with the given prefix
).