mirror of
https://git.intege.rs/xlib/x.git
synced 2025-12-05 20:35:01 +00:00
initial
This commit is contained in:
74
src/hash.rs
Normal file
74
src/hash.rs
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user