Skip to content

Commit e3f5ce4

Browse files
Merge pull request michaelsproul#37 from vks/prefix
Make it possible to get the prefix of a node
2 parents d6a145a + 48b0d7c commit e3f5ce4

8 files changed

+63
-32
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ Available on [Crates.io][] as [`radix_trie`][radix-crate].
2727
Just add `radix_trie` to the dependencies section of your `Cargo.toml`, like so:
2828

2929
```toml
30-
[dependencies]
31-
radix_trie = "*"
30+
radix_trie = "0.1"
3231
```
3332

3433
# Contributors

src/iter.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,24 @@ impl<'a, K, V> Iterator for Children<'a, K, V> {
100100
self.inner.next().map(|node| {
101101
SubTrie {
102102
prefix: self.prefix.clone().join(&node.key),
103-
node: &node,
103+
node: node,
104104
}
105105
})
106106
}
107107
}
108108

109109
impl<K, V> TrieNode<K, V> {
110110
/// Helper function to get all the non-empty children of a node.
111-
fn child_iter<'a>(&'a self) -> ChildIter<'a, K, V> {
112-
fn id<'b, K, V>(x: &'b Option<Child<K, V>>) -> Option<&'b Child<K, V>> {
111+
fn child_iter(&self) -> ChildIter<K, V> {
112+
fn id<K, V>(x: &Option<Child<K, V>>) -> Option<&Child<K, V>> {
113113
x.as_ref()
114114
}
115115

116116
self.children.iter().filter_map(id)
117117
}
118118

119119
/// Get the key and value of a node as a pair.
120-
fn kv_as_pair<'a>(&'a self) -> Option<(&'a K, &'a V)> {
120+
fn kv_as_pair(&self) -> Option<(&K, &V)> {
121121
self.key_value.as_ref().map(|kv| (&kv.key, &kv.value))
122122
}
123123
}
@@ -146,7 +146,7 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> {
146146
let action = match self.stack.last_mut() {
147147
Some(stack_top) => {
148148
match stack_top.next() {
149-
Some(child) => Push(&child),
149+
Some(child) => Push(child),
150150
None => Pop,
151151
}
152152
}

src/keys.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub fn check_keys<K: ?Sized>(key1: &K, key2: &K)
7272
}
7373
}
7474

75-
/// --- TrieKey Implementations for standard types --- ///
75+
// --- TrieKey Implementations for standard types --- ///
7676

