Documentation ¶
Overview ¶
Package to make a hash.Hash64 interface able to hash any value. For comparable types use ReHash and for non comparable types, use ReSubHash, which will be able to hash the comparable subset. All of the types calculate the hash in a non-portable way, that is, the hash obtained may be different on different architectures/systems (this is useful, for example, to write datatypes, like a hash table). It is written to be fast but in clean portable, non-fragile go. An slighly faster version hardwired to hash/maphash is also included. An example of client of this package to implement a cuckoo hash table can be found in cuckoo.
Example ¶
package main import ( "fmt" "gitlab.eif.urjc.es/paurea/dohash" "hash/maphash" ) func main() { type inner = struct { p string o byte } type outer = struct { i int s string in inner } s := [2]outer{{2, "bla", inner{"oooo", 'c'}}, {2, "bla", inner{"oooo", 'c'}}} var mh maphash.Hash dh := dohash.NewReHash[[2]outer](&mh) fmt.Printf("%x", dh.SumObj64(s)) dh.Reset() }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Hasher64 ¶
type Hasher64[K comparable] interface { SumObj64(key K) (s uint64) }
Hashes any comparable type.
type MapHash ¶
type MapHash[K comparable] struct { maphash.Hash // contains filtered or unexported fields }
Wrapper for hash/maphash.Hash capable of hashing values of type K.
Example ¶
package main import ( "fmt" "gitlab.eif.urjc.es/paurea/dohash" ) func main() { type inner = struct { p string o byte } type outer = struct { i int s string in inner } s := [2]outer{{2, "bla", inner{"oooo", 'c'}}, {2, "bla", inner{"oooo", 'c'}}} hm := dohash.NewMapHashFromExample(s) fmt.Printf("%x", hm.SumObj64(s)) hm.Reset() }
Output:
func NewMapHash ¶
func NewMapHash[K comparable]() (hm *MapHash[K])
Create a MapHash capable of hashing values of type K.
func NewMapHashFromExample ¶
func NewMapHashFromExample[K comparable](val K) (hm *MapHash[K])
Create a MapHash from an example value capable of hashing values of the same type K. The value will only have its type inspected.
type ReHash ¶
type ReHash[K comparable] struct { hash.Hash64 // contains filtered or unexported fields }
Wraps a hash.Hash64 adding the information needed to be able to provide generic methods which hash any object of type K which has to be comparable.
func NewReHash ¶
func NewReHash[K comparable](h hash.Hash64) (rh *ReHash[K])
Create a ReHash capable of hashing values of type K.
func NewReHashFromExample ¶
func NewReHashFromExample[K comparable](h hash.Hash64, val K) (rh *ReHash[K])
Create a ReHash from an example value capable of hashing values of the same type K. The value will only have its type inspected.
func (*ReHash[K]) SumObj64 ¶
SumObj64 hashes the key and returns the current 64-bit value, and updates its state. The semantics are similar to that of the underlying hash when hash/Sum64 is called.
type ReSubHash ¶
Wraps a hash.Hash64 adding the information needed to be able to provide generic methods which hash any object of type K ignoring the parts which are not comparable
func NewSubHash ¶
Create a ReHash capable of hashing values of type K. The non-comparable fields (even in the subfields of the fields) will be ignored.
func NewSubHashFromExample ¶
Create a ReSubHash from an example value capable of hashing values of the same type K. The non-comparable fields (even in the subfields of the fields) will be ignored. The value will only have its type inspected.
func (*ReSubHash[K]) SumSubObj64 ¶
Calculate the hash of any type K. See hash.go for details on how this works. The only difference is that this method will ignore any non-comparable fields.
type SubHasher64 ¶
Hashes the comparable subset of any type. The non-comparable fields will be ignored, in the fields of the type and in the fields of the fields recursively.