From 98900d55e7f08a4b4cb665152a01b0bbf655baae Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Fri, 15 Mar 2013 21:03:29 -0400 Subject: [PATCH 1/3] add constructor to TrieSet (was missing) --- src/libcore/trie.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/libcore/trie.rs b/src/libcore/trie.rs index 966db4ec66207..d6dcad334085d 100644 --- a/src/libcore/trie.rs +++ b/src/libcore/trie.rs @@ -137,6 +137,7 @@ impl Map for TrieMap { } impl TrieMap { + /// Create an empty TrieMap #[inline(always)] static pure fn new() -> TrieMap { TrieMap{root: TrieNode::new(), length: 0} @@ -191,6 +192,12 @@ impl Mutable for TrieSet { } impl TrieSet { + /// Create an empty TrieSet + #[inline(always)] + static pure fn new() -> TrieSet { + TrieSet{map: TrieMap::new()} + } + /// Return true if the set contains a value #[inline(always)] pure fn contains(&self, value: &uint) -> bool { From 0942c802725b4c2816f2232304cdd14b3e44cd3f Mon Sep 17 00:00:00 2001 From: Graydon Hoare Date: Tue, 5 Mar 2013 20:54:19 -0800 Subject: [PATCH 2/3] core: fix trie chunk function --- src/libcore/trie.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/trie.rs b/src/libcore/trie.rs index d6dcad334085d..cf34ba774a85f 100644 --- a/src/libcore/trie.rs +++ b/src/libcore/trie.rs @@ -272,8 +272,8 @@ impl TrieNode { // if this was done via a trait, the key could be generic #[inline(always)] pure fn chunk(n: uint, idx: uint) -> uint { - let real_idx = uint::bytes - 1 - idx; - (n >> (SHIFT * real_idx)) & MASK + let sh = uint::bits - (SHIFT * (idx + 1)); + (n >> sh) & MASK } fn insert(count: &mut uint, child: &mut Child, key: uint, value: T, From d856215b925441a4889cb23cab5758c36e4a4746 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Fri, 15 Mar 2013 21:43:54 -0400 Subject: [PATCH 3/3] add a test for the chunk function --- src/libcore/trie.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/libcore/trie.rs b/src/libcore/trie.rs index cf34ba774a85f..15b0e16043431 100644 --- a/src/libcore/trie.rs +++ b/src/libcore/trie.rs @@ -469,4 +469,26 @@ mod tests { n -= 1; } } + + #[test] + fn test_sane_chunk() { + let x = 1; + let y = 1 << (uint::bits - 1); + + let mut trie = TrieSet::new(); + + fail_unless!(trie.insert(x)); + fail_unless!(trie.insert(y)); + + fail_unless!(trie.len() == 2); + + let expected = [x, y]; + + let mut i = 0; + + for trie.each |x| { + fail_unless!(expected[i] == *x); + i += 1; + } + } }