1
Fork 0

Implement Hash for DList

This commit is contained in:
nham 2014-07-29 16:24:06 -04:00 committed by Alex Crichton
parent 3c453b36ce
commit f7bcb736ad

View file

@ -29,6 +29,7 @@ use core::fmt;
use core::iter;
use core::mem;
use core::ptr;
use std::hash::{Writer, Hash};
use {Collection, Mutable, Deque, MutableSeq};
@ -707,10 +708,20 @@ impl<A: fmt::Show> fmt::Show for DList<A> {
}
}
impl<S: Writer, A: Hash<S>> Hash<S> for DList<A> {
fn hash(&self, state: &mut S) {
self.len().hash(state);
for elt in self.iter() {
elt.hash(state);
}
}
}
#[cfg(test)]
mod tests {
use std::prelude::*;
use std::rand;
use std::hash;
use test::Bencher;
use test;
@ -1075,6 +1086,24 @@ mod tests {
assert!(n != m);
}
#[test]
fn test_hash() {
let mut x = DList::new();
let mut y = DList::new();
assert!(hash::hash(&x) == hash::hash(&y));
x.push_back(1i);
x.push_back(2);
x.push_back(3);
y.push_front(3i);
y.push_front(2);
y.push_front(1);
assert!(hash::hash(&x) == hash::hash(&y));
}
#[test]
fn test_ord() {
let n: DList<int> = list_from([]);