SNAP Library 2.2, Developer Reference  2014-03-11 19:15:55
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
hash.cpp
Go to the documentation of this file.
00001 
00002 // Big-String-Pool
00003 void TBigStrPool::Resize(TSize _MxBfL) {
00004   TSize newSize = MxBfL;
00005   while (newSize < _MxBfL) {
00006     if (newSize >= GrowBy && GrowBy > 0) newSize += GrowBy;
00007     else if (newSize > 0) newSize *= 2;
00008     else newSize = TInt::GetMn(GrowBy, 1024);
00009   }
00010   if (newSize > MxBfL) {
00011     Bf = (char *) realloc(Bf, newSize);
00012     IAssertR(Bf, TStr::Fmt("old Bf size: %u, new size: %u", MxBfL, newSize).CStr());
00013     MxBfL = newSize;
00014   }
00015   IAssert(MxBfL >= _MxBfL);
00016 }
00017 
00018 TBigStrPool::TBigStrPool(TSize MxBfLen, uint _GrowBy) : MxBfL(MxBfLen), BfL(0), GrowBy(_GrowBy), Bf(0) {
00019   //IAssert(MxBfL >= 0); IAssert(GrowBy >= 0);
00020   if (MxBfL > 0) { Bf = (char *) malloc(MxBfL);  IAssert(Bf); }
00021   AddStr(""); // add empty string
00022 }
00023 
00024 TBigStrPool::TBigStrPool(TSIn& SIn, bool LoadCompact) : MxBfL(0), BfL(0), GrowBy(0), Bf(0) {
00025   uint64 Tmp;
00026   SIn.Load(Tmp); IAssert(Tmp <= uint64(TSizeMx)); MxBfL=TSize(Tmp);
00027   SIn.Load(Tmp); IAssert(Tmp <= uint64(TSizeMx)); BfL=TSize(Tmp);
00028   SIn.Load(GrowBy);
00029   IAssert(MxBfL >= BfL);  IAssert(BfL >= 0);  IAssert(GrowBy >= 0);
00030   if (LoadCompact) MxBfL = BfL;
00031   if (MxBfL > 0) { Bf = (char *) malloc(MxBfL); IAssert(Bf); }
00032   if (BfL > 0) { SIn.LoadBf(Bf, BfL); }
00033   SIn.LoadCs();
00034   int NStr=0;  SIn.Load(NStr);
00035   IdOffV.Gen(NStr, 0);
00036   for (int i = 0; i < NStr; i++) {
00037     SIn.Load(Tmp);
00038     IAssert(Tmp <= uint64(TSizeMx));
00039     IdOffV.Add(TSize(Tmp));
00040   }
00041 }
00042 
00043 void TBigStrPool::Save(TSOut& SOut) const {
00044   SOut.Save(uint64(MxBfL));  SOut.Save(uint64(BfL));  SOut.Save(GrowBy);
00045   if (BfL > 0) { SOut.SaveBf(Bf, BfL); }
00046   SOut.SaveCs();
00047   SOut.Save(IdOffV.Len());
00048   for (int i = 0; i < IdOffV.Len(); i++) {
00049     SOut.Save(uint64(IdOffV[i])); 
00050   }
00051 }
00052 
00053 TBigStrPool& TBigStrPool::operator = (const TBigStrPool& Pool) {
00054   if (this != &Pool) {
00055     GrowBy = Pool.GrowBy;  MxBfL = Pool.MxBfL;  BfL = Pool.BfL;
00056     if (Bf) free(Bf); else IAssert(MxBfL == 0);
00057     Bf = (char *) malloc(MxBfL);  IAssert(Bf);  memcpy(Bf, Pool.Bf, BfL);
00058   }
00059   return *this;
00060 }
00061 
00062 // Adds Len characters to pool. To append a null terminated string Len must be equal to strlen(s) + 1
00063 int TBigStrPool::AddStr(const char *Str, uint Len) {
00064   IAssertR(Len > 0, "String too short (lenght includes the null character)");  //J: if (! Len) return -1;
00065   Assert(Str);  Assert(Len > 0);
00066   if (Len == 1 && IdOffV.Len() > 0) { return 0; } // empty string
00067   if (BfL + Len > MxBfL) { Resize(BfL + Len); }
00068   memcpy(Bf + BfL, Str, Len);
00069   TSize Pos = BfL;  BfL += Len;  
00070   IdOffV.Add(Pos);
00071   return IdOffV.Len()-1;
00072 }
00073 
00074 int TBigStrPool::GetPrimHashCd(const char *CStr) {
00075   return TStrHashF_DJB::GetPrimHashCd(CStr);
00076 }
00077 
00078 int TBigStrPool::GetSecHashCd(const char *CStr) {
00079   return TStrHashF_DJB::GetSecHashCd(CStr);
00080 }
00081 
00083 // String-Hash-Functions
00084 
00085 // Md5-Hash-Function
00086 int TStrHashF_Md5::GetPrimHashCd(const char *p) {
00087   TMd5Sig sig = TStr(p); 
00088   return sig.GetPrimHashCd(); 
00089 }
00090 
00091 int TStrHashF_Md5::GetSecHashCd(const char *p) {
00092   TMd5Sig sig = TStr(p);
00093   return sig.GetSecHashCd(); 
00094 }
00095 
00096 int TStrHashF_Md5::GetPrimHashCd(const TStr& s) {
00097   TMd5Sig sig(s);
00098   return sig.GetPrimHashCd(); 
00099 }
00100 
00101 int TStrHashF_Md5::GetSecHashCd(const TStr& s) {
00102   TMd5Sig sig(s);
00103   return sig.GetSecHashCd();
00104 }
00105