5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 2001 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; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 #ifndef SILCHASHTABLE_H
22 #define SILCHASHTABLE_H
24 /* Forward declarations */
25 typedef struct SilcHashTableStruct *SilcHashTable;
26 typedef struct SilcHashTableListStruct SilcHashTableList;
28 /* List structure to traverse the hash table. */
29 struct SilcHashTableListStruct {
35 /* A type for the hash function. This function is used to hash the
36 provided key value `key' and return the index for the hash table. */
37 typedef uint32 (*SilcHashFunction)(void *key, void *user_context);
39 /* A comparison funtion that is called to compare the two keys `key1' and
40 `key2'. If they are equal this must return TRUE or FALSE otherwise.
41 The application provides this function when allocating a new hash table. */
42 typedef bool (*SilcHashCompare)(void *key1, void *key2, void *user_context);
44 /* A destructor callback that the library will call to destroy the
45 `key' and `context'. The appliation provides the function when
46 allocating a new hash table. */
47 typedef void (*SilcHashDestructor)(void *key, void *context,
50 /* Foreach function. This is called when traversing the entrys in the
51 hash table using silc_hash_table_foreach. */
52 typedef void (*SilcHashForeach)(void *key, void *context, void *user_context);
54 /* Simple hash table interface */
56 SilcHashTable silc_hash_table_alloc(uint32 table_size,
57 SilcHashFunction hash,
58 void *hash_user_context,
59 SilcHashCompare compare,
60 void *compare_user_context,
61 SilcHashDestructor destructor,
62 void *destructor_user_context,
64 void silc_hash_table_free(SilcHashTable ht);
65 uint32 silc_hash_table_size(SilcHashTable ht);
66 uint32 silc_hash_table_count(SilcHashTable ht);
67 void silc_hash_table_add(SilcHashTable ht, void *key, void *context);
68 void silc_hash_table_replace(SilcHashTable ht, void *key, void *context);
69 bool silc_hash_table_del(SilcHashTable ht, void *key);
70 bool silc_hash_table_del_by_context(SilcHashTable ht, void *key,
72 bool silc_hash_table_find(SilcHashTable ht, void *key,
73 void **ret_key, void **ret_context);
74 void silc_hash_table_find_foreach(SilcHashTable ht, void *key,
75 SilcHashForeach foreach, void *user_context);
76 void silc_hash_table_foreach(SilcHashTable ht, SilcHashForeach foreach,
78 void silc_hash_table_rehash(SilcHashTable ht, uint32 new_size);
79 void silc_hash_table_list(SilcHashTable ht, SilcHashTableList *htl);
80 bool silc_hash_table_get(SilcHashTableList *htl, void **key, void **context);
83 /* Extended hash table interface (same as above but with specific
84 hash and comparison functions). */
86 void silc_hash_table_add_ext(SilcHashTable ht, void *key, void *context,
87 SilcHashFunction hash, void *hash_user_context);
88 void silc_hash_table_replace_ext(SilcHashTable ht, void *key, void *context,
89 SilcHashFunction hash,
90 void *hash_user_context);
91 bool silc_hash_table_del_ext(SilcHashTable ht, void *key,
92 SilcHashFunction hash,
93 void *hash_user_context,
94 SilcHashCompare compare,
95 void *compare_user_context);
96 bool silc_hash_table_del_by_context_ext(SilcHashTable ht, void *key,
98 SilcHashFunction hash,
99 void *hash_user_context,
100 SilcHashCompare compare,
101 void *compare_user_context);
102 bool silc_hash_table_find_ext(SilcHashTable ht, void *key,
103 void **ret_key, void **ret_context,
104 SilcHashFunction hash,
105 void *hash_user_context,
106 SilcHashCompare compare,
107 void *compare_user_context);
108 void silc_hash_table_find_foreach_ext(SilcHashTable ht, void *key,
109 SilcHashFunction hash,
110 void *hash_user_context,
111 SilcHashCompare compare,
112 void *compare_user_context,
113 SilcHashForeach foreach,
114 void *foreach_user_context);
115 void silc_hash_table_rehash_ext(SilcHashTable ht, uint32 new_size,
116 SilcHashFunction hash,
117 void *hash_user_context);