diff options
Diffstat (limited to 'third_party/rust/murmurhash3/src/hasher.rs')
-rw-r--r-- | third_party/rust/murmurhash3/src/hasher.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/third_party/rust/murmurhash3/src/hasher.rs b/third_party/rust/murmurhash3/src/hasher.rs new file mode 100644 index 0000000000..7b7f6c87ca --- /dev/null +++ b/third_party/rust/murmurhash3/src/hasher.rs @@ -0,0 +1,61 @@ +use std::hash::Hasher; +use std::collections::hash_state::HashState; + +use mmh3_32::murmurhash3_x86_32; + +pub struct Murmur3Hasher { + seed: u32, + bytes: Vec<u8>, +} + +#[derive(Clone, Copy)] +pub struct Murmur3HashState { + seed: u32, +} + +impl Murmur3HashState { + pub fn new() -> Murmur3HashState { + return Murmur3HashState { seed: 0 }; + } + + pub fn with_seed(seed: u32) -> Murmur3HashState { + return Murmur3HashState { seed: seed }; + } +} + + +impl Hasher for Murmur3Hasher { + fn finish(&self) -> u64 { + return murmurhash3_x86_32(&self.bytes, self.seed) as u64; + } + + fn write(&mut self, bytes: &[u8]) { + self.bytes.push_all(bytes); + } +} + +impl HashState for Murmur3HashState { + type Hasher = Murmur3Hasher; + + fn hasher(&self) -> Murmur3Hasher { + return Murmur3Hasher { seed: self.seed, bytes: vec![] }; + } +} + +#[cfg(test)] +mod test { + use super::Murmur3HashState; + use std::collections::hash_map::HashMap; + + #[test] + fn use_in_hashmap() { + let mut hashmap = HashMap::with_hash_state(Murmur3HashState::new()); + hashmap.insert("one", 1); + hashmap.insert("two", 2); + + assert!(hashmap.len() == 2); + + assert!(*hashmap.get("one").unwrap() == 1); + assert!(*hashmap.get("two").unwrap() == 2); + } +} |