From 9e83d29f30046292f43e982893517f9aee47fa48 Mon Sep 17 00:00:00 2001 From: nham Date: Tue, 22 Jul 2014 16:36:09 -0400 Subject: [PATCH] Derive Hash for TrieMap and TrieSet --- src/libcollections/trie.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/libcollections/trie.rs b/src/libcollections/trie.rs index 29ec85590b3..21fafdec019 100644 --- a/src/libcollections/trie.rs +++ b/src/libcollections/trie.rs @@ -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}; @@ -292,7 +293,16 @@ impl Extendable<(uint, T)> for TrieMap { } } +impl> Hash for TrieMap { + fn hash(&self, state: &mut S) { + for elt in self.iter() { + elt.hash(state); + } + } +} + #[allow(missing_doc)] +#[deriving(Hash)] pub struct TrieSet { map: TrieMap<()> } @@ -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; @@ -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)); + } }