1. c/c++版
#define GOLDEN_SEED 0xEE6B27EB
1.1 应用操作:
QWORD hash64 = MurmurHash64B(di->name, di->nl, GOLDEN_SEED);
result = HashTable_Node_Add(hash64, di->sect, di->nl, di->name)
1.2 MurmurHash64B 算法如下:
static unsigned long long MurmurHash64B ( const void * key, int len, unsigned int seed )
{
const unsigned int m = 0x5bd1e995;
const int r = 24;
unsigned int h1 = seed ^ len;
unsigned int h2 = 0;
const unsigned int * data = (const unsigned int *)key;
while(len >= 8)
{
unsigned int k1 = *data++;
k1 *= m; k1 ^= k1 >> r; k1 *= m;
h1 *= m; h1 ^= k1;
len -= 4;
unsigned int k2 = *data++;
k2 *= m; k2 ^= k2 >> r; k2 *= m;
h2 *= m; h2 ^= k2;
len -= 4;
}
if(len >= 4)
{
unsigned int k1 = *data++;
k1 *= m; k1 ^= k1 >> r; k1 *= m;
h1 *= m; h1 ^= k1;
len -= 4;
}
switch(len)
{
case 3: h2 ^= ((unsigned char*)data)[2] << 16;
case 2: h2 ^= ((unsigned char*)data)[1] << 8;
case 1: h2 ^= ((unsigned char*)data)[0];
h2 *= m;
};
h1 ^= h2 >> 18; h1 *= m;
h2 ^= h1 >> 22; h2 *= m;
h1 ^= h2 >> 17; h1 *= m;
h2 ^= h1 >> 19; h2 *= m;
unsigned long long h = h1;
h = (h << 32) | h2;
return h;
}
2.C#版
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace gjh.utility
{
public class MurmurHash64B
{
public static ulong MakeHashValue(byte[] key, uint seed = 0xee6b27eb)
{
uint len = (uint)key.Length;
const uint m = 0x5bd1e995;
const int r = 24;
uint h1 = seed ^ len;
uint h2 = 0;
int pos = 0;
while (len >= 8)
{
uint k1 = System.BitConverter.ToUInt32(key, pos);
pos += 4;
k1 *= m; k1 ^= k1 >> r; k1 *= m;
h1 *= m; h1 ^= k1;
len -= 4;
uint k2 = System.BitConverter.ToUInt32(key, pos);
pos += 4;
k2 *= m; k2 ^= k2 >> r; k2 *= m;
h2 *= m; h2 ^= k2;
len -= 4;
}
if (len >= 4)
{
uint k1 = System.BitConverter.ToUInt32(key, pos);
pos += 4;
k1 *= m; k1 ^= k1 >> r; k1 *= m;
h1 *= m; h1 ^= k1;
len -= 4;
}
if (len == 3)
{
h2 ^= (uint)key[2] << 16;
h2 ^= (uint)key[1] << 8;
h2 ^= key[0];
h2 *= m;
}
else if (len == 2)
{
h2 ^= (uint)key[1] << 8;
h2 ^= key[0];
h2 *= m;
}
else if (len == 1)
{
h2 ^= key[0];
h2 *= m;
}
h1 ^= h2 >> 18; h1 *= m;
h2 ^= h1 >> 22; h2 *= m;
h1 ^= h2 >> 17; h1 *= m;
h2 ^= h1 >> 19; h2 *= m;
ulong h = h1;
h = (h << 32) | h2;
return h;
}
public static ulong StringToHashValue(string source)
{
byte[] bytes = System.Text.Encoding.Default.GetBytes(source);
return MakeHashValue(bytes);
}
}
}