Skip to content

Commit 88231a9

Browse files
committed
auto merge of #15633 : nham/rust/hash_treeset, r=alexcrichton
cc #15294
2 parents 13dc0d7 + a54dc54 commit 88231a9

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/libcollections/treemap.rs

+34
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use core::iter::Peekable;
2222
use core::iter;
2323
use core::mem::{replace, swap};
2424
use core::ptr;
25+
use std::hash::{Writer, Hash};
2526

2627
use {Collection, Mutable, Set, MutableSet, MutableMap, Map};
2728
use vec::Vec;
@@ -1055,6 +1056,14 @@ impl<K: Ord, V> Extendable<(K, V)> for TreeMap<K, V> {
10551056
}
10561057
}
10571058

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+
10581067
impl<T: Ord> FromIterator<T> for TreeSet<T> {
10591068
fn from_iter<Iter: Iterator<T>>(iter: Iter) -> TreeSet<T> {
10601069
let mut set = TreeSet::new();
@@ -1072,6 +1081,14 @@ impl<T: Ord> Extendable<T> for TreeSet<T> {
10721081
}
10731082
}
10741083

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+
10751092
#[cfg(test)]
10761093
mod test_treemap {
10771094
use std::prelude::*;
@@ -1608,6 +1625,7 @@ mod bench {
16081625
#[cfg(test)]
16091626
mod test_set {
16101627
use std::prelude::*;
1628+
use std::hash;
16111629

16121630
use {Set, MutableSet, Mutable, MutableMap};
16131631
use super::{TreeMap, TreeSet};
@@ -1748,6 +1766,22 @@ mod test_set {
17481766
assert!(m.clone() == m);
17491767
}
17501768

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+
17511785
fn check(a: &[int],
17521786
b: &[int],
17531787
expected: &[int],

0 commit comments

Comments
 (0)