@@ -99,15 +99,18 @@ impl<T: Clone> Map<String, T> for PatriciaTrie<T> {
99
99
match * self {
100
100
Tip => panic ! ( "lookup on empty tree." ) ,
101
101
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
+ }
111
114
}
112
115
} else {
113
116
panic ! ( "element does not exist" )
@@ -167,6 +170,7 @@ fn patricia_trie() {
167
170
. bind ( "toast" . to_string ( ) , 6 )
168
171
. bind ( "toad" . to_string ( ) , 7 ) ;
169
172
173
+ println ! ( "{}" , t2) ;
170
174
assert_eq ! ( t2. lookup( "test" . to_string( ) ) , 0 ) ;
171
175
assert_eq ! ( t2. lookup( "slow" . to_string( ) ) , 1 ) ;
172
176
assert_eq ! ( t2. lookup( "water" . to_string( ) ) , 2 ) ;
0 commit comments