summaryrefslogtreecommitdiff
path: root/src/utils/hashtable.c
blob: c2ff8ce6cb1b1206324b36f4db8e6b33a3e2e97d (plain)
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
481
482
483
484
485
486
487
488
489
490
491
492
/*
 * This file is part of libdom.
 * Licensed under the MIT License,
 *			http://www.opensource.org/licenses/mit-license.php
 * Copyright 2006 Rob Kendrick <rjek@rjek.com>
 * Copyright 2006 Richard Wilson <info@tinct.net>
 * Copyright 2009 Bo Yang <struggleyb.nku@gmail.com>
 */

#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#ifdef TEST_RIG
#include <stdio.h>
#endif
#include "utils/hashtable.h"

/* The hash table entry */
struct _dom_hash_entry {
	void *key;			/**< The key pointer */
	void *value;			/**< The value pointer */
	struct _dom_hash_entry *next;	/**< Next entry */
};

/* The hash table */
struct dom_hash_table {
	unsigned int nchains;	/**< The chains number */
	dom_hash_func hash;		/**< The hash function */
	struct _dom_hash_entry **chain;	/**< The chain head */
	unsigned int number;		/**< The enries in this table */

	dom_alloc alloc;	/**< Memory allocation function */
	void *ptr;	/**< The private data for the memory allocator */
};


/**
 * Create a new hash table, and return a context for it.  The memory consumption
 * of a hash table is approximately 8 + (nchains * 12) bytes if it is empty.
 *
 * \param chains  Number of chains/buckets this hash table will have.  This
 *                should be a prime number, and ideally a prime number just
 *                over a power of two, for best performance and distribution
 * \param hash    The hash function
 * \param alloc   The memory allocator
 * \param ptr     The private pointer for the allocator
 * \return struct dom_hash_table containing the context of this hash table or
 *         NULL if there is insufficent memory to create it and its chains.
 */
struct dom_hash_table *_dom_hash_create(unsigned int chains, dom_hash_func hash,
		dom_alloc alloc, void *ptr)
{
	struct dom_hash_table *r = alloc(NULL, sizeof(struct dom_hash_table),
			ptr);

	if (r == NULL) {
		return NULL;
	}

	r->nchains = chains;
	r->hash = hash;
	r->alloc = alloc;
	r->ptr = ptr;
	r->chain = (struct _dom_hash_entry **)alloc(NULL, 
			chains*sizeof(struct _dom_hash_entry *), ptr);
	r->number = 0;

	unsigned int i;
	for (i = 0; i < chains; i++)
		r->chain[i] = NULL;

	if (r->chain == NULL) {
		alloc(r, 0, ptr);
		return NULL;
	}

	return r;
}

/**
 * Clone a hash table.
 *
 * \param ht        Hash table to clone.
 * \param alloc     The allocator.
 * \param pw        The private data for the allocator.
 * \param kf        The function pointer used to copy the key.
 * \param key_pw    The private data for the key cloner.
 * \param vf        The function pointer used to copy the value.
 * \param value_pw  The private data for the value cloner.
 *
 * \return The cloned hash table.
 */
struct dom_hash_table *_dom_hash_clone(struct dom_hash_table *ht, 
		dom_alloc alloc, void *pw, dom_key_func kf, void *key_pw, 
		dom_value_func vf, void *value_pw)
{
	struct dom_hash_table *ret;
	
	ret = _dom_hash_create(ht->nchains, ht->hash, alloc, pw);
	if (ret == NULL)
		return NULL;

	void *key = NULL, *nkey = NULL;
	void *value = NULL, *nvalue = NULL;
	unsigned int c1, *c2 = NULL;
	while ( (key = _dom_hash_iterate(ht, &c1, &c2)) != NULL) {
		nkey = kf(key, key_pw, alloc, pw, true);
		if (nkey == NULL) {
			_dom_hash_destroy(ret, kf, key_pw, vf, value_pw);
			return NULL;
		}

		value = _dom_hash_get(ht, key);
		nvalue = vf(value, value_pw, alloc, pw, true);
		if (nvalue == NULL) {
			kf(nkey, key_pw, alloc, pw, false);
			_dom_hash_destroy(ret, kf, key_pw, vf, value_pw);
			return NULL;
		}

		if (_dom_hash_add(ret, nkey, nvalue, false) == false) {
			_dom_hash_destroy(ret, kf, key_pw, vf, value_pw);
			return NULL;
		}
	}

	return ret;
}

/**
 * Destroys a hash table, freeing all memory associated with it.
 *
 * \param ht        Hash table to destroy. After the function returns, this
 *                  will nolonger be valid
 * \param kf        The key destroy function
 * \param key_pw    The key destroy function private data
 * \param vf        The value destroy function
 * \param value_pw  The value destroy function private data
 */
