@@ -76,6 +76,16 @@ impl<K: Borrow<[u8]>, V> Branch<K, V> {
7676 self . entries . contains ( index)
7777 }
7878
79+ /// Corresponds to the key-value pair at this position.
80+ #[ inline]
81+ pub fn head_entry ( & self ) -> Option < & Leaf < K , V > > {
82+ match self . entries . get ( 0 ) {
83+ Some ( Node :: Leaf ( leaf) ) => Some ( leaf) ,
84+ None => None ,
85+ _ => unsafe { debug_unreachable ! ( ) } ,
86+ }
87+ }
88+
7989 #[ inline]
8090 pub fn entry_mut ( & mut self , index : u8 ) -> & mut Node < K , V > {
8191 let entry = self . entries . get_mut ( index) ;
@@ -89,6 +99,13 @@ impl<K: Borrow<[u8]>, V> Branch<K, V> {
8999 self . entries . get ( nybble_index ( self . choice , key) )
90100 }
91101
102+ // Get the child node corresponding to the given key.
103+ #[ inline]
104+ pub fn child_with_offsetted_key ( & self , key : & [ u8 ] , key_offset : usize ) -> Option < & Node < K , V > > {
105+ self . entries
106+ . get ( nybble_index ( self . choice . checked_sub ( key_offset * 2 ) ?, key) )
107+ }
108+
92109 // Mutable version of `Branch::child`.
93110 #[ inline]
94111 pub fn child_mut ( & mut self , key : & [ u8 ] ) -> Option < & mut Node < K , V > > {
@@ -119,6 +136,18 @@ impl<K: Borrow<[u8]>, V> Branch<K, V> {
119136 self . entries . get_or_any ( nybble_index ( self . choice , key) )
120137 }
121138
139+ // Retrieve the node which contains the exemplar. This does not recurse and return the actual
140+ // exemplar - just the node which might be or contain it.
141+ #[ inline]
142+ pub fn exemplar_with_offset ( & self , key : & [ u8 ] , key_offset : usize ) -> & Node < K , V > {
143+ self . entries . get_or_any (
144+ self . choice
145+ . checked_sub ( key_offset * 2 )
146+ . map ( |choice| nybble_index ( choice, key) )
147+ . unwrap_or_default ( ) ,
148+ )
149+ }
150+
122151 // As `Branch::exemplar` but for mutable borrows.
123152 #[ inline]
124153 pub fn exemplar_mut ( & mut self , key : & [ u8 ] ) -> & mut Node < K , V > {
@@ -132,6 +161,12 @@ impl<K: Borrow<[u8]>, V> Branch<K, V> {
132161 self . exemplar ( key) . get_exemplar ( key)
133162 }
134163
164+ #[ inline]
165+ pub fn get_exemplar_with_offset ( & self , key : & [ u8 ] , key_offset : usize ) -> & Leaf < K , V > {
166+ self . exemplar_with_offset ( key, key_offset)
167+ . get_exemplar_with_offset ( key, key_offset)
168+ }
169+
135170 // Mutably borrow the exemplar for the given key, mutually recursing through
136171 // `Node::get_exemplar_mut`.
137172 #[ inline]
@@ -307,6 +342,13 @@ impl<K: Borrow<[u8]>, V> Node<K, V> {
307342 }
308343 }
309344
345+ pub fn get_exemplar_with_offset ( & self , key : & [ u8 ] , key_offset : usize ) -> & Leaf < K , V > {
346+ match * self {
347+ Node :: Leaf ( ref leaf) => leaf,
348+ Node :: Branch ( ref branch) => branch. get_exemplar_with_offset ( key, key_offset) ,
349+ }
350+ }
351+
310352 // Mutably borrow the exemplar for a given key.
311353 pub fn get_exemplar_mut ( & mut self , key : & [ u8 ] ) -> & mut Leaf < K , V > {
312354 match * self {
@@ -320,19 +362,23 @@ impl<K: Borrow<[u8]>, V> Node<K, V> {
320362 //
321363 // PRECONDITION:
322364 // - There exists at least one node in the trie with the given prefix.
323- pub fn get_prefix_validated < ' a > ( & ' a self , prefix : & [ u8 ] ) -> & ' a Node < K , V > {
365+ pub fn get_prefix_validated < ' a > (
366+ & ' a self ,
367+ prefix : & [ u8 ] ,
368+ prefix_offset : usize ,
369+ ) -> & ' a Node < K , V > {
324370 match * self {
325371 Node :: Leaf ( ..) => self ,
326372 Node :: Branch ( ref branch) => {
327- if branch. choice >= prefix. len ( ) * 2 {
373+ if branch. choice >= ( prefix. len ( ) + prefix_offset ) * 2 {
328374 self
329375 } else {
330- let child_opt = branch. child ( prefix) ;
376+ let child_opt = branch. child_with_offsetted_key ( prefix, prefix_offset ) ;
331377
332378 // unsafe: child must exist in the trie - prefix'd nodes must exist.
333379 let child = unsafe { child_opt. unwrap_unchecked ( ) } ;
334380
335- child. get_prefix_validated ( prefix)
381+ child. get_prefix_validated ( prefix, prefix_offset )
336382 }
337383 }
338384 }
@@ -346,7 +392,31 @@ impl<K: Borrow<[u8]>, V> Node<K, V> {
346392 Node :: Branch ( ref branch)
347393 if branch. get_exemplar ( prefix) . key_slice ( ) . starts_with ( prefix) =>
348394 {
349- Some ( self . get_prefix_validated ( prefix) )
395+ Some ( self . get_prefix_validated ( prefix, 0 ) )
396+ }
397+
398+ _ => None ,
399+ }
400+ }
401+
402+ // Borrow the node which contains all and only entries with keys continuing with
403+ // `prefix`.
404+ pub fn get_prefix_with_offset < ' a > (
405+ & ' a self ,
406+ prefix : & [ u8 ] ,
407+ prefix_offset : usize ,
408+ ) -> Option < & ' a Node < K , V > > {
409+ match * self {
410+ Node :: Leaf ( ref leaf) if leaf. key_slice ( ) [ prefix_offset..] . starts_with ( prefix) => {
411+ Some ( self )
412+ }
413+ Node :: Branch ( ref branch)
414+ if branch
415+ . get_exemplar_with_offset ( prefix, prefix_offset)
416+ . key_slice ( ) [ prefix_offset..]
417+ . starts_with ( prefix) =>
418+ {
419+ Some ( self . get_prefix_validated ( prefix, prefix_offset) )
350420 }
351421
352422 _ => None ,
0 commit comments