This commit is contained in:
numbers
2023-09-04 06:26:35 -04:00
commit a5fe71ebb3
26 changed files with 1186 additions and 0 deletions

74
src/hash.rs Normal file
View File

@@ -0,0 +1,74 @@
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: &[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 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
}