void _dom_hash_destroy(struct dom_hash_table *ht, dom_key_func kf, 
		void *key_pw, dom_value_func vf, void *value_pw)
{
	unsigned int i;

	if (ht == NULL)
		return;

	assert(ht->alloc != NULL);

	for (i = 0; i < ht->nchains; i++) {
		if (ht->chain[i] != NULL) {
			struct _dom_hash_entry *e = ht->chain[i];
			while (e) {
				struct _dom_hash_entry *n = e->next;
				if (kf != NULL) {
					kf(e->key, key_pw, ht->alloc,
							ht->ptr, false);
				}
				if (vf != NULL) {
					vf(e->value, value_pw, ht->alloc,
							ht->ptr, false);
				}
				ht->alloc(e, 0, ht->ptr);
				e = n;
			}
		}
	}

	ht->alloc(ht->chain, 0, ht->ptr);
	ht->alloc(ht, 0, ht->ptr);
}

/**
 * Adds a key/value pair to a hash table
 *
 * \param  ht     The hash table context to add the key/value pair to.
 * \param  key    The key to associate the value with.
 * \param  value  The value to associate the key with.
 * \return true if the add succeeded, false otherwise.  (Failure most likely
 *         indicates insufficent memory to make copies of the key and value.
 */
bool _dom_hash_add(struct dom_hash_table *ht, void *key, void *value, 
		bool replace)
{
	unsigned int h, c;
	struct _dom_hash_entry *e;

	if (ht == NULL || key == NULL || value == NULL)
		return false;

	h = ht->hash(key);
	c = h % ht->nchains;

	for (e = ht->chain[c]; e; e = e->next)
		if (key == e->key) {
			if (replace == true) {
				e->value = value;
				return true;
			} else {
				return false;
			}
		}

	assert(ht->alloc != NULL);

	e = ht->alloc(NULL, sizeof(struct _dom_hash_entry), ht->ptr);
	if (e == NULL) {
		return false;
	}

	e->key = key;
	e->value = value;

	e->next = ht->chain[c];
	ht->chain[c] = e;
	ht->number ++;

	return true;
}

/**
 * Looks up a the value associated with with a key from a specific hash table.
 *
 * \param  ht   The hash table context to look up
 * \param  key  The key to search for
 * \return The value associated with the key, or NULL if it was not found.
 */
void *_dom_hash_get(struct dom_hash_table *ht, void *key)
{
	unsigned int h, c;
	struct _dom_hash_entry *e;

	if (ht == NULL || key == NULL)
		return NULL;

	h = ht->hash(key);
	c = h % ht->nchains;

	for (e = ht->chain[c]; e; e = e->next)
		if (key == e->key) 
			return e->value;

	return NULL;
}

/**
 * Delete the key from the hashtable.
 *
 * \param ht   The hashtable object
 * \param key  The key to delete
 * \return The deleted value
 */
void *_dom_hash_del(struct dom_hash_table *ht, void *key)
{
	unsigned int h, c;
	struct _dom_hash_entry *e, *p;
	void *ret;

	if (ht == NULL || key == NULL)
		return NULL;

	h = ht->hash(key);
	c = h % ht->nchains;

	assert(ht->alloc != NULL);

	p = ht->chain[c];
	for (e = p; e; p = e, e = e->next)
		if (key == e->key) {
			if (p != e) {
				p->next = e->next;
			} else {
				/* The first item in this chain is target*/
				ht->chain[c] = e->next;
			}

			ret = e->value;
			ht->alloc(e, 0, ht->ptr);
			ht->number --;
			return ret;
		}
	
	return NULL;
}

/**
 * Iterate through all available hash keys.
 *
 * \param  ht  The hash table context to iterate.
 * \param  c1  Pointer to first context
 * \param  c2  Pointer to second context (set to 0 on first call)
 * \return The next hash key, or NULL for no more keys
 */
void *_dom_hash_iterate(struct dom_hash_table *ht, unsigned int *c1,
		unsigned int **c2)
{
	struct _dom_hash_entry **he = (struct _dom_hash_entry **)c2;

	if (ht == NULL)
		return NULL;

	if (!*he)
		*c1 = -1;
	else
		*he = (*he)->next;

	if (*he)
		return (*he)->key;

	while (!*he) {
		(*c1)++;
		if (*c1 >= ht->nchains)
			return NULL;
		*he = ht->chain[*c1];
	}
	return (*he)->key;
}

/**
 * Get the number of elements in this hash table 
 *
 * \param ht  The hash table
 * 
 * \return the number of elements
 */
unsigned int _dom_hash_get_length(struct dom_hash_table *ht)
{
	return ht->number;
}

/**
 * Get the chain number of this hash table 
 *
 * \param ht  The hash table
 * 
 * \return the number of chains
 */
unsigned int _dom_hash_get_chains(struct dom_hash_table *ht)
{
	return ht->nchains;
}

