~brenns10/funlisp

ref: 7347d0eca6d0a50799d3048c21f376277e827224 funlisp/src/hashtable.c -rw-r--r-- 10.8 KiB
7347d0ecStephen Brennan Updates for readthedocs 3 years ago
                                                                                
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
/*
 * hashtable.c: a generic hash table with quadratic probing
 *
 * Designed for storing any size key and value. For the common case (when the
 * key and value are simply pointers), a set of _ptr functions are provided.
 *
 * Stephen Brennan <stephen@brennan.io>
 */

#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#include "iter.h"
#include "hashtable.h"

#define HTA_KEY_OFFSET 1
#define HASH_TABLE_INITIAL_SIZE 31
#define HASH_TABLE_MAX_LOAD_FACTOR 0.5

#define HT_EMPTY 0
#define HT_FULL 1
#define HT_GRAVE 2

#define HTA_MARK(t, i) ((int8_t*)t->table)[i]

#define nelem(x) (sizeof(x)/sizeof((x)[0]))

/*
 * private functions
 */

unsigned int ht_primes[] = {
	31, /* 2^5 */
	61,
	127,
	257,
	509,
	1021,
	2053,
	4093,
	8191,
	16381,
	32771,
	65537,
	131071,
	262147,
	524287,
	1048573,
	2097143,
	4194301,
	8388617,
	16777213,
	33554467,
	67108859,
	134217757,
	268435459,
	536870909,
	1073741827,
	2147483647,
	4294967291 /* 2^32 */
};

int binary_search(unsigned int *array, int len, unsigned int value)
{
	int lo = 0, hi = len, mid;
	while (lo < hi) {
		mid = (lo + hi) / 2;
		if (value <= array[mid]) {
			hi = mid;
		} else {
			lo = mid + 1;
		}
	}
	return lo;
}

/**
 * @brief Returns the next hashtable size.

 * @param current The current size of the hash table.
 * @returns The next size in the sequence for hash tables.
 */
int ht_next_size(int current)
{
	int curridx = binary_search(ht_primes, nelem(ht_primes), current);
	return ht_primes[curridx + 1];
}

unsigned int item_size(const struct hashtable *obj)
{
	return HTA_KEY_OFFSET + obj->key_size + obj->value_size;
}

unsigned int convert_idx(const struct hashtable *obj, unsigned int orig)
{
	return orig * item_size(obj);
}

/**
 * @brief Find the proper index for insertion into the table.
 * @param obj Hash table object.
 * @param key Key we're inserting.
 */
unsigned int ht_find_insert(const struct hashtable *obj, void *key)
{
	unsigned int index = obj->hash(key) % obj->allocated;
	unsigned int bufidx = convert_idx(obj, index);
	unsigned int j = 1;

	/*
	 * Continue searching until we either find a non-full slot, or we find
	 * the key we're trying to insert:
	 * until (cell.mark != full || cell.key == key)
	 * while (cell.mark == full && cell.key != key)
	 */
	while (HTA_MARK(obj, bufidx) == HT_FULL &&
	       obj->equal(key, obj->table + bufidx + HTA_KEY_OFFSET) != 0) {
		/*
		 * This is quadratic probing, but I'm avoiding squaring numbers:
		 * j:     1, 3, 5, 7,  9, 11, ..
		 * index: 0, 1, 4, 9, 16, 25, 36
		 */
		index = (index + j) % obj->allocated;
		j += 2;
		bufidx = convert_idx(obj, index);
	}

	return index;
}

/**
 * @brief Find the proper index for retrieval from the table.
 * @param obj Hash table object.
 * @param key Key we're looking up.
 */
unsigned int ht_find_retrieve(const struct hashtable *obj, void *key)
{
	unsigned int index = obj->hash(key) % obj->allocated;
	unsigned int bufidx = convert_idx(obj, index);
	unsigned int j = 1;

	/*
	 * Continue searching until we either find an empty slot, or we find
	 * the key we're trying to insert:
	 * until (cell.mark == empty || cell.key == key)
	 * while (cell.mark != empty && cell.key != key)
	 */
	while (HTA_MARK(obj, bufidx) != HT_EMPTY &&
	       obj->equal(key, obj->table + bufidx + HTA_KEY_OFFSET) != 0) {
		/*
		 * This is quadratic probing, but I'm avoiding squaring numbers:
		 * j:     1, 3, 5, 7,  9, 11, ..
		 * index: 0, 1, 4, 9, 16, 25, 36
		 */
		index = (index + j) % obj->allocated;
		j += 2;
		bufidx = convert_idx(obj, index);
	}

	return index;
}

