File: htable.c

package info (click to toggle)
universal-ctags 6.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,612 kB
  • sloc: ansic: 158,498; sh: 8,621; lisp: 7,742; vhdl: 6,518; cpp: 2,583; perl: 2,578; python: 2,324; javascript: 2,054; cs: 1,193; lex: 1,015; sql: 897; makefile: 787; ruby: 764; php: 755; cobol: 741; f90: 566; ada: 559; asm: 509; yacc: 465; fortran: 412; xml: 405; objc: 289; tcl: 280; java: 173; erlang: 65; pascal: 58; ml: 49; awk: 44; haskell: 42
file content (537 lines) | stat: -rw-r--r-- 11,717 bytes parent folder | download
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
*
*   Copyright (c) 2014, Red Hat, Inc.
*   Copyright (c) 2014, Masatake YAMATO
*
*   This source code is released for free distribution under the terms of the
*   GNU General Public License version 2 or (at your option) any later version.
*
*   Defines hashtable
*/

#include "general.h"
#include "debug.h"
#include "htable.h"
#include "routines.h"

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


typedef struct sHashEntry hentry;
struct sHashEntry {
	void *key;
	void *value;
	unsigned int hash;
	hentry *next;
};

struct sHashTable {
	hentry** table;
	unsigned int size;
	unsigned int count;
	hashTableHashFunc hashfn;
	hashTableEqualFunc equalfn;
	hashTableDeleteFunc keyfreefn;
	hashTableDeleteFunc valfreefn;
	void *valForNotUnknownKey;
	hashTableDeleteFunc valForNotUnknownKeyfreefn;
};

struct chainTracker {
	const void *const target_key;
	hashTableForeachFunc user_proc;
	void *user_data;
	hashTableEqualFunc equalfn;
};

static hentry* entry_new (void *key, void *value, unsigned int hash, hentry* next)
{
	hentry* entry = xMalloc (1, hentry);

	entry->key = key;
	entry->value = value;
	entry->next = next;
	entry->hash = hash;

	return entry;
}

static void entry_reset  (hentry* entry,
						  void *newkey,
						  void *newval,
						  hashTableDeleteFunc keyfreefn,
						  hashTableDeleteFunc valfreefn)
{
	if (keyfreefn)
		keyfreefn (entry->key);
	if (valfreefn)
		valfreefn (entry->value);

	if (keyfreefn)
		entry->key = newkey;
	entry->value = newval;
}

static hentry* entry_destroy (hentry* entry,
			      hashTableDeleteFunc keyfreefn,
			      hashTableDeleteFunc valfreefn)
{
	hentry* tmp;

	entry_reset (entry, NULL, NULL, keyfreefn, valfreefn);
	tmp = entry->next;
	eFree (entry);

	return tmp;
}

static void  entry_reclaim (hentry* entry,
			    hashTableDeleteFunc keyfreefn,
			    hashTableDeleteFunc valfreefn)
{
	while (entry)
		entry = entry_destroy (entry, keyfreefn, valfreefn);
}

/* Looking for an entry having KEY as its key in the chain:
 * entry, entry->next, entry->next->next,...
 *
 * We have a chance to optimize for the case when a same key is
 * used repeatedly. By moving the entry found in the last time to
 * the head of the chain, we can avoid taking time for tracking
 * down the ->next-> chain.
 * The cost for the optimization is very small.
 */
static void *entry_find (hentry** root_entry, const void* const key, hashTableEqualFunc equalfn,
						 void *valForNotUnknownKey)
{
	hentry *entry = *root_entry;
	hentry *last = NULL;
	while (entry)
	{
		if (equalfn( key, entry->key))
		{
			if (last)
			{
				last->next = entry->next;
				entry->next = *root_entry;
				*root_entry = entry;
			}
			return entry->value;
		}

		last = entry;
		entry = entry->next;
	}
	return valForNotUnknownKey;
}

static bool		entry_delete (hentry **entry, const void *key, hashTableEqualFunc equalfn,
			      hashTableDeleteFunc keyfreefn, hashTableDeleteFunc valfreefn)
{
	while (*entry)
	{
		if (equalfn (key, (*entry)->key))
		{
			*entry = entry_destroy (*entry, keyfreefn, valfreefn);
			return true;
		}
		entry = &((*entry)->next);
	}
	return false;
}

static bool		entry_update (hentry *entry, void *key, void *value, hashTableEqualFunc equalfn,
			      hashTableDeleteFunc keyfreefn, hashTableDeleteFunc valfreefn)
{
	while (entry)
	{
		if (equalfn (key, entry->key))
		{
			entry_reset (entry, key, value, keyfreefn, valfreefn);
			return true;
		}
		entry = entry->next;
	}
	return false;
}

