Struct ic_stable_structures::btreemap::BTreeMap

source ·
pub struct BTreeMap<K, V, M>
where K: Storable + Ord + Clone, V: Storable, M: Memory,
{ /* 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>
where K: Storable + Ord + Clone, V: Storable, M: Memory,

source

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.

source

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.

source

pub fn load(memory: M) -> Self

Loads the map from memory.

source

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() <= max_size(Key) value.to_bytes().len() <= max_size(Value)

source

pub fn get(&self, key: &K) -> Option<V>

Returns the value associated with the given key if it exists.

source

pub fn contains_key(&self, key: &K) -> bool

Returns true if the key exists in the map, false otherwise.

source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

source

pub fn len(&self) -> u64

Returns the number of elements in the map.

source

pub fn into_memory(self) -> M

Returns the underlying memory.

source

pub fn clear(self) -> Self

👎Deprecated since 0.6.3: please use clear_new instead

Removes all elements from the map.

source

pub fn clear_new(&mut self)

Removes all elements from the map.

source

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.

source

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.

source

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.

source

pub fn pop_last(&mut self) -> Option<(K, V)>

Removes and returns the last element in the map. The key of this element is the maximum key that was in the map

source

pub fn pop_first(&mut self) -> Option<(K, V)>

Removes and returns the first element in the map. The key of this element is the minimum key that was in the map

source

pub fn iter(&self) -> Iter<'_, K, V, M>

Returns an iterator over the entries of the map, sorted by key.

source

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.

source

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.

Auto Trait Implementations§

§

impl<K, V, M> Freeze for BTreeMap<K, V, M>
where M: Freeze,

§

impl<K, V, M> RefUnwindSafe for BTreeMap<K, V, M>

§

impl<K, V, M> Send for BTreeMap<K, V, M>
where M: Send, K: Send, V: Send,

§

impl<K, V, M> Sync for BTreeMap<K, V, M>
where M: Sync, K: Sync, V: Sync,

§

impl<K, V, M> Unpin for BTreeMap<K, V, M>
where M: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, M> UnwindSafe for BTreeMap<K, V, M>
where M: UnwindSafe, K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.