Struct stack_map::StackMap

source ·
pub struct StackMap<K: Clone + Ord, V: Clone, const FANOUT: usize> { /* private fields */ }
Expand description

StackMap is a constant-size, zero-allocation associative container backed by an array. It can be used as a building block for various interesting higher-level data structures.

Implementations§

source§

impl<K: Clone + Ord, V: Clone, const FANOUT: usize> StackMap<K, V, FANOUT>

source

pub fn get<Q>(&self, key: &Q) -> Option<&V>where K: Borrow<Q>, Q: Ord + ?Sized,

source

pub fn insert(&mut self, key: K, value: V) -> Option<V>

Inserts an item and return the previous value if it exists.

Panics

This method will panic if called with a new key-value pair when already full.

The StackMap should be checked to ensure that it is not already full before calling this method. It is full when the self.is_full() method returns true, which happens when self.len() == FANOUT.

source

pub fn remove<Q>(&mut self, key: &Q) -> Option<V>where K: Borrow<Q>, Q: Ord + ?Sized,

source

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

source

pub fn iter(&self) -> impl DoubleEndedIterator<Item = &(K, V)>

source

pub fn split_off(&mut self, split_idx: usize) -> (K, Self)

Splits this StackMap into two. self will retain all key-value pairs before the provided split index. Returns the split key at the given split index and a new StackMap created out of all key-value pairs at or after the provided split index.

source

pub fn get_index(&self, index: usize) -> Option<&(K, V)>

Get a key-value pair based on its internal relative index in the backing array.

source

pub fn get_less_than_or_equal<Q>(&self, key: &Q) -> Option<&(K, V)>where K: Borrow<Q>, Q: Ord + ?Sized,

Get the key-value pair that is less than or equal to the provided key. Useful for any least upper bound operation, such as MVCC lookups where the key is suffixed by a version or an internal b-tree index lookup where you are looking for the next node based on a node’s low key.

Examples
let mut map = stack_map::StackMap::<u8, u8, 64>::default();
map.insert(1, 1);
map.insert(2, 2);
map.insert(3, 3);

let lt = map.get_less_than_or_equal(&4).unwrap();
let expected = &(3, 3);
assert_eq!(expected, lt);

let lt = map.get_less_than_or_equal(&3).unwrap();
let expected = &(3, 3);
assert_eq!(expected, lt);

let lt = map.get_less_than_or_equal(&2).unwrap();
let expected = &(2, 2);
assert_eq!(expected, lt);

let lt = map.get_less_than_or_equal(&1).unwrap();
let expected = &(1, 1);
assert_eq!(expected, lt);

let lt = map.get_less_than_or_equal(&0);
let expected = None;
assert_eq!(expected, lt);
source

pub fn get_less_than<Q>(&self, key: &Q) -> Option<&(K, V)>where K: Borrow<Q>, Q: Ord + ?Sized,

Gets a kv pair that has a key that is less than the provided key.

Examples
let mut map = stack_map::StackMap::<u8, u8, 64>::default();
map.insert(1, 1);
map.insert(2, 2);
map.insert(3, 3);

let lt = map.get_less_than(&4).unwrap();
let expected = &(3, 3);
assert_eq!(expected, lt);

let lt = map.get_less_than(&3).unwrap();
let expected = &(2, 2);
assert_eq!(expected, lt);

let lt = map.get_less_than(&2).unwrap();
let expected = &(1, 1);
assert_eq!(expected, lt);

let lt = map.get_less_than(&1);
let expected = None;
assert_eq!(expected, lt);

let lt = map.get_less_than(&0);
let expected = None;
assert_eq!(expected, lt);
source

pub const fn is_full(&self) -> bool

source

pub const fn len(&self) -> usize

source

pub const fn is_empty(&self) -> bool

Trait Implementations§

source§

impl<K: Clone + Ord, V: Clone, const FANOUT: usize> Clone for StackMap<K, V, FANOUT>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<K: Debug + Clone + Ord, V: Debug + Clone, const FANOUT: usize> Debug for StackMap<K, V, FANOUT>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<K: Clone + Ord, V: Clone, const FANOUT: usize> Default for StackMap<K, V, FANOUT>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<K: Clone + Ord, V: Clone, const FANOUT: usize> Drop for StackMap<K, V, FANOUT>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<K, V, const FANOUT: usize> RefUnwindSafe for StackMap<K, V, FANOUT>where K: RefUnwindSafe, V: RefUnwindSafe,

§

impl<K, V, const FANOUT: usize> Send for StackMap<K, V, FANOUT>where K: Send, V: Send,

§

impl<K, V, const FANOUT: usize> Sync for StackMap<K, V, FANOUT>where K: Sync, V: Sync,

§

impl<K, V, const FANOUT: usize> Unpin for StackMap<K, V, FANOUT>where K: Unpin, V: Unpin,

§

impl<K, V, const FANOUT: usize> UnwindSafe for StackMap<K, V, FANOUT>where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere 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 Twhere 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.