static bool  entry_foreach (hentry *entry, hashTableForeachFunc proc, void *user_data)
{
	while (entry)
	{
		if (!proc (entry->key, entry->value, user_data))
			return false;
		entry = entry->next;
	}
	return true;
}

extern hashTable *hashTableNew    (unsigned int size,
				   hashTableHashFunc hashfn,
				   hashTableEqualFunc equalfn,
				   hashTableDeleteFunc keyfreefn,
				   hashTableDeleteFunc valfreefn)
{
	hashTable *htable;

	htable = xMalloc (1, hashTable);

	if (size < 3)
		size = 3;
	if ((size % 2) == 0)
		size++;

	htable->size = size;
	htable->count = 0;
	htable->table = xCalloc (size, hentry*);

	htable->hashfn = hashfn;
	htable->equalfn = equalfn;
	htable->keyfreefn = keyfreefn;
	htable->valfreefn = valfreefn;
	htable->valForNotUnknownKey = NULL;
	htable->valForNotUnknownKeyfreefn = NULL;

	return htable;
}

extern void hashTableSetValueForUnknownKey (hashTable *htable,
											void *val,
											hashTableDeleteFunc valfreefn)
{
	if (htable->valfreefn)
		htable->valfreefn (htable->valForNotUnknownKey);

	htable->valForNotUnknownKey = val;
	htable->valForNotUnknownKeyfreefn = valfreefn;
}

extern void       hashTableDelete (hashTable *htable)
{
	if (!htable)
		return;

	hashTableClear (htable);

	if (htable->valForNotUnknownKeyfreefn)
		htable->valForNotUnknownKeyfreefn (htable->valForNotUnknownKey);
	eFree (htable->table);
	eFree (htable);
}

extern void       hashTableClear (hashTable *htable)
{
	unsigned int i;
	if (!htable)
		return;

	for (i = 0; i < htable->size; i++)
	{
		hentry *entry;

		entry = htable->table[i];
		entry_reclaim (entry, htable->keyfreefn, htable->valfreefn);
		htable->table[i] = NULL;
	}
}

static void       hashTablePutItem00    (hashTable *htable, void *key, void *value, unsigned int h)
{
	unsigned int i = h % htable->size;
	htable->table[i] = entry_new(key, value, h, htable->table[i]);
	htable->count++;
}

/* "doubling" primes smaller than 2^32 */
static const unsigned int primes[] = {
	3, 7, 17, 37, 79, 163, 331, 673, 1361, 2729, 5471, 10949, 21911,
	43853, 87719, 175447, 350899, 701819, 1403641, 2807303, 5614657,
	11229331, 22458671, 44917381, 89834777, 179669557, 359339171,
	718678369, 1437356741, 2874713497,
};

static unsigned int
prime_double(unsigned int i)
{
	unsigned int j;

	Assert (i > 2);
	Assert (i % 2);

	for (j = 0; j < sizeof(primes) / sizeof(primes[0]); ++j)
		if (primes[j] > i)
			return primes[j];

	return i;
}

static void       hashTableGrow        (hashTable *htable)
{
	unsigned int current_size = htable->size;
	unsigned int new_size = prime_double (current_size);

	if (new_size <= current_size)
		return;

	hentry** new_table = xCalloc (new_size, hentry*);
	for (unsigned int i = 0; i < current_size; i++)
	{
		hentry *entry;
		while ((entry = htable->table[i]))
		{
			unsigned int hash = entry->hash;
			unsigned int j = hash % new_size;
			htable->table[i] = entry->next;
			entry->next = new_table[j];
			new_table[j] = entry;
		}
	}

	hentry** old_table = htable->table;
	htable->table = new_table;
	htable->size = new_size;
	eFree (old_table);
}

static void       hashTablePutItem0    (hashTable *htable, void *key, void *value, unsigned int h)
{
	if (((double)htable->count / (double)htable->size) < 0.8)
	{
		hashTablePutItem00 (htable, key, value,  h);
		return;
	}

	hashTableGrow (htable);
	hashTablePutItem00 (htable, key, value, h);
}

extern void       hashTablePutItem    (hashTable *htable, void *key, void *value)
{
	hashTablePutItem0 (htable, key, value, htable->hashfn (key));
}

extern void*      hashTableGetItem   (hashTable *htable, const void * key)
{
	unsigned int h, i;

	h = htable->hashfn (key);
	i = h % htable->size;
	return entry_find(& (htable->table[i]), key, htable->equalfn, htable->valForNotUnknownKey);
}

extern bool     hashTableDeleteItem (hashTable *htable, const void *key)
{
	unsigned int h;
	unsigned int i;
	h = htable->hashfn (key);
	i = h % htable->size;

	bool r = entry_delete(&htable->table[i], key,
			    htable->equalfn, htable->keyfreefn, htable->valfreefn);
	if (r)
		htable->count--;
	return r;
}

