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