forked from Embroidermodder/libembroidery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtable.c
714 lines (639 loc) · 26.9 KB
/
hashtable.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
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/*--------------------------------------------------------------------------*\
* -----===== HashTable =====-----
*
* Author: Keith Pomakis ([email protected])
* Date: August, 1998
* Released to the public domain.
*
*--------------------------------------------------------------------------
* $Id: hashtable.c,v 1.5 2012/03/16 18:32:37 pomakis Exp pomakis $
\*--------------------------------------------------------------------------*/
#include "hashtable.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
static int pointercmp(const void* pointer1, const void* pointer2);
static unsigned long pointerHashFunction(const void* pointer);
static int isProbablePrime(long number);
static long calculateIdealNumOfBuckets(EmbHashTable* hashTable);
/*--------------------------------------------------------------------------*\
* NAME:
* HashTableCreate() - creates a new HashTable
* DESCRIPTION:
* Creates a new HashTable. When finished with this HashTable, it
* should be explicitly destroyed by calling the HashTableDestroy()
* function.
* EFFICIENCY:
* O(1)
* ARGUMENTS:
* numOfBuckets - the number of buckets to start the HashTable out with.
* Must be greater than zero, and should be prime.
* Ideally, the number of buckets should between 1/5
* and 1 times the expected number of elements in the
* HashTable. Values much more or less than this will
* result in wasted memory or decreased performance
* respectively. The number of buckets in a HashTable
* can be re-calculated to an appropriate number by
* calling the HashTableRehash() function once the
* HashTable has been populated. The number of buckets
* in a HashTable may also be re-calculated
* automatically if the ratio of elements to buckets
* passes the thresholds set by HashTableSetIdealRatio().
* RETURNS:
* HashTable - a new Hashtable, or NULL on error
\*--------------------------------------------------------------------------*/
EmbHashTable* HashTableCreate(long numOfBuckets)
{
EmbHashTable* hashTable;
int i;
assert(numOfBuckets > 0);
hashTable = (EmbHashTable*)malloc(sizeof(EmbHashTable));
if (hashTable == NULL)
return NULL;
hashTable->bucketArray = (KeyValuePair**)
malloc(numOfBuckets * sizeof(KeyValuePair*));
if (hashTable->bucketArray == NULL) {
free(hashTable);
return NULL;
}
hashTable->numOfBuckets = numOfBuckets;
hashTable->numOfElements = 0;
for (i = 0; i < numOfBuckets; i++)
hashTable->bucketArray[i] = NULL;
hashTable->idealRatio = 3.0;
hashTable->lowerRehashThreshold = 0.0;
hashTable->upperRehashThreshold = 15.0;
hashTable->keycmp = pointercmp;
hashTable->valuecmp = pointercmp;
hashTable->hashFunction = pointerHashFunction;
hashTable->keyDeallocator = NULL;
hashTable->valueDeallocator = NULL;
return hashTable;
}
/*--------------------------------------------------------------------------*\
* NAME:
* HashTableDestroy() - destroys an existing HashTable
* DESCRIPTION:
* Destroys an existing HashTable.
* EFFICIENCY:
* O(n)
* ARGUMENTS:
* hashTable - the HashTable to destroy
* RETURNS:
* <nothing>
\*--------------------------------------------------------------------------*/
void HashTableDestroy(EmbHashTable* hashTable)
{
int i;
for (i = 0; i < hashTable->numOfBuckets; i++) {
KeyValuePair* pair = hashTable->bucketArray[i];
while (pair != NULL) {
KeyValuePair* nextPair = pair->next;
if (hashTable->keyDeallocator != NULL)
hashTable->keyDeallocator((void*)pair->key);
if (hashTable->valueDeallocator != NULL)
hashTable->valueDeallocator(pair->value);
free(pair);
pair = nextPair;
}
}
free(hashTable->bucketArray);
free(hashTable);
}
/*--------------------------------------------------------------------------*\
* NAME:
* HashTableContainsKey() - checks the existence of a key in a HashTable
* DESCRIPTION:
* Determines whether or not the specified HashTable contains the
* specified key. Uses the comparison function specified by
* HashTableSetKeyComparisonFunction().
* EFFICIENCY:
* O(1), assuming a good hash function and element-to-bucket ratio
* ARGUMENTS:
* hashTable - the HashTable to search
* key - the key to search for
* RETURNS:
* bool - whether or not the specified HashTable contains the
* specified key.
\*--------------------------------------------------------------------------*/
int HashTableContainsKey(const EmbHashTable* hashTable, const void* key)
{
return (HashTableGet(hashTable, key) != NULL);
}
/*--------------------------------------------------------------------------*\
* NAME:
* HashTableContainsValue()
* - checks the existence of a value in a HashTable
* DESCRIPTION:
* Determines whether or not the specified HashTable contains the
* specified value. Unlike HashTableContainsKey(), this function is
* not very efficient, since it has to scan linearly looking for a
* match. Uses the comparison function specified by
* HashTableSetValueComparisonFunction().
* EFFICIENCY:
* O(n)
* ARGUMENTS:
* hashTable - the HashTable to search
* value - the value to search for
* RETURNS:
* bool - whether or not the specified HashTable contains the
* specified value.
\*--------------------------------------------------------------------------*/
int HashTableContainsValue(const EmbHashTable* hashTable, const void* value)
{
int i;
for (i = 0; i < hashTable->numOfBuckets; i++) {
KeyValuePair* pair = hashTable->bucketArray[i];
while (pair != NULL) {
if (hashTable->valuecmp(value, pair->value) == 0)
return 1;
pair = pair->next;
}
}
return 0;
}
/*--------------------------------------------------------------------------*\
* NAME:
* HashTablePut() - adds a key/value pair to a HashTable
* DESCRIPTION:
* Adds the specified key/value pair to the specified HashTable. If
* the key already exists in the HashTable (determined by the comparison
* function specified by HashTableSetKeyComparisonFunction()), its value
* is replaced by the new value. May trigger an auto-rehash (see
* HashTableSetIdealRatio()). It is illegal to specify NULL as the
* key or value.
* EFFICIENCY:
* O(1), assuming a good hash function and element-to-bucket ratio
* ARGUMENTS:
* hashTable - the HashTable to add to
* key - the key to add or whose value to replace
* value - the value associated with the key
* RETURNS:
* err - 0 if successful, -1 if an error was encountered
\*--------------------------------------------------------------------------*/
int HashTablePut(EmbHashTable* hashTable, const void* key, void* value)
{
long hashValue;
KeyValuePair* pair;
assert(key != NULL);
assert(value != NULL);
hashValue = hashTable->hashFunction(key) % hashTable->numOfBuckets;
pair = hashTable->bucketArray[hashValue];
while (pair != NULL && hashTable->keycmp(key, pair->key) != 0)
pair = pair->next;
if (pair) {
if (pair->key != key) {
if (hashTable->keyDeallocator != NULL)
hashTable->keyDeallocator((void*)pair->key);
pair->key = key;
}
if (pair->value != value) {
if (hashTable->valueDeallocator != NULL)
hashTable->valueDeallocator(pair->value);
pair->value = value;
}
} else {
KeyValuePair* newPair = (KeyValuePair*)malloc(sizeof(KeyValuePair));
if (newPair == NULL) {
return -1;
} else {
newPair->key = key;
newPair->value = value;
newPair->next = hashTable->bucketArray[hashValue];
hashTable->bucketArray[hashValue] = newPair;
hashTable->numOfElements++;
if (hashTable->upperRehashThreshold > hashTable->idealRatio) {
float elementToBucketRatio = (float)hashTable->numOfElements / (float)hashTable->numOfBuckets;
if (elementToBucketRatio > hashTable->upperRehashThreshold)
HashTableRehash(hashTable, 0);
}
}
}
return 0;
}
/*--------------------------------------------------------------------------*\
* NAME:
* HashTableGet() - retrieves the value of a key in a HashTable
* DESCRIPTION:
* Retrieves the value of the specified key in the specified HashTable.
* Uses the comparison function specified by
* HashTableSetKeyComparisonFunction().
* EFFICIENCY:
* O(1), assuming a good hash function and element-to-bucket ratio
* ARGUMENTS:
* hashTable - the HashTable to search
* key - the key whose value is desired
* RETURNS:
* void * - the value of the specified key, or NULL if the key
* doesn't exist in the HashTable
\*--------------------------------------------------------------------------*/
void* HashTableGet(const EmbHashTable* hashTable, const void* key)
{
long hashValue = hashTable->hashFunction(key) % hashTable->numOfBuckets;
KeyValuePair* pair = hashTable->bucketArray[hashValue];
while (pair != NULL && hashTable->keycmp(key, pair->key) != 0)
pair = pair->next;
return (pair == NULL) ? NULL : pair->value;
}
/*--------------------------------------------------------------------------*\
* NAME:
* HashTableRemove() - removes a key/value pair from a HashTable
* DESCRIPTION:
* Removes the key/value pair identified by the specified key from the
* specified HashTable if the key exists in the HashTable. May trigger
* an auto-rehash (see HashTableSetIdealRatio()).
* EFFICIENCY:
* O(1), assuming a good hash function and element-to-bucket ratio
* ARGUMENTS:
* hashTable - the HashTable to remove the key/value pair from
* key - the key specifying the key/value pair to be removed
* RETURNS:
* <nothing>
\*--------------------------------------------------------------------------*/
void HashTableRemove(EmbHashTable* hashTable, const void* key)
{
long hashValue = hashTable->hashFunction(key) % hashTable->numOfBuckets;
KeyValuePair* pair = hashTable->bucketArray[hashValue];
KeyValuePair* previousPair = NULL;
while (pair != NULL && hashTable->keycmp(key, pair->key) != 0) {
previousPair = pair;
pair = pair->next;
}
if (pair != NULL) {
if (hashTable->keyDeallocator != NULL)
hashTable->keyDeallocator((void*)pair->key);
if (hashTable->valueDeallocator != NULL)
hashTable->valueDeallocator(pair->value);
if (previousPair != NULL)
previousPair->next = pair->next;
else
hashTable->bucketArray[hashValue] = pair->next;
free(pair);
hashTable->numOfElements--;
if (hashTable->lowerRehashThreshold > 0.0) {
float elementToBucketRatio = (float)hashTable->numOfElements / (float)hashTable->numOfBuckets;
if (elementToBucketRatio < hashTable->lowerRehashThreshold)
HashTableRehash(hashTable, 0);
}
}
}
/*--------------------------------------------------------------------------*\
* NAME:
* HashTableRemoveAll() - removes all key/value pairs from a HashTable
* DESCRIPTION:
* Removes all key/value pairs from the specified HashTable. May trigger
* an auto-rehash (see HashTableSetIdealRatio()).
* EFFICIENCY:
* O(n)
* ARGUMENTS:
* hashTable - the HashTable to remove all key/value pairs from
* RETURNS:
* <nothing>
\*--------------------------------------------------------------------------*/
void HashTableRemoveAll(EmbHashTable* hashTable)
{
int i;
for (i = 0; i < hashTable->numOfBuckets; i++) {
KeyValuePair* pair = hashTable->bucketArray[i];
while (pair != NULL) {
KeyValuePair* nextPair = pair->next;
if (hashTable->keyDeallocator != NULL)
hashTable->keyDeallocator((void*)pair->key);
if (hashTable->valueDeallocator != NULL)
hashTable->valueDeallocator(pair->value);
free(pair);
pair = nextPair;
}
hashTable->bucketArray[i] = NULL;
}
hashTable->numOfElements = 0;
HashTableRehash(hashTable, 5);
}
/*--------------------------------------------------------------------------*\
* NAME:
* HashTableIsEmpty() - determines if a HashTable is empty
* DESCRIPTION:
* Determines whether or not the specified HashTable contains any
* key/value pairs.
* EFFICIENCY:
* O(1)
* ARGUMENTS:
* hashTable - the HashTable to check
* RETURNS:
* bool - whether or not the specified HashTable contains any
* key/value pairs
\*--------------------------------------------------------------------------*/
int HashTableIsEmpty(const EmbHashTable* hashTable)
{
return (hashTable->numOfElements == 0);
}
/*--------------------------------------------------------------------------*\
* NAME:
* HashTableSize() - returns the number of elements in a HashTable
* DESCRIPTION:
* Returns the number of key/value pairs that are present in the
* specified HashTable.
* EFFICIENCY:
* O(1)
* ARGUMENTS:
* hashTable - the HashTable whose size is requested
* RETURNS:
* long - the number of key/value pairs that are present in
* the specified HashTable
\*--------------------------------------------------------------------------*/
long HashTableSize(const EmbHashTable* hashTable)
{
return hashTable->numOfElements;
}
/*--------------------------------------------------------------------------*\
* NAME:
* HashTableGetNumBuckets() - returns the number of buckets in a HashTable
* DESCRIPTION:
* Returns the number of buckets that are in the specified HashTable.
* This may change dynamically throughout the life of a HashTable if
* automatic or manual rehashing is performed.
* EFFICIENCY:
* O(1)
* ARGUMENTS:
* hashTable - the HashTable whose number of buckets is requested
* RETURNS:
* long - the number of buckets that are in the specified
* HashTable
\*--------------------------------------------------------------------------*/
long HashTableGetNumBuckets(const EmbHashTable* hashTable)
{
return hashTable->numOfBuckets;
}
/*--------------------------------------------------------------------------*\
* NAME:
* HashTableSetKeyComparisonFunction()
* - specifies the function used to compare keys in a HashTable
* DESCRIPTION:
* Specifies the function used to compare keys in the specified
* HashTable. The specified function should return zero if the two
* keys are considered equal, and non-zero otherwise. The default
* function is one that simply compares pointers.
* ARGUMENTS:
* hashTable - the HashTable whose key comparison function is being
* specified
* keycmp - a function which returns zero if the two arguments
* passed to it are considered "equal" keys and non-zero
* otherwise
* RETURNS:
* <nothing>
\*--------------------------------------------------------------------------*/
void HashTableSetKeyComparisonFunction(EmbHashTable* hashTable,
int (*keycmp)(const void* key1, const void* key2))
{
assert(keycmp != NULL);
hashTable->keycmp = keycmp;
}
/*--------------------------------------------------------------------------*\
* NAME:
* HashTableSetValueComparisonFunction()
* - specifies the function used to compare values in a HashTable
* DESCRIPTION:
* Specifies the function used to compare values in the specified
* HashTable. The specified function should return zero if the two
* values are considered equal, and non-zero otherwise. The default
* function is one that simply compares pointers.
* ARGUMENTS:
* hashTable - the HashTable whose value comparison function is being
* specified
* valuecmp - a function which returns zero if the two arguments
* passed to it are considered "equal" values and non-zero
* otherwise
* RETURNS:
* <nothing>
\*--------------------------------------------------------------------------*/
void HashTableSetValueComparisonFunction(EmbHashTable* hashTable,
int (*valuecmp)(const void* value1, const void* value2))
{
assert(valuecmp != NULL);
hashTable->valuecmp = valuecmp;
}
/*--------------------------------------------------------------------------*\
* NAME:
* HashTableSetHashFunction()
* - specifies the hash function used by a HashTable
* DESCRIPTION:
* Specifies the function used to determine the hash value for a key
* in the specified HashTable (before modulation). An ideal hash
* function is one which is easy to compute and approximates a
* "random" function. The default function is one that works
* relatively well for pointers. If the HashTable keys are to be
* strings (which is probably the case), then this default function
* will not suffice, in which case consider using the provided
* HashTableStringHashFunction() function.
* ARGUMENTS:
* hashTable - the HashTable whose hash function is being specified
* hashFunction - a function which returns an appropriate hash code
* for a given key
* RETURNS:
* <nothing>
\*--------------------------------------------------------------------------*/
void HashTableSetHashFunction(EmbHashTable* hashTable,
unsigned long (*hashFunction)(const void* key))
{
assert(hashFunction != NULL);
hashTable->hashFunction = hashFunction;
}
/*--------------------------------------------------------------------------*\
* NAME:
* HashTableRehash() - reorganizes a HashTable to be more efficient
* DESCRIPTION:
* Reorganizes a HashTable to be more efficient. If a number of
* buckets is specified, the HashTable is rehashed to that number of
* buckets. If 0 is specified, the HashTable is rehashed to a number
* of buckets which is automatically calculated to be a prime number
* that achieves (as closely as possible) the ideal element-to-bucket
* ratio specified by the HashTableSetIdealRatio() function.
* EFFICIENCY:
* O(n)
* ARGUMENTS:
* hashTable - the HashTable to be reorganized
* numOfBuckets - the number of buckets to rehash the HashTable to.
* Should be prime. Ideally, the number of buckets
* should be between 1/5 and 1 times the expected
* number of elements in the HashTable. Values much
* more or less than this will result in wasted memory
* or decreased performance respectively. If 0 is
* specified, an appropriate number of buckets is
* automatically calculated.
* RETURNS:
* <nothing>
\*--------------------------------------------------------------------------*/
void HashTableRehash(EmbHashTable* hashTable, long numOfBuckets)
{
KeyValuePair** newBucketArray;
int i;
assert(numOfBuckets >= 0);
if (numOfBuckets == 0)
numOfBuckets = calculateIdealNumOfBuckets(hashTable);
if (numOfBuckets == hashTable->numOfBuckets)
return; /* already the right size! */
newBucketArray = (KeyValuePair**)
malloc(numOfBuckets * sizeof(KeyValuePair*));
if (newBucketArray == NULL) {
/* Couldn't allocate memory for the new array. This isn't a fatal
* error; we just can't perform the rehash. */
return;
}
for (i = 0; i < numOfBuckets; i++)
newBucketArray[i] = NULL;
for (i = 0; i < hashTable->numOfBuckets; i++) {
KeyValuePair* pair = hashTable->bucketArray[i];
while (pair != NULL) {
KeyValuePair* nextPair = pair->next;
long hashValue = hashTable->hashFunction(pair->key) % numOfBuckets;
pair->next = newBucketArray[hashValue];
newBucketArray[hashValue] = pair;
pair = nextPair;
}
}
free(hashTable->bucketArray);
hashTable->bucketArray = newBucketArray;
hashTable->numOfBuckets = numOfBuckets;
}
/*--------------------------------------------------------------------------*\
* NAME:
* HashTableSetIdealRatio()
* - sets the ideal element-to-bucket ratio of a HashTable
* DESCRIPTION:
* Sets the ideal element-to-bucket ratio, as well as the lower and
* upper auto-rehash thresholds, of the specified HashTable. Note
* that this function doesn't actually perform a rehash.
*
* The default values for these properties are 3.0, 0.0 and 15.0
* respectively. This is likely fine for most situations, so there
* is probably no need to call this function.
* ARGUMENTS:
* hashTable - a HashTable
* idealRatio - the ideal element-to-bucket ratio. When a rehash
* occurs (either manually via a call to the
* HashTableRehash() function or automatically due the
* the triggering of one of the thresholds below), the
* number of buckets in the HashTable will be
* recalculated to be a prime number that achieves (as
* closely as possible) this ideal ratio. Must be a
* positive number.
* lowerRehashThreshold
* - the element-to-bucket ratio that is considered
* unacceptably low (i.e., too few elements per bucket).
* If the actual ratio falls below this number, a
* rehash will automatically be performed. Must be
* lower than the value of idealRatio. If no ratio
* is considered unacceptably low, a value of 0.0 can
* be specified.
* upperRehashThreshold
* - the element-to-bucket ratio that is considered
* unacceptably high (i.e., too many elements per bucket).
* If the actual ratio rises above this number, a
* rehash will automatically be performed. Must be
* higher than idealRatio. However, if no ratio
* is considered unacceptably high, a value of 0.0 can
* be specified.
* RETURNS:
* <nothing>
\*--------------------------------------------------------------------------*/
void HashTableSetIdealRatio(EmbHashTable* hashTable, float idealRatio,
float lowerRehashThreshold, float upperRehashThreshold)
{
assert(idealRatio > 0.0);
assert(lowerRehashThreshold < idealRatio);
assert(upperRehashThreshold == 0.0 || upperRehashThreshold > idealRatio);
hashTable->idealRatio = idealRatio;
hashTable->lowerRehashThreshold = lowerRehashThreshold;
hashTable->upperRehashThreshold = upperRehashThreshold;
}
/*--------------------------------------------------------------------------*\
* NAME:
* HashTableSetDeallocationFunctions()
* - sets the key and value deallocation functions of a HashTable
* DESCRIPTION:
* Sets the key and value deallocation functions of the specified
* HashTable. This determines what happens to a key or a value when it
* is removed from the HashTable. If the deallocation function is NULL
* (the default if this function is never called), its reference is
* simply dropped and it is up to the calling program to perform the
* proper memory management. If the deallocation function is non-NULL,
* it is called to free the memory used by the object. E.g., for simple
* objects, an appropriate deallocation function may be free().
*
* This affects the behaviour of the HashTableDestroy(), HashTablePut(),
* HashTableRemove() and HashTableRemoveAll() functions.
* ARGUMENTS:
* hashTable - a HashTable
* keyDeallocator
* - if non-NULL, the function to be called when a key is
* removed from the HashTable.
* valueDeallocator
* - if non-NULL, the function to be called when a value is
* removed from the HashTable.
* RETURNS:
* <nothing>
\*--------------------------------------------------------------------------*/
void HashTableSetDeallocationFunctions(EmbHashTable* hashTable,
void (*keyDeallocator)(void* key),
void (*valueDeallocator)(void* value))
{
hashTable->keyDeallocator = keyDeallocator;
hashTable->valueDeallocator = valueDeallocator;
}
/*--------------------------------------------------------------------------*\
* NAME:
* HashTableStringHashFunction() - a good hash function for strings
* DESCRIPTION:
* A hash function that is appropriate for hashing strings. Note that
* this is not the default hash function. To make it the default hash
* function, call HashTableSetHashFunction(hashTable,
* HashTableStringHashFunction).
* ARGUMENTS:
* key - the key to be hashed
* RETURNS:
* unsigned long - the unmodulated hash value of the key
\*--------------------------------------------------------------------------*/
unsigned long HashTableStringHashFunction(const void* key)
{
const unsigned char* str = (const unsigned char*)key;
unsigned long hash = 5381;
int c;
/* djb2 algorithm */
while ((c = *str++) != '\0') {
hash = hash * 33 + c;
}
return hash;
}
static int pointercmp(const void* pointer1, const void* pointer2)
{
return (pointer1 != pointer2);
}
static unsigned long pointerHashFunction(const void* pointer)
{
return ((unsigned long)pointer) >> 4;
}
static int isProbablePrime(long oddNumber)
{
long i;
for (i = 3; i < 51; i += 2) {
if (oddNumber == i) {
return 1;
} else if (oddNumber % i == 0) {
return 0;
}
}
return 1; /* maybe */
}
static long calculateIdealNumOfBuckets(EmbHashTable* hashTable)
{
long idealNumOfBuckets = (long)(hashTable->numOfElements / hashTable->idealRatio);
if (idealNumOfBuckets < 5) {
idealNumOfBuckets = 5;
} else {
idealNumOfBuckets |= 0x01; /* make it an odd number */
}
while (!isProbablePrime(idealNumOfBuckets)) {
idealNumOfBuckets += 2;
}
return idealNumOfBuckets;
}