Documentation ¶
Overview ¶
Package hash provides a type class for hashable types, and defines some commonly used instances.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CombineHashes ¶
CombineHashes calculates a combined hash for the given hash values
Example ¶
package main import ( "fmt" "github.com/peterzeller/go-fun/hash" ) func main() { fmt.Println("combined hash is", hash.CombineHashes(1, 2, 42)) }
Output: combined hash is 30856
Types ¶
type EqHash ¶
type EqHash[T any] interface { equality.Equality[T] // Hash deterministically computes an int value fom a value. // Ideally, distinct values should have a high chance of distinct hash values, and computation should be fast. Hash(v T) int64 }
EqHash compines an equals function with a Hash function
func Gob ¶
func Gob[T comparable]() EqHash[T]
Gob encoding based hash code. This hashes the bytes of the gob encoding of T. Remember that this excludes private fields, pointers, and can fail at runtime for some types.
Example ¶
package main import ( "fmt" "github.com/peterzeller/go-fun/hash" ) type exampleStruct struct { A int B string } func main() { h := hash.Gob[exampleStruct]() v := exampleStruct{ A: 42, B: "hello", } fmt.Println("hash is", h.Hash(v)) }
Output: hash is -4727944318207215451
func Map ¶
Map creates an EqHash instance for A, given an EqHash instance for B and a function from A to B
Example ¶
package main import ( "fmt" "github.com/peterzeller/go-fun/hash" ) func main() { h := hash.Map(hash.String(), func(bs []byte) string { return string(bs) }) fmt.Println("hash is", h.Hash([]byte("hello world"))) }
Output: hash is 8618312879776256743
func Natural ¶ added in v1.1.0
func Natural[T EqHashable[T]]() EqHash[T]
Natural uses the hash function implemented by the types Hash function.
Example ¶
package main import ( "fmt" "github.com/peterzeller/go-fun/hash" ) type pair struct { a int b string } func (p pair) Equal(other pair) bool { return p == other } func (p pair) Hash() int64 { return hash.CombineHashes( hash.Num[int]().Hash(p.a), hash.String().Hash(p.b)) } func main() { h := hash.Natural[pair]() v := pair{ a: 42, b: "hello", } fmt.Println("hash is", h.Hash(v)) }
Output: hash is -6615550055289272862
func Num ¶
Example ¶
package main import ( "fmt" "github.com/peterzeller/go-fun/hash" ) func main() { h := hash.Num[int]() fmt.Println("hash of 42 =", h.Hash(42)) }
Output: hash of 42 = 42
func PairHash ¶
PairHash creates an EqHash instance for a pair, combining two EqHash instances
Example ¶
package main import ( "fmt" "github.com/peterzeller/go-fun/hash" ) type pair struct { a int b string } func (p pair) Equal(other pair) bool { return p == other } func (p pair) Hash() int64 { return hash.CombineHashes( hash.Num[int]().Hash(p.a), hash.String().Hash(p.b)) } func main() { ph := hash.PairHash(hash.Num[int](), hash.String()) h := hash.Map(ph, func(v pair) hash.Pair[int, string] { return hash.Pair[int, string]{ A: v.a, B: v.b, } }) v := pair{ a: 42, b: "hello", } fmt.Println("hash is", h.Hash(v)) }
Output: hash is -6615550055289272862
func String ¶
Example ¶
package main import ( "fmt" "github.com/peterzeller/go-fun/hash" ) func main() { h := hash.String() fmt.Println("1. hash is", h.Hash("hello world")) fmt.Println("2. hash is", h.Hash("hello worle")) fmt.Println("3. hash is", h.Hash("gello world")) }
Output: 1. hash is 8618312879776256743 2. hash is 8618311780264628532 3. hash is 1592153891954394282
type EqHashable ¶ added in v1.1.0
EqHashable combines equality.Equal and Hashable interfaces
type Hashable ¶ added in v1.1.0
type Hashable interface { // Hash deterministically computes an int value fom a value. // Ideally, distinct values should have a high chance of distinct hash values, and computation should be fast. Hash() int64 }
Hashable are types with a Hash function.