SNAP Library 6.0, Developer Reference  2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
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  if (IsShM) {
12  Bf = (char*) malloc(newSize);
13  IsShM = false;
14  } else {
15  Bf = (char *) realloc(Bf, newSize);
16  }
17  IAssertR(Bf, TStr::Fmt("old Bf size: %u, new size: %u", MxBfL, newSize).CStr());
18  MxBfL = newSize;
19  }
20  IAssert(MxBfL >= _MxBfL);
21 }
22 
23 TBigStrPool::TBigStrPool(TSize MxBfLen, uint _GrowBy) : MxBfL(MxBfLen), BfL(0), GrowBy(_GrowBy), Bf(0), IsShM(false) {
24  //IAssert(MxBfL >= 0); IAssert(GrowBy >= 0);
25  if (MxBfL > 0) { Bf = (char *) malloc(MxBfL); IAssert(Bf); }
26  AddStr(""); // add empty string
27 }
28 
29 TBigStrPool::TBigStrPool(TSIn& SIn, bool LoadCompact) : MxBfL(0), BfL(0), GrowBy(0), Bf(0), IsShM(false) {
30  uint64 Tmp;
31  SIn.Load(Tmp); IAssert(Tmp <= uint64(TSizeMx)); MxBfL=TSize(Tmp);
32  SIn.Load(Tmp); IAssert(Tmp <= uint64(TSizeMx)); BfL=TSize(Tmp);
33  SIn.Load(GrowBy);
34  IAssert(MxBfL >= BfL); IAssert(BfL >= 0); IAssert(GrowBy >= 0);
35  if (LoadCompact) MxBfL = BfL;
36  if (MxBfL > 0) { Bf = (char *) malloc(MxBfL); IAssert(Bf); }
37  if (BfL > 0) { SIn.LoadBf(Bf, BfL); }
38  SIn.LoadCs();
39  int NStr=0; SIn.Load(NStr);
40  IdOffV.Gen(NStr, 0);
41  for (int i = 0; i < NStr; i++) {
42  SIn.Load(Tmp);
43  IAssert(Tmp <= uint64(TSizeMx));
44  IdOffV.Add(TSize(Tmp));
45  }
46 }
47 
48 void TBigStrPool::LoadPoolShM(TShMIn& ShMIn, bool LoadCompact) {
49  uint64 Tmp;
50  ShMIn.Load(Tmp); IAssert(Tmp <= uint64(TSizeMx)); MxBfL=TSize(Tmp);
51  ShMIn.Load(Tmp); IAssert(Tmp <= uint64(TSizeMx)); BfL=TSize(Tmp);
52  ShMIn.Load(GrowBy); IAssert(MxBfL >= BfL); IAssert(BfL >= 0); IAssert(GrowBy >= 0);
53  IsShM = true;
54  if (LoadCompact) {
55  MxBfL = BfL;
56  Bf = (char*)(ShMIn.AdvanceCursor(BfL));
57  IsShM = true;
58  } else {
59  if (MxBfL > 0) { Bf = (char *) malloc(MxBfL); IAssert(Bf); IsShM = false;}
60  if (BfL > 0) { ShMIn.LoadBf(Bf, BfL); }
61  IsShM = false;
62  }
63  ShMIn.LoadCs();
64  int NStr=0;
65  ShMIn.Load(NStr);
66  IdOffV.Gen(NStr, 0);
67  for (int i = 0; i < NStr; i++) {
68  ShMIn.Load(Tmp);
69  IAssert(Tmp <= uint64(TSizeMx));
70  IdOffV.Add(TSize(Tmp));
71  }
72 }
73 
74 
75 void TBigStrPool::Save(TSOut& SOut) const {
76  SOut.Save(uint64(MxBfL)); SOut.Save(uint64(BfL)); SOut.Save(GrowBy);
77  if (BfL > 0) { SOut.SaveBf(Bf, BfL); }
78  SOut.SaveCs();
79  SOut.Save(IdOffV.Len());
80  for (int i = 0; i < IdOffV.Len(); i++) {
81  SOut.Save(uint64(IdOffV[i]));
82  }
83 }
84 
86  if (this != &Pool) {
87  GrowBy = Pool.GrowBy; MxBfL = Pool.MxBfL; BfL = Pool.BfL;
88  if (Bf) free(Bf); else IAssert(MxBfL == 0);
89  Bf = (char *) malloc(MxBfL); IAssert(Bf); memcpy(Bf, Pool.Bf, BfL);
90  }
91  return *this;
92 }
93 
94 // Adds Len characters to pool. To append a null terminated string Len must be equal to strlen(s) + 1
95 int TBigStrPool::AddStr(const char *Str, uint Len) {
96  IAssertR(Len > 0, "String too short (lenght includes the null character)"); //J: if (! Len) return -1;
97  Assert(Str); Assert(Len > 0);
98  if (Len == 1 && IdOffV.Len() > 0) { return 0; } // empty string
99  if (BfL + Len > MxBfL) { Resize(BfL + Len); }
100  memcpy(Bf + BfL, Str, Len);
101  TSize Pos = BfL; BfL += Len;
102  IdOffV.Add(Pos);
103  return IdOffV.Len()-1;
104 }
105 
106 int TBigStrPool::GetPrimHashCd(const char *CStr) {
107  return TStrHashF_DJB::GetPrimHashCd(CStr);
108 }
109 
110 int TBigStrPool::GetSecHashCd(const char *CStr) {
111  return TStrHashF_DJB::GetSecHashCd(CStr);
112 }
113 
115 // String-Hash-Functions
116 
117 // Md5-Hash-Function
118 int TStrHashF_Md5::GetPrimHashCd(const char *p) {
119  TMd5Sig sig = TStr(p);
120  return sig.GetPrimHashCd();
121 }
122 
123 int TStrHashF_Md5::GetSecHashCd(const char *p) {
124  TMd5Sig sig = TStr(p);
125  return sig.GetSecHashCd();
126 }
127 
129  TMd5Sig sig(s);
130  return sig.GetPrimHashCd();
131 }
132 
134  TMd5Sig sig(s);
135  return sig.GetSecHashCd();
136 }
137 
#define IAssert(Cond)
Definition: bd.h:262
#define IAssertR(Cond, Reason)
Definition: bd.h:265
TVec< TSize > IdOffV
Definition: hash.h:716
TBigStrPool(TSize MxBfLen=0, uint _GrowBy=16 *1024 *1024)
Definition: hash.cpp:23
void LoadPoolShM(TShMIn &ShMIn, bool LoadCompact=true)
Definition: hash.cpp:48
bool IsShM
Definition: hash.h:717
uint GrowBy
Definition: hash.h:714
unsigned int uint
Definition: bd.h:11
char * AdvanceCursor(TSize N)
Return the current pointer and advance the cursor.
Definition: fl.h:425
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
int AddStr(const char *Str, uint Len)
Definition: hash.cpp:95
TSize Len() const
Definition: hash.h:742
void Save(TSOut &SOut) const
Definition: hash.cpp:75
TSize MxBfL
Definition: hash.h:713
Definition: fl.h:384
static int GetPrimHashCd(const char *p)
Definition: hash.cpp:118
static int GetSecHashCd(const char *p)
Definition: hash.h:1256
virtual 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:1183
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:85
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:110
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:523
static int GetPrimHashCd(const char *CStr)
Definition: hash.cpp:106
Definition: md5.h:81
TSize BfL
Definition: hash.h:713
char * Bf
Definition: hash.h:715
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
int GetSecHashCd() const
Definition: md5.cpp:271
void LoadCs()
Definition: fl.h:408
int GetPrimHashCd() const
Definition: md5.cpp:264
static int GetSecHashCd(const char *p)
Definition: hash.cpp:123
void Resize(TSize _MxBfL)
Definition: hash.cpp:3
static int GetPrimHashCd(const char *p)
Definition: hash.h:1253