ui

source of std::hash

1// Hashing: the Key interface of hash map keys.2//3// Key is a lang item (LANGUAGE_SPEC.md section 6.1a) at the canonical4// path std::hash::Key. The compiler satisfies it for every primitive type5// through the extensions below; String declares it, and any type may.6// Every enum and struct whose parts all satisfy it satisfies it with no7// marker too, through `hash_parts` and `same_parts` below.8// `same` is the key equality a map uses, and two keys that are the same9// must hash alike. Hashes multiply by odd 64-bit constants with the10// wrapping `*%` (section 4.10), folding the high half down first so keys11// differing only there spread too.1213exp interface Key {14    fn hash(self: &) -> u64;15    fn same(self: &, other: &Self) -> bool;16}1718// Mixes 64 bits so that every bit of the input reaches the low half.19exp fn hash_bits(bits: u64) -> u64 {20    const h = (bits ^ (bits >> 32)) *% 0xD6E8FEB86659FD93;21    return h ^ (h >> 32);22}2324// Hashes the bytes eight at a time, the last chunk padded with zeros.25exp fn hash_bytes(bytes: &[u8]) -> u64 {26    const n = bytes.length();27    var state = n *% 0x9E3779B97F4A7C15;28    var at: u64 = 0;29    while (at + 8 <= n) {30        var chunk: u64 = 0;31        for ([at..at + 8]) |k| {32            chunk |= (bytes[k] to u64) << ((k - at) * 8);33        }34        state = (state ^ chunk) *% 0x9E3779B97F4A7C15;35        state ^= state >> 29;36        at += 8;37    }38    var rest: u64 = 0;39    for ([at..n]) |k| {40        rest |= (bytes[k] to u64) << ((k - at) * 8);41    }42    return hash_bits(state ^ rest);43}4445exp fn hash(self value: &u8) -> u64 => hash_bits(value to u64);46exp fn hash(self value: &u16) -> u64 => hash_bits(value to u64);47exp fn hash(self value: &u32) -> u64 => hash_bits(value to u64);48exp fn hash(self value: &u64) -> u64 => hash_bits(value);49exp fn hash(self value: &i8) -> u64 => hash_bits((value to i64) as u64);50exp fn hash(self value: &i16) -> u64 => hash_bits((value to i64) as u64);51exp fn hash(self value: &i32) -> u64 => hash_bits((value to i64) as u64);52exp fn hash(self value: &i64) -> u64 => hash_bits(value as u64);5354// -0.0 is the same key as 0.0; a NaN is the same as nothing, itself55// included, so it is never found again.56exp fn hash(self value: &f32) -> u64 {57    if (value == 0.0) {58        return hash_bits(0);59    }60    return hash_bits((value as u32) to u64);61}6263exp fn hash(self value: &f64) -> u64 {64    if (value == 0.0) {65        return hash_bits(0);66    }67    return hash_bits(value as u64);68}6970exp fn hash(self value: &bool) -> u64 => hash_bits(if (value) 1 else 0);7172exp fn same(self value: &u8, other: &u8) -> bool => value == other;73exp fn same(self value: &u16, other: &u16) -> bool => value == other;74exp fn same(self value: &u32, other: &u32) -> bool => value == other;75exp fn same(self value: &u64, other: &u64) -> bool => value == other;76exp fn same(self value: &i8, other: &i8) -> bool => value == other;77exp fn same(self value: &i16, other: &i16) -> bool => value == other;78exp fn same(self value: &i32, other: &i32) -> bool => value == other;79exp fn same(self value: &i64, other: &i64) -> bool => value == other;80exp fn same(self value: &f32, other: &f32) -> bool => value == other;81exp fn same(self value: &f64, other: &f64) -> bool => value == other;82exp fn same(self value: &bool, other: &bool) -> bool => value == other;8384// Mixes the hash of an enum's or struct's next part into the state.85fn combine(state: u64, part: u64) -> u64 => hash_bits((state ^ part) *% 0x9E3779B97F4A7C15);8687// The `hash` and `same` of an enum or struct satisfying Key with no88// marker, which the compiler builds for each such type in place of these89// bodies: `hash_parts` combines the hashes of its parts in turn, from90// zero, an enum's tag first, and `same_parts` asks each part's `same`,91// an enum's tags equal first. The bodies are what a struct with no parts92// gives.93fn hash_parts<T: Key>(value: &T) -> u64 => 0;9495fn same_parts<T: Key>(value: &T, other: &T) -> bool => true;