use seahash::SeaHasher;
use serde::{Deserialize, Serialize};
use std::hash::{BuildHasher, Hasher};
const M1: u64 = 0xff51afd7ed558ccd;
const M2: u64 = 0xc4ceb9fe1a85ec53;
#[inline]
pub fn fmix64(key: u64) -> u64 {
let mut k = key;
k ^= k >> 33;
k = k.wrapping_mul(M1);
k ^= k >> 33;
k = k.wrapping_mul(M2);
k ^= k >> 33;
k
}
const C1: u64 = 0x87c37b91114253d5;
const C2: u64 = 0x4cf5ad432745937f;
#[inline]
pub fn murmur_hash3(key: u64) -> u64 {
let k1 = key.wrapping_mul(C1).rotate_left(31).wrapping_mul(C2);
let mut h1 = k1;
h1 = h1.rotate_left(27);
h1 = h1.wrapping_mul(5).wrapping_add(0x52dce729);
h1 ^= 8u64;
h1 = fmix64(h1);
h1
}
#[inline]
pub fn sea_hash(key: u64) -> u64 {
let mut hasher = SeaHasher::default();
hasher.write_u64(key);
hasher.finish()
}
#[derive(Default, Serialize, Deserialize, Debug)]
pub struct KHasher {
hash: u64,
}
impl Hasher for KHasher {
#[inline]
fn write(&mut self, _: &[u8]) {}
#[inline]
fn write_u64(&mut self, i: u64) {
self.hash = i;
}
#[inline]
fn finish(&self) -> u64 {
self.hash
}
}
#[derive(Default, Serialize, Deserialize, Debug)]
pub struct KBuildHasher;
impl BuildHasher for KBuildHasher {
type Hasher = KHasher;
fn build_hasher(&self) -> Self::Hasher {
KHasher { hash: 0 }
}
}
#[derive(Default, Serialize, Deserialize)]
pub struct SBuildHasher;
impl BuildHasher for SBuildHasher {
type Hasher = SeaHasher;
fn build_hasher(&self) -> Self::Hasher {
SeaHasher::default()
}
}