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
TStrPool Class Reference

#include <dt.h>

Public Member Functions

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

Static Public Member Functions

static PStrPool New (const uint &_MxBfLen=0, const uint &_GrowBy=16 *1024 *1024)
 
static PStrPool New (TSIn &SIn)
 
static PStrPool New (const TStr &fileName)
 
static PStrPool Load (TSIn &SIn, bool LoadCompacted=true)
 
static int GetPrimHashCd (const char *CStr)
 
static int GetSecHashCd (const char *CStr)
 
static PStrPool LoadShM (TSIn &SIn)
 

Private Member Functions

void Resize (const uint &_MxBfL)
 

Private Attributes

TCRef CRef
 
uint MxBfL
 
uint BfL
 
uint GrowBy
 
char * Bf
 

Friends

class TPt< TStrPool >
 

Detailed Description

Definition at line 781 of file dt.h.

Constructor & Destructor Documentation

TStrPool::TStrPool ( const uint MxBfLen = 0,
const uint _GrowBy = 16*1024*1024 
)

Definition at line 1679 of file dt.cpp.

1679  : MxBfL(MxBfLen), BfL(0), GrowBy(_GrowBy), Bf(0) {
1680  //IAssert(MxBfL >= 0); IAssert(GrowBy >= 0);
1681  if (MxBfL > 0) { Bf = (char *) malloc(MxBfL); IAssertR(Bf, TStr::Fmt("Can not resize buffer to %u bytes. [Program failed to allocate more memory. Solution: Get a bigger machine.]", MxBfL).CStr()); }
1682  AddStr(""); // add an empty string at the beginning for fast future access
1683 }
#define IAssertR(Cond, Reason)
Definition: bd.h:265
uint MxBfL
Definition: dt.h:783
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * Bf
Definition: dt.h:784
uint GrowBy
Definition: dt.h:783
uint AddStr(const char *Str, const uint &Len)
Definition: dt.cpp:1711
uint BfL
Definition: dt.h:783
TStrPool::TStrPool ( TSIn SIn,
bool  LoadCompact = true 
)

Definition at line 1685 of file dt.cpp.

1685  : MxBfL(0), BfL(0), GrowBy(0), Bf(0) {
1686  SIn.Load(MxBfL); SIn.Load(BfL); SIn.Load(GrowBy);
1687  //IAssert(MxBfL >= BfL); IAssert(BfL >= 0); IAssert(GrowBy >= 0);
1688  if (LoadCompact) MxBfL = BfL;
1689  if (MxBfL > 0) { Bf = (char *) malloc(MxBfL); IAssertR(Bf, TStr::Fmt("Can not resize buffer to %u bytes. [Program failed to allocate more memory. Solution: Get a bigger machine.]", MxBfL).CStr()); }
1690  if (BfL > 0) SIn.LoadBf(Bf, BfL);
1691  SIn.LoadCs();
1692 }
#define IAssertR(Cond, Reason)
Definition: bd.h:265
uint MxBfL
Definition: dt.h:783
virtual void LoadCs()
Definition: fl.cpp:28
void Load(bool &Bool)
Definition: fl.h:84
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * Bf
Definition: dt.h:784
void LoadBf(const void *Bf, const TSize &BfL)
Definition: fl.h:81
uint GrowBy
Definition: dt.h:783
uint BfL
Definition: dt.h:783
TStrPool::TStrPool ( const TStrPool Pool)
inline

Definition at line 790 of file dt.h.

790  : MxBfL(Pool.MxBfL), BfL(Pool.BfL), GrowBy(Pool.GrowBy) {
791  Bf = (char *) malloc(Pool.MxBfL); IAssertR(Bf, TStr::Fmt("Can not resize buffer to %u bytes. [Program failed to allocate more memory. Solution: Get a bigger machine.]", MxBfL).CStr()); memcpy(Bf, Pool.Bf, Pool.BfL); }
#define IAssertR(Cond, Reason)
Definition: bd.h:265
uint MxBfL
Definition: dt.h:783
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * Bf
Definition: dt.h:784
uint GrowBy
Definition: dt.h:783
uint BfL
Definition: dt.h:783
TStrPool::~TStrPool ( )
inline

