SNAP Library 2.2, User Reference
2014-03-11 19:15:55
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
|
#include <hash.h>
Public Member Functions | |
TStrHash () | |
TStrHash (const PStringPool &StrPool) | |
TStrHash (const int &Ports, const bool &_AutoSizeP=false, const PStringPool &StrPool=PStringPool()) | |
TStrHash (const TStrHash &Hash) | |
TStrHash (TSIn &SIn, bool PoolToo=true) | |
void | Load (TSIn &SIn, bool PoolToo=true) |
void | Save (TSOut &SOut, bool PoolToo=true) const |
void | SetPool (const PStringPool &StrPool) |
PStringPool | GetPool () const |
TStrHash & | operator= (const TStrHash &Hash) |
bool | Empty () const |
int | Len () const |
int | Reserved () const |
int | GetPorts () const |
bool | IsAutoSize () const |
int | GetMxKeyIds () const |
bool | IsKeyIdEqKeyN () const |
int | AddKey (const char *Key) |
int | AddKey (const TStr &Key) |
int | AddKey (const TChA &Key) |
int | AddDat (const char *Key, const TDat &Dat) |
int | AddDat (const TStr &Key, const TDat &Dat) |
int | AddDat (const TChA &Key, const TDat &Dat) |
TDat & | AddDat (const char *Key) |
TDat & | AddDat (const TStr &Key) |
TDat & | AddDat (const TChA &Key) |
TDat & | AddDatId (const char *Key) |
TDat & | AddDatId (const TStr &Key) |
TDat & | AddDatId (const TChA &Key) |
const TDat & | operator[] (const int &KeyId) const |
TDat & | operator[] (const int &KeyId) |
const TDat & | operator() (const char *Key) const |
const TDat & | GetDat (const char *Key) const |
const TDat & | GetDat (const TStr &Key) const |
TDat & | GetDat (const char *Key) |
const TDat & | GetDat (const TStr &Key) |
const TDat & | GetDat (const TChA &Key) |
TDat & | GetDatId (const int &KeyId) |
const TDat & | GetDatId (const int &KeyId) const |
void | GetKeyDat (const int &KeyId, int &KeyO, TDat &Dat) const |
void | GetKeyDat (const int &KeyId, const char *&Key, TDat &Dat) const |
void | GetKeyDat (const int &KeyId, TStr &Key, TDat &Dat) const |
void | GetKeyDat (const int &KeyId, TChA &Key, TDat &Dat) const |
int | GetKeyId (const char *Key) const |
int | GetKeyId (const TStr &Key) const |
const char * | GetKey (const int &KeyId) const |
int | GetKeyOfs (const int &KeyId) const |
const char * | KeyFromOfs (const int &KeyO) const |
bool | IsKey (const char *Key) const |
bool | IsKey (const TStr &Key) const |
bool | IsKey (const TChA &Key) const |
bool | IsKey (const char *Key, int &KeyId) const |
bool | IsKeyGetDat (const char *Key, TDat &Dat) const |
bool | IsKeyGetDat (const TStr &Key, TDat &Dat) const |
bool | IsKeyGetDat (const TChA &Key, TDat &Dat) const |
bool | IsKeyId (const int &KeyId) const |
int | FFirstKeyId () const |
bool | FNextKeyId (int &KeyId) const |
void | GetKeyV (TVec< TStr > &KeyV) const |
void | GetStrIdV (TIntV &StrIdV) const |
void | GetDatV (TVec< TDat > &DatV) const |
void | GetKeyDatPrV (TVec< TPair< TStr, TDat > > &KeyDatPrV) const |
void | GetDatKeyPrV (TVec< TPair< TDat, TStr > > &DatKeyPrV) const |
void | Pack () |
Private Types | |
typedef TPt< TStringPool > | PStringPool |
typedef THashKeyDat< TInt, TDat > | THKeyDat |
typedef TPair< TInt, TDat > | TKeyDatP |
typedef TVec< THKeyDat > | THKeyDatV |
Private Member Functions | |
uint | GetNextPrime (const uint &Val) const |
void | Resize () |
const THKeyDat & | GetHashKeyDat (const int &KeyId) const |
THKeyDat & | GetHashKeyDat (const int &KeyId) |
Private Attributes | |
TIntV | PortV |
THKeyDatV | KeyDatV |
TBool | AutoSizeP |
TInt | FFreeKeyId |
TInt | FreeKeys |
PStringPool | Pool |
typedef TPt<TStringPool> TStrHash< TDat, TStringPool, THashFunc >::PStringPool [private] |
typedef THashKeyDat<TInt, TDat> TStrHash< TDat, TStringPool, THashFunc >::THKeyDat [private] |
TStrHash< TDat, TStringPool, THashFunc >::TStrHash | ( | const PStringPool & | StrPool | ) | [inline] |
TStrHash< TDat, TStringPool, THashFunc >::TStrHash | ( | const int & | Ports, |
const bool & | _AutoSizeP = false , |
||
const PStringPool & | StrPool = PStringPool() |
||
) | [inline] |
TStrHash< TDat, TStringPool, THashFunc >::TStrHash | ( | TSIn & | SIn, |
bool | PoolToo = true |
||
) | [inline] |
Definition at line 742 of file hash.h.
: PortV(SIn), KeyDatV(SIn), AutoSizeP(SIn), FFreeKeyId(SIn), FreeKeys(SIn){ SIn.LoadCs(); if (PoolToo) Pool = PStringPool(SIn); }
int TStrHash< TDat, TStringPool, THashFunc >::AddKey | ( | const char * | Key | ) |
Definition at line 869 of file hash.h.
{ if (Pool.Empty()) Pool = TStringPool::New(); if ((AutoSizeP && KeyDatV.Len() > PortV.Len()) || PortV.Empty()) Resize(); const int PortN = abs(THashFunc::GetPrimHashCd(Key) % PortV.Len()); const int HashCd = abs(THashFunc::GetSecHashCd(Key)); int PrevKeyId = -1; int KeyId = PortV[PortN]; while (KeyId != -1 && ! (KeyDatV[KeyId].HashCd == HashCd && Pool->Cmp(KeyDatV[KeyId].Key, Key) == 0)) { PrevKeyId = KeyId; KeyId = KeyDatV[KeyId].Next; } if (KeyId == -1) { const int StrId = Pool->AddStr(Key); if (FFreeKeyId == -1) { KeyId = KeyDatV.Add(THKeyDat(-1, HashCd, StrId)); } else { KeyId = FFreeKeyId; FFreeKeyId = KeyDatV[FFreeKeyId].Next; FreeKeys--; KeyDatV[KeyId] = THKeyDat(-1, HashCd, StrId); } if (PrevKeyId == -1) PortV[PortN] = KeyId; else KeyDatV[PrevKeyId].Next = KeyId; } return KeyId; }
int TStrHash< TDat, TStringPool, THashFunc >::FFirstKeyId | ( | ) | const [inline] |
bool TStrHash< TDat, TStringPool, THashFunc >::FNextKeyId | ( | int & | KeyId | ) | const |
void TStrHash< TDat, TStringPool, THashFunc >::GetDatKeyPrV | ( | TVec< TPair< TDat, TStr > > & | DatKeyPrV | ) | const |
Definition at line 947 of file hash.h.
{ DatKeyPrV.Gen(Len(), 0); TStr Str; TDat Dat; int KeyId = FFirstKeyId(); while (FNextKeyId(KeyId)){ GetKeyDat(KeyId, Str, Dat); DatKeyPrV.Add(TPair<TDat, TStr>(Dat, Str)); } }
void TStrHash< TDat, TStringPool, THashFunc >::GetDatV | ( | TVec< TDat > & | DatV | ) | const |
Definition at line 928 of file hash.h.
{ DatV.Gen(Len(), 0); int KeyId = FFirstKeyId(); while (FNextKeyId(KeyId)) DatV.Add(GetHashKeyDat(KeyId).Dat); }
const THKeyDat& TStrHash< TDat, TStringPool, THashFunc >::GetHashKeyDat | ( | const int & | KeyId | ) | const [inline, private] |
THKeyDat& TStrHash< TDat, TStringPool, THashFunc >::GetHashKeyDat | ( | const int & | KeyId | ) | [inline, private] |
const char* TStrHash< TDat, TStringPool, THashFunc >::GetKey | ( | const int & | KeyId | ) | const [inline] |
Definition at line 794 of file hash.h.
{ return Pool->GetCStr(GetHashKeyDat(KeyId).Key); }
void TStrHash< TDat, TStringPool, THashFunc >::GetKeyDat | ( | const int & | KeyId, |
int & | KeyO, | ||
TDat & | Dat | ||
) | const [inline] |
Definition at line 787 of file hash.h.
{ const THKeyDat& KeyDat = GetHashKeyDat(KeyId); KeyO = KeyDat.Key; Dat = KeyDat.Dat; }
void TStrHash< TDat, TStringPool, THashFunc >::GetKeyDat | ( | const int & | KeyId, |
const char *& | Key, | ||
TDat & | Dat | ||
) | const [inline] |
Definition at line 788 of file hash.h.
{ const THKeyDat& KeyDat = GetHashKeyDat(KeyId); Key = KeyFromOfs(KeyDat.Key); Dat = KeyDat.Dat; }
void TStrHash< TDat, TStringPool, THashFunc >::GetKeyDat | ( | const int & | KeyId, |
TStr & | Key, | ||
TDat & | Dat | ||
) | const [inline] |
Definition at line 789 of file hash.h.
{ const THKeyDat& KeyDat = GetHashKeyDat(KeyId); Key = KeyFromOfs(KeyDat.Key); Dat = KeyDat.Dat;}
void TStrHash< TDat, TStringPool, THashFunc >::GetKeyDat | ( | const int & | KeyId, |
TChA & | Key, | ||
TDat & | Dat | ||
) | const [inline] |
Definition at line 790 of file hash.h.
{ const THKeyDat& KeyDat = GetHashKeyDat(KeyId); Key = KeyFromOfs(KeyDat.Key); Dat = KeyDat.Dat;}
void TStrHash< TDat, TStringPool, THashFunc >::GetKeyDatPrV | ( | TVec< TPair< TStr, TDat > > & | KeyDatPrV | ) | const |
Definition at line 936 of file hash.h.
{ KeyDatPrV.Gen(Len(), 0); TStr Str; TDat Dat; int KeyId = FFirstKeyId(); while (FNextKeyId(KeyId)){ GetKeyDat(KeyId, Str, Dat); KeyDatPrV.Add(TPair<TStr, TDat>(Str, Dat)); } }
int TStrHash< TDat, TStringPool, THashFunc >::GetKeyId | ( | const char * | Key | ) | const |
Definition at line 895 of file hash.h.
{ if (PortV.Empty()) return -1; const int PortN = abs(THashFunc::GetPrimHashCd(Key) % PortV.Len()); const int Hc = abs(THashFunc::GetSecHashCd(Key)); int KeyId = PortV[PortN]; while (KeyId != -1 && ! (KeyDatV[KeyId].HashCd == Hc && Pool->Cmp(KeyDatV[KeyId].Key, Key) == 0)) KeyId = KeyDatV[KeyId].Next; return KeyId; }
int TStrHash< TDat, TStringPool, THashFunc >::GetKeyOfs | ( | const int & | KeyId | ) | const [inline] |
Definition at line 795 of file hash.h.
{ return GetHashKeyDat(KeyId).Key; } // pool string id
void TStrHash< TDat, TStringPool, THashFunc >::GetKeyV | ( | TVec< TStr > & | KeyV | ) | const |
Definition at line 912 of file hash.h.
{ KeyV.Gen(Len(), 0); int KeyId = FFirstKeyId(); while (FNextKeyId(KeyId)) KeyV.Add(GetKey(KeyId)); }
int TStrHash< TDat, TStringPool, THashFunc >::GetMxKeyIds | ( | ) | const [inline] |
uint TStrHash< TDat, TStringPool, THashFunc >::GetNextPrime | ( | const uint & | Val | ) | const [private] |
Definition at line 820 of file hash.h.
{ uint *f = (uint *) TIntH::HashPrimeT, *m, *l = (uint *) TIntH::HashPrimeT + (int) TIntH::HashPrimes; int h, len = (int)TIntH::HashPrimes; while (len > 0) { h = len >> 1; m = f + h; if (*m < Val) { f = m; f++; len = len - h - 1; } else len = h; } return f == l ? *(l - 1) : *f; }
PStringPool TStrHash< TDat, TStringPool, THashFunc >::GetPool | ( | ) | const [inline] |
void TStrHash< TDat, TStringPool, THashFunc >::GetStrIdV | ( | TIntV & | StrIdV | ) | const |
Definition at line 920 of file hash.h.
{ StrIdV.Gen(Len(), 0); int KeyId = FFirstKeyId(); while (FNextKeyId(KeyId)) StrIdV.Add(GetKeyOfs(KeyId)); }
bool TStrHash< TDat, TStringPool, THashFunc >::IsAutoSize | ( | ) | const [inline] |
bool TStrHash< TDat, TStringPool, THashFunc >::IsKeyGetDat | ( | const char * | Key, |
TDat & | Dat | ||
) | const [inline] |
bool TStrHash< TDat, TStringPool, THashFunc >::IsKeyGetDat | ( | const TStr & | Key, |
TDat & | Dat | ||
) | const [inline] |
bool TStrHash< TDat, TStringPool, THashFunc >::IsKeyGetDat | ( | const TChA & | Key, |
TDat & | Dat | ||
) | const [inline] |
bool TStrHash< TDat, TStringPool, THashFunc >::IsKeyIdEqKeyN | ( | ) | const [inline] |
const char* TStrHash< TDat, TStringPool, THashFunc >::KeyFromOfs | ( | const int & | KeyO | ) | const [inline] |
const TDat& TStrHash< TDat, TStringPool, THashFunc >::operator() | ( | const char * | Key | ) | const [inline] |
const TDat& TStrHash< TDat, TStringPool, THashFunc >::operator[] | ( | const int & | KeyId | ) | const [inline] |
Definition at line 775 of file hash.h.
{return GetHashKeyDat(KeyId).Dat;}
TDat& TStrHash< TDat, TStringPool, THashFunc >::operator[] | ( | const int & | KeyId | ) | [inline] |
Definition at line 776 of file hash.h.
{return GetHashKeyDat(KeyId).Dat;}
void TStrHash< TDat, TStringPool, THashFunc >::Resize | ( | ) | [private] |
Definition at line 832 of file hash.h.
{ // resize & initialize port vector if (PortV.Empty()) { PortV.Gen(17); PortV.PutAll(-1); } else if (AutoSizeP && KeyDatV.Len() > 3 * PortV.Len()) { const int NxPrime = GetNextPrime(KeyDatV.Len()); //printf("%s resize PortV: %d -> %d, Len: %d\n", GetTypeNm(*this).CStr(), PortV.Len(), NxPrime, Len()); PortV.Gen(NxPrime); PortV.PutAll(-1); } else return; // rehash keys const int NPorts = PortV.Len(); for (int i = 0; i < KeyDatV.Len(); i++) { THKeyDat& KeyDat = KeyDatV[i]; if (KeyDat.HashCd != -1) { const int Port = abs(THashFunc::GetPrimHashCd(Pool->GetCStr(KeyDat.Key)) % NPorts); KeyDat.Next = PortV[Port]; PortV[Port] = i; } } }
void TStrHash< TDat, TStringPool, THashFunc >::SetPool | ( | const PStringPool & | StrPool | ) | [inline] |
TInt TStrHash< TDat, TStringPool, THashFunc >::FFreeKeyId [private] |
PStringPool TStrHash< TDat, TStringPool, THashFunc >::Pool [private] |