1
/*
2
 * Generic hash table implementation.
3
 *
4
 * Copyright (c) 2006  Dustin Sallings <dustin@spy.net>
5
 */
6
7
#ifndef GENHASH_H
8
#define GENHASH_H 1
9
10
/*! \mainpage genhash
11
 *
12
 * \section intro_sec Introduction
13
 *
14
 * genhash is a generic hash table implementation in C.  It's
15
 * well-tested, freely available (MIT-license) and does what you need.
16
 *
17
 * \section docs_sec API Documentation
18
 *
19
 * Jump right into <a href="group___core.html">the API docs</a> to get started.
20
 */
21
22
/**
23
 * \defgroup Core genhash core
24
 */
25
26
/**
27
 * \addtogroup Core
28
 * @{
29
 */
30
31
/**
32
 * Operations on keys and values in the hash table.
33
 */
34
struct hash_ops {
35
    /**
36
     * Function to compute a hash for the given value.
37
     */
38
    int   (*hashfunc)(const void *, size_t);
39
    /**
40
     * Function that returns true if the given keys are equal.
41
     */
42
    int   (*hasheq)(const void *, size_t, const void *, size_t);
43
    /**
44
     * Function to duplicate a key for storage.
45
     */
46
    void* (*dupKey)(const void *, size_t);
47
    /**
48
     * Function to duplicate a value for storage.
49
     */
50
    void* (*dupValue)(const void *, size_t);
51
    /**
52
     * Function to free a key.
53
     */
54
    void  (*freeKey)(void *);
55
    /**
56
     * Function to free a value.
57
     */
58
    void  (*freeValue)(void *);
59
};
60
61
/**
62
 * The hash table structure.
63
 */
64
typedef struct _genhash genhash_t ;
65
66
/**
67
 * Type of update performed by an update function.
68
 */
69
enum update_type {
70
    MODIFICATION, /**< This update is modifying an existing entry */
71
    NEW           /**< This update is creating a new entry */
72
};
73
74
/**
75
 * Create a new generic hashtable.
76
 *
77
 * @param est the estimated number of items to store (must be > 0)
78
 * @param ops the key and value operations
79
 *
80
 * @return the new genhash_t or NULL if one cannot be created
81
 */
82
genhash_t* genhash_init(int est, struct hash_ops ops);
83
84
/**
85
 * Free a gen hash.
86
 *
87
 * @param h the genhash to free (may be NULL)
88
 */
89
void genhash_free(genhash_t *h);
90
91
/**
92
 * Store an item.
93
 *
94
 * @param h the genhash
95
 * @param k the key
96
 * @param v the value
97
 */
98
void genhash_store(genhash_t *h, const void *k, size_t klen,
99
                   const void *v, size_t vlen);
100
101
/**
102
 * Get the most recent value stored for the given key.
103
 *
104
 * @param h the genhash
105
 * @param k the key
106
 *
107
 * @return the value, or NULL if one cannot be found
108
 */
109
void* genhash_find(genhash_t *h, const void *k, size_t klen);
110
111
/**
112
 * Delete the most recent value stored for a key.
113
 *
114
 * @param h the genhash
115
 * @param k the key
116
 *
117
 * @return the number of items deleted
118
 */
119
int genhash_delete(genhash_t *h, const void *k, size_t klen);
120
121
/**
122
 * Delete all mappings of a given key.
123
 *
124
 * @param h the genhash
125
 * @param k the key
126
 *
127
 * @return the number of items deleted
128
 */
129
int genhash_delete_all(genhash_t *h, const void *k, size_t klen);
130
131
/**
132
 * Create or update an item in-place.
133
 *
134
 * @param h the genhash
135
 * @param k the key
136
 * @param v the new value to store for this key
137
 *
138
 * @return an indicator of whether this created a new item or updated
139
 *         an existing one
140
 */
141
enum update_type genhash_update(genhash_t *h, const void *k, size_t klen,
142
                                const void *v, size_t vlen);
143
144
/**
145
 * Create or update an item in-place with a function.
146
 *
147
 * @param h hashtable
148
 * @param key the key of the item
149
 * @param upd function that will be called with the key and current
150
 *        value.  Should return the new value.
151
 * @param fr function to free the return value returned by the update
152
 *        function
153
 * @param def default value
154
 *
155
 * @return an indicator of whether this created a new item or updated
156
 *         an existing one
157
 */
158
enum update_type genhash_fun_update(genhash_t *h, const void *key, size_t klen,
159
                                    void *(*upd)(const void *k, const void *oldv,
160
                                                 size_t *ns, void *a),
161
                                    void (*fr)(void*),
162
                                    void *arg,
163
                                    const void *def, size_t deflen);
164
165
/**
166
 * Iterate all keys and values in a hash table.
167
 *
168
 * @param h the genhash
169
 * @param iterfunc a function that will be called once for every k/v pair
170
 * @param arg an argument to be passed to the iterfunc on each iteration
171
 */
172
void genhash_iter(genhash_t *h,
173
                  void (*iterfunc)(const void* key, size_t nkey,
174
                                   const void* val, size_t nval,
175
                                   void *arg),
176
                  void *arg);
177
178
/**
179
 * Iterate all values for a given key in a hash table.
180
 *
181
 * @param h the genhash
182
 * @param key the key to iterate
183
 * @param iterfunc a function that will be called once for every k/v pair
184
 * @param arg an argument to be passed to the iterfunc on each iteration
185
 */
186
void genhash_iter_key(genhash_t *h, const void* key, size_t nkey,
187
                      void (*iterfunc)(const void* key, size_t inkey,
188
                                       const void* val, size_t inval,
189
                                       void *arg),
190
                      void *arg);
191
192
/**
193
 * Get the total number of entries in this hash table.
194
 *
195
 * @param h the genhash
196
 *
197
 * @return the number of entries in the hash table
198
 */
199
int genhash_size(genhash_t *h);
200
201
/**
202
 * Remove all items from a genhash.
203
 *
204
 * @param h the genhash
205
 *
206
 * @return the number of items removed
207
 */
208
int genhash_clear(genhash_t *h);
209
210
/**
211
 * Get the total number of entries in this hash table that map to the given
212
 * key.
213
 *
214
 * @param h the genhash
215
 * @param k a key
216
 *
217
 * @return the number of entries keyed with the given key
218
 */
219
int genhash_size_for_key(genhash_t *h, const void *k, size_t nkey);
220
221
/**
222
 * Convenient hash function for strings.
223
 *
224
 * @param k a null-terminated string key.
225
 *
226
 * @return a hash value for this string.
227
 */
228
int genhash_string_hash(const void *k, size_t nkey);
229
230
/**
231
 * @}
232
 */
233
234
#endif /* GENHASH_H */