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 W. Dijkstra
Sourcery
Working with since1998 » BasicPascal1999 » HTMLASP2000 » SQLCSSJSVB2001 » JavaPHP2002 » C#ASP.NET2003 » XSLT / XPath2004 » Prolog2006 » SharePoint2008 » F# • Python • Lisp2011 » Go • WebGL • Node.js • CoffeeScript • 2012 » OpenGLGLSL2015 » Haskell2016 » TypeScriptElm2017 » PureScriptVue.jsGraphQL
Details..
Making-of
Site theme: none; hand-crafted.
Static site gen: HaXtatic.
Icons, logos, fonts: © respective owners.