|
SNAP Library 2.0, Developer Reference
2013-05-13 16:33:57
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
|
#include <shash.h>

Public Types | |
| typedef TSHashKeyDat< TKey, TDat > | THashKeyDat |
| typedef TSparseTable < THashKeyDat, GroupSize > ::TIter | TIter |
| typedef TSparseTable < THashKeyDat, GroupSize > ::TSGroup | TSGroup |
Public Member Functions | |
| TSparseHash (const int &WantedVals=0) | |
| TSparseHash (TSIn &SIn) | |
| void | Load (TSIn &SIn) |
| void | Save (TSOut &SOut) const |
| TSparseHash & | operator= (const TSparseHash &SHT) |
| bool | operator== (const TSparseHash &SHT) const |
| bool | operator< (const TSparseHash &SHT) const |
| ::TSize | GetMemUsed () const |
| TIter | BegI () const |
| TIter | EndI () const |
| TIter | GetI (const TKey &Key) const |
| bool | Empty () const |
| int | Len () const |
| int | Reserved () const |
| uint | GetDiskSz () const |
| void | Reserve (const int &MxVals) |
| void | Clr (const bool &DoDel=true) |
| void | Swap (TSparseHash &HT) |
| int | AddKey (const TKey &Key) |
| TDat & | AddDat (const TKey &Key) |
| TDat & | AddDat (const TKey &Key, const TDat &Dat) |
| const TKey & | GetKey (const int &KeyId) const |
| int | GetKeyId (const TKey &Key) const |
| bool | IsKey (const TKey &Key) const |
| bool | IsKey (const TKey &Key, int &KeyId) const |
| bool | IsKeyId (const int &KeyId) const |
| int | GetRndKeyId (TRnd &Rnd=TInt::Rnd) const |
| const TDat & | GetDat (const TKey &Key) const |
| TDat & | GetDat (const TKey &Key) |
| const TDat & | GetDatKeyId (const int &KeyId) const |
| TDat & | GetDatKeyId (const int &KeyId) |
| void | GetKeyDat (const int &KeyId, TKey &Key, TDat &Dat) const |
| bool | IsKeyGetDat (const TKey &Key, TDat &Dat) 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 |
Static Public Attributes | |
| static const float | MxOccupancy = 0.8f |
| static const float | MnOccupancy = 0.4f * 0.8f |
| static const int | MinBuckets = 32 |
Private Member Functions | |
| void | ResetThresh () |
| int | GetMinSize (const int &CurVals, const int &WantedVals) const |
| void | CopyFrom (const TSparseHash &HT, const int &MnWanted) |
| void | MoveFrom (TSparseHash &HT, const int &MnWanted) |
| void | ResizeDelta (const int &ValsToAdd, const int &MnWanted=0) |
| void | FindPos (const TKey &Key, int &Pos, int &PosToIns) const |
Private Attributes | |
| TInt | ShrinkThresh |
| TInt | ExpandThresh |
| TSparseTable< THashKeyDat, GroupSize > | Table |
| typedef TSHashKeyDat<TKey, TDat> TSparseHash< TKey, TDat, GroupSize >::THashKeyDat |
| typedef TSparseTable<THashKeyDat, GroupSize>::TIter TSparseHash< TKey, TDat, GroupSize >::TIter |
| typedef TSparseTable<THashKeyDat, GroupSize>::TSGroup TSparseHash< TKey, TDat, GroupSize >::TSGroup |
| TSparseHash< TKey, TDat, GroupSize >::TSparseHash | ( | const int & | WantedVals = 0 | ) | [inline] |
Definition at line 494 of file shash.h.
References TSparseHash< TKey, TDat, GroupSize >::ResetThresh().
: Table(GetMinSize(0, WantedVals)) { ResetThresh(); }

| TSparseHash< TKey, TDat, GroupSize >::TSparseHash | ( | TSIn & | SIn | ) | [inline] |
Definition at line 495 of file shash.h.
: ShrinkThresh(SIn), ExpandThresh(SIn), Table(SIn) { }
| TDat & TSparseHash< TKey, TDat, GroupSize >::AddDat | ( | const TKey & | Key | ) |
Definition at line 687 of file shash.h.
Referenced by TBigNet< TNodeData, IsDir >::GetSubGraph().
{
ResizeDelta(1);
int Pos, PosToIns; FindPos(Key, Pos, PosToIns);
if (PosToIns != -1) {
return Table.Set(PosToIns, THashKeyDat(Key)).Dat;
} else { return Table.Set(Pos).Dat; }
}

