SNAP Library 6.0, User Reference  2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
TBigStrPool Class Reference

#include <hash.h>

Public Member Functions

 TBigStrPool (TSize MxBfLen=0, uint _GrowBy=16 *1024 *1024)
 
 TBigStrPool (TSIn &SIn, bool LoadCompact=true)
 
 TBigStrPool (const TBigStrPool &Pool)
 
 ~TBigStrPool ()
 
void Save (TSOut &SOut) const
 
void Save (const TStr &fileName)
 
int GetStrs () const
 
TSize Len () const
 
TSize Size () const
 
bool Empty () const
 
char * operator() () const
 
TBigStrPooloperator= (const TBigStrPool &Pool)
 
::TSize GetMemUsed ()
 
int AddStr (const char *Str, uint Len)
 
int AddStr (const char *Str)
 
int AddStr (const TStr &Str)
 
TStr GetStr (const int &StrId) const
 
const char * GetCStr (const int &StrId) const
 
TStr GetStrFromOffset (const TSize &Offset) const
 
const char * GetCStrFromOffset (const TSize &Offset) const
 
void Clr (bool DoDel=false)
 
int Cmp (const int &StrId, const char *Str) const
 
int GetPrimHashCd (const int &StrId)
 
int GetSecHashCd (const int &StrId)
 

Static Public Member Functions

static PBigStrPool New (TSize _MxBfLen=0, uint _GrowBy=16 *1024 *1024)
 
static PBigStrPool New (TSIn &SIn)
 
static PBigStrPool New (const TStr &fileName)
 
static PBigStrPool Load (TSIn &SIn, bool LoadCompacted=true)
 
static PBigStrPool LoadShM (TShMIn &ShMIn, bool LoadCompact=true)
 Load the string pool with the buffer backed by shared memory. More...
 
static int GetPrimHashCd (const char *CStr)
 
static int GetSecHashCd (const char *CStr)
 

Private Member Functions

void Resize (TSize _MxBfL)
 
void LoadPoolShM (TShMIn &ShMIn, bool LoadCompact=true)
 

Private Attributes

TCRef CRef
 
TSize MxBfL
 
TSize BfL
 
uint GrowBy
 
char * Bf
 
TVec< TSizeIdOffV
 
bool IsShM
 

Friends

class TPt< TBigStrPool >
 

Detailed Description

Definition at line 711 of file hash.h.

Constructor & Destructor Documentation

TBigStrPool::TBigStrPool ( TSize  MxBfLen = 0,
uint  _GrowBy = 16*1024*1024 
)

Definition at line 23 of file hash.cpp.

23  : 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 }
#define IAssert(Cond)
Definition: bd.h:262
bool IsShM
Definition: hash.h:717
uint GrowBy
Definition: hash.h:714
int AddStr(const char *Str, uint Len)
Definition: hash.cpp:95
TSize MxBfL
Definition: hash.h:713
TSize BfL
Definition: hash.h:713
char * Bf
Definition: hash.h:715
TBigStrPool::TBigStrPool ( TSIn SIn,
bool  LoadCompact = true 
)

Definition at line 29 of file hash.cpp.

29  : 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 }
#define IAssert(Cond)
Definition: bd.h:262
TVec< TSize > IdOffV
Definition: hash.h:716
bool IsShM
Definition: hash.h:717
uint GrowBy
Definition: hash.h:714
TSize MxBfL
Definition: hash.h:713
virtual void LoadCs()
Definition: fl.cpp:28
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
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
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
TBigStrPool::TBigStrPool ( const TBigStrPool Pool)
inline

Definition at line 724 of file hash.h.

724  : MxBfL(Pool.MxBfL), BfL(Pool.BfL), GrowBy(Pool.GrowBy) {
725  Bf = (char *) malloc(Pool.MxBfL); IAssert(Bf); memcpy(Bf, Pool.Bf, Pool.BfL); }
#define IAssert(Cond)
Definition: bd.h:262
uint GrowBy
Definition: hash.h:714
TSize MxBfL
Definition: hash.h:713
TSize BfL
Definition: hash.h:713
char * Bf
Definition: hash.h:715
TBigStrPool::~TBigStrPool ( )
inline

Definition at line 726 of file hash.h.

726 { if (Bf && !IsShM) free(Bf); else IAssert(MxBfL == 0 || IsShM); MxBfL = 0; BfL = 0; }
#define IAssert(Cond)
Definition: bd.h:262
bool IsShM
Definition: hash.h:717
TSize MxBfL
Definition: hash.h:713
TSize BfL
Definition: hash.h:713
char * Bf
Definition: hash.h:715

