pub struct SBTreeMap<K: StableType + AsFixedSizeBytes + Ord, V: StableType + AsFixedSizeBytes> { /* private fields */ }Expand description
Right-biased B-plus tree based map data structure
Entries are stored in ascending order of their keys. Use std::cmp::Reverse or a custom std::cmp::Ord impl, to differ the order.
B is 8. This implementation is optimized to perform as few stable memory (de)allocations
as possible. Also, this data structure implements several non-conventional functions in order to
share code with other data structures, based on this one.
This is an “infinite” data structure - it can handle up to u64::MAX key-value entries.
Both K and V have to implement StableType and AsFixedSizeBytes traits. SBTreeMap also
implements these trait, so you can nest it in other stable structures.
Implementations§
Source§impl<K: StableType + AsFixedSizeBytes + Ord, V: StableType + AsFixedSizeBytes> SBTreeMap<K, V>
impl<K: StableType + AsFixedSizeBytes + Ord, V: StableType + AsFixedSizeBytes> SBTreeMap<K, V>
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 the provided key-value pair into this SBTreeMap
May allocate stable and heap memory. If your canister is out of stable memory, will return Err with the key-value pair that was about to get inserted.
If the insertion is successful, returns Option with a value, that was previously stored under this key.
§Example
let mut map = SBTreeMap::new();
match map.insert(10u64, 100u64) {
Ok(prev) => println!("Success! Previous value == {prev:?}"),
Err((k, v)) => println!("Out of memory. Unable to insert pair: {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. May release some of stable memory occupied by this stable structure.
§Examples
let mut map = SBTreeMap::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 = SBTreeMap::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 SBTreeMap::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 = SBTreeMap::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_random_key(&self, seed: u32) -> Option<SRef<'_, K>>
pub fn get_random_key(&self, seed: u32) -> Option<SRef<'_, K>>
Returns a random key, deterministically deriving the randomness from the seed. This function is usefull, when you have a source of real randomness.
If the collection is empty, returns None.
Same seed on the same collection leads to the same returned key. Same seed on a modified collection may still lead to the same returned key. You can use [utils::math::shuffle_bits] function to pseudo-randomly generate more seeds.
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 SBTreeMap::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 fn iter(&self) -> SBTreeMapIter<'_, K, V>
pub fn iter(&self) -> SBTreeMapIter<'_, K, V>
Returns an iterator over entries of this SBTreeMap
Elements of this iterator are presented in ascending order.
§Example
let mut map = SBTreeMap::new();
for i in 0..100 {
map.insert(i, i).expect("Out of memory");
}
let mut i = 0;
for (k, v) in map.iter() {
assert_eq!(*k, i);
assert_eq!(*v, i);
i += 1;
}
assert_eq!(i, 100);One can use .rev() to get elements in reverse order.
§Example
let mut map = SBTreeMap::new();
for i in 0..100 {
map.insert(i, i).expect("Out of memory");
}
let mut i = 100;
for (k, v) in map.iter().rev() {
i -= 1;
assert_eq!(*k, i);
assert_eq!(*v, i);
}
assert_eq!(i, 0);Source§impl<K: StableType + AsFixedSizeBytes + Ord + Debug, V: StableType + AsFixedSizeBytes + Debug> SBTreeMap<K, V>
impl<K: StableType + AsFixedSizeBytes + Ord + Debug, V: StableType + AsFixedSizeBytes + Debug> SBTreeMap<K, V>
pub fn debug_print_stack(&self)
pub fn debug_print(&self)
Trait Implementations§
Source§impl<K: StableType + AsFixedSizeBytes + Ord, V: StableType + AsFixedSizeBytes> AsFixedSizeBytes for SBTreeMap<K, V>
impl<K: StableType + AsFixedSizeBytes + Ord, V: StableType + AsFixedSizeBytes> AsFixedSizeBytes for SBTreeMap<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 + Ord + Debug, V: StableType + AsFixedSizeBytes + Debug> Debug for SBTreeMap<K, V>
impl<K: StableType + AsFixedSizeBytes + Ord + Debug, V: StableType + AsFixedSizeBytes + Debug> Debug for SBTreeMap<K, V>
Source§impl<K: StableType + AsFixedSizeBytes + Ord, V: StableType + AsFixedSizeBytes> Default for SBTreeMap<K, V>
impl<K: StableType + AsFixedSizeBytes + Ord, V: StableType + AsFixedSizeBytes> Default for SBTreeMap<K, V>
Source§impl<K: StableType + AsFixedSizeBytes + Ord, V: StableType + AsFixedSizeBytes> Drop for SBTreeMap<K, V>
impl<K: StableType + AsFixedSizeBytes + Ord, V: StableType + AsFixedSizeBytes> Drop for SBTreeMap<K, V>
Source§impl<K: StableType + AsFixedSizeBytes + Ord, V: StableType + AsFixedSizeBytes> StableType for SBTreeMap<K, V>
impl<K: StableType + AsFixedSizeBytes + Ord, V: StableType + AsFixedSizeBytes> StableType for SBTreeMap<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