SNAP Library 2.0, User 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
TGLib_OLD::TVecPool< TVal > Class Template Reference

#include <ds.h>

List of all members.

Public Types

typedef TPt< TVecPool< TVal > > PVecPool
typedef TVec< TVal > TValV

Public Member Functions

 TVecPool (const ::TSize &ExpectVals=0, const ::TSize &_GrowBy=1000000, const bool &_FastCopy=false, const TVal &_EmptyVal=TVal())
 TVecPool (const TVecPool &Pool)
 TVecPool (TSIn &SIn)
 ~TVecPool ()
void Save (TSOut &SOut) const
TVecPooloperator= (const TVecPool &Pool)
::TSize GetVals () const
::TSize GetVecs () const
bool IsVId (const int &VId) const
::TSize Reserved () const
void Reserve (const ::TSize &MxVals)
const TVal & GetEmptyVal () const
void SetEmptyVal (const TVal &_EmptyVal)
::TSize GetMemUsed () const
int AddV (const TValV &ValV)
int AddEmptyV (const int &ValVLen)
uint GetVLen (const int &VId) const
TVal * GetValVPt (const int &VId) const
void GetV (const int &VId, TValV &ValV) const
void PutV (const int &VId, const TValV &ValV)
void CompactPool (const TVal &DelVal)
void ShuffleAll (TRnd &Rnd=TInt::Rnd)
void Clr (bool DoDel=true)
void PutAll (const TVal &Val)

Static Public Member Functions

static PVecPool New (const ::TSize &ExpectVals=0, const ::TSize &GrowBy=1000000, const bool &FastCopy=false)
static PVecPool Load (TSIn &SIn)
static PVecPool Load (const TStr &FNm)

Private Member Functions

void Resize (const ::TSize &_MxVals)

Private Attributes

TCRef CRef
TBool FastCopy
::TSize GrowBy
::TSize MxVals
::TSize Vals
TVal EmptyVal
TVal * ValBf
TVec< ::TSizeIdToOffV

Friends

class TPt< TVecPool< TVal > >

Detailed Description

template<class TVal>
class TGLib_OLD::TVecPool< TVal >

Definition at line 2802 of file ds.h.


Member Typedef Documentation

template<class TVal >
typedef TPt<TVecPool<TVal> > TGLib_OLD::TVecPool< TVal >::PVecPool

Definition at line 2804 of file ds.h.

template<class TVal >
typedef TVec<TVal> TGLib_OLD::TVecPool< TVal >::TValV

Definition at line 2805 of file ds.h.


Constructor & Destructor Documentation

template<class TVal >
TVecPool< TVal >::TVecPool ( const ::TSize ExpectVals = 0,
const ::TSize _GrowBy = 1000000,
const bool &  _FastCopy = false,
const TVal &  _EmptyVal = TVal() 
)

Definition at line 2901 of file ds.h.

                                                                                                                    :
  GrowBy(_GrowBy), MxVals(0), Vals(0), EmptyVal(_EmptyVal), ValBf(NULL) {
  IdToOffV.Add(0);
  Resize(ExpectVals);
}
template<class TVal >
TVecPool< TVal >::TVecPool ( const TVecPool< TVal > &  Pool)

Definition at line 2908 of file ds.h.

                                            :
  FastCopy(Pool.FastCopy), GrowBy(Pool.GrowBy),
  MxVals(Pool.MxVals), Vals(Pool.Vals), EmptyVal(Pool.EmptyVal), IdToOffV(Pool.IdToOffV) {
  try { ValBf = new TVal [MxVals]; }
  catch (std::exception Ex) { FailR(TStr::Fmt("TVecPool::TVecPool: %s, MxVals: %d", Ex.what(), MxVals).CStr()); }
  IAssert(ValBf != NULL);
  if (FastCopy) {
    memcpy(ValBf, Pool.ValBf, MxVals*sizeof(TVal)); }
  else {
    for (TSize ValN = 0; ValN < MxVals; ValN++){ ValBf[ValN] = Pool.ValBf[ValN]; } }
}
template<class TVal >
TVecPool< TVal >::TVecPool ( TSIn SIn)

