UYS  0.1
Useful Yet Simple (C libraries collection)
 All Data Structures Files Functions Typedefs Macros Pages
lookup3.h
1 #ifndef LOOKUP3_H
2 #define LOOKUP3_H
3 
4 #include <stdint.h> /* defines uint32_t etc */
5 
6 /*
7 ------------------------------------------------------------------------------
8 hashlittle() -- hash a variable-length key into a 32-bit value
9  k : the key (the unaligned variable-length array of bytes)
10  length : the length of the key, counting by bytes
11  initval : can be any 4-byte value
12 
13 Returns a 32-bit value. Every bit of the key affects every bit of
14 the return value. Two keys differing by one or two bits will have
15 totally different hash values.
16 
17 The best hash table sizes are powers of 2. There is no need to do
18 mod a prime (mod is sooo slow!). If you need less than 32 bits,
19 use a bitmask. For example, if you need only 10 bits, do
20  h = (h & hashmask(10));
21 In which case, the hash table should have hashsize(10) elements.
22 
23 If you are hashing n strings (uint8_t **)k, do it like this:
24  for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
25 
26 By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
27 code any way you wish, private, educational, or commercial. It's free.
28 
29 Use for hash table lookup, or anything where one collision in 2^^32 is
30 acceptable. Do NOT use for cryptographic purposes.
31 ------------------------------------------------------------------------------
32 */
33 
34 uint32_t hashlittle(const void *key, size_t length, uint32_t initval);
35 
36 
37 /*
38  * hashlittle2: return 2 32-bit hash values
39  *
40  * This is identical to hashlittle(), except it returns two 32-bit hash
41  * values instead of just one. This is good enough for hash table
42  * lookup with 2^^64 buckets, or if you want a second hash if you're not
43  * happy with the first, or if you want a probably-unique 64-bit ID for
44  * the key. *pc is better mixed than *pb, so use *pc first. If you want
45  * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
46  *
47  * /param *key: the key to hash;
48  * /param length: length of the key;
49  * /param *pc: IN: primary initval, OUT: primary hash
50  * /param *pb: IN: secondary initval, OUT: secondary hash
51  */
52 void hashlittle2(const void *key, size_t length, uint32_t *pc, uint32_t *pb);
53 
54 #endif