Files
go-cuckoo/hash.go
M.V. Hutz 5c39182958
All checks were successful
CI / Check PR Title (pull_request) Successful in 29s
CI / Go Lint (pull_request) Successful in 39s
CI / Makefile Lint (pull_request) Successful in 48s
CI / Markdown Lint (pull_request) Successful in 31s
CI / Unit Tests (pull_request) Successful in 37s
CI / Fuzz Tests (pull_request) Successful in 1m38s
CI / Mutation Tests (pull_request) Successful in 1m19s
refactor: HashTable -> Table, table -> subtable
2026-04-13 21:11:37 -04:00

28 lines
827 B
Go

package cuckoo
import (
"hash/maphash"
)
// A Hash function maps any data to a fixed-length value (in this case, a
// [uint64]).
//
// It is used by the [Table] to evenly distribute values
// amongst its slots. A good hash function is uniform, [chaotic], and
// deterministic. [Table] uses [NewDefaultHash] by default, which is built on
// [maphash.Comparable].
//
// [chaotic]: https://en.wikipedia.org/wiki/Avalanche_effect
type Hash[K any] = func(key K) (digest uint64)
// NewDefaultHash returns a new [Hash] which uses [maphash.Comparable].
//
// Each hash has a random seed, so calling this function again will return a new
// hash. Do not use this for testing.
func NewDefaultHash[K comparable]() Hash[K] {
seed := maphash.MakeSeed()
return func(key K) (digest uint64) {
return maphash.Comparable(seed, key)
}
}