Definition at line 2921 of file ds.h.

                                 :
  FastCopy(SIn) {
  uint64 _GrowBy, _MxVals, _Vals;
  SIn.Load(_GrowBy); SIn.Load(_MxVals);  SIn.Load(_Vals);
  IAssert(_GrowBy<TSizeMx && _MxVals<TSizeMx && _Vals<TSizeMx);
  GrowBy=TSize(_GrowBy);  MxVals=TSize(_Vals);  Vals=TSize(_Vals); //note MxVals==Vals
  EmptyVal = TVal(SIn);
  if (MxVals==0) { ValBf = NULL; } else { ValBf = new TVal [MxVals]; }
  for (TSize ValN = 0; ValN < Vals; ValN++) { ValBf[ValN] = TVal(SIn); }
  { TInt MxVals(SIn), Vals(SIn);
  IdToOffV.Gen(Vals);
  for (int ValN = 0; ValN < Vals; ValN++) {
    uint64 Offset;  SIn.Load(Offset);  IAssert(Offset < TSizeMx);
    IdToOffV[ValN]=TSize(Offset);
  } }
}
template<class TVal >
TGLib_OLD::TVecPool< TVal >::~TVecPool ( ) [inline]

Definition at line 2819 of file ds.h.

{ if (ValBf != NULL) { delete [] ValBf; } ValBf=NULL; }

Member Function Documentation

template<class TVal >
int TVecPool< TVal >::AddEmptyV ( const int &  ValVLen)

Definition at line 2983 of file ds.h.

                                                {
  if (ValVLen==0){return 0;}
  if (MxVals < Vals+ValVLen){Resize(Vals+max(TSize(ValVLen), GrowBy)); }
  Vals+=ValVLen; IdToOffV.Add(Vals);
  return IdToOffV.Len()-1;
}
template<class TVal >
int TVecPool< TVal >::AddV ( const TValV ValV)

Definition at line 2972 of file ds.h.

                                          {
  const ::TSize ValVLen = ValV.Len();
  if (ValVLen == 0) { return 0; }
  if (MxVals < Vals+ValVLen) { Resize(Vals+max(ValVLen, GrowBy)); }
  if (FastCopy) { memcpy(ValBf+Vals, ValV.BegI(), sizeof(TVal)*ValV.Len()); }
  else { for (uint ValN=0; ValN < ValVLen; ValN++) { ValBf[Vals+ValN]=ValV[ValN]; } }
  Vals+=ValVLen;  IdToOffV.Add(Vals);
  return IdToOffV.Len()-1;
}
template<class TVal >
void TGLib_OLD::TVecPool< TVal >::Clr ( bool  DoDel = true) [inline]

Definition at line 2862 of file ds.h.

                              {
    IdToOffV.Clr(DoDel);  MxVals=0;  Vals=0;
    if (DoDel && ValBf!=NULL) { delete [] ValBf; ValBf=NULL;}
    if (! DoDel) { PutAll(EmptyVal); }
  }
template<class TVal >
void TVecPool< TVal >::CompactPool ( const TVal &  DelVal)

Definition at line 2993 of file ds.h.

                                                   {
  ::TSize TotalDel=0, NDel=0;
  // printf("Compacting %d vectors\n", IdToOffV.Len());
  for (int vid = 1; vid < IdToOffV.Len(); vid++) {
    // if (vid % 10000000 == 0) { printf(" %dm", vid/1000000);  fflush(stdout); }
    const uint Len = GetVLen(vid);
    TVal* ValV = GetValVPt(vid);
    if (TotalDel > 0) { IdToOffV[vid-1] -= TotalDel; } // update end of vector
    if (Len == 0) { continue; }
    NDel = 0;
    for (TVal* v = ValV; v < ValV+Len-NDel; v++) {
      if (*v == DelVal) {
        TVal* Beg = v;
        while (*v == DelVal && v < ValV+Len) { v++; NDel++; }
        memcpy(Beg, v, sizeof(TVal)*int(Len - ::TSize(v - ValV)));
        v -= NDel;
      }
    }
    memcpy(ValV-TotalDel, ValV, sizeof(TVal)*Len);  // move data
    TotalDel += NDel;
  }
  IdToOffV.Last() -= TotalDel;
  for (::TSize i = Vals-TotalDel; i < Vals; i++) { ValBf[i] = EmptyVal; }
  Vals -= TotalDel;
  // printf("  deleted %llu elements from the pool\n", TotalDel);
}
template<class TVal >
const TVal& TGLib_OLD::TVecPool< TVal >::GetEmptyVal ( ) const [inline]

Definition at line 2833 of file ds.h.

{ return EmptyVal; }
template<class TVal >
::TSize TGLib_OLD::TVecPool< TVal >::GetMemUsed ( ) const [inline]

Definition at line 2835 of file ds.h.

                           {
    return sizeof(TCRef)+sizeof(TBool)+3*sizeof(TSize)+sizeof(TVal*)+MxVals*sizeof(TVal);}
template<class TVal >
void TGLib_OLD::TVecPool< TVal >::GetV ( const int &  VId,
TValV ValV 
) const [inline]