/**
 * Get the hash function of this hash table 
 *
 * \param ht  The hash table
 * 
 * \return the hash function
 */
dom_hash_func _dom_hash_get_func(struct dom_hash_table *ht)
{
	return ht->hash;
}

/* A simple test rig.  To compile, use:
 * gcc -g  -o hashtest -I../ -I../../include  -DTEST_RIG  hashtable.c
 *
 * If you make changes to this hash table implementation, please rerun this
 * test, and if possible, through valgrind to make sure there are no memory
 * leaks or invalid memory accesses.  If you add new functionality, please
 * include a test for it that has good coverage along side the other tests.
 */

#ifdef TEST_RIG


/**
 * Hash a pointer, returning a 32bit value.  
 *
 * \param  ptr  The pointer to hash.
 * \return the calculated hash value for the pointer.
 */

static inline unsigned int _dom_hash_pointer_fnv(void *ptr)
{
	return (unsigned int) ptr;
}

static void *test_alloc(void *p, size_t size, void *ptr)
{
	if (p != NULL) {
		free(p);
		return NULL;
	}

	if (p == NULL) {
		return malloc(size);
	}
}

int main(int argc, char *argv[])
{
	struct dom_hash_table *a, *b;
	FILE *dict;
	char keybuf[BUFSIZ], valbuf[BUFSIZ];
	int i;
	char *cow="cow", *moo="moo", *pig="pig", *oink="oink",
			*chicken="chikcken", *cluck="cluck",
			*dog="dog", *woof="woof", *cat="cat", 
			*meow="meow";
	void *ret;

	a = _dom_hash_create(79, _dom_hash_pointer_fnv, test_alloc, NULL);
	assert(a != NULL);

	b = _dom_hash_create(103, _dom_hash_pointer_fnv, test_alloc, NULL);
	assert(b != NULL);

	_dom_hash_add(a, cow, moo ,true);
	_dom_hash_add(b, moo, cow ,true);

	_dom_hash_add(a, pig, oink ,true);
	_dom_hash_add(b, oink, pig ,true);

	_dom_hash_add(a, chicken, cluck ,true);
	_dom_hash_add(b, cluck, chicken ,true);

	_dom_hash_add(a, dog, woof ,true);
	_dom_hash_add(b, woof, dog ,true);

	_dom_hash_add(a, cat, meow ,true);
	_dom_hash_add(b, meow, cat ,true);

#define MATCH(x,y) assert(!strcmp((char *)hash_get(a, x), (char *)y)); \
		assert(!strcmp((char *)hash_get(b, y), (char *)x))
	MATCH(cow, moo);
	MATCH(pig, oink);
	MATCH(chicken, cluck);
	MATCH(dog, woof);
	MATCH(cat, meow);

	assert(hash_get_length(a) == 5);
	assert(hash_get_length(b) == 5);

	_dom_hash_del(a, cat);
	_dom_hash_del(b, meow);
	assert(hash_get(a, cat) == NULL);
	assert(hash_get(b, meow) == NULL);

	assert(hash_get_length(a) == 4);
	assert(hash_get_length(b) == 4);

	_dom_hash_destroy(a, NULL, NULL);
	_dom_hash_destroy(b, NULL, NULL);

	/* This test requires /usr/share/dict/words - a large list of English
	 * words.  We load the entire file - odd lines are used as keys, and
	 * even lines are used as the values for the previous line.  we then
	 * work through it again making sure everything matches.
	 *
	 * We do this twice - once in a hash table with many chains, and once
	 * with a hash table with fewer chains.
	 */

	a = _dom_hash_create(1031, _dom_hash_pointer_fnv, test_alloc, NULL);
	b = _dom_hash_create(7919, _dom_hash_pointer_fnv, test_alloc, NULL);

	dict = fopen("/usr/share/dict/words", "r");
	if (dict == NULL) {
		fprintf(stderr, "Unable to open /usr/share/dict/words - \
				extensive testing skipped.\n");
		exit(0);
	}

	while (!feof(dict)) {
		fscanf(dict, "%s", keybuf);
		fscanf(dict, "%s", valbuf);
		_dom_hash_add(a, keybuf, valbuf, true);
		_dom_hash_add(b, keybuf, valbuf, true);
	}

	for (i = 0; i < 5; i++) {
		fseek(dict, 0, SEEK_SET);

		while (!feof(dict)) {
			fscanf(dict, "%s", keybuf);
			fscanf(dict, "%s", valbuf);
			assert(strcmp(hash_get(a, keybuf), valbuf) == 0);
			assert(strcmp(hash_get(b, keybuf), valbuf) == 0);
		}
	}

	_dom_hash_destroy(a, NULL, NULL);
	_dom_hash_destroy(b, NULL, NULL);

	fclose(dict);

	return 0;
}

#endif