SNAP Library 2.4, Developer Reference  2015-05-11 19:40:56
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
hash.h
Go to the documentation of this file.
1 #include "bd.h"
2 
4 // Hash-Table-Key-Data
5 #pragma pack(push, 1) // pack class size
6 template <class TKey, class TDat>
7 class THashKeyDat{
8 public:
11  TKey Key;
12  TDat Dat;
13 public:
15  Next(-1), HashCd(-1), Key(), Dat(){}
16  THashKeyDat(const int& _Next, const int& _HashCd, const TKey& _Key):
17  Next(_Next), HashCd(_HashCd), Key(_Key), Dat(){}
18  explicit THashKeyDat(TSIn& SIn):
19  Next(SIn), HashCd(SIn), Key(SIn), Dat(SIn){}
20  void Save(TSOut& SOut) const {
21  Next.Save(SOut); HashCd.Save(SOut); Key.Save(SOut); Dat.Save(SOut);}
22  void LoadXml(const PXmlTok& XmlTok, const TStr& Nm="");
23  void SaveXml(TSOut& SOut, const TStr& Nm) const;
24 
25  bool operator==(const THashKeyDat& HashKeyDat) const {
26  if (this==&HashKeyDat || (HashCd==HashKeyDat.HashCd
27  && Key==HashKeyDat.Key && Dat==HashKeyDat.Dat)){return true;}
28  return false;}
29  THashKeyDat& operator=(const THashKeyDat& HashKeyDat){
30  if (this!=&HashKeyDat){
31  Next=HashKeyDat.Next; HashCd=HashKeyDat.HashCd;
32  Key=HashKeyDat.Key; Dat=HashKeyDat.Dat;}
33  return *this;}
34 };
35 #pragma pack(pop)
36 
38 // Hash-Table-Key-Data-Iterator
39 template<class TKey, class TDat>
41 public:
43 private:
46 public:
47  THashKeyDatI(): KeyDatI(NULL), EndI(NULL){}
48  THashKeyDatI(const THashKeyDatI& _HashKeyDatI):
49  KeyDatI(_HashKeyDatI.KeyDatI), EndI(_HashKeyDatI.EndI){}
50  THashKeyDatI(const THKeyDat* _KeyDatI, const THKeyDat* _EndI):
51  KeyDatI((THKeyDat*)_KeyDatI), EndI((THKeyDat*)_EndI){}
52 
53  THashKeyDatI& operator=(const THashKeyDatI& HashKeyDatI){
54  KeyDatI=HashKeyDatI.KeyDatI; EndI=HashKeyDatI.EndI; return *this;}
55  bool operator==(const THashKeyDatI& HashKeyDatI) const {
56  return KeyDatI==HashKeyDatI.KeyDatI;}
57  bool operator<(const THashKeyDatI& HashKeyDatI) const {
58  return KeyDatI<HashKeyDatI.KeyDatI;}
59  THashKeyDatI& operator++(int){ KeyDatI++; while (KeyDatI < EndI && KeyDatI->HashCd==-1) { KeyDatI++; } return *this; }
60  THashKeyDatI& operator--(int){ do { KeyDatI--; } while (KeyDatI->HashCd==-1); return *this;}
61  THKeyDat& operator*() const { return *KeyDatI; }
62  THKeyDat& operator()() const { return *KeyDatI; }
63  THKeyDat* operator->() const { return KeyDatI; }
64  THashKeyDatI& Next(){ operator++(1); return *this; }
65 
67  bool IsEmpty() const { return KeyDatI == NULL; }
69  bool IsEnd() const { return EndI == KeyDatI; }
70 
71  const TKey& GetKey() const {Assert((KeyDatI!=NULL)&&(KeyDatI->HashCd!=-1)); return KeyDatI->Key;}
72  const TDat& GetDat() const {Assert((KeyDatI!=NULL)&&(KeyDatI->HashCd!=-1)); return KeyDatI->Dat;}
73  TDat& GetDat() {Assert((KeyDatI!=NULL)&&(KeyDatI->HashCd!=-1)); return KeyDatI->Dat;}
74 };
75 
77 // Default-Hash-Function
78 template<class TKey>
80 public:
81  static inline int GetPrimHashCd(const TKey& Key) { return Key.GetPrimHashCd(); }
82  static inline int GetSecHashCd(const TKey& Key) { return Key.GetSecHashCd(); }
83 };
84 
86 // Hash-Table
87 template<class TKey, class TDat, class THashFunc = TDefaultHashFunc<TKey> >
88 class THash{
89 public:
90  enum {HashPrimes=32};
91  static const unsigned int HashPrimeT[HashPrimes];
92 public:
94 private:
101 private:
103  public:
105  bool CmpKey, Asc;
106  THashKeyDatCmp(THash<TKey, TDat, THashFunc>& _Hash, const bool& _CmpKey, const bool& _Asc) :
107  Hash(_Hash), CmpKey(_CmpKey), Asc(_Asc) { }
108  bool operator () (const int& KeyId1, const int& KeyId2) const {
109  if (CmpKey) {
110  if (Asc) { return Hash.GetKey(KeyId1) < Hash.GetKey(KeyId2); }
111  else { return Hash.GetKey(KeyId2) < Hash.GetKey(KeyId1); } }
112  else {
113  if (Asc) { return Hash[KeyId1] < Hash[KeyId2]; }
114  else { return Hash[KeyId2] < Hash[KeyId1]; } } }
115  };
116 private:
117  THKeyDat& GetHashKeyDat(const int& KeyId){
118  THKeyDat& KeyDat=KeyDatV[KeyId];
119  Assert(KeyDat.HashCd!=-1); return KeyDat;}
120  const THKeyDat& GetHashKeyDat(const int& KeyId) const {
121  const THKeyDat& KeyDat=KeyDatV[KeyId];
122  Assert(KeyDat.HashCd!=-1); return KeyDat;}
123  uint GetNextPrime(const uint& Val) const;
124  void Resize();
125 public:
126  THash():
127  PortV(), KeyDatV(),
128  AutoSizeP(true), FFreeKeyId(-1), FreeKeys(0){}
129  THash(const THash& Hash):
130  PortV(Hash.PortV), KeyDatV(Hash.KeyDatV), AutoSizeP(Hash.AutoSizeP),
131  FFreeKeyId(Hash.FFreeKeyId), FreeKeys(Hash.FreeKeys){}
132  explicit THash(const int& ExpectVals, const bool& _AutoSizeP=false);
133  explicit THash(TSIn& SIn):
134  PortV(SIn), KeyDatV(SIn),
135  AutoSizeP(SIn), FFreeKeyId(SIn), FreeKeys(SIn){
136  SIn.LoadCs();}
137  void Load(TSIn& SIn){
138  PortV.Load(SIn); KeyDatV.Load(SIn);
139  AutoSizeP=TBool(SIn); FFreeKeyId=TInt(SIn); FreeKeys=TInt(SIn);
140  SIn.LoadCs();}
141  void Save(TSOut& SOut) const {
142  PortV.Save(SOut); KeyDatV.Save(SOut);
143  AutoSizeP.Save(SOut); FFreeKeyId.Save(SOut); FreeKeys.Save(SOut);
144  SOut.SaveCs();}
145  void LoadXml(const PXmlTok& XmlTok, const TStr& Nm="");
146  void SaveXml(TSOut& SOut, const TStr& Nm);
147 
148  THash& operator=(const THash& Hash){
149  if (this!=&Hash){
150  PortV=Hash.PortV; KeyDatV=Hash.KeyDatV; AutoSizeP=Hash.AutoSizeP;
152  return *this;}
153  bool operator==(const THash& Hash) const; //J: zdaj tak kot je treba
154  bool operator < (const THash& Hash) const { Fail; return true; }
156  const TDat& operator[](const int& KeyId) const {return GetHashKeyDat(KeyId).Dat;}
157  TDat& operator[](const int& KeyId){return GetHashKeyDat(KeyId).Dat;}
158  TDat& operator()(const TKey& Key){return AddDat(Key);}
159  ::TSize GetMemUsed() const {
160  // return PortV.GetMemUsed()+KeyDatV.GetMemUsed()+sizeof(bool)+2*sizeof(int);}
161  int64 MemUsed = sizeof(bool)+2*sizeof(int);
162  MemUsed += int64(PortV.Reserved()) * int64(sizeof(TInt));
163  for (int KeyDatN = 0; KeyDatN < KeyDatV.Len(); KeyDatN++) {
164  MemUsed += int64(2 * sizeof(TInt));
165  MemUsed += int64(KeyDatV[KeyDatN].Key.GetMemUsed());
166  MemUsed += int64(KeyDatV[KeyDatN].Dat.GetMemUsed());
167  }
168  return ::TSize(MemUsed);
169  }
170 
171  TIter BegI() const {
172  if (Len() == 0){return TIter(KeyDatV.EndI(), KeyDatV.EndI());}
173  if (IsKeyIdEqKeyN()) { return TIter(KeyDatV.BegI(), KeyDatV.EndI());}
174  int FKeyId=-1; FNextKeyId(FKeyId);
175  return TIter(KeyDatV.BegI()+FKeyId, KeyDatV.EndI()); }
176  TIter EndI() const {return TIter(KeyDatV.EndI(), KeyDatV.EndI());}
177  //TIter GetI(const int& KeyId) const {return TIter(&KeyDatV[KeyId], KeyDatV.EndI());}
178  TIter GetI(const TKey& Key) const {return TIter(&KeyDatV[GetKeyId(Key)], KeyDatV.EndI());}
179 
180  void Gen(const int& ExpectVals){
181  PortV.Gen(GetNextPrime(ExpectVals/2)); KeyDatV.Gen(ExpectVals, 0);
182  FFreeKeyId=-1; FreeKeys=0; PortV.PutAll(TInt(-1));}
183 
184  void Clr(const bool& DoDel=true, const int& NoDelLim=-1, const bool& ResetDat=true);
185  bool Empty() const {return Len()==0;}
186  int Len() const {return KeyDatV.Len()-FreeKeys;}
187  int GetPorts() const {return PortV.Len();}
188  bool IsAutoSize() const {return AutoSizeP;}
189  int GetMxKeyIds() const {return KeyDatV.Len();}
190  int GetReservedKeyIds() const {return KeyDatV.Reserved();}
191  bool IsKeyIdEqKeyN() const {return FreeKeys==0;}
192 
193  int AddKey(const TKey& Key);
194  TDat& AddDatId(const TKey& Key){
195  int KeyId=AddKey(Key); return KeyDatV[KeyId].Dat=KeyId;}
196  TDat& AddDat(const TKey& Key){return KeyDatV[AddKey(Key)].Dat;}
197  TDat& AddDat(const TKey& Key, const TDat& Dat){
198  return KeyDatV[AddKey(Key)].Dat=Dat;}
199 
200  void DelKey(const TKey& Key);
201  bool DelIfKey(const TKey& Key){
202  int KeyId; if (IsKey(Key, KeyId)){DelKeyId(KeyId); return true;} return false;}
203  void DelKeyId(const int& KeyId){DelKey(GetKey(KeyId));}
204  void DelKeyIdV(const TIntV& KeyIdV){
205  for (int KeyIdN=0; KeyIdN<KeyIdV.Len(); KeyIdN++){DelKeyId(KeyIdV[KeyIdN]);}}
206 
207  void MarkDelKey(const TKey& Key); // marks the record as deleted - doesn't delete Dat (to avoid fragmentation)
208  void MarkDelKeyId(const int& KeyId){MarkDelKey(GetKey(KeyId));}
209 
210  const TKey& GetKey(const int& KeyId) const { return GetHashKeyDat(KeyId).Key;}
211  int GetKeyId(const TKey& Key) const;
213  int GetRndKeyId(TRnd& Rnd) const;
215  int GetRndKeyId(TRnd& Rnd, const double& EmptyFrac);
216  bool IsKey(const TKey& Key) const {return GetKeyId(Key)!=-1;}
217  bool IsKey(const TKey& Key, int& KeyId) const { KeyId=GetKeyId(Key); return KeyId!=-1;}
218  bool IsKeyId(const int& KeyId) const {
219  return (0<=KeyId)&&(KeyId<KeyDatV.Len())&&(KeyDatV[KeyId].HashCd!=-1);}
220  const TDat& GetDat(const TKey& Key) const {return KeyDatV[GetKeyId(Key)].Dat;}
221  TDat& GetDat(const TKey& Key){return KeyDatV[GetKeyId(Key)].Dat;}
222 // TKeyDatP GetKeyDat(const int& KeyId) const {
223 // TKeyDat& KeyDat=GetHashKeyDat(KeyId);
224 // return TKeyDatP(KeyDat.Key, KeyDat.Dat);}
225  void GetKeyDat(const int& KeyId, TKey& Key, TDat& Dat) const {
226  const THKeyDat& KeyDat=GetHashKeyDat(KeyId);
227  Key=KeyDat.Key; Dat=KeyDat.Dat;}
228  bool IsKeyGetDat(const TKey& Key, TDat& Dat) const {int KeyId;
229  if (IsKey(Key, KeyId)){Dat=GetHashKeyDat(KeyId).Dat; return true;}
230  else {return false;}}
231 
232  int FFirstKeyId() const {return 0-1;}
233  bool FNextKeyId(int& KeyId) const;
234  void GetKeyV(TVec<TKey>& KeyV) const;
235  void GetDatV(TVec<TDat>& DatV) const;
236  void GetKeyDatPrV(TVec<TPair<TKey, TDat> >& KeyDatPrV) const;
237  void GetDatKeyPrV(TVec<TPair<TDat, TKey> >& DatKeyPrV) const;
238  void GetKeyDatKdV(TVec<TKeyDat<TKey, TDat> >& KeyDatKdV) const;
239  void GetDatKeyKdV(TVec<TKeyDat<TDat, TKey> >& DatKeyKdV) const;
240 
241  void Swap(THash& Hash);
242  void Defrag();
243  void Pack(){KeyDatV.Pack();}
244  void Sort(const bool& CmpKey, const bool& Asc);
245  void SortByKey(const bool& Asc=true) { Sort(true, Asc); }
246  void SortByDat(const bool& Asc=true) { Sort(false, Asc); }
247 };
248 
249 template<class TKey, class TDat, class THashFunc>
250 const unsigned int THash<TKey, TDat, THashFunc>::HashPrimeT[HashPrimes]={
251  3ul, 5ul, 11ul, 23ul,
252  53ul, 97ul, 193ul, 389ul, 769ul,
253  1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
254  49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
255  1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
256  50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
257  1610612741ul, 3221225473ul, 4294967291ul
258 };
259 
260 template<class TKey, class TDat, class THashFunc>
262  const uint* f=(const uint*)HashPrimeT, *m, *l=(const uint*)HashPrimeT + (int)HashPrimes;
263  int h, len = (int)HashPrimes;
264  while (len > 0) {
265  h = len >> 1; m = f + h;
266  if (*m < Val) { f = m; f++; len = len - h - 1; }
267  else len = h;
268  }
269  return f == l ? *(l - 1) : *f;
270 }
271 
272 template<class TKey, class TDat, class THashFunc>
274  // resize & initialize port vector
275  //if (PortV.Len()==0){PortV.Gen(17);}
276  //else {PortV.Gen(2*PortV.Len()+1);}
277  if (PortV.Len()==0){
278  PortV.Gen(17);
279  } else if (AutoSizeP&&(KeyDatV.Len()>2*PortV.Len())){
280  PortV.Gen(GetNextPrime(PortV.Len()+1));
281  } else {
282  return;
283  }
284  PortV.PutAll(TInt(-1));
285  // rehash keys
286  for (int KeyId=0; KeyId<KeyDatV.Len(); KeyId++){
287  THKeyDat& KeyDat=KeyDatV[KeyId];
288  if (KeyDat.HashCd!=-1){
289  const int PortN = abs(THashFunc::GetPrimHashCd(KeyDat.Key) % PortV.Len());
290  KeyDat.Next=PortV[PortN];
291  PortV[PortN]=KeyId;
292  }
293  }
294 }
295 
296 template<class TKey, class TDat, class THashFunc>
297 THash<TKey, TDat, THashFunc>::THash(const int& ExpectVals, const bool& _AutoSizeP):
298  PortV(GetNextPrime(ExpectVals/2)), KeyDatV(ExpectVals, 0),
299  AutoSizeP(_AutoSizeP), FFreeKeyId(-1), FreeKeys(0){
300  PortV.PutAll(TInt(-1));
301 }
302 
303 template<class TKey, class TDat, class THashFunc>
305  if (Len() != Hash.Len()) { return false; }
306  for (int i = FFirstKeyId(); FNextKeyId(i); ) {
307  const TKey& Key = GetKey(i);
308  if (! Hash.IsKey(Key)) { return false; }
309  if (GetDat(Key) != Hash.GetDat(Key)) { return false; }
310  }
311  return true;
312 }
313 
314 template<class TKey, class TDat, class THashFunc>
315 void THash<TKey, TDat, THashFunc>::Clr(const bool& DoDel, const int& NoDelLim, const bool& ResetDat){
316  if (DoDel){
317  PortV.Clr(); KeyDatV.Clr();
318  } else {
319  PortV.PutAll(TInt(-1));
320  KeyDatV.Clr(DoDel, NoDelLim);
321  if (ResetDat){KeyDatV.PutAll(THKeyDat());}
322  }
323  FFreeKeyId=TInt(-1); FreeKeys=TInt(0);
324 }
325 
326 template<class TKey, class TDat, class THashFunc>
328  if ((KeyDatV.Len()>2*PortV.Len())||PortV.Empty()){Resize();}
329  const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len());
330  const int HashCd=abs(THashFunc::GetSecHashCd(Key));
331  int PrevKeyId=-1;
332  int KeyId=PortV[PortN];
333  while ((KeyId!=-1) &&
334  !((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){
335  PrevKeyId=KeyId; KeyId=KeyDatV[KeyId].Next;}
336 
337  if (KeyId==-1){
338  if (FFreeKeyId==-1){
339  KeyId=KeyDatV.Add(THKeyDat(-1, HashCd, Key));
340  } else {
341  KeyId=FFreeKeyId; FFreeKeyId=KeyDatV[FFreeKeyId].Next; FreeKeys--;
342  //KeyDatV[KeyId]=TKeyDat(-1, HashCd, Key); // slow version
343  KeyDatV[KeyId].Next=-1;
344  KeyDatV[KeyId].HashCd=HashCd;
345  KeyDatV[KeyId].Key=Key;
346  //KeyDatV[KeyId].Dat=TDat(); // already empty
347  }
348  if (PrevKeyId==-1){
349  PortV[PortN]=KeyId;
350  } else {
351  KeyDatV[PrevKeyId].Next=KeyId;
352  }
353  }
354  return KeyId;
355 }
356 
357 template<class TKey, class TDat, class THashFunc>
359  IAssert(!PortV.Empty());
360  const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len());
361  const int HashCd=abs(THashFunc::GetSecHashCd(Key));
362  int PrevKeyId=-1;
363  int KeyId=PortV[PortN];
364 
365  while ((KeyId!=-1) &&
366  !((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){
367  PrevKeyId=KeyId; KeyId=KeyDatV[KeyId].Next;}
368 
369  //IAssertR(KeyId!=-1, Key.GetStr()); //J: some classes do not provide GetStr()?
370  IAssert(KeyId!=-1); //J: some classes do not provide GetStr()?
371  if (PrevKeyId==-1){PortV[PortN]=KeyDatV[KeyId].Next;}
372  else {KeyDatV[PrevKeyId].Next=KeyDatV[KeyId].Next;}
373  KeyDatV[KeyId].Next=FFreeKeyId; FFreeKeyId=KeyId; FreeKeys++;
374  KeyDatV[KeyId].HashCd=TInt(-1);
375  KeyDatV[KeyId].Key=TKey();
376  KeyDatV[KeyId].Dat=TDat();
377 }
378 
379 template<class TKey, class TDat, class THashFunc>
381  // MarkDelKey is same as Delkey except last two lines
382  IAssert(!PortV.Empty());
383  const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len());
384  const int HashCd=abs(THashFunc::GetSecHashCd(Key));
385  int PrevKeyId=-1;
386  int KeyId=PortV[PortN];
387  while ((KeyId!=-1) &&
388  !((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){
389  PrevKeyId=KeyId; KeyId=KeyDatV[KeyId].Next;}
390  IAssertR(KeyId!=-1, Key.GetStr());
391  if (PrevKeyId==-1){PortV[PortN]=KeyDatV[KeyId].Next;}
392  else {KeyDatV[PrevKeyId].Next=KeyDatV[KeyId].Next;}
393  KeyDatV[KeyId].Next=FFreeKeyId; FFreeKeyId=KeyId; FreeKeys++;
394  KeyDatV[KeyId].HashCd=TInt(-1);
395 }
396 
397 template<class TKey, class TDat, class THashFunc>
399  IAssert(! Empty());
400  int KeyId = abs(Rnd.GetUniDevInt(KeyDatV.Len()));
401  while (KeyDatV[KeyId].HashCd == -1) { // if the index is empty, just try again
402  KeyId = abs(Rnd.GetUniDevInt(KeyDatV.Len())); }
403  return KeyId;
404 }
405 
406 // return random KeyId even if the hash table contains deleted keys
407 // defrags the table if necessary
408 template<class TKey, class TDat, class THashFunc>
409 int THash<TKey, TDat, THashFunc>::GetRndKeyId(TRnd& Rnd, const double& EmptyFrac) {
410  IAssert(! Empty());
411  if (FreeKeys/double(Len()+FreeKeys) > EmptyFrac) { Defrag(); }
412  int KeyId = Rnd.GetUniDevInt(KeyDatV.Len());
413  while (KeyDatV[KeyId].HashCd == -1) { // if the index is empty, just try again
414  KeyId = Rnd.GetUniDevInt(KeyDatV.Len());
415  }
416  return KeyId;
417 }
418 
419 template<class TKey, class TDat, class THashFunc>
420 int THash<TKey, TDat, THashFunc>::GetKeyId(const TKey& Key) const {
421  if (PortV.Empty()){return -1;}
422  const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len());
423  const int HashCd=abs(THashFunc::GetSecHashCd(Key));
424  int KeyId=PortV[PortN];
425  while ((KeyId!=-1) &&
426  !((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){
427  KeyId=KeyDatV[KeyId].Next;}
428  return KeyId;
429 }
430 
431 template<class TKey, class TDat, class THashFunc>
433  do {KeyId++;} while ((KeyId<KeyDatV.Len())&&(KeyDatV[KeyId].HashCd==-1));
434  return KeyId<KeyDatV.Len();
435 }
436 
437 template<class TKey, class TDat, class THashFunc>
439  KeyV.Gen(Len(), 0);
440  int KeyId=FFirstKeyId();
441  while (FNextKeyId(KeyId)){
442  KeyV.Add(GetKey(KeyId));}
443 }
444 
445 template<class TKey, class TDat, class THashFunc>
447  DatV.Gen(Len(), 0);
448  int KeyId=FFirstKeyId();
449  while (FNextKeyId(KeyId)){
450  DatV.Add(GetHashKeyDat(KeyId).Dat);}
451 }
452 
453 template<class TKey, class TDat, class THashFunc>
455  KeyDatPrV.Gen(Len(), 0);
456  TKey Key; TDat Dat;
457  int KeyId=FFirstKeyId();
458  while (FNextKeyId(KeyId)){
459  GetKeyDat(KeyId, Key, Dat);
460  KeyDatPrV.Add(TPair<TKey, TDat>(Key, Dat));
461  }
462 }
463 
464 template<class TKey, class TDat, class THashFunc>
466  DatKeyPrV.Gen(Len(), 0);
467  TKey Key; TDat Dat;
468  int KeyId=FFirstKeyId();
469  while (FNextKeyId(KeyId)){
470  GetKeyDat(KeyId, Key, Dat);
471  DatKeyPrV.Add(TPair<TDat, TKey>(Dat, Key));
472  }
473 }
474 
475 template<class TKey, class TDat, class THashFunc>
477  KeyDatKdV.Gen(Len(), 0);
478  TKey Key; TDat Dat;
479  int KeyId=FFirstKeyId();
480  while (FNextKeyId(KeyId)){
481  GetKeyDat(KeyId, Key, Dat);
482  KeyDatKdV.Add(TKeyDat<TKey, TDat>(Key, Dat));
483  }
484 }
485 
486 template<class TKey, class TDat, class THashFunc>
488  DatKeyKdV.Gen(Len(), 0);
489  TKey Key; TDat Dat;
490  int KeyId=FFirstKeyId();
491  while (FNextKeyId(KeyId)){
492  GetKeyDat(KeyId, Key, Dat);
493  DatKeyKdV.Add(TKeyDat<TDat, TKey>(Dat, Key));
494  }
495 }
496 
497 template<class TKey, class TDat, class THashFunc>
499  if (this!=&Hash){
500  PortV.Swap(Hash.PortV);
501  KeyDatV.Swap(Hash.KeyDatV);
502  ::Swap(AutoSizeP, Hash.AutoSizeP);
503  ::Swap(FFreeKeyId, Hash.FFreeKeyId);
504  ::Swap(FreeKeys, Hash.FreeKeys);
505  }
506 }
507 
508 template<class TKey, class TDat, class THashFunc>
510  if (!IsKeyIdEqKeyN()){
511  THash<TKey, TDat, THashFunc> Hash(PortV.Len());
512  int KeyId=FFirstKeyId(); TKey Key; TDat Dat;
513  while (FNextKeyId(KeyId)){
514  GetKeyDat(KeyId, Key, Dat);
515  Hash.AddDat(Key, Dat);
516  }
517  Pack();
518  operator=(Hash);
519  IAssert(IsKeyIdEqKeyN());
520  }
521 }
522 
523 template<class TKey, class TDat, class THashFunc>
524 void THash<TKey, TDat, THashFunc>::Sort(const bool& CmpKey, const bool& Asc) {
525  IAssertR(IsKeyIdEqKeyN(), "THash::Sort only works when table has no deleted keys.");
526  TIntV TargV(Len()), MapV(Len()), StateV(Len());
527  for (int i = 0; i < TargV.Len(); i++) {
528  TargV[i] = i; MapV[i] = i; StateV[i] = i;
529  }
530  // sort KeyIds
531  THashKeyDatCmp HashCmp(*this, CmpKey, Asc);
532  TargV.SortCmp(HashCmp);
533  // now sort the update vector
535  for (int i = 0; i < TargV.Len()-1; i++) {
536  const int SrcPos = MapV[TargV[i]];
537  const int Loc = i;
538  // swap data
539  Tmp = KeyDatV[SrcPos];
540  KeyDatV[SrcPos] = KeyDatV[Loc];
541  KeyDatV[Loc] = Tmp;
542  // swap keys
543  MapV[StateV[i]] = SrcPos;
544  StateV.Swap(Loc, SrcPos);
545  }
546  for (int i = 0; i < TargV.Len(); i++) {
547  MapV[TargV[i]] = i; }
548  for (int p = 0; p < PortV.Len(); p++) {
549  if (PortV[p] != -1) {
550  PortV[p] = MapV[PortV[p]]; } }
551  for (int i = 0; i < KeyDatV.Len(); i++) {
552  if (KeyDatV[i].Next != -1) {
553  KeyDatV[i].Next = MapV[KeyDatV[i].Next]; }
554  }
555 }
556 
558 // Common-Hash-Types
624 
626 // Hash-Pointer
627 template <class TKey, class TDat>
628 class PHash{
629 private:
631 public:
633 public:
636  return new PHash<TKey, TDat>();}
637  PHash<TKey, TDat>(const int& MxVals, const int& Vals): H(MxVals, Vals){}
638  static TPt<PHash<TKey, TDat> > New(const int& MxVals, const int& Vals){
639  return new PHash<TKey, TDat>(MxVals, Vals);}
642  return new PHash<TKey, TDat>(H);}
643  explicit PHash<TKey, TDat>(TSIn& SIn): H(SIn){}
644  static TPt<PHash<TKey, TDat> > Load(TSIn& SIn){return new PHash<TKey, TDat>(SIn);}
645  void Save(TSOut& SOut) const {H.Save(SOut);}
646 
648  if (this!=&Vec){H=Vec.H;} return *this;}
649  bool operator==(const PHash<TKey, TDat>& Vec) const {return H==Vec.H;}
650  bool operator<(const PHash<TKey, TDat>& Vec) const {return H<Vec.H;}
651 
652  friend class TPt<PHash<TKey, TDat> >;
653 };
654 
656 // Big-String-Pool (holds up to 2 giga strings, storage overhead is 8(4) bytes per string)
657 //J: have to put it here since it uses TVec (can't be in dt.h)
659 private:
660  TSize MxBfL, BfL;
661  uint GrowBy;
662  char *Bf;
663  TVec<TSize> IdOffV; // string ID to offset
664 private:
665  void Resize(TSize _MxBfL);
666 public:
667  TBigStrPool(TSize MxBfLen = 0, uint _GrowBy = 16*1024*1024);
668  TBigStrPool(TSIn& SIn, bool LoadCompact = true);
669  TBigStrPool(const TBigStrPool& Pool) : MxBfL(Pool.MxBfL), BfL(Pool.BfL), GrowBy(Pool.GrowBy) {
670  Bf = (char *) malloc(Pool.MxBfL); IAssert(Bf); memcpy(Bf, Pool.Bf, Pool.BfL); }
671  ~TBigStrPool() { if (Bf) free(Bf); else IAssert(MxBfL == 0); MxBfL = 0; BfL = 0; }
672 
673  static PBigStrPool New(TSize _MxBfLen = 0, uint _GrowBy = 16*1024*1024) { return PBigStrPool(new TBigStrPool(_MxBfLen, _GrowBy)); }
674  static PBigStrPool New(TSIn& SIn) { return new TBigStrPool(SIn); }
675  static PBigStrPool New(const TStr& fileName) { PSIn SIn = TFIn::New(fileName); return new TBigStrPool(*SIn); }
676  static PBigStrPool Load(TSIn& SIn, bool LoadCompacted = true) { return PBigStrPool(new TBigStrPool(SIn, LoadCompacted)); }
677  void Save(TSOut& SOut) const;
678  void Save(const TStr& fileName) { TFOut FOut(fileName); Save(FOut); }
679 
680  int GetStrs() const { return IdOffV.Len(); }
681  TSize Len() const { return BfL; }
682  TSize Size() const { return MxBfL; }
683  bool Empty() const { return ! Len(); }
684  char* operator () () const { return Bf; }
685  TBigStrPool& operator = (const TBigStrPool& Pool);
686 
687  int AddStr(const char *Str, uint Len);
688  int AddStr(const char *Str) { return AddStr(Str, uint(strlen(Str)) + 1); }
689  int AddStr(const TStr& Str) { return AddStr(Str.CStr(), Str.Len() + 1); }
690 
691  TStr GetStr(const int& StrId) const { Assert(StrId < GetStrs());
692  if (StrId == 0) return TStr::GetNullStr(); else return TStr(Bf + (TSize)IdOffV[StrId]); }
693  const char *GetCStr(const int& StrId) const { Assert(StrId < GetStrs());
694  if (StrId == 0) return TStr::GetNullStr().CStr(); else return (Bf + (TSize)IdOffV[StrId]); }
695 
696  TStr GetStrFromOffset(const TSize& Offset) const { Assert(Offset < BfL);
697  if (Offset == 0) return TStr::GetNullStr(); else return TStr(Bf + Offset); }
698  const char *GetCStrFromOffset(const TSize& Offset) const { Assert(Offset < BfL);
699  if (Offset == 0) return TStr::GetNullStr().CStr(); else return Bf + Offset; }
700 
701  void Clr(bool DoDel = false) { BfL = 0; if (DoDel && Bf) { free(Bf); Bf = 0; MxBfL = 0; } }
702  int Cmp(const int& StrId, const char *Str) const { Assert(StrId < GetStrs());
703  if (StrId != 0) return strcmp(Bf + (TSize)IdOffV[StrId], Str); else return strcmp("", Str); }
704 
705  static int GetPrimHashCd(const char *CStr);
706  static int GetSecHashCd(const char *CStr);
707  int GetPrimHashCd(const int& StrId) { Assert(StrId < GetStrs());
708  if (StrId != 0) return GetPrimHashCd(Bf + (TSize)IdOffV[StrId]); else return GetPrimHashCd(""); }
709  int GetSecHashCd(const int& StrId) { Assert(StrId < GetStrs());
710  if (StrId != 0) return GetSecHashCd(Bf + (TSize)IdOffV[StrId]); else return GetSecHashCd(""); }
711 };
712 
714 // String-Hash-Table
715 template <class TDat, class TStringPool = TStrPool, class THashFunc = TDefaultHashFunc<TStr> >
716 class TStrHash{
717 private:
718  //typedef typename PStringPool::TObj TStringPool;
728 private:
729  uint GetNextPrime(const uint& Val) const;
730  void Resize();
731  const THKeyDat& GetHashKeyDat(const int& KeyId) const {
732  const THKeyDat& KeyDat = KeyDatV[KeyId]; Assert(KeyDat.HashCd != -1); return KeyDat; }
733  THKeyDat& GetHashKeyDat(const int& KeyId) {
734  THKeyDat& KeyDat = KeyDatV[KeyId]; Assert(KeyDat.HashCd != -1); return KeyDat; }
735 public:
736  TStrHash(): PortV(), KeyDatV(), AutoSizeP(true), FFreeKeyId(-1), FreeKeys(0), Pool() { }
737  TStrHash(const PStringPool& StrPool): PortV(), KeyDatV(), AutoSizeP(true), FFreeKeyId(-1), FreeKeys(0), Pool(StrPool) { }
738  TStrHash(const int& Ports, const bool& _AutoSizeP = false, const PStringPool& StrPool = PStringPool()) :
739  PortV(Ports), KeyDatV(Ports, 0), AutoSizeP(_AutoSizeP), FFreeKeyId(-1), FreeKeys(0), Pool(StrPool) { PortV.PutAll(-1); }
740  TStrHash(const TStrHash& Hash): PortV(Hash.PortV), KeyDatV(Hash.KeyDatV), AutoSizeP(Hash.AutoSizeP),
741  FFreeKeyId(Hash.FFreeKeyId), FreeKeys(Hash.FreeKeys), Pool() {
742  if (! Hash.Pool.Empty()) { Pool=PStringPool(new TStringPool(*Hash.Pool)); } }
743  TStrHash(TSIn& SIn, bool PoolToo = true): PortV(SIn), KeyDatV(SIn), AutoSizeP(SIn), FFreeKeyId(SIn), FreeKeys(SIn){ SIn.LoadCs(); if (PoolToo) Pool = PStringPool(SIn); }
744 
745  void Load(TSIn& SIn, bool PoolToo = true) { PortV.Load(SIn); KeyDatV.Load(SIn); AutoSizeP.Load(SIn); FFreeKeyId.Load(SIn);
746  FreeKeys.Load(SIn); SIn.LoadCs(); if (PoolToo) Pool = PStringPool(SIn); }
747  void Save(TSOut& SOut, bool PoolToo = true) const { PortV.Save(SOut); KeyDatV.Save(SOut);
748  AutoSizeP.Save(SOut); FFreeKeyId.Save(SOut); FreeKeys.Save(SOut); SOut.SaveCs(); if (PoolToo) Pool.Save(SOut); }
749 
750  void SetPool(const PStringPool& StrPool) { IAssert(Pool.Empty() || Pool->Empty()); Pool = StrPool; }
751  PStringPool GetPool() const { return Pool; }
752 
753  TStrHash& operator = (const TStrHash& Hash);
754 
755  bool Empty() const {return ! Len(); }
756  int Len() const { return KeyDatV.Len() - FreeKeys; }
757  int Reserved() const { return KeyDatV.Reserved(); }
758  int GetPorts() const { return PortV.Len(); }
759  bool IsAutoSize() const { return AutoSizeP; }
760  int GetMxKeyIds() const { return KeyDatV.Len(); }
761  bool IsKeyIdEqKeyN() const {return ! FreeKeys; }
762 
763  int AddKey(const char *Key);
764  int AddKey(const TStr& Key) { return AddKey(Key.CStr()); }
765  int AddKey(const TChA& Key) { return AddKey(Key.CStr()); }
766  int AddDat(const char *Key, const TDat& Dat) { const int KeyId = AddKey(Key); KeyDatV[KeyId].Dat = Dat; return KeyId; }
767  int AddDat(const TStr& Key, const TDat& Dat) { const int KeyId = AddKey(Key.CStr()); KeyDatV[KeyId].Dat = Dat; return KeyId; }
768  int AddDat(const TChA& Key, const TDat& Dat) { const int KeyId = AddKey(Key.CStr()); KeyDatV[KeyId].Dat = Dat; return KeyId; }
769  TDat& AddDat(const char *Key) { return KeyDatV[AddKey(Key)].Dat; }
770  TDat& AddDat(const TStr& Key) { return KeyDatV[AddKey(Key.CStr())].Dat; }
771  TDat& AddDat(const TChA& Key) { return KeyDatV[AddKey(Key.CStr())].Dat; }
772  TDat& AddDatId(const char *Key) { const int KeyId = AddKey(Key); return KeyDatV[KeyId].Dat = KeyId; }
773  TDat& AddDatId(const TStr& Key) { const int KeyId = AddKey(Key.CStr()); return KeyDatV[KeyId].Dat = KeyId; }
774  TDat& AddDatId(const TChA& Key) { const int KeyId = AddKey(Key.CStr()); return KeyDatV[KeyId].Dat = KeyId; }
775 
776  const TDat& operator[](const int& KeyId) const {return GetHashKeyDat(KeyId).Dat;}
777  TDat& operator[](const int& KeyId){return GetHashKeyDat(KeyId).Dat;}
778  const TDat& operator () (const char *Key) const { return GetDat(Key);}
779  //TDat& operator ()(const char *Key){return AddDat(Key);} // add if not found
780 
781  const TDat& GetDat(const char *Key) const { return KeyDatV[GetKeyId(Key)].Dat; }
782  const TDat& GetDat(const TStr& Key) const { return GetDat(Key.CStr()); }
783  TDat& GetDat(const char *Key) { return KeyDatV[GetKeyId(Key)].Dat; }
784  const TDat& GetDat(const TStr& Key) { return GetDat(Key.CStr()); }
785  const TDat& GetDat(const TChA& Key) { return GetDat(Key.CStr()); }
786  TDat& GetDatId(const int& KeyId) { return KeyDatV[KeyId].Dat; }
787  const TDat& GetDatId(const int& KeyId) const { return KeyDatV[KeyId].Dat; }
788  void GetKeyDat(const int& KeyId, int& KeyO, TDat& Dat) const { const THKeyDat& KeyDat = GetHashKeyDat(KeyId); KeyO = KeyDat.Key; Dat = KeyDat.Dat; }
789  void GetKeyDat(const int& KeyId, const char*& Key, TDat& Dat) const { const THKeyDat& KeyDat = GetHashKeyDat(KeyId); Key = KeyFromOfs(KeyDat.Key); Dat = KeyDat.Dat; }
790  void GetKeyDat(const int& KeyId, TStr& Key, TDat& Dat) const { const THKeyDat& KeyDat = GetHashKeyDat(KeyId); Key = KeyFromOfs(KeyDat.Key); Dat = KeyDat.Dat;}
791  void GetKeyDat(const int& KeyId, TChA& Key, TDat& Dat) const { const THKeyDat& KeyDat = GetHashKeyDat(KeyId); Key = KeyFromOfs(KeyDat.Key); Dat = KeyDat.Dat;}
792 
793  int GetKeyId(const char *Key) const;
794  int GetKeyId(const TStr& Key) const { return GetKeyId(Key.CStr()); }
795  const char *GetKey(const int& KeyId) const { return Pool->GetCStr(GetHashKeyDat(KeyId).Key); }
796  int GetKeyOfs(const int& KeyId) const { return GetHashKeyDat(KeyId).Key; } // pool string id
797  const char *KeyFromOfs(const int& KeyO) const { return Pool->GetCStr(KeyO); }
798 
799  bool IsKey(const char *Key) const { return GetKeyId(Key) != -1; }
800  bool IsKey(const TStr& Key) const { return GetKeyId(Key.CStr()) != -1; }
801  bool IsKey(const TChA& Key) const { return GetKeyId(Key.CStr()) != -1; }
802  bool IsKey(const char *Key, int& KeyId) const { KeyId = GetKeyId(Key); return KeyId != -1; }
803  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; }
804  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; }
805  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; }
806  bool IsKeyId(const int& KeyId) const { return 0 <= KeyId && KeyId < KeyDatV.Len() && KeyDatV[KeyId].HashCd != -1; }
807 
808  int FFirstKeyId() const {return 0-1;}
809  bool FNextKeyId(int& KeyId) const;
810 
811  void GetKeyV(TVec<TStr>& KeyV) const;
812  void GetStrIdV(TIntV& StrIdV) const;
813  void GetDatV(TVec<TDat>& DatV) const;
814  void GetKeyDatPrV(TVec<TPair<TStr, TDat> >& KeyDatPrV) const;
815  void GetDatKeyPrV(TVec<TPair<TDat, TStr> >& DatKeyPrV) const;
816 
817  void Pack(){KeyDatV.Pack();}
818 };
819 
820 template <class TDat, class TStringPool, class THashFunc>
822  uint *f = (uint *) TIntH::HashPrimeT, *m, *l = (uint *) TIntH::HashPrimeT + (int) TIntH::HashPrimes;
823  int h, len = (int)TIntH::HashPrimes;
824  while (len > 0) {
825  h = len >> 1; m = f + h;
826  if (*m < Val) { f = m; f++; len = len - h - 1; }
827  else len = h;
828  }
829  return f == l ? *(l - 1) : *f;
830 }
831 
832 template <class TDat, class TStringPool, class THashFunc>
834  // resize & initialize port vector
835  if (PortV.Empty()) { PortV.Gen(17); PortV.PutAll(-1); }
836  else
837  if (AutoSizeP && KeyDatV.Len() > 3 * PortV.Len()) {
838  const int NxPrime = GetNextPrime(KeyDatV.Len());
839  //printf("%s resize PortV: %d -> %d, Len: %d\n", GetTypeNm(*this).CStr(), PortV.Len(), NxPrime, Len());
840  PortV.Gen(NxPrime); PortV.PutAll(-1); }
841  else
842  return;
843  // rehash keys
844  const int NPorts = PortV.Len();
845  for (int i = 0; i < KeyDatV.Len(); i++) {
846  THKeyDat& KeyDat = KeyDatV[i];
847  if (KeyDat.HashCd != -1) {
848  const int Port = abs(THashFunc::GetPrimHashCd(Pool->GetCStr(KeyDat.Key)) % NPorts);
849  KeyDat.Next = PortV[Port];
850  PortV[Port] = i;
851  }
852  }
853 }
854 
855 template <class TDat, class TStringPool, class THashFunc>
857  if (this != &Hash) {
858  PortV = Hash.PortV;
859  KeyDatV = Hash.KeyDatV;
860  AutoSizeP = Hash.AutoSizeP;
861  FFreeKeyId = Hash.FFreeKeyId;
862  FreeKeys = Hash.FreeKeys;
863  if (! Hash.Pool.Empty()) Pool = PStringPool(new TStringPool(*Hash.Pool));
864  else Pool = NULL;
865  }
866  return *this;
867 }
868 
869 template <class TDat, class TStringPool, class THashFunc>
871  if (Pool.Empty()) Pool = TStringPool::New();
872  if ((AutoSizeP && KeyDatV.Len() > PortV.Len()) || PortV.Empty()) Resize();
873  const int PortN = abs(THashFunc::GetPrimHashCd(Key) % PortV.Len());
874  const int HashCd = abs(THashFunc::GetSecHashCd(Key));
875  int PrevKeyId = -1;
876  int KeyId = PortV[PortN];
877  while (KeyId != -1 && ! (KeyDatV[KeyId].HashCd == HashCd && Pool->Cmp(KeyDatV[KeyId].Key, Key) == 0)) {
878  PrevKeyId = KeyId; KeyId = KeyDatV[KeyId].Next; }
879  if (KeyId == -1) {
880  const int StrId = Pool->AddStr(Key);
881  if (FFreeKeyId == -1) {
882  KeyId = KeyDatV.Add(THKeyDat(-1, HashCd, StrId));
883  } else {
884  KeyId = FFreeKeyId;
885  FFreeKeyId = KeyDatV[FFreeKeyId].Next;
886  FreeKeys--;
887  KeyDatV[KeyId] = THKeyDat(-1, HashCd, StrId);
888  }
889  if (PrevKeyId == -1) PortV[PortN] = KeyId;
890  else KeyDatV[PrevKeyId].Next = KeyId;
891  }
892  return KeyId;
893 }
894 
895 template <class TDat, class TStringPool, class THashFunc>
897  if (PortV.Empty()) return -1;
898  const int PortN = abs(THashFunc::GetPrimHashCd(Key) % PortV.Len());
899  const int Hc = abs(THashFunc::GetSecHashCd(Key));
900  int KeyId = PortV[PortN];
901  while (KeyId != -1 && ! (KeyDatV[KeyId].HashCd == Hc && Pool->Cmp(KeyDatV[KeyId].Key, Key) == 0))
902  KeyId = KeyDatV[KeyId].Next;
903  return KeyId;
904 }
905 
906 template <class TDat, class TStringPool, class THashFunc>
908  do KeyId++; while (KeyId < KeyDatV.Len() && KeyDatV[KeyId].HashCd == -1);
909  return KeyId < KeyDatV.Len();
910 }
911 
912 template <class TDat, class TStringPool, class THashFunc>
914  KeyV.Gen(Len(), 0);
915  int KeyId = FFirstKeyId();
916  while (FNextKeyId(KeyId))
917  KeyV.Add(GetKey(KeyId));
918 }
919 
920 template <class TDat, class TStringPool, class THashFunc>
922  StrIdV.Gen(Len(), 0);
923  int KeyId = FFirstKeyId();
924  while (FNextKeyId(KeyId))
925  StrIdV.Add(GetKeyOfs(KeyId));
926 }
927 
928 template <class TDat, class TStringPool, class THashFunc>
930  DatV.Gen(Len(), 0);
931  int KeyId = FFirstKeyId();
932  while (FNextKeyId(KeyId))
933  DatV.Add(GetHashKeyDat(KeyId).Dat);
934 }
935 
936 template <class TDat, class TStringPool, class THashFunc>
938  KeyDatPrV.Gen(Len(), 0);
939  TStr Str; TDat Dat;
940  int KeyId = FFirstKeyId();
941  while (FNextKeyId(KeyId)){
942  GetKeyDat(KeyId, Str, Dat);
943  KeyDatPrV.Add(TPair<TStr, TDat>(Str, Dat));
944  }
945 }
946 
947 template <class TDat, class TStringPool, class THashFunc>
949  DatKeyPrV.Gen(Len(), 0);
950  TStr Str; TDat Dat;
951  int KeyId = FFirstKeyId();
952  while (FNextKeyId(KeyId)){
953  GetKeyDat(KeyId, Str, Dat);
954  DatKeyPrV.Add(TPair<TDat, TStr>(Dat, Str));
955  }
956 }
957 
959 // Common-String-Hash-Types
963 
965 // Cache
966 template <class TKey, class TDat, class THashFunc = TDefaultHashFunc<TKey> >
967 class TCache{
968 private:
969  typedef TLst<TKey> TKeyL; typedef TLstNd<TKey>* TKeyLN;
975  void* RefToBs;
976  void Purge(const int64& MemToPurge);
977 public:
978  TCache(){}
979  TCache(const TCache&);
980  TCache(const int64& _MxMemUsed, const int& Ports, void* _RefToBs):
981  MxMemUsed(_MxMemUsed), CurMemUsed(0),
982  KeyDatH(Ports), TimeKeyL(), RefToBs(_RefToBs){}
983 
984  TCache& operator=(const TCache&);
985  int64 GetMemUsed() const;
986  int64 GetMxMemUsed() const { return MxMemUsed; }
987  bool RefreshMemUsed();
988 
989  void Put(const TKey& Key, const TDat& Dat);
990  bool Get(const TKey& Key, TDat& Dat);
991  void Del(const TKey& Key, const bool& DoEventCall=true);
992  void Flush();
993  void FlushAndClr();
994  void* FFirstKeyDat();
995  bool FNextKeyDat(void*& KeyDatP, TKey& Key, TDat& Dat);
996 
997  void PutRefToBs(void* _RefToBs){RefToBs=_RefToBs;}
998  void* GetRefToBs(){return RefToBs;}
999 };
1000 
1001 template <class TKey, class TDat, class THashFunc>
1003  const int64 StartMemUsed = CurMemUsed;
1004  while (!TimeKeyL.Empty()&&(StartMemUsed-CurMemUsed<MemToPurge)){
1005  TKey Key=TimeKeyL.Last()->GetVal();
1006  Del(Key);
1007  }
1008 }
1009 
1010 template <class TKey, class TDat, class THashFunc>
1012  int64 MemUsed=0;
1013  int KeyId=KeyDatH.FFirstKeyId();
1014  while (KeyDatH.FNextKeyId(KeyId)){
1015  const TKey& Key=KeyDatH.GetKey(KeyId);
1016  const TKeyLNDatPr& KeyLNDatPr=KeyDatH[KeyId];
1017  TDat Dat=KeyLNDatPr.Val2;
1018  MemUsed+=int64(Key.GetMemUsed()+Dat->GetMemUsed());
1019  }
1020  return MemUsed;
1021 }
1022 
1023 template <class TKey, class TDat, class THashFunc>
1025  CurMemUsed=GetMemUsed();
1026  if (CurMemUsed>MxMemUsed){
1027  Purge(CurMemUsed-MxMemUsed);
1028  return true;
1029  }
1030  return false;
1031 }
1032 
1033 template <class TKey, class TDat, class THashFunc>
1034 void TCache<TKey, TDat, THashFunc>::Put(const TKey& Key, const TDat& Dat){
1035  int KeyId=KeyDatH.GetKeyId(Key);
1036  if (KeyId==-1){
1037  int64 KeyDatMem=int64(Key.GetMemUsed()+Dat->GetMemUsed());
1038  if (CurMemUsed+KeyDatMem>MxMemUsed){Purge(KeyDatMem);}
1039  CurMemUsed+=KeyDatMem;
1040  TKeyLN KeyLN=TimeKeyL.AddFront(Key);
1041  TKeyLNDatPr KeyLNDatPr(KeyLN, Dat);
1042  KeyDatH.AddDat(Key, KeyLNDatPr);
1043  } else {
1044  TKeyLNDatPr& KeyLNDatPr=KeyDatH[KeyId];
1045  TKeyLN KeyLN=KeyLNDatPr.Val1;
1046  KeyLNDatPr.Val2=Dat;
1047  TimeKeyL.PutFront(KeyLN);
1048  }
1049 }
1050 
1051 template <class TKey, class TDat, class THashFunc>
1052 bool TCache<TKey, TDat, THashFunc>::Get(const TKey& Key, TDat& Dat){
1053  int KeyId=KeyDatH.GetKeyId(Key);
1054  if (KeyId==-1){
1055  return false;
1056  } else {
1057  Dat=KeyDatH[KeyId].Val2;
1058  return true;
1059  }
1060 }
1061 
1062 template <class TKey, class TDat, class THashFunc>
1063 void TCache<TKey, TDat, THashFunc>::Del(const TKey& Key, const bool& DoEventCall){
1064  int KeyId=KeyDatH.GetKeyId(Key);
1065  if (KeyId!=-1){
1066  TKeyLNDatPr& KeyLNDatPr=KeyDatH[KeyId];
1067  TKeyLN KeyLN=KeyLNDatPr.Val1;
1068  TDat& Dat=KeyLNDatPr.Val2;
1069  if (DoEventCall){
1070  Dat->OnDelFromCache(Key, RefToBs);}
1071  CurMemUsed-=int64(Key.GetMemUsed()+Dat->GetMemUsed());
1072  Dat=NULL;
1073  TimeKeyL.Del(KeyLN);
1074  KeyDatH.DelKeyId(KeyId);
1075  }
1076 }
1077 
1078 template <class TKey, class TDat, class THashFunc>
1080  printf("To flush: %d\n", KeyDatH.Len());
1081  int KeyId=KeyDatH.FFirstKeyId(); int Done = 0;
1082  while (KeyDatH.FNextKeyId(KeyId)){
1083  if (Done%10000==0){printf("%d\r", Done);}
1084  const TKey& Key=KeyDatH.GetKey(KeyId);
1085  TKeyLNDatPr& KeyLNDatPr=KeyDatH[KeyId];
1086  TDat Dat=KeyLNDatPr.Val2;
1087  Dat->OnDelFromCache(Key, RefToBs);
1088  Done++;
1089  }
1090  printf("Done %d\n", KeyDatH.Len());
1091 }
1092 
1093 template <class TKey, class TDat, class THashFunc>
1095  Flush();
1096  CurMemUsed=0;
1097  KeyDatH.Clr();
1098  TimeKeyL.Clr();
1099 }
1100 
1101 template <class TKey, class TDat, class THashFunc>
1103  return TimeKeyL.First();
1104 }
1105 
1106 template <class TKey, class TDat, class THashFunc>
1107 bool TCache<TKey, TDat, THashFunc>::FNextKeyDat(void*& KeyDatP, TKey& Key, TDat& Dat){
1108  if (KeyDatP==NULL){
1109  return false;
1110  } else {
1111  Key=TKeyLN(KeyDatP)->GetVal(); Dat=KeyDatH.GetDat(Key).Val2;
1112  KeyDatP=TKeyLN(KeyDatP)->Next(); return true;
1113  }
1114 }
1115 
1117 // Old-Hash-Functions
1118 
1119 // Old-String-Hash-Function
1121 public:
1122  inline static int GetPrimHashCd(const char *p) {
1123  const int MulBy = 16; // even older version used MulBy=2
1124  int HashCd = 0;
1125  while (*p) { HashCd = (MulBy * HashCd) + *p++; HashCd &= 0x0FFFFFFF; }
1126  return HashCd; }
1127  inline static int GetSecHashCd(const char *p) {
1128  const int MulBy = 16; // even older version used MulBy=2
1129  int HashCd = 0;
1130  while (*p) { HashCd = (MulBy * HashCd) ^ *p++; HashCd &= 0x0FFFFFFF; }
1131  return HashCd; }
1132  inline static int GetPrimHashCd(const TStr& s) { return GetPrimHashCd(s.CStr()); }
1133  inline static int GetSecHashCd(const TStr& s) { return GetSecHashCd(s.CStr()); }
1134 };
1135 
1136 // Md5-Hash-Function
1138 public:
1139  static int GetPrimHashCd(const char *p);
1140  static int GetSecHashCd(const char *p);
1141  static int GetPrimHashCd(const TStr& s);
1142  static int GetSecHashCd(const TStr& s);
1143 };
1144 
1145 // DJB-Hash-Function
1147 private:
1148  inline static unsigned int DJBHash(const char* Str, const ::TSize& Len) {
1149  unsigned int hash = 5381;
1150  for(unsigned int i = 0; i < Len; Str++, i++) {
1151  hash = ((hash << 5) + hash) + (*Str); }
1152  return hash;
1153  }
1154 public:
1155  inline static int GetPrimHashCd(const char *p) {
1156  const char *r = p; while (*r) { r++; }
1157  return (int) DJBHash((const char *) p, r - p) & 0x7fffffff; }
1158  inline static int GetSecHashCd(const char *p) {
1159  const char *r = p; while (*r) { r++; }
1160  return (int) DJBHash((const char *) p, r - p) & 0x7fffffff; }
1161  inline static int GetPrimHashCd(const TStr& s) { return GetPrimHashCd(s.CStr()); }
1162  inline static int GetSecHashCd(const TStr& s) { return GetSecHashCd(s.CStr()); }
1163 };
1164 
1165 // Old-Vector-Hash-Function
1166 template <class TVec>
1168 public:
1169  static inline int GetPrimHashCd(const TVec& Vec) {
1170  int HashCd=0;
1171  for (int ValN=0; ValN<Vec.Len(); ValN++){
1172  HashCd+=Vec[ValN].GetPrimHashCd();}
1173  return abs(HashCd);
1174  }
1175  inline static int GetSecHashCd(const TVec& Vec) {
1176  int HashCd=0;
1177  for (int ValN=0; ValN<Vec.Len(); ValN++){
1178  HashCd+=Vec[ValN].GetSecHashCd();}
1179  return abs(HashCd);
1180  }
1181 };
Definition: ds.h:336
Definition: bd.h:440
bool IsKey(const TChA &Key) const
Definition: hash.h:801
#define IAssert(Cond)
Definition: bd.h:262
THashKeyDatI()
Definition: hash.h:47
void GetKeyDat(const int &KeyId, TChA &Key, TDat &Dat) const
Definition: hash.h:791
bool operator==(const THashKeyDat &HashKeyDat) const
Definition: hash.h:25
const TDat & GetDat(const TStr &Key) const
Definition: hash.h:782
void GetKeyDat(const int &KeyId, TStr &Key, TDat &Dat) const
Definition: hash.h:790
TIter EndI() const
Returns an iterator referring to the past-the-end element in the vector.
Definition: ds.h:552
THash< TInt, TFltPr > TIntFltPrH
Definition: hash.h:570
THash< TStr, TStr > TStrStrH
Definition: hash.h:602
int AddDat(const TStr &Key, const TDat &Dat)
Definition: hash.h:767
int Reserved() const
Definition: hash.h:757
PStringPool GetPool() const
Definition: hash.h:751
TDat Dat
Definition: hash.h:12
bool IsKeyIdEqKeyN() const
Definition: hash.h:191
TKeyL TimeKeyL
Definition: hash.h:974
TSizeTy Reserved() const
Returns the size of allocated storage capacity.
Definition: ds.h:537
int GetKeyOfs(const int &KeyId) const
Definition: hash.h:796
TVec< THKeyDat > THKeyDatV
Definition: hash.h:722
#define IAssertR(Cond, Reason)
Definition: bd.h:265
THash< TStr, TFltV > TStrFltVH
Definition: hash.h:601
int GetPrimHashCd() const
Returns primary hash code of the vector. Used by THash.
Definition: ds.h:930
THashKeyDatI(const THashKeyDatI &_HashKeyDatI)
Definition: hash.h:48
THash< TStr, TInt > TStrH
Definition: hash.h:592
THash< TIntTr, TFlt > TIntTrFltH
Definition: hash.h:587
int Len() const
Definition: dt.h:487
THashKeyDat & operator=(const THashKeyDat &HashKeyDat)
Definition: hash.h:29
void GetDatV(TVec< TDat > &DatV) const
Definition: hash.h:446
TPt & operator=(const TPt &Pt)
Definition: bd.h:487
THash< TCh, TCh > TChChH
Definition: hash.h:559
THash< TIntPr, TIntPrV > TIntPrIntPrVH
Definition: hash.h:580
THash< TIntTr, TInt > TIntTrIntH
Definition: hash.h:581
PStringPool Pool
Definition: hash.h:727
const char * GetCStr(const int &StrId) const
Definition: hash.h:693
TDat & operator[](const int &KeyId)
Definition: hash.h:777
TDat & GetDat(const char *Key)
Definition: hash.h:783
bool IsKeyGetDat(const TChA &Key, TDat &Dat) const
Definition: hash.h:805
void Sort(const bool &CmpKey, const bool &Asc)
Definition: hash.h:524
bool IsAutoSize() const
Definition: hash.h:759
Definition: dt.h:11
THash & operator=(const THash &Hash)
Definition: hash.h:148
TInt FreeKeys
Definition: hash.h:726
bool operator==(const THashKeyDatI &HashKeyDatI) const
Definition: hash.h:55
int GetMxKeyIds() const
Definition: hash.h:760
THash< TStr, TUInt64V > TStrUInt64VH
Definition: hash.h:598
THash< TUInt64, TInt > TUInt64H
Definition: hash.h:562
THash< TStrIntPr, TInt > TStrIntPrIntH
Definition: hash.h:618
static PBigStrPool New(TSize _MxBfLen=0, uint _GrowBy=16 *1024 *1024)
Definition: hash.h:673
void Save(TSOut &SOut) const
Definition: dt.h:1058
void * RefToBs
Definition: hash.h:975
int FFirstKeyId() const
Definition: hash.h:808
int64 MxMemUsed
Definition: hash.h:971
bool operator==(const THash &Hash) const
Definition: hash.h:304
THash< TStr, TIntV > TStrIntVH
Definition: hash.h:596
unsigned int uint
Definition: bd.h:11
const THKeyDat & GetHashKeyDat(const int &KeyId) const
Definition: hash.h:120
bool IsKeyId(const int &KeyId) const
Definition: hash.h:218
TDat & operator()(const TKey &Key)
Definition: hash.h:158
#define Fail
Definition: bd.h:238
TCache(const int64 &_MxMemUsed, const int &Ports, void *_RefToBs)
Definition: hash.h:980
int64 CurMemUsed
Definition: hash.h:972
bool Empty() const
Definition: bd.h:501
THash< TInt, TIntH > TIntIntHH
Definition: hash.h:568
void GetKeyDat(const int &KeyId, TKey &Key, TDat &Dat) const
Definition: hash.h:225
bool Empty() const
Definition: hash.h:683
void Flush()
Definition: hash.h:1079
TIter BegI() const
Definition: hash.h:171
static int GetSecHashCd(const TStr &s)
Definition: hash.h:1133
void Load(TSIn &SIn)
Definition: dt.h:901
Definition: fl.h:319
void GetKeyDat(const int &KeyId, const char *&Key, TDat &Dat) const
Definition: hash.h:789
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
int Len() const
Definition: hash.h:756
int GetSecHashCd() const
Returns secondary hash code of the vector. Used by THash.
Definition: ds.h:942
THash< TStr, TStrPr > TStrStrPrH
Definition: hash.h:603
void Save(TSOut &SOut) const
Definition: hash.h:141
TDat & AddDatId(const TStr &Key)
Definition: hash.h:773
static int GetSecHashCd(const TKey &Key)
Definition: hash.h:82
bool Empty() const
Definition: hash.h:185
static const unsigned int HashPrimeT[HashPrimes]
Definition: hash.h:91
static PBigStrPool New(const TStr &fileName)
Definition: hash.h:675
TInt Next
Definition: hash.h:9
TSize Len() const
Definition: hash.h:681
TStr GetStr(const int &StrId) const
Definition: hash.h:691
int AddStr(const TStr &Str)
Definition: hash.h:689
TInt FFreeKeyId
Definition: hash.h:726
void Purge(const int64 &MemToPurge)
Definition: hash.h:1002
TCache()
Definition: hash.h:978
TPair< TKeyLN, TDat > TKeyLNDatPr
Definition: hash.h:970
TDat & operator[](const int &KeyId)
Definition: hash.h:157
int64 GetMemUsed() const
Definition: hash.h:1011
THashKeyDat(const int &_Next, const int &_HashCd, const TKey &_Key)
Definition: hash.h:16
void SetPool(const PStringPool &StrPool)
Definition: hash.h:750
THash< TIntPr, TStrV > TIntPrStrVH
Definition: hash.h:589
bool IsKeyGetDat(const TStr &Key, TDat &Dat) const
Definition: hash.h:804
int AddDat(const char *Key, const TDat &Dat)
Definition: hash.h:766
THash< TStrTr, TInt > TStrTrIntH
Definition: hash.h:617
TDat & AddDatId(const char *Key)
Definition: hash.h:772
void Save(TSOut &SOut) const
Definition: dt.h:902
THash< TIntPr, TFlt > TIntPrFltH
Definition: hash.h:586
TStrHash()
Definition: hash.h:736
int AddKey(const TStr &Key)
Definition: hash.h:764
THash< TInt, TUInt64 > TIntUInt64H
Definition: hash.h:565
THashKeyDat()
Definition: hash.h:14
static int GetPrimHashCd(const char *p)
Definition: hash.cpp:86
TPt< TBigStrPool > PBigStrPool
Definition: hash.h:658
void LoadXml(const PXmlTok &XmlTok, const TStr &Nm="")
Definition: xmlser.h:127
THash< TIntPr, TIntV > TIntPrIntVH
Definition: hash.h:579
void Put(const TKey &Key, const TDat &Dat)
Definition: hash.h:1034
TIntV PortV
Definition: hash.h:723
TCRef CRef
Definition: hash.h:630
THash< TInt, TStr > TIntStrH
Definition: hash.h:573
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
bool IsEmpty() const
Tests whether the iterator has been initialized.
Definition: hash.h:67
void DelKeyId(const int &KeyId)
Definition: hash.h:203
TIter EndI() const
Definition: hash.h:176
THashKeyDat(TSIn &SIn)
Definition: hash.h:18
void Load(TSIn &SIn)
Definition: ds.h:877
const TDat & operator[](const int &KeyId) const
Definition: hash.h:776
TDat & GetDat(const TKey &Key)
Definition: hash.h:221
THash< TIntPr, TInt > TIntPrIntH
Definition: hash.h:578
void DelKeyIdV(const TIntV &KeyIdV)
Definition: hash.h:204
TSizeTy GetMemUsed() const
Returns the memory footprint (the number of bytes) of the vector.
Definition: ds.h:474
static int GetSecHashCd(const char *p)
Definition: hash.h:1158
bool FNextKeyId(int &KeyId) const
Definition: hash.h:907
const char * GetCStrFromOffset(const TSize &Offset) const
Definition: hash.h:698
THash< TUInt, TUInt > TUIntH
Definition: hash.h:583
void GetDatKeyPrV(TVec< TPair< TDat, TKey > > &DatKeyPrV) const
Definition: hash.h:465
void GetDatV(TVec< TDat > &DatV) const
Definition: hash.h:929
THash< TChTr, TInt > TChTrIntH
Definition: hash.h:560
TStr GetStrFromOffset(const TSize &Offset) const
Definition: hash.h:696
void Save(TSOut &SOut) const
Definition: hash.h:645
void LoadCs()
Definition: fl.cpp:28
TPair< TKey, TDat > TKeyDatP
Definition: hash.h:96
void Load(TSIn &SIn)
Definition: hash.h:137
const TKey & GetKey() const
Definition: hash.h:71
void Save(const TStr &fileName)
Definition: hash.h:678
THash< TStrV, TInt > TStrVIntH
Definition: hash.h:620
void Pack()
Definition: hash.h:817
void Defrag()
Definition: hash.h:509
int GetSecHashCd(const int &StrId)
Definition: hash.h:709
TInt FreeKeys
Definition: hash.h:100
Definition: fl.h:58
void Save(TSOut &SOut) const
Definition: ds.h:885
int AddStr(const char *Str)
Definition: hash.h:688
bool Get(const TKey &Key, TDat &Dat)
Definition: hash.h:1052
THash< TDbStr, TStr > TDbStrStrH
Definition: hash.h:611
THash< TStrPr, TStrV > TStrPrStrVH
Definition: hash.h:616
#define ClassTP(TNm, PNm)
Definition: bd.h:126
THash< TStrV, TStrV > TStrVStrVH
Definition: hash.h:623
TStrHash< TInt > TStrIntSH
Definition: hash.h:961
const char * KeyFromOfs(const int &KeyO) const
Definition: hash.h:797
static PSIn New(const TStr &FNm)
Definition: fl.cpp:290
void Swap(THash &Hash)
Definition: hash.h:498
void DelKey(const TKey &Key)
Definition: hash.h:358
TBool AutoSizeP
Definition: hash.h:99
int AddKey(const TChA &Key)
Definition: hash.h:765
int Cmp(const int &StrId, const char *Str) const
Definition: hash.h:702
void MarkDelKey(const TKey &Key)
Definition: hash.h:380
THash< TInt, TFltTr > TIntFltTrH
Definition: hash.h:571
bool operator<(const THashKeyDatI &HashKeyDatI) const
Definition: hash.h:57
THash< TInt, TFlt > TIntFltH
Definition: hash.h:569
void SaveCs()
Definition: fl.h:171
THash< TInt, TFltV > TIntFltVH
Definition: hash.h:572
void LoadXml(const PXmlTok &XmlTok, const TStr &Nm="")
Definition: xmlser.h:119
const TDat & GetDat() const
Definition: hash.h:72
static TPt< PHash< TKey, TDat > > New()
Definition: hash.h:635
THash< TInt, TInt > TIntIntH
Definition: hash.h:564
THash< TStr, TIntPr > TStrIntPrH
Definition: hash.h:595
char * CStr()
Definition: dt.h:255
void Gen(const int &ExpectVals)
Definition: hash.h:180
void GetDatKeyPrV(TVec< TPair< TDat, TStr > > &DatKeyPrV) const
Definition: hash.h:948
bool IsKey(const char *Key) const
Definition: hash.h:799
THash< TStr, TIntFltPr > TStrIntFltPrH
Definition: hash.h:607
THashKeyDat< TKey, TDat > THKeyDat
Definition: hash.h:95
bool IsEnd() const
Tests whether the iterator is pointing to the past-end element.
Definition: hash.h:69
TDat & AddDat(const TKey &Key, const TDat &Dat)
Definition: hash.h:197
static int GetPrimHashCd(const TStr &s)
Definition: hash.h:1132
THash< TKey, TDat > H
Definition: hash.h:632
THash< TInt, TIntFltPr > TIntIntFltPrH
Definition: hash.h:566
void PutAll(const TVal &Val)
Sets all elements of the vector to value Val.
Definition: ds.h:1130
void PutRefToBs(void *_RefToBs)
Definition: hash.h:997
static TPt< PHash< TKey, TDat > > New(const int &MxVals, const int &Vals)
Definition: hash.h:638
bool IsKeyIdEqKeyN() const
Definition: hash.h:761
THash< TStrV, TIntV > TStrVIntVH
Definition: hash.h:621
THKeyDat * KeyDatI
Definition: hash.h:44
THash< TStr, TStrIntKdV > TStrStrIntKdVH
Definition: hash.h:609
static PBigStrPool New(TSIn &SIn)
Definition: hash.h:674
const char * GetKey(const int &KeyId) const
Definition: hash.h:795
TPair< TInt, TDat > TKeyDatP
Definition: hash.h:721
void GetKeyDatPrV(TVec< TPair< TStr, TDat > > &KeyDatPrV) const
Definition: hash.h:937
void Del(const TKey &Key, const bool &DoEventCall=true)
Definition: hash.h:1063
static TPt< PHash< TKey, TDat > > Load(TSIn &SIn)
Definition: hash.h:644
THashKeyDatI & operator++(int)
Definition: hash.h:59
TLstNd< TKey > * TKeyLN
Definition: hash.h:969
bool FNextKeyId(int &KeyId) const
Definition: hash.h:432
void Load(TSIn &SIn)
Definition: dt.h:1057
size_t TSize
Definition: bd.h:58
#define Assert(Cond)
Definition: bd.h:251
THashKeyDatI & Next()
Definition: hash.h:64
bool IsKeyGetDat(const TKey &Key, TDat &Dat) const
Definition: hash.h:228
TStrHash< TInt > TStrSH
Definition: hash.h:960
THash< TInt, TIntV > TIntIntVH
Definition: hash.h:567
int GetSecHashCd() const
Definition: bd.h:507
THKeyDatV KeyDatV
Definition: hash.h:724
THashKeyDatCmp(THash< TKey, TDat, THashFunc > &_Hash, const bool &_CmpKey, const bool &_Asc)
Definition: hash.h:106
THashKeyDatI(const THKeyDat *_KeyDatI, const THKeyDat *_EndI)
Definition: hash.h:50
int FFirstKeyId() const
Definition: hash.h:232
TRec * operator()() const
Definition: bd.h:497
::TSize GetMemUsed() const
Definition: hash.h:159
void FlushAndClr()
Definition: hash.h:1094
const THKeyDat & GetHashKeyDat(const int &KeyId) const
Definition: hash.h:731
void Save(TSOut &SOut, bool PoolToo=true) const
Definition: hash.h:747
static int GetPrimHashCd(const char *p)
Definition: hash.h:1122
THash< TStrPr, TBool > TStrPrBoolH
Definition: hash.h:612
THash< TStr, TFlt > TStrFltH
Definition: hash.h:600
static TStr GetNullStr()
Definition: dt.cpp:1626
void GetKeyDat(const int &KeyId, int &KeyO, TDat &Dat) const
Definition: hash.h:788
THash< TIntV, TInt > TIntVIntH
Definition: hash.h:582
void Resize()
Definition: hash.h:273
int64 GetMxMemUsed() const
Definition: hash.h:986
void Save(TSOut &SOut) const
Definition: hash.h:20
THash< TStrPr, TStr > TStrPrStrH
Definition: hash.h:615
TDat & GetDatId(const int &KeyId)
Definition: hash.h:786
void Resize()
Definition: hash.h:833
THash< TKey, TKeyLNDatPr, THashFunc > KeyDatH
Definition: hash.h:973
TStrHash(const int &Ports, const bool &_AutoSizeP=false, const PStringPool &StrPool=PStringPool())
Definition: hash.h:738
Definition: fl.h:128
const TDat & operator[](const int &KeyId) const
The [] operator takes KeyId, use GetDat() if you need value access via the key.
Definition: hash.h:156
THash< TInt, TInt > TIntH
Definition: hash.h:561
PHash< TKey, TDat > & operator=(const PHash< TKey, TDat > &Vec)
Definition: hash.h:647
THashKeyDatI< TKey, TDat > TIter
Definition: hash.h:93
const TDat & operator()(const char *Key) const
Definition: hash.h:778
int AddKey(const char *Key)
Definition: hash.h:870
uint GetNextPrime(const uint &Val) const
Definition: hash.h:821
int GetMxKeyIds() const
Definition: hash.h:189
THash< TDbStr, TInt > TDbStrIntH
Definition: hash.h:610
THashKeyDat< TKey, TDat > THKeyDat
Definition: hash.h:42
bool FNextKeyDat(void *&KeyDatP, TKey &Key, TDat &Dat)
Definition: hash.h:1107
THash< TStr, TUInt64 > TStrUInt64H
Definition: hash.h:597
void SortByKey(const bool &Asc=true)
Definition: hash.h:245
Definition: dt.h:1042
int GetKeyId(const TKey &Key) const
Definition: hash.h:420
const TDat & GetDatId(const int &KeyId) const
Definition: hash.h:787
Definition: hash.h:716
Definition: dt.h:201
THash< TIntStrPr, TInt > TIntStrPrIntH
Definition: hash.h:590
Definition: hash.h:628
TStrHash(const PStringPool &StrPool)
Definition: hash.h:737
bool Empty() const
Definition: hash.h:755
Definition: ds.h:32
int AddKey(const TKey &Key)
Definition: hash.h:327
TInt FFreeKeyId
Definition: hash.h:100
TLst< TKey > TKeyL
Definition: hash.h:969
static PBigStrPool Load(TSIn &SIn, bool LoadCompacted=true)
Definition: hash.h:676
void Load(TSIn &SIn, bool PoolToo=true)
Definition: hash.h:745
bool RefreshMemUsed()
Definition: hash.h:1024
TDat & AddDat(const TStr &Key)
Definition: hash.h:770
TBool AutoSizeP
Definition: hash.h:725
int GetKeyId(const TStr &Key) const
Definition: hash.h:794
THash()
Definition: hash.h:126
THash< TStr, TStrKdV > TStrStrKdVH
Definition: hash.h:606
bool operator<(const THash &Hash) const
Definition: hash.h:154
THKeyDat * operator->() const
Definition: hash.h:63
long long int64
Definition: bd.h:27
THash< TStr, TIntPrV > TStrIntPrVH
Definition: hash.h:599
void GetStrIdV(TIntV &StrIdV) const
Definition: hash.h:921
void GetKeyV(TVec< TKey > &KeyV) const
Definition: hash.h:438
THash< TIntPr, TStr > TIntPrStrH
Definition: hash.h:588
TStrHash(const TStrHash &Hash)
Definition: hash.h:740
THash< TStr, TStrIntPrV > TStrStrIntPrVH
Definition: hash.h:608
void Save(TSOut &SOut) const
Definition: xmlser.h:16
Definition: dt.h:412
int GetReservedKeyIds() const
Definition: hash.h:190
uint GetNextPrime(const uint &Val) const
Definition: hash.h:261
const TDat & GetDat(const TStr &Key)
Definition: hash.h:784
TIter BegI() const
Returns an iterator pointing to the first element in the vector.
Definition: ds.h:550
void Pack()
The vector reduces its capacity (frees memory) to match its size.
Definition: ds.h:987
THashKeyDatI & operator=(const THashKeyDatI &HashKeyDatI)
Definition: hash.h:53
THash< TStr, TStrV > TStrStrVH
Definition: hash.h:604
TSize Size() const
Definition: hash.h:682
void * FFirstKeyDat()
Definition: hash.h:1102
int GetPorts() const
Definition: hash.h:187
THKeyDat & GetHashKeyDat(const int &KeyId)
Definition: hash.h:117
Definition: hash.h:88
TKey Key
Definition: hash.h:11
void SaveXml(TSOut &SOut, const TStr &Nm)
Definition: xmlser.h:133
TIntV PortV
Definition: hash.h:97
static int GetPrimHashCd(const TKey &Key)
Definition: hash.h:81
void GetKeyDatPrV(TVec< TPair< TKey, TDat > > &KeyDatPrV) const
Definition: hash.h:454
THKeyDat & operator*() const
Definition: hash.h:61
void GetKeyV(TVec< TStr > &KeyV) const
Definition: hash.h:913
static int GetSecHashCd(const TVec &Vec)
Definition: hash.h:1175
bool operator()(const int &KeyId1, const int &KeyId2) const
Definition: hash.h:108
bool DelIfKey(const TKey &Key)
Definition: hash.h:201
void Clr(bool DoDel=false)
Definition: hash.h:701
TVec< THKeyDat > KeyDatV
Definition: hash.h:98
TVal1 Val1
Definition: ds.h:34
int GetPorts() const
Definition: hash.h:758
TVal2 Val2
Definition: ds.h:35
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:315
Definition: bd.h:196
THash< TStrV, TInt > TStrVH
Definition: hash.h:619
static int GetSecHashCd(const TStr &s)
Definition: hash.h:1162
THash< TStr, TBool > TStrBoolH
Definition: hash.h:593
TStrHash< TIntV > TStrToIntVSH
Definition: hash.h:962
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...
Definition: hash.h:398
THash< TStrV, TStr > TStrVStrH
Definition: hash.h:622
THash< TFlt, TFlt > TFltFltH
Definition: hash.h:591
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:486
TStrHash & operator=(const TStrHash &Hash)
Definition: hash.h:856
TVal & GetVal()
Definition: ds.h:2581
THashKeyDat< TInt, TDat > THKeyDat
Definition: hash.h:720
int GetUniDevInt(const int &Range=0)
Definition: dt.cpp:39
int GetPrimHashCd(const int &StrId)
Definition: hash.h:707
TDat & AddDat(const char *Key)
Definition: hash.h:769
const THash< TKey, TDat, THashFunc > & Hash
Definition: hash.h:104
bool IsKeyGetDat(const char *Key, TDat &Dat) const
Definition: hash.h:803
TStrHash(TSIn &SIn, bool PoolToo=true)
Definition: hash.h:743
TDat & AddDatId(const TKey &Key)
Definition: hash.h:194
THKeyDat & GetHashKeyDat(const int &KeyId)
Definition: hash.h:733
~TBigStrPool()
Definition: hash.h:671
TPt< TStringPool > PStringPool
Definition: hash.h:719
bool IsKey(const TKey &Key, int &KeyId) const
Definition: hash.h:217
static int GetPrimHashCd(const TStr &s)
Definition: hash.h:1161
char * CStr()
Definition: dt.h:476
bool IsKey(const TKey &Key) const
Definition: hash.h:216
bool operator==(const PHash< TKey, TDat > &Vec) const
Definition: hash.h:649
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:559
static unsigned int DJBHash(const char *Str, const ::TSize &Len)
Definition: hash.h:1148
THash< TStr, TInt > TStrIntH
Definition: hash.h:594
Definition: dt.h:881
Definition: hash.h:967
THash< TUInt64, TStrV > TUInt64StrVH
Definition: hash.h:577
int Len() const
Definition: hash.h:186
static TPt< PHash< TKey, TDat > > New(const THash< TKey, TDat > &H)
Definition: hash.h:641
TDat & GetDat()
Definition: hash.h:73
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
THKeyDat & operator()() const
Definition: hash.h:62
bool IsKey(const char *Key, int &KeyId) const
Definition: hash.h:802
bool IsAutoSize() const
Definition: hash.h:188
TCache & operator=(const TCache &)
THash< TStrPr, TFlt > TStrPrFltH
Definition: hash.h:614
THash< TStr, TStrPrV > TStrStrPrVH
Definition: hash.h:605
THKeyDat * EndI
Definition: hash.h:45
TDat & AddDat(const TChA &Key)
Definition: hash.h:771
TInt HashCd
Definition: hash.h:10
THash< TInt, TStrV > TIntStrVH
Definition: hash.h:574
THash< TInt, TIntPr > TIntIntPrH
Definition: hash.h:575
THash< TStrPr, TInt > TStrPrIntH
Definition: hash.h:613
THash(TSIn &SIn)
Definition: hash.h:133
static int GetPrimHashCd(const TVec &Vec)
Definition: hash.h:1169
TDat & AddDatId(const TChA &Key)
Definition: hash.h:774
int GetPrimHashCd() const
Definition: bd.h:506
void Pack()
Definition: hash.h:243
int AddDat(const TChA &Key, const TDat &Dat)
Definition: hash.h:768
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
bool IsKey(const TStr &Key) const
Definition: hash.h:800
THash(const THash &Hash)
Definition: hash.h:129
const TDat & GetDat(const TChA &Key)
Definition: hash.h:785
void GetDatKeyKdV(TVec< TKeyDat< TDat, TKey > > &DatKeyKdV) const
Definition: hash.h:487
int GetStrs() const
Definition: hash.h:680
THash< TInt, TIntPrV > TIntIntPrVH
Definition: hash.h:576
void GetKeyDatKdV(TVec< TKeyDat< TKey, TDat > > &KeyDatKdV) const
Definition: hash.h:476
static int GetSecHashCd(const char *p)
Definition: hash.cpp:91
THash< TInt, TBool > TIntBoolH
Definition: hash.h:563
int GetKeyId(const char *Key) const
Definition: hash.h:896
void Swap(TRec &Rec1, TRec &Rec2)
Definition: bd.h:568
void MarkDelKeyId(const int &KeyId)
Definition: hash.h:208
bool IsKeyId(const int &KeyId) const
Definition: hash.h:806
void SaveXml(TSOut &SOut, const TStr &Nm) const
Definition: xmlser.h:123
THashKeyDatI & operator--(int)
Definition: hash.h:60
const TDat & GetDat(const char *Key) const
Definition: hash.h:781
static int GetPrimHashCd(const char *p)
Definition: hash.h:1155
void * GetRefToBs()
Definition: hash.h:998
TIter GetI(const TKey &Key) const
Definition: hash.h:178
void SortByDat(const bool &Asc=true)
Definition: hash.h:246
TLstNd * Next() const
Definition: ds.h:2580
static int GetSecHashCd(const char *p)
Definition: hash.h:1127