Phil Schumann, Berlin
Software Developer

27 Oct 2012 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..

Hashing uints in Go

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.

 
Scroll to:
Cogent
Simplicity is a great virtue but it requires hard work to achieve it and education to appreciate it. And to make matters worse: complexity sells better.
Edsger Dijkstra
Sourcery
  • Since 2016 ⋅ PureScript, Elm
  • Since 2015Haskell
  • Since 2014 ⋅ UnityScript
  • Since 2012 ⋅ OpenGL+GLSL
  • Since 2011 ⋅ Go (golang), WebGL, Node.js
  • Since 2008F#, Python, Lisp / Scheme
  • Since 2006 ⋅ SharePoint
  • Since 2004 ⋅ Prolog
  • Since 2003 ⋅ XSLT, XPath
  • Since 2002 ⋅ C#, ASP.NET
  • Since 2001 ⋅ Java, PHP
  • Since 2000 ⋅ SQL, CSS, JavaScript, VisualBasic
  • Since 1999 ⋅ HTML, ASP
  • Since 1998 ⋅ Basic, Pascal
More about me..
Making-of
Site theme: none; hand-crafted.
Static site gen: HaXtatic.
Icons, logos, fonts: © respective owners.