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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
/*
* Copyright © 2008 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/**
* \file hash_table.h
* \brief Implementation of a generic, opaque hash table data type.
*
* \author Ian Romanick <ian.d.romanick@intel.com>
*/
#ifndef HASH_TABLE_H
#define HASH_TABLE_H
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <assert.h>
struct string_to_uint_map;
#ifdef __cplusplus
extern "C" {
#endif
struct hash_table;
typedef unsigned (*hash_func_t)(const void *key);
typedef bool (*hash_compare_func_t)(const void *key1, const void *key2);
/**
* Hash table constructor
*
* Creates a hash table with the specified number of buckets. The supplied
* \c hash and \c compare routines are used when adding elements to the table
* and when searching for elements in the table.
*
* \param num_buckets Number of buckets (bins) in the hash table.
* \param hash Function used to compute hash value of input keys.
* \param compare Function used to compare keys.
*/
extern struct hash_table *hash_table_ctor(unsigned num_buckets,
hash_func_t hash, hash_compare_func_t compare);
/**
* Release all memory associated with a hash table
*
* \warning
* This function cannot release memory occupied either by keys or data.
*/
extern void hash_table_dtor(struct hash_table *ht);
/**
* Flush all entries from a hash table
*
* \param ht Table to be cleared of its entries.
*/
extern void hash_table_clear(struct hash_table *ht);
/**
* Search a hash table for a specific element
*
* \param ht Table to be searched
* \param key Key of the desired element
*
* \return
* The \c data value supplied to \c hash_table_insert when the element with
* the matching key was added. If no matching key exists in the table,
* \c NULL is returned.
*/
extern void *hash_table_find(struct hash_table *ht, const void *key);
/**
* Add an element to a hash table
*
* \warning
* If \c key is already in the hash table, it will be added again. Future
* calls to \c hash_table_find and \c hash_table_remove will return or remove,
* repsectively, the most recently added instance of \c key.
*
* \warning
* The value passed by \c key is kept in the hash table and is used by later
* calls to \c hash_table_find.
*
* \sa hash_table_replace
*/
extern void hash_table_insert(struct hash_table *ht, void *data,
const void *key);
/**
* Add an element to a hash table with replacement
*
* \return
* 1 if it did replace the value (in which case the old key is kept), 0 if it
* did not replace the value (in which case the new key is kept).
*
* \warning
* If \c key is already in the hash table, \c data will \b replace the most
* recently inserted \c data (see the warning in \c hash_table_insert) for
* that key.
*
* \sa hash_table_insert
*/
extern bool hash_table_replace(struct hash_table *ht, void *data,
const void *key);
/**
* Remove a specific element from a hash table.
*/
extern void hash_table_remove(struct hash_table *ht, const void *key);
/**
* Compute hash value of a string
*
* Computes the hash value of a string using the DJB2 algorithm developed by
* Professor Daniel J. Bernstein. It was published on comp.lang.c once upon
* a time. I was unable to find the original posting in the archives.
*
* \param key Pointer to a NUL terminated string to be hashed.
*
* \sa hash_table_string_compare
*/
extern unsigned hash_table_string_hash(const void *key);
/**
* Compare two strings used as keys
*
* This is just a wrapper around \c strcmp.
*
* \sa hash_table_string_hash
*/
bool hash_table_string_compare(const void *a, const void *b);
/**
* Compute hash value of a pointer
*
* \param key Pointer to be used as a hash key
*
* \note
* The memory pointed to by \c key is \b never accessed. The value of \c key
* itself is used as the hash key
*
* \sa hash_table_pointer_compare
*/
unsigned
hash_table_pointer_hash(const void *key);
/**
* Compare two pointers used as keys
*
* \sa hash_table_pointer_hash
*/
bool
hash_table_pointer_compare(const void *key1, const void *key2);
void
hash_table_call_foreach(struct hash_table *ht,
void (*callback)(const void *key,
void *data,
void *closure),
void *closure);
struct string_to_uint_map *
string_to_uint_map_ctor();
void
string_to_uint_map_dtor(struct string_to_uint_map *);
#ifdef __cplusplus
}
struct string_map_iterate_wrapper_closure {
void (*callback)(const char *key, unsigned value, void *closure);
void *closure;
};
/**
* Map from a string (name) to an unsigned integer value
*
* \note
* Because of the way this class interacts with the \c hash_table
* implementation, values of \c UINT_MAX cannot be stored in the map.
*/
struct string_to_uint_map {
public:
string_to_uint_map()
{
this->ht = hash_table_ctor(0, hash_table_string_hash,
hash_table_string_compare);
}
~string_to_uint_map()
{
hash_table_call_foreach(this->ht, delete_key, NULL);
hash_table_dtor(this->ht);
}
/**
* Remove all mappings from this map.
*/
void clear()
{
hash_table_call_foreach(this->ht, delete_key, NULL);
hash_table_clear(this->ht);
}
/**
* Runs a passed callback for the hash
*/
void iterate(void (*func)(const char *, unsigned, void *), void *closure)
{
struct string_map_iterate_wrapper_closure *wrapper;
wrapper = (struct string_map_iterate_wrapper_closure *)
malloc(sizeof(struct string_map_iterate_wrapper_closure));
if (wrapper == NULL)
return;
wrapper->callback = func;
wrapper->closure = closure;
hash_table_call_foreach(this->ht, subtract_one_wrapper, wrapper);
free(wrapper);
}
/**
* Get the value associated with a particular key
*
* \return
* If \c key is found in the map, \c true is returned. Otherwise \c false
* is returned.
*
* \note
* If \c key is not found in the table, \c value is not modified.
*/
bool get(unsigned &value, const char *key)
{
const intptr_t v =
(intptr_t) hash_table_find(this->ht, (const void *) key);
if (v == 0)
return false;
value = (unsigned)(v - 1);
return true;
}
void put(unsigned value, const char *key)
{
/* The low-level hash table structure returns NULL if key is not in the
* hash table. However, users of this map might want to store zero as a
* valid value in the table. Bias the value by +1 so that a
* user-specified zero is stored as 1. This enables ::get to tell the
* difference between a user-specified zero (returned as 1 by
* hash_table_find) and the key not in the table (returned as 0 by
* hash_table_find).
*
* The net effect is that we can't store UINT_MAX in the table. This is
* because UINT_MAX+1 = 0.
*/
assert(value != UINT_MAX);
char *dup_key = strdup(key);
bool result = hash_table_replace(this->ht,
(void *) (intptr_t) (value + 1),
dup_key);
if (result)
free(dup_key);
}
private:
static void delete_key(const void *key, void *data, void *closure)
{
(void) data;
(void) closure;
free((char *)key);
}
static void subtract_one_wrapper(const void *key, void *data, void *closure)
{
struct string_map_iterate_wrapper_closure *wrapper =
(struct string_map_iterate_wrapper_closure *) closure;
unsigned value = (intptr_t) data;
value -= 1;
wrapper->callback((const char *) key, value, wrapper->closure);
}
struct hash_table *ht;
};
#endif /* __cplusplus */
#endif /* HASH_TABLE_H */
|