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
27 /* A type for the hash function. This function is used to hash the
28    provided key value `key' and return the index for the hash table. */
29 typedef uint32 (*SilcHashFunction)(void *key, void *user_context);
30
31 /* A comparison funtion that is called to compare the two keys `key1' and
32    `key2'. If they are equal this must return TRUE or FALSE otherwise.
33    The application provides this function when allocating a new hash table. */
34 typedef bool (*SilcHashCompare)(void *key1, void *key2, void *user_context);
35
36 /* A destructor callback that the library will call to destroy the 
37    `key' and `context'.  The appliation provides the function when
38    allocating a new hash table. */
39 typedef void (*SilcHashDestructor)(void *key, void *context, 
40                                    void *user_context);
41
42 /* Foreach function. This is called when traversing the entrys in the
43    hash table using silc_hash_table_foreach. */
44 typedef void (*SilcHashForeach)(void *key, void *context, void *user_context);
45
46 /* Simple hash table interface */
47
48 SilcHashTable silc_hash_table_alloc(uint32 table_size, 
49                                     SilcHashFunction hash,
50                                     void *hash_user_context,
51                                     SilcHashCompare compare,
52                                     void *compare_user_context,
53                                     SilcHashDestructor destructor,
54                                     void *destructor_user_context);
55 void silc_hash_table_free(SilcHashTable ht);
56 uint32 silc_hash_table_size(SilcHashTable ht);
57 uint32 silc_hash_table_count(SilcHashTable ht);
58 void silc_hash_table_add(SilcHashTable ht, void *key, void *context);
59 void silc_hash_table_replace(SilcHashTable ht, void *key, void *context);
60 bool silc_hash_table_del(SilcHashTable ht, void *key);
61 bool silc_hash_table_del_by_context(SilcHashTable ht, void *key, 
62                                     void *context);
63 bool silc_hash_table_find(SilcHashTable ht, void *key,
64                           void **ret_key, void **ret_context);
65 void silc_hash_table_find_foreach(SilcHashTable ht, void *key,
66                                   SilcHashForeach foreach, void *user_context);
67 void silc_hash_table_foreach(SilcHashTable ht, SilcHashForeach foreach,
68                              void *user_context);
69 void silc_hash_table_rehash(SilcHashTable ht, uint32 new_size);
70
71
72 /* Extended hash table interface (same as above but with specific
73    hash and comparison functions). */
74
75 void silc_hash_table_add_ext(SilcHashTable ht, void *key, void *context,
76                              SilcHashFunction hash, void *hash_user_context);
77 void silc_hash_table_replace_ext(SilcHashTable ht, void *key, void *context,
78                                  SilcHashFunction hash, 
79                                  void *hash_user_context);
80 bool silc_hash_table_del_ext(SilcHashTable ht, void *key,
81                              SilcHashFunction hash, 
82                              void *hash_user_context,
83                              SilcHashCompare compare, 
84                              void *compare_user_context);
85 bool silc_hash_table_del_by_context_ext(SilcHashTable ht, void *key, 
86                                         void *context,
87                                         SilcHashFunction hash, 
88                                         void *hash_user_context,
89                                         SilcHashCompare compare, 
90                                         void *compare_user_context);
91 bool silc_hash_table_find_ext(SilcHashTable ht, void *key,
92                               void **ret_key, void **ret_context,
93                               SilcHashFunction hash, 
94                               void *hash_user_context,
95                               SilcHashCompare compare, 
96                               void *compare_user_context);
97 void silc_hash_table_find_foreach_ext(SilcHashTable ht, void *key,
98                                       SilcHashFunction hash, 
99                                       void *hash_user_context,
100                                       SilcHashCompare compare, 
101                                       void *compare_user_context,
102                                       SilcHashForeach foreach, 
103                                       void *foreach_user_context);
104
105 #endif