CompressedBTreeMap

Struct CompressedBTreeMap 

Source
pub struct CompressedBTreeMap<K: PartialOrd + Debug, V, B: ArrayLength = U6, A: Allocator = Global>
where U2: Mul<B>, Prod<U2, B>: ArrayLength, U1: Add<Prod<U2, B>>, Sum<U1, Prod<U2, B>>: ArrayLength,
{ /* private fields */ }
Expand description

An ergonomic compressed B-Tree map that owns its allocator.

This is a memory-optimized B-Tree that uses specialized leaf nodes without child pointers, reducing memory usage by approximately 30% compared to the naive implementation.

This is the recommended type for most use cases. It wraps the allocated B-Tree and provides safe methods without requiring unsafe blocks or passing allocators manually.

§Example

use allocated_btree::CompressedBTreeMap;

let mut map = CompressedBTreeMap::new();
map.insert(1, "one").unwrap();
map.insert(2, "two").unwrap();

assert_eq!(map.get(&1), Some(&"one"));
assert_eq!(map.len(), 2);

Implementations§

Source§

impl<K: PartialOrd + Debug, V> CompressedBTreeMap<K, V>

Source

pub fn new() -> Self

Create a new empty compressed B-Tree using the global allocator.

§Panics

Panics if allocation fails.

Source§

impl<K: PartialOrd + Debug, V, B: ArrayLength, A: Allocator> CompressedBTreeMap<K, V, B, A>
where U2: Mul<B>, Prod<U2, B>: ArrayLength, U1: Add<Prod<U2, B>>, Sum<U1, Prod<U2, B>>: ArrayLength,

Source

pub fn new_in(alloc: A) -> AllocResult<Self>

Create a new empty compressed B-Tree using the provided allocator.

§Errors

Returns an error if allocation fails.

Source

pub fn len(&self) -> usize

Returns the number of elements in the map.

Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

Source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: PartialOrd + Debug,

Returns true if the map contains a value for the specified key.

Source

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

Returns a reference to the value corresponding to the key.

Source

pub fn get_key_value<'s, Q>(&'s self, key: &'s Q) -> Option<(&'s K, &'s V)>
where K: Borrow<Q>, Q: PartialOrd + Debug,

Returns the key-value pair corresponding to the supplied key.

Source

pub fn get_mut<'s, Q>(&'s mut self, key: &'s Q) -> Option<&'s mut V>
where K: Borrow<Q>, Q: PartialOrd + Debug,

Returns a mutable reference to the value corresponding to the key.

Source

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

Inserts a key-value pair into the map.

If the map did not have this key present, None is returned. If the map did have this key present, the value is updated, and the old value is returned.

§Errors

Returns an error if allocation fails during tree rebalancing.

Source

pub fn clear(&mut self) -> AllocResult<()>

Clears the map, removing all key-value pairs.

§Errors

Returns an error if allocation fails.

Source

pub fn entry(&mut self, key: K) -> Entry<'_, '_, A, K, V, B>

Gets the given key’s corresponding entry in the map for in-place manipulation.

Source

pub fn entry_ref<Q>( &mut self, key: &Q, ) -> Option<OccupiedEntry<'_, '_, A, K, V, B>>
where K: Borrow<Q>, Q: PartialOrd + Debug + ?Sized,

Gets the given key’s corresponding occupied entry in the map for in-place manipulation.

Returns None if the key is not present in the map.

The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.

§Examples
use allocated_btree::CompressedBTreeMap;

let mut map = CompressedBTreeMap::new();
map.insert(1, "a").unwrap();

// Get the entry if it exists and modify it
if let Some(mut entry) = map.entry_ref(&1) {
    *entry.get_mut() = "b";
}
assert_eq!(map.get(&1), Some(&"b"));

// Non-existent key returns None
assert!(map.entry_ref(&2).is_none());

Using String keys with &str lookups:

use allocated_btree::CompressedBTreeMap;

let mut map = CompressedBTreeMap::new();
map.insert("hello".to_string(), 42).unwrap();

if let Some(entry) = map.entry_ref("hello") {
    assert_eq!(entry.key(), &"hello".to_string());
}
Source

pub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, '_, A, K, V, B>>

Gets the first entry in the map for in-place manipulation.

Source

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

Returns the first key-value pair in the map.

Source

pub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, '_, A, K, V, B>>

Gets the last entry in the map for in-place manipulation.

Source

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

Returns the last key-value pair in the map.

Source

pub fn first(&self) -> Option<&K>

Returns the first key in the map.

Source

pub fn last(&self) -> Option<&K>

Returns the last key in the map.

Source

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

Removes and returns the first key-value pair in the map. The key in this pair is the minimum key that was in the map.

§Examples
use allocated_btree::CompressedBTreeMap;

let mut map = CompressedBTreeMap::new();
map.insert(1, "a").unwrap();
map.insert(2, "b").unwrap();

assert_eq!(map.pop_first(), Some((1, "a")));
assert_eq!(map.pop_first(), Some((2, "b")));
assert_eq!(map.pop_first(), None);
Source

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

Removes and returns the last key-value pair in the map. The key in this pair is the maximum key that was in the map.

§Examples
use allocated_btree::CompressedBTreeMap;

let mut map = CompressedBTreeMap::new();
map.insert(1, "a").unwrap();
map.insert(2, "b").unwrap();

assert_eq!(map.pop_last(), Some((2, "b")));
assert_eq!(map.pop_last(), Some((1, "a")));
assert_eq!(map.pop_last(), None);
Source

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

