AllocatedBTreeMap

Struct AllocatedBTreeMap 

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

A compressed B-Tree map implementation using the allocated pattern.

This is the low-level “allocated” type that requires manual allocator passing. For most use cases, prefer the CompressedBTreeMap wrapper which owns its allocator and provides a safe, ergonomic API.

This implementation uses ~30% less memory than the naive implementation by using specialized node types:

  • Leaf nodes: Store only keys and values (no child pointers)
  • Interior nodes: Store keys, values, and child pointers

§Type Parameters

  • K: Key type, must be PartialOrd + Debug
  • V: Value type
  • B: Branching factor (defaults to U6 for 6-way tree). Controls the number of keys per node (2*B keys maximum).

§Examples

use allocated_btree::AllocatedCompressedBTreeMap;
use allocated::CountingAllocator;

let alloc = CountingAllocator::default();
let mut map = AllocatedCompressedBTreeMap::<u32, String>::new_in(&alloc)?;

unsafe {
    map.insert_in(&alloc, 1, "one".to_string())?;
    map.insert_in(&alloc, 2, "two".to_string())?;
}

assert_eq!(map.len(), 2);

Implementations§

Source§

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

Source

pub fn new_in<A: Allocator>(alloc: &A) -> DropGuardResult<Self, &A>

§Errors

Will return Err if the allocation fails.

Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

Source

pub fn len(&self) -> usize

Returns the number of elements in the map.

Source

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

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

Source

pub unsafe fn insert_in<A: Allocator>( &mut self, alloc: &A, 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.

§Safety

alloc MUST be the allocator used to allocate this object.

§Errors

Will return Err if the allocation fails.

Source

pub unsafe fn clear_in<A: Allocator>(&mut self, alloc: &A) -> AllocResult<()>

Clears the map, removing all elements.

§Safety

alloc MUST be the allocator used to allocate this object.

§Errors

Will return Err if the allocation fails.

Source

pub fn get<'s, Q>(&'s self, key: &Q) -> Option<&'s V>
where K: Borrow<Q> + PartialOrd, 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> + PartialOrd, 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> + PartialOrd, Q: PartialOrd + Debug,

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

Source

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

Returns an iterator over the key-value pairs of the map, in sorted order by key.

Source

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

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

Source

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

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

Source

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

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

Source

pub unsafe fn entry_in<'a, 's, A: Allocator>( &'s mut self, alloc: &'a A, key: K, ) -> Entry<'a, 's, A, K, V, B>

§Safety

alloc MUST be the allocator used to allocate this object.

Source

pub unsafe fn first_entry_in<'a, 's, A: Allocator>( &'s mut self, alloc: &'a A, ) -> Option<OccupiedEntry<'a, 's, A, K, V, B>>

§Safety

alloc MUST be the allocator used to allocate this object.

Source

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

Returns a reference to the first key-value pair in the map. The key in this pair is the minimum key in the map.

Trait Implementations§

Source§

impl<K: PartialOrd + Debug, V, B: ArrayLength> DropIn for AllocatedBTreeMap<K, V, B>
where U2: Mul<B>, Prod<U2, B>: ArrayLength, U1: Add<Prod<U2, B>>, Sum<U1, Prod<U2, B>>: ArrayLength,

Source§

unsafe fn drop_in<A: Allocator>(&mut self, alloc: &A)

§Safety

alloc must be the allocator used to allocate this object.

Source§

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

Source§

fn from_iter_in<T>(alloc: &'a A, iter: T) -> DropGuardResult<Self, &'a A>
where T: IntoIterator<Item = (K, V)>,

Create a container from elements of an iterator. Any memory that needs to be allocated will be allocated in alloc. The instance of Self MUST be drop_in using the same instance of the allocator.
Source§

impl<'s, K: PartialOrd + Debug, V, B: ArrayLength> IntoIterator for &'s AllocatedBTreeMap<K, V, B>
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
Source§

impl<'a, K: PartialOrd + Debug, V, B: ArrayLength, A: Allocator + 'a> IntoIteratorIn<'a, A> for AllocatedBTreeMap<K, V, B>
where U2: Mul<B>, Prod<U2, B>: ArrayLength, U1: Add<Prod<U2, B>>, Sum<U1, Prod<U2, B>>: ArrayLength,

Source§

type Item = (K, V)

Source§

type IntoIter = IntoIter<'a, K, V, B, A>

Source§

unsafe fn into_iter_in(self, alloc: &'a A) -> Self::IntoIter

Safety Read more
Source§

impl<K, V, B> RecursiveDropIn for AllocatedBTreeMap<K, V, B>
where K: PartialOrd + Debug + DropIn, V: DropIn, B: ArrayLength, U2: Mul<B>, Prod<U2, B>: ArrayLength, U1: Add<Prod<U2, B>>, Sum<U1, Prod<U2, B>>: ArrayLength,

Source§

unsafe fn recursive_drop_in<A: Allocator>(&mut self, alloc: &A)

§Safety

alloc must be the allocator used to allocate this object.

Auto Trait Implementations§

§

impl<K, V, B = UInt<UInt<UInt<UTerm, B1>, B1>, B0>> !Freeze for AllocatedBTreeMap<K, V, B>

§

impl<K, V, B = UInt<UInt<UInt<UTerm, B1>, B1>, B0>> !RefUnwindSafe for AllocatedBTreeMap<K, V, B>

§

impl<K, V, B> Send for AllocatedBTreeMap<K, V, B>
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, K: Send, V: Send,

§

impl<K, V, B> Sync for AllocatedBTreeMap<K, V, B>
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, K: Sync, V: Sync,

§

impl<K, V, B = UInt<UInt<UInt<UTerm, B1>, B1>, B0>> !Unpin for AllocatedBTreeMap<K, V, B>

§

impl<K, V, B = UInt<UInt<UInt<UTerm, B1>, B1>, B0>> !UnwindSafe for AllocatedBTreeMap<K, V, B>

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.