Skip to main content

PackedMapBuilder

Struct PackedMapBuilder 

Source
pub struct PackedMapBuilder<K, V>
where K: ToOwned + ?Sized + Ord + PackedItem, K::Owned: Ord,
{ /* private fields */ }
Expand description

A builder for PackedMap that allows incrementally appending potentially unsorted key-value pairs.

§Algorithm

PackedMapBuilder uses an LSM-tree (Log-Structured Merge-tree) inspired approach to handle out-of-order insertions efficiently while maintaining a compact, immutable representation for the final map.

It maintains two structures:

  1. packed: A PackedMap containing elements that are already sorted and packed.
  2. unpacked: A BTreeMap serving as a buffer for new, potentially out-of-order insertions.

§Operations

  • Insertion:
    • We first attempt to insert the key-value pair directly into the packed map. PackedMap::insert succeeds if the key already exists (overwriting it) or if the key is greater than or equal to the last key in the map (maintaining sorted order). This fast path takes O(log N) or O(1) time.
    • If the key is out of order, the packed map rejects it. We then insert it into the unpacked buffer (BTreeMap).
  • Lookup: To find a key, we check both the packed map and the unpacked buffer.
  • Build: When build() is called, any remaining elements in the unpacked buffer are merged with the packed map to produce the final PackedMap.

This design optimizes for the common case where data is mostly sorted, while still supporting full unpacked insertion without severe performance degradation.

Implementations§

Source§

impl<K, V> PackedMapBuilder<K, V>
where K: ?Sized + Ord + PackedItem + ToOwned, K::Owned: Ord,

Source

pub fn new() -> Self

Creates a new empty PackedMapBuilder.

Source

pub fn with_capacity(element_capacity: usize, buffer_capacity: usize) -> Self

Creates a new PackedMapBuilder with specified capacity.

The element_capacity argument specifies the number of slices that can be stored without reallocating the offsets vector. The buffer_capacity argument specifies the cumulative length of slices that can be stored without reallocating the data vector.

Source

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

Returns true if the builder contains the given key.

Source

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

Returns a reference to the value corresponding to the key.

Source

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

Inserts a key-value pair into the builder.

§Complexity
  • O(1) if the key is greater than or equal to the last key in the map.
  • O(log N) otherwise, where N is the number of elements in the map.
Source

pub fn build(self) -> PackedMap<K, V>

Builds the PackedMap.

Trait Implementations§

Source§

impl<K, V> Default for PackedMapBuilder<K, V>
where K: ?Sized + Ord + PackedItem + ToOwned, K::Owned: Ord,

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<K, V> Freeze for PackedMapBuilder<K, V>
where K: ?Sized,

§

impl<K, V> RefUnwindSafe for PackedMapBuilder<K, V>
where V: RefUnwindSafe, <K as ToOwned>::Owned: RefUnwindSafe, K: ?Sized,

§

impl<K, V> Send for PackedMapBuilder<K, V>
where <K as ToOwned>::Owned: Send, V: Send, K: ?Sized,

§

impl<K, V> Sync for PackedMapBuilder<K, V>
where <K as ToOwned>::Owned: Sync, V: Sync, K: ?Sized,

§

impl<K, V> Unpin for PackedMapBuilder<K, V>
where V: Unpin, K: ?Sized,

§

impl<K, V> UnsafeUnpin for PackedMapBuilder<K, V>
where K: ?Sized,

§

impl<K, V> UnwindSafe for PackedMapBuilder<K, V>

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

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.