Skip to content

Commit ccf43b5

Browse files
committed
Fix lookup.
1 parent d0b846f commit ccf43b5

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/trie.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,18 @@ impl<T: Clone> Map<String, T> for PatriciaTrie<T> {
9999
match *self {
100100
Tip => panic!("lookup on empty tree."),
101101
Node { ref key, ref value, ref children } => {
102-
if k == *key {
103-
match *value {
104-
Some(ref v) => v.clone(),
105-
None => panic!("element does not exist"),
106-
}
107-
} else if k.starts_with(key) {
108-
match children.get(&k.chars().last().unwrap()) {
109-
Some(t) => t.lookup(k[key.len()..].to_string()),
110-
None => panic!("element does not exist"),
102+
if k.starts_with(key) {
103+
let k2 = k[key.len()..].to_string();
104+
if k2 == "" {
105+
match *value {
106+
Some(ref v) => v.clone(),
107+
None => panic!("element does not exist"),
108+
}
109+
} else {
110+
match children.get(&first_char_unwrap(&k2)) {
111+
Some(t) => t.lookup(k2),
112+
None => panic!("element does not exist"),
113+
}
111114
}
112115
} else {
113116
panic!("element does not exist")
@@ -167,6 +170,7 @@ fn patricia_trie() {
167170
.bind("toast".to_string(), 6)
168171
.bind("toad".to_string(), 7);
169172

173+
println!("{}", t2);
170174
assert_eq!(t2.lookup("test".to_string()), 0);
171175
assert_eq!(t2.lookup("slow".to_string()), 1);
172176
assert_eq!(t2.lookup("water".to_string()), 2);

0 commit comments

Comments
 (0)