forked from icl-utk-edu/papi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtable.h
More file actions
417 lines (339 loc) · 9.43 KB
/
htable.h
File metadata and controls
417 lines (339 loc) · 9.43 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**
* @file htable.c
* @author Giuseppe Congiu
* gcongiu@icl.utk.edu
*
*/
#ifndef __HTABLE_H__
#define __HTABLE_H__
#include <string.h>
#include <inttypes.h>
#include "papi.h"
#include "papi_internal.h"
#include "papi_memory.h"
#define HTABLE_SUCCESS ( 0)
#define HTABLE_ENOVAL (-1)
#define HTABLE_EINVAL (-2)
#define HTABLE_ENOMEM (-3)
#define HTABLE_NEEDS_TO_GROW(table) (table->size > 0 && table->capacity / table->size < 2)
#define HTABLE_NEEDS_TO_SHRINK(table) (table->size > 0 && table->capacity / table->size > 8)
struct hash_table_entry {
char *key;
void *val;
struct hash_table_entry *next;
};
struct hash_table {
uint32_t capacity;
uint32_t size;
struct hash_table_entry **buckets;
};
static uint64_t hash_func(const char *);
static int create_table(uint64_t, struct hash_table **);
static int destroy_table(struct hash_table *);
static int rehash_table(struct hash_table *, struct hash_table *);
static int move_table(struct hash_table *, struct hash_table *);
static int check_n_resize_table(struct hash_table *);
static int destroy_table_entries(struct hash_table *);
static int create_table_entry(const char *, void *,
struct hash_table_entry **);
static int destroy_table_entry(struct hash_table_entry *);
static int insert_table_entry(struct hash_table *, struct hash_table_entry *);
static int delete_table_entry(struct hash_table *, struct hash_table_entry *);
static int find_table_entry(struct hash_table *, const char *,
struct hash_table_entry **);
static inline int
htable_init(void **handle)
{
int htable_errno = HTABLE_SUCCESS;
#define HTABLE_MIN_SIZE (8)
struct hash_table *table = NULL;
htable_errno = create_table(HTABLE_MIN_SIZE, &table);
if (htable_errno != HTABLE_SUCCESS) {
goto fn_fail;
}
*handle = table;
fn_exit:
return htable_errno;
fn_fail:
*handle = NULL;
goto fn_exit;
}
static inline int
htable_shutdown(void *handle)
{
int htable_errno = HTABLE_SUCCESS;
struct hash_table *table = (struct hash_table *) handle;
if (table == NULL) {
return HTABLE_EINVAL;
}
destroy_table_entries(table);
destroy_table(table);
return htable_errno;
}
static inline int
htable_insert(void *handle, const char *key, void *in)
{
int htable_errno = HTABLE_SUCCESS;
struct hash_table *table = (struct hash_table *) handle;
if (table == NULL || key == NULL) {
return HTABLE_EINVAL;
}
struct hash_table_entry *entry = NULL;
htable_errno = find_table_entry(table, key, &entry);
if (htable_errno == HTABLE_SUCCESS) {
entry->val = in;
goto fn_exit;
}
htable_errno = create_table_entry(key, in, &entry);
if (htable_errno != HTABLE_SUCCESS) {
goto fn_fail;
}
htable_errno = insert_table_entry(table, entry);
if (htable_errno != HTABLE_SUCCESS) {
goto fn_fail;
}
htable_errno = check_n_resize_table(table);
fn_exit:
return htable_errno;
fn_fail:
if (entry) {
free(entry);
}
goto fn_exit;
}
static inline int
htable_delete(void *handle, const char *key)
{
int htable_errno = HTABLE_SUCCESS;
struct hash_table *table = (struct hash_table *) handle;
if (table == NULL || key == NULL) {
return HTABLE_EINVAL;
}
struct hash_table_entry *entry = NULL;
htable_errno = find_table_entry(table, key, &entry);
if (htable_errno != HTABLE_SUCCESS) {
return htable_errno;
}
entry->val = NULL;
htable_errno = delete_table_entry(table, entry);
if (htable_errno != HTABLE_SUCCESS) {
return htable_errno;
}
htable_errno = destroy_table_entry(entry);
if (htable_errno != HTABLE_SUCCESS) {
return htable_errno;
}
return check_n_resize_table(table);
}
static inline int
htable_find(void *handle, const char *key, void **out)
{
int htable_errno = HTABLE_SUCCESS;
struct hash_table *table = (struct hash_table *) handle;
if (table == NULL || key == NULL || out == NULL) {
return HTABLE_EINVAL;
}
struct hash_table_entry *entry = NULL;
htable_errno = find_table_entry(table, key, &entry);
if (htable_errno != HTABLE_SUCCESS) {
return htable_errno;
}
*out = entry->val;
return htable_errno;
}
/**
* djb2 hash function
*/
uint64_t
hash_func(const char *string)
{
uint64_t hash = 5381;
int c;
while ((c = *string++)) {
hash = ((hash << 5) + hash) + c;
}
return hash;
}
int
create_table(uint64_t size, struct hash_table **table)
{
int htable_errno = HTABLE_SUCCESS;
*table = calloc(1, sizeof(**table));
if (table == NULL) {
htable_errno = HTABLE_ENOMEM;
goto fn_exit;
}
(*table)->buckets = calloc(size, sizeof(*(*table)->buckets));
if ((*table)->buckets == NULL) {
htable_errno = HTABLE_ENOMEM;
goto fn_exit;
}
(*table)->capacity = size;
fn_exit:
return htable_errno;
}
int
destroy_table(struct hash_table *table)
{
int htable_errno = HTABLE_SUCCESS;
if (table && table->buckets) {
free(table->buckets);
}
if (table) {
free(table);
}
return htable_errno;
}
int
rehash_table(struct hash_table *old_table, struct hash_table *new_table)
{
uint64_t old_id;
for (old_id = 0; old_id < old_table->capacity; ++old_id) {
struct hash_table_entry *entry = old_table->buckets[old_id];
struct hash_table_entry *next;
while (entry) {
next = entry->next;
delete_table_entry(old_table, entry);
insert_table_entry(new_table, entry);
entry = next;
}
}
return HTABLE_SUCCESS;
}
int
move_table(struct hash_table *new_table, struct hash_table *old_table)
{
int htable_errno = HTABLE_SUCCESS;
struct hash_table_entry **old_buckets = old_table->buckets;
old_table->capacity = new_table->capacity;
old_table->size = new_table->size;
old_table->buckets = new_table->buckets;
new_table->buckets = NULL;
free(old_buckets);
return htable_errno;
}
int
destroy_table_entries(struct hash_table *table)
{
int htable_errno = HTABLE_SUCCESS;
uint64_t i;
for (i = 0; i < table->capacity; ++i) {
struct hash_table_entry *entry = table->buckets[i];
struct hash_table_entry *tmp = NULL;
while (entry) {
tmp = entry;
entry = entry->next;
delete_table_entry(table, tmp);
destroy_table_entry(tmp);
}
}
return htable_errno;
}
int
check_n_resize_table(struct hash_table *table)
{
int htable_errno = HTABLE_SUCCESS;
struct hash_table *new_table = NULL;
char resize =
(HTABLE_NEEDS_TO_GROW(table) << 1) | HTABLE_NEEDS_TO_SHRINK(table);
if (resize) {
uint64_t new_capacity = (resize & 0x2) ?
table->capacity * 2 : table->capacity / 2;
htable_errno = create_table(new_capacity, &new_table);
if (htable_errno != HTABLE_SUCCESS) {
goto fn_fail;
}
htable_errno = rehash_table(table, new_table);
if (htable_errno != HTABLE_SUCCESS) {
goto fn_fail;
}
move_table(new_table, table);
destroy_table(new_table);
}
fn_exit:
return htable_errno;
fn_fail:
if (new_table) {
destroy_table(new_table);
}
goto fn_exit;
}
int
create_table_entry(const char *key, void *val, struct hash_table_entry **entry)
{
int htable_errno = HTABLE_SUCCESS;
*entry = calloc(1, sizeof(**entry));
if (*entry == NULL) {
return HTABLE_ENOMEM;
}
(*entry)->key = strdup(key);
(*entry)->val = val;
(*entry)->next = NULL;
return htable_errno;
}
int
destroy_table_entry(struct hash_table_entry *entry)
{
int htable_errno = HTABLE_SUCCESS;
free(entry->key);
free(entry);
return htable_errno;
}
int
insert_table_entry(struct hash_table *table, struct hash_table_entry *entry)
{
int htable_errno = HTABLE_SUCCESS;
uint64_t id = hash_func(entry->key) % table->capacity;
if (table->buckets[id]) {
entry->next = table->buckets[id];
}
table->buckets[id] = entry;
++table->size;
return htable_errno;
}
int
delete_table_entry(struct hash_table *table, struct hash_table_entry *entry)
{
int htable_errno = HTABLE_SUCCESS;
uint64_t id = hash_func(entry->key) % table->capacity;
if (table->buckets[id] == entry) {
table->buckets[id] = entry->next;
entry->next = NULL;
goto fn_exit;
}
struct hash_table_entry *prev = table->buckets[id];
struct hash_table_entry *curr = table->buckets[id]->next;
while (curr) {
if (curr == entry) {
prev->next = curr->next;
curr->next = NULL;
break;
}
prev = prev->next;
curr = curr->next;
}
fn_exit:
--table->size;
return htable_errno;
}
int
find_table_entry(struct hash_table *table, const char *key,
struct hash_table_entry **entry)
{
int htable_errno;
uint64_t id = hash_func(key) % table->capacity;
struct hash_table_entry *head = table->buckets[id];
if (head == NULL) {
htable_errno = HTABLE_ENOVAL;
goto fn_exit;
}
struct hash_table_entry *curr = head;
while (curr && strcmp(curr->key, key)) {
curr = curr->next;
}
*entry = curr;
htable_errno = (curr) ? HTABLE_SUCCESS : HTABLE_ENOVAL;
fn_exit:
return htable_errno;
}
#endif /* __HTABLE_H__ */