SNAP Library 2.0, Developer Reference  2013-05-13 16:33:57
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TStrPool Class Reference

#include <dt.h>

Collaboration diagram for TStrPool:

List of all members.

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

References AddStr(), Bf, TStr::Fmt(), IAssertR, and MxBfL.

                                                           : MxBfL(MxBfLen), BfL(0), GrowBy(_GrowBy), Bf(0) {
  //IAssert(MxBfL >= 0); IAssert(GrowBy >= 0);
  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()); }
  AddStr(""); // add an empty string at the beginning for fast future access
}

Here is the call graph for this function:

TStrPool::TStrPool ( TSIn SIn,
bool  LoadCompact = true 
)

Definition at line 1685 of file dt.cpp.

References Bf, BfL, TStr::Fmt(), GrowBy, IAssertR, TSIn::Load(), TSIn::LoadBf(), TSIn::LoadCs(), and MxBfL.

                                              : MxBfL(0), BfL(0), GrowBy(0), Bf(0) {
  SIn.Load(MxBfL);  SIn.Load(BfL);  SIn.Load(GrowBy);
  //IAssert(MxBfL >= BfL);  IAssert(BfL >= 0);  IAssert(GrowBy >= 0);
  if (LoadCompact) MxBfL = BfL;
  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()); }
  if (BfL > 0) SIn.LoadBf(Bf, BfL);
  SIn.LoadCs();
}

Here is the call graph for this function:

TStrPool::TStrPool ( const TStrPool Pool) [inline]

Definition at line 788 of file dt.h.

References TStr::Fmt(), and IAssertR.

                                 : MxBfL(Pool.MxBfL), BfL(Pool.BfL), GrowBy(Pool.GrowBy) {
    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); }

Here is the call graph for this function:

TStrPool::~TStrPool ( ) [inline]

Definition at line 790 of file dt.h.

References TStr::Fmt(), and IAssertR.

Referenced by TUniChDb::Load().

{ if (Bf) free(Bf); else IAssertR(MxBfL == 0, TStr::Fmt("size: %u, expected size: 0", MxBfL).CStr());  Bf = 0; MxBfL = 0; BfL = 0; }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Function Documentation

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

Definition at line 1711 of file dt.cpp.

References Assert, Bf, BfL, IAssertR, Len(), MxBfL, and Resize().

Referenced by TUniChDb::LoadTxt(), and TStrPool().

                                                      {
  IAssertR(Len > 0, "String too short (length includes the null character)");  //J: if (! Len) return -1;
  if (Len == 1 && BfL > 0) { return 0; } // empty string
  Assert(Str);  Assert(Len > 0);
  if (BfL + Len > MxBfL) Resize(BfL + Len);
  memcpy(Bf + BfL, Str, Len);
  uint Pos = BfL;  BfL += Len;  return Pos;
}

Here is the call graph for this function:

Here is the caller graph for this function:

uint TStrPool::AddStr ( const char *  Str) [inline]

Definition at line 806 of file dt.h.

References AddStr().

Referenced by AddStr().

{ return AddStr(Str, uint(strlen(Str)) + 1); }

Here is the call graph for this function:

Here is the caller graph for this function:

uint TStrPool::AddStr ( const TStr Str) [inline]

Definition at line 807 of file dt.h.

References AddStr(), TStr::CStr(), and TStr::Len().

Referenced by AddStr().

{ return AddStr(Str.CStr(), Str.Len() + 1); }

Here is the call graph for this function:

Here is the caller graph for this function:

void TStrPool::Clr ( bool  DoDel = false) [inline]

Definition at line 816 of file dt.h.

Referenced by TUniChDb::Clr().

{ BfL = 0; if (DoDel && Bf) { free(Bf); Bf = 0; MxBfL = 0; } }

Here is the caller graph for this function:

int TStrPool::Cmp ( const uint Offset,
const char *  Str 
) const [inline]

Definition at line 817 of file dt.h.

References Assert.

                                                     { Assert(Offset < BfL);
    if (Offset != 0) return strcmp(Bf + Offset, Str); else return strcmp("", Str); }
bool TStrPool::Empty ( ) const [inline]

Definition at line 801 of file dt.h.

{ return ! Len(); }
const char* TStrPool::GetCStr ( const uint Offset) const [inline]

Definition at line 811 of file dt.h.

References Assert, TStr::CStr(), and TStr::GetNullStr().

Referenced by TUniChDb::GetCharName().

                                                { Assert(Offset < BfL);
    if (Offset == 0) return TStr::GetNullStr().CStr(); else return Bf + Offset; }

Here is the call graph for this function:

Here is the caller graph for this function:

int TStrPool::GetPrimHashCd ( const char *  CStr) [static]

Definition at line 1720 of file dt.cpp.

                                            {
  return TStrHashF_DJB::GetPrimHashCd(CStr);
}
int TStrPool::GetPrimHashCd ( const uint Offset) [inline]

Definition at line 822 of file dt.h.

References Assert.

                                        { Assert(Offset < BfL);
    if (Offset != 0) return GetPrimHashCd(Bf + Offset); else return GetPrimHashCd(""); }
int TStrPool::GetSecHashCd ( const char *  CStr) [static]

Definition at line 1724 of file dt.cpp.

                                           {
  return TStrHashF_DJB::GetSecHashCd(CStr);
}
int TStrPool::GetSecHashCd ( const uint Offset) [inline]