Member Function Documentation

int TBigStrPool::AddStr ( const char *  Str,
uint  Len 
)

Definition at line 95 of file hash.cpp.

95  {
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 }
#define IAssertR(Cond, Reason)
Definition: bd.h:265
TVec< TSize > IdOffV
Definition: hash.h:716
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TSize Len() const
Definition: hash.h:742
TSize MxBfL
Definition: hash.h:713
size_t TSize
Definition: bd.h:58
#define Assert(Cond)
Definition: bd.h:251
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
void Resize(TSize _MxBfL)
Definition: hash.cpp:3
int TBigStrPool::AddStr ( const char *  Str)
inline

Definition at line 752 of file hash.h.

752 { return AddStr(Str, uint(strlen(Str)) + 1); }
unsigned int uint
Definition: bd.h:11
int AddStr(const char *Str, uint Len)
Definition: hash.cpp:95
int TBigStrPool::AddStr ( const TStr Str)
inline

Definition at line 753 of file hash.h.

753 { return AddStr(Str.CStr(), Str.Len() + 1); }
int Len() const
Definition: dt.h:490
int AddStr(const char *Str, uint Len)
Definition: hash.cpp:95
char * CStr()
Definition: dt.h:479
void TBigStrPool::Clr ( bool  DoDel = false)
inline

Definition at line 765 of file hash.h.

765  {
766  BfL = 0; if (DoDel && Bf) {if (!IsShM) { free(Bf);} Bf = 0; MxBfL = 0;}}
bool IsShM
Definition: hash.h:717
TSize MxBfL
Definition: hash.h:713
TSize BfL
Definition: hash.h:713
char * Bf
Definition: hash.h:715
int TBigStrPool::Cmp ( const int &  StrId,
const char *  Str 
) const
inline

Definition at line 767 of file hash.h.

767  { Assert(StrId < GetStrs());
768  if (StrId != 0) return strcmp(Bf + (TSize)IdOffV[StrId], Str); else return strcmp("", Str); }
TVec< TSize > IdOffV
Definition: hash.h:716
size_t TSize
Definition: bd.h:58
#define Assert(Cond)
Definition: bd.h:251
char * Bf
Definition: hash.h:715
int GetStrs() const
Definition: hash.h:741
bool TBigStrPool::Empty ( ) const
inline

Definition at line 744 of file hash.h.

744 { return ! Len(); }
TSize Len() const
Definition: hash.h:742
const char* TBigStrPool::GetCStr ( const int &  StrId) const
inline

Definition at line 757 of file hash.h.

757  { Assert(StrId < GetStrs());
758  if (StrId == 0) return TStr::GetNullStr().CStr(); else return (Bf + (TSize)IdOffV[StrId]); }
TVec< TSize > IdOffV
Definition: hash.h:716
size_t TSize
Definition: bd.h:58
#define Assert(Cond)
Definition: bd.h:251
static TStr GetNullStr()
Definition: dt.cpp:1626
char * CStr()
Definition: dt.h:479
char * Bf
Definition: hash.h:715
int GetStrs() const
Definition: hash.h:741
const char* TBigStrPool::GetCStrFromOffset ( const TSize Offset) const
inline

Definition at line 762 of file hash.h.

762  { Assert(Offset < BfL);
763  if (Offset == 0) return TStr::GetNullStr().CStr(); else return Bf + Offset; }
#define Assert(Cond)
Definition: bd.h:251
static TStr GetNullStr()
Definition: dt.cpp:1626
char * CStr()
Definition: dt.h:479
TSize BfL
Definition: hash.h:713
char * Bf
Definition: hash.h:715
::TSize TBigStrPool::GetMemUsed ( )
inline

Definition at line 747 of file hash.h.

747  {
748  return 4 * sizeof(int) + IdOffV.GetMemUsed() + MxBfL;
749  }
TVec< TSize > IdOffV
Definition: hash.h:716
TSize MxBfL
Definition: hash.h:713
TSizeTy GetMemUsed() const
Returns the memory footprint (the number of bytes) of the vector.
Definition: ds.h:511
int TBigStrPool::GetPrimHashCd ( const char *  CStr)
static

Definition at line 106 of file hash.cpp.

106  {
107  return TStrHashF_DJB::GetPrimHashCd(CStr);
108 }
static int GetPrimHashCd(const char *p)
Definition: hash.h:1253
int TBigStrPool::GetPrimHashCd ( const int &  StrId)
inline

Definition at line 772 of file hash.h.