Definition at line 2846 of file ds.h.

                                               {
    if (GetVLen(VId)==0){ValV.Clr();}
    else { ValV.GenExt(GetValVPt(VId), GetVLen(VId)); } }
template<class TVal >
::TSize TGLib_OLD::TVecPool< TVal >::GetVals ( ) const [inline]

Definition at line 2828 of file ds.h.

{ return Vals; }
template<class TVal >
TVal* TGLib_OLD::TVecPool< TVal >::GetValVPt ( const int &  VId) const [inline]

Definition at line 2843 of file ds.h.

                                        {
    if (GetVLen(VId)==0){return (TVal*)&EmptyVal;}
    else {return ValBf+IdToOffV[VId-1];}}
template<class TVal >
::TSize TGLib_OLD::TVecPool< TVal >::GetVecs ( ) const [inline]

Definition at line 2829 of file ds.h.

{ return IdToOffV.Len(); }
template<class TVal >
uint TGLib_OLD::TVecPool< TVal >::GetVLen ( const int &  VId) const [inline]

Definition at line 2840 of file ds.h.

                                     {
    if (VId==0){return 0;}
    else {return uint(IdToOffV[VId]-IdToOffV[VId-1]);}}
template<class TVal >
bool TGLib_OLD::TVecPool< TVal >::IsVId ( const int &  VId) const [inline]

Definition at line 2830 of file ds.h.

{ return (0 <= VId) && (VId < IdToOffV.Len()); }
template<class TVal >
static PVecPool TGLib_OLD::TVecPool< TVal >::Load ( TSIn SIn) [inline, static]

Definition at line 2822 of file ds.h.

{ return new TVecPool(SIn); }
template<class TVal >
static PVecPool TGLib_OLD::TVecPool< TVal >::Load ( const TStr FNm) [inline, static]

Definition at line 2823 of file ds.h.

{ TFIn FIn(FNm); return Load(FIn); }
template<class TVal >
static PVecPool TGLib_OLD::TVecPool< TVal >::New ( const ::TSize ExpectVals = 0,
const ::TSize GrowBy = 1000000,
const bool &  FastCopy = false 
) [inline, static]

Definition at line 2820 of file ds.h.

                                                                                                          {
    return new TVecPool(ExpectVals, GrowBy, FastCopy); }
template<class TVal >
TVecPool< TVal > & TVecPool< TVal >::operator= ( const TVecPool< TVal > &  Pool)

Definition at line 2952 of file ds.h.

                                                                {
  if (this!=&Pool) {
    FastCopy = Pool.FastCopy;
    GrowBy = Pool.GrowBy;
    MxVals = Pool.MxVals;
    Vals = Pool.Vals;
    EmptyVal = Pool.EmptyVal;
    IdToOffV=Pool.IdToOffV;
    try { ValBf = new TVal [MxVals]; }
    catch (std::exception Ex) { FailR(TStr::Fmt("TVec::operator= : %s, MxVals: %d", Ex.what(), MxVals).CStr()); }
    IAssert(ValBf != NULL);
    if (FastCopy) {
      memcpy(ValBf, Pool.ValBf, Vals*sizeof(TVal)); }
    else {
      for (uint64 ValN = 0; ValN < Vals; ValN++){ ValBf[ValN] = Pool.ValBf[ValN]; } }
  }
  return *this;
}
template<class TVal >
void TGLib_OLD::TVecPool< TVal >::PutAll ( const TVal &  Val) [inline]

Definition at line 2867 of file ds.h.

                               {
    for (TSize ValN = 0; ValN < MxVals; ValN++) { ValBf[ValN]=Val; } }
template<class TVal >
void TGLib_OLD::TVecPool< TVal >::PutV ( const int &  VId,
const TValV ValV 
) [inline]

Definition at line 2849 of file ds.h.

                                               {
    IAssert(IsVId(VId) && GetVLen(VId) == ValV.Len());
    if (FastCopy) {
      memcpy(GetValVPt(VId), ValV.BegI(), sizeof(TVal)*ValV.Len()); }
    else { TVal* ValPt = GetValVPt(VId);
      for (uint ValN=0; ValN < uint(ValV.Len()); ValN++, ValPt++) { *ValPt=ValV[ValN]; }
    }
  }
template<class TVal >
void TGLib_OLD::TVecPool< TVal >::Reserve ( const ::TSize MxVals) [inline]

Definition at line 2832 of file ds.h.

{ Resize(MxVals); }
template<class TVal >
::TSize TGLib_OLD::TVecPool< TVal >::Reserved ( ) const [inline]

Definition at line 2831 of file ds.h.

{ return MxVals; }
template<class TVal >
void TVecPool< TVal >::Resize ( const ::TSize _MxVals) [private]

