6 #pragma pack(push, 1) // pack class size
7 template <
class TKey,
class TDat>
16 Next(-1), HashCd(-1), Key(), Dat(){}
17 THashKeyDat(
const int& _Next,
const int& _HashCd,
const TKey& _Key):
18 Next(_Next), HashCd(_HashCd), Key(_Key), Dat(){}
20 Next(SIn), HashCd(SIn), Key(SIn), Dat(SIn){}
22 Next.
Save(SOut); HashCd.
Save(SOut); Key.Save(SOut); Dat.Save(SOut);}
26 template<
typename TDatFunctor>
32 LoadDatFromShared(&Dat, ShMIn);
35 if (
this==&HashKeyDat || (HashCd==HashKeyDat.
HashCd
36 && Key==HashKeyDat.
Key && Dat==HashKeyDat.
Dat)){
return true;}
39 if (
this!=&HashKeyDat){
40 Next=HashKeyDat.
Next; HashCd=HashKeyDat.
HashCd;
41 Key=HashKeyDat.
Key; Dat=HashKeyDat.
Dat;}
48 template<
class TKey,
class TDat>
58 KeyDatI(_HashKeyDatI.KeyDatI), EndI(_HashKeyDatI.EndI){}
60 KeyDatI((THKeyDat*)_KeyDatI), EndI((THKeyDat*)_EndI){}
63 KeyDatI=HashKeyDatI.
KeyDatI; EndI=HashKeyDatI.
EndI;
return *
this;}
65 return KeyDatI==HashKeyDatI.
KeyDatI;}
67 return KeyDatI<HashKeyDatI.
KeyDatI;}
68 THashKeyDatI&
operator++(
int){ KeyDatI++;
while (KeyDatI < EndI && KeyDatI->HashCd==-1) { KeyDatI++; }
return *
this; }
76 bool IsEmpty()
const {
return KeyDatI == NULL; }
90 static inline int GetPrimHashCd(
const TKey& Key) {
return Key.GetPrimHashCd(); }
91 static inline int GetSecHashCd(
const TKey& Key) {
return Key.GetSecHashCd(); }
96 template<
class TKey,
class TDat,
class THashFunc = TDefaultHashFunc<TKey> >
116 Hash(_Hash), CmpKey(_CmpKey), Asc(_Asc) { }
119 if (Asc) {
return Hash.
GetKey(KeyId1) < Hash.
GetKey(KeyId2); }
120 else {
return Hash.
GetKey(KeyId2) < Hash.
GetKey(KeyId1); } }
122 if (Asc) {
return Hash[KeyId1] < Hash[KeyId2]; }
123 else {
return Hash[KeyId2] < Hash[KeyId1]; } } }
126 template<
typename TDatInitFn>
137 THKeyDat& KeyDat=KeyDatV[KeyId];
140 const THKeyDat& KeyDat=KeyDatV[KeyId];
147 AutoSizeP(true), FFreeKeyId(-1), FreeKeys(0){}
149 PortV(Hash.PortV), KeyDatV(Hash.KeyDatV), AutoSizeP(Hash.AutoSizeP),
150 FFreeKeyId(Hash.FFreeKeyId), FreeKeys(Hash.FreeKeys){}
151 explicit THash(
const int& ExpectVals,
const bool& _AutoSizeP=
false);
153 PortV(SIn), KeyDatV(SIn),
154 AutoSizeP(SIn), FFreeKeyId(SIn), FreeKeys(SIn){
160 AutoSizeP=
TBool(ShMIn);
161 FFreeKeyId=
TInt(ShMIn);
162 FreeKeys=
TInt(ShMIn);
166 template <
typename TDatInitFn>
168 TLoadTHKeyDatInitializer<TDatInitFn> HKeyDatFn(Fn);
170 KeyDatV.
LoadShM(ShMIn, HKeyDatFn);
171 AutoSizeP=
TBool(ShMIn);
172 FFreeKeyId=
TInt(ShMIn);
173 FreeKeys=
TInt(ShMIn);
179 AutoSizeP=
TBool(SIn); FFreeKeyId=
TInt(SIn); FreeKeys=
TInt(SIn);
184 PortV.
Save(SOut); KeyDatV.
Save(SOut);
185 AutoSizeP.
Save(SOut); FFreeKeyId.
Save(SOut); FreeKeys.
Save(SOut);
203 int64 MemUsed =
sizeof(bool)+2*
sizeof(
int);
205 for (
int KeyDatN = 0; KeyDatN < KeyDatV.
Len(); KeyDatN++) {
222 void Gen(
const int& ExpectVals){
224 FFreeKeyId=-1; FreeKeys=0; PortV.
PutAll(
TInt(-1));}
226 void Clr(
const bool& DoDel=
true,
const int& NoDelLim=-1,
const bool& ResetDat=
true);
235 int AddKey(
const TKey& Key);
237 int KeyId=
AddKey(Key);
return KeyDatV[KeyId].Dat=KeyId;}
239 TDat&
AddDat(
const TKey& Key,
const TDat& Dat){
240 return KeyDatV[
AddKey(Key)].Dat=Dat;}
242 void DelKey(
const TKey& Key);
244 int KeyId;
if (
IsKey(Key, KeyId)){
DelKeyId(KeyId);
return true;}
return false;}
247 for (
int KeyIdN=0; KeyIdN<KeyIdV.
Len(); KeyIdN++){
DelKeyId(KeyIdV[KeyIdN]);}}
253 int GetKeyId(
const TKey& Key)
const;
259 bool IsKey(
const TKey& Key,
int& KeyId)
const { KeyId=
GetKeyId(Key);
return KeyId!=-1;}
261 return (0<=KeyId)&&(KeyId<KeyDatV.
Len())&&(KeyDatV[KeyId].HashCd!=-1);}
266 return KeyId >= 0 ? KeyDatV[KeyId].Dat : DefaultValue;
271 void GetKeyDat(
const int& KeyId, TKey& Key, TDat& Dat)
const {
273 Key=KeyDat.
Key; Dat=KeyDat.
Dat;}
276 else {
return false;}}
290 void Sort(
const bool& CmpKey,
const bool& Asc);
295 template<
class TKey,
class TDat,
class THashFunc>
297 3ul, 5ul, 11ul, 23ul,
298 53ul, 97ul, 193ul, 389ul, 769ul,
299 1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
300 49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
301 1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
302 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
303 1610612741ul, 3221225473ul, 4294967291ul
306 template<
class TKey,
class TDat,
class THashFunc>
308 const uint* f=(
const uint*)HashPrimeT, *m, *l=(
const uint*)HashPrimeT + (int)HashPrimes;
309 int h, len = (int)HashPrimes;
311 h = len >> 1; m = f + h;
312 if (*m < Val) { f = m; f++; len = len - h - 1; }
315 return f == l ? *(l - 1) : *f;
318 template<
class TKey,
class TDat,
class THashFunc>
325 }
else if (AutoSizeP&&(KeyDatV.Len()>2*PortV.Len())){
326 PortV.Gen(GetNextPrime(PortV.Len()+1));
330 PortV.PutAll(
TInt(-1));
332 for (
int KeyId=0; KeyId<KeyDatV.Len(); KeyId++){
335 const int PortN = abs(THashFunc::GetPrimHashCd(KeyDat.
Key) % PortV.Len());
336 KeyDat.
Next=PortV[PortN];
342 template<
class TKey,
class TDat,
class THashFunc>
344 PortV(GetNextPrime(ExpectVals/2)), KeyDatV(ExpectVals, 0),
345 AutoSizeP(_AutoSizeP), FFreeKeyId(-1), FreeKeys(0){
349 template<
class TKey,
class TDat,
class THashFunc>
351 if (Len() != Hash.
Len()) {
return false; }
352 for (
int i = FFirstKeyId(); FNextKeyId(i); ) {
353 const TKey& Key = GetKey(i);
354 if (! Hash.
IsKey(Key)) {
return false; }
355 if (GetDat(Key) != Hash.
GetDat(Key)) {
return false; }
360 template<
class TKey,
class TDat,
class THashFunc>
363 PortV.Clr(); KeyDatV.Clr();
365 PortV.PutAll(
TInt(-1));
366 KeyDatV.Clr(DoDel, NoDelLim);
367 if (ResetDat){KeyDatV.PutAll(
THKeyDat());}
369 FFreeKeyId=
TInt(-1); FreeKeys=
TInt(0);
372 template<
class TKey,
class TDat,
class THashFunc>
374 if ((KeyDatV.Len()>2*PortV.Len())||PortV.Empty()){Resize();}
375 const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len());
376 const int HashCd=abs(THashFunc::GetSecHashCd(Key));
378 int KeyId=PortV[PortN];
379 while ((KeyId!=-1) &&
380 !((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){
381 PrevKeyId=KeyId; KeyId=KeyDatV[KeyId].Next;}
385 KeyId=KeyDatV.Add(
THKeyDat(-1, HashCd, Key));
387 KeyId=FFreeKeyId; FFreeKeyId=KeyDatV[FFreeKeyId].Next; FreeKeys--;
389 KeyDatV[KeyId].Next=-1;
390 KeyDatV[KeyId].HashCd=HashCd;
391 KeyDatV[KeyId].Key=Key;
397 KeyDatV[PrevKeyId].Next=KeyId;
403 template<
class TKey,
class TDat,
class THashFunc>
406 const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len());
407 const int HashCd=abs(THashFunc::GetSecHashCd(Key));
409 int KeyId=PortV[PortN];
411 while ((KeyId!=-1) &&
412 !((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){
413 PrevKeyId=KeyId; KeyId=KeyDatV[KeyId].Next;}
417 if (PrevKeyId==-1){PortV[PortN]=KeyDatV[KeyId].Next;}
418 else {KeyDatV[PrevKeyId].Next=KeyDatV[KeyId].Next;}
419 KeyDatV[KeyId].Next=FFreeKeyId; FFreeKeyId=KeyId; FreeKeys++;
420 KeyDatV[KeyId].HashCd=
TInt(-1);
421 KeyDatV[KeyId].Key=TKey();
422 KeyDatV[KeyId].Dat=TDat();
425 template<
class TKey,
class TDat,
class THashFunc>
429 const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len());
430 const int HashCd=abs(THashFunc::GetSecHashCd(Key));
432 int KeyId=PortV[PortN];
433 while ((KeyId!=-1) &&
434 !((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){
435 PrevKeyId=KeyId; KeyId=KeyDatV[KeyId].Next;}
437 if (PrevKeyId==-1){PortV[PortN]=KeyDatV[KeyId].Next;}
438 else {KeyDatV[PrevKeyId].Next=KeyDatV[KeyId].Next;}
439 KeyDatV[KeyId].Next=FFreeKeyId; FFreeKeyId=KeyId; FreeKeys++;
440 KeyDatV[KeyId].HashCd=
TInt(-1);
443 template<
class TKey,
class TDat,
class THashFunc>
447 while (KeyDatV[KeyId].HashCd == -1) {
454 template<
class TKey,
class TDat,
class THashFunc>
457 if (FreeKeys/
double(Len()+FreeKeys) > EmptyFrac) { Defrag(); }
459 while (KeyDatV[KeyId].HashCd == -1) {
465 template<
class TKey,
class TDat,
class THashFunc>
467 if (PortV.Empty()){
return -1;}
468 const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len());
469 const int HashCd=abs(THashFunc::GetSecHashCd(Key));
470 int KeyId=PortV[PortN];
471 while ((KeyId!=-1) &&
472 !((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){
473 KeyId=KeyDatV[KeyId].Next;}
477 template<
class TKey,
class TDat,
class THashFunc>
479 do {KeyId++;}
while ((KeyId<KeyDatV.Len()) && (KeyDatV[KeyId].HashCd==-1));
480 return KeyId<KeyDatV.Len();
483 template<
class TKey,
class TDat,
class THashFunc>
486 int KeyId=FFirstKeyId();
487 while (FNextKeyId(KeyId)){
488 KeyV.
Add(GetKey(KeyId));}
491 template<
class TKey,
class TDat,
class THashFunc>
494 int KeyId=FFirstKeyId();
495 while (FNextKeyId(KeyId)){
496 DatV.
Add(GetHashKeyDat(KeyId).Dat);}
499 template<
class TKey,
class TDat,
class THashFunc>
501 KeyDatPrV.Gen(Len(), 0);
503 int KeyId=FFirstKeyId();
504 while (FNextKeyId(KeyId)){
505 GetKeyDat(KeyId, Key, Dat);
510 template<
class TKey,
class TDat,
class THashFunc>
512 DatKeyPrV.Gen(Len(), 0);
514 int KeyId=FFirstKeyId();
515 while (FNextKeyId(KeyId)){
516 GetKeyDat(KeyId, Key, Dat);
521 template<
class TKey,
class TDat,
class THashFunc>
523 KeyDatKdV.Gen(Len(), 0);
525 int KeyId=FFirstKeyId();
526 while (FNextKeyId(KeyId)){
527 GetKeyDat(KeyId, Key, Dat);
532 template<
class TKey,
class TDat,
class THashFunc>
534 DatKeyKdV.Gen(Len(), 0);
536 int KeyId=FFirstKeyId();
537 while (FNextKeyId(KeyId)){
538 GetKeyDat(KeyId, Key, Dat);
543 template<
class TKey,
class TDat,
class THashFunc>
546 PortV.Swap(Hash.
PortV);
554 template<
class TKey,
class TDat,
class THashFunc>
556 if (!IsKeyIdEqKeyN()){
558 int KeyId=FFirstKeyId(); TKey Key; TDat Dat;
559 while (FNextKeyId(KeyId)){
560 GetKeyDat(KeyId, Key, Dat);
561 Hash.AddDat(Key, Dat);
569 template<
class TKey,
class TDat,
class THashFunc>
571 IAssertR(IsKeyIdEqKeyN(),
"THash::Sort only works when table has no deleted keys.");
572 TIntV TargV(Len()), MapV(Len()), StateV(Len());
573 for (
int i = 0; i < TargV.Len(); i++) {
574 TargV[i] = i; MapV[i] = i; StateV[i] = i;
578 TargV.SortCmp(HashCmp);
581 for (
int i = 0; i < TargV.Len()-1; i++) {
582 const int SrcPos = MapV[TargV[i]];
585 Tmp = KeyDatV[SrcPos];
586 KeyDatV[SrcPos] = KeyDatV[Loc];
589 MapV[StateV[i]] = SrcPos;
590 StateV.Swap(Loc, SrcPos);
592 for (
int i = 0; i < TargV.Len(); i++) {
593 MapV[TargV[i]] = i; }
594 for (
int p = 0; p < PortV.Len(); p++) {
595 if (PortV[p] != -1) {
596 PortV[p] = MapV[PortV[p]]; } }
597 for (
int i = 0; i < KeyDatV.Len(); i++) {
598 if (KeyDatV[i].Next != -1) {
599 KeyDatV[i].
Next = MapV[KeyDatV[i].Next]; }
680 template <
class TKey,
class TDat>
701 if (
this!=&Vec){H=Vec.
H;}
return *
this;}
703 bool operator<(const PHash<TKey, TDat>& Vec)
const {
return H<Vec.H;}
719 void Resize(
TSize _MxBfL);
720 void LoadPoolShM(
TShMIn& ShMIn,
bool LoadCompact = true);
725 Bf = (
char *) malloc(Pool.MxBfL);
IAssert(Bf); memcpy(Bf, Pool.Bf, Pool.BfL); }
744 bool Empty()
const {
return ! Len(); }
748 return 4 *
sizeof(int) + IdOffV.GetMemUsed() + MxBfL;
751 int AddStr(
const char *Str,
uint Len);
765 void Clr(
bool DoDel =
false) {
766 BfL = 0;
if (DoDel && Bf) {
if (!IsShM) { free(Bf);} Bf = 0; MxBfL = 0;}}
767 int Cmp(
const int& StrId,
const char *Str)
const {
Assert(StrId < GetStrs());
768 if (StrId != 0)
return strcmp(Bf + (
TSize)IdOffV[StrId], Str);
else return strcmp(
"", Str); }
780 template <
class TDat,
class TStringPool = TStrPool,
class THashFunc = TDefaultHashFunc<TStr> >
798 const THKeyDat& KeyDat = KeyDatV[KeyId];
Assert(KeyDat.
HashCd != -1);
return KeyDat; }
800 THKeyDat& KeyDat = KeyDatV[KeyId];
Assert(KeyDat.
HashCd != -1);
return KeyDat; }
802 TStrHash(): PortV(), KeyDatV(), AutoSizeP(true), FFreeKeyId(-1), FreeKeys(0), Pool() { }
803 TStrHash(
const PStringPool& StrPool): PortV(), KeyDatV(), AutoSizeP(true), FFreeKeyId(-1), FreeKeys(0), Pool(StrPool) { }
805 PortV(Ports), KeyDatV(Ports, 0), AutoSizeP(_AutoSizeP), FFreeKeyId(-1), FreeKeys(0), Pool(StrPool) { PortV.
PutAll(-1); }
806 TStrHash(
const TStrHash& Hash): PortV(Hash.PortV), KeyDatV(Hash.KeyDatV), AutoSizeP(Hash.AutoSizeP),
807 FFreeKeyId(Hash.FFreeKeyId), FreeKeys(Hash.FreeKeys), Pool() {
809 TStrHash(
TSIn& SIn,
bool PoolToo =
true): PortV(SIn), KeyDatV(SIn), AutoSizeP(SIn), FFreeKeyId(SIn), FreeKeys(SIn){ SIn.
LoadCs();
if (PoolToo) Pool =
PStringPool(SIn); }
818 AutoSizeP.
Load(ShMIn);
819 FFreeKeyId.
Load(ShMIn);
820 FreeKeys.
Load(ShMIn);
826 Pool = TStringPool::LoadShM(ShMIn);
834 AutoSizeP.
Save(SOut); FFreeKeyId.
Save(SOut); FreeKeys.
Save(SOut); SOut.
SaveCs();
if (PoolToo) Pool.
Save(SOut); }
849 int AddKey(
const char *Key);
852 int AddDat(
const char *Key,
const TDat& Dat) {
const int KeyId =
AddKey(Key); KeyDatV[KeyId].Dat = Dat;
return KeyId; }
853 int AddDat(
const TStr& Key,
const TDat& Dat) {
const int KeyId =
AddKey(Key.
CStr()); KeyDatV[KeyId].Dat = Dat;
return KeyId; }
854 int AddDat(
const TChA& Key,
const TDat& Dat) {
const int KeyId =
AddKey(Key.
CStr()); KeyDatV[KeyId].Dat = Dat;
return KeyId; }
858 TDat&
AddDatId(
const char *Key) {
const int KeyId =
AddKey(Key);
return KeyDatV[KeyId].Dat = KeyId; }
867 int64 MemUsed =
sizeof(bool)+2*
sizeof(
int);
869 for (
int KeyDatN = 0; KeyDatN < KeyDatV.
Len(); KeyDatN++) {
884 TDat&
GetDatId(
const int& KeyId) {
return KeyDatV[KeyId].Dat; }
885 const TDat&
GetDatId(
const int& KeyId)
const {
return KeyDatV[KeyId].Dat; }
891 int GetKeyId(
const char *Key)
const;
900 bool IsKey(
const char *Key,
int& KeyId)
const { KeyId =
GetKeyId(Key);
return KeyId != -1; }
901 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; }
902 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; }
903 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; }
904 bool IsKeyId(
const int& KeyId)
const {
return 0 <= KeyId && KeyId < KeyDatV.
Len() && KeyDatV[KeyId].HashCd != -1; }
918 template <
class TDat,
class TStringPool,
class THashFunc>
923 h = len >> 1; m = f + h;
924 if (*m < Val) { f = m; f++; len = len - h - 1; }
927 return f == l ? *(l - 1) : *f;
930 template <
class TDat,
class TStringPool,
class THashFunc>
935 if (AutoSizeP && KeyDatV.
Len() > 3 * PortV.
Len()) {
936 const int NxPrime = GetNextPrime(KeyDatV.
Len());
942 const int NPorts = PortV.
Len();
943 for (
int i = 0; i < KeyDatV.
Len(); i++) {
944 THKeyDat& KeyDat = KeyDatV[i];
945 if (KeyDat.
HashCd != -1) {
946 const int Port = abs(THashFunc::GetPrimHashCd(Pool->
GetCStr(KeyDat.
Key)) % NPorts);
947 KeyDat.
Next = PortV[Port];
953 template <
class TDat,
class TStringPool,
class THashFunc>
961 if (! Hash.
Pool.
Empty()) Pool = PStringPool(
new TStringPool(*Hash.
Pool));
967 template <
class TDat,
class TStringPool,
class THashFunc>
969 if (Pool.
Empty()) Pool = TStringPool::New();
970 if ((AutoSizeP && KeyDatV.
Len() > PortV.
Len()) || PortV.
Empty()) Resize();
971 const int PortN = abs(THashFunc::GetPrimHashCd(Key) % PortV.
Len());
972 const int HashCd = abs(THashFunc::GetSecHashCd(Key));
974 int KeyId = PortV[PortN];
975 while (KeyId != -1 && ! (KeyDatV[KeyId].HashCd == HashCd && Pool->
Cmp(KeyDatV[KeyId].Key, Key) == 0)) {
976 PrevKeyId = KeyId; KeyId = KeyDatV[KeyId].Next; }
978 const int StrId = Pool->
AddStr(Key);
979 if (FFreeKeyId == -1) {
980 KeyId = KeyDatV.
Add(THKeyDat(-1, HashCd, StrId));
983 FFreeKeyId = KeyDatV[FFreeKeyId].Next;
985 KeyDatV[KeyId] = THKeyDat(-1, HashCd, StrId);
987 if (PrevKeyId == -1) PortV[PortN] = KeyId;
988 else KeyDatV[PrevKeyId].Next = KeyId;
993 template <
class TDat,
class TStringPool,
class THashFunc>
995 if (PortV.
Empty())
return -1;
996 const int PortN = abs(THashFunc::GetPrimHashCd(Key) % PortV.
Len());
997 const int Hc = abs(THashFunc::GetSecHashCd(Key));
998 int KeyId = PortV[PortN];
999 while (KeyId != -1 && ! (KeyDatV[KeyId].HashCd == Hc && Pool->
Cmp(KeyDatV[KeyId].Key, Key) == 0))
1000 KeyId = KeyDatV[KeyId].Next;
1004 template <
class TDat,
class TStringPool,
class THashFunc>
1006 do KeyId++;
while (KeyId < KeyDatV.
Len() && KeyDatV[KeyId].HashCd == -1);
1007 return KeyId < KeyDatV.
Len();
1010 template <
class TDat,
class TStringPool,
class THashFunc>
1013 int KeyId = FFirstKeyId();
1014 while (FNextKeyId(KeyId))
1015 KeyV.
Add(GetKey(KeyId));
1018 template <
class TDat,
class TStringPool,
class THashFunc>
1020 StrIdV.
Gen(Len(), 0);
1021 int KeyId = FFirstKeyId();
1022 while (FNextKeyId(KeyId))
1023 StrIdV.
Add(GetKeyOfs(KeyId));
1026 template <
class TDat,
class TStringPool,
class THashFunc>
1029 int KeyId = FFirstKeyId();
1030 while (FNextKeyId(KeyId))
1031 DatV.
Add(GetHashKeyDat(KeyId).Dat);
1034 template <
class TDat,
class TStringPool,
class THashFunc>
1036 KeyDatPrV.Gen(Len(), 0);
1038 int KeyId = FFirstKeyId();
1039 while (FNextKeyId(KeyId)){
1040 GetKeyDat(KeyId, Str, Dat);
1045 template <
class TDat,
class TStringPool,
class THashFunc>
1047 DatKeyPrV.Gen(Len(), 0);
1049 int KeyId = FFirstKeyId();
1050 while (FNextKeyId(KeyId)){
1051 GetKeyDat(KeyId, Str, Dat);
1064 template <
class TKey,
class TDat,
class THashFunc = TDefaultHashFunc<TKey> >
1079 MxMemUsed(_MxMemUsed), CurMemUsed(0),
1080 KeyDatH(Ports), TimeKeyL(), RefToBs(_RefToBs){}
1087 void Put(
const TKey& Key,
const TDat& Dat);
1088 bool Get(
const TKey& Key, TDat& Dat);
1089 void Del(
const TKey& Key,
const bool& DoEventCall=
true);
1093 bool FNextKeyDat(
void*& KeyDatP, TKey& Key, TDat& Dat);
1099 template <
class TKey,
class TDat,
class THashFunc>
1101 const int64 StartMemUsed = CurMemUsed;
1102 while (!TimeKeyL.Empty()&&(StartMemUsed-CurMemUsed<MemToPurge)){
1103 TKey Key=TimeKeyL.Last()->GetVal();
1108 template <
class TKey,
class TDat,
class THashFunc>
1111 int KeyId=KeyDatH.FFirstKeyId();
1112 while (KeyDatH.FNextKeyId(KeyId)){
1113 const TKey& Key=KeyDatH.GetKey(KeyId);
1115 TDat Dat=KeyLNDatPr.
Val2;
1116 MemUsed+=
int64(Key.GetMemUsed()+Dat->GetMemUsed());
1121 template <
class TKey,
class TDat,
class THashFunc>
1123 CurMemUsed=GetMemUsed();
1124 if (CurMemUsed>MxMemUsed){
1125 Purge(CurMemUsed-MxMemUsed);
1131 template <
class TKey,
class TDat,
class THashFunc>
1133 int KeyId=KeyDatH.GetKeyId(Key);
1135 int64 KeyDatMem=
int64(Key.GetMemUsed()+Dat->GetMemUsed());
1136 if (CurMemUsed+KeyDatMem>MxMemUsed){Purge(KeyDatMem);}
1137 CurMemUsed+=KeyDatMem;
1138 TKeyLN KeyLN=TimeKeyL.AddFront(Key);
1140 KeyDatH.AddDat(Key, KeyLNDatPr);
1144 KeyLNDatPr.
Val2=Dat;
1145 TimeKeyL.PutFront(KeyLN);
1149 template <
class TKey,
class TDat,
class THashFunc>
1151 int KeyId=KeyDatH.GetKeyId(Key);
1155 Dat=KeyDatH[KeyId].Val2;
1160 template <
class TKey,
class TDat,
class THashFunc>
1162 int KeyId=KeyDatH.GetKeyId(Key);
1166 TDat& Dat=KeyLNDatPr.
Val2;
1168 Dat->OnDelFromCache(Key, RefToBs);}
1169 CurMemUsed-=
int64(Key.GetMemUsed()+Dat->GetMemUsed());
1171 TimeKeyL.Del(KeyLN);
1172 KeyDatH.DelKeyId(KeyId);
1176 template <
class TKey,
class TDat,
class THashFunc>
1178 printf(
"To flush: %d\n", KeyDatH.Len());
1179 int KeyId=KeyDatH.FFirstKeyId();
int Done = 0;
1180 while (KeyDatH.FNextKeyId(KeyId)){
1181 if (Done%10000==0){printf(
"%d\r", Done);}
1182 const TKey& Key=KeyDatH.GetKey(KeyId);
1184 TDat Dat=KeyLNDatPr.
Val2;
1185 Dat->OnDelFromCache(Key, RefToBs);
1188 printf(
"Done %d\n", KeyDatH.Len());
1191 template <
class TKey,
class TDat,
class THashFunc>
1199 template <
class TKey,
class TDat,
class THashFunc>
1201 return TimeKeyL.First();
1204 template <
class TKey,
class TDat,
class THashFunc>
1209 Key=
TKeyLN(KeyDatP)->
GetVal(); Dat=KeyDatH.GetDat(Key).Val2;
1210 KeyDatP=
TKeyLN(KeyDatP)->
Next();
return true;
1221 const int MulBy = 16;
1223 while (*p) { HashCd = (MulBy * HashCd) + *p++; HashCd &= 0x0FFFFFFF; }
1226 const int MulBy = 16;
1228 while (*p) { HashCd = (MulBy * HashCd) ^ *p++; HashCd &= 0x0FFFFFFF; }
1247 unsigned int hash = 5381;
1248 for(
unsigned int i = 0; i < Len; Str++, i++) {
1249 hash = ((hash << 5) + hash) + (*Str); }
1254 const char *r = p;
while (*r) { r++; }
1255 return (
int)
DJBHash((
const char *) p, r - p) & 0x7fffffff; }
1257 const char *r = p;
while (*r) { r++; }
1258 return (
int)
DJBHash((
const char *) p, r - p) & 0x7fffffff; }
1264 return (
int)
DJBHash((
const char *) p, Len) & 0x7fffffff; }
1266 return (
int)
DJBHash((
const char *) p, Len) & 0x7fffffff; }
1270 template <
class TVec>
1275 for (
int ValN=0; ValN<Vec.
Len(); ValN++){
1281 for (
int ValN=0; ValN<Vec.
Len(); ValN++){
bool IsKey(const TChA &Key) const
void GetKeyDat(const int &KeyId, TChA &Key, TDat &Dat) const
bool operator==(const THashKeyDat &HashKeyDat) const
const TDat & GetDat(const TStr &Key) const
void GetKeyDat(const int &KeyId, TStr &Key, TDat &Dat) const
TIter EndI() const
Returns an iterator referring to the past-the-end element in the vector.
THash< TInt, TFltPr > TIntFltPrH
THash< TStr, TStr > TStrStrH
int AddDat(const TStr &Key, const TDat &Dat)
PStringPool GetPool() const
bool IsKeyIdEqKeyN() const
void LoadShM(TShMIn &ShMIn, TDatFunctor LoadDatFromShared)
TSizeTy Reserved() const
Returns the size of allocated storage capacity.
int GetKeyOfs(const int &KeyId) const
TVec< THKeyDat > THKeyDatV
#define IAssertR(Cond, Reason)
THash< TStr, TFltV > TStrFltVH
int GetPrimHashCd() const
Returns primary hash code of the vector. Used by THash.
THashKeyDatI(const THashKeyDatI &_HashKeyDatI)
THash< TStr, TInt > TStrH
THash< TInt, TIntVFltVPr > TIntIntVFltVPrH
THash< TIntTr, TFlt > TIntTrFltH
THashKeyDat & operator=(const THashKeyDat &HashKeyDat)
void GetDatV(TVec< TDat > &DatV) const
TPt & operator=(const TPt &Pt)
THash< TIntPr, TIntPrV > TIntPrIntPrVH
THash< TIntTr, TInt > TIntTrIntH
::TSize GetMemUsed() const
const char * GetCStr(const int &StrId) const
TDat & operator[](const int &KeyId)
TDat & GetDat(const char *Key)
bool IsKeyGetDat(const TChA &Key, TDat &Dat) const
void Sort(const bool &CmpKey, const bool &Asc)
THash< TInt, TStrPrV > TIntStrPrVH
THash & operator=(const THash &Hash)
bool operator==(const THashKeyDatI &HashKeyDatI) const
void LoadPoolShM(TShMIn &ShMIn, bool LoadCompact=true)
THash< TStr, TUInt64V > TStrUInt64VH
THash< TUInt64, TInt > TUInt64H
THash< TStrIntPr, TInt > TStrIntPrIntH
static PBigStrPool New(TSize _MxBfLen=0, uint _GrowBy=16 *1024 *1024)
void Save(TSOut &SOut) const
bool operator==(const THash &Hash) const
THash< TStr, TIntV > TStrIntVH
const THKeyDat & GetHashKeyDat(const int &KeyId) const
bool IsKeyId(const int &KeyId) const
TDat & operator()(const TKey &Key)
TCache(const int64 &_MxMemUsed, const int &Ports, void *_RefToBs)
THash< TInt, TIntH > TIntIntHH
void GetKeyDat(const int &KeyId, TKey &Key, TDat &Dat) const
static int GetSecHashCd(const TStr &s)
void GetKeyDat(const int &KeyId, const char *&Key, TDat &Dat) const
TSizeTy Len() const
Returns the number of elements in the vector.
void operator()(THKeyDat *HKeyDat, TShMIn &ShMIn)
int GetSecHashCd() const
Returns secondary hash code of the vector. Used by THash.
THash< TStr, TStrPr > TStrStrPrH
void Save(TSOut &SOut) const
TDat & AddDatId(const TStr &Key)
static int GetSecHashCd(const TKey &Key)
static const unsigned int HashPrimeT[HashPrimes]
static PBigStrPool New(const TStr &fileName)
int AddStr(const char *Str, uint Len)
TStr GetStr(const int &StrId) const
int AddStr(const TStr &Str)
void Purge(const int64 &MemToPurge)
TPair< TKeyLN, TDat > TKeyLNDatPr
TDat & operator[](const int &KeyId)
THashKeyDat(const int &_Next, const int &_HashCd, const TKey &_Key)
void SetPool(const PStringPool &StrPool)
THash< TIntPr, TStrV > TIntPrStrVH
bool IsKeyGetDat(const TStr &Key, TDat &Dat) const
TDat GetDatWithDefault(const TKey &Key, TDat DefaultValue)
int AddDat(const char *Key, const TDat &Dat)
THash< TStrTr, TInt > TStrTrIntH
TDat & AddDatId(const char *Key)
void Save(TSOut &SOut) const
THash< TIntPr, TFlt > TIntPrFltH
int AddKey(const TStr &Key)
THash< TInt, TUInt64 > TIntUInt64H
static int GetPrimHashCd(const char *p)
TPt< TBigStrPool > PBigStrPool
void LoadXml(const PXmlTok &XmlTok, const TStr &Nm="")
THash< TIntPr, TIntV > TIntPrIntVH
void Put(const TKey &Key, const TDat &Dat)
THash< TInt, TStr > TIntStrH
const TDat & GetDat(const TKey &Key) const
bool IsEmpty() const
Tests whether the iterator has been initialized.
void DelKeyId(const int &KeyId)
const TDat & operator[](const int &KeyId) const
TDat & GetDat(const TKey &Key)
THash< TIntPr, TInt > TIntPrIntH
void DelKeyIdV(const TIntV &KeyIdV)
TSizeTy GetMemUsed() const
Returns the memory footprint (the number of bytes) of the vector.
static int GetSecHashCd(const char *p)
bool FNextKeyId(int &KeyId) const
const char * GetCStrFromOffset(const TSize &Offset) const
THash< TUInt, TUInt > TUIntH
void GetDatKeyPrV(TVec< TPair< TDat, TKey > > &DatKeyPrV) const
void GetDatV(TVec< TDat > &DatV) const
THash< TChTr, TInt > TChTrIntH
void LoadShM(TShMIn &ShMIn)
Load THash from shared memory file. Copying/Deleting Keys is illegal.
TStr GetStrFromOffset(const TSize &Offset) const
void Save(TSOut &SOut) const
TPair< TKey, TDat > TKeyDatP
const TKey & GetKey() const
void Save(const TStr &fileName)
THash< TStrV, TInt > TStrVIntH
int GetSecHashCd(const int &StrId)
void Save(TSOut &SOut) const
int AddStr(const char *Str)
bool Empty() const
Tests whether the vector is empty.
bool Get(const TKey &Key, TDat &Dat)
THash< TDbStr, TStr > TDbStrStrH
THash< TStrPr, TStrV > TStrPrStrVH
#define ClassTP(TNm, PNm)
THash< TStrV, TStrV > TStrVStrVH
TStrHash< TInt > TStrIntSH
const char * KeyFromOfs(const int &KeyO) const
static PSIn New(const TStr &FNm)
void DelKey(const TKey &Key)
int AddKey(const TChA &Key)
int Cmp(const int &StrId, const char *Str) const
void MarkDelKey(const TKey &Key)
THash< TInt, TFltTr > TIntFltTrH
bool operator<(const THashKeyDatI &HashKeyDatI) const
THash< TInt, TFlt > TIntFltH
THash< TInt, TFltV > TIntFltVH
void LoadXml(const PXmlTok &XmlTok, const TStr &Nm="")
const TDat & GetDat() const
static TPt< PHash< TKey, TDat > > New()
THash< TInt, TInt > TIntIntH
THash< TStr, TIntPr > TStrIntPrH
void Gen(const int &ExpectVals)
void GetDatKeyPrV(TVec< TPair< TDat, TStr > > &DatKeyPrV) const
bool IsKey(const char *Key) const
THash< TStr, TIntFltPr > TStrIntFltPrH
THashKeyDat< TKey, TDat > THKeyDat
static int GetSecHashCd(const char *p, const ::TSize &Len)
bool IsEnd() const
Tests whether the iterator is pointing to the past-end element.
TDat & AddDat(const TKey &Key, const TDat &Dat)
static int GetPrimHashCd(const TStr &s)
THash< TInt, TIntFltPr > TIntIntFltPrH
void PutAll(const TVal &Val)
Sets all elements of the vector to value Val.
void PutRefToBs(void *_RefToBs)
static TPt< PHash< TKey, TDat > > New(const int &MxVals, const int &Vals)
bool IsKeyIdEqKeyN() const
THash< TStrV, TIntV > TStrVIntVH
THash< TStr, TStrIntKdV > TStrStrIntKdVH
static PBigStrPool New(TSIn &SIn)
const char * GetKey(const int &KeyId) const
static int GetPrimHashCd(const char *p, const ::TSize &Len)
TPair< TInt, TDat > TKeyDatP
void GetKeyDatPrV(TVec< TPair< TStr, TDat > > &KeyDatPrV) const
void Del(const TKey &Key, const bool &DoEventCall=true)
static TPt< PHash< TKey, TDat > > Load(TSIn &SIn)
THashKeyDatI & operator++(int)
bool FNextKeyId(int &KeyId) const
TLoadTHKeyDatInitializer(TDatInitFn Fn)
bool IsKeyGetDat(const TKey &Key, TDat &Dat) const
THash< TInt, TIntV > TIntIntVH
void LoadShM(TShMIn &ShMIn, bool SharedPool=true)
Load hash from shared memory. If shared pool is true load pool from shared memory.
THash< TIntIntPrPr, TFlt > TIntIntPrPrFltH
THashKeyDatCmp(THash< TKey, TDat, THashFunc > &_Hash, const bool &_CmpKey, const bool &_Asc)
THashKeyDatI(const THKeyDat *_KeyDatI, const THKeyDat *_EndI)
THash< TIntIntPrPr, TInt > TIntIntPrPrIntH
TRec * operator()() const
::TSize GetMemUsed() const
const THKeyDat & GetHashKeyDat(const int &KeyId) const
void Save(TSOut &SOut, bool PoolToo=true) const
static int GetPrimHashCd(const char *p)
THash< TStrPr, TBool > TStrPrBoolH
THash< TStr, TFlt > TStrFltH
void GetKeyDat(const int &KeyId, int &KeyO, TDat &Dat) const
THash< TIntV, TInt > TIntVIntH
int64 GetMxMemUsed() const
void Save(TSOut &SOut) const
THash< TStrPr, TStr > TStrPrStrH
TDat & GetDatId(const int &KeyId)
THash< TKey, TKeyLNDatPr, THashFunc > KeyDatH
TStrHash(const int &Ports, const bool &_AutoSizeP=false, const PStringPool &StrPool=PStringPool())
const TDat & operator[](const int &KeyId) const
The [] operator takes KeyId, use GetDat() if you need value access via the key.
THash< TInt, TInt > TIntH
PHash< TKey, TDat > & operator=(const PHash< TKey, TDat > &Vec)
THashKeyDatI< TKey, TDat > TIter
const TDat & operator()(const char *Key) const
int AddKey(const char *Key)
uint GetNextPrime(const uint &Val) const
THash< TDbStr, TInt > TDbStrIntH
THashKeyDat< TKey, TDat > THKeyDat
bool FNextKeyDat(void *&KeyDatP, TKey &Key, TDat &Dat)
THash< TStr, TUInt64 > TStrUInt64H
void SortByKey(const bool &Asc=true)
int GetKeyId(const TKey &Key) const
const TDat & GetDatId(const int &KeyId) const
static PBigStrPool LoadShM(TShMIn &ShMIn, bool LoadCompact=true)
Load the string pool with the buffer backed by shared memory.
THash< TIntStrPr, TInt > TIntStrPrIntH
void LoadShM(TShMIn &ShMIn)
Constructs the vector from a shared memory input.
TStrHash(const PStringPool &StrPool)
int AddKey(const TKey &Key)
static PBigStrPool Load(TSIn &SIn, bool LoadCompacted=true)
void Load(TSIn &SIn, bool PoolToo=true)
TDat & AddDat(const TStr &Key)
THash< TIntIntPrPr, TStr > TIntIntPrPrStrH
int GetKeyId(const TStr &Key) const
THash< TStr, TStrKdV > TStrStrKdVH
bool operator<(const THash &Hash) const
THKeyDat * operator->() const
THash< TStr, TIntPrV > TStrIntPrVH
void GetStrIdV(TIntV &StrIdV) const
void GetKeyV(TVec< TKey > &KeyV) const
THash< TIntPr, TStr > TIntPrStrH
TStrHash(const TStrHash &Hash)
THash< TStr, TStrIntPrV > TStrStrIntPrVH
void Save(TSOut &SOut) const
int GetReservedKeyIds() const
uint GetNextPrime(const uint &Val) const
void LoadShM(TShMIn &ShMIn, TDatInitFn Fn)
Load THash from shared memory passing in the Dat initializer.
const TDat & GetDat(const TStr &Key)
TIter BegI() const
Returns an iterator pointing to the first element in the vector.
void Pack()
Reduces vector capacity (frees memory) to match its size.
THashKeyDatI & operator=(const THashKeyDatI &HashKeyDatI)
THash< TStr, TStrV > TStrStrVH
THash< TIntPr, TIntPr > TIntPrH
THKeyDat & GetHashKeyDat(const int &KeyId)
void SaveXml(TSOut &SOut, const TStr &Nm)
static int GetPrimHashCd(const TKey &Key)
void GetKeyDatPrV(TVec< TPair< TKey, TDat > > &KeyDatPrV) const
THKeyDat & operator*() const
void GetKeyV(TVec< TStr > &KeyV) const
static int GetSecHashCd(const TVec &Vec)
bool operator()(const int &KeyId1, const int &KeyId2) const
bool DelIfKey(const TKey &Key)
void Clr(bool DoDel=false)
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
THash< TStrV, TInt > TStrVH
static int GetSecHashCd(const TStr &s)
THash< TStr, TBool > TStrBoolH
TStrHash< TIntV > TStrToIntVSH
int GetRndKeyId(TRnd &Rnd) const
Get an index of a random element. If the hash table has many deleted keys, this may take a long time...
THash< TStrV, TStr > TStrVStrH
THash< TFlt, TFlt > TFltFltH
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
TStrHash & operator=(const TStrHash &Hash)
THashKeyDat< TInt, TDat > THKeyDat
int GetUniDevInt(const int &Range=0)
int GetPrimHashCd(const int &StrId)
TDat & AddDat(const char *Key)
const THash< TKey, TDat, THashFunc > & Hash
bool IsKeyGetDat(const char *Key, TDat &Dat) const
TStrHash(TSIn &SIn, bool PoolToo=true)
TDat & AddDatId(const TKey &Key)
THKeyDat & GetHashKeyDat(const int &KeyId)
TPt< TStringPool > PStringPool
bool IsKey(const TKey &Key, int &KeyId) const
static int GetPrimHashCd(const TStr &s)
bool IsKey(const TKey &Key) const
bool operator==(const PHash< TKey, TDat > &Vec) const
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
static unsigned int DJBHash(const char *Str, const ::TSize &Len)
THash< TStr, TInt > TStrIntH
THash< TUInt64, TStrV > TUInt64StrVH
static TPt< PHash< TKey, TDat > > New(const THash< TKey, TDat > &H)
TDat & AddDat(const TKey &Key)
THKeyDat & operator()() const
bool IsKey(const char *Key, int &KeyId) const
TCache & operator=(const TCache &)
THash< TStrPr, TFlt > TStrPrFltH
THash< TStr, TStrPrV > TStrStrPrVH
TDat & AddDat(const TChA &Key)
THash< TInt, TStrV > TIntStrVH
THash< TInt, TIntPr > TIntIntPrH
THash< TStrPr, TInt > TStrPrIntH
static int GetPrimHashCd(const TVec &Vec)
TDat & AddDatId(const TChA &Key)
int GetPrimHashCd() const
int AddDat(const TChA &Key, const TDat &Dat)
const TKey & GetKey(const int &KeyId) const
bool IsKey(const TStr &Key) const
const TDat & GetDat(const TChA &Key)
void GetDatKeyKdV(TVec< TKeyDat< TDat, TKey > > &DatKeyKdV) const
THash< TInt, TIntPrV > TIntIntPrVH
void GetKeyDatKdV(TVec< TKeyDat< TKey, TDat > > &KeyDatKdV) const
THash< TInt, TIntStrPr > TIntIntStrPrH
static int GetSecHashCd(const char *p)
THash< TInt, TBool > TIntBoolH
int GetKeyId(const char *Key) const
void Swap(TRec &Rec1, TRec &Rec2)
void MarkDelKeyId(const int &KeyId)
bool IsKeyId(const int &KeyId) const
void SaveXml(TSOut &SOut, const TStr &Nm) const
THashKeyDatI & operator--(int)
const TDat & GetDat(const char *Key) const
static int GetPrimHashCd(const char *p)
TIter GetI(const TKey &Key) const
void SortByDat(const bool &Asc=true)
static int GetSecHashCd(const char *p)