@@ -22,6 +22,7 @@ use core::iter::Peekable;
22
22
use core::iter;
23
23
use core::mem::{replace, swap};
24
24
use core::ptr;
25
+ use std::hash::{Writer, Hash};
25
26
26
27
use {Collection, Mutable, Set, MutableSet, MutableMap, Map};
27
28
use vec::Vec;
@@ -1055,6 +1056,14 @@ impl<K: Ord, V> Extendable<(K, V)> for TreeMap<K, V> {
1055
1056
}
1056
1057
}
1057
1058
1059
+ impl<S: Writer, K: Ord + Hash<S>, V: Hash<S>> Hash<S> for TreeMap<K, V> {
1060
+ fn hash(&self, state: &mut S) {
1061
+ for elt in self.iter() {
1062
+ elt.hash(state);
1063
+ }
1064
+ }
1065
+ }
1066
+
1058
1067
impl<T: Ord> FromIterator<T> for TreeSet<T> {
1059
1068
fn from_iter<Iter: Iterator<T>>(iter: Iter) -> TreeSet<T> {
1060
1069
let mut set = TreeSet::new();
@@ -1072,6 +1081,14 @@ impl<T: Ord> Extendable<T> for TreeSet<T> {
1072
1081
}
1073
1082
}
1074
1083
1084
+ impl<S: Writer, T: Ord + Hash<S>> Hash<S> for TreeSet<T> {
1085
+ fn hash(&self, state: &mut S) {
1086
+ for elt in self.iter() {
1087
+ elt.hash(state);
1088
+ }
1089
+ }
1090
+ }
1091
+
1075
1092
#[cfg(test)]
1076
1093
mod test_treemap {
1077
1094
use std::prelude::*;
@@ -1608,6 +1625,7 @@ mod bench {
1608
1625
#[cfg(test)]
1609
1626
mod test_set {
1610
1627
use std::prelude::*;
1628
+ use std::hash;
1611
1629
1612
1630
use {Set, MutableSet, Mutable, MutableMap};
1613
1631
use super::{TreeMap, TreeSet};
@@ -1748,6 +1766,22 @@ mod test_set {
1748
1766
assert!(m.clone() == m);
1749
1767
}
1750
1768
1769
+ #[test]
1770
+ fn test_hash() {
1771
+ let mut x = TreeSet::new();
1772
+ let mut y = TreeSet::new();
1773
+
1774
+ x.insert(1i);
1775
+ x.insert(2);
1776
+ x.insert(3);
1777
+
1778
+ y.insert(3i);
1779
+ y.insert(2);
1780
+ y.insert(1);
1781
+
1782
+ assert!(hash::hash(&x) == hash::hash(&y));
1783
+ }
1784
+
1751
1785
fn check(a: &[int],
1752
1786
b: &[int],
1753
1787
expected: &[int],
0 commit comments