Skip to main content

NibbleTrie

Struct NibbleTrie 

Source
pub struct NibbleTrie<K, T, PTR: TrieIndex = u32, LEN: TrieIndex = u16>
where K: ByteKey,
{ /* private fields */ }

Implementations§

Source§

impl<K: ByteKey, T, PTR: TrieIndex, LEN: TrieIndex> NibbleTrie<K, T, PTR, LEN>

Source

pub fn new() -> Self

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn get_index(&self, key: &[u8]) -> Option<usize>

Source

pub fn get(&self, key: &[u8]) -> Option<&T>

Source

pub fn get_mut(&mut self, key: &[u8]) -> Option<&mut T>

Source

pub unsafe fn get_unchecked(&self, key: &[u8]) -> Option<&T>

if the key is guaranteed to be in the set, the final comparison can be skipped, improving perf substantially.

Source

pub fn iter(&self) -> Cursor<'_, K, T, PTR, LEN>

Public forward cursor: parked before the first key (so current() is None and next() yields the first key). A linear scan over the sparse index, skipping None gaps.

Source

pub fn iter_last(&self) -> Cursor<'_, K, T, PTR, LEN>

Public reverse cursor: parked on the last key (current() returns it, prev() walks backward). Linear scan over index.

Source

pub fn iter_mut(&mut self) -> CursorMut<'_, K, T, PTR, LEN>

Public forward mutable cursor: parked before the first key, lending out &mut T borrows tied to the cursor (see CursorMut).

Source

pub fn iter_mut_last(&mut self) -> CursorMut<'_, K, T, PTR, LEN>

Public reverse mutable cursor: parked on the last key, lending out &mut T borrows tied to the cursor (see CursorMut).

Source

pub fn range<'q>( &self, bounds: impl RangeBounds<&'q [u8]>, ) -> Range<'_, K, T, PTR, LEN>

Iterate the keys in bounds in ascending order — a zero-allocation Range yielding (K::Borrowed<'_>, &T). Both bounds are resolved by O(keylen) seeks up front; the scan between them is then bounded by slot index (pos < end_pos), so no per-element key comparison is needed. Accepts any [RangeBounds<&[u8]>]: start..end, start.., ..end, .. (operands are &[u8]). The bounds’ byte slices are used only during the initial seeks and need not outlive the call.

Source

pub fn range_bounds( &self, start: Bound<&[u8]>, end: Bound<&[u8]>, ) -> Range<'_, K, T, PTR, LEN>

Like range but with explicit Bounds — for mixed Included/Excluded bounds without the &&[u8] double-reference the RangeBounds tuple form would require.

Source

pub fn into_keys_values(self) -> (Vec<K>, Vec<T>)

Source

pub fn near_capacity(&self) -> bool

Source

pub fn optimize(&mut self)

Rewrite buf in DFS (key-sorted) order and re-spread index into a sparse layout: a fresh vec of capacity 2*n+1 with each key placed at slot 2*i+1 (DFS rank i), leaving even slots as None gaps. Forward iteration then hits buf in ascending memory order, and the gaps give future inserts room to shift into without re-sorting.

Also (re)establishes the leftmost-leaf invariant: every node’s leaf is set to the key index of the leftmost key in its subtree. The arena topology (child structure) is unchanged — only key indices are remapped. Idempotent.

Source

pub fn flatten(&mut self)

Flatten small multi-Inode subtrees into single [FlatNode]s.

Rebuilds the arena top-down: any non-root subtree with ≤ [FNODE_CAP] keys and ≥ 2 Inodes (so collapsing it actually saves memory) is replaced by one Fnode built from a pre-order DFS of that subtree. The root stays an Inode. This is an arena-only rebuild — key indices (leaf children, each Inode’s leaf, and Fnode base/offsets) are unchanged; only internal-child arena indices are remapped. So index/buf are untouched and the existing get/iter/seek paths work unchanged.

Idempotent: an Fnode holds no arena refs, so a subtree already containing an Fnode can’t be re-flattened (the Fnode is copied verbatim and [build_fnode_subtree] rejects Fnode children). Calling flatten on an already-flat trie is a no-op topology copy.

Best called after optimize: the 2i+1 respread makes a ≤16-key subtree span exactly ≤32 index slots, so every offset fits in u8 with room (≤ 30 ≪ 0xFF), and offsets come out canonical even values 0, 2, 4, ….

Source§

impl<K: ByteKey, T, PTR: TrieIndex, LEN: TrieIndex> NibbleTrie<K, T, PTR, LEN>

Source

pub fn insert(&mut self, key: K, value: T) -> Result<usize, ()>

Source§

impl<K: ByteKey, T, PTR: TrieIndex, LEN: TrieIndex> NibbleTrie<K, T, PTR, LEN>

Source

pub fn promote<NewPTR: TrieIndex>(self) -> NibbleTrie<K, T, NewPTR, LEN>

Promote the arena index type to a wider PTR. All child indices and leaf key indices are widened via NewPTR::from_usize.

Source

pub fn demote<NewPTR: TrieIndex>( self, ) -> Result<NibbleTrie<K, T, NewPTR, LEN>, Self>

Demote the arena index type to a narrower PTR. Returns Err(self) if any index doesn’t fit in the narrower type.

Trait Implementations§

Source§

impl<K, T: Clone, PTR: Clone + TrieIndex, LEN: Clone + TrieIndex> Clone for NibbleTrie<K, T, PTR, LEN>
where K: ByteKey + Clone,

Source§

fn clone(&self) -> NibbleTrie<K, T, PTR, LEN>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<K: ByteKey, T, PTR: TrieIndex, LEN: TrieIndex> Default for NibbleTrie<K, T, PTR, LEN>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<K, T, PTR, LEN> Freeze for NibbleTrie<K, T, PTR, LEN>

§

impl<K, T, PTR, LEN> RefUnwindSafe for NibbleTrie<K, T, PTR, LEN>

§

impl<K, T, PTR, LEN> Send for NibbleTrie<K, T, PTR, LEN>
where K: Send, LEN: Send, PTR: Send, T: Send,

§

impl<K, T, PTR, LEN> Sync for NibbleTrie<K, T, PTR, LEN>
where K: Sync, LEN: Sync, PTR: Sync, T: Sync,

§

impl<K, T, PTR, LEN> Unpin for NibbleTrie<K, T, PTR, LEN>
where K: Unpin, LEN: Unpin, PTR: Unpin, T: Unpin,

§

impl<K, T, PTR, LEN> UnsafeUnpin for NibbleTrie<K, T, PTR, LEN>

§

impl<K, T, PTR, LEN> UnwindSafe for NibbleTrie<K, T, PTR, LEN>
where K: UnwindSafe, LEN: UnwindSafe, PTR: UnwindSafe, T: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

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