Definition at line 792 of file dt.h.

792 { if (Bf) free(Bf); else IAssertR(MxBfL == 0, TStr::Fmt("size: %u, expected size: 0", MxBfL).CStr()); Bf = 0; MxBfL = 0; BfL = 0; }
#define IAssertR(Cond, Reason)
Definition: bd.h:265
uint MxBfL
Definition: dt.h:783
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * Bf
Definition: dt.h:784
uint BfL
Definition: dt.h:783

Member Function Documentation

uint TStrPool::AddStr ( const char *  Str,
const uint Len 
)

Definition at line 1711 of file dt.cpp.

1711  {
1712  IAssertR(Len > 0, "String too short (length includes the null character)"); //J: if (! Len) return -1;
1713  if (Len == 1 && BfL > 0) { return 0; } // empty string
1714  Assert(Str); Assert(Len > 0);
1715  if (BfL + Len > MxBfL) Resize(BfL + Len);
1716  memcpy(Bf + BfL, Str, Len);
1717  uint Pos = BfL; BfL += Len; return Pos;
1718 }
void Resize(const uint &_MxBfL)
Definition: dt.cpp:1662
#define IAssertR(Cond, Reason)
Definition: bd.h:265
unsigned int uint
Definition: bd.h:11
uint MxBfL
Definition: dt.h:783
#define Assert(Cond)
Definition: bd.h:251
char * Bf
Definition: dt.h:784
uint Len() const
Definition: dt.h:801
uint BfL
Definition: dt.h:783
uint TStrPool::AddStr ( const char *  Str)
inline

Definition at line 809 of file dt.h.

809 { return AddStr(Str, uint(strlen(Str)) + 1); }
unsigned int uint
Definition: bd.h:11
uint AddStr(const char *Str, const uint &Len)
Definition: dt.cpp:1711
uint TStrPool::AddStr ( const TStr Str)
inline

Definition at line 810 of file dt.h.

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

Definition at line 819 of file dt.h.

819 { BfL = 0; if (DoDel && Bf) { free(Bf); Bf = 0; MxBfL = 0; } }
uint MxBfL
Definition: dt.h:783
char * Bf
Definition: dt.h:784
uint BfL
Definition: dt.h:783
int TStrPool::Cmp ( const uint Offset,
const char *  Str 
) const
inline

Definition at line 820 of file dt.h.

820  { Assert(Offset < BfL);
821  if (Offset != 0) return strcmp(Bf + Offset, Str); else return strcmp("", Str); }
#define Assert(Cond)
Definition: bd.h:251
char * Bf
Definition: dt.h:784
uint BfL
Definition: dt.h:783
bool TStrPool::Empty ( ) const
inline

Definition at line 803 of file dt.h.

803 { return ! Len(); }
uint Len() const
Definition: dt.h:801
const char* TStrPool::GetCStr ( const uint Offset) const
inline

Definition at line 814 of file dt.h.

