pub struct IdentityHasher
{
hash: u64
}
impl IdentityHasher
{
fn new() -> Self
{
IdentityHasher { hash: 0 }
}
}
impl ::std::hash::Hasher for IdentityHasher
{
fn finish(&self) -> u64
{
self.hash
}
fn write(&mut self, _bytes: &[u8])
{
unimplemented!();
}
fn write_u64(&mut self, x: u64)
{
self.hash = x;
}
fn write_usize(&mut self, x: usize)
{
self.hash = x as u64;
}
}
pub struct BuildIdentityHasher {}
impl BuildIdentityHasher
{
fn new() -> Self
{
BuildIdentityHasher {}
}
}
impl ::std::hash::BuildHasher for BuildIdentityHasher
{
type Hasher = IdentityHasher;
fn build_hasher(&self) -> Self::Hasher
{
IdentityHasher::new()
}
}
pub type U64HashMap<V> = ::std::collections::HashMap<u64, V, BuildIdentityHasher>;
pub type USizeHashMap<V> = ::std::collections::HashMap<usize, V, BuildIdentityHasher>;
pub fn new_u64_hash_map<V>() -> U64HashMap<V>
{
U64HashMap::with_hasher(BuildIdentityHasher::new())
}
pub fn new_usize_hash_map<V>() -> USizeHashMap<V>
{
USizeHashMap::with_hasher(BuildIdentityHasher::new())
}
#[cfg(test)]
mod tests
{
use super::{new_u64_hash_map, new_usize_hash_map};
#[test]
fn test_u64()
{
let mut map = new_u64_hash_map();
map.insert(1, "one");
map.insert(2, "two");
map.insert(0xffffffffff, "lots");
assert_eq!(map.len(), 3);
assert_eq!(map.keys().collect::<Vec<&u64>>(), vec![&1, &2, &0xffffffffff]);
assert_eq!(map[&1], "one");
assert_eq!(map[&2], "two");
assert_eq!(map[&0xffffffffff], "lots");
}
#[test]
fn test_usize()
{
let mut map = new_usize_hash_map();
map.insert(1, "one");
map.insert(2, "two");
map.insert(0xffffffff, "many");
assert_eq!(map.len(), 3);
assert_eq!(map.keys().collect::<Vec<&usize>>(), vec![&1, &2, &0xffffffff]);
assert_eq!(map[&1], "one");
assert_eq!(map[&2], "two");
assert_eq!(map[&0xffffffff], "many");
}
}