| TDat & TSparseHash< TKey, TDat, GroupSize >::AddDat | ( | const TKey & | Key, |
| const TDat & | Dat | ||
| ) |
| int TSparseHash< TKey, TDat, GroupSize >::AddKey | ( | const TKey & | Key | ) |
Definition at line 676 of file shash.h.
{
ResizeDelta(1);
int Pos, PosToIns; FindPos(Key, Pos, PosToIns);
if (Pos != -1) { return Pos; } // key exists
else {
Table.Set(PosToIns, THashKeyDat(Key));
return PosToIns;
}
}
| TIter TSparseHash< TKey, TDat, GroupSize >::BegI | ( | ) | const [inline] |
Definition at line 504 of file shash.h.
References TSparseTable< TVal, GroupSize >::BegI(), and TSparseHash< TKey, TDat, GroupSize >::Table.

| void TSparseHash< TKey, TDat, GroupSize >::Clr | ( | const bool & | DoDel = true | ) | [inline] |
Definition at line 514 of file shash.h.
References TSparseTable< TVal, GroupSize >::Clr(), TSparseHash< TKey, TDat, GroupSize >::ResetThresh(), and TSparseHash< TKey, TDat, GroupSize >::Table.
{ Table.Clr(DoDel); ResetThresh(); }

| void TSparseHash< TKey, TDat, GroupSize >::CopyFrom | ( | const TSparseHash< TKey, TDat, GroupSize > & | HT, |
| const int & | MnWanted | ||
| ) | [private] |
Definition at line 568 of file shash.h.
References Assert, TSparseTable< TVal, GroupSize >::GetGroup(), TSparseTable< TVal, GroupSize >::Groups(), TSparseHash< TKey, TDat, GroupSize >::Reserved(), and TSparseHash< TKey, TDat, GroupSize >::Table.
{
Clr(false);
const int NewSize = GetMinSize(HT.Reserved(), MnWanted);
if (NewSize > Reserved()) {
Table.Resize(NewSize);
ResetThresh();
}
const uint BuckM1 = Reserved() - 1;
for (int g = 0; g < HT.Table.Groups(); g++) {
const TSGroup& Group = HT.Table.GetGroup(g);
for (int b = 0; b < Group.Len(); b++) {
int Tries = 0; uint BuckNum;
for (BuckNum = Group.Offset(b).Hash() & BuckM1;
! Table.IsEmpty(BuckNum); BuckNum = (BuckNum + Tries) & BuckM1) {
Tries++;
Assert(Tries < Reserved());
}
Table.Set(BuckNum, Group.Offset(b));
}
}
}

| bool TSparseHash< TKey, TDat, GroupSize >::Empty | ( | ) | const [inline] |
Definition at line 508 of file shash.h.
References TSparseHash< TKey, TDat, GroupSize >::Len().
{ return Len() == 0; }

| TIter TSparseHash< TKey, TDat, GroupSize >::EndI | ( | ) | const [inline] |
Definition at line 505 of file shash.h.
References TSparseTable< TVal, GroupSize >::EndI(), and TSparseHash< TKey, TDat, GroupSize >::Table.

| void TSparseHash< TKey, TDat, GroupSize >::FindPos | ( | const TKey & | Key, |
| int & | Pos, | ||
| int & | PosToIns | ||
| ) | const [private] |
Definition at line 631 of file shash.h.
References Assert.
Referenced by TSparseHash< TKey, TDat, GroupSize >::GetKeyId().
{
const uint BuckM1 = Reserved() - 1;
uint BuckNum = Key.GetPrimHashCd() & BuckM1;
int Tries = 0;
while (true) {
if (Table.IsEmpty(BuckNum)) {
Pos = -1; PosToIns = BuckNum; return;
}
else if (Key == Table.Get(BuckNum).Key) {
Pos = BuckNum; PosToIns = -1; return;
}
Tries++;
BuckNum = (BuckNum + Tries) & BuckM1;
Assert(Tries < Reserved());
}
}

