hashicorp / go-set

The go-set package provides generic Set implementations for Go, including HashSet for types with a Hash() function and TreeSet for orderable data
Mozilla Public License 2.0
118 stars 8 forks source link

hashset: provide HashString() and HashParts() helper functions #29

Closed shoenig closed 1 year ago

shoenig commented 1 year ago

This PR adds HashString(string) and HashParts() helper functions for computing usable hash values for implementing Hash() functions of complex struct types.

e.g.

type S struct { A int B string }

func (s *S) Hash() int { return set.HashParts( s.A, set.HashString(B), ) }

shoenig commented 1 year ago

I don't think we can ever rely on non-perfect hashing, at least not while using map under the hood. Unlike some implementations, we have no way of identifying and resolving collisions. First we would need to change the generic parameter type to include an Equal function (making it incompatible with built-in types), and we would also need to implementing chaining for each of the map values - storing values with colliding keys.

Maybe it'll be worth revisiting if the Go team figures out how to make == vs Equal less awkward, but until then we should just keep using map as-is and avoid collisions altogether.