Skip to content

Commit

Permalink
auto merge of #15902 : nham/rust/hash_triemap, r=alexcrichton
Browse files Browse the repository at this point in the history
  • Loading branch information
bors committed Jul 23, 2014
2 parents 826b835 + 366c66e commit a234337
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/libcollections/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -40,6 +41,15 @@ pub struct TrieMap<T> {
length: uint
}

impl<T: PartialEq> PartialEq for TrieMap<T> {
fn eq(&self, other: &TrieMap<T>) -> bool {
self.len() == other.len() &&
self.iter().zip(other.iter()).all(|(a, b)| a == b)
}
}

impl<T: Eq> Eq for TrieMap<T> {}

impl<T> Collection for TrieMap<T> {
/// Return the number of elements in the map
#[inline]
Expand Down Expand Up @@ -292,7 +302,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, PartialEq, Eq)]
pub struct TrieSet {
map: TrieMap<()>
}
Expand Down Expand Up @@ -661,6 +680,7 @@ mod test_map {
use std::prelude::*;
use std::iter::range_step;
use std::uint;
use std::hash;

use {MutableMap, Map};
use super::{TrieMap, TrieNode, Internal, External, Nothing};
Expand Down Expand Up @@ -933,6 +953,41 @@ mod test_map {
assert!(m_lower.iter().all(|(_, &x)| x == 0));
assert!(m_upper.iter().all(|(_, &x)| x == 0));
}

#[test]
fn test_eq() {
let mut a = TrieMap::new();
let mut b = TrieMap::new();

assert!(a == b);
assert!(a.insert(0, 5i));
assert!(a != b);
assert!(b.insert(0, 4i));
assert!(a != b);
assert!(a.insert(5, 19));
assert!(a != b);
assert!(!b.insert(0, 5));
assert!(a != b);
assert!(b.insert(5, 19));
assert!(a == b);
}

#[test]
fn test_hash() {
let mut x = TrieMap::new();
let mut y = TrieMap::new();

assert!(hash::hash(&x) == hash::hash(&y));
x.insert(1, 'a');
x.insert(2, 'b');
x.insert(3, 'c');

y.insert(3, 'c');
y.insert(2, 'b');
y.insert(1, 'a');

assert!(hash::hash(&x) == hash::hash(&y));
}
}

#[cfg(test)]
Expand Down

0 comments on commit a234337

Please sign in to comment.