Definition at line 2874 of file ds.h.

                                               {
  if (_MxVals <= MxVals){ return; } else { MxVals = _MxVals; }
  if (ValBf == NULL) {
    try { ValBf = new TVal [MxVals]; }
    catch (std::exception Ex) {
      FailR(TStr::Fmt("TVecPool::Resize 1: %s, MxVals: %d. [Program failed to allocate more memory. Solution: Get a bigger machine and a 64-bit compiler.]", Ex.what(), _MxVals).CStr()); }
    IAssert(ValBf != NULL);
    if (EmptyVal != TVal()) { PutAll(EmptyVal); }
  } else {
    // printf("*** Resize vector pool: %llu -> %llu\n", uint64(Vals), uint64(MxVals));
    TVal* NewValBf = NULL;
    try { NewValBf = new TVal [MxVals]; }
    catch (std::exception Ex) { FailR(TStr::Fmt("TVecPool::Resize 2: %s, MxVals: %d. [Program failed to allocate more memory. Solution: Get a bigger machine and a 64-bit compiler.]", Ex.what(), _MxVals).CStr()); }
    IAssert(NewValBf != NULL);
    if (FastCopy) {
      memcpy(NewValBf, ValBf, Vals*sizeof(TVal)); }
    else {
      for (TSize ValN = 0; ValN < Vals; ValN++){ NewValBf[ValN] = ValBf[ValN]; } }
    if (EmptyVal != TVal()) { // init empty values
      for (TSize ValN = Vals; ValN < MxVals; ValN++) { NewValBf[ValN] = EmptyVal; }
    }
    delete [] ValBf;
    ValBf = NewValBf;
  }
}
template<class TVal >
void TVecPool< TVal >::Save ( TSOut SOut) const

Definition at line 2939 of file ds.h.

                                           {
  SOut.Save(FastCopy);
  uint64 _GrowBy=GrowBy, _MxVals=MxVals, _Vals=Vals;
  SOut.Save(_GrowBy); SOut.Save(_MxVals);  SOut.Save(_Vals);
  SOut.Save(EmptyVal);
  for (TSize ValN = 0; ValN < Vals; ValN++) { ValBf[ValN].Save(SOut); }
  { SOut.Save(IdToOffV.Len());  SOut.Save(IdToOffV.Len());
  for (int ValN = 0; ValN < IdToOffV.Len(); ValN++) {
    const uint64 Offset=IdToOffV[ValN];  SOut.Save(Offset);
  } }
}
template<class TVal >
void TGLib_OLD::TVecPool< TVal >::SetEmptyVal ( const TVal &  _EmptyVal) [inline]

Definition at line 2834 of file ds.h.

{ EmptyVal = _EmptyVal; }
template<class TVal >
void TVecPool< TVal >::ShuffleAll ( TRnd Rnd = TInt::Rnd)

Definition at line 3022 of file ds.h.

                                         {
  for (::TSize n = Vals-1; n > 0; n--) {
    const ::TSize k = ::TSize(((uint64(Rnd.GetUniDevInt())<<32) | uint64(Rnd.GetUniDevInt())) % (n+1));
    const TVal Tmp = ValBf[n];
    ValBf[n] = ValBf[k];
    ValBf[k] = Tmp;
  }
}

Friends And Related Function Documentation

template<class TVal >
friend class TPt< TVecPool< TVal > > [friend]

Definition at line 2870 of file ds.h.


Member Data Documentation

template<class TVal >
TCRef TGLib_OLD::TVecPool< TVal >::CRef [private]

Definition at line 2807 of file ds.h.

template<class TVal >
TVal TGLib_OLD::TVecPool< TVal >::EmptyVal [private]

Definition at line 2810 of file ds.h.

template<class TVal >
TBool TGLib_OLD::TVecPool< TVal >::FastCopy [private]

Definition at line 2808 of file ds.h.

template<class TVal >
::TSize TGLib_OLD::TVecPool< TVal >::GrowBy [private]

Definition at line 2809 of file ds.h.

template<class TVal >
TVec< ::TSize> TGLib_OLD::TVecPool< TVal >::IdToOffV [private]

Definition at line 2812 of file ds.h.

template<class TVal >
::TSize TGLib_OLD::TVecPool< TVal >::MxVals [private]

Definition at line 2809 of file ds.h.

template<class TVal >
TVal* TGLib_OLD::TVecPool< TVal >::ValBf [private]

Definition at line 2811 of file ds.h.

template<class TVal >
::TSize TGLib_OLD::TVecPool< TVal >::Vals [private]

Definition at line 2809 of file ds.h.


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