Files
sakurac/core/traits/hashable.sakura
T

93 lines
3.1 KiB
Plaintext
Raw Normal View History

2026-10-26 10:14:11 -05:00
#! @file hashable.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the hashable trait, which a type implements to be hashed, as a
#! dictionary's keys are, and `hash` and `mix`, which hash values and fold words
#! into a hash.
module core;
#! A type whose values can be hashed into a `u64`, as a dictionary's keys are.
#!
#! Equal values must hash alike, and values that differ should hash differently
#! as often as possible.
pub trait hashable {
#! Hashes this value.
#! @param this The value to hash.
#! @returns Its hash code, which equal values share.
def hash(&this): u64;
}
#! Shortcut function for hashing a value, providing a 64-bit hashed value.
#! @tparam T The type of the value.
#! @param item The object to hash.
#! @returns The hash value of the object, from its `hash`.
#! @remarks A type that is not `hashable` is an error where it is hashed, since
#! its bytes may differ where its values do not, as the padding between
#! fields does, and equal values must hash alike.
pub def hash<T>(item: &T): u64 {
if const !(T is hashable) {
compile_error("`hash` hashes values through `hashable`, which this type does not implement.");
}
return item.hash();
}
# Hashes bytes a word at a time, the last few as one word read without passing
# their end, so that equal bytes always hash alike.
def hash_bytes(data: *u8, len: u64): u64 {
var code := len;
var at := data;
var left := len;
while left >= 8 {
code = mix(code, *(at as *u64));
at = at + 8;
left = left - 8;
}
if left >= 4 {
return mix(code, (*(at as *u32) as u64) | ((*((at + left - 4) as *u32) as u64) << 32));
}
if left >= 2 {
return mix(code, (*(at as *u16) as u64) | ((*((at + left - 2) as *u16) as u64) << 16));
}
if left == 1 {
return mix(code, *at as u64);
}
return code;
}
#! Packs two 32-bit numbers into one word, the first in its high half, as a key
#! naming a pair, such as a type and a trait, is made for a dictionary.
#! @param high The number held in the high half.
#! @param low The number held in the low half.
#! @returns The word, which `pair_high` and `pair_low` take apart, and which no
#! other pair packs into.
pub def pair_key(high: u32, low: u32): u64 {
return (high as u64 << 32) | low as u64;
}
#! The number held in the high half of a word `pair_key` packed.
#! @param key The word.
#! @returns The first number given to `pair_key`.
pub def pair_high(key: u64): u32 {
return (key >> 32) as u32;
}
#! The number held in the low half of a word `pair_key` packed.
#! @param key The word.
#! @returns The second number given to `pair_key`.
pub def pair_low(key: u64): u32 {
return (key & 0xFFFFFFFF) as u32;
}
#! Folds a word into a hash code, as hashing a value made of several does for
#! each.
#! @param code The hash code so far.
#! @param value The word to fold in.
#! @returns The new hash code.
pub def mix(code: u64, value: u64): u64 {
var bits := code ^ value;
bits *= 0x9E3779B97F4A7C15;
return bits ^ (bits >> 29);
}