Definition at line 824 of file dt.h.

References Assert.

                                       { Assert(Offset < BfL);
    if (Offset != 0) return GetSecHashCd(Bf + Offset); else return GetSecHashCd(""); }
TStr TStrPool::GetStr ( const uint Offset) const [inline]

Definition at line 809 of file dt.h.

References Assert, and TStr::GetNullStr().

                                        { Assert(Offset < BfL);
    if (Offset == 0) return TStr::GetNullStr(); else return TStr(Bf + Offset); }

Here is the call graph for this function:

uint TStrPool::Len ( ) const [inline]

Definition at line 799 of file dt.h.

Referenced by AddStr().

{ return BfL; }

Here is the caller graph for this function:

static PStrPool TStrPool::Load ( TSIn SIn,
bool  LoadCompacted = true 
) [inline, static]

Definition at line 795 of file dt.h.

{ return PStrPool(new TStrPool(SIn, LoadCompacted)); }
static PStrPool TStrPool::New ( const uint _MxBfLen = 0,
const uint _GrowBy = 16*1024*1024 
) [inline, static]

Definition at line 792 of file dt.h.

{ return PStrPool(new TStrPool(_MxBfLen, _GrowBy)); }
static PStrPool TStrPool::New ( TSIn SIn) [inline, static]

Definition at line 793 of file dt.h.

{ return new TStrPool(SIn); }
static PStrPool TStrPool::New ( const TStr fileName) [inline, static]

Definition at line 794 of file dt.h.

References TFIn::New().

{ PSIn SIn = TFIn::New(fileName); return new TStrPool(*SIn); }

Here is the call graph for this function:

char* TStrPool::operator() ( ) const [inline]

Definition at line 802 of file dt.h.

{ return Bf; }
TStrPool & TStrPool::operator= ( const TStrPool Pool)

Definition at line 1700 of file dt.cpp.

References Bf, BfL, TStr::Fmt(), GrowBy, IAssertR, and MxBfL.

                                                    {
  if (this != &Pool) {
    GrowBy = Pool.GrowBy;  MxBfL = Pool.MxBfL;  BfL = Pool.BfL;
    if (Bf) free(Bf); else IAssertR(MxBfL == 0, TStr::Fmt("size: %u, expected size: 0", MxBfL).CStr());
    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);
  }
  return *this;
}

Here is the call graph for this function:

void TStrPool::Resize ( const uint _MxBfL) [private]

Definition at line 1662 of file dt.cpp.

References Bf, TStr::Fmt(), TInt::GetMn(), GrowBy, IAssertR, and MxBfL.

Referenced by AddStr().

                                        {
  uint newSize = MxBfL;
  while (newSize < _MxBfL) {
    if (newSize >= GrowBy && GrowBy > 0) newSize += GrowBy;
    else if (newSize > 0) newSize *= 2;
    else newSize = TInt::GetMn(GrowBy, 1024);
    // check for overflow at 4GB
    IAssertR(newSize >= MxBfL, TStr::Fmt("TStrPool::Resize: %u, %u [Size larger than 4Gb, which is not supported by TStrPool]", newSize, MxBfL).CStr());
  }
  if (newSize > MxBfL) {
    Bf = (char *) realloc(Bf, newSize);
    IAssertR(Bf, TStr::Fmt("old Bf size: %u, new size: %u", MxBfL, newSize).CStr());
    MxBfL = newSize;
  }
  IAssertR(MxBfL >= _MxBfL, TStr::Fmt("new size: %u, requested size: %u", MxBfL, _MxBfL).CStr());
}

Here is the call graph for this function:

Here is the caller graph for this function:

void TStrPool::Save ( TSOut SOut) const

Definition at line 1694 of file dt.cpp.

References Bf, BfL, GrowBy, MxBfL, TSOut::Save(), TSOut::SaveBf(), and TSOut::SaveCs().

Referenced by TUniChDb::Save().

                                     {
  SOut.Save(MxBfL);  SOut.Save(BfL);  SOut.Save(GrowBy);
  SOut.SaveBf(Bf, BfL);
  SOut.SaveCs();
}

Here is the call graph for this function:

Here is the caller graph for this function:

void TStrPool::Save ( const TStr FNm) [inline]

Definition at line 797 of file dt.h.

References TFOut::New(), and Save().

Referenced by Save().

{PSOut SOut=TFOut::New(FNm); Save(*SOut);}

Here is the call graph for this function:

Here is the caller graph for this function:

uint TStrPool::Size ( ) const [inline]

Definition at line 800 of file dt.h.

{ return MxBfL; }

Friends And Related Function Documentation

friend class TPt< TStrPool > [friend]

Definition at line 779 of file dt.h.


Member Data Documentation

char* TStrPool::Bf [private]

Definition at line 782 of file dt.h.

Referenced by AddStr(), operator=(), Resize(), Save(), and TStrPool().

uint TStrPool::BfL [private]

Definition at line 781 of file dt.h.

Referenced by AddStr(), operator=(), Save(), and TStrPool().

TCRef TStrPool::CRef [private]

Definition at line 779 of file dt.h.

Definition at line 781 of file dt.h.

Referenced by operator=(), Resize(), Save(), and TStrPool().

uint TStrPool::MxBfL [private]

Definition at line 781 of file dt.h.

Referenced by AddStr(), operator=(), Resize(), Save(), and TStrPool().


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