tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame^] | 1 | #include "Hashes.h"
|
| 2 |
|
| 3 | #include "Random.h"
|
| 4 |
|
| 5 | //----------------------------------------------------------------------------
|
| 6 | // fake / bad hashes
|
| 7 |
|
| 8 | void randhash ( const void *, int, uint32_t, void * out )
|
| 9 | {
|
| 10 | *(uint32_t*)out = rand_u32();
|
| 11 | }
|
| 12 |
|
| 13 | void BadHash ( const void * key, int len, uint32_t seed, void * out )
|
| 14 | {
|
| 15 | uint32_t h = seed;
|
| 16 |
|
| 17 | const uint8_t * data = (const uint8_t*)key;
|
| 18 |
|
| 19 | for(int i = 0; i < len; i++)
|
| 20 | {
|
| 21 | h ^= h >> 3;
|
| 22 | h ^= h << 5;
|
| 23 | h ^= data[i];
|
| 24 | }
|
| 25 |
|
| 26 | *(uint32_t*)out = h;
|
| 27 | }
|
| 28 |
|
| 29 | void sumhash ( const void * key, int len, uint32_t seed, void * out )
|
| 30 | {
|
| 31 | uint32_t h = seed;
|
| 32 |
|
| 33 | const uint8_t * data = (const uint8_t*)key;
|
| 34 |
|
| 35 | for(int i = 0; i < len; i++)
|
| 36 | {
|
| 37 | h += data[i];
|
| 38 | }
|
| 39 |
|
| 40 | *(uint32_t*)out = h;
|
| 41 | }
|
| 42 |
|
| 43 | void DoNothingHash ( const void *, int, uint32_t, void * )
|
| 44 | {
|
| 45 | return;
|
| 46 | }
|
| 47 |
|
| 48 | //-----------------------------------------------------------------------------
|
| 49 | // One-byte-at-a-time hash based on Murmur's mix
|
| 50 |
|
| 51 | uint32_t MurmurOAAT ( const void * key, int len, uint32_t h )
|
| 52 | {
|
| 53 | const uint8_t * data = (const uint8_t*)key;
|
| 54 |
|
| 55 | h ^= len;
|
| 56 |
|
| 57 | for(int i = 0; i < len; i++)
|
| 58 | {
|
| 59 | h ^= data[i];
|
| 60 | h *= 0x5bd1e995;
|
| 61 | h ^= h >> 16;
|
| 62 | }
|
| 63 |
|
| 64 | return h;
|
| 65 | }
|
| 66 |
|
| 67 | //----------------------------------------------------------------------------
|
| 68 |
|
| 69 | void FNV ( const void * key, int len, uint32_t seed, void * out )
|
| 70 | {
|
| 71 | unsigned int h = seed;
|
| 72 |
|
| 73 | const uint8_t * data = (const uint8_t*)key;
|
| 74 |
|
| 75 | h ^= 2166136261;
|
| 76 |
|
| 77 | for(int i = 0; i < len; i++)
|
| 78 | {
|
| 79 | h ^= data[i];
|
| 80 | h *= 16777619;
|
| 81 | }
|
| 82 |
|
| 83 | *(uint32_t*)out = h;
|
| 84 | }
|
| 85 |
|
| 86 | //-----------------------------------------------------------------------------
|
| 87 |
|
| 88 | uint32_t x17 ( const void * key, int len, uint32_t h )
|
| 89 | {
|
| 90 | const uint8_t * data = (const uint8_t*)key;
|
| 91 |
|
| 92 | for(int i = 0; i < len; ++i)
|
| 93 | {
|
| 94 | h = 17 * h + (data[i] - ' ');
|
| 95 | }
|
| 96 |
|
| 97 | return h ^ (h >> 16);
|
| 98 | }
|
| 99 |
|
| 100 | //-----------------------------------------------------------------------------
|
| 101 |
|
| 102 | uint32_t Bernstein ( const void * key, int len, uint32_t h )
|
| 103 | {
|
| 104 | const uint8_t * data = (const uint8_t*)key;
|
| 105 |
|
| 106 | for(int i = 0; i < len; ++i)
|
| 107 | {
|
| 108 | h = 33 * h + data[i];
|
| 109 | }
|
| 110 |
|
| 111 | return h;
|
| 112 | }
|
| 113 |
|
| 114 | //-----------------------------------------------------------------------------
|