Skip to content

Commit

Permalink
Derive Hash for TrieMap and TrieSet
Browse files Browse the repository at this point in the history
  • Loading branch information
nham committed Jul 22, 2014
1 parent 31c908b commit 9e83d29
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/libcollections/trie.rs
Expand Up @@ -17,6 +17,7 @@ use core::default::Default;
use core::mem::zeroed;
use core::mem;
use core::uint;
use std::hash::{Writer, Hash};

use {Collection, Mutable, Map, MutableMap, Set, MutableSet};
use slice::{Items, MutItems};
Expand Down Expand Up @@ -292,7 +293,16 @@ impl<T> Extendable<(uint, T)> for TrieMap<T> {
}
}

impl<S: Writer, T: Hash<S>> Hash<S> for TrieMap<T> {
fn hash(&self, state: &mut S) {
for elt in self.iter() {
elt.hash(state);
}
}
}

#[allow(missing_doc)]
#[deriving(Hash)]
pub struct TrieSet {
map: TrieMap<()>
}
Expand Down Expand Up @@ -1049,6 +1059,7 @@ mod bench_map {
mod test_set {
use std::prelude::*;
use std::uint;
use std::hash;

use {MutableSet, Set};
use super::TrieSet;
Expand Down Expand Up @@ -1082,4 +1093,20 @@ mod test_set {
assert!(set.contains(x));
}
}

#[test]
fn test_hash() {
let mut x = TrieSet::new();
let mut y = TrieSet::new();

x.insert(1);
x.insert(2);
x.insert(3);

y.insert(3);
y.insert(2);
y.insert(1);

assert!(hash::hash(&x) == hash::hash(&y));
}
}

0 comments on commit 9e83d29

Please sign in to comment.