| const TDat & TSparseHash< TKey, TDat, GroupSize >::GetDat | ( | const TKey & | Key | ) | const |
| TDat & TSparseHash< TKey, TDat, GroupSize >::GetDat | ( | const TKey & | Key | ) |
| const TDat& TSparseHash< TKey, TDat, GroupSize >::GetDatKeyId | ( | const int & | KeyId | ) | const [inline] |
Definition at line 534 of file shash.h.
References TSHashKeyDat< TKey, TDat >::Dat, TSparseTable< TVal, GroupSize >::Get(), and TSparseHash< TKey, TDat, GroupSize >::Table.

| TDat& TSparseHash< TKey, TDat, GroupSize >::GetDatKeyId | ( | const int & | KeyId | ) | [inline] |
Definition at line 535 of file shash.h.
References TSHashKeyDat< TKey, TDat >::Dat, TSparseTable< TVal, GroupSize >::Set(), and TSparseHash< TKey, TDat, GroupSize >::Table.

| void TSparseHash< TKey, TDat, GroupSize >::GetDatKeyPrV | ( | TVec< TPair< TDat, TKey > > & | DatKeyPrV | ) | const |
| void TSparseHash< TKey, TDat, GroupSize >::GetDatV | ( | TVec< TDat > & | DatV | ) | const |
Definition at line 746 of file shash.h.
References TVec< TVal, TSizeTy >::Add(), and TVec< TVal, TSizeTy >::Gen().

| uint TSparseHash< TKey, TDat, GroupSize >::GetDiskSz | ( | ) | const [inline] |
Definition at line 511 of file shash.h.
References TSparseTable< TVal, GroupSize >::GetDiskSz(), and TSparseHash< TKey, TDat, GroupSize >::Table.

| TIter TSparseHash< TKey, TDat, GroupSize >::GetI | ( | const TKey & | Key | ) | const [inline] |
Definition at line 506 of file shash.h.
References Assert, TSparseTable< TVal, GroupSize >::GetI(), TSparseHash< TKey, TDat, GroupSize >::GetKeyId(), TSparseHash< TKey, TDat, GroupSize >::IsKey(), and TSparseHash< TKey, TDat, GroupSize >::Table.

| const TKey& TSparseHash< TKey, TDat, GroupSize >::GetKey | ( | const int & | KeyId | ) | const [inline] |
Definition at line 521 of file shash.h.
References TSparseTable< TVal, GroupSize >::Get(), TSHashKeyDat< TKey, TDat >::Key, and TSparseHash< TKey, TDat, GroupSize >::Table.

| void TSparseHash< TKey, TDat, GroupSize >::GetKeyDat | ( | const int & | KeyId, |
| TKey & | Key, | ||
| TDat & | Dat | ||
| ) | const |
Definition at line 721 of file shash.h.
References Assert, TSHashKeyDat< TKey, TDat >::Dat, and TSHashKeyDat< TKey, TDat >::Key.
| void TSparseHash< TKey, TDat, GroupSize >::GetKeyDatPrV | ( | TVec< TPair< TKey, TDat > > & | KeyDatPrV | ) | const |
| int TSparseHash< TKey, TDat, GroupSize >::GetKeyId | ( | const TKey & | Key | ) | const [inline] |
Definition at line 522 of file shash.h.
References TSparseHash< TKey, TDat, GroupSize >::FindPos().
Referenced by TSparseHash< TKey, TDat, GroupSize >::GetI(), and TSparseHash< TKey, TDat, GroupSize >::IsKey().
{
int Pos, PosToIns; FindPos(Key, Pos, PosToIns); return Pos; }


| void TSparseHash< TKey, TDat, GroupSize >::GetKeyV | ( | TVec< TKey > & | KeyV | ) | const |
Definition at line 738 of file shash.h.
References TVec< TVal, TSizeTy >::Add(), and TVec< TVal, TSizeTy >::Gen().

| ::TSize TSparseHash< TKey, TDat, GroupSize >::GetMemUsed | ( | ) | const [inline] |
Definition at line 502 of file shash.h.
References TSparseTable< TVal, GroupSize >::GetMemUsed(), and TSparseHash< TKey, TDat, GroupSize >::Table.
{ return 2*sizeof(TInt)+Table.GetMemUsed(); }