/**
 * @brief Expand the hash table, adding increment to the capacity of the table.
 *
 * @param table The table to expand.
 */
void ht_resize(struct hashtable *table)
{
	void *old_table;
	unsigned int index, old_allocated, bufidx;

	/* Step one: allocate new space for the table */
	old_table = table->table;
	old_allocated = table->allocated;
	table->length = 0;
	table->allocated = ht_next_size(old_allocated);
	table->table = calloc(table->allocated, item_size(table));

	/* Step two, add the old items to the new table. */
	for (index = 0; index < old_allocated; index++) {
		bufidx = convert_idx(table, index);
		if (((int8_t*)old_table)[bufidx] == HT_FULL) {
			ht_insert(table, old_table + bufidx + HTA_KEY_OFFSET,
			          old_table + bufidx + HTA_KEY_OFFSET + table->value_size);
		}
	}

	/* Step three: free old data. */
	free(old_table);
}

/**
 * @brief Return the load factor of a hash table.
 *
 * @param table The table to find the load factor of.
 * @returns The load factor of the hash table.
 */
double ht_load_factor(struct hashtable *table)
{
	return ((double) table->length) / ((double) table->allocated);
}

/*
 * public functions
 */

void ht_init(struct hashtable *table, hash_t hash_func, comp_t equal,
             unsigned int key_size, unsigned int value_size)
{
	/* Initialize values */
	table->length = 0;
	table->allocated = HASH_TABLE_INITIAL_SIZE;
	table->key_size = key_size;
	table->value_size = value_size;
	table->hash = hash_func;
	table->equal = equal;

	/* Allocate table */
	table->table = calloc(HASH_TABLE_INITIAL_SIZE, item_size(table));
}

struct hashtable *ht_create(hash_t hash_func, comp_t equal,
                            unsigned int key_size, unsigned int value_size)
{
	/* Allocate and create the table. */
	struct hashtable *table;
	table = calloc(sizeof(struct hashtable), 1);
	ht_init(table, hash_func, equal, key_size, value_size);
	return table;
}

void ht_destroy(struct hashtable *table)
{
	free(table->table);
}

void ht_delete(struct hashtable *table)
{
	if (!table) {
		return;
	}

	ht_destroy(table);
	free(table);
}

void ht_insert(struct hashtable *table, void *key, void *value)
{
	unsigned int index, bufidx;
	if (ht_load_factor(table) > HASH_TABLE_MAX_LOAD_FACTOR) {
		ht_resize(table);
	}

	/*
	 * First, probe for the key as if we're trying to return it. If we find
	 * it, we update the existing key.
	 */
	index = ht_find_retrieve(table, key);
	bufidx = convert_idx(table, index);
	if (HTA_MARK(table, bufidx) == HT_FULL) {
		memcpy(table->table + bufidx + HTA_KEY_OFFSET + table->key_size,
		       value, table->value_size);
		return;
	}

	/*
	 * If we don't find the key, then we find the first open slot or
	 * gravestone.
	 */
	index = ht_find_insert(table, key);
	bufidx = convert_idx(table, index);
	HTA_MARK(table, bufidx) = HT_FULL;
	memcpy(table->table + bufidx + HTA_KEY_OFFSET, key, table->key_size);
	memcpy(table->table + bufidx + HTA_KEY_OFFSET + table->key_size, value,
	       table->value_size);
	table->length++;
}

void ht_insert_ptr(struct hashtable *table, void *key, void *value)
{
	ht_insert(table, &key, &value);
}

int ht_remove(struct hashtable *table, void *key)
{
	unsigned int index = ht_find_retrieve(table, key);
	unsigned int bufidx = convert_idx(table, index);

	/* If the returned slot isn't full, that means we couldn't find it. */
	if (HTA_MARK(table, bufidx) != HT_FULL) {
		return -1;
	}

	/* Mark the slot with a "grave stone", indicating it is deleted. */
	HTA_MARK(table, bufidx) = HT_GRAVE;
	table->length--;
	return 0;
}

int ht_remove_ptr(struct hashtable *table, void *key)
{
	return ht_remove(table, &key);
}

