Files
sakurac/core/traits/comparison.sakura
T
2026-11-19 04:54:17 -06:00

141 lines
5.2 KiB
Plaintext

#! @file comparison.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the traits comparing values: `equatable`, which `==` calls, with
#! `equals`, which compares two values through it; `ordered`, which gives values
#! an order, as numbers, enums, and pointers have; and `hashable`, which hashes
#! values, as a dictionary's keys are, with `hash` and `mix`, which hash values
#! and fold words into a hash.
module core;
#! A type whose values can be compared for equality, by a method comparing a
#! value with another of the same type.
#!
#! A type that is `equatable` is compared by `==` and `!=`, which call its
#! `equals`, and the method can be called by name too.
pub trait equatable {
#! Compares this value with another of the same type.
#! @param this The value to compare against.
#! @param other The value to compare with.
#! @returns Whether the two are equal.
def equals(&this, other: &self): bool;
}
#! Compares two values of the same type through its `equals`, as `==` does.
#! @tparam T The type of the values.
#! @param first The value to compare against.
#! @param second The value to compare with.
#! @returns Whether the two are equal.
#! @remarks A type that is not `equatable` is an error where it is compared, as
#! it is with `==`, since its bytes may differ where its values do not,
#! as the padding between fields does.
pub def equals<T>(first: &T, second: &T): bool {
if const !(T is equatable) {
compile_error("`equals` compares values through `equatable`, which this type does not implement, as `==` does.");
}
return first.equals(second);
}
#! A type whose values have an order.
#!
#! `compare` gives a negative number when `this` comes before `other`, zero when
#! they are equal, and a positive number when it comes after. `<`, `<=`, `>`,
#! and `>=` between two values of the type call it, as `==` calls `equals`,
#! which an ordered type also gives, so the two always agree. Numbers, enums,
#! and pointers are ordered without declaring anything.
pub trait ordered : equatable {
#! Compares this value with another of the same type, for their order.
#! @param this The value to compare against.
#! @param other The value to compare with.
#! @returns A negative number where this value comes first, zero where they
#! are equal, and a positive number where it comes after.
def compare(&this, other: &self): i64;
}
#! 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);
}