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
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)
 

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 778 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:780
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * Bf
Definition: dt.h:781
uint GrowBy
Definition: dt.h:780
uint AddStr(const char *Str, const uint &Len)
Definition: dt.cpp:1711
uint BfL
Definition: dt.h:780
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:780
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:781
void LoadBf(const void *Bf, const TSize &BfL)
Definition: fl.h:81
uint GrowBy
Definition: dt.h:780
uint BfL
Definition: dt.h:780
TStrPool::TStrPool ( const TStrPool Pool)
inline

Definition at line 787 of file dt.h.

787  : MxBfL(Pool.MxBfL), BfL(Pool.BfL), GrowBy(Pool.GrowBy) {
788  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:780
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * Bf
Definition: dt.h:781
uint GrowBy
Definition: dt.h:780
uint BfL
Definition: dt.h:780
TStrPool::~TStrPool ( )
inline

Definition at line 789 of file dt.h.

789 { 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:780
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * Bf
Definition: dt.h:781
uint BfL
Definition: dt.h:780

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:780
#define Assert(Cond)
Definition: bd.h:251
char * Bf
Definition: dt.h:781
uint Len() const
Definition: dt.h:798
uint BfL
Definition: dt.h:780
uint TStrPool::AddStr ( const char *  Str)
inline

Definition at line 806 of file dt.h.

806 { 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 807 of file dt.h.

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

Definition at line 816 of file dt.h.

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

Definition at line 817 of file dt.h.

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

Definition at line 800 of file dt.h.

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

Definition at line 811 of file dt.h.

811  { Assert(Offset < BfL);
812  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:781
char * CStr()
Definition: dt.h:476
uint BfL
Definition: dt.h:780
::TSize TStrPool::GetMemUsed ( )
inline

Definition at line 803 of file dt.h.

803 { return 4 * sizeof(int) + MxBfL;}
uint MxBfL
Definition: dt.h:780
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:1181
int TStrPool::GetPrimHashCd ( const uint Offset)
inline

Definition at line 822 of file dt.h.

822  { Assert(Offset < BfL);
823  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:781
uint BfL
Definition: dt.h:780
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:1184
int TStrPool::GetSecHashCd ( const uint Offset)
inline

Definition at line 824 of file dt.h.

824  { Assert(Offset < BfL);
825  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:781
uint BfL
Definition: dt.h:780
TStr TStrPool::GetStr ( const uint Offset) const
inline

Definition at line 809 of file dt.h.

809  { Assert(Offset < BfL);
810  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:781
uint BfL
Definition: dt.h:780
uint TStrPool::Len ( ) const
inline

Definition at line 798 of file dt.h.

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

Definition at line 794 of file dt.h.

794 { return PStrPool(new TStrPool(SIn, LoadCompacted)); }
TPt< TStrPool > PStrPool
Definition: dt.h:778
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 791 of file dt.h.

791 { return PStrPool(new TStrPool(_MxBfLen, _GrowBy)); }
TPt< TStrPool > PStrPool
Definition: dt.h:778
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 792 of file dt.h.

792 { 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 793 of file dt.h.

793 { 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 801 of file dt.h.

801 { return Bf; }
char * Bf
Definition: dt.h:781
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:780
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * Bf
Definition: dt.h:781
uint GrowBy
Definition: dt.h:780
uint BfL
Definition: dt.h:780
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:780
static int GetMn(const int &Int1, const int &Int2)
Definition: dt.h:1090
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
char * Bf
Definition: dt.h:781
uint GrowBy
Definition: dt.h:780
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:780
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:781
uint GrowBy
Definition: dt.h:780
uint BfL
Definition: dt.h:780
void TStrPool::Save ( const TStr FNm)
inline

Definition at line 796 of file dt.h.

796 {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 799 of file dt.h.

799 { return MxBfL; }
uint MxBfL
Definition: dt.h:780

Friends And Related Function Documentation

friend class TPt< TStrPool >
friend

Definition at line 778 of file dt.h.

Member Data Documentation

char* TStrPool::Bf
private

Definition at line 781 of file dt.h.

uint TStrPool::BfL
private

Definition at line 780 of file dt.h.

TCRef TStrPool::CRef
private

Definition at line 778 of file dt.h.

uint TStrPool::GrowBy
private

Definition at line 780 of file dt.h.

uint TStrPool::MxBfL
private

Definition at line 780 of file dt.h.


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