Me@2: //----------------------------------------------------------------------------- Me@2: // MurmurHash2, by Austin Appleby Me@2: Me@2: // Note - This code makes a few assumptions about how your machine behaves - Me@2: Me@2: // 1. We can read a 4-byte value from any address without crashing Me@2: // 2. sizeof(int) == 4 Me@2: Me@2: // And it has a few limitations - Me@2: Me@2: // 1. It will not work incrementally. Me@2: // 2. It will not produce the same results on little-endian and big-endian Me@2: // machines. Me@2: Me@2: unsigned int MurmurHash2 ( const void * key, int len, unsigned int seed ) Me@2: { Me@2: // 'm' and 'r' are mixing constants generated offline. Me@2: // They're not really 'magic', they just happen to work well. Me@2: Me@2: const unsigned int m = 0x5bd1e995; Me@2: const int r = 24; Me@2: Me@2: // Initialize the hash to a 'random' value Me@2: Me@2: unsigned int h = seed ^ len; Me@2: Me@2: // Mix 4 bytes at a time into the hash Me@2: Me@2: const unsigned char * data = (const unsigned char *)key; Me@2: Me@2: while(len >= 4) Me@2: { Me@2: unsigned int k = *(unsigned int *)data; Me@2: Me@2: k *= m; Me@2: k ^= k >> r; Me@2: k *= m; Me@2: Me@2: h *= m; Me@2: h ^= k; Me@2: Me@2: data += 4; Me@2: len -= 4; Me@2: } Me@2: Me@2: // Handle the last few bytes of the input array Me@2: Me@2: switch(len) Me@2: { Me@2: case 3: h ^= data[2] << 16; Me@2: case 2: h ^= data[1] << 8; Me@2: case 1: h ^= data[0]; Me@2: h *= m; Me@2: }; Me@2: Me@2: // Do a few final mixes of the hash to ensure the last few Me@2: // bytes are well-incorporated. Me@2: Me@2: h ^= h >> 13; Me@2: h *= m; Me@2: h ^= h >> 15; Me@2: Me@2: return h; Me@2: }