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