changeset 2:1218b245530c

adding MurmurHash2.c -- code for high perf hash from net
author Me
date Wed, 28 Jul 2010 13:15:28 -0700
parents 5900d90f5d71
children e6fe47763ee6
files MurmurHash2.c
diffstat 1 files changed, 64 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/MurmurHash2.c	Wed Jul 28 13:15:28 2010 -0700
     1.3 @@ -0,0 +1,64 @@
     1.4 +//-----------------------------------------------------------------------------
     1.5 +// MurmurHash2, by Austin Appleby
     1.6 +
     1.7 +// Note - This code makes a few assumptions about how your machine behaves -
     1.8 +
     1.9 +// 1. We can read a 4-byte value from any address without crashing
    1.10 +// 2. sizeof(int) == 4
    1.11 +
    1.12 +// And it has a few limitations -
    1.13 +
    1.14 +// 1. It will not work incrementally.
    1.15 +// 2. It will not produce the same results on little-endian and big-endian
    1.16 +//    machines.
    1.17 +
    1.18 +unsigned int MurmurHash2 ( const void * key, int len, unsigned int seed )
    1.19 +{
    1.20 +	// 'm' and 'r' are mixing constants generated offline.
    1.21 +	// They're not really 'magic', they just happen to work well.
    1.22 +
    1.23 +	const unsigned int m = 0x5bd1e995;
    1.24 +	const int r = 24;
    1.25 +
    1.26 +	// Initialize the hash to a 'random' value
    1.27 +
    1.28 +	unsigned int h = seed ^ len;
    1.29 +
    1.30 +	// Mix 4 bytes at a time into the hash
    1.31 +
    1.32 +	const unsigned char * data = (const unsigned char *)key;
    1.33 +
    1.34 +	while(len >= 4)
    1.35 +	{
    1.36 +		unsigned int k = *(unsigned int *)data;
    1.37 +
    1.38 +		k *= m; 
    1.39 +		k ^= k >> r; 
    1.40 +		k *= m; 
    1.41 +		
    1.42 +		h *= m; 
    1.43 +		h ^= k;
    1.44 +
    1.45 +		data += 4;
    1.46 +		len -= 4;
    1.47 +	}
    1.48 +	
    1.49 +	// Handle the last few bytes of the input array
    1.50 +
    1.51 +	switch(len)
    1.52 +	{
    1.53 +	case 3: h ^= data[2] << 16;
    1.54 +	case 2: h ^= data[1] << 8;
    1.55 +	case 1: h ^= data[0];
    1.56 +	        h *= m;
    1.57 +	};
    1.58 +
    1.59 +	// Do a few final mixes of the hash to ensure the last few
    1.60 +	// bytes are well-incorporated.
    1.61 +
    1.62 +	h ^= h >> 13;
    1.63 +	h *= m;
    1.64 +	h ^= h >> 15;
    1.65 +
    1.66 +	return h;
    1.67 +}