Files
x/src/hash.rs
numbers 31d80c4a56 .
2023-09-19 18:42:44 -04:00

74 lines
1.6 KiB
Rust

const INITIAL_STATE: u64 = 0xcbf29ce484222325;
const PRIME: u64 = 0x100000001b3;
//noinspection DuplicatedCode
pub const fn hash(bytes: &[u8]) -> u64 {
let mut hash = INITIAL_STATE;
let mut i = 0;
while i < bytes.len() {
hash = hash ^ bytes[i] as u64;
hash = hash.wrapping_mul(PRIME);
i += 1;
}
hash
}
//noinspection DuplicatedCode
pub const fn hash32(bytes: &[u32]) -> u64 {
let mut hash = INITIAL_STATE;
let mut i = 0;
while i < bytes.len() {
hash = hash ^ bytes[i] as u64;
hash = hash.wrapping_mul(PRIME);
i += 1;
}
hash
}
//noinspection DuplicatedCode
pub const fn hash64(bytes: &[u64]) -> u64 {
let mut hash = INITIAL_STATE;
let mut i = 0;
while i < bytes.len() {
hash = hash ^ bytes[i];
hash = hash.wrapping_mul(PRIME);
i += 1;
}
hash
}
//noinspection DuplicatedCode
pub const fn hash_utf8(bytes: &[u8]) -> u64 {
let mut hash = INITIAL_STATE;
let mut i = 0;
while i < bytes.len() {
let char = match bytes[i] {
0x40..=0x5A => bytes[i] + 0x20,
_ => bytes[i],
} as u64;
hash = hash ^ (char);
hash = hash.wrapping_mul(PRIME);
i += 1;
}
hash
}
//noinspection DuplicatedCode
pub const fn hash_utf16(bytes: &[u16]) -> u64 {
let mut hash = INITIAL_STATE;
let mut i = 0;
while i < bytes.len() {
let char = match bytes[i] {
0x40..=0x5A => bytes[i] + 0x20,
_ => bytes[i],
} as u64;
hash = hash ^ (char);
hash = hash.wrapping_mul(PRIME);
i += 1;
}
hash
}