Hashing uints in Go

  • 27th Oct 2012
  • 1 min read
  • • 
  • Tags: 
  • Go
  • Golang

Needed to hash a set of 3 unsigned integers into one integer value uniquely identifying-representing that particular sequence of uints, to be used as a "hash key" in a map[int]sometype..

Simple arithmetics (add or mult) are out to uniquely hash different combinations: the set 1,2,4 should produce a different hash key than 1,4,2 and 2,1,4 and 2,4,1 and 4,1,2 and 4,2,1.

Found Robert Jenkins' 96 bit Mix Function with a neat source snippet right there (Java). Implemented in Go and seems to work even though I'm not sure if Golangs >> right-shift operator (spec'd as integer >> unsigned integer) really works like Java's >>> "unsigned right shift"...

Update:

Actually by now it dawned on me I can just use a [3]uint as a hash key in Golang here, which is fine in this use-case. Simplicity FTW!

But if you're curious about this topic, here's an incredible testing and comparison of hashing algos.