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.cpp
Go to the documentation of this file.
1 // Big-String-Pool
3 void TBigStrPool::Resize(TSize _MxBfL) {
4  TSize newSize = MxBfL;
5  while (newSize < _MxBfL) {
6  if (newSize >= GrowBy && GrowBy > 0) newSize += GrowBy;
7  else if (newSize > 0) newSize *= 2;
8  else newSize = TInt::GetMn(GrowBy, 1024);
9  }
10  if (newSize > MxBfL) {
11  Bf = (char *) realloc(Bf, newSize);
12  IAssertR(Bf, TStr::Fmt("old Bf size: %u, new size: %u", MxBfL, newSize).CStr());
13  MxBfL = newSize;
14  }
15  IAssert(MxBfL >= _MxBfL);
16 }
17 
18 TBigStrPool::TBigStrPool(TSize MxBfLen, uint _GrowBy) : MxBfL(MxBfLen), BfL(0), GrowBy(_GrowBy), Bf(0) {
19  //IAssert(MxBfL >= 0); IAssert(GrowBy >= 0);
20  if (MxBfL > 0) { Bf = (char *) malloc(MxBfL); IAssert(Bf); }
21  AddStr(""); // add empty string
22 }
23 
24 TBigStrPool::TBigStrPool(TSIn& SIn, bool LoadCompact) : MxBfL(0), BfL(0), GrowBy(0), Bf(0) {
25  uint64 Tmp;
26  SIn.Load(Tmp); IAssert(Tmp <= uint64(TSizeMx)); MxBfL=TSize(Tmp);
27  SIn.Load(Tmp); IAssert(Tmp <= uint64(TSizeMx)); BfL=TSize(Tmp);
28  SIn.Load(GrowBy);
29  IAssert(MxBfL >= BfL); IAssert(BfL >= 0); IAssert(GrowBy >= 0);
30  if (LoadCompact) MxBfL = BfL;
31  if (MxBfL > 0) { Bf = (char *) malloc(MxBfL); IAssert(Bf); }
32  if (BfL > 0) { SIn.LoadBf(Bf, BfL); }
33  SIn.LoadCs();
34  int NStr=0; SIn.Load(NStr);
35  IdOffV.Gen(NStr, 0);
36  for (int i = 0; i < NStr; i++) {
37  SIn.Load(Tmp);
38  IAssert(Tmp <= uint64(TSizeMx));
39  IdOffV.Add(TSize(Tmp));
40  }
41 }
42 
43 void TBigStrPool::Save(TSOut& SOut) const {
44  SOut.Save(uint64(MxBfL)); SOut.Save(uint64(BfL)); SOut.Save(GrowBy);
45  if (BfL > 0) { SOut.SaveBf(Bf, BfL); }
46  SOut.SaveCs();
47  SOut.Save(IdOffV.Len());
48  for (int i = 0; i < IdOffV.Len(); i++) {
49  SOut.Save(uint64(IdOffV[i]));
50  }
51 }
52 
54  if (this != &Pool) {
55  GrowBy = Pool.GrowBy; MxBfL = Pool.MxBfL; BfL = Pool.BfL;
56  if (Bf) free(Bf); else IAssert(MxBfL == 0);
57  Bf = (char *) malloc(MxBfL); IAssert(Bf); memcpy(Bf, Pool.Bf, BfL);
58  }
59  return *this;
60 }
61 
62 // Adds Len characters to pool. To append a null terminated string Len must be equal to strlen(s) + 1
63 int TBigStrPool::AddStr(const char *Str, uint Len) {
64  IAssertR(Len > 0, "String too short (lenght includes the null character)"); //J: if (! Len) return -1;
65  Assert(Str); Assert(Len > 0);
66  if (Len == 1 && IdOffV.Len() > 0) { return 0; } // empty string
67  if (BfL + Len > MxBfL) { Resize(BfL + Len); }
68  memcpy(Bf + BfL, Str, Len);
69  TSize Pos = BfL; BfL += Len;
70  IdOffV.Add(Pos);
71  return IdOffV.Len()-1;
72 }
73 
74 int TBigStrPool::GetPrimHashCd(const char *CStr) {
75  return TStrHashF_DJB::GetPrimHashCd(CStr);
76 }
77 
78 int TBigStrPool::GetSecHashCd(const char *CStr) {
79  return TStrHashF_DJB::GetSecHashCd(CStr);
80 }
81 
83 // String-Hash-Functions
84 
85 // Md5-Hash-Function
86 int TStrHashF_Md5::GetPrimHashCd(const char *p) {
87  TMd5Sig sig = TStr(p);
88  return sig.GetPrimHashCd();
89 }
90 
91 int TStrHashF_Md5::GetSecHashCd(const char *p) {
92  TMd5Sig sig = TStr(p);
93  return sig.GetSecHashCd();
94 }
95 
97  TMd5Sig sig(s);
98  return sig.GetPrimHashCd();
99 }
100 
102  TMd5Sig sig(s);
103  return sig.GetSecHashCd();
104 }
105 
#define IAssert(Cond)
Definition: bd.h:262
#define IAssertR(Cond, Reason)
Definition: bd.h:265
TVec< TSize > IdOffV
Definition: hash.h:673
TBigStrPool(TSize MxBfLen=0, uint _GrowBy=16 *1024 *1024)
Definition: hash.cpp:18
uint GrowBy
Definition: hash.h:671
unsigned int uint
Definition: bd.h:11
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
int AddStr(const char *Str, uint Len)
Definition: hash.cpp:63
TSize Len() const
Definition: hash.h:691
void Save(TSOut &SOut) const
Definition: hash.cpp:43
TSize MxBfL
Definition: hash.h:670
static int GetPrimHashCd(const char *p)
Definition: hash.cpp:86
static int GetSecHashCd(const char *p)
Definition: hash.h:1184
void LoadCs()
Definition: fl.cpp:28
Definition: fl.h:58
void SaveCs()
Definition: fl.h:171
static int GetMn(const int &Int1, const int &Int2)
Definition: dt.h:1090
unsigned long long uint64
Definition: bd.h:38
void Load(bool &Bool)
Definition: fl.h:84
#define TSizeMx
Definition: bd.h:59
size_t TSize
Definition: bd.h:58
#define Assert(Cond)
Definition: bd.h:251
TBigStrPool & operator=(const TBigStrPool &Pool)
Definition: hash.cpp:53
void SaveBf(const void *Bf, const TSize &BfL)
Definition: fl.h:172
Definition: fl.h:128
static int GetSecHashCd(const char *CStr)
Definition: hash.cpp:78
void Save(const bool &Bool)
Definition: fl.h:173
Definition: dt.h:412
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
void LoadBf(const void *Bf, const TSize &BfL)
Definition: fl.h:81
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:495
static int GetPrimHashCd(const char *CStr)
Definition: hash.cpp:74
Definition: md5.h:81
TSize BfL
Definition: hash.h:670
char * Bf
Definition: hash.h:672
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int GetSecHashCd() const
Definition: md5.cpp:271
int GetPrimHashCd() const
Definition: md5.cpp:264
static int GetSecHashCd(const char *p)
Definition: hash.cpp:91
void Resize(TSize _MxBfL)
Definition: hash.cpp:3
static int GetPrimHashCd(const char *p)
Definition: hash.h:1181