Skip to content

Trie fixes #5408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/libcore/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ impl<T> Map<uint, T> for TrieMap<T> {
}

impl<T> TrieMap<T> {
/// Create an empty TrieMap
#[inline(always)]
static pure fn new() -> TrieMap<T> {
TrieMap{root: TrieNode::new(), length: 0}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -265,8 +272,8 @@ impl<T> TrieNode<T> {
// 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<T>(count: &mut uint, child: &mut Child<T>, key: uint, value: T,
Expand Down Expand Up @@ -462,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;
}
}
}