Skip to main content

DynTrie

Struct DynTrie 

Source
pub struct DynTrie<T> { /* private fields */ }
Expand description

A nibble trie that starts with compact u8 arena indices and automatically promotes to u16/u32/u64 as needed.

Node sizes by PTR width:

  • u8: 32 bytes (fits half a cache line)
  • u16: 48 bytes (fits a cache line)
  • u32: 80 bytes
  • u64: 152 bytes

Promotion is transparent: insert checks capacity and promotes before the underlying trie overflows. No vtable dispatch or heap allocation on promotion — just an enum variant swap.

Implementations§

Source§

impl<T> DynTrie<T>

Source

pub fn new() -> Self

Create an empty DynTrie starting with u8 arena indices (32-byte nodes).

Source

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

Look up a key. Returns the value if found.

Source

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

Look up a key’s value for mutation. Returns the value if found.

Source

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

Insert a key-value pair. Automatically promotes to a wider PTR type if the current one is approaching capacity. Returns the key index on success, or Err(()) on duplicate key.

Source

pub fn len(&self) -> usize

Number of entries in the trie.

Source

pub fn is_empty(&self) -> bool

Returns true if the trie is empty.

Source

pub fn optimize(&mut self)

Optimize the trie’s memory layout for cache locality.

Source

pub fn ptr_size(&self) -> usize

Returns the size of the current PTR type in bytes (1, 2, 4, or 8). Useful for debugging and testing promotion.

Source

pub fn iter_fwd(&self, f: &mut dyn FnMut(&[u8], &T))

Iterate all key-value pairs in forward (ascending) order.

Source

pub fn iter_rev(&self, f: &mut dyn FnMut(&[u8], &T))

Iterate all key-value pairs in reverse (descending) order.

Source

pub fn demote(&mut self) -> Result<(), ()>

Manually demote to a narrower PTR type if the trie is small enough. Returns Ok(()) on success, Err(()) if the trie is too large or already at the minimum width (u8).

Trait Implementations§

Source§

impl<T> Default for DynTrie<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> Freeze for DynTrie<T>

§

impl<T> RefUnwindSafe for DynTrie<T>
where T: RefUnwindSafe,

§

impl<T> Send for DynTrie<T>
where T: Send,

§

impl<T> Sync for DynTrie<T>
where T: Sync,

§

impl<T> Unpin for DynTrie<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for DynTrie<T>

§

impl<T> UnwindSafe for DynTrie<T>
where 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> 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.