hash

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2022 License: Apache-2.0 Imports: 3 Imported by: 2

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

func CombineHashes(hashes ...int64) int64

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

func Map[A, B any](base EqHash[B], f func(A) B) EqHash[A]

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

func Num[T Number]() EqHash[T]
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

func PairHash[A, B any](a EqHash[A], b EqHash[B]) EqHash[Pair[A, B]]

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

func String() EqHash[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

type EqHashable[T any] interface {
	equality.Equal[T]
	Hashable
}

EqHashable combines equality.Equal and Hashable interfaces

type Fun

type Fun[T any] struct {
	Eq func(T, T) bool
	H  func(T) int64
}

func (Fun[T]) Equal

func (f Fun[T]) Equal(a, b T) bool

func (Fun[T]) Hash

func (f Fun[T]) Hash(a T) int64

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.

type Number

type Number interface {
	int | int8 | int16 | int32 | int64 |
		uint | uint8 | uint16 | uint32 | uint64 |
		float32 | float64 |
		uintptr
}

type Pair

type Pair[A, B any] struct {
	A A
	B B
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL