Created
October 15, 2016 08:20
-
-
Save arthurprs/390a10c039176bf456f2a47c5f4dbec4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::hash::{Hasher, BuildHasherDefault}; | |
use std::collections::HashMap; | |
pub type IdHasherBuilder = BuildHasherDefault<IdHasher>; | |
pub type IdHashMap<K, V> = HashMap<K, V, IdHasherBuilder>; | |
pub struct IdHasher(u64); | |
impl Default for IdHasher { | |
#[inline] | |
fn default() -> IdHasher { | |
IdHasher(0) | |
} | |
} | |
impl Hasher for IdHasher { | |
#[inline] | |
fn finish(&self) -> u64 { | |
// Primes used in XXH64's finalizer. | |
const PRIME_2: u64 = 14029467366897019727; | |
const PRIME_3: u64 = 1609587929392839161; | |
let mut hash = self.0; | |
hash ^= hash >> 33; | |
hash = hash.wrapping_mul(PRIME_2); | |
hash ^= hash >> 29; | |
hash = hash.wrapping_mul(PRIME_3); | |
hash ^= hash >> 32; | |
hash | |
} | |
#[inline] | |
fn write(&mut self, bytes: &[u8]) { | |
debug_assert!(bytes.len() <= 8); | |
unsafe { | |
let mut temp = 0u64; | |
::std::ptr::copy_nonoverlapping(bytes.as_ptr(), | |
&mut temp as *mut _ as *mut u8, | |
bytes.len()); | |
self.0 ^= temp; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment