@@ -137,6 +137,7 @@ impl<T> Map<uint, T> for TrieMap<T> {
137137}
138138
139139impl < T > TrieMap < T > {
140+ /// Create an empty TrieMap
140141 #[ inline( always) ]
141142 static pure fn new( ) -> TrieMap <T > {
142143 TrieMap { root: TrieNode :: new( ) , length: 0 }
@@ -191,6 +192,12 @@ impl Mutable for TrieSet {
191192}
192193
193194impl TrieSet {
195+ /// Create an empty TrieSet
196+ #[ inline( always) ]
197+ static pure fn new( ) -> TrieSet {
198+ TrieSet { map: TrieMap :: new ( ) }
199+ }
200+
194201 /// Return true if the set contains a value
195202 #[ inline( always) ]
196203 pure fn contains ( & self , value : & uint ) -> bool {
@@ -265,8 +272,8 @@ impl<T> TrieNode<T> {
265272// if this was done via a trait, the key could be generic
266273#[ inline( always) ]
267274pure fn chunk ( n : uint , idx : uint ) -> uint {
268- let real_idx = uint:: bytes - 1 - idx;
269- ( n >> ( SHIFT * real_idx ) ) & MASK
275+ let sh = uint:: bits - ( SHIFT * ( idx + 1 ) ) ;
276+ ( n >> sh ) & MASK
270277}
271278
272279fn insert < T > ( count : & mut uint , child : & mut Child < T > , key : uint , value : T ,
@@ -462,4 +469,26 @@ mod tests {
462469 n -= 1 ;
463470 }
464471 }
472+
473+ #[ test]
474+ fn test_sane_chunk ( ) {
475+ let x = 1 ;
476+ let y = 1 << ( uint:: bits - 1 ) ;
477+
478+ let mut trie = TrieSet :: new ( ) ;
479+
480+ fail_unless ! ( trie. insert( x) ) ;
481+ fail_unless ! ( trie. insert( y) ) ;
482+
483+ fail_unless ! ( trie. len( ) == 2 ) ;
484+
485+ let expected = [ x, y] ;
486+
487+ let mut i = 0 ;
488+
489+ for trie. each |x| {
490+ fail_unless ! ( expected[ i] == * x) ;
491+ i += 1 ;
492+ }
493+ }
465494}
0 commit comments