7777
// This blanket implementation goes into play when specialization is stabilized
7878
// impl<T> TrieKey for T where T: Into<Vec<u8>> + Clone + Eq + PartialEq {
@@ -90,7 +90,7 @@ impl TrieKey for Vec<u8> {
9090

9191
impl TrieKey for [u8] {
9292
fn encode_bytes(&self) -> Vec<u8> {
93-
self.clone().to_vec()
93+
self.to_vec()
9494
}
9595
}
9696

@@ -122,15 +122,15 @@ impl TrieKey for i8 {
122122
fn encode_bytes(&self) -> Vec<u8> {
123123
let mut v: Vec<u8> = Vec::with_capacity(1);
124124
v.push(*self as u8);
125-
return v;
125+
v
126126
}
127127
}
128128

129129
impl TrieKey for u8 {
130130
fn encode_bytes(&self) -> Vec<u8> {
131131
let mut v: Vec<u8> = Vec::with_capacity(1);
132132
v.push(*self);
133-
return v;
133+
v
134134
}
135135
}
136136

src/test.rs

+17
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,20 @@ fn test_get_raw_descendant_borrow() {
423423
let subtrie = trie.get_raw_descendant("/boot/lo").unwrap();
424424
assert_eq!(*subtrie.value().unwrap(), "lol");
425425
}
426+
427+
#[test]
428+
fn test_prefix() {
429+
let mut t = Trie::<u8, ()>::new();
430+
t.insert(0xf1, ());
431+
t.insert(0xf2, ());
432+
println!("{:#?}", t);
433+
assert_eq!(t.prefix(), [].as_ref());
434+
let first = t.children().next().unwrap();
435+
assert_eq!(first.prefix(), [0xf].as_ref());
436+
let mut c = first.children();
437+
let second = c.next().unwrap();
438+
let third = c.next().unwrap();
439+
assert!(c.next().is_none());
440+
assert_eq!(second.prefix(), [0x1].as_ref());
441+
assert_eq!(third.prefix(), [0x2].as_ref());
442+
}

src/traversal.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ macro_rules! get_func {
8181
get_func!(name: iterative_get, trie_type: &'a TrieNode<K, V>, mutability: );
8282
get_func!(name: iterative_get_mut, trie_type: &'a mut TrieNode<K, V>, mutability: mut);
8383

84-
fn iterative_insert<'a, K, V>(trie: &'a mut TrieNode<K, V>,
85-
key: K,
86-
value: V,
87-
mut nv: NibbleVec)
88-
-> Option<V>
84+
fn iterative_insert<K, V>(trie: &mut TrieNode<K, V>,
85+
key: K,
86+
value: V,
87+
mut nv: NibbleVec)
88+
-> Option<V>
8989
where K: TrieKey
9090
{
9191
if nv.len() == 0 {
@@ -255,11 +255,11 @@ fn get_ancestor<'a, K, V>(trie: &'a TrieNode<K, V>,
255255
let bucket = nv.get(depth) as usize;
256256
let current = prev;
257257
if let Some(ref child) = current.children[bucket] {
258-
match match_keys(depth, &nv, &child.key) {
258+
match match_keys(depth, nv, &child.key) {
259259
KeyMatch::Full => {
260260
return child.as_value_node()
261261
.map(|node| (node, depth + node.key.len()))
262-
.or(ancestor.map(|anc| (anc, depth)));
262+
.or_else(|| ancestor.map(|anc| (anc, depth)));
263263
}
264264
KeyMatch::FirstPrefix |
265265
KeyMatch::Partial(_) => {
@@ -296,7 +296,7 @@ fn get_raw_ancestor<'a, K, V>(trie: &'a TrieNode<K, V>,
296296
let bucket = nv.get(depth) as usize;
297297
let current = prev;
298298
if let Some(ref child) = current.children[bucket] {
299-
match match_keys(depth, &nv, &child.key) {
299+
match match_keys(depth, nv, &child.key) {
300300
KeyMatch::Full => {
301301
return (child, depth + child.key.len());
302302
}
@@ -337,7 +337,7 @@ fn get_raw_descendant<'a, K, V>(trie: &'a TrieNode<K, V>,
337337
let bucket = nv.get(depth) as usize;
338338
let current = prev;
339339
if let Some(ref child) = current.children[bucket] {
340-
match match_keys(depth, &nv, &child.key) {
340+
match match_keys(depth, nv, &child.key) {
341341
KeyMatch::Full => {
342342
return Some(NoModification(child));
343343
}

src/trie.rs

+6
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,9 @@ impl<K, V> PartialEq for Trie<K, V> where K: TrieKey, V: PartialEq {
180180
)
181181
}
182182
}
183+
184+
impl<K: TrieKey, V> Default for Trie<K, V> {
185+
fn default() -> Self {
186+
Self::new()
187+
}
188+
}

src/trie_common.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use {Trie, TrieKey, SubTrie, SubTrieMut};
1+
use {NibbleVec, Trie, TrieKey, SubTrie, SubTrieMut};
22
use trie_node::TrieNode;
33
use iter::*;
44

@@ -47,6 +47,11 @@ pub trait TrieCommon<'a, K: 'a, V: 'a>: ContainsTrieNode<'a, K, V>
4747

4848
/// Return an iterator over the child subtries of this node.
4949
fn children(self) -> Children<'a, K, V>;
50+
51+
/// Get the prefix of this node.
52+
fn prefix(self) -> &'a NibbleVec {
53+
&self.trie_node().key
54+
}
5055
}
5156

5257
/// Helper trait for Trie/SubTrie/SubTrieMut, which all contain a trie node.

src/trie_node.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::borrow::Borrow;
2+
use std::default::Default;
23
use {SubTrie, SubTrieMut, NibbleVec, BRANCH_FACTOR};
34
use keys::*;
45

@@ -104,7 +105,7 @@ impl<K, V> TrieNode<K, V>
104105
let mut size = if self.key_value.is_some() { 1 } else { 0 };
105106

106107
for child in &self.children {
107-
if let &Some(ref child) = child {
108+
if let Some(ref child) = *child {
108109
// TODO: could unroll this recursion
109110
size += child.compute_size();
110111
}
@@ -130,7 +131,7 @@ impl<K, V> TrieNode<K, V>
130131

131132
/// Helper function for removing the single child of a node.
132133
pub fn take_only_child(&mut self) -> Box<TrieNode<K, V>> {
133-
debug_assert!(self.child_count == 1);
134+
debug_assert_eq!(self.child_count, 1);
134135
for i in 0..BRANCH_FACTOR {
135136
if let Some(child) = self.take_child(i) {
136137
return child;
@@ -206,7 +207,7 @@ impl<K, V> TrieNode<K, V>
206207
}));
207208
}
208209

209-
pub fn as_subtrie<'a>(&'a self, prefix: NibbleVec) -> SubTrie<'a, K, V> {
210+
pub fn as_subtrie(&self, prefix: NibbleVec) -> SubTrie<K, V> {
210211
SubTrie {
211212
prefix: prefix,
212213
node: self
@@ -256,17 +257,14 @@ impl<K, V> TrieNode<K, V>
256257
let trie_key = prefix.clone().join(&self.key);
257258

258259
// Account for this node in the size check, and check its key.
259-
match self.key_value {
260-
Some(ref kv) => {
261-
sub_tree_size += 1;
260+
if let Some(ref kv) = self.key_value {
261+
sub_tree_size += 1;
262262

263-
let actual_key = kv.key.encode();
263+
let actual_key = kv.key.encode();
264264

265-
if trie_key != actual_key {
266-
return (false, sub_tree_size);
267-
}
265+
if trie_key != actual_key {
266+
return (false, sub_tree_size);
268267
}
269-
None => (),
270268
}
271269

272270
// Recursively check children.
@@ -282,3 +280,9 @@ impl<K, V> TrieNode<K, V>
282280
(true, sub_tree_size)
283281
}
284282
}
283+
284+
impl<K: TrieKey, V> Default for TrieNode<K, V> {
285+
fn default() -> Self {
286+
Self::new()
287+
}
288+
}

0 commit comments

Comments
 (0)