Removes a key from the map, returning the value at the key if the key was previously in the map.

The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.

§Examples
use allocated_btree::CompressedBTreeMap;

let mut map = CompressedBTreeMap::new();
map.insert(1, "a").unwrap();
assert_eq!(map.remove(&1), Some("a"));
assert_eq!(map.remove(&1), None);

Using String keys with &str lookups:

use allocated_btree::CompressedBTreeMap;

let mut map = CompressedBTreeMap::new();
map.insert("hello".to_string(), 42).unwrap();
assert_eq!(map.remove("hello"), Some(42));  // Note: &str, not &String
assert_eq!(map.remove("hello"), None);
Source

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

Removes a key from the map, returning the stored key and value if the key was previously in the map.

The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.

§Examples
use allocated_btree::CompressedBTreeMap;

let mut map = CompressedBTreeMap::new();
map.insert(1, "a").unwrap();
assert_eq!(map.remove_entry(&1), Some((1, "a")));
assert_eq!(map.remove_entry(&1), None);
Source

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

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

Source

pub fn keys(&self) -> Keys<'_, K, V, B>

Gets an iterator over the keys of the map, in sorted order.

Source

pub fn values(&self) -> Values<'_, K, V, B>

Gets an iterator over the values of the map, in order by key.

Source

pub fn values_mut(&mut self) -> ValuesMut<'_, K, V, B>

Gets a mutable iterator over the values of the map, in order by key.

Trait Implementations§

Source§

impl<K: PartialOrd + Debug, V> Default for CompressedBTreeMap<K, V>

Source§

fn default() -> Self

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

impl<K: PartialOrd + Debug, V, B: ArrayLength, A: Allocator> Drop for CompressedBTreeMap<K, V, B, A>
where U2: Mul<B>, Prod<U2, B>: ArrayLength, U1: Add<Prod<U2, B>>, Sum<U1, Prod<U2, B>>: ArrayLength,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<K: PartialOrd + Debug, V, B: ArrayLength, A: Allocator> Extend<(K, V)> for CompressedBTreeMap<K, V, B, A>
where U2: Mul<B>, Prod<U2, B>: ArrayLength, U1: Add<Prod<U2, B>>, Sum<U1, Prod<U2, B>>: ArrayLength,

Source§

fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)

Extends the map with the contents of an iterator.

If the iterator yields duplicate keys, the last value wins.

§Panics

Panics if allocation fails during insertion.

§Examples
use allocated_btree::CompressedBTreeMap;

let mut map = CompressedBTreeMap::new();
map.insert(1, "a").unwrap();

let more_items = vec![(2, "b"), (3, "c")];
map.extend(more_items);

assert_eq!(map.len(), 3);
assert_eq!(map.get(&2), Some(&"b"));
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K: PartialOrd + Debug, V> FromIterator<(K, V)> for CompressedBTreeMap<K, V>

Source§

fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self

Creates a B-tree map from an iterator of key-value pairs.

If the iterator yields multiple values for the same key, the last value wins.

§Panics

Panics if allocation fails during construction.

§Examples
use allocated_btree::CompressedBTreeMap;

let items = vec![(1, "a"), (2, "b"), (3, "c")];
let map: CompressedBTreeMap<_, _> = items.into_iter().collect();

assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), Some(&"b"));
assert_eq!(map.get(&3), Some(&"c"));
assert_eq!(map.len(), 3);
Source§

impl<'s, K: PartialOrd + Debug, V, B: ArrayLength, A: Allocator> IntoIterator for &'s CompressedBTreeMap<K, V, B, A>
where U2: Mul<B>, Prod<U2, B>: ArrayLength, U1: Add<Prod<U2, B>>, Sum<U1, Prod<U2, B>>: ArrayLength,

Source§

type IntoIter = Iter<'s, K, V, B>

Which kind of iterator are we turning this into?
Source§

type Item = (&'s K, &'s V)

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<K, V, B = UInt<UInt<UInt<UTerm, B1>, B1>, B0>, A = Global> !Freeze for CompressedBTreeMap<K, V, B, A>

§

impl<K, V, B = UInt<UInt<UInt<UTerm, B1>, B1>, B0>, A = Global> !RefUnwindSafe for CompressedBTreeMap<K, V, B, A>

§

impl<K, V, B, A> Send for CompressedBTreeMap<K, V, B, A>
where <UInt<UTerm, B1> as Add<<UInt<UInt<UTerm, B1>, B0> as Mul<B>>::Output>>::Output: Sized, <UInt<UInt<UTerm, B1>, B0> as Mul<B>>::Output: Sized, A: Send, K: Send, V: Send,

§

impl<K, V, B, A> Sync for CompressedBTreeMap<K, V, B, A>
where <UInt<UTerm, B1> as Add<<UInt<UInt<UTerm, B1>, B0> as Mul<B>>::Output>>::Output: Sized, <UInt<UInt<UTerm, B1>, B0> as Mul<B>>::Output: Sized, A: Sync, K: Sync, V: Sync,

§

impl<K, V, B = UInt<UInt<UInt<UTerm, B1>, B1>, B0>, A = Global> !Unpin for CompressedBTreeMap<K, V, B, A>

§

impl<K, V, B = UInt<UInt<UInt<UTerm, B1>, B1>, B0>, A = Global> !UnwindSafe for CompressedBTreeMap<K, V, B, A>

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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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>,

Source§

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.