| int TSparseHash< TKey, TDat, GroupSize >::GetMinSize | ( | const int & | CurVals, |
| const int & | WantedVals | ||
| ) | const [private] |
Definition at line 561 of file shash.h.
{
int Size = MinBuckets;
while (Size*MxOccupancy < WantedVals || CurVals >= Size * MxOccupancy) Size *= 2;
return Size;
}
| int TSparseHash< TKey, TDat, GroupSize >::GetRndKeyId | ( | TRnd & | Rnd = TInt::Rnd | ) | const [inline] |
Definition at line 528 of file shash.h.
References Assert, TSparseHash< TKey, TDat, GroupSize >::IsKeyId(), TSparseHash< TKey, TDat, GroupSize >::Len(), and TSparseHash< TKey, TDat, GroupSize >::Reserved().
{ Assert(Len()>0);
int KeyId = Rnd.GetUniDevInt(Reserved());
while (! IsKeyId(KeyId)) { KeyId = Rnd.GetUniDevInt(Reserved()); } return KeyId; }

| bool TSparseHash< TKey, TDat, GroupSize >::IsKey | ( | const TKey & | Key | ) | const [inline] |
Definition at line 524 of file shash.h.
References TSparseHash< TKey, TDat, GroupSize >::GetKeyId().
Referenced by TSparseHash< TKey, TDat, GroupSize >::GetI().
{ return GetKeyId(Key) != -1; }


| bool TSparseHash< TKey, TDat, GroupSize >::IsKey | ( | const TKey & | Key, |
| int & | KeyId | ||
| ) | const [inline] |
Definition at line 525 of file shash.h.
References TSparseHash< TKey, TDat, GroupSize >::GetKeyId().
{
KeyId = GetKeyId(Key); return KeyId != -1; }

| bool TSparseHash< TKey, TDat, GroupSize >::IsKeyGetDat | ( | const TKey & | Key, |
| TDat & | Dat | ||
| ) | const |
| bool TSparseHash< TKey, TDat, GroupSize >::IsKeyId | ( | const int & | KeyId | ) | const [inline] |
Definition at line 527 of file shash.h.
References TSparseTable< TVal, GroupSize >::IsEmpty(), and TSparseHash< TKey, TDat, GroupSize >::Table.
Referenced by TSparseHash< TKey, TDat, GroupSize >::GetRndKeyId().


| int TSparseHash< TKey, TDat, GroupSize >::Len | ( | ) | const [inline] |
Definition at line 509 of file shash.h.
References TSparseTable< TVal, GroupSize >::Len(), and TSparseHash< TKey, TDat, GroupSize >::Table.
Referenced by TSparseHash< TKey, TDat, GroupSize >::Empty(), TSparseHash< TKey, TDat, GroupSize >::GetRndKeyId(), and TSparseHash< TKey, TDat, GroupSize >::Reserve().


| void TSparseHash< TKey, TDat, GroupSize >::Load | ( | TSIn & | SIn | ) | [inline] |
Definition at line 496 of file shash.h.
References TSparseHash< TKey, TDat, GroupSize >::ExpandThresh, TSparseTable< TVal, GroupSize >::Load(), TInt::Load(), TSparseHash< TKey, TDat, GroupSize >::ShrinkThresh, and TSparseHash< TKey, TDat, GroupSize >::Table.
{ ShrinkThresh.Load(SIn); ExpandThresh.Load(SIn); Table.Load(SIn); }

