00001 /*!@file Util/jenkinshash.H 32-bit hash functions (NOT CRYPTOGRAPHIC!) */ 00002 00003 // This code in the corresponding .C file is almost verbatim from 00004 // http://burtleburtle.net/bob/c/lookup2.c (see original public domain 00005 // notice below) with only trivial modifications. The prototypes here 00006 // represent wrappings of those original functions from lookup2.c, and 00007 // the comments are from the lookup2.c. 00008 00009 // 00010 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu> 00011 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Util/jenkinshash.H $ 00012 // $Id: jenkinshash.H 5359 2005-08-20 01:16:05Z rjpeters $ 00013 // 00014 00015 #ifndef UTIL_JENKINSHASH_H_DEFINED 00016 #define UTIL_JENKINSHASH_H_DEFINED 00017 00018 #include "Util/Types.H" 00019 00020 //! hash a variable-length key into a 32-bit value 00021 /*! 00022 k : the key (the unaligned variable-length array of bytes) 00023 len : the length of the key, counting by bytes 00024 level : can be any 4-byte value 00025 Returns a 32-bit value. Every bit of the key affects every bit of 00026 the return value. Every 1-bit and 2-bit delta achieves avalanche. 00027 About 36+6len instructions. 00028 00029 The best hash table sizes are powers of 2. There is no need to do 00030 mod a prime (mod is sooo slow!). If you need less than 32 bits, 00031 use a bitmask. For example, if you need only 10 bits, do 00032 h = (h & hashmask(10)); 00033 In which case, the hash table should have hashsize(10) elements. 00034 00035 If you are hashing n strings (ub1 **)k, do it like this: 00036 for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h); 00037 00038 By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this 00039 code any way you wish, private, educational, or commercial. It's free. 00040 00041 See http://burlteburtle.net/bob/hash/evahash.html 00042 Use for hash table lookup, or anything where one collision in 2^32 is 00043 acceptable. Do NOT use for cryptographic purposes. 00044 */ 00045 uint32 jenkinshash(const byte* key, uint32 length, uint32 initval = 0); 00046 00047 //! Hash a variable-length key into a 32-bit value 00048 /*! 00049 This works on all machines. jenkinshash2() is identical to jenkinshash() on 00050 little-endian machines, except that the length has to be measured 00051 in ub4s instead of bytes. It is much faster than hash(). It 00052 requires 00053 -- that the key be an array of ub4's, and 00054 -- that all your machines have the same endianness, and 00055 -- that the length be the number of ub4's in the key 00056 */ 00057 uint32 jenkinshash2(const uint32* key, uint32 length, uint32 initval = 0); 00058 00059 // ###################################################################### 00060 /* So things look consistent in everyone's emacs... */ 00061 /* Local Variables: */ 00062 /* indent-tabs-mode: nil */ 00063 /* End: */ 00064 00065 #endif // UTIL_JENKINSHASH_H_DEFINED