814  { Assert(Offset < BfL);
815  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 * Bf
Definition: dt.h:784
char * CStr()
Definition: dt.h:479
uint BfL
Definition: dt.h:783
::TSize TStrPool::GetMemUsed ( )
inline

Definition at line 806 of file dt.h.

806 { return 4 * sizeof(int) + MxBfL;}
uint MxBfL
Definition: dt.h:783
int TStrPool::GetPrimHashCd ( const char *  CStr)
static

Definition at line 1720 of file dt.cpp.

1720  {
1721  return TStrHashF_DJB::GetPrimHashCd(CStr);
1722 }
static int GetPrimHashCd(const char *p)
Definition: hash.h:1253
int TStrPool::GetPrimHashCd ( const uint Offset)
inline

Definition at line 825 of file dt.h.

825  { Assert(Offset < BfL);
826  if (Offset != 0) return GetPrimHashCd(Bf + Offset); else return GetPrimHashCd(""); }
static int GetPrimHashCd(const char *CStr)
Definition: dt.cpp:1720
#define Assert(Cond)
Definition: bd.h:251
char * Bf
Definition: dt.h:784
uint BfL
Definition: dt.h:783
int TStrPool::GetSecHashCd ( const char *  CStr)
static

Definition at line 1724 of file dt.cpp.

1724  {
1725  return TStrHashF_DJB::GetSecHashCd(CStr);
1726 }
static int GetSecHashCd(const char *p)
Definition: hash.h:1256
int TStrPool::GetSecHashCd ( const uint Offset)
inline

Definition at line 827 of file dt.h.

827  { Assert(Offset < BfL);
828  if (Offset != 0) return GetSecHashCd(Bf + Offset); else return GetSecHashCd(""); }
static int GetSecHashCd(const char *CStr)
Definition: dt.cpp:1724
#define Assert(Cond)
Definition: bd.h:251
char * Bf
Definition: dt.h:784
uint BfL
Definition: dt.h:783
TStr TStrPool::GetStr ( const uint Offset) const
inline

Definition at line 812 of file dt.h.

812  { Assert(Offset < BfL);
813  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
char * Bf
Definition: dt.h:784
uint BfL
Definition: dt.h:783
uint TStrPool::Len ( ) const
inline

Definition at line 801 of file dt.h.

801 { return BfL; }
uint BfL
Definition: dt.h:783
static PStrPool TStrPool::Load ( TSIn SIn,
bool  LoadCompacted = true 
)
inlinestatic

Definition at line 797 of file dt.h.

797 { return PStrPool(new TStrPool(SIn, LoadCompacted)); }
TPt< TStrPool > PStrPool
Definition: dt.h:781
TStrPool(const uint &MxBfLen=0, const uint &_GrowBy=16 *1024 *1024)
Definition: dt.cpp:1679
static PStrPool TStrPool::LoadShM ( TSIn SIn)
inlinestatic

Definition at line 829 of file dt.h.

829 { return new TStrPool(SIn); }
TStrPool(const uint &MxBfLen=0, const uint &_GrowBy=16 *1024 *1024)
Definition: dt.cpp:1679
static PStrPool TStrPool::New ( const uint _MxBfLen = 0,
const uint _GrowBy = 16*1024*1024 
)
inlinestatic

Definition at line 794 of file dt.h.

794 { return PStrPool(new TStrPool(_MxBfLen, _GrowBy)); }
TPt< TStrPool > PStrPool
Definition: dt.h:781
TStrPool(const uint &MxBfLen=0, const uint &_GrowBy=16 *1024 *1024)
Definition: dt.cpp:1679
static PStrPool TStrPool::New ( TSIn SIn)
inlinestatic

Definition at line 795 of file dt.h.

795 { return new TStrPool(SIn); }
TStrPool(const uint &MxBfLen=0, const uint &_GrowBy=16 *1024 *1024)
Definition: dt.cpp:1679
static PStrPool TStrPool::New ( const TStr fileName)
inlinestatic

Definition at line 796 of file dt.h.

796 { PSIn SIn = TFIn::New(fileName); return new TStrPool(*SIn); }
static PSIn New(const TStr &FNm)
Definition: fl.cpp:290
TStrPool(const uint &MxBfLen=0, const uint &_GrowBy=16 *1024 *1024)
Definition: dt.cpp:1679
char* TStrPool::operator() ( ) const
inline

Definition at line 804 of file dt.h.

804 { return Bf; }
char * Bf
Definition: dt.h:784
TStrPool & TStrPool::operator= ( const TStrPool Pool)

Definition at line 1700 of file dt.cpp.

1700  {
1701  if (this != &Pool) {
1702  GrowBy = Pool.GrowBy; MxBfL = Pool.MxBfL; BfL = Pool.BfL;
1703  if (Bf) free(Bf); else IAssertR(MxBfL == 0, TStr::Fmt("size: %u, expected size: 0", MxBfL).CStr());
1704  Bf = (char *) malloc(MxBfL); IAssertR(Bf, TStr::Fmt("Can not resize buffer to %u bytes. [Program failed to allocate more memory. Solution: Get a bigger machine.]", MxBfL).CStr()); memcpy(Bf, Pool.Bf, BfL);
1705  }
1706  return *this;
1707 }
#define IAssertR(Cond, Reason)
Definition: bd.h:265
uint MxBfL
Definition: dt.h:783
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * Bf
Definition: dt.h:784
uint GrowBy
Definition: dt.h:783
uint BfL
Definition: dt.h:783
void TStrPool::Resize ( const uint _MxBfL)
private

Definition at line 1662 of file dt.cpp.

1662  {
1663  uint newSize = MxBfL;
1664  while (newSize < _MxBfL) {
1665  if (newSize >= GrowBy && GrowBy > 0) newSize += GrowBy;
1666  else if (newSize > 0) newSize *= 2;
1667  else newSize = TInt::GetMn(GrowBy, 1024);
1668  // check for overflow at 4GB
1669  IAssertR(newSize >= MxBfL, TStr::Fmt("TStrPool::Resize: %u, %u [Size larger than 4Gb, which is not supported by TStrPool]", newSize, MxBfL).CStr());
1670  }
1671  if (newSize > MxBfL) {
1672  Bf = (char *) realloc(Bf, newSize);
1673  IAssertR(Bf, TStr::Fmt("old Bf size: %u, new size: %u", MxBfL, newSize).CStr());
1674  MxBfL = newSize;
1675  }
1676  IAssertR(MxBfL >= _MxBfL, TStr::Fmt("new size: %u, requested size: %u", MxBfL, _MxBfL).CStr());
1677 }
#define IAssertR(Cond, Reason)
Definition: bd.h:265
unsigned int uint
Definition: bd.h:11
uint MxBfL
Definition: dt.h:783
static int GetMn(const int &Int1, const int &Int2)
Definition: dt.h:1183
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * Bf
Definition: dt.h:784
uint GrowBy
Definition: dt.h:783
void TStrPool::Save ( TSOut SOut) const

Definition at line 1694 of file dt.cpp.

1694  {
1695  SOut.Save(MxBfL); SOut.Save(BfL); SOut.Save(GrowBy);
1696  SOut.SaveBf(Bf, BfL);
1697  SOut.SaveCs();
1698 }
uint MxBfL
Definition: dt.h:783
void SaveCs()
Definition: fl.h:171
void SaveBf(const void *Bf, const TSize &BfL)
Definition: fl.h:172
void Save(const bool &Bool)
Definition: fl.h:173
char * Bf
Definition: dt.h:784
uint GrowBy
Definition: dt.h:783
uint BfL
Definition: dt.h:783
void TStrPool::Save ( const TStr FNm)
inline

Definition at line 799 of file dt.h.

799 {PSOut SOut=TFOut::New(FNm); Save(*SOut);}
static PSOut New(const TStr &FNm, const bool &Append=false)
Definition: fl.cpp:442
void Save(TSOut &SOut) const
Definition: dt.cpp:1694
Definition: bd.h:196
uint TStrPool::Size ( ) const
inline

Definition at line 802 of file dt.h.

802 { return MxBfL; }
uint MxBfL
Definition: dt.h:783

Friends And Related Function Documentation

friend class TPt< TStrPool >
friend

Definition at line 781 of file dt.h.

Member Data Documentation

char* TStrPool::Bf
private

Definition at line 784 of file dt.h.

uint TStrPool::BfL
private

Definition at line 783 of file dt.h.

TCRef TStrPool::CRef
private

Definition at line 781 of file dt.h.

uint TStrPool::GrowBy
private

Definition at line 783 of file dt.h.

uint TStrPool::MxBfL
private

Definition at line 783 of file dt.h.


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