772  { Assert(StrId < GetStrs());
773  if (StrId != 0) return GetPrimHashCd(Bf + (TSize)IdOffV[StrId]); else return GetPrimHashCd(""); }
TVec< TSize > IdOffV
Definition: hash.h:716
size_t TSize
Definition: bd.h:58
#define Assert(Cond)
Definition: bd.h:251
static int GetPrimHashCd(const char *CStr)
Definition: hash.cpp:106
char * Bf
Definition: hash.h:715
int GetStrs() const
Definition: hash.h:741
int TBigStrPool::GetSecHashCd ( const char *  CStr)
static

Definition at line 110 of file hash.cpp.

110  {
111  return TStrHashF_DJB::GetSecHashCd(CStr);
112 }
static int GetSecHashCd(const char *p)
Definition: hash.h:1256
int TBigStrPool::GetSecHashCd ( const int &  StrId)
inline

Definition at line 774 of file hash.h.

774  { Assert(StrId < GetStrs());
775  if (StrId != 0) return GetSecHashCd(Bf + (TSize)IdOffV[StrId]); else return GetSecHashCd(""); }
TVec< TSize > IdOffV
Definition: hash.h:716
size_t TSize
Definition: bd.h:58
#define Assert(Cond)
Definition: bd.h:251
static int GetSecHashCd(const char *CStr)
Definition: hash.cpp:110
char * Bf
Definition: hash.h:715
int GetStrs() const
Definition: hash.h:741
TStr TBigStrPool::GetStr ( const int &  StrId) const
inline

Definition at line 755 of file hash.h.

755  { Assert(StrId < GetStrs());
756  if (StrId == 0) return TStr::GetNullStr(); else return TStr(Bf + (TSize)IdOffV[StrId]); }
TVec< TSize > IdOffV
Definition: hash.h:716
size_t TSize
Definition: bd.h:58
#define Assert(Cond)
Definition: bd.h:251
static TStr GetNullStr()
Definition: dt.cpp:1626
Definition: dt.h:412
char * Bf
Definition: hash.h:715
int GetStrs() const
Definition: hash.h:741
TStr TBigStrPool::GetStrFromOffset ( const TSize Offset) const
inline

Definition at line 760 of file hash.h.

760  { Assert(Offset < BfL);
761  if (Offset == 0) return TStr::GetNullStr(); else return TStr(Bf + Offset); }
#define Assert(Cond)
Definition: bd.h:251
static TStr GetNullStr()
Definition: dt.cpp:1626
Definition: dt.h:412
TSize BfL
Definition: hash.h:713
char * Bf
Definition: hash.h:715
int TBigStrPool::GetStrs ( ) const
inline

Definition at line 741 of file hash.h.

741 { return IdOffV.Len(); }
TVec< TSize > IdOffV
Definition: hash.h:716
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TSize TBigStrPool::Len ( ) const
inline

Definition at line 742 of file hash.h.

742 { return BfL; }
TSize BfL
Definition: hash.h:713
static PBigStrPool TBigStrPool::Load ( TSIn SIn,
bool  LoadCompacted = true 
)
inlinestatic

Definition at line 731 of file hash.h.

731 { return PBigStrPool(new TBigStrPool(SIn, LoadCompacted)); }
TBigStrPool(TSize MxBfLen=0, uint _GrowBy=16 *1024 *1024)
Definition: hash.cpp:23
TPt< TBigStrPool > PBigStrPool
Definition: hash.h:711
void TBigStrPool::LoadPoolShM ( TShMIn ShMIn,
bool  LoadCompact = true 
)
private

Definition at line 48 of file hash.cpp.

48  {
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 }
#define IAssert(Cond)
Definition: bd.h:262
TVec< TSize > IdOffV
Definition: hash.h:716
bool IsShM
Definition: hash.h:717
uint GrowBy
Definition: hash.h:714
char * AdvanceCursor(TSize N)
Return the current pointer and advance the cursor.
Definition: fl.h:425
TSize MxBfL
Definition: hash.h:713
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
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
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
void LoadCs()
Definition: fl.h:408
static PBigStrPool TBigStrPool::LoadShM ( TShMIn ShMIn,
bool  LoadCompact = true 
)
inlinestatic

Load the string pool with the buffer backed by shared memory.

Definition at line 733 of file hash.h.

