Simple data structures / objects in plain C Snapshot
|
00001 /* Copyright (c) Michael Moser (2011) . 3-clause BSD License applies */ 00002 00003 #ifndef _VHASHFUNCTION_H_ 00004 #define _VHASHFUNCTION_H_ 00005 00006 #ifdef __cplusplus 00007 extern "C" { 00008 #endif 00009 00010 00011 #include <cutils/base.h> 00012 00013 typedef uint32_t HASH_VALUE; 00014 00015 /** 00016 * A collection of hash functions, gathered from various places. 00017 */ 00018 00019 00020 #define HASH_STRING 0xFFFFFFFF 00021 00022 /** 00023 * @brief P.J. Weinberger hash function 00024 * 00025 * Source: Compilers Principles Techniques and Tools by Aho,Sethi,Ullman page 436 00026 * Good for string keys 00027 * 00028 * @param keydata (in) pointer to key data 00029 * @param key_length (in) size of key data. If value is VHASH_STRING then keydata is treated as null terminated string. 00030 00031 * @return hash value 00032 */ 00033 HASH_VALUE HASHFUNCTION_PJW(void * keydata, ssize_t key_length); 00034 00035 00036 00037 /** 00038 * @brief Rotating hash 00039 * 00040 * Source: 00041 00042 * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx 00043 * 00044 * "Much of the time, the rotating hash is sufficient, and can be considered the 00045 * minimal acceptable algorithm. Notice that with each improvement, the internal 00046 * state is being mixed up more and more a key element in a good hash function" 00047 00048 * state is being mixed up more and more. 00049 * @param keydata (in) pointer to key data 00050 * @param key_length (in) size of key data. If value is VHASH_STRING then keydata is treated as null terminated string. 00051 00052 * @return hash value 00053 */ 00054 HASH_VALUE HASHFUNCTION_rotating(void * keydata, ssize_t key_length); 00055 00056 /** 00057 * @brief Shift and XOR 00058 * 00059 * Source: 00060 00061 * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx 00062 * 00063 * "Very similar to rotating hash. 00064 * Like many effective hashes, it will fail tests for avalanche, but 00065 * that does not seem to affect its performance in practice." 00066 00067 00068 * @param keydata (in) pointer to key data 00069 * @param key_length (in) size of key data. If value is VHASH_STRING then keydata is treated as null terminated string. 00070 00071 * @return hash value 00072 */ 00073 HASH_VALUE HASHFUNCTION_shift_and_xor(void * keydata, ssize_t key_length); 00074 00075 /** 00076 * @brief Bernstein hash 00077 * 00078 * Source: 00079 00080 * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx 00081 * 00082 * "despite that the algorithm itself is not very sound when it comes to avalanche 00083 * and permutation of the internal state. It has proven very good for small character 00084 * keys, where it can outperform algorithms that result in a more random distribution: 00085 * Bernstein's hash should be used with caution. It performs very well in practice, 00086 * for no apparently known reasons (much like how the constant 33 does better than 00087 * more logical constants for no apparent reason), but in theory it is not up to 00088 * snuff. Always test this function with sample data for every application to ensure 00089 * that it does not encounter a degenerate case and cause excessive collisions." 00090 00091 * @param keydata (in) pointer to key data 00092 * @param key_length (in) size of key data. 00093 00094 * @return hash value 00095 */ 00096 HASH_VALUE HASHFUNCTION_shift_and_xor(void * keydata, ssize_t key_length); 00097 00098 /** 00099 * @brief Fowler/Noll/Vo hash 00100 * 00101 * Source: 00102 00103 * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx 00104 * 00105 * "Follows the same lines as Bernstein's modified hash with carefully chosen constants" 00106 00107 * @param keydata (in) pointer to key data 00108 * @param key_length (in) size of key data. If value is VHASH_STRING then keydata is treated as null terminated string. 00109 00110 * @return hash value 00111 */ 00112 HASH_VALUE HASHFUNCTION_Fowler_Noll_Vo(void * keydata, ssize_t key_length); 00113 00114 #if 0 00115 /** 00116 * @brief Julienne Walker hash 00117 * 00118 * Source: 00119 00120 * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx 00121 * 00122 * "In general, this algorithm is among the better ones that I have tested in terms of both distribution and performance." 00123 00124 * @param keydata (in) pointer to key data 00125 * @param key_length (in) size of key data. If value is VHASH_STRING then keydata is treated as null terminated string. 00126 00127 * @return hash value 00128 */ 00129 M_INLINE HASHFUNCTION_Julienne_Walker (void * keydata, ssize_t key_length) 00130 { 00131 HASH_VALUE hash = 16777551;; 00132 unsigned char * key = (unsigned char *) keydata; 00133 ssize_t i; 00134 00135 if (key_length != -1) { 00136 for (hash = 0, i=0; i<key_length; ++i) { 00137 hash = ( hash << 1 | hash >> 31 ) ^ tab[p[i]];; 00138 } 00139 return hash; 00140 } else { 00141 for (hash = 0; *key; ++key) { 00142 hash = ( hash << 1 | hash >> 31 ) ^ tab[p[i]]; 00143 } 00144 return hash; 00145 } 00146 } 00147 #endif 00148 00149 /** 00150 * @brief One at a time by Bob Jenkins 00151 * 00152 * Source: 00153 00154 * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx 00155 * 00156 * "This algorithm quickly reaches avalanche (!!!) and performs very well 00157 * this function is another that should be one of the first to be tested 00158 * in any application, if not the very first" 00159 00160 * @param keydata (in) pointer to key data 00161 * @param key_length (in) size of key data. If value is VHASH_STRING then keydata is treated as null terminated string. 00162 00163 * @return hash value 00164 */ 00165 HASH_VALUE HASHFUNCTION_Bob_Jenkins_one_at_a_time(void * keydata, ssize_t key_length); 00166 00167 /** 00168 * @brief ELF hash 00169 * 00170 * Source: 00171 00172 * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx 00173 * 00174 00175 * @param keydata (in) pointer to key data 00176 * @param key_length (in) size of key data. If value is VHASH_STRING then keydata is treated as null terminated string. 00177 00178 * @return hash value 00179 */ 00180 HASH_VALUE HASHFUNCTION_ELF(void * keydata, ssize_t key_length); 00181 00182 00183 /** 00184 * @brief Bob Jenkins 00185 * 00186 * Source: 00187 00188 * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx 00189 * 00190 * Very good hash function. Really very good - passes all sorts of avalanche tests. 00191 00192 * @param keydata (in) pointer to key data 00193 * @param key_length (in) size of key data. If value is VHASH_STRING then keydata is treated as null terminated string. 00194 00195 * @return hash value 00196 */ 00197 HASH_VALUE HASHFUNCTION_Bob_Jenkins(void * keydata, ssize_t length); 00198 00199 00200 00201 /** 00202 * @brief good for most cases. 00203 * 00204 * Source: 00205 00206 * @param keydata (in) pointer to key data 00207 * @param key_length (in) the size of key data. If value is VHASH_STRING then keydata is treated as null terminated string. 00208 * @return hash value 00209 */ 00210 HASH_VALUE HASHFUNCTION_sample_hash_func(void * keydata, ssize_t key_length); 00211 00212 00213 00214 #ifdef __cplusplus 00215 } 00216 #endif 00217 00218 #endif