-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable.c
More file actions
902 lines (758 loc) · 22.8 KB
/
hashtable.c
File metadata and controls
902 lines (758 loc) · 22.8 KB
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
/*
* PROJECT: Hash Table
* LICENSE: MIT (https://spdx.org/licenses/MIT)
* PURPOSE: Hash Table main file
* COPYRIGHT: Copyright 2025 Curtis Wilson <LiquidFox1776@gmail.com>
*/
//#define HASH_TABLE_UNIT_TEST
#ifdef HASH_TABLE_UNIT_TEST
#define STATIC
#include "tests.h"
#else
#define STATIC static
#include "hashtable.h"
#endif
typedef struct _INTERNAL_HASH_TABLE
{
size_t nNumberOfEntries;
size_t nNumberOfBuckets;
size_t nNumberOfBucketsUsed;
PHASH_ENTRY* Buckets;
DOUBLE dLoadFactor;
}INTERNAL_HASH_TABLE, *PINTERNAL_HASH_TABLE;
#ifndef HASH_TABLE_UNIT_TEST
typedef struct _HASH_TABLE_ITERATOR
{
PHASH_TABLE pHashTable;
PHASH_ENTRY pHashEntry;
SIZE_T nIndex;
} HASH_TABLE_ITERATOR, *PHASH_TABLE_ITERATOR;
#endif
STATIC
PHASH_ENTRY
_FindEntry(
struct _HASH_TABLE* self,
PVOID pKey,
SIZE_T nKeyLength);
STATIC
BOOL
_HashTableFreeEntry(
PHASH_ENTRY pHashEntry,
BOOL bFreeKey,
BOOL bFreeValue);
// *********************************************************************
STATIC
BOOL
IsPrime(SIZE_T n)
{
// 0 or 1 are not prime
if (n <= 1)
{
return FALSE;
}
// 2 and 3 are prime
if (n <= 3)
{
return TRUE;
}
// Eliminate multiples of 2 and 3
if ((n % 2 == 0) || (n % 3 == 0))
{
return FALSE;
}
// Only check up to the square root of n
SIZE_T limit = (SIZE_T)sqrt(n);
// Check for factors of the form 6k ± 1
for (SIZE_T i = 5; i <= limit; i += 6)
{
if (n % i == 0 || n % (i + 2) == 0)
{
return FALSE; // Check both 6k - 1 and 6k + 1 forms
}
}
return TRUE;
}
// Find the closest prime number
STATIC
SIZE_T
FindClosestPrime(
SIZE_T number)
{
// If we are at the maximum value, return it directly
if (number == SIZE_MAX)
{
return SIZE_MAX;
}
SIZE_T candidate = number; // Start from the next number
while (TRUE) {
if (IsPrime(candidate))
{
return candidate; // Return the first prime found
}
// Prevent overflow
if (candidate == SIZE_MAX)
break;
candidate++;
}
return SIZE_MAX;
}
STATIC
DWORD
_Fnv1aHash32(
PVOID data,
SIZE_T data_length)
{
if (data == NULL || data_length == 0)
{
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
DWORD hash = 0x811c9dc5;
DWORD FNV_prime = 0x01000193;
unsigned char* c_data = (unsigned char*)data;
for (size_t index = 0; index < data_length; index++)
{
hash ^= c_data[index];
hash *= FNV_prime;
}
return hash;
}
/**
* @brief Deletes an entry.
*
* This function deletes an entry in @p self by searching for a key @p pKey.
* The code does not free any memory allocated in the Bucket array, but it
* will free either the key or value as specified by @p bFreeKey and @p FreeValue.
* If the entry is a node in a linked list, the memory that holds the node will be
* freed.
*
* @param[in] self
* Contains a pointer to the current HASH_tABLE structure.
*
* @param[in] pKey
* The key to hash and use as an entry point into the hash table.
*
* @param[in] nKeyLength
* The length of pKey.
*
* @param[in] bFreeKey
* If TRUE, _DeleteEntry will attempt to free the memory used by pKey in the HASH_ENTRY
*
* @param[in] bFreeValue
* If TRUE, _DeleteEntry will attempt to free the memory used by pValue in the HASH_ENTRY
*
* @return
* TRUE on success and FALSE on failure. Check GetLastError for more detailed information.
* GetLastError returns the following:
* STATUS_SUCCESS in case of success.
* ERROR_INVALID_PARAMETER if the input arguments are invalid.
* ERROR_INVALID_HANDLE if pInternal is NULL.
* ERROR_NO_MATCH if the key cannot be found.
*
**/
STATIC
BOOL
_DeleteEntry(
_In_ struct _HASH_TABLE* self,
_In_ PVOID pKey,
_In_ SIZE_T nKeyLength,
_In_ BOOL bFreeKey,
_In_ BOOL bFreeValue)
{
DWORD dwHash = 0;
SIZE_T nIndex = 0;
PHASH_ENTRY pHashEntry = NULL;
PHASH_ENTRY pParentHashEntry = NULL;
BOOL bIsNode = FALSE;
if ((self == NULL)
|| (pKey == NULL)
|| (nKeyLength == 0))
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
PINTERNAL_HASH_TABLE pInternalHashTable = self->pInternal;
if (self->pInternal == NULL)
{
SetLastError(ERROR_INVALID_HANDLE);
return FALSE;
}
dwHash = _Fnv1aHash32(pKey, nKeyLength);
nIndex = dwHash % pInternalHashTable->nNumberOfBuckets;
pHashEntry = pInternalHashTable->Buckets[nIndex];
if (pHashEntry != NULL && pHashEntry->pNext == NULL)
pInternalHashTable->nNumberOfBucketsUsed--;
if (pHashEntry != NULL)
{
// follow the linked list until we find a key
while (pHashEntry)
{
// if the key lengths match we can compare
// the key to see if they match
if (pHashEntry->nKeyLength == nKeyLength)
{
if (memcmp(pHashEntry->pKey, pKey, nKeyLength) == 0)
{
// Remove the link in the chain
// if the node has a parrent
if (pParentHashEntry != NULL)
{
pParentHashEntry->pNext = pHashEntry->pNext;
bIsNode = TRUE;
}
// we are at the head of the linked list found inside
// of this bucket
else
{
pInternalHashTable->Buckets[nIndex] = pHashEntry->pNext;
}
break;
}
}
pParentHashEntry = pHashEntry;
pHashEntry = pHashEntry->pNext;
}
}
// if the entry is not null then we have found the correct key
if (pHashEntry)
{
BOOL bFreeSuccessful = TRUE;
if (bFreeKey == TRUE)
{
if (HeapFree(GetProcessHeap(), 0, pHashEntry->pKey) == 0)
bFreeSuccessful = FALSE;
}
if (bFreeValue == TRUE)
{
if (HeapFree(GetProcessHeap(), 0, pHashEntry->pValue) == 0)
bFreeSuccessful = FALSE;
}
pHashEntry->bIsFull = FALSE;
pHashEntry->nKeyLength = 0;
pInternalHashTable->nNumberOfEntries--;
if (bIsNode == TRUE)
{
if (HeapFree(GetProcessHeap(), 0, pHashEntry) == 0)
bFreeSuccessful = FALSE;
}
// Check if any if the HeapFree calls failed
if (bFreeSuccessful == FALSE)
{
return FALSE;
}
SetLastError(ERROR_SUCCESS);
return TRUE;
}
// key does not exist
SetLastError(ERROR_NO_MATCH);
return FALSE;
}
STATIC
BOOL
_HashTableGetEntryCleanup(
PHASH_TABLE_ITERATOR *sIterator)
{
HeapFree(GetProcessHeap(), 0, *sIterator);
*sIterator = NULL;
SetLastError(ERROR_NO_MORE_ITEMS);
return FALSE;
}
STATIC
BOOL
_HashTableGetNextEntry(
PHASH_TABLE_ITERATOR *sIterator,
PHASH_ENTRY *pHashEntry)
{
if (sIterator == NULL || *sIterator == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
PINTERNAL_HASH_TABLE pInternalHashTable = (*sIterator)->pHashTable->pInternal;
// check if we have a linked list to work with
if ((*sIterator)->pHashEntry->pNext == NULL)
{
// iterate the entries until we find the next slot
for (size_t index = (*sIterator)->nIndex + 1; index < pInternalHashTable->nNumberOfBuckets; index++)
{
// make sure the entry exists first
if ((pInternalHashTable->Buckets[index] != NULL )
&& (pInternalHashTable->Buckets[index]->bIsFull == TRUE))
{
// update the find entry structure
(*sIterator)->nIndex = index;
(*sIterator)->pHashEntry = pInternalHashTable->Buckets[index];
// somthing bad has happened if this is true
if ((*sIterator)->pHashEntry == NULL)
{
*pHashEntry = NULL;
return _HashTableGetEntryCleanup(sIterator);
}
// we found what we came here for
*pHashEntry = (*sIterator)->pHashEntry;
SetLastError(ERROR_SUCCESS);
return TRUE;
}
}
// there are no more entries to process so we can cleanup
*pHashEntry = NULL;
return _HashTableGetEntryCleanup(sIterator);
}
// we are in a linked list
else
{
(*sIterator)->pHashEntry = (*sIterator)->pHashEntry->pNext;
*pHashEntry = (*sIterator)->pHashEntry;
}
// if we made it here all is well
SetLastError(ERROR_SUCCESS);
return TRUE;
}
STATIC
PHASH_TABLE_ITERATOR
_HashTableGetFirstEntry(
PHASH_TABLE pHashTable,
PHASH_ENTRY *pHashEntry)
{
if (pHashTable == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return NULL;
}
if (pHashTable->pInternal == NULL)
{
SetLastError(ERROR_INVALID_HANDLE);
return NULL;
}
PINTERNAL_HASH_TABLE pInternalHashTable = pHashTable->pInternal;
PHASH_TABLE_ITERATOR sIterator = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HASH_TABLE_ITERATOR));
if (sIterator == NULL)
{
SetLastError(ERROR_OUTOFMEMORY);
return NULL;
}
sIterator->pHashTable = pHashTable;
// find a bucket that is being used
for (size_t index = 0; index < pInternalHashTable->nNumberOfBuckets; index++)
{
// make sure are bucket exists and is not empty
if (pInternalHashTable->Buckets[index] != NULL
&& pInternalHashTable->Buckets[index]->bIsFull == TRUE)
{
// update the iterator to contain the newest entry
sIterator->nIndex = index;
sIterator->pHashEntry = pInternalHashTable->Buckets[index];
break;
}
}
// set pHashEntry
*pHashEntry = sIterator->pHashEntry;
if (sIterator->pHashEntry == NULL)
{
_HashTableGetEntryCleanup(&sIterator);
return NULL;
}
SetLastError(ERROR_SUCCESS);
return sIterator;
}
STATIC
BOOL
_ReHashTableEntries(
PHASH_TABLE pCurrentHashTable,
PHASH_TABLE pNewHashTable)
{
if (pCurrentHashTable == NULL || pNewHashTable == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
PHASH_ENTRY pHashEntry = NULL;
PHASH_TABLE_ITERATOR sIterator = _HashTableGetFirstEntry(pCurrentHashTable, &pHashEntry);
while (pHashEntry)
{
pNewHashTable->SetEntry(&pNewHashTable,
pHashEntry->pKey,
pHashEntry->nKeyLength,
pHashEntry->pValue);
_HashTableGetNextEntry(&sIterator, &pHashEntry);
}
SetLastError(ERROR_SUCCESS);
return TRUE;
}
STATIC
BOOL
_HashTableExpand(PHASH_TABLE *pHashTable)
{
if (pHashTable == NULL || *pHashTable == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
PHASH_TABLE pTempHashTable = NULL;
PHASH_TABLE pNewHashTable = NULL;
PINTERNAL_HASH_TABLE pInternalHashTable = (*pHashTable)->pInternal;
if (pInternalHashTable == NULL)
{
SetLastError(ERROR_INVALID_HANDLE);
return FALSE;
}
size_t nNewSize = FindClosestPrime((SIZE_T)(pInternalHashTable->nNumberOfEntries
/ pInternalHashTable->dLoadFactor));
pNewHashTable = CreateHashTable(nNewSize, pInternalHashTable->dLoadFactor);
// see if the allocation was successful
if (pNewHashTable == NULL)
{
SetLastError(ERROR_OUTOFMEMORY);
return FALSE;
}
// rehash all of the keys found in pHashTable and store them in pNewHashTable
_ReHashTableEntries((*pHashTable), pNewHashTable);
pTempHashTable = *pHashTable;
*pHashTable = pNewHashTable;
// free the old hash table
FreeHashTable(&pTempHashTable, FALSE, FALSE);
SetLastError(ERROR_SUCCESS);
return TRUE;
}
STATIC
SIZE_T _GetNumberOfEntries(
struct _HASH_TABLE* self)
{
if (self == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
if (self->pInternal == NULL)
{
SetLastError(ERROR_INVALID_HANDLE);
return 0;
}
SetLastError(ERROR_SUCCESS);
return ((PINTERNAL_HASH_TABLE)self->pInternal)->nNumberOfEntries;
}
STATIC
PHASH_ENTRY
_CreateHashEntry(PVOID pKey, SIZE_T nKeyLength, PVOID pValue)
{
if (pKey == NULL
|| nKeyLength == 0
|| pValue == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return NULL;
}
PHASH_ENTRY pHashEntry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HASH_ENTRY));
if (pHashEntry == NULL)
{
SetLastError(ERROR_OUTOFMEMORY);
return FALSE;
}
pHashEntry->bIsFull = TRUE;
pHashEntry->pValue = pValue;
pHashEntry->pKey = pKey;
pHashEntry->nKeyLength = nKeyLength;
SetLastError(ERROR_SUCCESS);
return pHashEntry;
}
STATIC
BOOL
_SetEntry(
struct _HASH_TABLE** self,
PVOID pKey,
SIZE_T nKeyLength,
PVOID pValue)
{
DWORD dwHash = 0;
SIZE_T nIndex = 0;
if (self == NULL
|| *self == NULL
|| (nKeyLength == 0)
|| (pValue == NULL)
|| (pKey == NULL))
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
PINTERNAL_HASH_TABLE pInternalHashTable = (*self)->pInternal;
if (pInternalHashTable == NULL)
{
SetLastError(ERROR_INVALID_HANDLE);
return FALSE;
}
if (pInternalHashTable->nNumberOfBuckets == 0)
return FALSE;
dwHash = _Fnv1aHash32(pKey, nKeyLength);
nIndex = dwHash % pInternalHashTable->nNumberOfBuckets;
if (pInternalHashTable->Buckets[nIndex] != NULL)
{
// if the bucket is empty fill it
if (pInternalHashTable->Buckets[nIndex]->bIsFull == FALSE)
{
pInternalHashTable->Buckets[nIndex]->pValue = pValue;
pInternalHashTable->Buckets[nIndex]->bIsFull = TRUE;
pInternalHashTable->Buckets[nIndex]->pKey = pKey;
pInternalHashTable->Buckets[nIndex]->nKeyLength = nKeyLength;
pInternalHashTable->nNumberOfBucketsUsed++;
}
else
{
// see if the key already exists and update the value if it does
HASH_ENTRY* pHashEntry = pInternalHashTable->Buckets[nIndex];
while (pHashEntry)
{
if (pInternalHashTable->Buckets[nIndex]->nKeyLength == nKeyLength)
{
if (memcmp(pInternalHashTable->Buckets[nIndex]->pKey, pKey, nKeyLength) == 0)
{
pInternalHashTable->Buckets[nIndex]->pValue = pValue;
SetLastError(ERROR_SUCCESS);
return TRUE;
}
}
pHashEntry = pHashEntry->pNext;
}
// create a new node in the linked list
PHASH_ENTRY pNewEntry = _CreateHashEntry(pKey, nKeyLength, pValue);
if (pNewEntry == NULL)
return FALSE;
pNewEntry->pNext = pInternalHashTable->Buckets[nIndex];
pInternalHashTable->Buckets[nIndex] = pNewEntry;
}
}
// memory for the bucket has not been allocated yet
else
{
PHASH_ENTRY pHashEntry = _CreateHashEntry(pKey, nKeyLength, pValue);
if (pHashEntry == NULL)
return FALSE;
pInternalHashTable->Buckets[nIndex] = pHashEntry;
pInternalHashTable->nNumberOfBucketsUsed++;
}
pInternalHashTable->nNumberOfEntries++;
// see if we need to resize the hash table
if ((pInternalHashTable->nNumberOfEntries / pInternalHashTable->dLoadFactor) > pInternalHashTable->nNumberOfBuckets)
{
_HashTableExpand(self);
}
SetLastError(ERROR_SUCCESS);
return TRUE;
}
STATIC
PVOID
_GetValue(
struct _HASH_TABLE* self,
PVOID pKey,
SIZE_T nKeyLength)
{
// _FindEntry sets the last error
PHASH_ENTRY pHashEntry = _FindEntry(self, pKey, nKeyLength);
if (pHashEntry != NULL)
{
return pHashEntry->pValue;
}
return NULL;
}
STATIC
PHASH_ENTRY
_FindEntry(
struct _HASH_TABLE* self,
PVOID pKey,
SIZE_T nKeyLength)
{
DWORD dwHash = 0;
SIZE_T nIndex = 0;
if (self == NULL
|| nKeyLength == 0
|| pKey == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return NULL;
}
PINTERNAL_HASH_TABLE pInternalHashTable = self->pInternal;
if (pInternalHashTable == NULL)
{
SetLastError(ERROR_INVALID_HANDLE);
return NULL;
}
if (pInternalHashTable->nNumberOfBuckets == 0)
{
SetLastError(ERROR_NO_MATCH);
return NULL;
}
dwHash = _Fnv1aHash32(pKey, nKeyLength);
nIndex = dwHash % pInternalHashTable->nNumberOfBuckets;
if (pInternalHashTable->Buckets[nIndex] == NULL)
{
SetLastError(ERROR_NO_MATCH);
return NULL;
}
// if there is nothing in the slot then nothing has been assigned
if (pInternalHashTable->Buckets[nIndex]->bIsFull == FALSE)
{
SetLastError(ERROR_NO_MATCH);
return NULL;
}
PHASH_ENTRY pHashEntry = pInternalHashTable->Buckets[nIndex];
// check every slot in the bucket
while (pHashEntry)
{
// if Key Lengths are different then the keys cant match
if (pHashEntry->nKeyLength == nKeyLength)
{
// check if keys match
if (memcmp(pHashEntry->pKey, pKey, nKeyLength) == 0)
{
SetLastError(ERROR_SUCCESS);
return pHashEntry;
}
}
pHashEntry = pHashEntry->pNext;
}
SetLastError(ERROR_NO_MATCH);
return NULL;
}
/*
* Frees an entry in the hashtable
*/
STATIC
BOOL
_HashTableFreeEntry(
PHASH_ENTRY pHashEntry,
BOOL bFreeKey,
BOOL bFreeValue)
{
if (pHashEntry == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
if (bFreeKey == TRUE)
{
HeapFree(GetProcessHeap(), 0, pHashEntry->pKey);
pHashEntry->pKey = NULL;
}
if (bFreeValue == TRUE)
{
HeapFree(GetProcessHeap(), 0, pHashEntry->pValue);
pHashEntry->pValue = NULL;
}
HeapFree(GetProcessHeap(), 0, pHashEntry);
SetLastError(ERROR_SUCCESS);
return TRUE;
}
STATIC
BOOL
_HashTableFreeList(
PHASH_ENTRY pHashEntry,
BOOL bFreeKey,
BOOL bFreeValue)
{
PHASH_ENTRY pTempHashEntry = NULL;
if (pHashEntry == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
// free every node in the list
while (pHashEntry)
{
pTempHashEntry = pHashEntry;
pHashEntry = pHashEntry->pNext;
_HashTableFreeEntry(pTempHashEntry, bFreeKey, bFreeValue);
pTempHashEntry = 0;
}
SetLastError(ERROR_SUCCESS);
return TRUE;
}
BOOL
FreeHashTable(
PHASH_TABLE *pHashTable,
BOOL bFreeKey,
BOOL bFreeValue)
{
if (pHashTable == NULL || (*pHashTable) == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
if ((*pHashTable)->pInternal == NULL)
{
return FALSE;
}
PINTERNAL_HASH_TABLE pInternalHashTable = (*pHashTable)->pInternal;
if (pInternalHashTable == NULL)
{
SetLastError(ERROR_INVALID_HANDLE);
return FALSE;
}
// iterate every bucket
for (size_t index = 0; index < pInternalHashTable->nNumberOfBuckets; index++)
{
// make sure we have somthing to work with
if ((pInternalHashTable->Buckets != NULL) && (pInternalHashTable->Buckets[index] != NULL))
{
_HashTableFreeList(pInternalHashTable->Buckets[index], bFreeKey, bFreeValue);
}
}
HeapFree(GetProcessHeap(), 0, (*pHashTable)->pInternal);
HeapFree(GetProcessHeap(), 0, (*pHashTable));
(*pHashTable) = NULL;
SetLastError(ERROR_SUCCESS);
return TRUE;
}
PHASH_TABLE
CreateHashTable(
SIZE_T nNumberOfInitalBuckets,
DOUBLE dLoadFactor)
{
// do not use a load factor under 1%
if (dLoadFactor < .01)
{
SetLastError(ERROR_INVALID_PARAMETER);
return NULL;
}
// Allocate memory for the hash table
PHASH_TABLE pHashTable = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HASH_TABLE));
if (pHashTable == NULL)
{
SetLastError(ERROR_OUTOFMEMORY);
return NULL;
}
if (IsPrime(nNumberOfInitalBuckets) != TRUE)
{
nNumberOfInitalBuckets = FindClosestPrime(nNumberOfInitalBuckets);
}
// Allocate memory for the buckets
PHASH_ENTRY *pBuckets = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
sizeof(HASH_ENTRY *) * nNumberOfInitalBuckets);
if (pBuckets == NULL)
{
SetLastError(ERROR_OUTOFMEMORY);
HeapFree(GetProcessHeap(), 0, pHashTable);
return NULL;
}
PINTERNAL_HASH_TABLE pInternalHashTable = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
sizeof(INTERNAL_HASH_TABLE));
if (pInternalHashTable == NULL)
{
SetLastError(ERROR_OUTOFMEMORY);
HeapFree(GetProcessHeap(), 0, pBuckets);
HeapFree(GetProcessHeap(), 0, pHashTable);
return NULL;
}
pInternalHashTable->dLoadFactor = dLoadFactor;
pInternalHashTable->nNumberOfBuckets = nNumberOfInitalBuckets;
pInternalHashTable->Buckets = pBuckets;
pHashTable->pInternal = pInternalHashTable;
pHashTable->DeleteEntry = _DeleteEntry;
pHashTable->GetValue = _GetValue;
pHashTable->SetEntry = _SetEntry;
pHashTable->GetNumberOfEntries = _GetNumberOfEntries;
pHashTable->GetFirstEntry = (HANDLE)_HashTableGetFirstEntry;
pHashTable->GetNextEntry = (BOOL(*)(HANDLE, PHASH_ENTRY*))_HashTableGetNextEntry;
SetLastError(ERROR_SUCCESS);
return pHashTable;
}