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);
}
}
}
c++实现:
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;
}
吐槽一下csdn童鞋,,,文章留着自己玩吧,反正别人也看不着。fuck
标签:int,k2,c#,h1,c++,h2,k1,len,murmurhash64B From: https://www.cnblogs.com/uniqs/p/17789840.html