-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmap.c
480 lines (365 loc) · 8.93 KB
/
hmap.c
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hmap.h"
static unsigned Coeff1 = DEFAULT_COEFF;
/******** STRUCTS AND TYPEDEFS *********/
typedef struct node_struct {
struct node_struct *next;
char *key;
VAL_TYPE val;
unsigned hval;
}NODE;
typedef struct {
NODE *members;
int n;
} TBL_ENTRY;
struct hmap {
TBL_ENTRY *tbl;
int tsize;
int n;
double lfactor;
int max_n;
HFUNC hfunc;
char *hfunc_desc;
};
typedef struct {
HFUNC hfunc;
char *description;
} HFUNC_STRUCT;
/******** END STRUCTS AND TYPEDEFS *********/
/******** LIBRARY SUPPLIED HASH FUNCTIONS *****/
static unsigned h0(char *s) {
unsigned h = 0;
while (*s != '\0') {
h += *s;
s++;
}
return h;
}
static unsigned h1(char *s) {
unsigned h = 0;
while (*s != '\0') {
h = h*Coeff1 + *s;
s++;
}
return h;
}
/******** END LIBRARY SUPPLIED HASH FUNCTIONS *****/
/***** GLOBALS TO THIS FILE ******/
static HFUNC_STRUCT HashFunctions[] =
{
{ h0, "naive char sum" },
{ h1, "weighted char sum" }
};
static int NumHFuncs = sizeof(HashFunctions) / sizeof(HFUNC_STRUCT);
/***** END GLOBALS ******/
/***** FORWARD DECLARATIONS *****/
static int match(char *key, unsigned hval, NODE *p);
static NODE **get_node_pred(HMAP * map, char *key);
static TBL_ENTRY *create_tbl_array(int tsize);
static void resize(HMAP * map);
static void free_lst(NODE *l, int free_vals);
static void add_front(TBL_ENTRY *entry, NODE *p);
/***** END FORWARD DECLARATIONS *****/
/***** BEGIN hmap FUNCTIONS ******/
HMAP * hmap_create(unsigned init_tsize, double lfactor) {
HMAP * map = malloc(sizeof(struct hmap));
map->n = 0;
if (lfactor <= 0)
lfactor = DEFAULT_LFACTOR;
if (init_tsize <= 0)
init_tsize = DEFAULT_INIT_SIZE;
map->lfactor = lfactor;
map->tsize = init_tsize;
map->max_n = init_tsize * lfactor;
map->hfunc = HashFunctions[DEFAULT_HFUNC_ID].hfunc;
map->hfunc_desc = HashFunctions[DEFAULT_HFUNC_ID].description;
map->tbl = create_tbl_array(init_tsize);
map->n = 0;
return map;
}
void hmap_set_coeff(unsigned new_coeff) {
Coeff1 = new_coeff;
}
int hmap_size(HMAP * map) {
return map->n;
}
void hmap_display(HMAP * map) {
int i, j;
for (i = 0; i<map->tsize; i++) {
printf("|-|");
for (j = 0; j<map->tbl[i].n; j++)
printf("X");
printf("\n");
}
}
int hmap_set_hfunc(HMAP * map, int hfunc_id) {
if (map->n > 0) {
fprintf(stderr,
"warning: attempt to change hash function on non-empty table\n");
return 0;
}
if (hfunc_id < 0 || hfunc_id >= NumHFuncs) {
fprintf(stderr,
"warning: invalid hash function id %i\n", hfunc_id);
return 0;
}
map->hfunc = HashFunctions[hfunc_id].hfunc;
map->hfunc_desc = HashFunctions[hfunc_id].description;
return 1;
}
int hmap_set_user_hfunc(HMAP * map, HFUNC hfunc, char *desc) {
if (map->n > 0) {
fprintf(stderr,
"warning: attempt to change hash function on non-empty table\n");
return 0;
}
map->hfunc = hfunc;
if (desc == NULL)
map->hfunc_desc = "user-supplied hash function";
else
map->hfunc_desc = desc;
return 1;
}
int hmap_contains(HMAP * map, char *key) {
NODE **pp;
pp = get_node_pred(map, key);
return (*pp == NULL ? 0 : 1);
}
VAL_TYPE hmap_get(HMAP * map, char *key) {
NODE **pp;
pp = get_node_pred(map, key);
return (*pp == NULL ? DEFAULT_VAL : (*pp)->val);
}
VAL_TYPE hmap_set(HMAP * map, char *key, VAL_TYPE val) {
unsigned h;
int idx;
NODE *p, **pp;
pp = get_node_pred(map, key);
p = *pp;
if (p == NULL) { // key not present
char *key_clone;
key_clone = malloc((strlen(key) + 1) * sizeof(char));
strcpy(key_clone, key);
map->n++;
if (map->n > map->max_n)
resize(map);
h = map->hfunc(key);
idx = h % map->tsize;
p = malloc(sizeof(NODE));
p->key = key_clone;
p->val = val;
p->hval = h;
add_front(&(map->tbl[idx]), p);
return DEFAULT_VAL; // no old value to return
}
else { // key already in map
VAL_TYPE tmp = p->val;
p->val = val;
return tmp; // return old value
}
}
void hmap_insert(HMAP * map, char *key) {
hmap_set(map, key, DEFAULT_VAL);
}
VAL_TYPE hmap_remove(HMAP * map, char *key) {
NODE *p, **pp;
pp = get_node_pred(map, key);
p = *pp;
if (p == NULL) {
return DEFAULT_VAL;
}
else {
unsigned idx;
VAL_TYPE val = p->val;
*pp = p->next; // make predecessor skip node
// being removed
free(p->key);
free(p);
idx = (p->hval) % map->tsize;
map->tbl[idx].n--;
map->n--;
return val;
}
}
static int num_empty(HMAP * map) {
int i;
int count = 0;
for (i = 0; i<map->tsize; i++)
if (map->tbl[i].n == 0)
count++;
return count;
}
static void print_max_len_bucket(HMAP * map) {
int i, imax = 0;
int max = 0;
NODE *p;
for (i = 0; i<map->tsize; i++) {
if (map->tbl[i].n > max) {
max = map->tbl[i].n;
imax = i;
}
}
p = map->tbl[imax].members;
while (p != NULL) {
printf(" '%s'\n", p->key);
p = p->next;
}
}
static int max_len(HMAP * map) {
int i;
int max = 0;
for (i = 0; i<map->tsize; i++)
if (map->tbl[i].n > max)
max = map->tbl[i].n;
return max;
}
static double avg_cmps(HMAP * map) {
int i;
double total = 0;
for (i = 0; i<map->tsize; i++) {
int ni = map->tbl[i].n;
total += (ni*(ni + 1)) / 2;
}
return total / map->n;
}
static void length_distribution(HMAP *map) {
int i;
int freq[11] = { 0 };
for (i = 0; i<map->tsize; i++) {
int ni = map->tbl[i].n;
if (ni < 11)
freq[ni]++;
}
printf("BUCKET-LIST LENGTH PROFILE:\n\n");
printf(" LIST-LENGTH PERCENT BUCKETS\n");
for (i = 0; i<11; i++) {
double percent;
percent = ((double)freq[i]) / map->tsize;
percent *= 100;
printf(" %3i %2.3f\n", i, percent);
}
printf("--------------------------------\n");
}
void hmap_print_stats(HMAP * map) {
// printf("sorry, hmap_print_stats() not implemented\n");
printf("######## TABLE STATS ##########\n");
printf(" hash-func: %s \n", map->hfunc_desc);
printf(" tblsize: %i \n", map->tsize);
printf(" numkeys: %i \n", map->n);
printf(" num empty slots: %i \n", num_empty(map));
printf(" max-collisions: %i \n", max_len(map));
printf(" avg-cmps-good-lookup: %f \n", avg_cmps(map));
printf("\n Keys In Biggest Bucket:\n");
length_distribution(map);
print_max_len_bucket(map);
printf("###### END TABLE STATS ##########\n");
}
VAL_TYPE * hmap_extract_values(HMAP * map) {
int i, k;
NODE *p;
VAL_TYPE *values = malloc(map->n * (sizeof(VAL_TYPE)));
k = 0;
for (i = 0; i<map->tsize; i++) {
p = map->tbl[i].members;
while (p != NULL) {
values[k] = p->val;
k++;
p = p->next;
}
}
return values;
}
KV_PAIR * hmap_extract_kv_pairs(HMAP * map) {
int i, k;
NODE *p;
KV_PAIR *pairs = malloc(map->n * (sizeof(KV_PAIR)));
k = 0;
for (i = 0; i<map->tsize; i++) {
p = map->tbl[i].members;
while (p != NULL) {
pairs[k].key = p->key;
pairs[k].val = p->val;
k++;
p = p->next;
}
}
return pairs;
}
void hmap_free(HMAP * map, int free_vals) {
int i;
for (i = 0; i<map->tsize; i++)
free_lst(map->tbl[i].members, free_vals);
free(map->tbl);
map->tbl = NULL; // not needed
free(map);
}
/**** UTILITY FUNCTIONS *******/
static int match(char *key, unsigned hval, NODE *p) {
return (p->hval == hval && strcmp(key, p->key) == 0);
}
static NODE **get_node_pred(HMAP * map, char *key) {
unsigned h;
int i;
NODE **pp;
h = map->hfunc(key);
i = h % map->tsize;
pp = &(map->tbl[i].members);
while (*pp != NULL) {
if (match(key, h, *pp))
return pp;
pp = &((*pp)->next);
}
return pp;
}
static void add_front(TBL_ENTRY *entry, NODE *p) {
entry->n++;
p->next = entry->members;
entry->members = p;
}
static TBL_ENTRY *create_tbl_array(int tsize) {
int i;
TBL_ENTRY *tbl;
NODE *p;
tbl = malloc(tsize * sizeof(TBL_ENTRY));
for (i = 0; i<tsize; i++) {
tbl[i].members = NULL;
tbl[i].n = 0;
}
return tbl;
}
static void resize(HMAP * map) {
int ntsize;
TBL_ENTRY *ntbl;
NODE *nxt, *p;
unsigned h;
int i, idx;
ntsize = 2 * map->tsize;
ntbl = create_tbl_array(ntsize);
for (i = 0; i<map->tsize; i++) {
p = map->tbl[i].members;
while (p != NULL) {
nxt = p->next;
// h = map->hfunc(key); // no need to recompute
idx = p->hval % ntsize;
add_front(&ntbl[idx], p);
p = nxt;
}
}
free(map->tbl);
map->tbl = ntbl;
map->tsize = ntsize;
map->max_n = (int)(ntsize * map->lfactor);
}
static void free_lst(NODE *l, int free_vals) {
if (l == NULL) return;
free_lst(l->next, free_vals);
free(l->key); // made our own copy
#ifdef POINTER_VAL
if (free_vals && l->val != NULL)
free(l->val);
#endif
free(l);
}
/**** END UTILITY FUNCTIONS *******/