Skip to content

Commit

Permalink
Implement Hash for DList
Browse files Browse the repository at this point in the history
  • Loading branch information
nham authored and alexcrichton committed Jul 29, 2014
1 parent 3c453b3 commit f7bcb73
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/libcollections/dlist.rs
Expand Up @@ -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};

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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([]);
Expand Down

0 comments on commit f7bcb73

Please sign in to comment.