/
hashmap.c
480 lines (382 loc) · 8.61 KB
/
hashmap.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 <assert.h>
#include <string.h>
#include <stdlib.h>
#include "hashmap.h"
typedef struct entry_t
{
hashmap_key_t key;
hashmap_value_t value;
struct entry_t *next;
} entry_t;
typedef struct bucket_stats_t
{
unsigned int num_entries;
} bucket_stats_t;
typedef struct bucket_t
{
entry_t *entries;
bucket_stats_t stats;
} bucket_t;
static int hashmap_key_compare( const hashmap_key_t lhs, const hashmap_key_t rhs )
{
return strcmp( lhs, rhs );
}
static entry_t *entry_create( hashmap_key_t key, hashmap_value_t value )
{
entry_t *entry = malloc( sizeof(entry_t) );
if( NULL != entry )
{
size_t key_len = strlen( key ) + 1;
hashmap_key_t key_copy = malloc( key_len );
if( NULL != key_copy )
{
memcpy( key_copy, key, key_len );
entry->key = key_copy;
entry->value = value;
entry->next = NULL;
}
else
{
free( entry );
entry = NULL;
}
}
return entry;
}
static void entry_destroy( entry_t *entry )
{
assert( NULL != entry );
free( entry->key );
free( entry );
}
static void bucket_clear( bucket_t *bucket )
{
entry_t *iter;
entry_t *next;
assert( NULL != bucket );
for( iter = bucket->entries; NULL != iter; iter = next )
{
next = iter->next;
entry_destroy( iter );
}
bucket->entries = NULL;
bucket->stats.num_entries = 0;
}
static entry_t *bucket_find_entry( bucket_t *bucket, hashmap_key_t key, entry_t ** const prev_entry )
{
entry_t *prev = NULL;
entry_t *iter;
for( iter = bucket->entries; NULL != iter; iter = iter->next )
{
if( 0 == hashmap_key_compare( key, iter->key ) )
{
break;
}
prev = iter;
}
if( NULL != prev_entry )
{
*prev_entry = prev;
}
return iter;
}
static unsigned int bucket_find( bucket_t *bucket, hashmap_key_t key, hashmap_value_t * const value )
{
unsigned int found = 0;
entry_t *entry = bucket_find_entry( bucket, key, NULL );
if( NULL != entry )
{
found = 1;
if( NULL != value )
{
*value = entry->value;
}
}
return found;
}
static int bucket_insert_existed( bucket_t *bucket, const hashmap_key_t key, const hashmap_value_t value, unsigned int * const existed )
{
entry_t *entry;
int err = 0;
assert( NULL != bucket );
assert( NULL != key );
assert( NULL != existed );
entry = bucket_find_entry( bucket, key, NULL );
if( NULL != entry )
{
/* Key exists in this bucket, update the value. */
entry->value = value;
*existed = 1;
}
else
{
/* Key doesn't exist in this bucket, add it. */
entry_t *new_entry = entry_create( key, value );
if( NULL != new_entry )
{
new_entry->next = bucket->entries;
bucket->entries = new_entry;
++bucket->stats.num_entries;
*existed = 0;
}
else
{
err = -1;
}
}
return err;
}
static void bucket_erase_existed( bucket_t *bucket, const hashmap_key_t key, unsigned int * const existed )
{
entry_t *prev;
entry_t *iter;
assert( NULL != bucket );
assert( NULL != key );
assert( NULL != existed );
iter = bucket_find_entry( bucket, key, &prev );
if( NULL != iter )
{
if( NULL == prev )
{
bucket->entries = iter->next;
}
else
{
prev->next = iter->next;
}
entry_destroy( iter );
--bucket->stats.num_entries;
*existed = 1;
}
else
{
*existed = 0;
}
}
int hashmap_init( hashmap_t *hashmap )
{
assert( NULL != hashmap );
return hashmap_init_with_buckets( hashmap, HASHMAP_DEFAULT_BUCKETS );
}
int hashmap_init_with_buckets( hashmap_t *hashmap, size_t num_buckets )
{
int err = -1;
assert( NULL != hashmap );
assert( num_buckets > 0 );
hashmap->size = 0;
hashmap->num_buckets = num_buckets;
hashmap->buckets = calloc( hashmap->num_buckets, sizeof(bucket_t) );
if( NULL != hashmap->buckets )
{
err = 0;
}
return err;
}
void hashmap_term( hashmap_t *hashmap )
{
assert( NULL != hashmap );
hashmap_clear( hashmap );
free( hashmap->buckets );
}
hashmap_t *hashmap_create( void )
{
return hashmap_create_with_buckets( HASHMAP_DEFAULT_BUCKETS );
}
hashmap_t *hashmap_create_with_buckets( size_t num_buckets )
{
hashmap_t *hashmap;
assert( num_buckets > 0 );
hashmap = malloc( sizeof(hashmap_t) );
if( NULL != hashmap )
{
int err = hashmap_init( hashmap );
if( 0 != err )
{
free( hashmap );
hashmap = NULL;
}
}
return hashmap;
}
void hashmap_destroy( hashmap_t *hashmap )
{
assert( NULL != hashmap );
hashmap_term( hashmap );
free( hashmap );
}
/* Reference for hashing functions:
http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
*/
#define generate_hash oat_hash
__attribute__((unused)) static size_t djb_hash( void *key, size_t len )
{
unsigned char *p = key;
size_t hash = 5381;
size_t i;
for( i = 0; i < len; ++i )
{
hash = hash * 33 + p[i];
}
return hash;
}
static size_t oat_hash( void *key, size_t len )
{
unsigned char *p = key;
unsigned int hash = 0;
unsigned int i;
for( i = 0; i < len; ++i )
{
hash += p[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
static size_t hashmap_bucket_index( const hashmap_t *hashmap, hashmap_key_t key )
{
assert( NULL != hashmap );
assert( NULL != key );
return generate_hash( key, strlen( key ) ) % hashmap->num_buckets;
}
static bucket_t *hashmap_bucket_get( hashmap_t *hashmap, hashmap_key_t key )
{
size_t index;
assert( NULL != hashmap );
assert( NULL != key );
index = hashmap_bucket_index( hashmap, key );
return &hashmap->buckets[index];
}
int hashmap_insert( hashmap_t *hashmap, hashmap_key_t key, hashmap_value_t value )
{
return hashmap_insert_existed( hashmap, key, value, NULL );
}
int hashmap_insert_existed( hashmap_t *hashmap, hashmap_key_t key, hashmap_value_t value, unsigned int * const existed )
{
unsigned int local_existed;
bucket_t *bucket = hashmap_bucket_get( hashmap, key );
int err = bucket_insert_existed( bucket, key, value, &local_existed );
if( 0 == err )
{
if( 0 == local_existed )
{
++hashmap->size;
}
if( NULL != existed )
{
*existed = local_existed;
}
}
return err;
}
void hashmap_erase( hashmap_t *hashmap, hashmap_key_t key )
{
hashmap_erase_existed( hashmap, key, NULL );
}
void hashmap_erase_existed( hashmap_t *hashmap, hashmap_key_t key, unsigned int * const existed )
{
unsigned int local_existed;
bucket_t *bucket = hashmap_bucket_get( hashmap, key );
bucket_erase_existed( bucket, key, &local_existed );
if( 0 != local_existed )
{
--hashmap->size;
}
if( NULL != existed )
{
*existed = local_existed;
}
}
void hashmap_clear( hashmap_t *hashmap )
{
size_t i;
assert( NULL != hashmap );
for( i = 0; i < hashmap->num_buckets; ++i )
{
bucket_clear( &hashmap->buckets[i] );
}
hashmap->size = 0;
}
void hashmap_stats_fprintf( FILE *fp, const hashmap_t *hashmap )
{
unsigned int i;
assert( NULL != fp );
assert( NULL != hashmap );
for( i = 0; i < hashmap->num_buckets; ++i )
{
fprintf( fp, "%u\n", hashmap->buckets[i].stats.num_entries );
}
}
unsigned int hashmap_find( hashmap_t *hashmap, hashmap_key_t key, hashmap_value_t * const value )
{
bucket_t *bucket = hashmap_bucket_get( hashmap, key );
return bucket_find( bucket, key, value );
}
size_t hashmap_size( const hashmap_t *hashmap )
{
assert( NULL != hashmap );
return hashmap->size;
}
unsigned int hashmap_empty( const hashmap_t *hashmap )
{
assert( NULL != hashmap );
return (0 == hashmap->size) ? 1 : 0;
}
static void hashmap_iter_next_bucket( const hashmap_t *hashmap, hashmap_iter_t *iter )
{
assert( NULL != iter );
/* Search for the next populated bucket in the hashmap. */
for( ; iter->bucket < hashmap->num_buckets; ++iter->bucket )
{
bucket_t *bucket = &hashmap->buckets[iter->bucket];
if( NULL != bucket->entries )
{
iter->entry = bucket->entries;
break;
}
}
}
void hashmap_iter_begin( const hashmap_t *hashmap, hashmap_iter_t *iter )
{
assert( NULL != hashmap );
assert( NULL != iter );
iter->bucket = 0;
hashmap_iter_next_bucket( hashmap, iter );
}
void hashmap_iter_next( const hashmap_t *hashmap, hashmap_iter_t *iter )
{
entry_t *entry;
assert( NULL != hashmap );
assert( NULL != iter );
assert( NULL != iter->entry );
entry = iter->entry;
if( NULL != entry->next )
{
iter->entry = entry->next;
}
else
{
++iter->bucket;
hashmap_iter_next_bucket( hashmap, iter );
}
}
unsigned int hashmap_iter_end( const hashmap_t *hashmap, const hashmap_iter_t *iter )
{
assert( NULL != hashmap );
assert( NULL != iter );
return (hashmap->num_buckets == iter->bucket) ? 1 : 0;
}
hashmap_key_t hashmap_iter_key( const hashmap_iter_t *iter )
{
assert( NULL != iter );
assert( NULL != iter->entry );
return iter->entry->key;
}
hashmap_value_t hashmap_iter_value( const hashmap_iter_t *iter )
{
assert( NULL != iter );
assert( NULL != iter->entry );
return iter->entry->value;
}