about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authornham <hamann.nick@gmail.com>2014-07-22 16:36:09 -0400
committernham <hamann.nick@gmail.com>2014-07-22 16:36:09 -0400
commit9e83d29f30046292f43e982893517f9aee47fa48 (patch)
treebca4b102104c8b3d2f3e8f9665a46772b9626fb2 /src
parent31c908b7be51899b16935dbd453718bdcbee431a (diff)
downloadrust-9e83d29f30046292f43e982893517f9aee47fa48.tar.gz
rust-9e83d29f30046292f43e982893517f9aee47fa48.zip
Derive Hash for TrieMap and TrieSet
Diffstat (limited to 'src')
-rw-r--r--src/libcollections/trie.rs27
1 files changed, 27 insertions, 0 deletions
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<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<()>
 }
@@ -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));
+    }
 }