void *ht_get(struct hashtable const *table, void *key)
{
	unsigned int index = ht_find_retrieve(table, key);
	unsigned int bufidx = convert_idx(table, index);

	/* If the slot is not marked full, we didn't find the key. */
	if (HTA_MARK(table, bufidx) != HT_FULL) {
		return NULL;
	}

	/* Otherwise, return the value. */
	return table->table + bufidx + HTA_KEY_OFFSET + table->key_size;
}

void *ht_get_ptr(struct hashtable const *table, void *key)
{
	void **result = ht_get(table, &key);
	if (!result) {
		return NULL;
	}
	return *result;
}

bool ht_contains(struct hashtable const *table, void *key)
{
	return ht_get(table, key) != NULL;
}

bool ht_contains_ptr(struct hashtable const *table, void *key)
{
	return ht_contains(table, &key);
}

unsigned int ht_string_hash(void *data)
{
	char *theString = *(char**)data;
	unsigned int hash = 0;

	while (theString && *theString != '\0' ) {
		hash = (hash << 5) - hash + *theString;
		theString++;
	}

	return hash;
}

int ht_string_comp(void *left, void *right)
{
	char **l = (char**)left, **r = (char**)right;
	return strcmp(*l,*r);
}

int ht_int_comp(void *left, void *right)
{
	int *l = (int*)left, *r = (int*)right;
	return *l - *r;
}

void ht_print(FILE* f, struct hashtable const *table, print_t key, print_t value,
              int full_mode)
{
	unsigned int i, bufidx;
	char *MARKS[] = {"EMPTY", " FULL", "GRAVE"};

	for (i = 0; i < table->allocated; i++) {
		bufidx = convert_idx(table, i);
		int8_t mark = HTA_MARK(table, bufidx);
		if (full_mode || mark == HT_FULL) {
			printf("[%04d|%05d|%s]:\n", i, bufidx, MARKS[mark]);
			if (mark == HT_FULL) {
				printf("  key: ");
				if (key) key(f, table->table + bufidx + HTA_KEY_OFFSET);
				printf("\n  value: ");
				if (value) value(f, table->table + bufidx + HTA_KEY_OFFSET + table->key_size);
				printf("\n");
			}
		}
	}
}

static void *ht_next(struct iterator *iter)
{
	struct hashtable *table = iter->ds;
	unsigned int bufidx;
	int8_t mark = HT_EMPTY;

	for (; mark != HT_FULL && iter->state_int < (int)table->allocated; iter->state_int++) {
		bufidx = convert_idx(table, iter->state_int);
		mark = HTA_MARK(table, bufidx);
	}

	if (mark != HT_FULL) return NULL;

	iter->index++;

	if (!iter->state_ptr)
		return table->table + bufidx + HTA_KEY_OFFSET;
	else
		return table->table + bufidx + HTA_KEY_OFFSET + table->key_size;
}

static void *ht_next_ptr(struct iterator *iter)
{
	void **res = ht_next(iter);
	if (res == NULL)
		return NULL;
	return *res;
}

static bool ht_has_next(struct iterator *iter)
{
	struct hashtable *table = iter->ds;
	return iter->index < (int)table->length;
}

struct iterator ht_iter_keys(struct hashtable *table)
{
	struct iterator it = {
		.ds = table,
		.index = 0,
		.state_int = 0,
		.state_ptr = NULL, /* when null, return keys, else return values */
		.has_next = ht_has_next,
		.next = ht_next,
		.close = iterator_close_noop,
	};
	return it;
}

struct iterator ht_iter_keys_ptr(struct hashtable *table)
{
	struct iterator it = {
		.ds = table,
		.index = 0,
		.state_int = 0,
		.state_ptr = NULL, /* when null, return keys, else return values */
		.has_next = ht_has_next,
		.next = ht_next_ptr,
		.close = iterator_close_noop,
	};
	return it;
}

struct iterator ht_iter_values(struct hashtable *table)
{
	struct iterator it = {
		.ds = table,
		.index = 0,
		.state_int = 0,
		.state_ptr = table, /* when null, return keys, else return values */
		.has_next = ht_has_next,
		.next = ht_next,
		.close = iterator_close_noop,
	};
	return it;
}

struct iterator ht_iter_values_ptr(struct hashtable *table)
{
	struct iterator it = {
		.ds = table,
		.index = 0,
		.state_int = 0,
		.state_ptr = table, /* when null, return keys, else return values */
		.has_next = ht_has_next,
		.next = ht_next_ptr,
		.close = iterator_close_noop,
	};
	return it;
}