| void TSparseHash< TKey, TDat, GroupSize >::MoveFrom | ( | TSparseHash< TKey, TDat, GroupSize > & | HT, |
| const int & | MnWanted | ||
| ) | [private] |
Definition at line 591 of file shash.h.
References Assert, TSparseTable< TVal, GroupSize >::GetGroup(), TSparseTable< TVal, GroupSize >::Groups(), TSparseHash< TKey, TDat, GroupSize >::Reserved(), and TSparseHash< TKey, TDat, GroupSize >::Table.
Referenced by TSparseHash< TKey, TDat, GroupSize >::ResizeDelta().
{
Clr(false);
int NewSize;
if (MnWanted == 0) NewSize = HT.Reserved();
else NewSize = GetMinSize(HT.Reserved(), MnWanted);
if (NewSize > Reserved()) {
Table.Resize(NewSize);
ResetThresh();
}
const uint BuckM1 = Reserved() - 1;
for (int g = 0; g < HT.Table.Groups(); g++) {
TSGroup& Group = HT.Table.GetGroup(g);
for (int b = 0; b < Group.Len(); b++) {
int Tries = 0; uint BuckNum;
for (BuckNum = Group.Offset(b).Hash() & BuckM1;
! Table.IsEmpty(BuckNum); BuckNum = (BuckNum + Tries) & BuckM1) {
Tries++;
Assert(Tries < Reserved());
}
Assert(Table.IsEmpty(BuckNum));
Table.Set(BuckNum, Group.Offset(b));
}
Group.Clr(true); // delete
}
}


| bool TSparseHash< TKey, TDat, GroupSize >::operator< | ( | const TSparseHash< TKey, TDat, GroupSize > & | SHT | ) | const |
Definition at line 664 of file shash.h.
References TSparseHash< TKey, TDat, GroupSize >::Table.
| TSparseHash< TKey, TDat, GroupSize > & TSparseHash< TKey, TDat, GroupSize >::operator= | ( | const TSparseHash< TKey, TDat, GroupSize > & | SHT | ) |
Definition at line 649 of file shash.h.
References TSparseHash< TKey, TDat, GroupSize >::ExpandThresh, TSparseHash< TKey, TDat, GroupSize >::ShrinkThresh, and TSparseHash< TKey, TDat, GroupSize >::Table.
{
if (this != &SHT) {
ShrinkThresh = SHT.ShrinkThresh;
ExpandThresh = SHT.ExpandThresh;
Table = SHT.Table;
}
return *this;
}
| bool TSparseHash< TKey, TDat, GroupSize >::operator== | ( | const TSparseHash< TKey, TDat, GroupSize > & | SHT | ) | const |
Definition at line 659 of file shash.h.
References TSparseHash< TKey, TDat, GroupSize >::Table.
| void TSparseHash< TKey, TDat, GroupSize >::Reserve | ( | const int & | MxVals | ) | [inline] |
Definition at line 513 of file shash.h.
References TSparseHash< TKey, TDat, GroupSize >::Len(), and TSparseHash< TKey, TDat, GroupSize >::ResizeDelta().
{ if (MxVals > Len()) ResizeDelta(MxVals - Len(), 0); }

| int TSparseHash< TKey, TDat, GroupSize >::Reserved | ( | ) | const [inline] |
Definition at line 510 of file shash.h.
References TSparseTable< TVal, GroupSize >::Reserved(), and TSparseHash< TKey, TDat, GroupSize >::Table.
Referenced by TSparseHash< TKey, TDat, GroupSize >::CopyFrom(), TSparseHash< TKey, TDat, GroupSize >::GetRndKeyId(), and TSparseHash< TKey, TDat, GroupSize >::MoveFrom().


| void TSparseHash< TKey, TDat, GroupSize >::ResetThresh | ( | ) | [private] |
Definition at line 555 of file shash.h.
Referenced by TSparseHash< TKey, TDat, GroupSize >::Clr(), TSparseHash< TKey, TDat, GroupSize >::ResizeDelta(), and TSparseHash< TKey, TDat, GroupSize >::TSparseHash().
{
ExpandThresh = int(Table.Reserved() * MxOccupancy);
ShrinkThresh = int(Table.Reserved() * MnOccupancy);
}

| void TSparseHash< TKey, TDat, GroupSize >::ResizeDelta | ( | const int & | ValsToAdd, |
| const int & | MnWanted = 0 |
||
| ) | [private] |
Definition at line 618 of file shash.h.
References TSparseHash< TKey, TDat, GroupSize >::MoveFrom(), TSparseHash< TKey, TDat, GroupSize >::ResetThresh(), and Swap().
Referenced by TSparseHash< TKey, TDat, GroupSize >::Reserve().
{
if (Reserved() > MnWanted && Len()+ValsToAdd < ExpandThresh) { return; }
const int NewSize = GetMinSize(Table.Len()+ValsToAdd, MnWanted);
if (NewSize > Reserved()) {
printf("***Resize SparseHash:%d->%d\n", Reserved(), NewSize);
TSparseHash TmpHt(ValsToAdd+Len());
TmpHt.ResetThresh();
TmpHt.MoveFrom(*this, Len());
Swap(TmpHt);
}
}


