Skip to content

Instantly share code, notes, and snippets.

@jigargandhi
Created October 14, 2019 15:27
Show Gist options
  • Select an option

  • Save jigargandhi/14608721a6cf19ced61a3e7e113dd45b to your computer and use it in GitHub Desktop.

Select an option

Save jigargandhi/14608721a6cf19ced61a3e7e113dd45b to your computer and use it in GitHub Desktop.
Murmur Hash for a ReadonlyMemory
public int GetHashCode(ReadOnlyMemory<char> source)
{
uint c1 = 0xcc9e2d51;
uint c2 = 0x1b873593;
uint m = 5;
uint n = 0xe6546b64;
uint hash = 42;
foreach (var charItem in source.Span)
{
uint k = (uint)charItem;
k *= c1;
k = (k << 15) | (k >> 17);
k *= c2;
hash ^= k;
hash = (hash << 13) | (hash >> 19);
hash = (hash * m) + n;
}
hash ^= (uint)source.Length;
hash ^= (hash >> 16);
hash *= 0x85ebca6b;
hash ^= (hash >> 13);
hash *= 0xc2b2ae35;
hash ^= (hash >> 16);
return (int)hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment