Implementazione di una tabella hash
[bertos.git] / mware / hashtable.c
1 /*!
2  * \file
3  * <!--
4  * Copyright (C) 2004 Giovanni Bajo
5  * Copyright (C) 2004 Develer S.r.l. (http://www.develer.com/)
6  * All Rights Reserved.
7  * -->
8  *
9  * \brief Portable hash table implementation
10  *
11  * Some rationales of our choices in implementation:
12  *
13  * \li For embedded systems, it is vital to allocate the table in static memory. To do
14  * so, it is necessary to expose the \c HashNode and \c HashTable structures in the header file.
15  * Nevertheless, they should be used as opaque types (that is, the users should not
16  * access the structure fields directly).
17  *
18  * \li To statically allocate the structures, a macro is provided. With this macro, we
19  * are hiding completely \c HashNode to the user (who only manipulates \c HashTable). Without
20  * the macro, the user would have had to define both the \c HashNode and the \c HashTable
21  * manually, and pass both of them to \c ht_init() (which would have created the link between
22  * the two). Instead, the link is created with a literal initialization.
23  *
24  * \li The hash table is created as power of two to remove the divisions from the code.
25  * Of course, hash functions work at their best when the table size is a prime number.
26  * When calculating the modulus to convert the hash value to an index, the actual operation
27  * becomes a bitwise AND: this is fast, but truncates the value losing bits. Thus, the higher
28  * bits are first "merged" with the lower bits through some XOR operations (see the last line of
29  * \c calc_hash()).
30  *
31  * \li To minimize the memory occupation, there is no flag to set for the empty node. An
32  * empty node is recognized by its data pointer set to NULL. It is then invalid to store
33  * NULL as data pointer in the table.
34  *
35  * \li The visiting interface through iterators is implemented with pass-by-value semantic.
36  * While this is overkill for medium-to-stupid compilers, it is the best designed from an
37  * user point of view. Moreover, being totally inlined (defined completely in the header),
38  * even a stupid compiler should be able to perform basic optimizations on it.
39  * We thought about using a pass-by-pointer semantic but it was much more awful to use, and
40  * the compiler is then forced to spill everything to the stack (unless it is *very* smart).
41  *
42  * \li The current implementation allows to either store the key internally (that is, copy
43  * the key within the hash table) or keep it external (that is, a hook is used to extract
44  * the key from the data in the node). The former is more memory-hungry of course, as it
45  * allocated static space to store the key copies. The overhead to keep both methods at
46  * the same time is minimal:
47  *    <ul>
48  *    <li>There is a run-time check in node_get_key which is execute per each node visited.</li>
49  *    <li>Theoretically, there is no memory overhead. In practice, there were no
50  *        flags in \c struct HashTable till now, so we had to add a first bit flag, but the
51  *        overhead will disappear if a second flag is added for a different reason later.</li>
52  *    <li>There is a little interface overhead, since we have two different versions of
53  *        \c ht_insert(), one with the key passed as parameter and one without, but in
54  *        the common case (external keys) both can be used.</li>
55  *    </ul>
56  *
57  * \version $Id$
58  *
59  * \author Giovanni Bajo <rasky@develer.com>
60  */
61
62 /*
63  * $Log$
64  * Revision 1.1  2004/07/14 14:08:16  rasky
65  * Implementazione di una tabella hash
66  *
67  * Revision 1.13  2004/07/12 16:33:36  rasky
68  * Aggiunta nuova ASSERT2, con stringa di descrizione del problema (disabilitabile tramite una macro di configurazione)
69  * Modificato il codice del firmware per utilizzare ASSERT2
70  * Modificato il progetto in modo da disabilitare le stringhe di errore nel target xROM-xRAM
71  *
72  * Revision 1.12  2004/06/14 15:15:24  rasky
73  * Cambiato key_data in un union invece di castare
74  * Aggiunto un ASSERT sull'indice calcolata nella key_internal_get_ptr
75  *
76  * Revision 1.11  2004/06/14 15:09:04  rasky
77  * Cambiati i messaggi di assert (è inutile citare il nome della funzione)
78  *
79  * Revision 1.10  2004/06/14 15:07:38  rasky
80  * Convertito il loop di calc_hash a interi (per farlo ottimizzare maggiormente)
81  *
82  * Revision 1.9  2004/06/14 14:59:40  rasky
83  * Rinominanta la macro di configurazione per rispettare il namespace, e aggiunta in un punto in cui mancava
84  *
85  * Revision 1.8  2004/06/12 15:18:05  rasky
86  * Nuova hashtable con chiave esterna o interna a scelta, come discusso
87  *
88  * Revision 1.7  2004/06/04 17:16:31  rasky
89  * Fixato un bug nel caso in cui la chiave ecceda la dimensione massima: il clamp non può essere fatto dentro la perform_lookup perché anche la ht_insert deve avere il valore clampato a disposizione per fare la memcpy
90  *
91  * Revision 1.6  2004/05/26 16:36:50  rasky
92  * Aggiunto il rationale per l'interfaccia degli iteratori
93  *
94  * Revision 1.5  2004/05/24 15:28:20  rasky
95  * Sistemata la documentazione, rimossa keycmp in favore della memcmp
96  *
97  */
98 #include "hashtable.h"
99 #include <drv/kdebug.h>
100 #include <compiler.h>
101 #include <string.h>
102
103
104 #define ROTATE_LEFT_16(num, count)     (((num) << (count)) | ((num) >> (16-(count))))
105 #define ROTATE_RIGHT_16(num, count)    ROTATE_LEFT_16(num, 16-(count))
106
107 typedef const void** HashNodePtr;
108 #define NODE_EMPTY(node)               (!*(node))
109 #define HT_HAS_INTERNAL_KEY(ht)        (CONFIG_HT_OPTIONAL_INTERNAL_KEY && ht->flags.key_internal)
110
111 /*! For hash tables with internal keys, compute the pointer to the internal key for a given \a node. */
112 INLINE uint8_t* key_internal_get_ptr(struct HashTable* ht, HashNodePtr node)
113 {
114         uint8_t* key_buf = ht->key_data.mem;
115         size_t index;
116
117         // Compute the index of the node and use it to move within the whole key buffer
118         index = node - &ht->mem[0];
119         ASSERT(index < (1 << ht->max_elts_log2));
120         key_buf += index * (INTERNAL_KEY_MAX_LENGTH + 1);
121
122         return key_buf;
123 }
124
125
126 INLINE void node_get_key(struct HashTable* ht, HashNodePtr node, const void** key, uint8_t* key_length)
127 {
128         if (HT_HAS_INTERNAL_KEY(ht))
129         {
130                 uint8_t* k = key_internal_get_ptr(ht, node);
131
132                 // Key has its length stored in the first byte
133                 *key_length = *k++;
134                 *key = k;
135         }
136         else
137                 *key = ht->key_data.hook(*node, key_length);
138 }
139
140 INLINE bool node_key_match(struct HashTable* ht, HashNodePtr node, const void* key, uint8_t key_length)
141 {
142         const void* key2;
143         uint8_t key2_length;
144
145         node_get_key(ht, node, &key2, &key2_length);
146
147         return (key_length == key2_length && memcmp(key, key2, key_length) == 0);
148 }
149
150 static uint16_t calc_hash(const void* _key, uint8_t key_length)
151 {
152         const char* key = (const char*)_key;
153         uint16_t hash = key_length;
154         int i;
155         int len = (int)key_length;
156
157         for (i = 0; i < len; ++i)
158                 hash = ROTATE_LEFT_16(hash, 4) ^ key[i];
159
160         return hash ^ (hash >> 6) ^ (hash >> 13);
161 }
162
163 static HashNodePtr perform_lookup(struct HashTable* ht,
164                                   const void* key, uint8_t key_length)
165 {
166         uint16_t hash = calc_hash(key, key_length);
167         uint16_t mask = ((1 << ht->max_elts_log2) - 1);
168         uint16_t index = hash & mask;
169         uint16_t first_index = index;
170         uint16_t step;
171         HashNodePtr node;
172
173         // Fast-path optimization: we check immediately if the current node
174         //  is the one we were looking for, so we save the computation of the
175         //  increment step in the common case.
176         node = &ht->mem[index];
177         if (NODE_EMPTY(node)
178                 || node_key_match(ht, node, key, key_length))
179                 return node;
180
181         // Increment while going through the hash table in case of collision.
182         //  This implements the double-hash technique: we use the higher part
183         //  of the hash as a step increment instead of just going to the next
184         //  element, to minimize the collisions. 
185         // Notice that the number must be odd to be sure that the whole table
186         //  is traversed. Actually MCD(table_size, step) must be 1, but 
187         //  table_size is always a power of 2, so we just ensure that step is
188         //  never a multiple of 2.
189         step = (ROTATE_RIGHT_16(hash, ht->max_elts_log2) & mask) | 1;
190
191         do
192         {
193                 index += step;
194                 index &= mask;
195
196                 node = &ht->mem[index];
197                 if (NODE_EMPTY(node)
198                         || node_key_match(ht, node, key, key_length))
199                         return node;
200
201                 // The check is done after the key compare. This actually causes
202                 //  one more compare in the case the table is full (since the first
203                 //  element was compared at the very start, and then at the end),
204                 //  but it makes faster the common path where we enter this loop
205                 //  for the first time, and index will not match first_index for
206                 //  sure.
207         } while (index != first_index);
208
209         return NULL;
210 }
211
212 void ht_init(struct HashTable* ht)
213 {
214         memset(ht->mem, 0, sizeof(ht->mem[0]) * (1 << ht->max_elts_log2));
215 }
216
217 static bool insert(struct HashTable* ht, const void* key, uint8_t key_length, const void* data)
218 {
219         HashNodePtr node;
220
221         if (!data)
222                 return false;
223
224         if (HT_HAS_INTERNAL_KEY(ht))
225                 key_length = MIN(key_length, INTERNAL_KEY_MAX_LENGTH);
226
227         node = perform_lookup(ht, key, key_length);
228         if (!node)
229                 return false;
230
231         if (HT_HAS_INTERNAL_KEY(ht))
232         {
233                 uint8_t* k = key_internal_get_ptr(ht, node);
234                 *k++ = key_length;
235                 memcpy(k, key, key_length);
236         }
237
238         *node = data;
239         return true;
240 }
241
242 bool ht_insert_with_key(struct HashTable* ht, const void* key, uint8_t key_length, const void* data)
243 {
244 #ifdef _DEBUG
245         if (!HT_HAS_INTERNAL_KEY(ht))
246         {
247                 // Construct a fake node and use it to match the key
248                 HashNodePtr node = &data;
249                 if (!node_key_match(ht, node, key, key_length))
250                 {
251                         ASSERT2(0, "parameter key is different from the external key");
252                         return false;
253                 }
254         }
255 #endif
256
257         return insert(ht, key, key_length, data);
258 }
259
260 bool ht_insert(struct HashTable* ht, const void* data)
261 {
262         const void* key;
263         uint8_t key_length;
264
265 #ifdef _DEBUG
266         if (HT_HAS_INTERNAL_KEY(ht))
267         {
268                 ASSERT("parameter cannot be a hash table with internal keys - use ht_insert_with_key()"
269                        && 0);
270                 return false;
271         }
272 #endif
273
274         key = ht->key_data.hook(data, &key_length);
275
276         return insert(ht, key, key_length, data);
277 }
278
279 const void* ht_find(struct HashTable* ht, const void* key, uint8_t key_length)
280 {
281         HashNodePtr node;
282         
283         if (HT_HAS_INTERNAL_KEY(ht))
284                 key_length = MIN(key_length, INTERNAL_KEY_MAX_LENGTH);
285
286         node = perform_lookup(ht, key, key_length);
287
288         if (!node || NODE_EMPTY(node))
289                 return NULL;
290
291         return *node;
292 }
293
294
295 #if 0
296
297 #include <stdlib.h>
298
299 bool ht_test(void);
300
301 static const void* test_get_key(const void* ptr, uint8_t* length)
302 {
303         const char* s = ptr;
304         *length = strlen(s);
305         return s;
306 }
307
308 #define NUM_ELEMENTS   256
309 DECLARE_HASHTABLE_STATIC(test1, 256, test_get_key);
310 DECLARE_HASHTABLE_INTERNALKEY_STATIC(test2, 256);
311
312 static char data[NUM_ELEMENTS][10];
313 static char keydomain[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
314
315 static bool single_test(void)
316 {
317         int i;
318
319         ht_init(&test1);
320         ht_init(&test2);
321
322         for (i=0;i<NUM_ELEMENTS;i++)
323         {
324                 int k;
325                 int klen;
326                 
327                 do 
328                 {
329                         klen = (rand() % 8) + 1;
330                         for (k=0;k<klen;k++)
331                                 data[i][k] = keydomain[rand() % (sizeof(keydomain)-1)];
332                         data[i][k]=0;
333                 } while (ht_find_str(&test1, data[i]) != NULL);
334
335                 ASSERT(ht_insert(&test1, data[i]));
336                 ASSERT(ht_insert_str(&test2, data[i], data[i]));
337         }
338
339         for (i=0;i<NUM_ELEMENTS;i++)
340         {
341                 char *found1, *found2;
342                 
343                 found1 = (char*)ht_find_str(&test1, data[i]);
344                 if (strcmp(found1, data[i]) != 0)
345                 {
346                         ASSERT(strcmp(found1,data[i]) == 0);
347                         return false;
348                 }
349
350                 found2 = (char*)ht_find_str(&test2, data[i]);
351                 if (strcmp(found2, data[i]) != 0)
352                 {
353                         ASSERT(strcmp(found2,data[i]) == 0);
354                         return false;
355                 }
356         }
357
358         return true;
359 }
360
361 static uint16_t rand_seeds[] = { 1, 42, 666, 0xDEAD, 0xBEEF, 0x1337, 0xB00B };
362
363 bool ht_test(void)
364 {
365         int i;
366
367         for (i=0;i<countof(rand_seeds);++i)
368         {
369                 srand(rand_seeds[i]);
370                 if (!single_test())
371                 {
372                         kprintf("ht_test failed\n");
373                         return false;
374                 }
375         }
376
377         kprintf("ht_test successful\n");
378         return true;
379 }
380
381 #endif