| void TSparseHash< TKey, TDat, GroupSize >::Save | ( | TSOut & | SOut | ) | const [inline] |
Definition at line 497 of file shash.h.
References TSparseHash< TKey, TDat, GroupSize >::ExpandThresh, TSparseTable< TVal, GroupSize >::Save(), TInt::Save(), TSparseHash< TKey, TDat, GroupSize >::ShrinkThresh, and TSparseHash< TKey, TDat, GroupSize >::Table.
{ ShrinkThresh.Save(SOut); ExpandThresh.Save(SOut); Table.Save(SOut); }

| void TSparseHash< TKey, TDat, GroupSize >::Swap | ( | TSparseHash< TKey, TDat, GroupSize > & | HT | ) |
Definition at line 669 of file shash.h.
References TSparseHash< TKey, TDat, GroupSize >::ExpandThresh, TSparseHash< TKey, TDat, GroupSize >::ShrinkThresh, Swap(), and TSparseHash< TKey, TDat, GroupSize >::Table.
{
::Swap(ShrinkThresh, HT.ShrinkThresh);
::Swap(ExpandThresh, HT.ExpandThresh);
Table.Swap(HT.Table);
}

TInt TSparseHash< TKey, TDat, GroupSize >::ExpandThresh [private] |
Definition at line 491 of file shash.h.
Referenced by TSparseHash< TKey, TDat, GroupSize >::Load(), TSparseHash< TKey, TDat, GroupSize >::operator=(), TSparseHash< TKey, TDat, GroupSize >::Save(), and TSparseHash< TKey, TDat, GroupSize >::Swap().
const int TSparseHash< TKey, TDat, GroupSize >::MinBuckets = 32 [static] |
const float TSparseHash< TKey, TDat, GroupSize >::MnOccupancy = 0.4f * 0.8f [static] |
const float TSparseHash< TKey, TDat, GroupSize >::MxOccupancy = 0.8f [static] |
TInt TSparseHash< TKey, TDat, GroupSize >::ShrinkThresh [private] |
Definition at line 491 of file shash.h.
Referenced by TSparseHash< TKey, TDat, GroupSize >::Load(), TSparseHash< TKey, TDat, GroupSize >::operator=(), TSparseHash< TKey, TDat, GroupSize >::Save(), and TSparseHash< TKey, TDat, GroupSize >::Swap().
TSparseTable<THashKeyDat, GroupSize> TSparseHash< TKey, TDat, GroupSize >::Table [private] |
Definition at line 492 of file shash.h.
Referenced by TSparseHash< TKey, TDat, GroupSize >::BegI(), TSparseHash< TKey, TDat, GroupSize >::Clr(), TSparseHash< TKey, TDat, GroupSize >::CopyFrom(), TSparseHash< TKey, TDat, GroupSize >::EndI(), TSparseHash< TKey, TDat, GroupSize >::GetDatKeyId(), TSparseHash< TKey, TDat, GroupSize >::GetDiskSz(), TSparseHash< TKey, TDat, GroupSize >::GetI(), TSparseHash< TKey, TDat, GroupSize >::GetKey(), TSparseHash< TKey, TDat, GroupSize >::GetMemUsed(), TSparseHash< TKey, TDat, GroupSize >::IsKeyId(), TSparseHash< TKey, TDat, GroupSize >::Len(), TSparseHash< TKey, TDat, GroupSize >::Load(), TSparseHash< TKey, TDat, GroupSize >::MoveFrom(), TSparseHash< TKey, TDat, GroupSize >::operator<(), TSparseHash< TKey, TDat, GroupSize >::operator=(), TSparseHash< TKey, TDat, GroupSize >::operator==(), TSparseHash< TKey, TDat, GroupSize >::Reserved(), TSparseHash< TKey, TDat, GroupSize >::Save(), and TSparseHash< TKey, TDat, GroupSize >::Swap().