A hash function takes an arbitrary value and outputs a fixed-size integer. Requirements: deterministic, uniform distribution, fast. One common implementation is FNV-1a:
pub trait Hashable {
fn hash(&self) -> u32;
}
impl Hashable for String {
fn hash(&self) -> u32 {
let mut hash = 2166136261;
for c in self.as_bytes() {
hash = (hash ^ (*c as u32)).wrapping_mul(16777619);
}
hash
}
}References
See also
- SimHash - h(x) = sign(wᵀx) — a hash function designed specifically to preserve cosine similarity, rather than distribute uniformly