updates.
[silc.git] / lib / silcutil / silchashtable.h
1 /*
2
3   silchashtable.h
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 2001 Pekka Riikonen
8
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.
13   
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.
18
19 */
20
21 #ifndef SILCHASHTABLE_H
22 #define SILCHASHTABLE_H
23
24 /* Forward declarations */
25 typedef struct SilcHashTableStruct *SilcHashTable;
26 typedef struct SilcHashTableListStruct SilcHashTableList;
27
28 /* List structure to traverse the hash table. */
29 struct SilcHashTableListStruct {
30   SilcHashTable ht;
31   void *entry;
32   uint32 index;
33 };
34
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);
38
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);
43
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, 
48                                    void *user_context);
49
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);
53
54 /* Simple hash table interface */
55
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,
63                                     bool auto_rehash);
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, 
71                                     void *context);
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,
77                              void *user_context);
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);
81
82
83 /* Extended hash table interface (same as above but with specific
84    hash and comparison functions). */
85
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, 
97                                         void *context,
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);
118
119 #endif