|
SNAP Library 2.1, User Reference
2013-09-25 10:47:25
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
|
#include <hash.h>
Classes | |
| class | THashKeyDatCmp |
Public Types | |
| enum | { HashPrimes = 32 } |
| typedef THashKeyDatI< TKey, TDat > | TIter |
Public Member Functions | |
| THash () | |
| THash (const THash &Hash) | |
| THash (const int &ExpectVals, const bool &_AutoSizeP=false) | |
| THash (TSIn &SIn) | |
| void | Load (TSIn &SIn) |
| void | Save (TSOut &SOut) const |
| void | LoadXml (const PXmlTok &XmlTok, const TStr &Nm="") |
| void | SaveXml (TSOut &SOut, const TStr &Nm) |
| THash & | operator= (const THash &Hash) |
| bool | operator== (const THash &Hash) const |
| bool | operator< (const THash &Hash) const |
| const TDat & | operator[] (const int &KeyId) const |
| TDat & | operator[] (const int &KeyId) |
| TDat & | operator() (const TKey &Key) |
| ::TSize | GetMemUsed () const |
| TIter | BegI () const |
| TIter | EndI () const |
| TIter | GetI (const TKey &Key) const |
| void | Gen (const int &ExpectVals) |
| void | Clr (const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true) |
| bool | Empty () const |
| int | Len () const |
| int | GetPorts () const |
| bool | IsAutoSize () const |
| int | GetMxKeyIds () const |
| int | GetReservedKeyIds () const |
| bool | IsKeyIdEqKeyN () const |
| int | AddKey (const TKey &Key) |
| TDat & | AddDatId (const TKey &Key) |
| TDat & | AddDat (const TKey &Key) |
| TDat & | AddDat (const TKey &Key, const TDat &Dat) |
| void | DelKey (const TKey &Key) |
| bool | DelIfKey (const TKey &Key) |
| void | DelKeyId (const int &KeyId) |
| void | DelKeyIdV (const TIntV &KeyIdV) |
| void | MarkDelKey (const TKey &Key) |
| void | MarkDelKeyId (const int &KeyId) |
| const TKey & | GetKey (const int &KeyId) const |
| int | GetKeyId (const TKey &Key) const |
| 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. | |
| int | GetRndKeyId (TRnd &Rnd, const double &EmptyFrac) |
| Get an index of a random element. If the hash table has many deleted keys, defrag the hash table first (that's why the function is non-const). | |
| bool | IsKey (const TKey &Key) const |
| bool | IsKey (const TKey &Key, int &KeyId) const |
| bool | IsKeyId (const int &KeyId) const |
| const TDat & | GetDat (const TKey &Key) const |
| TDat & | GetDat (const TKey &Key) |
| void | GetKeyDat (const int &KeyId, TKey &Key, TDat &Dat) const |
| bool | IsKeyGetDat (const TKey &Key, TDat &Dat) const |
| int | FFirstKeyId () const |
| bool | FNextKeyId (int &KeyId) const |
| void | GetKeyV (TVec< TKey > &KeyV) const |
| void | GetDatV (TVec< TDat > &DatV) const |
| void | GetKeyDatPrV (TVec< TPair< TKey, TDat > > &KeyDatPrV) const |
| void | GetDatKeyPrV (TVec< TPair< TDat, TKey > > &DatKeyPrV) const |
| void | GetKeyDatKdV (TVec< TKeyDat< TKey, TDat > > &KeyDatKdV) const |
| void | GetDatKeyKdV (TVec< TKeyDat< TDat, TKey > > &DatKeyKdV) const |
| void | Swap (THash &Hash) |
| void | Defrag () |
| void | Pack () |
| void | Sort (const bool &CmpKey, const bool &Asc) |
| void | SortByKey (const bool &Asc=true) |
| void | SortByDat (const bool &Asc=true) |
Static Public Attributes | |
| static const unsigned int | HashPrimeT [HashPrimes] |
Private Types | |
| typedef THashKeyDat< TKey, TDat > | THKeyDat |
| typedef TPair< TKey, TDat > | TKeyDatP |
Private Member Functions | |
| THKeyDat & | GetHashKeyDat (const int &KeyId) |
| const THKeyDat & | GetHashKeyDat (const int &KeyId) const |
| uint | GetNextPrime (const uint &Val) const |
| void | Resize () |
Private Attributes | |
| TIntV | PortV |
| TVec< THKeyDat > | KeyDatV |
| TBool | AutoSizeP |
| TInt | FFreeKeyId |
| TInt | FreeKeys |
typedef THashKeyDat<TKey, TDat> THash< TKey, TDat, THashFunc >::THKeyDat [private] |
| typedef THashKeyDatI<TKey, TDat> THash< TKey, TDat, THashFunc >::TIter |
| anonymous enum |
| int THash< TKey, TDat, THashFunc >::AddKey | ( | const TKey & | Key | ) |
Definition at line 326 of file hash.h.
{
if ((KeyDatV.Len()>2*PortV.Len())||PortV.Empty()){Resize();}
const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len());
const int HashCd=abs(THashFunc::GetSecHashCd(Key));
int PrevKeyId=-1;
int KeyId=PortV[PortN];
while ((KeyId!=-1) &&
!((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){
PrevKeyId=KeyId; KeyId=KeyDatV[KeyId].Next;}
if (KeyId==-1){
if (FFreeKeyId==-1){
KeyId=KeyDatV.Add(THKeyDat(-1, HashCd, Key));
} else {
KeyId=FFreeKeyId; FFreeKeyId=KeyDatV[FFreeKeyId].Next; FreeKeys--;
//KeyDatV[KeyId]=TKeyDat(-1, HashCd, Key); // slow version
KeyDatV[KeyId].Next=-1;
KeyDatV[KeyId].HashCd=HashCd;
KeyDatV[KeyId].Key=Key;
//KeyDatV[KeyId].Dat=TDat(); // already empty
}
if (PrevKeyId==-1){
PortV[PortN]=KeyId;
} else {
KeyDatV[PrevKeyId].Next=KeyId;
}
}
return KeyId;
}
| void THash< TKey, TDat, THashFunc >::Defrag | ( | ) |
Definition at line 508 of file hash.h.
{
if (!IsKeyIdEqKeyN()){
THash<TKey, TDat, THashFunc> Hash(PortV.Len());
int KeyId=FFirstKeyId(); TKey Key; TDat Dat;
while (FNextKeyId(KeyId)){
GetKeyDat(KeyId, Key, Dat);
Hash.AddDat(Key, Dat);
}
Pack();
operator=(Hash);
IAssert(IsKeyIdEqKeyN());
}
}
| void THash< TKey, TDat, THashFunc >::DelKey | ( | const TKey & | Key | ) |
Definition at line 357 of file hash.h.
{
IAssert(!PortV.Empty());
const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len());
const int HashCd=abs(THashFunc::GetSecHashCd(Key));
int PrevKeyId=-1;
int KeyId=PortV[PortN];
while ((KeyId!=-1) &&
!((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){
PrevKeyId=KeyId; KeyId=KeyDatV[KeyId].Next;}
//IAssertR(KeyId!=-1, Key.GetStr()); //J: some classes do not provide GetStr()?
IAssert(KeyId!=-1); //J: some classes do not provide GetStr()?
if (PrevKeyId==-1){PortV[PortN]=KeyDatV[KeyId].Next;}
else {KeyDatV[PrevKeyId].Next=KeyDatV[KeyId].Next;}
KeyDatV[KeyId].Next=FFreeKeyId; FFreeKeyId=KeyId; FreeKeys++;
KeyDatV[KeyId].HashCd=TInt(-1);
KeyDatV[KeyId].Key=TKey();
KeyDatV[KeyId].Dat=TDat();
}
| int THash< TKey, TDat, THashFunc >::FFirstKeyId | ( | ) | const [inline] |
| bool THash< TKey, TDat, THashFunc >::FNextKeyId | ( | int & | KeyId | ) | const |
| void THash< TKey, TDat, THashFunc >::GetDatKeyKdV | ( | TVec< TKeyDat< TDat, TKey > > & | DatKeyKdV | ) | const |
Definition at line 486 of file hash.h.
{
DatKeyKdV.Gen(Len(), 0);
TKey Key; TDat Dat;
int KeyId=FFirstKeyId();
while (FNextKeyId(KeyId)){
GetKeyDat(KeyId, Key, Dat);
DatKeyKdV.Add(TKeyDat<TDat, TKey>(Dat, Key));
}
}
| void THash< TKey, TDat, THashFunc >::GetDatKeyPrV | ( | TVec< TPair< TDat, TKey > > & | DatKeyPrV | ) | const |
Definition at line 464 of file hash.h.
{
DatKeyPrV.Gen(Len(), 0);
TKey Key; TDat Dat;
int KeyId=FFirstKeyId();
while (FNextKeyId(KeyId)){
GetKeyDat(KeyId, Key, Dat);
DatKeyPrV.Add(TPair<TDat, TKey>(Dat, Key));
}
}
| void THash< TKey, TDat, THashFunc >::GetDatV | ( | TVec< TDat > & | DatV | ) | const |
Definition at line 445 of file hash.h.
{
DatV.Gen(Len(), 0);
int KeyId=FFirstKeyId();
while (FNextKeyId(KeyId)){
DatV.Add(GetHashKeyDat(KeyId).Dat);}
}
| THKeyDat& THash< TKey, TDat, THashFunc >::GetHashKeyDat | ( | const int & | KeyId | ) | [inline, private] |
| const THKeyDat& THash< TKey, TDat, THashFunc >::GetHashKeyDat | ( | const int & | KeyId | ) | const [inline, private] |
| const TKey& THash< TKey, TDat, THashFunc >::GetKey | ( | const int & | KeyId | ) | const [inline] |
Definition at line 209 of file hash.h.
{ return GetHashKeyDat(KeyId).Key;}
| void THash< TKey, TDat, THashFunc >::GetKeyDat | ( | const int & | KeyId, |
| TKey & | Key, | ||
| TDat & | Dat | ||
| ) | const [inline] |
Definition at line 224 of file hash.h.
{
const THKeyDat& KeyDat=GetHashKeyDat(KeyId);
Key=KeyDat.Key; Dat=KeyDat.Dat;}
| void THash< TKey, TDat, THashFunc >::GetKeyDatKdV | ( | TVec< TKeyDat< TKey, TDat > > & | KeyDatKdV | ) | const |
Definition at line 475 of file hash.h.
{
KeyDatKdV.Gen(Len(), 0);
TKey Key; TDat Dat;
int KeyId=FFirstKeyId();
while (FNextKeyId(KeyId)){
GetKeyDat(KeyId, Key, Dat);
KeyDatKdV.Add(TKeyDat<TKey, TDat>(Key, Dat));
}
}
| void THash< TKey, TDat, THashFunc >::GetKeyDatPrV | ( | TVec< TPair< TKey, TDat > > & | KeyDatPrV | ) | const |
Definition at line 453 of file hash.h.
{
KeyDatPrV.Gen(Len(), 0);
TKey Key; TDat Dat;
int KeyId=FFirstKeyId();
while (FNextKeyId(KeyId)){
GetKeyDat(KeyId, Key, Dat);
KeyDatPrV.Add(TPair<TKey, TDat>(Key, Dat));
}
}
| int THash< TKey, TDat, THashFunc >::GetKeyId | ( | const TKey & | Key | ) | const |
Definition at line 419 of file hash.h.
{
if (PortV.Empty()){return -1;}
const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len());
const int HashCd=abs(THashFunc::GetSecHashCd(Key));
int KeyId=PortV[PortN];
while ((KeyId!=-1) &&
!((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){
KeyId=KeyDatV[KeyId].Next;}
return KeyId;
}
| void THash< TKey, TDat, THashFunc >::GetKeyV | ( | TVec< TKey > & | KeyV | ) | const |
Definition at line 437 of file hash.h.
{
KeyV.Gen(Len(), 0);
int KeyId=FFirstKeyId();
while (FNextKeyId(KeyId)){
KeyV.Add(GetKey(KeyId));}
}
| ::TSize THash< TKey, TDat, THashFunc >::GetMemUsed | ( | ) | const [inline] |
Definition at line 158 of file hash.h.
{
// return PortV.GetMemUsed()+KeyDatV.GetMemUsed()+sizeof(bool)+2*sizeof(int);}
int64 MemUsed = sizeof(bool)+2*sizeof(int);
MemUsed += int64(PortV.Reserved()) * int64(sizeof(TInt));
for (int KeyDatN = 0; KeyDatN < KeyDatV.Len(); KeyDatN++) {
MemUsed += int64(2 * sizeof(TInt));
MemUsed += int64(KeyDatV[KeyDatN].Key.GetMemUsed());
MemUsed += int64(KeyDatV[KeyDatN].Dat.GetMemUsed());
}
return ::TSize(MemUsed);
}
| int THash< TKey, TDat, THashFunc >::GetMxKeyIds | ( | ) | const [inline] |
| uint THash< TKey, TDat, THashFunc >::GetNextPrime | ( | const uint & | Val | ) | const [private] |
Definition at line 260 of file hash.h.
{
const uint* f=(const uint*)HashPrimeT, *m, *l=(const uint*)HashPrimeT + (int)HashPrimes;
int h, len = (int)HashPrimes;
while (len > 0) {
h = len >> 1; m = f + h;
if (*m < Val) { f = m; f++; len = len - h - 1; }
else len = h;
}
return f == l ? *(l - 1) : *f;
}
| int THash< TKey, TDat, THashFunc >::GetReservedKeyIds | ( | ) | const [inline] |
| int THash< TKey, TDat, THashFunc >::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 at line 397 of file hash.h.
{
IAssert(! Empty());
int KeyId = abs(Rnd.GetUniDevInt(KeyDatV.Len()));
while (KeyDatV[KeyId].HashCd == -1) { // if the index is empty, just try again
KeyId = abs(Rnd.GetUniDevInt(KeyDatV.Len())); }
return KeyId;
}
| int THash< TKey, TDat, THashFunc >::GetRndKeyId | ( | TRnd & | Rnd, |
| const double & | EmptyFrac | ||
| ) |
Get an index of a random element. If the hash table has many deleted keys, defrag the hash table first (that's why the function is non-const).
| bool THash< TKey, TDat, THashFunc >::IsAutoSize | ( | ) | const [inline] |
| bool THash< TKey, TDat, THashFunc >::IsKeyGetDat | ( | const TKey & | Key, |
| TDat & | Dat | ||
| ) | const [inline] |
Definition at line 227 of file hash.h.
{int KeyId;
if (IsKey(Key, KeyId)){Dat=GetHashKeyDat(KeyId).Dat; return true;}
else {return false;}}
| bool THash< TKey, TDat, THashFunc >::IsKeyIdEqKeyN | ( | ) | const [inline] |
| void THash< TKey, TDat, THashFunc >::MarkDelKey | ( | const TKey & | Key | ) |
Definition at line 379 of file hash.h.
{
// MarkDelKey is same as Delkey except last two lines
IAssert(!PortV.Empty());
const int PortN=abs(THashFunc::GetPrimHashCd(Key)%PortV.Len());
const int HashCd=abs(THashFunc::GetSecHashCd(Key));
int PrevKeyId=-1;
int KeyId=PortV[PortN];
while ((KeyId!=-1) &&
!((KeyDatV[KeyId].HashCd==HashCd) && (KeyDatV[KeyId].Key==Key))){
PrevKeyId=KeyId; KeyId=KeyDatV[KeyId].Next;}
IAssertR(KeyId!=-1, Key.GetStr());
if (PrevKeyId==-1){PortV[PortN]=KeyDatV[KeyId].Next;}
else {KeyDatV[PrevKeyId].Next=KeyDatV[KeyId].Next;}
KeyDatV[KeyId].Next=FFreeKeyId; FFreeKeyId=KeyId; FreeKeys++;
KeyDatV[KeyId].HashCd=TInt(-1);
}
| void THash< TKey, TDat, THashFunc >::MarkDelKeyId | ( | const int & | KeyId | ) | [inline] |
Definition at line 207 of file hash.h.
{MarkDelKey(GetKey(KeyId));}
| TDat& THash< TKey, TDat, THashFunc >::operator() | ( | const TKey & | Key | ) | [inline] |
| bool THash< TKey, TDat, THashFunc >::operator== | ( | const THash< TKey, TDat, THashFunc > & | Hash | ) | const |
Definition at line 303 of file hash.h.
{
if (Len() != Hash.Len()) { return false; }
for (int i = FFirstKeyId(); FNextKeyId(i); ) {
const TKey& Key = GetKey(i);
if (! Hash.IsKey(Key)) { return false; }
if (GetDat(Key) != Hash.GetDat(Key)) { return false; }
}
return true;
}
| const TDat& THash< TKey, TDat, THashFunc >::operator[] | ( | const int & | KeyId | ) | const [inline] |
Definition at line 155 of file hash.h.
{return GetHashKeyDat(KeyId).Dat;}
| TDat& THash< TKey, TDat, THashFunc >::operator[] | ( | const int & | KeyId | ) | [inline] |
Definition at line 156 of file hash.h.
{return GetHashKeyDat(KeyId).Dat;}
| void THash< TKey, TDat, THashFunc >::Resize | ( | ) | [private] |
Definition at line 272 of file hash.h.
{
// resize & initialize port vector
//if (PortV.Len()==0){PortV.Gen(17);}
//else {PortV.Gen(2*PortV.Len()+1);}
if (PortV.Len()==0){
PortV.Gen(17);
} else if (AutoSizeP&&(KeyDatV.Len()>2*PortV.Len())){
PortV.Gen(GetNextPrime(PortV.Len()+1));
} else {
return;
}
PortV.PutAll(TInt(-1));
// rehash keys
for (int KeyId=0; KeyId<KeyDatV.Len(); KeyId++){
THKeyDat& KeyDat=KeyDatV[KeyId];
if (KeyDat.HashCd!=-1){
const int PortN = abs(THashFunc::GetPrimHashCd(KeyDat.Key) % PortV.Len());
KeyDat.Next=PortV[PortN];
PortV[PortN]=KeyId;
}
}
}
| void THash< TKey, TDat, THashFunc >::Sort | ( | const bool & | CmpKey, |
| const bool & | Asc | ||
| ) |
Definition at line 523 of file hash.h.
{
IAssertR(IsKeyIdEqKeyN(), "THash::Sort only works when table has no deleted keys.");
TIntV TargV(Len()), MapV(Len()), StateV(Len());
for (int i = 0; i < TargV.Len(); i++) {
TargV[i] = i; MapV[i] = i; StateV[i] = i;
}
// sort KeyIds
THashKeyDatCmp HashCmp(*this, CmpKey, Asc);
TargV.SortCmp(HashCmp);
// now sort the update vector
THashKeyDat<TKey, TDat> Tmp;
for (int i = 0; i < TargV.Len()-1; i++) {
const int SrcPos = MapV[TargV[i]];
const int Loc = i;
// swap data
Tmp = KeyDatV[SrcPos];
KeyDatV[SrcPos] = KeyDatV[Loc];
KeyDatV[Loc] = Tmp;
// swap keys
MapV[StateV[i]] = SrcPos;
StateV.Swap(Loc, SrcPos);
}
for (int i = 0; i < TargV.Len(); i++) {
MapV[TargV[i]] = i; }
for (int p = 0; p < PortV.Len(); p++) {
if (PortV[p] != -1) {
PortV[p] = MapV[PortV[p]]; } }
for (int i = 0; i < KeyDatV.Len(); i++) {
if (KeyDatV[i].Next != -1) {
KeyDatV[i].Next = MapV[KeyDatV[i].Next]; }
}
}
TInt THash< TKey, TDat, THashFunc >::FFreeKeyId [private] |
const unsigned int THash< TKey, TDat, THashFunc >::HashPrimeT [static] |
{
3ul, 5ul, 11ul, 23ul,
53ul, 97ul, 193ul, 389ul, 769ul,
1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
1610612741ul, 3221225473ul, 4294967291ul
}