-
Notifications
You must be signed in to change notification settings - Fork 7
/
HashData.h
940 lines (744 loc) · 20.9 KB
/
HashData.h
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
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
#pragma once
template< typename _Kty > class HashBase;
template< typename _Kty > class HashBaseData;
template< typename _Kty > class HashBaseIter;
template< class _Mty > class LongNIValHash;
template< class _Mty > class LongNIValHashData;
template< class _Mty > class LongNIValHashIter;
template< class _Mty > class LongNIHash;
template< class _Mty > class LongNIHashData;
template< class _Mty > class LongNIHashIter;
template< typename _Kty >
class HashBaseData
{
public :
HashBaseData() : m_pNext(NULL), m_Key(0)
{
}
virtual ~HashBaseData()
{
m_pNext = NULL;
m_Key = 0;
}
HashBaseData<_Kty> *m_pNext;
_Kty m_Key;
};
template< typename _Kty >
class HashBaseIter
{
public:
HashBaseIter(HashBase<_Kty> *pBase)
: m_pBase(pBase)
{
m_iBucket = 0;
m_pLast = NULL;
if (m_pBase)
{
m_pNodePtr = m_pBase->GetBucket(0);
m_bEndReached = FALSE;
if (!m_pNodePtr)
Next();
}
else
{
m_pNodePtr = NULL;
m_bEndReached = TRUE;
}
}
void Next()
{
// Grab the next node from this bucket.
if (m_pNodePtr)
{
m_pLast = m_pNodePtr;
m_pNodePtr = m_pNodePtr->m_pNext;
}
while (!m_pNodePtr)
{
// Dead node. Use the next bucket.
m_iBucket++;
m_pLast = NULL;
if (m_iBucket >= m_pBase->GetBucketCount())
{
// The end has been reached.
m_bEndReached = TRUE;
return;
}
m_pNodePtr = m_pBase->GetBucket(m_iBucket);
}
}
void DeleteCurrent()
{
if (EndReached())
return;
HashBaseData<_Kty> *pNextNode;
if (m_pLast)
{
// This current node was not the base of a bucket.
// To remove it from the chain, just link around it.
m_pLast->m_pNext = m_pNodePtr->m_pNext;
// This will be our next node.
pNextNode = m_pLast->m_pNext;
}
else
{
// Going to have to change the bucket base.
m_pBase->SetBucket(m_iBucket, m_pNodePtr->m_pNext);
// This will be our next node.
pNextNode = m_pBase->GetBucket(m_iBucket);
}
// This check is useless, but Turbine does it. Probably inlined.
if (m_pNodePtr)
delete m_pNodePtr;
while (!pNextNode)
{
// Dead node. Use the next bucket.
m_iBucket++;
m_pLast = NULL;
if (m_iBucket >= m_pBase->GetBucketCount())
{
// The end has been reached.
m_bEndReached = TRUE;
m_pNodePtr = NULL;
return;
}
pNextNode = m_pBase->GetBucket(m_iBucket);
}
m_pNodePtr = pNextNode;
}
// -- Pea you made this one up. It was probably inlined. --
HashBaseData<_Kty>* GetCurrent()
{
return m_pNodePtr;
}
// -- Pea you made this one up. It was probably inlined. --
BOOL EndReached()
{
return m_bEndReached;
}
private:
HashBase<_Kty> *m_pBase; // 0x00
size_t m_iBucket; // 0x04
HashBaseData<_Kty> *m_pNodePtr; // 0x08
HashBaseData<_Kty> *m_pLast; // 0x0C
BOOL m_bEndReached; // 0x10
};
template< typename _Kty >
class HashBase
{
public:
HashBase(size_t buckets)
{
preserve_table = FALSE;
hash_table = new HashBaseData<_Kty>*[ buckets ];
InternalInit(buckets);
}
virtual ~HashBase()
{
if (!preserve_table)
delete [] hash_table;
}
void InternalInit(size_t buckets)
{
bucket_count = buckets;
// 64-bit = 16-bit shift
// 32-bit = 8-bit shift
hash_shift = sizeof(_Kty) << 1;
// Calculate hash mask.
hash_mask = 0;
for (unsigned int bit = 1; (hash_mask | bit) < bucket_count; bit = bit << 1)
hash_mask |= bit;
// Init the table.
for (unsigned int i = 0; i < bucket_count; i++)
hash_table[ i ] = NULL;
}
HashBaseData<_Kty>* remove(_Kty Key)
{
size_t BucketIndex = (Key ^ (Key >> hash_shift)) & hash_mask;
HashBaseData<_Kty>* pBucket = hash_table[ BucketIndex ];
if (!pBucket)
return NULL;
if (pBucket->m_Key == Key)
{
// First entry of bucket was a match.
hash_table[ BucketIndex ] = pBucket->m_pNext;
return pBucket;
}
while (pBucket->m_pNext)
{
if (pBucket->m_pNext->m_Key == Key)
{
HashBaseData<_Kty> *pEntry = pBucket->m_pNext;
// Remove from chain.
pBucket->m_pNext = pEntry->m_pNext;
return pEntry;
}
pBucket = pBucket->m_pNext;
}
// No match found.
return NULL;
}
void add(HashBaseData<_Kty> *pData)
{
size_t BucketIndex = (pData->m_Key ^ (pData->m_Key >> hash_shift)) & hash_mask;
pData->m_pNext = hash_table[ BucketIndex ];
hash_table[ BucketIndex ] = pData;
}
HashBaseData<_Kty>* lookup(_Kty Key)
{
HashBaseData<_Kty>* pBucket = hash_table[ (Key ^ (Key >> hash_shift)) & hash_mask ];
while (pBucket)
{
if (pBucket->m_Key == Key)
return pBucket;
pBucket = pBucket->m_pNext;
}
return NULL;
}
HashBaseData<_Kty>* clobber(HashBaseData<_Kty>* pData)
{
size_t BucketIndex = (pData->m_Key ^ (pData->m_Key >> hash_shift)) & hash_mask;
HashBaseData<_Kty>* pBucket, pBaseBucket, pLastBucket;
pBaseBucket = pBucket = hash_table[ BucketIndex ];
pLastBucket = NULL;
if (!pBucket)
return NULL;
while (pData->m_Key != pBucket->m_Key)
{
pLastBucket = pBucket;
pBucket = pBucket->m_pNext;
if (!pBucket)
{
// Add me, I'm a unique data entry.
pData->m_pNext = pBaseBucket;
hash_table[ BucketIndex ] = pData;
// No data entry existed under my key.
return NULL;
}
}
if (pData != pBucket)
{
// Replace the existing data (It's using my key!).
if (!pLastBucket)
{
// The duplicate is the base bucket.
// To remove it, link the next and change the base.
pData->m_pNext = pBucket->m_pNext;
hash_table[ BucketIndex ] = pData;
}
else
{
// The duplicate is not the base bucket.
// To remove it, just link around it.
pLastBucket->m_pNext = pData;
pData->m_pNext = pBucket->m_pNext;
}
}
// Return the data that was already in the table.
return pBucket;
}
// -- Pea you made this one up. It was probably inlined. --
HashBaseData<_Kty>** GetTable()
{
return hash_table;
}
// -- Pea you made this one up. It was probably inlined. --
size_t GetBucketIndex(_Kty Key)
{
return((Key ^ (Key >> hash_shift)) & hash_mask);
}
// -- Pea you made this one up. It was probably inlined. --
HashBaseData<_Kty>* GetBucket(size_t index)
{
return hash_table[ index ];
}
// -- Pea you made this one up. It was probably inlined. --
size_t GetBucketCount()
{
return bucket_count;
}
// -- Pea you made this one up. It was probably inlined. --
void SetBucket(size_t index, HashBaseData<_Kty>* pData)
{
hash_table[ index ] = pData;
}
private:
_Kty hash_mask; // 0x04 (assuming 32-bit..)
UINT hash_shift; // 0x08
HashBaseData<_Kty>** hash_table; // 0x0C
size_t bucket_count; // 0x10
BOOL preserve_table; // 0x14
};
class LongHashData : public HashBaseData<unsigned long>
{
public:
virtual ~LongHashData() { }
};
template< class _Mty >
class LongHash : public HashBase<unsigned long>
{
public:
typedef HashBase<unsigned long> _Mybase;
typedef HashBaseData<unsigned long> _MybaseData;
typedef HashBaseIter<unsigned long> _MybaseIter;
LongHash(size_t buckets)
: _Mybase(buckets)
{
}
virtual ~LongHash()
{
}
void add(_Mty* pData)
{
_Mybase::add(pData);
}
void remove(unsigned long Key)
{
_Mybase::remove(Key);
}
_Mty* lookup(unsigned long Key)
{
// We are converting the data pointers to their parent objects.
_MybaseData* pData = _Mybase::lookup(Key);
if (pData) // Safe ?
return(static_cast< _Mty* >(pData));
return NULL;
}
void destroy_contents()
{
_MybaseIter it(this);
while (!it.EndReached())
it.DeleteCurrent();
}
};
template< class _Mty >
class LongNIValHashData : public HashBaseData<unsigned long>
{
public:
typedef HashBaseData<unsigned long> _Mybase;
LongNIValHashData(_Mty Data, unsigned long Key)
: m_Data(Data)
{
m_Key = Key;
}
virtual ~LongNIValHashData()
{
}
_Mty m_Data;
};
template< typename _Mty >
class LongNIValHashIter
{
public:
typedef LongNIValHash<_Mty> _MyHash;
typedef LongNIValHashData<_Mty> _MyData;
typedef LongNIValHashIter<_Mty> _MyIter;
LongNIValHashIter(_MyHash *pBase) : m_pBase(pBase)
{
m_iBucket = 0;
m_pLast = NULL;
if (m_pBase)
{
m_pNodePtr = static_cast<_MyData *>(m_pBase->GetBucket(0));
m_bEndReached = FALSE;
if (!m_pNodePtr)
Next();
}
else
{
m_pNodePtr = NULL;
m_bEndReached = TRUE;
}
}
void Next()
{
// Grab the next node from this bucket.
if (m_pNodePtr)
{
m_pLast = m_pNodePtr;
m_pNodePtr = static_cast<_MyData *>(m_pNodePtr->m_pNext);
}
while (!m_pNodePtr)
{
// Dead node. Use the next bucket.
m_iBucket++;
m_pLast = NULL;
if (m_iBucket >= m_pBase->GetBucketCount())
{
// The end has been reached.
m_bEndReached = TRUE;
return;
}
m_pNodePtr = static_cast<_MyData *>(m_pBase->GetBucket(m_iBucket));
}
}
void DeleteCurrent()
{
if (EndReached())
return;
_MyData *pNextNode;
if (m_pLast)
{
// This current node was not the base of a bucket.
// To remove it from the chain, just link around it.
m_pLast->m_pNext = m_pNodePtr->m_pNext;
// This will be our next node.
pNextNode = static_cast<_MyData *>(m_pLast->m_pNext);
}
else
{
// Going to have to change the bucket base.
m_pBase->SetBucket(m_iBucket, m_pNodePtr->m_pNext);
// This will be our next node.
pNextNode = static_cast<_MyData *>(m_pBase->GetBucket(m_iBucket));
}
// This check is useless, but Turbine does it. Probably inlined.
if (m_pNodePtr)
delete m_pNodePtr;
while (!pNextNode)
{
// Dead node. Use the next bucket.
m_iBucket++;
m_pLast = NULL;
if (m_iBucket >= m_pBase->GetBucketCount())
{
// The end has been reached.
m_bEndReached = TRUE;
m_pNodePtr = NULL;
return;
}
pNextNode = static_cast<_MyData *>(m_pBase->GetBucket(m_iBucket));
}
m_pNodePtr = pNextNode;
}
// -- Pea you made this one up. It was probably inlined. --
_MyData *GetCurrent()
{
return m_pNodePtr;
}
// -- Pea you made this one up. It was probably inlined. --
BOOL EndReached()
{
return m_bEndReached;
}
private:
_MyHash *m_pBase; // 0x00
size_t m_iBucket; // 0x04
_MyData *m_pNodePtr; // 0x08
_MyData *m_pLast; // 0x0C
BOOL m_bEndReached; // 0x10
};
template< class _Mty >
class LongNIValHash : public HashBase<unsigned long>
{
public:
typedef HashBase<unsigned long> _Mybase;
typedef HashBaseData<unsigned long> _MybaseData;
typedef HashBaseIter<unsigned long> _MybaseIter;
typedef LongNIValHashData<_Mty> _MyData;
typedef LongNIValHashIter<_Mty> _MyIter;
LongNIValHash(size_t buckets = 16)
: _Mybase(buckets)
{
}
virtual ~LongNIValHash()
{
destroy_contents();
}
void add(_Mty Data, unsigned long Key)
{
_MyData *pData = new _MyData(Data, Key);
_Mybase::add(pData);
}
_MybaseData* remove(unsigned long Key)
{
return _Mybase::remove(Key);
}
BOOL remove(unsigned long Key, _Mty* pOutData)
{
// We are converting the data pointers to their parent objects.
_MybaseData* pData = _Mybase::remove(Key);
if (!pData)
return FALSE;
_MyData* pInternal = static_cast< _MyData* >(pData);
*pOutData = pInternal->m_Data;
delete pInternal;
return TRUE;
}
BOOL lookup(unsigned long Key, _Mty* pOutData)
{
// We are converting the data pointers to their parent objects.
_MybaseData* pData = _Mybase::lookup(Key);
if (!pData)
return FALSE;
_MyData* pInternal = static_cast< _MyData* >(pData);
*pOutData = pInternal->m_Data;
return TRUE;
}
void destroy_contents()
{
_MyIter it(this);
while (!it.EndReached())
it.DeleteCurrent();
}
};
// TODO: LongNIHash
template< class _Mty >
class LongNIHashData
{
public:
typedef DWORD _Kty;
LongNIHashData()
{
m_pNext = NULL;
m_pData = NULL;
m_Key = 0;
}
LongNIHashData<_Mty> *m_pNext;
_Mty* m_pData;
_Kty m_Key;
};
template< typename _Mty >
class LongNIHashIter
{
public:
typedef LongNIHash<_Mty> _MyHash;
typedef LongNIHashData<_Mty> _MyData;
typedef LongNIHashIter<_Mty> _MyIter;
LongNIHashIter(_MyHash *pBase)
{
m_pBase = pBase;
m_iBucket = -1;
m_pNodePtr = NULL;
m_bEndReached = FALSE;
if (m_pBase)
{
do
{
if ((++m_iBucket) < m_pBase->GetBucketCount())
{
m_pNodePtr = m_pBase->GetBucket(m_iBucket);
}
else
{
m_bEndReached = TRUE;
break;
}
}
while (!m_pNodePtr);
}
else
m_bEndReached = TRUE;
}
void Next()
{
if (m_bEndReached)
return;
m_pNodePtr = m_pNodePtr->m_pNext;
while (!m_pNodePtr)
{
if ((++m_iBucket) < m_pBase->GetBucketCount())
{
m_pNodePtr = m_pBase->GetBucket(m_iBucket);
}
else
{
m_bEndReached = TRUE;
break;
}
}
}
// -- Pea you made this one up. It was probably inlined. --
_MyData *GetCurrent()
{
return m_pNodePtr;
}
// -- Pea you made this one up. It was probably inlined. --
_Mty *GetCurrentData()
{
return (m_pNodePtr ? m_pNodePtr->m_pData : NULL);
}
// -- Pea you made this one up. It was probably inlined. --
BOOL EndReached()
{
return m_bEndReached;
}
private:
_MyHash *m_pBase; // 0x00
size_t m_iBucket; // 0x04
_MyData *m_pNodePtr; // 0x08
BOOL m_bEndReached; // 0x0C
};
template< typename _Mty >
class LongNIHash
{
public:
typedef DWORD _Kty;
typedef LongNIHashData<_Mty> _MyData;
typedef LongNIHashIter<_Mty> _MyIter;
LongNIHash(size_t buckets)
{
hash_table = NULL;
bucket_count = buckets;
InternalInit(buckets);
}
virtual ~LongNIHash()
{
DeleteAll();
delete [] hash_table;
hash_table = NULL;
}
void InternalInit(size_t buckets)
{
hash_table = new _MyData *[ bucket_count ];
ZeroMemory(hash_table, sizeof(_MyData *) * bucket_count);
}
void DeleteAll()
{
_MyIter it(this);
while (!it.EndReached())
{
_Mty *pNodeData = it.GetCurrentData();
it.Next();
if (pNodeData)
delete pNodeData;
}
flush();
}
void flush()
{
for (DWORD i = 0; i < bucket_count; i++)
{
_MyData *pData = hash_table[i];
while (pData)
{
_MyData *pNextData = pData->m_pNext; // Next entry in chain.
delete pData;
pData = pNextData;
}
hash_table[i] = NULL;
}
}
_Mty* remove(_Kty Key)
{
size_t BucketIndex = (Key ^ (Key >> 16)) % bucket_count;
_MyData** BucketOffset = &hash_table[ BucketIndex ];
_MyData* RootEntry = BucketOffset[0];
if (!RootEntry)
return NULL;
if (RootEntry->m_Key == Key)
{
// First entry of bucket was a match.
hash_table[ BucketIndex ] = RootEntry->m_pNext;
_Mty *pData = RootEntry->m_pData;
delete RootEntry;
return pData;
}
_MyData* CurrentEntry = RootEntry;
// Iterate through the chain in this bucket.
while (CurrentEntry->m_pNext)
{
_MyData* NextEntry = CurrentEntry->m_pNext;
if (NextEntry->m_Key == Key)
{
// Next entry is the one we want, so remove from chain.
// This is a dumb check, but for whatever reason the client compiled this way.
if (CurrentEntry)
{
CurrentEntry->m_pNext = NextEntry->m_pNext;
_Mty *pData = NextEntry->m_pData;
delete NextEntry;
return pData;
}
else
{
hash_table[ BucketIndex ] = NextEntry->m_pNext;
_Mty *pData = NextEntry->m_pData;
delete NextEntry;
return pData;
}
}
CurrentEntry = NextEntry;
}
// No match found.
return NULL;
}
BOOL add(_Mty *pData, _Kty Key)
{
size_t BucketIndex = (Key ^ (Key >> 16)) % bucket_count;
_MyData **BucketOffset = &hash_table[ BucketIndex ];
_MyData *pHashData = new _MyData;
if (!pHashData)
return FALSE;
pHashData->m_Key = Key;
pHashData->m_pData = pData;
pHashData->m_pNext = *BucketOffset;
*BucketOffset = pHashData;
return TRUE;
}
_Mty *lookup(_Kty Key)
{
_Mty *pEntry = hash_table[ (Key ^ (Key >> 16)) % bucket_count ];
while (pEntry)
{
if (pEntry->m_Key == Key)
return pEntry;
pEntry = pEntry->m_pNext;
}
return NULL;
}
// -- Pea you made this one up. It was probably inlined. --
_MyData* GetTable()
{
return hash_table;
}
// -- Pea you made this one up. It was probably inlined. --
size_t GetBucketIndex(_Kty Key)
{
return((Key ^ (Key >> 16)) % bucket_count);
}
// -- Pea you made this one up. It was probably inlined. --
_MyData* GetBucket(size_t index)
{
return hash_table[ index ];
}
// -- Pea you made this one up. It was probably inlined. --
size_t GetBucketCount()
{
return bucket_count;
}
// -- Pea you made this one up. It was probably inlined. --
void SetBucket(size_t index, _MyData* pData)
{
hash_table[ index ] = pData;
}
private:
_MyData** hash_table; // 0x00
size_t bucket_count; // 0x04
};
template< typename _Kty, class _Dty >
class PackableHashTable
{
public:
PackableHashTable()
{
// ...
}
virtual ~PackableHashTable()
{
Destroy();
}
void Destroy()
{
// ...
}
void EmptyContents()
{
// ...
}
virtual BOOL UnPack(BYTE **ppData, ULONG iSize)
{
// ...
return TRUE;
}
};