Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > Hash_impl
view MurmurHash2.c @ 37:c21c30e45a17
changed name of netbeans proj dirs
| author | Sean Halle <seanhalle@yahoo.com> |
|---|---|
| date | Fri, 14 Feb 2014 07:14:08 -0800 |
| parents | 1218b245530c |
| children |
line source
2 /*This file is sample code pulled off the web -- NOT part of the compiled code
3 * make sure it doesn't get included in makefile 'cause it doesn't compile
4 */
6 //-----------------------------------------------------------------------------
7 // MurmurHash2, by Austin Appleby
9 // Note - This code makes a few assumptions about how your machine behaves -
11 // 1. We can read a 4-byte value from any address without crashing
12 // 2. sizeof(int) == 4
14 // And it has a few limitations -
16 // 1. It will not work incrementally.
17 // 2. It will not produce the same results on little-endian and big-endian
18 // machines.
20 unsigned int MurmurHash2 ( const void * key, int len, unsigned int seed )
21 {
22 // 'm' and 'r' are mixing constants generated offline.
23 // They're not really 'magic', they just happen to work well.
25 const unsigned int m = 0x5bd1e995;
26 const int r = 24;
28 // Initialize the hash to a 'random' value
30 unsigned int h = seed ^ len;
32 // Mix 4 bytes at a time into the hash
34 const unsigned char * data = (const unsigned char *)key;
36 while(len >= 4)
37 {
38 unsigned int k = *(unsigned int *)data;
40 k *= m;
41 k ^= k >> r;
42 k *= m;
44 h *= m;
45 h ^= k;
47 data += 4;
48 len -= 4;
49 }
51 // Handle the last few bytes of the input array
53 switch(len)
54 {
55 case 3: h ^= data[2] << 16;
56 case 2: h ^= data[1] << 8;
57 case 1: h ^= data[0];
58 h *= m;
59 };
61 // Do a few final mixes of the hash to ensure the last few
62 // bytes are well-incorporated.
64 h ^= h >> 13;
65 h *= m;
66 h ^= h >> 15;
68 return h;
69 }
