hashtable.c

Go to the documentation of this file.
00001 
00087 /*#*
00088  *#* $Log$
00089  *#* Revision 1.8  2007/02/06 16:05:01  asterix
00090  *#* Replaced ROTATE_* with ROT* defined in macros.h
00091  *#*
00092  *#* Revision 1.7  2006/07/19 12:56:27  bernie
00093  *#* Convert to new Doxygen style.
00094  *#*
00095  *#* Revision 1.6  2006/06/01 12:27:39  marco
00096  *#* Added utilities for protocols
00097  *#*
00098  *#*/
00099 
00100 #include "hashtable.h"
00101 #include <cfg/debug.h>
00102 #include <cfg/compiler.h>
00103 #include <cfg/macros.h> //ROTL(), ROTR();
00104 
00105 #include <string.h>
00106 
00107 
00108 
00109 typedef const void** HashNodePtr;
00110 #define NODE_EMPTY(node)               (!*(node))
00111 #define HT_HAS_INTERNAL_KEY(ht)        (CONFIG_HT_OPTIONAL_INTERNAL_KEY && ht->flags.key_internal)
00112 
00114 INLINE uint8_t *key_internal_get_ptr(struct HashTable *ht, HashNodePtr node)
00115 {
00116     uint8_t* key_buf = ht->key_data.mem;
00117     size_t index;
00118 
00119     // Compute the index of the node and use it to move within the whole key buffer
00120     index = node - &ht->mem[0];
00121     ASSERT(index < (size_t)(1 << ht->max_elts_log2));
00122     key_buf += index * (INTERNAL_KEY_MAX_LENGTH + 1);
00123 
00124     return key_buf;
00125 }
00126 
00127 
00128 INLINE void node_get_key(struct HashTable* ht, HashNodePtr node, const void** key, uint8_t* key_length)
00129 {
00130     if (HT_HAS_INTERNAL_KEY(ht))
00131     {
00132         uint8_t* k = key_internal_get_ptr(ht, node);
00133 
00134         // Key has its length stored in the first byte
00135         *key_length = *k++;
00136         *key = k;
00137     }
00138     else
00139         *key = ht->key_data.hook(*node, key_length);
00140 }
00141 
00142 
00143 INLINE bool node_key_match(struct HashTable* ht, HashNodePtr node, const void* key, uint8_t key_length)
00144 {
00145     const void* key2;
00146     uint8_t key2_length;
00147 
00148     node_get_key(ht, node, &key2, &key2_length);
00149 
00150     return (key_length == key2_length && memcmp(key, key2, key_length) == 0);
00151 }
00152 
00153 
00154 static uint16_t calc_hash(const void* _key, uint8_t key_length)
00155 {
00156     const char* key = (const char*)_key;
00157     uint16_t hash = key_length;
00158     int i;
00159     int len = (int)key_length;
00160 
00161     for (i = 0; i < len; ++i)
00162         hash = ROTL(hash, 4) ^ key[i];
00163 
00164     return hash ^ (hash >> 6) ^ (hash >> 13);
00165 }
00166 
00167 
00168 static HashNodePtr perform_lookup(struct HashTable* ht,
00169                                   const void* key, uint8_t key_length)
00170 {
00171     uint16_t hash = calc_hash(key, key_length);
00172     uint16_t mask = ((1 << ht->max_elts_log2) - 1);
00173     uint16_t index = hash & mask;
00174     uint16_t first_index = index;
00175     uint16_t step;
00176     HashNodePtr node;
00177 
00178     // Fast-path optimization: we check immediately if the current node
00179     //  is the one we were looking for, so we save the computation of the
00180     //  increment step in the common case.
00181     node = &ht->mem[index];
00182     if (NODE_EMPTY(node)
00183         || node_key_match(ht, node, key, key_length))
00184         return node;
00185 
00186     // Increment while going through the hash table in case of collision.
00187     //  This implements the double-hash technique: we use the higher part
00188     //  of the hash as a step increment instead of just going to the next
00189     //  element, to minimize the collisions.
00190     // Notice that the number must be odd to be sure that the whole table
00191     //  is traversed. Actually MCD(table_size, step) must be 1, but
00192     //  table_size is always a power of 2, so we just ensure that step is
00193     //  never a multiple of 2.
00194     step = (ROTR(hash, ht->max_elts_log2) & mask) | 1;
00195 
00196     do
00197     {
00198         index += step;
00199         index &= mask;
00200 
00201         node = &ht->mem[index];
00202         if (NODE_EMPTY(node)
00203             || node_key_match(ht, node, key, key_length))
00204             return node;
00205 
00206         // The check is done after the key compare. This actually causes
00207         //  one more compare in the case the table is full (since the first
00208         //  element was compared at the very start, and then at the end),
00209         //  but it makes faster the common path where we enter this loop
00210         //  for the first time, and index will not match first_index for
00211         //  sure.
00212     } while (index != first_index);
00213 
00214     return NULL;
00215 }
00216 
00217 
00218 void ht_init(struct HashTable* ht)
00219 {
00220     memset(ht->mem, 0, sizeof(ht->mem[0]) * (1 << ht->max_elts_log2));
00221 }
00222 
00223 
00224 static bool insert(struct HashTable* ht, const void* key, uint8_t key_length, const void* data)
00225 {
00226     HashNodePtr node;
00227 
00228     if (!data)
00229         return false;
00230 
00231     if (HT_HAS_INTERNAL_KEY(ht))
00232         key_length = MIN(key_length, (uint8_t)INTERNAL_KEY_MAX_LENGTH);
00233 
00234     node = perform_lookup(ht, key, key_length);
00235     if (!node)
00236         return false;
00237 
00238     if (HT_HAS_INTERNAL_KEY(ht))
00239     {
00240         uint8_t* k = key_internal_get_ptr(ht, node);
00241         *k++ = key_length;
00242         memcpy(k, key, key_length);
00243     }
00244 
00245     *node = data;
00246     return true;
00247 }
00248 
00249 
00250 bool ht_insert_with_key(struct HashTable* ht, const void* key, uint8_t key_length, const void* data)
00251 {
00252 #ifdef _DEBUG
00253     if (!HT_HAS_INTERNAL_KEY(ht))
00254     {
00255         // Construct a fake node and use it to match the key
00256         HashNodePtr node = &data;
00257         if (!node_key_match(ht, node, key, key_length))
00258         {
00259             ASSERT2(0, "parameter key is different from the external key");
00260             return false;
00261         }
00262     }
00263 #endif
00264 
00265     return insert(ht, key, key_length, data);
00266 }
00267 
00268 
00269 bool ht_insert(struct HashTable* ht, const void* data)
00270 {
00271     const void* key;
00272     uint8_t key_length;
00273 
00274 #ifdef _DEBUG
00275     if (HT_HAS_INTERNAL_KEY(ht))
00276     {
00277         ASSERT("parameter cannot be a hash table with internal keys - use ht_insert_with_key()"
00278                && 0);
00279         return false;
00280     }
00281 #endif
00282 
00283     key = ht->key_data.hook(data, &key_length);
00284 
00285     return insert(ht, key, key_length, data);
00286 }
00287 
00288 
00289 const void* ht_find(struct HashTable* ht, const void* key, uint8_t key_length)
00290 {
00291     HashNodePtr node;
00292 
00293     if (HT_HAS_INTERNAL_KEY(ht))
00294         key_length = MIN(key_length, (uint8_t)INTERNAL_KEY_MAX_LENGTH);
00295 
00296     node = perform_lookup(ht, key, key_length);
00297 
00298     if (!node || NODE_EMPTY(node))
00299         return NULL;
00300 
00301     return *node;
00302 }
00303 
00304 
00305 #if 0
00306 
00307 #include <stdlib.h>
00308 
00309 bool ht_test(void);
00310 
00311 static const void* test_get_key(const void* ptr, uint8_t* length)
00312 {
00313     const char* s = ptr;
00314     *length = strlen(s);
00315     return s;
00316 }
00317 
00318 #define NUM_ELEMENTS   256
00319 DECLARE_HASHTABLE_STATIC(test1, 256, test_get_key);
00320 DECLARE_HASHTABLE_INTERNALKEY_STATIC(test2, 256);
00321 
00322 static char data[NUM_ELEMENTS][10];
00323 static char keydomain[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
00324 
00325 static bool single_test(void)
00326 {
00327     int i;
00328 
00329     ht_init(&test1);
00330     ht_init(&test2);
00331 
00332     for (i=0;i<NUM_ELEMENTS;i++)
00333     {
00334         int k;
00335         int klen;
00336 
00337         do
00338         {
00339             klen = (rand() % 8) + 1;
00340             for (k=0;k<klen;k++)
00341                 data[i][k] = keydomain[rand() % (sizeof(keydomain)-1)];
00342             data[i][k]=0;
00343         } while (ht_find_str(&test1, data[i]) != NULL);
00344 
00345         ASSERT(ht_insert(&test1, data[i]));
00346         ASSERT(ht_insert_str(&test2, data[i], data[i]));
00347     }
00348 
00349     for (i=0;i<NUM_ELEMENTS;i++)
00350     {
00351         char *found1, *found2;
00352 
00353         found1 = (char*)ht_find_str(&test1, data[i]);
00354         if (strcmp(found1, data[i]) != 0)
00355         {
00356             ASSERT(strcmp(found1,data[i]) == 0);
00357             return false;
00358         }
00359 
00360         found2 = (char*)ht_find_str(&test2, data[i]);
00361         if (strcmp(found2, data[i]) != 0)
00362         {
00363             ASSERT(strcmp(found2,data[i]) == 0);
00364             return false;
00365         }
00366     }
00367 
00368     return true;
00369 }
00370 
00371 static uint16_t rand_seeds[] = { 1, 42, 666, 0xDEAD, 0xBEEF, 0x1337, 0xB00B };
00372 
00373 bool ht_test(void)
00374 {
00375     int i;
00376 
00377     for (i=0;i<countof(rand_seeds);++i)
00378     {
00379         srand(rand_seeds[i]);
00380         if (!single_test())
00381         {
00382             kprintf("ht_test failed\n");
00383             return false;
00384         }
00385     }
00386 
00387     kprintf("ht_test successful\n");
00388     return true;
00389 }
00390 
00391 #endif