5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2001 - 2005 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
19 /* Implementation of collision resistant hash table. This is a hash table
20 that provides a reliable (what you add stays there, and duplicate keys
21 are allowed) with as fast reference to the key as possible. If duplicate
22 keys are a lot in the hash table the lookup gets slower of course.
23 However, this is reliable and no data is lost at any point. If you know
24 that you never have duplicate keys then this is as fast as any simple
28 #include "silcincludes.h"
29 #include "silchashtable.h"
31 /* Define to 1 if you want hash table debug enabled */
32 #define SILC_HASH_TABLE_DEBUG 0
34 #if SILC_HASH_TABLE_DEBUG == 1
35 #define SILC_HT_DEBUG(fmt) SILC_LOG_DEBUG(fmt)
37 #define SILC_HT_DEBUG(fmt)
40 /* Default size of the hash table (index to prime table) */
41 #define SILC_HASH_TABLE_SIZE 2
43 /* Produce the index by hashing the key */
44 #define SILC_HASH_TABLE_HASH(f, c) \
45 ((f)(key, (c)) % primesize[ht->table_size])
47 /* Check whether need to rehash */
48 #define SILC_HASH_REHASH_INC \
49 (ht->auto_rehash && (ht->entry_count / 2) > primesize[ht->table_size])
50 #define SILC_HASH_REHASH_DEC \
51 (ht->auto_rehash && (ht->entry_count * 2) < primesize[ht->table_size] && \
52 ht->entry_count > primesize[SILC_HASH_TABLE_SIZE])
54 /* One entry in the hash table. Includes the key and the associated
55 context. The `next' pointer is non-NULL if two (or more) different
56 keys hashed to same value. The pointer is the pointer to the next
58 typedef struct SilcHashTableEntryStruct {
61 struct SilcHashTableEntryStruct *next;
62 } *SilcHashTableEntry;
65 struct SilcHashTableStruct {
66 SilcHashTableEntry *table;
67 SilcUInt32 table_size;
68 SilcUInt32 entry_count;
69 SilcHashFunction hash;
70 SilcHashCompare compare;
71 SilcHashDestructor destructor;
72 void *hash_user_context;
73 void *compare_user_context;
74 void *destructor_user_context;
75 unsigned int auto_rehash : 1;
78 /* Prime sizes for the hash table. The size of the table will always
80 const SilcUInt32 primesize[] =
82 3, 5, 11, 17, 37, 67, 109, 131, 163, 257, 367, 521, 823, 1031,
83 1237, 1447, 2053, 2389, 2777, 3323, 4099, 5059, 6247, 7001, 8209, 10993,
84 14057, 16411, 19181, 21089, 25033, 32771, 40009, 47431, 65537, 106721,
85 131101, 262147, 360163, 524309, 810343, 1048583, 2097169, 4194319,
86 6153409, 8388617, 13845163, 16777259, 33554467, 67108879
89 /* Find appropriate size for the hash table. The size will be a prime. */
91 static SilcUInt32 silc_hash_table_primesize(SilcUInt32 size, SilcUInt32 *index)
95 for (i = 0; i < sizeof(primesize) / sizeof(primesize[0]); i++)
96 if (primesize[i] >= size) {
98 SILC_HT_DEBUG(("sizeof of the hash table is %d", primesize[*index]));
103 SILC_HT_DEBUG(("sizeof of the hash table is %d", primesize[*index]));
104 return primesize[i - 1];
107 /* Internal routine to find entry in the hash table by `key'. Returns
108 the previous entry (if exists) as well. */
110 static inline SilcHashTableEntry *
111 silc_hash_table_find_internal(SilcHashTable ht, void *key,
112 SilcHashTableEntry *prev_entry,
113 SilcHashFunction hash, void *hash_user_context,
114 SilcHashCompare compare,
115 void *compare_user_context)
117 SilcHashTableEntry *entry, prev = NULL;
118 SilcUInt32 i = SILC_HASH_TABLE_HASH(hash, hash_user_context);
120 SILC_HT_DEBUG(("index %d key %p", i, key));
122 entry = &ht->table[i];
124 while (*entry && !compare((*entry)->key, key, compare_user_context)) {
126 entry = &(*entry)->next;
129 while (*entry && (*entry)->key != key) {
131 entry = &(*entry)->next;
139 /* Internal routine to find entry in the hash table by `key' and `context'.
140 Returns the previous entry (if exists) as well to `prev_entry'. */
142 static inline SilcHashTableEntry *
143 silc_hash_table_find_internal_context(SilcHashTable ht, void *key,
145 SilcHashTableEntry *prev_entry,
146 SilcHashFunction hash,
147 void *hash_user_context,
148 SilcHashCompare compare,
149 void *compare_user_context)
151 SilcHashTableEntry *entry, prev = NULL;
152 SilcUInt32 i = SILC_HASH_TABLE_HASH(hash, hash_user_context);
154 SILC_HT_DEBUG(("index %d key %p context %p", i, key, context));
156 entry = &ht->table[i];
159 if (compare((*entry)->key, key, compare_user_context) &&
160 (*entry)->context == context)
163 entry = &(*entry)->next;
167 if ((*entry)->key == key && (*entry)->context == context)
170 entry = &(*entry)->next;
179 /* Internal routine to find entry in the hash table by `key'. */
181 static inline SilcHashTableEntry *
182 silc_hash_table_find_internal_simple(SilcHashTable ht, void *key,
183 SilcHashFunction hash,
184 void *hash_user_context,
185 SilcHashCompare compare,
186 void *compare_user_context)
188 SilcHashTableEntry *entry;
189 SilcUInt32 i = SILC_HASH_TABLE_HASH(hash, hash_user_context);
191 SILC_HT_DEBUG(("index %d key %p", i, key));
193 entry = &ht->table[i];
195 while (*entry && !compare((*entry)->key, key, compare_user_context))
196 entry = &(*entry)->next;
198 while (*entry && (*entry)->key != key)
199 entry = &(*entry)->next;
205 /* Internal routine to find all keys by `key'. This may return multiple
206 entries if multiple entries with same key exists. With specific
207 hash and comparison functions. */
210 silc_hash_table_find_internal_all(SilcHashTable ht, void *key,
211 SilcHashFunction hash,
212 void *hash_user_context,
213 SilcHashCompare compare,
214 void *compare_user_context,
215 SilcHashForeach foreach,
216 void *foreach_user_context)
218 SilcHashTableEntry e, tmp;
219 bool auto_rehash, found = FALSE;
220 SilcUInt32 i = SILC_HASH_TABLE_HASH(hash, hash_user_context);
222 SILC_HT_DEBUG(("index %d key %p", i, key));
224 /* Disallow auto rehashing while going through the table since we call
225 the `foreach' function which could alter the table. */
226 auto_rehash = ht->auto_rehash;
227 ht->auto_rehash = FALSE;
233 if (compare(e->key, key, compare_user_context)) {
235 foreach(e->key, e->context, foreach_user_context);
244 foreach(e->key, e->context, foreach_user_context);
250 /* If nothing was found call with NULL context the callback */
252 foreach(key, NULL, foreach_user_context);
254 ht->auto_rehash = auto_rehash;
257 /* Internal routine to add new key to the hash table */
260 silc_hash_table_add_internal(SilcHashTable ht, void *key, void *context,
261 SilcHashFunction hash,
262 void *hash_user_context)
264 SilcHashTableEntry *entry;
265 SilcUInt32 i = SILC_HASH_TABLE_HASH(hash, hash_user_context);
267 SILC_HT_DEBUG(("index %d key %p", i, key));
269 entry = &ht->table[i];
271 /* The entry exists already. We have a collision, add it to the
272 list to avoid collision. */
273 SilcHashTableEntry e, tmp;
282 SILC_HT_DEBUG(("Collision; adding new key to list"));
284 e->next = silc_calloc(1, sizeof(*e->next));
288 e->next->context = context;
292 SILC_HT_DEBUG(("New key"));
293 *entry = silc_calloc(1, sizeof(**entry));
297 (*entry)->context = context;
301 if (SILC_HASH_REHASH_INC)
302 silc_hash_table_rehash(ht, 0);
307 /* Internal routine to replace old key with new one (if it exists) */
310 silc_hash_table_replace_internal(SilcHashTable ht, void *key, void *context,
311 SilcHashFunction hash,
312 void *hash_user_context)
314 SilcHashTableEntry *entry;
315 SilcUInt32 i = SILC_HASH_TABLE_HASH(hash, hash_user_context);
317 SILC_HT_DEBUG(("index %d key %p", i, key));
319 entry = &ht->table[i];
321 /* The entry exists already. We have a collision, replace the old
324 ht->destructor((*entry)->key, (*entry)->context,
325 ht->destructor_user_context);
328 *entry = silc_calloc(1, sizeof(**entry));
335 (*entry)->context = context;
337 if (SILC_HASH_REHASH_INC)
338 silc_hash_table_rehash(ht, 0);
343 /* Allocates new hash table and returns it. If the `table_size' is not
344 zero then the hash table size is the size provided. If zero then the
345 default size will be used. Note that if the `table_size' is provided
346 it should be a prime. The `hash', `compare' and `destructor' are
347 the hash function, the key comparison function and key and context
348 destructor function, respectively. The `hash' is mandatory, the others
351 SilcHashTable silc_hash_table_alloc(SilcUInt32 table_size,
352 SilcHashFunction hash,
353 void *hash_user_context,
354 SilcHashCompare compare,
355 void *compare_user_context,
356 SilcHashDestructor destructor,
357 void *destructor_user_context,
361 SilcUInt32 size_index = SILC_HASH_TABLE_SIZE;
366 ht = silc_calloc(1, sizeof(*ht));
369 ht->table = silc_calloc(table_size ? silc_hash_table_primesize(table_size,
371 primesize[SILC_HASH_TABLE_SIZE],
377 ht->table_size = size_index;
379 ht->compare = compare;
380 ht->destructor = destructor;
381 ht->hash_user_context = hash_user_context;
382 ht->compare_user_context = compare_user_context;
383 ht->destructor_user_context = destructor_user_context;
384 ht->auto_rehash = auto_rehash;
389 /* Frees the hash table. The destructor function provided in the
390 silc_hash_table_alloc will be called for all keys in the hash table. */
392 void silc_hash_table_free(SilcHashTable ht)
394 SilcHashTableEntry e, tmp;
397 for (i = 0; i < primesize[ht->table_size]; i++) {
401 ht->destructor(e->key, e->context, ht->destructor_user_context);
408 silc_free(ht->table);
412 /* Returns the size of the hash table */
414 SilcUInt32 silc_hash_table_size(SilcHashTable ht)
416 return primesize[ht->table_size];
419 /* Returns the number of the entires in the hash table. If there is more
420 entries in the table thatn the size of the hash table calling the
421 silc_hash_table_rehash is recommended. */
423 SilcUInt32 silc_hash_table_count(SilcHashTable ht)
425 return ht->entry_count;
428 /* Adds new entry to the hash table. The `key' is hashed using the
429 hash function and the both `key' and `context' will be saved to the
430 hash table. This function quarantees that the entry is always added
431 to the hash table reliably (it is collision resistant). */
433 void silc_hash_table_add(SilcHashTable ht, void *key, void *context)
435 silc_hash_table_add_internal(ht, key, context, ht->hash,
436 ht->hash_user_context);
439 /* Same as above but with specific hash function and user context. */
441 void silc_hash_table_add_ext(SilcHashTable ht, void *key, void *context,
442 SilcHashFunction hash, void *hash_user_context)
444 silc_hash_table_add_internal(ht, key, context, hash, hash_user_context);
447 /* Same as above but if the `key' already exists in the hash table
448 the old key and the old context will be replace with the `key' and
449 the `context. The destructor function will be called for the
450 replaced key and context. */
452 void silc_hash_table_replace(SilcHashTable ht, void *key, void *context)
454 silc_hash_table_replace_internal(ht, key, context, ht->hash,
455 ht->hash_user_context);
458 /* Same as above but with specific hash function. */
460 void silc_hash_table_replace_ext(SilcHashTable ht, void *key, void *context,
461 SilcHashFunction hash,
462 void *hash_user_context)
464 silc_hash_table_replace_internal(ht, key, context, hash, hash_user_context);
467 /* Removes the entry from the hash table by the provided `key'. This will
468 call the destructor funtion for the found entry. Return TRUE if the
469 entry was removed successfully and FALSE otherwise. */
471 bool silc_hash_table_del(SilcHashTable ht, void *key)
473 SilcHashTableEntry *entry, prev, e;
475 entry = silc_hash_table_find_internal(ht, key, &prev,
476 ht->hash, ht->hash_user_context,
477 ht->compare, ht->compare_user_context);
483 if (!prev && e->next)
485 if (!prev && e->next == NULL)
490 prev->next = e->next;
493 ht->destructor(e->key, e->context, ht->destructor_user_context);
498 if (SILC_HASH_REHASH_DEC)
499 silc_hash_table_rehash(ht, 0);
504 /* Same as above but with specific hash and compare functions. */
506 bool silc_hash_table_del_ext(SilcHashTable ht, void *key,
507 SilcHashFunction hash,
508 void *hash_user_context,
509 SilcHashCompare compare,
510 void *compare_user_context,
511 SilcHashDestructor destructor,
512 void *destructor_user_context)
514 SilcHashTableEntry *entry, prev, e;
516 entry = silc_hash_table_find_internal(ht, key, &prev,
517 hash ? hash : ht->hash,
518 hash_user_context ? hash_user_context :
519 ht->hash_user_context,
520 compare ? compare : ht->compare,
521 compare_user_context ?
522 compare_user_context :
523 ht->compare_user_context);
529 if (!prev && e->next)
531 if (!prev && e->next == NULL)
536 prev->next = e->next;
539 destructor(e->key, e->context, destructor_user_context);
542 ht->destructor(e->key, e->context, ht->destructor_user_context);
548 if (SILC_HASH_REHASH_DEC)
549 silc_hash_table_rehash(ht, 0);
554 /* Same as above but verifies that the context associated with the `key'
555 matches the `context'. This is handy to use with hash tables that may
556 have duplicate keys. In that case the `context' may be used to check
557 whether the correct entry is being deleted. */
559 bool silc_hash_table_del_by_context(SilcHashTable ht, void *key,
562 SilcHashTableEntry *entry, prev, e;
564 entry = silc_hash_table_find_internal_context(ht, key, context, &prev,
566 ht->hash_user_context,
568 ht->compare_user_context);
574 if (!prev && e->next)
576 if (!prev && e->next == NULL)
581 prev->next = e->next;
584 ht->destructor(e->key, e->context, ht->destructor_user_context);
589 if (SILC_HASH_REHASH_DEC)
590 silc_hash_table_rehash(ht, 0);
595 /* Same as above but with specific hash and compare functions. */
597 bool silc_hash_table_del_by_context_ext(SilcHashTable ht, void *key,
599 SilcHashFunction hash,
600 void *hash_user_context,
601 SilcHashCompare compare,
602 void *compare_user_context,
603 SilcHashDestructor destructor,
604 void *destructor_user_context)
606 SilcHashTableEntry *entry, prev, e;
608 entry = silc_hash_table_find_internal_context(ht, key, context, &prev,
609 hash ? hash : ht->hash,
612 ht->hash_user_context,
614 compare : ht->compare,
615 compare_user_context ?
616 compare_user_context :
617 ht->compare_user_context);
623 if (!prev && e->next)
625 if (!prev && e->next == NULL)
630 prev->next = e->next;
633 destructor(e->key, e->context, destructor_user_context);
636 ht->destructor(e->key, e->context, ht->destructor_user_context);
642 if (SILC_HASH_REHASH_DEC)
643 silc_hash_table_rehash(ht, 0);
648 /* Finds the entry in the hash table by the provided `key' as fast as
649 possible. Return TRUE if the entry was found and FALSE otherwise.
650 The found entry is returned to the `ret_key' and `ret_context',
651 respectively. If the `ret_key and `ret_context' are NULL then this
652 maybe used only to check whether given key exists in the table. */
654 bool silc_hash_table_find(SilcHashTable ht, void *key,
655 void **ret_key, void **ret_context)
657 return silc_hash_table_find_ext(ht, key, ret_key, ret_context,
658 NULL, NULL, NULL, NULL);
661 /* Same as above but with specified hash and comparison functions. */
663 bool silc_hash_table_find_ext(SilcHashTable ht, void *key,
664 void **ret_key, void **ret_context,
665 SilcHashFunction hash,
666 void *hash_user_context,
667 SilcHashCompare compare,
668 void *compare_user_context)
670 SilcHashTableEntry *entry;
672 entry = silc_hash_table_find_internal_simple(ht, key,
673 hash ? hash : ht->hash,
676 ht->hash_user_context,
679 compare_user_context ?
680 compare_user_context :
681 ht->compare_user_context);
686 *ret_key = (*entry)->key;
688 *ret_context = (*entry)->context;
693 /* Same as silc_hash_table_find but finds with specific context. */
695 bool silc_hash_table_find_by_context(SilcHashTable ht, void *key,
696 void *context, void **ret_key)
698 return silc_hash_table_find_by_context_ext(ht, key, context, ret_key,
699 NULL, NULL, NULL, NULL);
702 /* Same as above but with specified hash and comparison functions. */
704 bool silc_hash_table_find_by_context_ext(SilcHashTable ht, void *key,
705 void *context, void **ret_key,
706 SilcHashFunction hash,
707 void *hash_user_context,
708 SilcHashCompare compare,
709 void *compare_user_context)
711 SilcHashTableEntry *entry;
713 entry = silc_hash_table_find_internal_context(ht, key, context, NULL,
714 hash ? hash : ht->hash,
717 ht->hash_user_context,
720 compare_user_context ?
721 compare_user_context :
722 ht->compare_user_context);
723 if (!entry || !(*entry))
727 *ret_key = (*entry)->key;
732 /* As the hash table is collision resistant it is possible to save duplicate
733 keys to the hash table. This function can be used to find all keys
734 and contexts from the hash table that are found using the `key'. The
735 `foreach' is called for every found key. */
737 void silc_hash_table_find_foreach(SilcHashTable ht, void *key,
738 SilcHashForeach foreach, void *user_context)
740 silc_hash_table_find_internal_all(ht, key, ht->hash, ht->hash_user_context,
741 ht->compare, ht->compare_user_context,
742 foreach, user_context);
745 /* Same as above but with specific hash and comparison functions. */
747 void silc_hash_table_find_foreach_ext(SilcHashTable ht, void *key,
748 SilcHashFunction hash,
749 void *hash_user_context,
750 SilcHashCompare compare,
751 void *compare_user_context,
752 SilcHashForeach foreach,
753 void *foreach_user_context)
755 silc_hash_table_find_internal_all(ht, key,
756 hash ? hash : ht->hash,
759 ht->hash_user_context,
762 compare_user_context ?
763 compare_user_context :
764 ht->compare_user_context,
765 foreach, foreach_user_context);
768 /* Traverse all entrys in the hash table and call the `foreach' for
769 every entry with the `user_context' context. */
771 void silc_hash_table_foreach(SilcHashTable ht, SilcHashForeach foreach,
774 SilcHashTableEntry e, tmp;
781 auto_rehash = ht->auto_rehash;
782 ht->auto_rehash = FALSE;
783 for (i = 0; i < primesize[ht->table_size]; i++) {
786 /* Entry may become invalid inside the `foreach' */
788 foreach(e->key, e->context, user_context);
792 ht->auto_rehash = auto_rehash;
795 /* Rehashs the hash table. The size of the new hash table is provided
796 as `new_size'. If the `new_size' is zero then this routine will make
797 the new table of a suitable size. Note that this operation may be
800 void silc_hash_table_rehash(SilcHashTable ht, SilcUInt32 new_size)
803 SilcHashTableEntry *table, e, tmp;
804 SilcUInt32 table_size, size_index;
807 SILC_HT_DEBUG(("Start"));
810 silc_hash_table_primesize(new_size, &size_index);
812 silc_hash_table_primesize(ht->entry_count, &size_index);
814 if (size_index == ht->table_size)
817 SILC_HT_DEBUG(("Rehashing"));
819 /* Take old hash table */
821 table_size = ht->table_size;
822 auto_rehash = ht->auto_rehash;
823 ht->auto_rehash = FALSE;
825 /* Allocate new table */
826 ht->table = silc_calloc(primesize[size_index], sizeof(*ht->table));
829 ht->table_size = size_index;
833 for (i = 0; i < primesize[table_size]; i++) {
836 silc_hash_table_add(ht, e->key, e->context);
840 /* Remove old entry */
845 ht->auto_rehash = auto_rehash;
847 /* Remove old table */
851 /* Same as above but with specific hash function. */
853 void silc_hash_table_rehash_ext(SilcHashTable ht, SilcUInt32 new_size,
854 SilcHashFunction hash,
855 void *hash_user_context)
858 SilcHashTableEntry *table, e, tmp;
859 SilcUInt32 table_size, size_index;
862 SILC_HT_DEBUG(("Start"));
865 silc_hash_table_primesize(new_size, &size_index);
867 silc_hash_table_primesize(ht->entry_count, &size_index);
869 if (size_index == ht->table_size)
872 SILC_HT_DEBUG(("Rehashing"));
874 /* Take old hash table */
876 table_size = ht->table_size;
877 auto_rehash = ht->auto_rehash;
878 ht->auto_rehash = FALSE;
880 /* Allocate new table */
881 ht->table = silc_calloc(primesize[size_index], sizeof(*ht->table));
884 ht->table_size = size_index;
888 for (i = 0; i < primesize[table_size]; i++) {
891 silc_hash_table_add_ext(ht, e->key, e->context, hash,
896 /* Remove old entry */
901 ht->auto_rehash = auto_rehash;
903 /* Remove old table */
907 /* Prepares the `htl' list structure sent as argument to be used in the
908 hash table traversing with the silc_hash_table_get. Usage:
909 SilcHashTableList htl; silc_hash_table_list(ht, &htl); */
911 void silc_hash_table_list(SilcHashTable ht, SilcHashTableList *htl)
916 htl->auto_rehash = ht->auto_rehash;
918 /* Disallow rehashing of the table while traversing the table */
919 ht->auto_rehash = FALSE;
922 /* Resets the `htl' SilcHashTableList. */
924 void silc_hash_table_list_reset(SilcHashTableList *htl)
926 /* Set back the original auto rehash value to the table */
927 htl->ht->auto_rehash = htl->auto_rehash;
930 /* Returns always the next entry in the hash table into the `key' and
931 `context' and TRUE. If this returns FALSE then there are no anymore
932 any entrys. Usage: while (silc_hash_table_get(&htl, &key, &context)) */
934 bool silc_hash_table_get(SilcHashTableList *htl, void **key, void **context)
936 SilcHashTableEntry entry = (SilcHashTableEntry)htl->entry;
938 if (!htl->ht->entry_count)
941 while (!entry && htl->index < primesize[htl->ht->table_size]) {
942 entry = htl->ht->table[htl->index];
949 htl->entry = entry->next;
954 *context = entry->context;