Skip to content

Commit 9e83d29

Browse files
committed
Derive Hash for TrieMap and TrieSet
1 parent 31c908b commit 9e83d29

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/libcollections/trie.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use core::default::Default;
1717
use core::mem::zeroed;
1818
use core::mem;
1919
use core::uint;
20+
use std::hash::{Writer, Hash};
2021

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

296+
impl<S: Writer, T: Hash<S>> Hash<S> for TrieMap<T> {
297+
fn hash(&self, state: &mut S) {
298+
for elt in self.iter() {
299+
elt.hash(state);
300+
}
301+
}
302+
}
303+
295304
#[allow(missing_doc)]
305+
#[deriving(Hash)]
296306
pub struct TrieSet {
297307
map: TrieMap<()>
298308
}
@@ -1049,6 +1059,7 @@ mod bench_map {
10491059
mod test_set {
10501060
use std::prelude::*;
10511061
use std::uint;
1062+
use std::hash;
10521063

10531064
use {MutableSet, Set};
10541065
use super::TrieSet;
@@ -1082,4 +1093,20 @@ mod test_set {
10821093
assert!(set.contains(x));
10831094
}
10841095
}
1096+
1097+
#[test]
1098+
fn test_hash() {
1099+
let mut x = TrieSet::new();
1100+
let mut y = TrieSet::new();
1101+
1102+
x.insert(1);
1103+
x.insert(2);
1104+
x.insert(3);
1105+
1106+
y.insert(3);
1107+
y.insert(2);
1108+
y.insert(1);
1109+
1110+
assert!(hash::hash(&x) == hash::hash(&y));
1111+
}
10851112
}

0 commit comments

Comments
 (0)