extern bool    hashTableUpdateItem (hashTable *htable, const void *key, void *value)
{
	unsigned int h, i;

	h = htable->hashfn (key);
	i = h % htable->size;
	bool r = entry_update(htable->table[i], (void *)key, value,
						  htable->equalfn, NULL, htable->valfreefn);
	return r;
}

extern bool    hashTableUpdateOrPutItem (hashTable *htable, void *key, void *value)
{
	unsigned int h, i;

	h = htable->hashfn (key);
	i = h % htable->size;
	bool r = entry_update(htable->table[i], key, value,
						  htable->equalfn, NULL, htable->valfreefn);
	if (!r)
		hashTablePutItem0(htable, key, value, h);

	return r;
}

extern bool    hashTableHasItem    (hashTable *htable, const void *key)
{
	return hashTableGetItem (htable, key) == htable->valForNotUnknownKey? false: true;
}

extern bool       hashTableForeachItem (hashTable *htable, hashTableForeachFunc proc, void *user_data)
{
	unsigned int i;

	for (i = 0; i < htable->size; i++)
		if (!entry_foreach(htable->table[i], proc, user_data))
			return false;
	return true;
}

static bool track_chain (const void *const key, void *value, void *chain_data)
{
	struct chainTracker *chain_tracker = chain_data;

	if (chain_tracker->equalfn (chain_tracker->target_key, key))
	{
		if (! chain_tracker->user_proc (key, value, chain_tracker->user_data))
			return false;
	}
	return true;
}

extern bool       hashTableForeachItemOnChain (hashTable *htable, const void *key, hashTableForeachFunc proc, void *user_data)
{
	unsigned int h, i;
	struct chainTracker chain_tracker = {
		.target_key = key,
		.user_proc = proc,
		.user_data = user_data,
		.equalfn   = htable->equalfn,
	};

	h = htable->hashfn (key);
	i = h % htable->size;
	if (!entry_foreach(htable->table[i], track_chain, &chain_tracker))
		return false;
	return true;
}

extern void hashTablePrintStatistics(hashTable *htable)
{
	if (htable->size == 0 || htable->count == 0)
		fprintf(stderr, "size: %u, count: %u, average: 0\n",
				htable->size, htable->count);

	double sum = 0.0;
	for (size_t i = 0; i < htable->size; i++)
	{
		hentry *e = htable->table[i];
		while (e)
		{
			sum += 1.0;
			e = e->next;
		}
	}
	double average = sum / (double)htable->size;

	double variance = 0.0;
	for (size_t i = 0; i < htable->size; i++)
	{
		double sum0 = 0;
		hentry *e = htable->table[i];
		while (e)
		{
			sum0 += 1.0;
			e = e->next;
		}
		double d = sum0 - average;
		variance += (d * d);
	}
	variance = variance / (double)htable->size;
	fprintf(stderr, "size: %u, count: %u, average: %lf, s.d.: sqrt(%lf)\n",
			htable->size, htable->count, average, variance);
}

extern unsigned int hashTableCountItem   (hashTable *htable)
{
	return htable->count;
}

unsigned int hashPtrhash (const void * const x)
{
	union {
		const void * ptr;
		unsigned int ui;
	} v;
	v.ui = 0;
	v.ptr = x;

	return v.ui;
}

bool hashPtreq (const void *const a, const void *const b)
{
	return (a == b)? true: false;
}


/* http://www.cse.yorku.ca/~oz/hash.html */
static unsigned long
djb2(const unsigned char *str)
{
	unsigned long hash = 5381;
	int c;

	while ((c = *str++))
		hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

	return hash;
}

static unsigned long
casedjb2(const unsigned char *str)
{
	unsigned long hash = 5381;
	int c;

	while ((c = *str++))
	{
		if (('a' <= c) && (c <= 'z'))
			c += ('A' - 'a');
		hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
	}

	return hash;
}


unsigned int hashCstrhash (const void *const x)
{
	const char *const s = x;
	return (unsigned int)djb2((const unsigned char *)s);
}

bool hashCstreq (const void * const a, const void *const b)
{
	return !!(strcmp (a, b) == 0);
}

unsigned int hashInthash (const void *const x)
{
       union tmp {
               unsigned int u;
               int i;
       } x0;

       x0.u = 0;
       x0.i = *(int *)x;
       return x0.u;
}

bool hashInteq (const void *const a, const void *const b)
{
       int ai = *(int *)a;
       int bi = *(int *)b;

       return !!(ai == bi);
}


unsigned int hashCstrcasehash (const void *const x)
{
	const char *const s = x;
	return (unsigned int)casedjb2((const unsigned char *)s);
}

bool hashCstrcaseeq (const void *const a, const void *const b)
{
	return !!(strcasecmp (a, b) == 0);
}