pub struct PackedMapBuilder<K, V>{ /* 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:
packed: APackedMapcontaining elements that are already sorted and packed.unpacked: ABTreeMapserving as a buffer for new, potentially out-of-order insertions.
§Operations
- Insertion:
- We first attempt to insert the key-value pair directly into the
packedmap.PackedMap::insertsucceeds 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 takesO(log N)orO(1)time. - If the key is out of order, the
packedmap rejects it. We then insert it into theunpackedbuffer (BTreeMap).
- We first attempt to insert the key-value pair directly into the
- Lookup: To find a key, we check both the
packedmap and theunpackedbuffer. - Build: When
build()is called, any remaining elements in theunpackedbuffer are merged with thepackedmap to produce the finalPackedMap.
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>
impl<K, V> PackedMapBuilder<K, V>
Sourcepub fn with_capacity(element_capacity: usize, buffer_capacity: usize) -> Self
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.
Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Returns true if the builder contains the given key.
Sourcepub fn get(&self, key: &K) -> Option<&V>
pub fn get(&self, key: &K) -> Option<&V>
Returns a reference to the value corresponding to the key.