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: usizeImplementations§
Source§impl<T, PTR: TrieIndex> FixedLenNibbleTrie<T, PTR>
impl<T, PTR: TrieIndex> FixedLenNibbleTrie<T, PTR>
Sourcepub fn new(max_len: usize) -> Self
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.
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
Sourcepub fn key_slice(&self, ki: PTR) -> &[u8] ⓘ
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.
pub fn get_index(&self, key: &[u8]) -> Option<usize>
pub fn get(&self, key: &[u8]) -> Option<&T>
pub fn get_mut(&mut self, key: &[u8]) -> Option<&mut T>
Sourcepub unsafe fn get_unchecked(&self, key: &[u8]) -> Option<&T>
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.
pub fn insert(&mut self, key: Vec<u8>, value: T) -> Result<usize, ()>
Sourcepub fn optimize(&mut self)
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.
Sourcepub fn insert_auto(&mut self, key: Vec<u8>, value: T) -> Result<usize, ()>
pub fn insert_auto(&mut self, key: Vec<u8>, value: T) -> Result<usize, ()>
Insert and auto-optimize on power-of-two sizes.