Skip to content

Instantly share code, notes, and snippets.

@morbidcamel101
Created November 18, 2014 17:02
Show Gist options
  • Save morbidcamel101/1b5240830ca14c4ca3c1 to your computer and use it in GitHub Desktop.
Save morbidcamel101/1b5240830ca14c4ca3c1 to your computer and use it in GitHub Desktop.
Jenkins Hash algorithm
public static int GetJenkinsHash(this int[] values)
{
unchecked
{
uint hash, i;
for (hash = i = 0; i < values.Length; ++i)
{
hash += (uint)(values[(int)i]);
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return (int)hash;
}
}
public static int GetJenkinsHash(this string text)
{
// http://en.wikipedia.org/wiki/Jenkins_hash_function
unchecked
{
uint hash, i;
for(hash = i = 0; i < text.Length; ++i)
{
hash += (uint)(text[(int)i]);
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return (int)hash;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment