pub struct BTreeMap<K, V, M>{ /* private fields */ }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§
Source§impl<K, V, M> BTreeMap<K, V, M>
impl<K, V, M> BTreeMap<K, V, M>
Sourcepub fn init(memory: M) -> Self
pub fn init(memory: M) -> 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) -> Self
pub fn new(memory: M) -> 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) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts a key-value pair into the map.
The previous value of the key, if present, is returned.
PRECONDITION: key.to_bytes().len() <= Key::MAX_SIZE value.to_bytes().len() <= Value::MAX_SIZE
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 into_memory(self) -> M
pub fn into_memory(self) -> M
Returns the underlying memory.
Sourcepub fn first_key_value(&self) -> Option<(K, V)>
pub fn first_key_value(&self) -> Option<(K, V)>
Returns the first key-value pair in the map. The key in this pair is the minimum key in the map.
Sourcepub fn last_key_value(&self) -> Option<(K, V)>
pub fn last_key_value(&self) -> Option<(K, V)>
Returns the last key-value pair in the map. The key in this pair is the maximum key in 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<'_, K, V, M> ⓘ
pub fn iter(&self) -> Iter<'_, K, V, M> ⓘ
Returns an iterator over the entries of the map, sorted by key.
Sourcepub fn range(&self, key_range: impl RangeBounds<K>) -> Iter<'_, K, V, M> ⓘ
pub fn range(&self, key_range: impl RangeBounds<K>) -> Iter<'_, K, V, M> ⓘ
Returns an iterator over the entries in the map where keys belong to the specified range.
Sourcepub fn iter_upper_bound(&self, bound: &K) -> Iter<'_, K, V, M> ⓘ
pub fn iter_upper_bound(&self, bound: &K) -> Iter<'_, K, V, M> ⓘ
Returns an iterator pointing to the first element below the given bound. Returns an empty iterator if there are no keys below the given bound.