github.com/mhmtszr/concurrent-swiss-map@v1.0.8/maphash/hasher.go (about) 1 // Copyright 2022 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package maphash 16 17 import "unsafe" 18 19 // Hasher hashes values of type K. 20 // Uses runtime AES-based hashing. 21 type Hasher[K comparable] struct { 22 hash hashfn 23 seed uintptr 24 } 25 26 // NewHasher creates a new Hasher[K] with a random seed. 27 func NewHasher[K comparable]() Hasher[K] { 28 return Hasher[K]{ 29 hash: getRuntimeHasher[K](), 30 seed: newHashSeed(), 31 } 32 } 33 34 // NewSeed returns a copy of |h| with a new hash seed. 35 func NewSeed[K comparable](h Hasher[K]) Hasher[K] { 36 return Hasher[K]{ 37 hash: h.hash, 38 seed: newHashSeed(), 39 } 40 } 41 42 // Hash hashes |key|. 43 func (h Hasher[K]) Hash(key K) uint64 { 44 // promise to the compiler that pointer 45 // |p| does not escape the stack. 46 p := noescape(unsafe.Pointer(&key)) 47 return uint64(h.hash(p, h.seed)) 48 }