Skip to main content

FixedLenNibbleTrie

Struct FixedLenNibbleTrie 

Source
pub struct FixedLenNibbleTrie<T, PTR: TrieIndex = u32> {
    pub arena: Vec<FixedLenNode<PTR>>,
    pub buf: Vec<u8>,
    pub values: Vec<T>,
    pub lens: Vec<u16>,
    pub max_len: usize,
}
Expand description

A fixed-length nibble trie map.

Keys are stored in fixed-size slots of max_len bytes, zero-padded on the right. Key length is recovered by scanning backward from the slot boundary for the first nonzero byte. Keys must not contain trailing zero bytes — a key like b"a\0" would be retrieved as b"a".

The PTR type parameter controls the width of arena/key indices and thus the maximum number of entries (~PTR::max_value() - 1).

Fields§

§arena: Vec<FixedLenNode<PTR>>§buf: Vec<u8>§values: Vec<T>§lens: Vec<u16>§max_len: usize

Implementations§

Source§

impl<T, PTR: TrieIndex> FixedLenNibbleTrie<T, PTR>

Source

pub fn new(max_len: usize) -> Self

Create a new empty trie with the given maximum key length.

Keys longer than max_len will be rejected by insert.

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn key_slice(&self, ki: PTR) -> &[u8]

Return the actual (unpadded) key slice for key index ki. Uses the stored length for O(1) retrieval.

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>

Unchecked value lookup — assumes the key is present in the trie.

§Safety

The key must have been inserted into this trie. If the key is not present, the result is unspecified.

Source

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

Source

pub fn optimize(&mut self)

Rewrite buf and values so that keys appear in sorted order, with contiguous layout for sequential access during iteration.

After optimize(), a forward iteration visits keys in ascending memory order within buf. Leaf indices in arena nodes are remapped to reflect the new positions.

No-op for empty tries.

Source

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

Insert and auto-optimize on power-of-two sizes.

Source

pub fn iter(&self) -> FixedLenIter<'_, T, PTR>

Source

pub fn iter_last(&self) -> FixedLenIter<'_, T, PTR>

Source

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

Source

pub fn near_capacity(&self) -> bool

Trait Implementations§

Source§

impl<T: Clone, PTR: Clone + TrieIndex> Clone for FixedLenNibbleTrie<T, PTR>

Source§

fn clone(&self) -> FixedLenNibbleTrie<T, PTR>

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<T, PTR: TrieIndex> Default for FixedLenNibbleTrie<T, PTR>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T, PTR> Freeze for FixedLenNibbleTrie<T, PTR>

§

impl<T, PTR> RefUnwindSafe for FixedLenNibbleTrie<T, PTR>

§

impl<T, PTR> Send for FixedLenNibbleTrie<T, PTR>
where T: Send, PTR: Send,

§

impl<T, PTR> Sync for FixedLenNibbleTrie<T, PTR>
where T: Sync, PTR: Sync,

§

impl<T, PTR> Unpin for FixedLenNibbleTrie<T, PTR>
where T: Unpin, PTR: Unpin,

§

impl<T, PTR> UnsafeUnpin for FixedLenNibbleTrie<T, PTR>

§

impl<T, PTR> UnwindSafe for FixedLenNibbleTrie<T, PTR>
where T: UnwindSafe, PTR: 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.