733  {
734  TBigStrPool* StrPool = new TBigStrPool();
735  StrPool->LoadPoolShM(ShMIn, LoadCompact);
736  return PBigStrPool(StrPool);
737  }
TBigStrPool(TSize MxBfLen=0, uint _GrowBy=16 *1024 *1024)
Definition: hash.cpp:23
void LoadPoolShM(TShMIn &ShMIn, bool LoadCompact=true)
Definition: hash.cpp:48
TPt< TBigStrPool > PBigStrPool
Definition: hash.h:711
static PBigStrPool TBigStrPool::New ( TSize  _MxBfLen = 0,
uint  _GrowBy = 16*1024*1024 
)
inlinestatic

Definition at line 728 of file hash.h.

728 { return PBigStrPool(new TBigStrPool(_MxBfLen, _GrowBy)); }
TBigStrPool(TSize MxBfLen=0, uint _GrowBy=16 *1024 *1024)
Definition: hash.cpp:23
TPt< TBigStrPool > PBigStrPool
Definition: hash.h:711
static PBigStrPool TBigStrPool::New ( TSIn SIn)
inlinestatic

Definition at line 729 of file hash.h.

729 { return new TBigStrPool(SIn); }
TBigStrPool(TSize MxBfLen=0, uint _GrowBy=16 *1024 *1024)
Definition: hash.cpp:23
static PBigStrPool TBigStrPool::New ( const TStr fileName)
inlinestatic

Definition at line 730 of file hash.h.

730 { PSIn SIn = TFIn::New(fileName); return new TBigStrPool(*SIn); }
TBigStrPool(TSize MxBfLen=0, uint _GrowBy=16 *1024 *1024)
Definition: hash.cpp:23
static PSIn New(const TStr &FNm)
Definition: fl.cpp:290
char* TBigStrPool::operator() ( ) const
inline

Definition at line 745 of file hash.h.

745 { return Bf; }
char * Bf
Definition: hash.h:715
TBigStrPool & TBigStrPool::operator= ( const TBigStrPool Pool)

Definition at line 85 of file hash.cpp.

85  {
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 }
#define IAssert(Cond)
Definition: bd.h:262
uint GrowBy
Definition: hash.h:714
TSize MxBfL
Definition: hash.h:713
TSize BfL
Definition: hash.h:713
char * Bf
Definition: hash.h:715
void TBigStrPool::Resize ( TSize  _MxBfL)
private

Definition at line 3 of file hash.cpp.

3  {
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 }
#define IAssert(Cond)
Definition: bd.h:262
#define IAssertR(Cond, Reason)
Definition: bd.h:265
bool IsShM
Definition: hash.h:717
uint GrowBy
Definition: hash.h:714
TSize MxBfL
Definition: hash.h:713
static int GetMn(const int &Int1, const int &Int2)
Definition: dt.h:1183
size_t TSize
Definition: bd.h:58
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * Bf
Definition: hash.h:715
void TBigStrPool::Save ( TSOut SOut) const

Definition at line 75 of file hash.cpp.

75  {
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 }
TVec< TSize > IdOffV
Definition: hash.h:716
uint GrowBy
Definition: hash.h:714
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TSize MxBfL
Definition: hash.h:713
void SaveCs()
Definition: fl.h:171
unsigned long long uint64
Definition: bd.h:38
void SaveBf(const void *Bf, const TSize &BfL)
Definition: fl.h:172
void Save(const bool &Bool)
Definition: fl.h:173
TSize BfL
Definition: hash.h:713
char * Bf
Definition: hash.h:715
void TBigStrPool::Save ( const TStr fileName)
inline

Definition at line 739 of file hash.h.

739 { TFOut FOut(fileName); Save(FOut); }
Definition: fl.h:319
void Save(TSOut &SOut) const
Definition: hash.cpp:75
TSize TBigStrPool::Size ( ) const
inline

Definition at line 743 of file hash.h.

743 { return MxBfL; }
TSize MxBfL
Definition: hash.h:713

Friends And Related Function Documentation

friend class TPt< TBigStrPool >
friend

Definition at line 711 of file hash.h.

Member Data Documentation

char* TBigStrPool::Bf
private

Definition at line 715 of file hash.h.

TSize TBigStrPool::BfL
private

Definition at line 713 of file hash.h.

TCRef TBigStrPool::CRef
private

Definition at line 711 of file hash.h.

uint TBigStrPool::GrowBy
private

Definition at line 714 of file hash.h.

TVec<TSize> TBigStrPool::IdOffV
private

Definition at line 716 of file hash.h.

bool TBigStrPool::IsShM
private

Definition at line 717 of file hash.h.

TSize TBigStrPool::MxBfL
private

Definition at line 713 of file hash.h.


The documentation for this class was generated from the following files: