-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.h
executable file
·1094 lines (1000 loc) · 42.2 KB
/
hash.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
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/////////////////////////////////////////////////
// Hash-Table-Key-Data
#pragma pack(push, 1) // pack class size
template <class TKey, class TDat>
class THashKeyDat{
public:
TInt Next;
TInt HashCd;
TKey Key;
TDat Dat;
public:
THashKeyDat():
Next(-1), HashCd(-1), Key(), Dat(){}
THashKeyDat(const int& _Next, const int& _HashCd, const TKey& _Key):
Next(_Next), HashCd(_HashCd), Key(_Key), Dat(){}
explicit THashKeyDat(TSIn& SIn):
Next(SIn), HashCd(SIn), Key(SIn), Dat(SIn){}
void Save(TSOut& SOut) const {
Next.Save(SOut); HashCd.Save(SOut); Key.Save(SOut); Dat.Save(SOut);}
void LoadXml(const PXmlTok& XmlTok, const TStr& Nm="");
void SaveXml(TSOut& SOut, const TStr& Nm) const;
bool operator==(const THashKeyDat& HashKeyDat) const {
if (this==&HashKeyDat || (HashCd==HashKeyDat.HashCd
&& Key==HashKeyDat.Key && Dat==HashKeyDat.Dat)){return true;}
return false;}
THashKeyDat& operator=(const THashKeyDat& HashKeyDat){
if (this!=&HashKeyDat){
Next=HashKeyDat.Next; HashCd=HashKeyDat.HashCd;
Key=HashKeyDat.Key; Dat=HashKeyDat.Dat;}
return *this;}
};
#pragma pack(pop)
/////////////////////////////////////////////////
// Hash-Table-Key-Data-Iterator
template<class TKey, class TDat>
class THashKeyDatI{
private:
typedef THashKeyDat<TKey, TDat> TKeyDat;
TKeyDat* KeyDatI;
TKeyDat* EndI;
public:
THashKeyDatI(): KeyDatI(NULL), EndI(NULL){}
THashKeyDatI(const THashKeyDatI& _HashKeyDatI):
KeyDatI(_HashKeyDatI.KeyDatI), EndI(_HashKeyDatI.EndI){}
THashKeyDatI(const TKeyDat* _KeyDatI, const TKeyDat* _EndI):
KeyDatI((TKeyDat*)_KeyDatI), EndI((TKeyDat*)_EndI){}
THashKeyDatI& operator=(const THashKeyDatI& HashKeyDatI){
KeyDatI=HashKeyDatI.KeyDatI; EndI=HashKeyDatI.EndI; return *this;}
bool operator==(const THashKeyDatI& HashKeyDatI) const {
return KeyDatI==HashKeyDatI.KeyDatI;}
bool operator<(const THashKeyDatI& HashKeyDatI) const {
return KeyDatI<HashKeyDatI.KeyDatI;}
THashKeyDatI& operator++(int){ KeyDatI++; while (KeyDatI < EndI && KeyDatI->HashCd==-1) { KeyDatI++; } return *this; }
THashKeyDatI& operator--(int){ do { KeyDatI--; } while (KeyDatI->HashCd==-1); return *this;}
TKeyDat& operator*() const { return *KeyDatI; }
TKeyDat& operator()() const { return *KeyDatI; }
TKeyDat* operator->() const { return KeyDatI; }
const TKey& GetKey() const {Assert((KeyDatI!=NULL)&&(KeyDatI->HashCd!=-1)); return KeyDatI->Key;}
const TDat& GetDat() const {Assert((KeyDatI!=NULL)&&(KeyDatI->HashCd!=-1)); return KeyDatI->Dat;}
TDat& GetDat() {Assert((KeyDatI!=NULL)&&(KeyDatI->HashCd!=-1)); return KeyDatI->Dat;}
bool IsEnd() { return EndI == KeyDatI; }
};
/////////////////////////////////////////////////
// Default-Hash-Function
template<class TKey>
class TDefaultHashFunc {
public:
static inline int GetPrimHashCd(const TKey& Key) { return Key.GetPrimHashCd(); }
static inline int GetSecHashCd(const TKey& Key) { return Key.GetSecHashCd(); }
};
/////////////////////////////////////////////////
// Hash-Table
template<class TKey, class TDat, class THashFunc = TDefaultHashFunc<TKey> >
class THash{
public:
enum {HashPrimes=32};
static const unsigned int HashPrimeT[HashPrimes];
public:
typedef THashKeyDatI<TKey, TDat> TIter;
private:
typedef THashKeyDat<TKey, TDat> TKeyDat;
typedef TPair<TKey, TDat> TKeyDatP;
TIntV PortV;
TVec<TKeyDat> KeyDatV;
TBool AutoSizeP;
TInt FFreeKeyId, FreeKeys;
private:
class THashKeyDatCmp {
public:
const THash<TKey, TDat, THashFunc>& Hash;
bool CmpKey, Asc;
THashKeyDatCmp(THash<TKey, TDat, THashFunc>& _Hash, const bool& _CmpKey, const bool& _Asc) :
Hash(_Hash), CmpKey(_CmpKey), Asc(_Asc) { }
bool operator () (const int& KeyId1, const int& KeyId2) const {
if (CmpKey) {
if (Asc) { return Hash.GetKey(KeyId1) < Hash.GetKey(KeyId2); }
else { return Hash.GetKey(KeyId1) > Hash.GetKey(KeyId2); } }
else {
if (Asc) { return Hash[KeyId1] < Hash[KeyId2]; }
else { return Hash[KeyId1] > Hash[KeyId2]; } } }
};
private:
TKeyDat& GetHashKeyDat(const int& KeyId){
TKeyDat& KeyDat=KeyDatV[KeyId];
Assert(KeyDat.HashCd!=-1); return KeyDat;}
const TKeyDat& GetHashKeyDat(const int& KeyId) const {
const TKeyDat& KeyDat=KeyDatV[KeyId];
Assert(KeyDat.HashCd!=-1); return KeyDat;}
uint GetNextPrime(const uint& Val) const;
void Resize();
public:
THash():
PortV(), KeyDatV(),
AutoSizeP(true), FFreeKeyId(-1), FreeKeys(0){}
THash(const THash& Hash):
PortV(Hash.PortV), KeyDatV(Hash.KeyDatV), AutoSizeP(Hash.AutoSizeP),
FFreeKeyId(Hash.FFreeKeyId), FreeKeys(Hash.FreeKeys){}
explicit THash(const int& ExpectVals, const bool& _AutoSizeP=false);
explicit THash(TSIn& SIn):
PortV(SIn), KeyDatV(SIn),
AutoSizeP(SIn), FFreeKeyId(SIn), FreeKeys(SIn){
SIn.LoadCs();}
void Load(TSIn& SIn){
PortV.Load(SIn); KeyDatV.Load(SIn);
AutoSizeP=TBool(SIn); FFreeKeyId=TInt(SIn); FreeKeys=TInt(SIn);
SIn.LoadCs();}
void Save(TSOut& SOut) const {
PortV.Save(SOut); KeyDatV.Save(SOut);
AutoSizeP.Save(SOut); FFreeKeyId.Save(SOut); FreeKeys.Save(SOut);
SOut.SaveCs();}
void LoadXml(const PXmlTok& XmlTok, const TStr& Nm="");
void SaveXml(TSOut& SOut, const TStr& Nm);
THash& operator=(const THash& Hash){
if (this!=&Hash){
PortV=Hash.PortV; KeyDatV=Hash.KeyDatV; AutoSizeP=Hash.AutoSizeP;
FFreeKeyId=Hash.FFreeKeyId; FreeKeys=Hash.FreeKeys;}
return *this;}
bool operator==(const THash& Hash) const; //J: zdaj tak kot je treba
bool operator < (const THash& Hash) const { Fail; return true; } //J:
const TDat& operator[](const int& KeyId) const {return GetHashKeyDat(KeyId).Dat;}
TDat& operator[](const int& KeyId){return GetHashKeyDat(KeyId).Dat;}
TDat& operator()(const TKey& Key){return AddDat(Key);}
::TSize GetMemUsed() const {
return PortV.GetMemUsed()+KeyDatV.GetMemUsed()+sizeof(bool)+2*sizeof(int);}
TIter BegI() const {
if (Len()>0){
if (IsKeyIdEqKeyN()) { return TIter(KeyDatV.BegI(), KeyDatV.EndI());}
int FKeyId=-1; FNextKeyId(FKeyId);
return TIter(KeyDatV.BegI()+FKeyId, KeyDatV.EndI());
}
return TIter(KeyDatV.EndI(), KeyDatV.EndI());
}
TIter EndI() const {return TIter(KeyDatV.EndI(), KeyDatV.EndI());}
//TIter GetI(const int& KeyId) const {return TIter(&KeyDatV[KeyId], KeyDatV.EndI());}
TIter GetI(const TKey& Key) const {return TIter(&KeyDatV[GetKeyId(Key)], KeyDatV.EndI());}
void Gen(const int& ExpectVals){
PortV.Gen(GetNextPrime(ExpectVals/2)); KeyDatV.Gen(ExpectVals, 0);
FFreeKeyId=-1; FreeKeys=0; PortV.PutAll(TInt(-1));}
void Clr(const bool& DoDel=true, const int& NoDelLim=-1, const bool& ResetDat=true);
bool Empty() const {return Len()==0;}
int Len() const {return KeyDatV.Len()-FreeKeys;}
int GetPorts() const {return PortV.Len();}
bool IsAutoSize() const {return AutoSizeP;}
int GetMxKeyIds() const {return KeyDatV.Len();}
int GetReservedKeyIds() const {return KeyDatV.Reserved();}
bool IsKeyIdEqKeyN() const {return FreeKeys==0;}
int AddKey(const TKey& Key);
TDat& AddDatId(const TKey& Key){
int KeyId=AddKey(Key); return KeyDatV[KeyId].Dat=KeyId;}
TDat& AddDat(const TKey& Key){return KeyDatV[AddKey(Key)].Dat;}
TDat& AddDat(const TKey& Key, const TDat& Dat){
return KeyDatV[AddKey(Key)].Dat=Dat;}
void DelKey(const TKey& Key);
void DelIfKey(const TKey& Key){
int KeyId; if (IsKey(Key, KeyId)){DelKeyId(KeyId);}}
void DelKeyId(const int& KeyId){DelKey(GetKey(KeyId));}
void DelKeyIdV(const TIntV& KeyIdV){
for (int KeyIdN=0; KeyIdN<KeyIdV.Len(); KeyIdN++){DelKeyId(KeyIdV[KeyIdN]);}}
void MarkDelKey(const TKey& Key); // marks the record as deleted - doesn't delete Dat (to avoid fragmentation)
void MarkDelKeyId(const int& KeyId){MarkDelKey(GetKey(KeyId));}
const TKey& GetKey(const int& KeyId) const {
return GetHashKeyDat(KeyId).Key;}
int GetKeyId(const TKey& Key) const;
int GetRndKeyId(TRnd& Rnd) const {
//IAssert((! Empty()) && IsKeyIdEqKeyN());
//return Rnd.GetUniDevInt(Len()); }
IAssert(! Empty()); // J: can handle hash tables with deleted entries
int KeyId = abs(Rnd.GetUniDevInt(KeyDatV.Len()));
while (KeyDatV[KeyId].HashCd == -1) {
KeyId = abs(Rnd.GetUniDevInt(KeyDatV.Len())); }
return KeyId; }
int GetRndKeyId(TRnd& Rnd, const double& EmptyFrac=0.8);
bool IsKey(const TKey& Key) const {return GetKeyId(Key)!=-1;}
bool IsKey(const TKey& Key, int& KeyId) const {
KeyId=GetKeyId(Key); return KeyId!=-1;}
bool IsKeyId(const int& KeyId) const {
return (0<=KeyId)&&(KeyId<KeyDatV.Len())&&(KeyDatV[KeyId].HashCd!=-1);}
const TDat& GetDat(const TKey& Key) const {return KeyDatV[GetKeyId(Key)].Dat;}
TDat& GetDat(const TKey& Key){return KeyDatV[GetKeyId(Key)].Dat;}
// TKeyDatP GetKeyDat(const int& KeyId) const {
// TKeyDat& KeyDat=GetHashKeyDat(KeyId);
// return TKeyDatP(KeyDat.Key, KeyDat.Dat);}
void GetKeyDat(const int& KeyId, TKey& Key, TDat& Dat) const {
const TKeyDat& KeyDat=GetHashKeyDat(KeyId);
Key=KeyDat.Key; Dat=KeyDat.Dat;}
bool IsKeyGetDat(const TKey& Key, TDat& Dat) const {int KeyId;
if (IsKey(Key, KeyId)){Dat=GetHashKeyDat(KeyId).Dat; return true;}
else {return false;}}
int FFirstKeyId() const {return 0-1;}
bool FNextKeyId(int& KeyId) const;
void GetKeyV(TVec<TKey>& KeyV) const;
void GetDatV(TVec<TDat>& DatV) const;
void GetKeyDatPrV(TVec<TPair<TKey, TDat> >& KeyDatPrV) const;
void GetDatKeyPrV(TVec<TPair<TDat, TKey> >& DatKeyPrV) const;
void Swap(THash& Hash);
void Defrag();
void Pack(){KeyDatV.Pack();}
void Sort(const bool& CmpKey, const bool& Asc);
void SortByKey(const bool& Asc=true) { Sort(true, Asc); }
void SortByDat(const bool& Asc=true) { Sort(false, Asc); }
};
template<class TKey, class TDat, class THashFunc>
const unsigned int THash<TKey, TDat, THashFunc>::HashPrimeT[HashPrimes]={
3ul, 5ul, 11ul, 23ul,
53ul, 97ul, 193ul, 389ul, 769ul,
1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
1610612741ul, 3221225473ul, 4294967291ul
};
template<class TKey, class TDat, class THashFunc>
uint THash<TKey, TDat, THashFunc>::GetNextPrime(const uint& Val) const {
const uint* f=(const uint*)HashPrimeT, *m, *l=(const uint*)HashPrimeT + (int)HashPrimes;
int h, len = (int)HashPrimes;
while (len > 0) {
h = len >> 1; m = f + h;
if (*m < Val) { f = m; f++; len = len - h - 1; }
else len = h;
}
return f == l ? *(l - 1) : *f;
}
template<class TKey, class TDat, class THashFunc>
void THash<TKey, TDat, THashFunc>::Resize(){
// resize & initialize port vector
//if (PortV.Len()==0){PortV.Gen(17);}
//else {PortV.Gen(2*PortV.Len()+1);}
if (PortV.Len()==0){
PortV.Gen(17);
} else if (AutoSizeP&&(KeyDatV.Len()>2*PortV.Len())){
PortV.Gen(GetNextPrime(PortV.Len()+1));
} else {
return;
}
PortV.PutAll(TInt(-1));
// rehash keys
for (int KeyId=0; KeyId<KeyDatV.Len(); KeyId++){
TKeyDat& KeyDat=KeyDatV[KeyId];
if (KeyDat.HashCd!=-1){
const int PortN = abs(THashFunc::GetPrimHashCd(KeyDat.Key) % PortV.Len());
KeyDat.Next=PortV[PortN];
PortV[PortN]=KeyId;
}
}
}
template<class TKey, class TDat, class THashFunc>
THash<TKey, TDat, THashFunc>::THash(const int& ExpectVals, const bool& _AutoSizeP):
PortV(GetNextPrime(ExpectVals/2)), KeyDatV(ExpectVals, 0),
AutoSizeP(_AutoSizeP), FFreeKeyId(-1), FreeKeys(0){
PortV.PutAll(TInt(-1));
}
template<class TKey, class TDat, class THashFunc>
bool THash<TKey, TDat, THashFunc>::operator==(const THash& Hash) const {
if (Len() != Hash.Len()) { return false; }
for (int i = FFirstKeyId(); FNextKeyId(i); ) {
const TKey& Key = GetKey(i);
if (! Hash.IsKey(Key)) { return false; }
if (GetDat(Key) != Hash.GetDat(Key)) { return false; }
}
return true;
}
template<class TKey, class TDat, class THashFunc>
void THash<TKey, TDat, THashFunc>::Clr(const bool& DoDel, const int& NoDelLim, const bool& ResetDat){
if (DoDel){
PortV.Clr(); KeyDatV.Clr();
} else {
PortV.PutAll(TInt(-1));
KeyDatV.Clr(DoDel, NoDelLim);
if (ResetDat){KeyDatV.PutAll(TKeyDat());}
}
FFreeKeyId=TInt(-1); FreeKeys=TInt(0);
}
template<class TKey, class TDat, class THashFunc>
int THash<TKey, TDat, THashFunc>::AddKey(const TKey& Key){
if ((KeyDatV.Len()>2*PortV.Len())||PortV.Empty()){Resize();}
const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len());
const int HashCd=abs(THashFunc::GetSecHashCd(Key));
int PrevKeyId=-1;
int KeyId=PortV[PortN];
while ((KeyId!=-1) &&
!((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){
PrevKeyId=KeyId; KeyId=KeyDatV[KeyId].Next;}
if (KeyId==-1){
if (FFreeKeyId==-1){
KeyId=KeyDatV.Add(TKeyDat(-1, HashCd, Key));
} else {
KeyId=FFreeKeyId; FFreeKeyId=KeyDatV[FFreeKeyId].Next; FreeKeys--;
//KeyDatV[KeyId]=TKeyDat(-1, HashCd, Key); // slow version
KeyDatV[KeyId].Next=-1;
KeyDatV[KeyId].HashCd=HashCd;
KeyDatV[KeyId].Key=Key;
//KeyDatV[KeyId].Dat=TDat(); // already empty
}
if (PrevKeyId==-1){
PortV[PortN]=KeyId;
} else {
KeyDatV[PrevKeyId].Next=KeyId;
}
}
return KeyId;
}
template<class TKey, class TDat, class THashFunc>
void THash<TKey, TDat, THashFunc>::DelKey(const TKey& Key){
IAssert(!PortV.Empty());
const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len());
const int HashCd=abs(THashFunc::GetSecHashCd(Key));
int PrevKeyId=-1;
int KeyId=PortV[PortN];
while ((KeyId!=-1) &&
!((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){
PrevKeyId=KeyId; KeyId=KeyDatV[KeyId].Next;}
IAssertR(KeyId!=-1, Key.GetStr()); //J: vsi razredi nimajo nujno funkcije GetStr()?
if (PrevKeyId==-1){PortV[PortN]=KeyDatV[KeyId].Next;}
else {KeyDatV[PrevKeyId].Next=KeyDatV[KeyId].Next;}
KeyDatV[KeyId].Next=FFreeKeyId; FFreeKeyId=KeyId; FreeKeys++;
KeyDatV[KeyId].HashCd=TInt(-1);
KeyDatV[KeyId].Key=TKey();
KeyDatV[KeyId].Dat=TDat();
}
template<class TKey, class TDat, class THashFunc>
void THash<TKey, TDat, THashFunc>::MarkDelKey(const TKey& Key){
// MarkDelKey is same as Delkey expect last two lines
IAssert(!PortV.Empty());
const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len());
const int HashCd=abs(THashFunc::GetSecHashCd(Key));
int PrevKeyId=-1;
int KeyId=PortV[PortN];
while ((KeyId!=-1) &&
!((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){
PrevKeyId=KeyId; KeyId=KeyDatV[KeyId].Next;}
IAssertR(KeyId!=-1, Key.GetStr());
if (PrevKeyId==-1){PortV[PortN]=KeyDatV[KeyId].Next;}
else {KeyDatV[PrevKeyId].Next=KeyDatV[KeyId].Next;}
KeyDatV[KeyId].Next=FFreeKeyId; FFreeKeyId=KeyId; FreeKeys++;
KeyDatV[KeyId].HashCd=TInt(-1);
}
// return random KeyId even if the hash table contains deleted keys
// defrags the table if necessary
template<class TKey, class TDat, class THashFunc>
int THash<TKey, TDat, THashFunc>::GetRndKeyId(TRnd& Rnd, const double& EmptyFrac) {
IAssert(! Empty());
if (FreeKeys/double(Len()+FreeKeys) > EmptyFrac) { Defrag(); }
int KeyId = Rnd.GetUniDevInt(KeyDatV.Len());
while (KeyDatV[KeyId].HashCd == -1) {
KeyId = Rnd.GetUniDevInt(KeyDatV.Len());
}
return KeyId;
}
template<class TKey, class TDat, class THashFunc>
int THash<TKey, TDat, THashFunc>::GetKeyId(const TKey& Key) const {
if (PortV.Empty()){return -1;}
const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len());
const int HashCd=abs(THashFunc::GetSecHashCd(Key));
int KeyId=PortV[PortN];
while ((KeyId!=-1) &&
!((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){
KeyId=KeyDatV[KeyId].Next;}
return KeyId;
}
template<class TKey, class TDat, class THashFunc>
bool THash<TKey, TDat, THashFunc>::FNextKeyId(int& KeyId) const {
do {KeyId++;} while ((KeyId<KeyDatV.Len())&&(KeyDatV[KeyId].HashCd==-1));
return KeyId<KeyDatV.Len();
}
template<class TKey, class TDat, class THashFunc>
void THash<TKey, TDat, THashFunc>::GetKeyV(TVec<TKey>& KeyV) const {
KeyV.Gen(Len(), 0);
int KeyId=FFirstKeyId();
while (FNextKeyId(KeyId)){
KeyV.Add(GetKey(KeyId));}
}
template<class TKey, class TDat, class THashFunc>
void THash<TKey, TDat, THashFunc>::GetDatV(TVec<TDat>& DatV) const {
DatV.Gen(Len(), 0);
int KeyId=FFirstKeyId();
while (FNextKeyId(KeyId)){
DatV.Add(GetHashKeyDat(KeyId).Dat);}
}
template<class TKey, class TDat, class THashFunc>
void THash<TKey, TDat, THashFunc>::GetKeyDatPrV(TVec<TPair<TKey, TDat> >& KeyDatPrV) const {
KeyDatPrV.Gen(Len(), 0);
TKey Key; TDat Dat;
int KeyId=FFirstKeyId();
while (FNextKeyId(KeyId)){
GetKeyDat(KeyId, Key, Dat);
KeyDatPrV.Add(TPair<TKey, TDat>(Key, Dat));
}
}
template<class TKey, class TDat, class THashFunc>
void THash<TKey, TDat, THashFunc>::GetDatKeyPrV(TVec<TPair<TDat, TKey> >& DatKeyPrV) const {
DatKeyPrV.Gen(Len(), 0);
TKey Key; TDat Dat;
int KeyId=FFirstKeyId();
while (FNextKeyId(KeyId)){
GetKeyDat(KeyId, Key, Dat);
DatKeyPrV.Add(TPair<TDat, TKey>(Dat, Key));
}
}
template<class TKey, class TDat, class THashFunc>
void THash<TKey, TDat, THashFunc>::Swap(THash& Hash) {
if (this!=&Hash){
PortV.Swap(Hash.PortV);
KeyDatV.Swap(Hash.KeyDatV);
::Swap(AutoSizeP, Hash.AutoSizeP);
::Swap(FFreeKeyId, Hash.FFreeKeyId);
::Swap(FreeKeys, Hash.FreeKeys);
}
}
template<class TKey, class TDat, class THashFunc>
void THash<TKey, TDat, THashFunc>::Defrag(){
if (!IsKeyIdEqKeyN()){
THash<TKey, TDat, THashFunc> Hash(PortV.Len());
int KeyId=FFirstKeyId(); TKey Key; TDat Dat;
while (FNextKeyId(KeyId)){
GetKeyDat(KeyId, Key, Dat);
Hash.AddDat(Key, Dat);
}
Pack();
operator=(Hash);
IAssert(IsKeyIdEqKeyN());
}
}
template<class TKey, class TDat, class THashFunc>
void THash<TKey, TDat, THashFunc>::Sort(const bool& CmpKey, const bool& Asc) {
IAssertR(IsKeyIdEqKeyN(), "THash::Sort only works when table has no deleted keys.");
TIntV TargV(Len()), MapV(Len()), StateV(Len());
for (int i = 0; i < TargV.Len(); i++) {
TargV[i] = i; MapV[i] = i; StateV[i] = i;
}
// sort KeyIds
THashKeyDatCmp HashCmp(*this, CmpKey, Asc);
TargV.SortCmp(HashCmp);
// now sort the update vector
THashKeyDat<TKey, TDat> Tmp;
for (int i = 0; i < TargV.Len()-1; i++) {
const int SrcPos = MapV[TargV[i]];
const int Loc = i;
// swap data
Tmp = KeyDatV[SrcPos];
KeyDatV[SrcPos] = KeyDatV[Loc];
KeyDatV[Loc] = Tmp;
// swap keys
MapV[StateV[i]] = SrcPos;
StateV.Swap(Loc, SrcPos);
}
for (int i = 0; i < TargV.Len(); i++) {
MapV[TargV[i]] = i; }
for (int p = 0; p < PortV.Len(); p++) {
if (PortV[p] != -1) {
PortV[p] = MapV[PortV[p]]; } }
for (int i = 0; i < KeyDatV.Len(); i++) {
if (KeyDatV[i].Next != -1) {
KeyDatV[i].Next = MapV[KeyDatV[i].Next]; }
}
}
/////////////////////////////////////////////////
// Common-Hash-Types
typedef THash<TCh, TCh> TChChH;
typedef THash<TInt, TInt> TIntH;
typedef THash<TUInt64, TInt> TUInt64H;
typedef THash<TInt, TBool> TIntBoolH;
typedef THash<TInt, TInt> TIntIntH;
typedef THash<TInt, TIntFltPr> TIntIntFltPrH;
typedef THash<TInt, TIntV> TIntIntVH;
typedef THash<TInt, TFlt> TIntFltH;
typedef THash<TInt, TFltTr> TIntFltTrH;
typedef THash<TInt, TFltV> TIntFltVH;
typedef THash<TInt, TStr> TIntStrH;
typedef THash<TInt, TStrV> TIntStrVH;
typedef THash<TInt, TIntPr> TIntIntPrH;
typedef THash<TInt, TIntPrV> TIntIntPrVH;
typedef THash<TIntPr, TInt> TIntPrIntH;
typedef THash<TIntTr, TInt> TIntTrIntH;
typedef THash<TIntV, TInt> TIntVIntH;
typedef THash<TUInt, TUInt> TUIntH;
typedef THash<TIntPr, TFlt> TIntPrFltH;
typedef THash<TIntPr, TStr> TIntPrStrH;
typedef THash<TIntPr, TStrV> TIntPrStrVH;
typedef THash<TFlt, TFlt> TFltFltH;
typedef THash<TStr, TInt> TStrH;
typedef THash<TStr, TBool> TStrBoolH;
typedef THash<TStr, TInt> TStrIntH;
typedef THash<TStr, TIntPr> TStrIntPrH;
typedef THash<TStr, TIntV> TStrIntVH;
typedef THash<TStr, TIntPrV> TStrIntPrVH;
typedef THash<TStr, TFlt> TStrFltH;
typedef THash<TStr, TFltV> TStrFltVH;
typedef THash<TStr, TStr> TStrStrH;
typedef THash<TStr, TStrPr> TStrStrPrH;
typedef THash<TStr, TStrV> TStrStrVH;
typedef THash<TStr, TStrPrV> TStrStrPrVH;
typedef THash<TStr, TStrKdV> TStrStrKdVH;
typedef THash<TStr, TIntFltPr> TStrIntFltPrH;
typedef THash<TStr, TStrIntPrV> TStrStrIntPrVH;
typedef THash<TStr, TStrIntKdV> TStrStrIntKdVH;
typedef THash<TDbStr, TInt> TDbStrIntH;
typedef THash<TDbStr, TStr> TDbStrStrH;
typedef THash<TStrPr, TBool> TStrPrBoolH;
typedef THash<TStrPr, TInt> TStrPrIntH;
typedef THash<TStrPr, TFlt> TStrPrFltH;
typedef THash<TStrPr, TStr> TStrPrStrH;
typedef THash<TStrPr, TStrV> TStrPrStrVH;
typedef THash<TStrIntPr, TInt> TStrIntPrIntH;
typedef THash<TStrV, TInt> TStrVH;
typedef THash<TStrV, TInt> TStrVIntH;
typedef THash<TStrV, TIntV> TStrVIntVH;
typedef THash<TStrV, TStr> TStrVStrH;
typedef THash<TStrV, TStrV> TStrVStrVH;
/////////////////////////////////////////////////
// Hash-Pointer
template <class TKey, class TDat>
class PHash{
private:
TCRef CRef;
public:
THash<TKey, TDat> H;
public:
PHash<TKey, TDat>(): H(){}
static TPt<PHash<TKey, TDat> > New(){
return new PHash<TKey, TDat>();}
PHash<TKey, TDat>(const int& MxVals, const int& Vals): H(MxVals, Vals){}
static TPt<PHash<TKey, TDat> > New(const int& MxVals, const int& Vals){
return new PHash<TKey, TDat>(MxVals, Vals);}
PHash<TKey, TDat>(const THash<TKey, TDat>& _V): H(_V){}
static TPt<PHash<TKey, TDat> > New(const THash<TKey, TDat>& H){
return new PHash<TKey, TDat>(H);}
explicit PHash<TKey, TDat>(TSIn& SIn): H(SIn){}
static TPt<PHash<TKey, TDat> > Load(TSIn& SIn){return new PHash<TKey, TDat>(SIn);}
void Save(TSOut& SOut) const {H.Save(SOut);}
PHash<TKey, TDat>& operator=(const PHash<TKey, TDat>& Vec){
if (this!=&Vec){H=Vec.H;} return *this;}
bool operator==(const PHash<TKey, TDat>& Vec) const {return H==Vec.H;}
bool operator<(const PHash<TKey, TDat>& Vec) const {return H<Vec.H;}
friend class TPt<PHash<TKey, TDat> >;
};
/////////////////////////////////////////////////
// Big-String-Pool (holds up to 2 giga strings, storage overhead is 8(4) bytes per string)
//J: have to put it here since it uses TVec (can't be in dt.h)
ClassTP(TBigStrPool, PBigStrPool)//{
private:
TSize MxBfL, BfL;
uint GrowBy;
char *Bf;
TVec<TSize> IdOffV; // string ID to offset
private:
void Resize(TSize _MxBfL);
public:
TBigStrPool(TSize MxBfLen = 0, uint _GrowBy = 16*1024*1024);
TBigStrPool(TSIn& SIn, bool LoadCompact = true);
TBigStrPool(const TBigStrPool& Pool) : MxBfL(Pool.MxBfL), BfL(Pool.BfL), GrowBy(Pool.GrowBy) {
Bf = (char *) malloc(Pool.MxBfL); IAssert(Bf); memcpy(Bf, Pool.Bf, Pool.BfL); }
~TBigStrPool() { if (Bf) free(Bf); else IAssert(MxBfL == 0); MxBfL = 0; BfL = 0; }
static PBigStrPool New(TSize _MxBfLen = 0, uint _GrowBy = 16*1024*1024) { return PBigStrPool(new TBigStrPool(_MxBfLen, _GrowBy)); }
static PBigStrPool New(TSIn& SIn) { return new TBigStrPool(SIn); }
static PBigStrPool New(const TStr& fileName) { PSIn SIn = TFIn::New(fileName); return new TBigStrPool(*SIn); }
static PBigStrPool Load(TSIn& SIn, bool LoadCompacted = true) { return PBigStrPool(new TBigStrPool(SIn, LoadCompacted)); }
void Save(TSOut& SOut) const;
void Save(const TStr& fileName) { TFOut FOut(fileName); Save(FOut); }
int GetStrs() const { return IdOffV.Len(); }
TSize Len() const { return BfL; }
TSize Size() const { return MxBfL; }
bool Empty() const { return ! Len(); }
char* operator () () const { return Bf; }
TBigStrPool& operator = (const TBigStrPool& Pool);
int AddStr(const char *Str, uint Len);
int AddStr(const char *Str) { return AddStr(Str, uint(strlen(Str)) + 1); }
int AddStr(const TStr& Str) { return AddStr(Str.CStr(), Str.Len() + 1); }
TStr GetStr(const int& StrId) const { Assert(StrId < GetStrs());
if (StrId == 0) return TStr::GetNullStr(); else return TStr(Bf + (TSize)IdOffV[StrId]); }
const char *GetCStr(const int& StrId) const { Assert(StrId < GetStrs());
if (StrId == 0) return TStr::GetNullStr().CStr(); else return (Bf + (TSize)IdOffV[StrId]); }
TStr GetStrFromOffset(const TSize& Offset) const { Assert(Offset < BfL);
if (Offset == 0) return TStr::GetNullStr(); else return TStr(Bf + Offset); }
const char *GetCStrFromOffset(const TSize& Offset) const { Assert(Offset < BfL);
if (Offset == 0) return TStr::GetNullStr().CStr(); else return Bf + Offset; }
void Clr(bool DoDel = false) { BfL = 0; if (DoDel && Bf) { free(Bf); Bf = 0; MxBfL = 0; } }
int Cmp(const int& StrId, const char *Str) const { Assert(StrId < GetStrs());
if (StrId != 0) return strcmp(Bf + (TSize)IdOffV[StrId], Str); else return strcmp("", Str); }
static int GetPrimHashCd(const char *CStr);
static int GetSecHashCd(const char *CStr);
int GetPrimHashCd(const int& StrId) { Assert(StrId < GetStrs());
if (StrId != 0) return GetPrimHashCd(Bf + (TSize)IdOffV[StrId]); else return GetPrimHashCd(""); }
int GetSecHashCd(const int& StrId) { Assert(StrId < GetStrs());
if (StrId != 0) return GetSecHashCd(Bf + (TSize)IdOffV[StrId]); else return GetSecHashCd(""); }
};
/////////////////////////////////////////////////
// String-Hash-Table
template <class TDat, class TStringPool = TStrPool, class THashFunc = TDefaultHashFunc<TStr> >
class TStrHash{
private:
//typedef typename PStringPool::TObj TStringPool;
typedef TPt<TStringPool> PStringPool;
typedef THashKeyDat<TInt, TDat> TKeyDat;
typedef TPair<TInt, TDat> TKeyDatP;
typedef TVec<TKeyDat> TKeyDatV;
TIntV PortV;
TKeyDatV KeyDatV;
TBool AutoSizeP;
TInt FFreeKeyId, FreeKeys;
PStringPool Pool;
private:
uint GetNextPrime(const uint& Val) const;
void Resize();
const TKeyDat& GetHashKeyDat(const int& KeyId) const {
const TKeyDat& KeyDat = KeyDatV[KeyId]; Assert(KeyDat.HashCd != -1); return KeyDat; }
TKeyDat& GetHashKeyDat(const int& KeyId) {
TKeyDat& KeyDat = KeyDatV[KeyId]; Assert(KeyDat.HashCd != -1); return KeyDat; }
public:
TStrHash(): PortV(), KeyDatV(), AutoSizeP(true), FFreeKeyId(-1), FreeKeys(0), Pool() { }
TStrHash(const PStringPool& StrPool): PortV(), KeyDatV(), AutoSizeP(true), FFreeKeyId(-1), FreeKeys(0), Pool(StrPool) { }
TStrHash(const int& Ports, const bool& _AutoSizeP = false, const PStringPool& StrPool = PStringPool()) :
PortV(Ports), KeyDatV(Ports, 0), AutoSizeP(_AutoSizeP), FFreeKeyId(-1), FreeKeys(0), Pool(StrPool) { PortV.PutAll(-1); }
TStrHash(const TStrHash& Hash): PortV(Hash.PortV), KeyDatV(Hash.KeyDatV), AutoSizeP(Hash.AutoSizeP),
FFreeKeyId(Hash.FFreeKeyId), FreeKeys(Hash.FreeKeys), Pool() {
if (! Hash.Pool.Empty()) { Pool=PStringPool(new TStringPool(*Hash.Pool)); } }
TStrHash(TSIn& SIn, bool PoolToo = true): PortV(SIn), KeyDatV(SIn), AutoSizeP(SIn), FFreeKeyId(SIn), FreeKeys(SIn){ SIn.LoadCs(); if (PoolToo) Pool = PStringPool(SIn); }
void Load(TSIn& SIn, bool PoolToo = true) { PortV.Load(SIn); KeyDatV.Load(SIn); AutoSizeP.Load(SIn); FFreeKeyId.Load(SIn);
FreeKeys.Load(SIn); SIn.LoadCs(); if (PoolToo) Pool = PStringPool(SIn); }
void Save(TSOut& SOut, bool PoolToo = true) const { PortV.Save(SOut); KeyDatV.Save(SOut);
AutoSizeP.Save(SOut); FFreeKeyId.Save(SOut); FreeKeys.Save(SOut); SOut.SaveCs(); if (PoolToo) Pool.Save(SOut); }
void SetPool(const PStringPool& StrPool) { IAssert(Pool.Empty() || Pool->Empty()); Pool = StrPool; }
PStringPool GetPool() const { return Pool; }
TStrHash& operator = (const TStrHash& Hash);
bool Empty() const {return ! Len(); }
int Len() const { return KeyDatV.Len() - FreeKeys; }
int Reserved() const { return KeyDatV.Reserved(); }
int GetPorts() const { return PortV.Len(); }
bool IsAutoSize() const { return AutoSizeP; }
int GetMxKeyIds() const { return KeyDatV.Len(); }
bool IsKeyIdEqKeyN() const {return ! FreeKeys; }
int AddKey(const char *Key);
int AddKey(const TStr& Key) { return AddKey(Key.CStr()); }
int AddKey(const TChA& Key) { return AddKey(Key.CStr()); }
int AddDat(const char *Key, const TDat& Dat) { const int KeyId = AddKey(Key); KeyDatV[KeyId].Dat = Dat; return KeyId; }
int AddDat(const TStr& Key, const TDat& Dat) { const int KeyId = AddKey(Key.CStr()); KeyDatV[KeyId].Dat = Dat; return KeyId; }
int AddDat(const TChA& Key, const TDat& Dat) { const int KeyId = AddKey(Key.CStr()); KeyDatV[KeyId].Dat = Dat; return KeyId; }
TDat& AddDat(const char *Key) { return KeyDatV[AddKey(Key)].Dat; }
TDat& AddDat(const TStr& Key) { return KeyDatV[AddKey(Key.CStr())].Dat; }
TDat& AddDat(const TChA& Key) { return KeyDatV[AddKey(Key.CStr())].Dat; }
TDat& AddDatId(const char *Key) { const int KeyId = AddKey(Key); return KeyDatV[KeyId].Dat = KeyId; }
TDat& AddDatId(const TStr& Key) { const int KeyId = AddKey(Key.CStr()); return KeyDatV[KeyId].Dat = KeyId; }
TDat& AddDatId(const TChA& Key) { const int KeyId = AddKey(Key.CStr()); return KeyDatV[KeyId].Dat = KeyId; }
const TDat& operator[](const int& KeyId) const {return GetHashKeyDat(KeyId).Dat;}
TDat& operator[](const int& KeyId){return GetHashKeyDat(KeyId).Dat;}
const TDat& operator () (const char *Key) const { return GetDat(Key);}
//TDat& operator ()(const char *Key){return AddDat(Key);} // add if not found
const TDat& GetDat(const char *Key) const { return KeyDatV[GetKeyId(Key)].Dat; }
const TDat& GetDat(const TStr& Key) const { return GetDat(Key.CStr()); }
TDat& GetDat(const char *Key) { return KeyDatV[GetKeyId(Key)].Dat; }
const TDat& GetDat(const TStr& Key) { return GetDat(Key.CStr()); }
const TDat& GetDat(const TChA& Key) { return GetDat(Key.CStr()); }
TDat& GetDatId(const int& KeyId) { return KeyDatV[KeyId].Dat; }
const TDat& GetDatId(const int& KeyId) const { return KeyDatV[KeyId].Dat; }
void GetKeyDat(const int& KeyId, int& KeyO, TDat& Dat) const { const TKeyDat& KeyDat = GetHashKeyDat(KeyId); KeyO = KeyDat.Key; Dat = KeyDat.Dat; }
void GetKeyDat(const int& KeyId, const char*& Key, TDat& Dat) const { const TKeyDat& KeyDat = GetHashKeyDat(KeyId); Key = KeyFromOfs(KeyDat.Key); Dat = KeyDat.Dat; }
void GetKeyDat(const int& KeyId, TStr& Key, TDat& Dat) const { const TKeyDat& KeyDat = GetHashKeyDat(KeyId); Key = KeyFromOfs(KeyDat.Key); Dat = KeyDat.Dat;}
void GetKeyDat(const int& KeyId, TChA& Key, TDat& Dat) const { const TKeyDat& KeyDat = GetHashKeyDat(KeyId); Key = KeyFromOfs(KeyDat.Key); Dat = KeyDat.Dat;}
int GetKeyId(const char *Key) const;
int GetKeyId(const TStr& Key) const { return GetKeyId(Key.CStr()); }
const char *GetKey(const int& KeyId) const { return Pool->GetCStr(GetHashKeyDat(KeyId).Key); }
int GetKeyOfs(const int& KeyId) const { return GetHashKeyDat(KeyId).Key; } // pool string id
const char *KeyFromOfs(const int& KeyO) const { return Pool->GetCStr(KeyO); }
bool IsKey(const char *Key) const { return GetKeyId(Key) != -1; }
bool IsKey(const TStr& Key) const { return GetKeyId(Key.CStr()) != -1; }
bool IsKey(const TChA& Key) const { return GetKeyId(Key.CStr()) != -1; }
bool IsKey(const char *Key, int& KeyId) const { KeyId = GetKeyId(Key); return KeyId != -1; }
bool IsKeyGetDat(const char *Key, TDat& Dat) const { const int KeyId = GetKeyId(Key); if (KeyId != -1) { Dat = KeyDatV[KeyId].Dat; return true; } else return false; }
bool IsKeyGetDat(const TStr& Key, TDat& Dat) const { const int KeyId = GetKeyId(Key.CStr()); if (KeyId != -1) { Dat = KeyDatV[KeyId].Dat; return true; } else return false; }
bool IsKeyGetDat(const TChA& Key, TDat& Dat) const { const int KeyId = GetKeyId(Key.CStr()); if (KeyId != -1) { Dat = KeyDatV[KeyId].Dat; return true; } else return false; }
bool IsKeyId(const int& KeyId) const { return 0 <= KeyId && KeyId < KeyDatV.Len() && KeyDatV[KeyId].HashCd != -1; }
int FFirstKeyId() const {return 0-1;}
bool FNextKeyId(int& KeyId) const;
void GetKeyV(TVec<TStr>& KeyV) const;
void GetStrIdV(TIntV& StrIdV) const;
void GetDatV(TVec<TDat>& DatV) const;
void GetKeyDatPrV(TVec<TPair<TStr, TDat> >& KeyDatPrV) const;
void GetDatKeyPrV(TVec<TPair<TDat, TStr> >& DatKeyPrV) const;
void Pack(){KeyDatV.Pack();}
};
template <class TDat, class TStringPool, class THashFunc>
uint TStrHash<TDat, TStringPool, THashFunc>::GetNextPrime(const uint& Val) const {
uint *f = (uint *) TIntH::HashPrimeT, *m, *l = (uint *) TIntH::HashPrimeT + (int) TIntH::HashPrimes;
int h, len = (int)TIntH::HashPrimes;
while (len > 0) {
h = len >> 1; m = f + h;
if (*m < Val) { f = m; f++; len = len - h - 1; }
else len = h;
}
return f == l ? *(l - 1) : *f;
}
template <class TDat, class TStringPool, class THashFunc>
void TStrHash<TDat, TStringPool, THashFunc>::Resize() {
// resize & initialize port vector
if (PortV.Empty()) { PortV.Gen(17); PortV.PutAll(-1); }
else
if (AutoSizeP && KeyDatV.Len() > 3 * PortV.Len()) {
const int NxPrime = GetNextPrime(KeyDatV.Len());
//printf("%s resize PortV: %d -> %d, Len: %d\n", GetTypeNm(*this).CStr(), PortV.Len(), NxPrime, Len());
PortV.Gen(NxPrime); PortV.PutAll(-1); }
else
return;
// rehash keys
const int NPorts = PortV.Len();
for (int i = 0; i < KeyDatV.Len(); i++) {
TKeyDat& KeyDat = KeyDatV[i];
if (KeyDat.HashCd != -1) {
const int Port = abs(THashFunc::GetPrimHashCd(Pool->GetCStr(KeyDat.Key)) % NPorts);
KeyDat.Next = PortV[Port];
PortV[Port] = i;
}
}
}
template <class TDat, class TStringPool, class THashFunc>
TStrHash<TDat, TStringPool, THashFunc>& TStrHash<TDat, TStringPool, THashFunc>:: operator = (const TStrHash& Hash) {
if (this != &Hash) {
PortV = Hash.PortV;
KeyDatV = Hash.KeyDatV;
AutoSizeP = Hash.AutoSizeP;
FFreeKeyId = Hash.FFreeKeyId;
FreeKeys = Hash.FreeKeys;
if (! Hash.Pool.Empty()) Pool = PStringPool(new TStringPool(*Hash.Pool));
else Pool = NULL;
}
return *this;
}
template <class TDat, class TStringPool, class THashFunc>
int TStrHash<TDat, TStringPool, THashFunc>::AddKey(const char *Key) {
if (Pool.Empty()) Pool = TStringPool::New();
if ((AutoSizeP && KeyDatV.Len() > PortV.Len()) || PortV.Empty()) Resize();
const int PortN = abs(THashFunc::GetPrimHashCd(Key) % PortV.Len());
const int HashCd = abs(THashFunc::GetSecHashCd(Key));
int PrevKeyId = -1;
int KeyId = PortV[PortN];
while (KeyId != -1 && ! (KeyDatV[KeyId].HashCd == HashCd && Pool->Cmp(KeyDatV[KeyId].Key, Key) == 0)) {
PrevKeyId = KeyId; KeyId = KeyDatV[KeyId].Next; }
if (KeyId == -1) {
const int StrId = Pool->AddStr(Key);
if (FFreeKeyId == -1) {
KeyId = KeyDatV.Add(TKeyDat(-1, HashCd, StrId));
} else {
KeyId = FFreeKeyId;
FFreeKeyId = KeyDatV[FFreeKeyId].Next;
FreeKeys--;
KeyDatV[KeyId] = TKeyDat(-1, HashCd, StrId);
}
if (PrevKeyId == -1) PortV[PortN] = KeyId;
else KeyDatV[PrevKeyId].Next = KeyId;
}
return KeyId;
}
template <class TDat, class TStringPool, class THashFunc>
int TStrHash<TDat, TStringPool, THashFunc>::GetKeyId(const char *Key) const {
if (PortV.Empty()) return -1;
const int PortN = abs(THashFunc::GetPrimHashCd(Key) % PortV.Len());
const int Hc = abs(THashFunc::GetSecHashCd(Key));
int KeyId = PortV[PortN];
while (KeyId != -1 && ! (KeyDatV[KeyId].HashCd == Hc && Pool->Cmp(KeyDatV[KeyId].Key, Key) == 0))
KeyId = KeyDatV[KeyId].Next;
return KeyId;
}
template <class TDat, class TStringPool, class THashFunc>
bool TStrHash<TDat, TStringPool, THashFunc>::FNextKeyId(int& KeyId) const {
do KeyId++; while (KeyId < KeyDatV.Len() && KeyDatV[KeyId].HashCd == -1);
return KeyId < KeyDatV.Len();
}
template <class TDat, class TStringPool, class THashFunc>
void TStrHash<TDat, TStringPool, THashFunc>::GetKeyV(TVec<TStr>& KeyV) const {
KeyV.Gen(Len(), 0);
int KeyId = FFirstKeyId();
while (FNextKeyId(KeyId))
KeyV.Add(GetKey(KeyId));
}
template <class TDat, class TStringPool, class THashFunc>
void TStrHash<TDat, TStringPool, THashFunc>::GetStrIdV(TIntV& StrIdV) const {
StrIdV.Gen(Len(), 0);
int KeyId = FFirstKeyId();
while (FNextKeyId(KeyId))
StrIdV.Add(GetKeyOfs(KeyId));
}
template <class TDat, class TStringPool, class THashFunc>
void TStrHash<TDat, TStringPool, THashFunc>::GetDatV(TVec<TDat>& DatV) const {
DatV.Gen(Len(), 0);
int KeyId = FFirstKeyId();
while (FNextKeyId(KeyId))
DatV.Add(GetHashKeyDat(KeyId).Dat);
}
template <class TDat, class TStringPool, class THashFunc>
void TStrHash<TDat, TStringPool, THashFunc>::GetKeyDatPrV(TVec<TPair<TStr, TDat> >& KeyDatPrV) const {
KeyDatPrV.Gen(Len(), 0);
TStr Str; TDat Dat;
int KeyId = FFirstKeyId();
while (FNextKeyId(KeyId)){
GetKeyDat(KeyId, Str, Dat);
KeyDatPrV.Add(TPair<TStr, TDat>(Str, Dat));
}
}
template <class TDat, class TStringPool, class THashFunc>
void TStrHash<TDat, TStringPool, THashFunc>::GetDatKeyPrV(TVec<TPair<TDat, TStr> >& DatKeyPrV) const {
DatKeyPrV.Gen(Len(), 0);
TStr Str; TDat Dat;
int KeyId = FFirstKeyId();
while (FNextKeyId(KeyId)){
GetKeyDat(KeyId, Str, Dat);
DatKeyPrV.Add(TPair<TDat, TStr>(Dat, Str));
}
}
/////////////////////////////////////////////////
// Cache
template <class TKey, class TDat, class THashFunc = TDefaultHashFunc<TKey> >
class TCache{
private:
typedef TLst<TKey> TKeyL; typedef TLstNd<TKey>* TKeyLN;
typedef TPair<TKeyLN, TDat> TKeyLNDatPr;
int MxMemUsed;
int CurMemUsed;
THash<TKey, TKeyLNDatPr, THashFunc> KeyDatH;
TKeyL TimeKeyL;
void* RefToBs;
void Purge(const int& MemToPurge);
public:
TCache(){}
TCache(const TCache&);
TCache(const int& _MxMemUsed, const int& Ports, void* _RefToBs):
MxMemUsed(_MxMemUsed), CurMemUsed(0),
KeyDatH(Ports), TimeKeyL(), RefToBs(_RefToBs){}
TCache& operator=(const TCache&);
int GetMemUsed();
void RefreshMemUsed();
void Put(const TKey& Key, const TDat& Dat);
bool Get(const TKey& Key, TDat& Dat);
void Del(const TKey& Key, const bool& DoEventCall=true);
void Flush();
void FlushAndClr();
void* FFirstKeyDat();
bool FNextKeyDat(void*& KeyDatP, TKey& Key, TDat& Dat);
void PutRefToBs(void* _RefToBs){RefToBs=_RefToBs;}
void* GetRefToBs(){return RefToBs;}
};
template <class TKey, class TDat, class THashFunc>
void TCache<TKey, TDat, THashFunc>::Purge(const int& MemToPurge){
while (!TimeKeyL.Empty()&&(MxMemUsed-CurMemUsed<MemToPurge)){
TKey Key=TimeKeyL.Last()->GetVal();
Del(Key);
}
}
template <class TKey, class TDat, class THashFunc>
int TCache<TKey, TDat, THashFunc>::GetMemUsed(){
int MemUsed=0;
int KeyId=KeyDatH.FFirstKeyId();
while (KeyDatH.FNextKeyId(KeyId)){
const TKey& Key=KeyDatH.GetKey(KeyId);
TKeyLNDatPr& KeyLNDatPr=KeyDatH[KeyId];
TDat Dat=KeyLNDatPr.Val2;
MemUsed+=Key.GetMemUsed()+Dat->GetMemUsed();
}
return MemUsed;
}
template <class TKey, class TDat, class THashFunc>
void TCache<TKey, TDat, THashFunc>::RefreshMemUsed(){
CurMemUsed=GetMemUsed();
if (CurMemUsed>MxMemUsed){
Purge(CurMemUsed-MxMemUsed);}
}
template <class TKey, class TDat, class THashFunc>
void TCache<TKey, TDat, THashFunc>::Put(const TKey& Key, const TDat& Dat){
int KeyId=KeyDatH.GetKeyId(Key);
if (KeyId==-1){
int KeyDatMem=Key.GetMemUsed()+Dat->GetMemUsed();
if (CurMemUsed+KeyDatMem>MxMemUsed){Purge(KeyDatMem);}
CurMemUsed+=KeyDatMem;
TKeyLN KeyLN=TimeKeyL.AddFront(Key);
TKeyLNDatPr KeyLNDatPr(KeyLN, Dat);
KeyDatH.AddDat(Key, KeyLNDatPr);
} else {
TKeyLNDatPr& KeyLNDatPr=KeyDatH[KeyId];
TKeyLN KeyLN=KeyLNDatPr.Val1;
KeyLNDatPr.Val2=Dat;
TimeKeyL.PutFront(KeyLN);
}
}
template <class TKey, class TDat, class THashFunc>
bool TCache<TKey, TDat, THashFunc>::Get(const TKey& Key, TDat& Dat){
int KeyId=KeyDatH.GetKeyId(Key);
if (KeyId==-1){
return false;
} else {
Dat=KeyDatH[KeyId].Val2;
return true;
}
}
template <class TKey, class TDat, class THashFunc>
void TCache<TKey, TDat, THashFunc>::Del(const TKey& Key, const bool& DoEventCall){
int KeyId=KeyDatH.GetKeyId(Key);
if (KeyId!=-1){