maphash

Imports

Imports #

"internal/byteorder"
"math"
"crypto/rand"
"errors"
"internal/byteorder"
"math/bits"
"reflect"
"internal/abi"
"internal/goarch"
"internal/goexperiment"
"unsafe"

Constants & Variables

bufSize const #

bufSize is the size of the Hash write buffer. The buffer ensures that writes depend only on the sequence of bytes, not the sequence of WriteByte/Write/WriteString calls, by always calling rthash with a full buffer (except for the tail).

const bufSize = 128

hashkey var #

var hashkey [4]uint64

m5 const #

const m5 = 0x1d8e4e27c47d124f

purego const #

const purego = true

purego const #

const purego = false

Structs

Hash struct #

A Hash computes a seeded hash of a byte sequence. The zero Hash is a valid Hash ready to use. A zero Hash chooses a random seed for itself during the first call to a Reset, Write, Seed, or Sum64 method. For control over the seed, use SetSeed. The computed hash values depend only on the initial seed and the sequence of bytes provided to the Hash object, not on the way in which the bytes are provided. For example, the three sequences h.Write([]byte{'f','o','o'}) h.WriteByte('f'); h.WriteByte('o'); h.WriteByte('o') h.WriteString("foo") all have the same effect. Hashes are intended to be collision-resistant, even for situations where an adversary controls the byte sequences being hashed. A Hash is not safe for concurrent use by multiple goroutines, but a Seed is. If multiple goroutines must compute the same seeded hash, each can declare its own Hash and call SetSeed with a common Seed.

type Hash struct {
_ [0]func()
seed Seed
state Seed
buf [bufSize]byte
n int
}

Seed struct #

A Seed is a random value that selects the specific hash function computed by a [Hash]. If two Hashes use the same Seeds, they will compute the same hash values for any given input. If two Hashes use different Seeds, they are very likely to compute distinct hash values for any given input. A Seed must be initialized by calling [MakeSeed]. The zero seed is uninitialized and not valid for use with [Hash]'s SetSeed method. Each Seed value is local to a single process and cannot be serialized or otherwise recreated in a different process.

type Seed struct {
s uint64
}

Functions

BlockSize method #

BlockSize returns h's block size.

func (h *Hash) BlockSize() int

Bytes function #

Bytes returns the hash of b with the given seed. Bytes is equivalent to, but more convenient and efficient than: var h Hash h.SetSeed(seed) h.Write(b) return h.Sum64()

func Bytes(seed Seed, b []byte) uint64

Comparable function #

Comparable returns the hash of comparable value v with the given seed such that Comparable(s, v1) == Comparable(s, v2) if v1 == v2. If v != v, then the resulting hash is randomly distributed.

func Comparable(seed Seed, v T) uint64

MakeSeed function #

MakeSeed returns a new random seed.

func MakeSeed() Seed

Reset method #

Reset discards all bytes added to h. (The seed remains the same.)

func (h *Hash) Reset()

Seed method #

Seed returns h's seed value.

func (h *Hash) Seed() Seed

SetSeed method #

SetSeed sets h to use seed, which must have been returned by [MakeSeed] or by another [Hash.Seed] method. Two [Hash] objects with the same seed behave identically. Two [Hash] objects with different seeds will very likely behave differently. Any bytes added to h before this call will be discarded.

func (h *Hash) SetSeed(seed Seed)

Size method #

Size returns h's hash value size, 8 bytes.

func (h *Hash) Size() int

String function #

String returns the hash of s with the given seed. String is equivalent to, but more convenient and efficient than: var h Hash h.SetSeed(seed) h.WriteString(s) return h.Sum64()

func String(seed Seed, s string) uint64

Sum method #

Sum appends the hash's current 64-bit value to b. It exists for implementing [hash.Hash]. For direct calls, it is more efficient to use [Hash.Sum64].

func (h *Hash) Sum(b []byte) []byte

Sum64 method #

Sum64 returns h's current 64-bit value, which depends on h's seed and the sequence of bytes added to h since the last call to [Hash.Reset] or [Hash.SetSeed]. All bits of the Sum64 result are close to uniformly and independently distributed, so it can be safely reduced by using bit masking, shifting, or modular arithmetic.

func (h *Hash) Sum64() uint64

Write method #

Write adds b to the sequence of bytes hashed by h. It always writes all of b and never fails; the count and error result are for implementing [io.Writer].

func (h *Hash) Write(b []byte) (int, error)

WriteByte method #

WriteByte adds b to the sequence of bytes hashed by h. It never fails; the error result is for implementing [io.ByteWriter].

func (h *Hash) WriteByte(b byte) error

WriteComparable function #

WriteComparable adds x to the data hashed by h.

func WriteComparable(h *Hash, x T)

WriteString method #

WriteString adds the bytes of s to the sequence of bytes hashed by h. It always writes all of s and never fails; the count and error result are for implementing [io.StringWriter].

func (h *Hash) WriteString(s string) (int, error)

appendT function #

appendT hash a value.

func appendT(h *Hash, v reflect.Value)

btoi function #

func btoi(b bool) byte

comparableHash function #

func comparableHash(v T, seed Seed) uint64

comparableHash function #

func comparableHash(v T, seed Seed) uint64

escapeForHash function #

escapeForHash forces v to be on the heap, if v contains a non-string pointer. We cannot hash pointers to local variables, as the address of the local variable might change on stack growth. Strings are okay as the hash depends on only the content, not the pointer. This is essentially if hasNonStringPointers(T) { abi.Escape(v) } Implemented as a compiler intrinsic.

func escapeForHash(v T)

float64 method #

func (h *Hash) float64(f float64)

flush method #

precondition: buffer is full.

func (h *Hash) flush()

init function #

func init()

initSeed method #

initSeed seeds the hash if necessary. initSeed is called lazily before any operation that actually uses h.seed/h.state. Note that this does not include Write/WriteByte/WriteString in the case where they only add to h.buf. (If they write too much, they call h.flush, which does call h.initSeed.)

func (h *Hash) initSeed()

mix function #

func mix(a uint64, b uint64) uint64

r3 function #

func r3(p []byte, k uint64) uint64

r4 function #

func r4(p []byte) uint64

r8 function #

func r8(p []byte) uint64

randUint64 function #

func randUint64() uint64

randUint64 function #

func randUint64() uint64

rthash function #

func rthash(buf []byte, seed uint64) uint64

rthash function #

func rthash(buf []byte, seed uint64) uint64

rthashString function #

func rthashString(s string, state uint64) uint64

rthashString function #

func rthashString(s string, state uint64) uint64

runtime_memhash function #

go:linkname runtime_memhash runtime.memhash go:noescape

func runtime_memhash(p unsafe.Pointer, seed uintptr, s uintptr) uintptr

runtime_rand function #

go:linkname runtime_rand runtime.rand

func runtime_rand() uint64

writeComparable function #

func writeComparable(h *Hash, v T)

writeComparable function #

func writeComparable(h *Hash, v T)

wyhash function #

func wyhash(key []byte, seed uint64, len uint64) uint64

Generated with Arrow