SNAP Library , Developer Reference  2013-01-07 14:03:36
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
dt.h
Go to the documentation of this file.
00001 #include "bd.h"
00002 
00004 // Forward
00005 class TILx;
00006 class TOLx;
00007 ClassHdTP(TXmlTok, PXmlTok);
00008 
00010 // Random
00011 class TRnd{
00012 public:
00013   static const int RndSeed;
00014 private:
00015   static const int a, m, q, r;
00016   int Seed;
00017   int GetNextSeed(){
00018     if ((Seed=a*(Seed%q)-r*(Seed/q))>0){return Seed;} else {return Seed+=m;}}
00019 public:
00020   TRnd(const int& _Seed=1, const int& Steps=0){
00021     PutSeed(_Seed); Move(Steps);}
00022   explicit TRnd(TSIn& SIn){SIn.Load(Seed);}
00023   void Save(TSOut& SOut) const {SOut.Save(Seed);}
00024   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm);
00025   void SaveXml(TSOut& SOut, const TStr& Nm) const;
00026 
00027   TRnd& operator=(const TRnd& Rnd){Seed=Rnd.Seed; return *this;}
00028   bool operator==(const TRnd&) const {Fail; return false;}
00029 
00030   double GetUniDev(){return GetNextSeed()/double(m);}
00031   int GetUniDevInt(const int& Range=0);
00032   int GetUniDevInt(const int& MnVal, const int& MxVal){
00033     IAssert(MnVal<=MxVal); return MnVal+GetUniDevInt(MxVal-MnVal+1);}
00034   uint GetUniDevUInt(const uint& Range=0);
00035   double GetNrmDev();
00036   double GetNrmDev(
00037    const double& Mean, const double& SDev, const double& Mn, const double& Mx);
00038   double GetExpDev();
00039   double GetExpDev(const double& Lambda); // mean=1/lambda
00040   double GetGammaDev(const int& Order);
00041   double GetPoissonDev(const double& Mean);
00042   double GetBinomialDev(const double& Prb, const int& Trials);
00043   int GetGeoDev(const double& Prb){
00044     return 1+(int)floor(log(1.0-GetUniDev())/log(1.0-Prb));}
00045   double GetPowerDev(const double& AlphaSlope){ // power-law degree distribution (AlphaSlope>0)
00046     IAssert(AlphaSlope>1.0);
00047     return pow(1.0-GetUniDev(), -1.0/(AlphaSlope-1.0));}
00048   double GetRayleigh(const double& Sigma) { // 1/sqrt(alpha) = sigma
00049     IAssert(Sigma>0.0);
00050     return Sigma*sqrt(-2*log(1-GetUniDev()));}
00051   double GetWeibull(const double& K, const double& Lambda) { // 1/alpha = lambda
00052     IAssert(Lambda>0.0 && K>0.0);
00053     return Lambda*pow(-log(1-GetUniDev()), 1.0/K);}
00054   //void GetSphereDev(const int& Dim, TFltV& ValV);
00055 
00056   void PutSeed(const int& _Seed);
00057   int GetSeed() const {return Seed;}
00058   void Randomize(){PutSeed(RndSeed);}
00059   void Move(const int& Steps);
00060   bool Check();
00061 
00062   static double GetUniDevStep(const int& Seed, const int& Steps){
00063     TRnd Rnd(Seed); Rnd.Move(Steps); return Rnd.GetUniDev();}
00064   static double GetNrmDevStep(const int& Seed, const int& Steps){
00065     TRnd Rnd(Seed); Rnd.Move(Steps); return Rnd.GetNrmDev();}
00066   static double GetExpDevStep(const int& Seed, const int& Steps){
00067     TRnd Rnd(Seed); Rnd.Move(Steps); return Rnd.GetExpDev();}
00068 
00069   static TRnd LoadTxt(TILx& Lx);
00070   void SaveTxt(TOLx& Lx) const;
00071 };
00072 
00074 // Memory
00075 ClassTP(TMem, PMem)//{
00076 private:
00077   int MxBfL, BfL;
00078   char* Bf;
00079   void Resize(const int& _MxBfL);
00080   bool DoFitLen(const int& LBfL) const {return BfL+LBfL<=MxBfL;}
00081 public:
00082   TMem(const int& _MxBfL=0):
00083     MxBfL(_MxBfL), BfL(0), Bf(NULL){ IAssert(BfL>=0);
00084     if (MxBfL>0){Bf=new char[MxBfL]; IAssert(Bf!=NULL);}}
00085   static PMem New(const int& MxBfL=0){return new TMem(MxBfL);}
00086   TMem(const void* _Bf, const int& _BfL):
00087     MxBfL(_BfL), BfL(_BfL), Bf(NULL){ IAssert(BfL>=0);
00088     if (BfL>0){Bf=new char[BfL]; IAssert(Bf!=NULL); memcpy(Bf, _Bf, BfL);}}
00089   static PMem New(const void* Bf, const int& BfL){return new TMem(Bf, BfL);}
00090   TMem(const TMem& Mem):
00091     MxBfL(Mem.MxBfL), BfL(Mem.BfL), Bf(NULL){
00092     if (MxBfL>0){Bf=new char[MxBfL]; memcpy(Bf, Mem.Bf, BfL);}}
00093   static PMem New(const TMem& Mem){return new TMem(Mem);}
00094   static PMem New(const PMem& Mem){return new TMem(*Mem);}
00095   TMem(const TStr& Str);
00096   static PMem New(const TStr& Str){return new TMem(Str);}
00097   ~TMem(){if (Bf!=NULL){delete[] Bf;}}
00098   explicit TMem(TSIn& SIn){
00099     SIn.Load(MxBfL); SIn.Load(BfL);
00100     Bf=new char[MxBfL=BfL]; SIn.LoadBf(Bf, BfL);}
00101   void Save(TSOut& SOut) const {
00102     SOut.Save(MxBfL); SOut.Save(BfL); SOut.SaveBf(Bf, BfL);}
00103   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm);
00104   void SaveXml(TSOut& SOut, const TStr& Nm) const;
00105 
00106   TMem& operator=(const TMem& Mem){
00107     if (this!=&Mem){
00108       if (Bf!=NULL){delete[] Bf;}
00109       MxBfL=Mem.MxBfL; BfL=Mem.BfL; Bf=NULL;
00110       if (MxBfL>0){Bf=new char[MxBfL]; memcpy(Bf, Mem.Bf, BfL);}}
00111     return *this;}
00112   char* operator()() const {return Bf;}
00113   TMem& operator+=(const char& Ch);
00114   TMem& operator+=(const TMem& Mem);
00115   TMem& operator+=(const TStr& Str);
00116   TMem& operator+=(const PSIn& SIn);
00117   char& operator[](const int& ChN) const {
00118     Assert((0<=ChN)&&(ChN<BfL)); return Bf[ChN];}
00119   int GetMemUsed() const {return 2*sizeof(int)+sizeof(char*)+MxBfL;}
00120 
00121   void Gen(const int& _BfL){
00122     Clr(); Resize(_BfL); BfL=_BfL;}
00123   void GenZeros(const int& _BfL){
00124     Clr(false); Resize(_BfL); BfL=_BfL;
00125     if (BfL > 0) memset(Bf, 0, BfL);}
00126   void Reserve(const int& _MxBfL, const bool& DoClr = true){
00127           if (DoClr){ Clr(); } Resize(_MxBfL);}
00128   void Del(const int& BChN, const int& EChN);
00129   void Clr(const bool& DoDel=true){
00130     if (DoDel){if (Bf!=NULL){delete[] Bf;} MxBfL=0; BfL=0; Bf=NULL;}
00131     else {BfL=0;}}
00132   int Len() const {return BfL;}
00133   bool Empty() const {return BfL==0;}
00134   void Trunc(const int& _BfL){
00135     if ((0<=_BfL)&&(_BfL<=BfL)){BfL=_BfL;}}
00136   void Push(const char& Ch){operator+=(Ch);}
00137   char Pop(){IAssert(BfL>0); BfL--; return Bf[BfL];}
00138 
00139   bool DoFitStr(const TStr& Str) const;
00140   //int AddStr(const TStr& Str);
00141   void AddBf(const void* Bf, const int& BfL);
00142   char* GetBf() const {return Bf;}
00143   TStr GetAsStr(const char& NewNullCh='\0') const;
00144   PSIn GetSIn() const {
00145     TMOut MOut(BfL); MOut.SaveBf(Bf, BfL); return MOut.GetSIn();}
00146 
00147   static void LoadMem(const PSIn& SIn, TMem& Mem){
00148     Mem.Clr(); Mem.Gen(SIn->Len()); SIn->GetBf(Mem.Bf, SIn->Len());}
00149   static void LoadMem(const PSIn& SIn, const PMem& Mem){
00150     Mem->Clr(); Mem->Gen(SIn->Len()); SIn->GetBf(Mem->Bf, SIn->Len());}
00151   void SaveMem(const PSOut& SOut) const {SOut->SaveBf(Bf, Len());}
00152 };
00153 
00155 // Input-Memory
00156 class TMemIn: public TSIn{
00157 private:
00158   PMem Mem;
00159   const char* Bf;
00160   int BfC, BfL;
00161 public:
00162   TMemIn(const TMem& _Mem, const int& _BfC=0);
00163   static PSIn New(const TMem& Mem){
00164     return PSIn(new TMemIn(Mem));}
00165   static PSIn New(const PMem& Mem){
00166     TMemIn* MemIn=new TMemIn(*Mem); MemIn->Mem=Mem; return PSIn(MemIn);}
00167   ~TMemIn(){}
00168 
00169   bool Eof(){return BfC==BfL;}
00170   int Len() const {return BfL-BfC;}
00171   char GetCh(){Assert(BfC<BfL); return Bf[BfC++];}
00172   char PeekCh(){Assert(BfC<BfL); return Bf[BfC];}
00173   int GetBf(const void* LBf, const TSize& LBfL);
00174   void Reset(){Cs=TCs(); BfC=0;}
00175 };
00176 
00178 // Output-Memory
00179 class TMemOut: public TSOut{
00180 private:
00181   PMem Mem;
00182 private:
00183   void FlushBf();
00184 public:
00185   TMemOut(const PMem& _Mem);
00186   static PSOut New(const PMem& Mem){
00187     return new TMemOut(Mem);}
00188   ~TMemOut(){}
00189 
00190   int PutCh(const char& Ch){
00191     Mem->operator+=(Ch); return Ch;}
00192   int PutBf(const void* LBf, const TSize& LBfL);
00193   void Flush(){}
00194 };
00195 
00197 // Char-Array
00198 class TChA{
00199 private:
00200   int MxBfL, BfL;
00201   char* Bf;
00202   void Resize(const int& _MxBfL);
00203 public:
00204   explicit TChA(const int& _MxBfL=256){
00205     Bf=new char[(MxBfL=_MxBfL)+1]; Bf[BfL=0]=0;}
00206   TChA(const char* CStr){
00207     Bf=new char[(MxBfL=BfL=int(strlen(CStr)))+1]; strcpy(Bf, CStr);}
00208   TChA(const char* CStr, const int& StrLen) : MxBfL(StrLen), BfL(StrLen) {
00209     Bf=new char[StrLen+1]; strncpy(Bf, CStr, StrLen); Bf[StrLen]=0;}
00210   TChA(const TChA& ChA){
00211     Bf=new char[(MxBfL=ChA.MxBfL)+1]; BfL=ChA.BfL; strcpy(Bf, ChA.CStr());}
00212   TChA(const TStr& Str);
00213   TChA(const TMem& Mem){
00214     Bf=new char[(MxBfL=BfL=Mem.Len())+1]; Bf[MxBfL]=0;
00215     memcpy(CStr(), Mem(), Mem.Len());}
00216   ~TChA(){delete[] Bf;}
00217   explicit TChA(TSIn& SIn){
00218     SIn.Load(MxBfL); SIn.Load(BfL); SIn.Load(Bf, MxBfL, BfL);}
00219   void Load(TSIn& SIn){ delete[] Bf;
00220     SIn.Load(MxBfL); SIn.Load(BfL); SIn.Load(Bf, MxBfL, BfL);}
00221   void Save(TSOut& SOut, const bool& SaveCompact=true) const { //J:
00222     SOut.Save(SaveCompact?BfL:MxBfL); SOut.Save(BfL); SOut.Save(Bf, BfL);}
00223   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm);
00224   void SaveXml(TSOut& SOut, const TStr& Nm) const;
00225 
00226   TChA& operator=(const TChA& ChA);
00227   TChA& operator=(const TStr& Str);
00228   TChA& operator=(const char* CStr);
00229   bool operator==(const TChA& ChA) const {return strcmp(CStr(), ChA.CStr())==0;}
00230   bool operator==(const char* _CStr) const {return strcmp(CStr(), _CStr)==0;}
00231   bool operator==(const char& Ch) const {return (BfL==1)&&(Bf[0]==Ch);}
00232   bool operator!=(const TChA& ChA) const {return strcmp(CStr(), ChA.CStr())!=0;}
00233   bool operator!=(const char* _CStr) const {return strcmp(CStr(), _CStr)!=0;}
00234   bool operator!=(const char& Ch) const {return !((BfL==1)&&(Bf[0]==Ch));}
00235   bool operator<(const TChA& ChA) const {return strcmp(CStr(), ChA.CStr())<0;}
00236 
00237   TChA& operator+=(const TMem& Mem);
00238   TChA& operator+=(const TChA& ChA);
00239   TChA& operator+=(const TStr& Str);
00240   TChA& operator+=(const char* CStr);
00241   TChA& operator+=(const char& Ch){
00242     if (BfL==MxBfL){Resize(BfL+1);}
00243     Bf[BfL]=Ch; BfL++; Bf[BfL]=0; return *this;}
00244   char operator[](const int& ChN) const {
00245     Assert((0<=ChN)&&(ChN<BfL)); return Bf[ChN];}
00246   char& operator[](const int& ChN){
00247     Assert((0<=ChN)&&(ChN<BfL)); return Bf[ChN];}
00248   int GetMemUsed() const {return 2*sizeof(int)+sizeof(char*)+sizeof(char)*MxBfL;}
00249 
00250   char* operator ()(){return Bf;}
00251   const char* operator ()() const {return Bf;}
00252   char* CStr() {return Bf;}
00253   const char* CStr() const {return Bf;}
00254 
00255   void Clr(){Bf[BfL=0]=0;}
00256   int Len() const {return BfL;}
00257   bool Empty() const {return BfL==0;}
00258   void Ins(const int& BChN, const char* CStr);
00259   void Del(const int& ChN);
00260   void DelLastCh(){Pop();}
00261   void Push(const char& Ch){operator+=(Ch);}
00262   char Pop(){IAssert(BfL>0); BfL--; char Ch=Bf[BfL]; Bf[BfL]=0; return Ch;}
00263   void Trunc();
00264   void Trunc(const int& _BfL){
00265     if ((0<=_BfL)&&(_BfL<=BfL)){Bf[BfL=_BfL]=0;}}
00266   void Reverse();
00267 
00268   void AddCh(const char& Ch, const int& MxLen=-1){
00269     if ((MxLen==-1)||(BfL<MxLen)){operator+=(Ch);}}
00270   void AddChTo(const char& Ch, const int& ToChN){
00271     while (Len()<ToChN){AddCh(Ch);}}
00272   void PutCh(const int& ChN, const char& Ch){
00273     Assert((0<=ChN)&&(ChN<BfL)); Bf[ChN]=Ch;}
00274   char GetCh(const int& ChN) const {return operator[](ChN);}
00275   char LastCh() const { Assert(1<=BfL); return Bf[BfL-1]; }
00276   char LastLastCh() const { Assert(2<=BfL); return Bf[BfL-2]; }
00277 
00278   TChA GetSubStr(const int& BChN, const int& EChN) const;
00279 
00280   int CountCh(const char& Ch, const int& BChN=0) const;
00281   int SearchCh(const char& Ch, const int& BChN=0) const;
00282   int SearchChBack(const char& Ch, int BChN=-1) const;
00283   int SearchStr(const TChA& Str, const int& BChN=0) const;
00284   int SearchStr(const TStr& Str, const int& BChN=0) const;
00285   int SearchStr(const char* CStr, const int& BChN=0) const;
00286   bool IsStrIn(const TStr& Str) const {return SearchStr(Str)!=-1;}
00287   bool IsPrefix(const char* CStr, const int& BChN=0) const;
00288   bool IsPrefix(const TStr& Str) const;
00289   bool IsPrefix(const TChA& Str) const;
00290   bool IsSuffix(const char* CStr) const;
00291   bool IsSuffix(const TStr& Str) const;
00292   bool IsSuffix(const TChA& Str) const;
00293 
00294   bool IsChIn(const char& Ch) const {return SearchCh(Ch)!=-1;}
00295   void ChangeCh(const char& SrcCh, const char& DstCh);
00296   TChA& ToUc();
00297   TChA& ToLc();
00298   TChA& ToTrunc();
00299   void CompressWs();
00300   void Swap(const int& ChN1, const int& ChN2);
00301   void Swap(TChA& ChA);
00302 
00303   int GetPrimHashCd() const;
00304   int GetSecHashCd() const;
00305 
00306   static void LoadTxt(const PSIn& SIn, TChA& ChA);
00307   void SaveTxt(const PSOut& SOut) const;
00308   
00309   //friend TChA operator+(const TChA& LStr, const TChA& RStr);
00310   //friend TChA operator+(const TChA& LStr, const TStr& RStr);
00311   //friend TChA operator+(const TChA& LStr, const char* RCStr);
00312 };
00313 
00315 // Input-Char-Array
00316 class TChAIn: public TSIn{
00317 private:
00318   const char* Bf;
00319   int BfC, BfL;
00320 private:
00321   TChAIn();
00322   TChAIn(const TChAIn&);
00323   TChAIn& operator=(const TChAIn&);
00324 public:
00325   TChAIn(const TChA& ChA, const int& _BfC=0);
00326   static PSIn New(const TChA& ChA){return PSIn(new TChAIn(ChA));}
00327   ~TChAIn(){}
00328 
00329   bool Eof(){return BfC==BfL;}
00330   int Len() const {return BfL-BfC;}
00331   char GetCh(){Assert(BfC<BfL); return Bf[BfC++];}
00332   char PeekCh(){Assert(BfC<BfL); return Bf[BfC];}
00333   int GetBf(const void* LBf, const TSize& LBfL);
00334   void Reset(){Cs=TCs(); BfC=0;}
00335 };
00336 
00338 // Ref-String
00339 class TRStr{
00340 public:
00341   char* Bf;
00342   int Refs;
00343 public:
00344   TRStr(){Refs=1; Bf=new char[0+1]; Bf[0]=0;}
00345   TRStr(const int& Len){
00346     IAssert(Len>=0); Refs=0; Bf=new char[Len+1]; Bf[Len]=0;}
00347   TRStr(const char* CStr){
00348     Refs=0; Bf=new char[strlen(CStr)+1]; strcpy(Bf, CStr);}
00349   TRStr(const char* CStr, const int& MxLen){
00350     Refs=0; Bf=new char[MxLen+1]; strncpy(Bf, CStr, MxLen); Bf[MxLen]=0;}
00351   TRStr(const char* CStr1, const char* CStr2){
00352     Refs=0; int CStr1Len=int(strlen(CStr1)); Bf=new char[CStr1Len+int(strlen(CStr2))+1];
00353     strcpy(Bf, CStr1); strcpy(Bf+CStr1Len, CStr2);}
00354   TRStr(const char& Ch){
00355     Refs=0; Bf=new char[1+1]; Bf[0]=Ch; Bf[1]=0;}
00356   TRStr(const char& Ch1, const char& Ch2){
00357     Refs=0; Bf=new char[2+1]; Bf[0]=Ch1; Bf[1]=Ch2; Bf[2]=0;}
00358   ~TRStr(){
00359     Assert(((this!=GetNullRStr())&&(Refs==0))||((this==GetNullRStr())&&(Refs==1)));
00360     delete[] Bf;}
00361   explicit TRStr(TSIn& SIn, const bool& IsSmall){
00362     if (IsSmall){Refs=0; SIn.Load(Bf);}
00363     else {Refs=0; int BfL; SIn.Load(BfL); SIn.Load(Bf, BfL, BfL);}}
00364   void Save(TSOut& SOut, const bool& IsSmall) const {
00365     if (IsSmall){SOut.Save(Bf);}
00366     else {int BfL=int(strlen(Bf)); SOut.Save(BfL); SOut.Save(Bf, BfL);}}
00367 
00368   TRStr& operator=(const TRStr&){Fail; return *this;}
00369   int GetMemUsed() const {return int(sizeof(int))+int(strlen(Bf));}
00370 
00371   void MkRef(){Refs++;}
00372   void UnRef(){Assert(Refs>0); if (--Refs==0){delete this;}}
00373 
00374   const char* CStr() const {return Bf;}
00375   char* CStr() {return Bf;}
00376   bool Empty() const {return Bf[0]==0;}
00377   int Len() const {return int(strlen(Bf));}
00378 
00379   void PutCh(const int& ChN, const char& Ch){
00380     Assert((0<=ChN)&&(ChN<Len())); Bf[ChN]=Ch;}
00381   char GetCh(const int& ChN) const {
00382     Assert((0<=ChN)&&(ChN<Len())); return Bf[ChN];}
00383 
00384   bool IsUc() const;
00385   void ToUc();
00386   bool IsLc() const;
00387   void ToLc();
00388   void ToCap();
00389   void ConvUsFromYuAscii();
00390   static int CmpI(const char* CStr1, const char* CStr2);
00391 
00392   int GetPrimHashCd() const;
00393   int GetSecHashCd() const;
00394 
00395   static TRStr* GetNullRStr(){
00396     static TRStr NullRStr; Assert(NullRStr.Bf!=NULL); return &NullRStr;}
00397 };
00398 
00400 // String
00401 class TStr;
00402 template <class TVal> class TVec;
00403 typedef TVec<TStr> TStrV;
00404 
00405 class TStr{
00406 private:
00407   TRStr* RStr;
00408 private:
00409   TStr(const char& Ch, bool){
00410     RStr=new TRStr(Ch); RStr->MkRef();}
00411   TStr(const char& Ch1, const char& Ch2, bool){
00412     RStr=new TRStr(Ch1, Ch2); RStr->MkRef();}
00413   static TRStr* GetRStr(const char* CStr);
00414   void Optimize();
00415 public:
00416   TStr(){RStr=TRStr::GetNullRStr(); RStr->MkRef();}
00417   TStr(const TStr& Str){RStr=Str.RStr; RStr->MkRef();}
00418   TStr(const TChA& ChA){RStr=GetRStr(ChA.CStr()); RStr->MkRef();}
00419   TStr(const TSStr& SStr){RStr=GetRStr(SStr.CStr()); RStr->MkRef();}
00420   TStr(const char* CStr){RStr=GetRStr(CStr); RStr->MkRef();}
00421   explicit TStr(const char& Ch){RStr=GetChStr(Ch).RStr; RStr->MkRef();}
00422   TStr(const TMem& Mem){
00423     RStr=new TRStr(Mem.Len()); RStr->MkRef();
00424     memcpy(CStr(), Mem(), Mem.Len()); Optimize();}
00425   explicit TStr(const PSIn& SIn){
00426     int SInLen=SIn->Len(); RStr=new TRStr(SInLen); RStr->MkRef();
00427     SIn->GetBf(CStr(), SInLen); Optimize();}
00428   ~TStr(){RStr->UnRef();}
00429   explicit TStr(TSIn& SIn, const bool& IsSmall=false):
00430     RStr(new TRStr(SIn, IsSmall)){RStr->MkRef(); Optimize();}
00431   void Load(TSIn& SIn, const bool& IsSmall=false){
00432     *this=TStr(SIn, IsSmall);}
00433   void Save(TSOut& SOut, const bool& IsSmall=false) const {
00434     RStr->Save(SOut, IsSmall);}
00435   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm);
00436   void SaveXml(TSOut& SOut, const TStr& Nm) const;
00437 
00438   TStr& operator=(const TStr& Str){
00439     if (this!=&Str){RStr->UnRef(); RStr=Str.RStr; RStr->MkRef();} return *this;}
00440   TStr& operator=(const TChA& ChA){
00441     RStr->UnRef(); RStr=GetRStr(ChA.CStr()); RStr->MkRef(); return *this;}
00442   TStr& operator=(const char* CStr){
00443     RStr->UnRef(); RStr=GetRStr(CStr); RStr->MkRef(); return *this;}
00444   TStr& operator=(const char& Ch){
00445     RStr->UnRef(); RStr=GetChStr(Ch).RStr; RStr->MkRef(); return *this;}
00446   TStr& operator+=(const TStr& Str){
00447     TRStr* NewRStr=new TRStr(RStr->CStr(), Str.RStr->CStr());
00448     RStr->UnRef(); RStr=NewRStr; RStr->MkRef();
00449     Optimize(); return *this;}
00450   TStr& operator+=(const char* CStr){
00451     TRStr* NewRStr=new TRStr(RStr->CStr(), CStr);
00452     RStr->UnRef(); RStr=NewRStr; RStr->MkRef();
00453     Optimize(); return *this;}
00454   bool operator==(const TStr& Str) const {
00455     return (RStr==Str.RStr)||(strcmp(RStr->CStr(), Str.RStr->CStr())==0);}
00456   bool operator==(const char* CStr) const {
00457     return strcmp(RStr->CStr(), CStr)==0;}
00458 //  bool operator!=(const TStr& Str) const {
00459 //    return strcmp(RStr->CStr(), Str.RStr->CStr())!=0;}
00460   bool operator!=(const char* CStr) const {
00461     return strcmp(RStr->CStr(), CStr)!=0;}
00462   bool operator<(const TStr& Str) const {
00463     return strcmp(RStr->CStr(), Str.RStr->CStr())<0;}
00464   char operator[](const int& ChN) const {return RStr->GetCh(ChN);}
00465   int GetMemUsed() const {return sizeof(TRStr*)+RStr->GetMemUsed();}
00466 
00467   char* operator()(){return RStr->CStr();}
00468   const char* operator()() const {return RStr->CStr();}
00469   char* CStr() {return RStr->CStr();}
00470   const char* CStr() const {return RStr->CStr();}
00471 
00472   void PutCh(const int& ChN, const char& Ch){
00473     TRStr* NewRStr=new TRStr(RStr->CStr());
00474     RStr->UnRef(); RStr=NewRStr; RStr->MkRef();
00475     RStr->PutCh(ChN, Ch); Optimize();}
00476   char GetCh(const int& ChN) const {return RStr->GetCh(ChN);}
00477   char LastCh() const {return GetCh(Len()-1);}
00478 
00479   void Clr(){RStr->UnRef(); RStr=TRStr::GetNullRStr(); RStr->MkRef();}
00480   int Len() const {return RStr->Len();}
00481   bool Empty() const {return RStr->Empty();}
00482 
00483   // upper-case
00484   bool IsUc() const {return RStr->IsUc();}
00485   TStr& ToUc();
00486   TStr GetUc() const {return TStr(*this).ToUc();}
00487   int CmpI(const TStr& Str) const {return TRStr::CmpI(CStr(), Str.CStr());}
00488   bool EqI(const TStr& Str) const {return TRStr::CmpI(CStr(), Str.CStr())==0;}
00489   // lower-case
00490   bool IsLc() const {return RStr->IsLc();}
00491   TStr& ToLc();
00492   TStr GetLc() const {return TStr(*this).ToLc();}
00493   // capitalize
00494   TStr& ToCap();
00495   TStr GetCap() const {return TStr(*this).ToCap();}
00496 
00497   // truncate
00498   TStr& ToTrunc();
00499   TStr GetTrunc() const {return TStr(*this).ToTrunc();}
00500   // Yu-Ascii to Us-Ascii
00501   TStr& ConvUsFromYuAscii();
00502   TStr GetUsFromYuAscii() const {return TStr(*this).ConvUsFromYuAscii();}
00503   // hex
00504   TStr& ToHex();
00505   TStr GetHex() const {return TStr(*this).ToHex();}
00506   TStr& FromHex();
00507   TStr GetFromHex() const {return TStr(*this).FromHex();}
00508 
00509   TStr GetSubStr(const int& BChN, const int& EChN) const;
00510   TStr GetSubStr(const int& BChN) const { return GetSubStr(BChN, Len()-1); }
00511   void InsStr(const int& BChN, const TStr& Str);
00512   void DelChAll(const char& Ch);
00513   void DelSubStr(const int& BChN, const int& EChN);
00514   bool DelStr(const TStr& Str);
00515   TStr LeftOf(const char& SplitCh) const;
00516   TStr LeftOfLast(const char& SplitCh) const;
00517   TStr RightOf(const char& SplitCh) const;
00518   TStr RightOfLast(const char& SplitCh) const;
00519   void SplitOnCh(TStr& LStr, const char& SplitCh, TStr& RStr) const;
00520   void SplitOnLastCh(TStr& LStr, const char& SplitCh, TStr& RStr) const;
00521   void SplitOnAllCh(
00522    const char& SplitCh, TStrV& StrV, const bool& SkipEmpty=true) const;
00523   void SplitOnAllAnyCh(
00524    const TStr& SplitChStr, TStrV& StrV, const bool& SkipEmpty=true) const;
00525   void SplitOnWs(TStrV& StrV) const;
00526   void SplitOnNonAlNum(TStrV& StrV) const;
00527   void SplitOnStr(const TStr& SplitStr, TStrV& StrV) const;
00528   void SplitOnStr(TStr& LeftStr, const TStr& MidStr, TStr& RightStr) const;
00529 
00530   //TStr Left(const int& Chs) const { return Chs>0 ? GetSubStr(0, Chs-1) : GetSubStr(0, Len()-Chs-1);}
00531   //TStr Right(const int& Chs) const {return GetSubStr(Len()-Chs, Len()-1);}
00532   TStr Mid(const int& BChN, const int& Chs) const { return GetSubStr(BChN, BChN+Chs-1); }
00533   TStr Mid(const int& BChN) const {return GetSubStr(BChN, Len()-1); }
00534   //TStr Slice(const int& BChN, const int& EChNP1) const {return GetSubStr(BChN, EChNP1-1);}
00535   //TStr operator()(const int& BChN, const int& EChNP1) const {return Slice(BChN, EChNP1);}
00536   //J: as in python or matlab: 1 is 1st character, -1 is last character
00537   // TODO ROK, ask Jure about this comment
00538   TStr Left(const int& EChN) const { return EChN>0 ? GetSubStr(0, EChN-1) : GetSubStr(0, Len()+EChN-1);}
00539   TStr Right(const int& BChN) const {return BChN>=0 ? GetSubStr(BChN, Len()-1) : GetSubStr(Len()+BChN, Len()-1);}
00540   TStr Slice(int BChN, int EChNP1) const { if(BChN<0){BChN=Len()+BChN;} if(EChNP1<=0){EChNP1=Len()+EChNP1;} return GetSubStr(BChN, EChNP1-1); }
00541   TStr operator()(const int& BChN, const int& EChNP1) const {return Slice(BChN, EChNP1);}
00542 
00543   int CountCh(const char& Ch, const int& BChN=0) const;
00544   int SearchCh(const char& Ch, const int& BChN=0) const;
00545   int SearchChBack(const char& Ch, int BChN=-1) const;
00546   int SearchStr(const TStr& Str, const int& BChN=0) const;
00547   bool IsChIn(const char& Ch) const {return SearchCh(Ch)!=-1;}
00548   bool IsStrIn(const TStr& Str) const {return SearchStr(Str)!=-1;}
00549   bool IsPrefix(const char *Str) const;
00550   bool IsPrefix(const TStr& Str) const {
00551     return IsPrefix(Str.CStr());}
00552   bool IsSuffix(const char *Str) const;
00553   bool IsSuffix(const TStr& Str) const {
00554     return IsSuffix(Str.CStr());}
00555 
00556   int ChangeCh(const char& SrcCh, const char& DstCh, const int& BChN=0);
00557   int ChangeChAll(const char& SrcCh, const char& DstCh);
00558   int ChangeStr(const TStr& SrcStr, const TStr& DstStr, const int& BChN=0);
00559   int ChangeStrAll(const TStr& SrcStr, const TStr& DstStr, const bool& FromStartP=false);
00560   TStr Reverse() const {
00561     TChA ChA(*this); ChA.Reverse(); return ChA;}
00562 
00563   int GetPrimHashCd() const {return RStr->GetPrimHashCd();}
00564   int GetSecHashCd() const {return RStr->GetSecHashCd();}
00565 
00566   bool IsBool(bool& Val) const;
00567 
00568   bool IsInt(
00569    const bool& Check, const int& MnVal, const int& MxVal, int& Val) const;
00570   bool IsInt(int& Val) const {return IsInt(false, 0, 0, Val);}
00571   bool IsInt() const {int Val; return IsInt(false, 0, 0, Val);}
00572   int GetInt() const {int Val; IAssertR(IsInt(false, 0, 0, Val), *this); return Val;}
00573   int GetInt(const int& DfVal) const {
00574     int Val; if (IsInt(false, 0, 0, Val)){return Val;} else {return DfVal;}}
00575 
00576   bool IsUInt(
00577    const bool& Check, const uint& MnVal, const uint& MxVal, uint& Val) const;
00578   bool IsUInt(uint& Val) const {return IsUInt(false, 0, 0, Val);}
00579   bool IsUInt() const {uint Val; return IsUInt(false, 0, 0, Val);}
00580   uint GetUInt() const {uint Val; IAssert(IsUInt(false, 0, 0, Val)); return Val;}
00581   uint GetUInt(const uint& DfVal) const {
00582     uint Val; if (IsUInt(false, 0, 0, Val)){return Val;} else {return DfVal;}}
00583 
00584   bool IsInt64(
00585    const bool& Check, const int64& MnVal, const int64& MxVal, int64& Val) const;
00586   bool IsInt64(int64& Val) const {return IsInt64(false, 0, 0, Val);}
00587   bool IsInt64() const {int64 Val; return IsInt64(false, 0, 0, Val);}
00588   int64 GetInt64() const {
00589     int64 Val; IAssert(IsInt64(false, 0, 0, Val)); return Val;}
00590   int64 GetInt64(const int64& DfVal) const {
00591     int64 Val; if (IsInt64(false, 0, 0, Val)){return Val;} else {return DfVal;}}
00592 
00593   bool IsUInt64(
00594    const bool& Check, const uint64& MnVal, const uint64& MxVal, uint64& Val) const;
00595   bool IsUInt64(uint64& Val) const {return IsUInt64(false, 0, 0, Val);}
00596   bool IsUInt64() const {uint64 Val; return IsUInt64(false, 0, 0, Val);}
00597   uint64 GetUInt64() const {
00598     uint64 Val; IAssert(IsUInt64(false, 0, 0, Val)); return Val;}
00599   uint64 GetUInt64(const uint64& DfVal) const {
00600     uint64 Val; if (IsUInt64(false, 0, 0, Val)){return Val;} else {return DfVal;}}
00601 
00602   bool IsHexInt(const bool& Check, const int& MnVal, const int& MxVal, int& Val) const;
00603   bool IsHexInt(int& Val) const {return IsHexInt(false, 0, 0, Val);}
00604   bool IsHexInt() const {int Val; return IsHexInt(false, 0, 0, Val);}
00605   int GetHexInt() const {
00606     int Val; IAssert(IsHexInt(false, 0, 0, Val)); return Val;}
00607   int GetHexInt(const int& DfVal) const {
00608     int Val; if (IsHexInt(false, 0, 0, Val)){return Val;} else {return DfVal;}}
00609 
00610   bool IsHexInt64(const bool& Check, const int64& MnVal, const int64& MxVal, int64& Val) const;
00611   bool IsHexInt64(int64& Val) const {return IsHexInt64(false, 0, 0, Val);}
00612   bool IsHexInt64() const {int64 Val; return IsHexInt64(false, 0, 0, Val);}
00613   int64 GetHexInt64() const {
00614     int64 Val; IAssert(IsHexInt64(false, 0, 0, Val)); return Val;}
00615   int64 GetHexInt64(const int64& DfVal) const {
00616     int64 Val; if (IsHexInt64(false, 0, 0, Val)){return Val;} else {return DfVal;}}
00617 
00618   bool IsFlt(const bool& Check, const double& MnVal, const double& MxVal,
00619    double& Val, const char& DecDelimCh='.') const;
00620   bool IsFlt(double& Val) const {return IsFlt(false, 0, 0, Val);}
00621   bool IsFlt() const {double Val; return IsFlt(false, 0, 0, Val);}
00622   double GetFlt() const {
00623     double Val; IAssert(IsFlt(false, 0, 0, Val)); return Val;}
00624   double GetFlt(const double& DfVal) const {
00625     double Val; if (IsFlt(false, 0, 0, Val)){return Val;} else {return DfVal;}}
00626 
00627   bool IsWord(const bool& WsPrefixP=true, const bool& FirstUcAllowedP=true) const;
00628   bool IsWs() const;
00629 
00630   bool IsWcMatch(
00631    const int& StrBChN, const TStr& WcStr, const int& WcStrBChN, TStrV& StarStrV,
00632    const char& StarCh='*', const char& QuestCh='?') const;
00633   bool IsWcMatch(
00634    const TStr& WcStr, TStrV& StarStrV,
00635    const char& StarCh='*', const char& QuestCh='?') const;
00636   bool IsWcMatch(const TStr& WcStr, const char& StarCh, const char& QuestCh) const;
00637   bool IsWcMatch(const TStr& WcStr, const int& StarStrN, TStr& StarStr) const;
00638   bool IsWcMatch(const TStr& WcStr) const;
00639   TStr GetWcMatch(const TStr& WcStr, const int& StarStrN=0) const;
00640 
00641   TStr GetFPath() const;
00642   TStr GetFBase() const;
00643   TStr GetFMid() const;
00644   TStr GetFExt() const;
00645   static TStr GetNrFPath(const TStr& FPath);
00646   static TStr GetNrFMid(const TStr& FMid);
00647   static TStr GetNrFExt(const TStr& FExt);
00648   static TStr GetNrNumFExt(const int& FExtN);
00649   static TStr GetNrFNm(const TStr& FNm);
00650   static TStr GetNrAbsFPath(const TStr& FPath, const TStr& BaseFPath=TStr());
00651   static bool IsAbsFPath(const TStr& FPath);
00652   static TStr PutFExt(const TStr& FNm, const TStr& FExt);
00653   static TStr PutFExtIfEmpty(const TStr& FNm, const TStr& FExt);
00654   static TStr PutFBase(const TStr& FNm, const TStr& FBase);
00655   static TStr PutFBaseIfEmpty(const TStr& FNm, const TStr& FBase);
00656   static TStr AddToFMid(const TStr& FNm, const TStr& ExtFMid);
00657   static TStr GetNumFNm(const TStr& FNm, const int& Num);
00658   static TStr GetFNmStr(const TStr& Str, const bool& AlNumOnlyP=true);
00659 
00660   static TStr LoadTxt(const PSIn& SIn){
00661     return TStr(SIn);}
00662   static TStr LoadTxt(const TStr& FNm){
00663     PSIn SIn=TFIn::New(FNm); return LoadTxt(SIn);}
00664   void SaveTxt(const PSOut& SOut) const {
00665     SOut->SaveBf(CStr(), Len());}
00666   void SaveTxt(const TStr& FNm) const {
00667     PSOut SOut=TFOut::New(FNm); SaveTxt(SOut);}
00668 
00669   static TStr& GetChStr(const char& Ch);
00670   static TStr& GetDChStr(const char& Ch1, const char& Ch2);
00671 
00672   TStr GetStr() const {return *this;}
00673   static TStr GetStr(const TStr& Str, const char* FmtStr);
00674   static TStr GetStr(const TStr& Str, const TStr& FmtStr){
00675     return GetStr(Str, FmtStr.CStr());}
00676   static TStr GetStr(const TStrV& StrV, const TStr& DelimiterStr);
00677   static TStr Fmt(const char *FmtStr, ...);
00678   static TStr GetSpaceStr(const int& Spaces);
00679   char* GetCStr() const {
00680     char* Bf=new char[Len()+1]; strcpy(Bf, CStr()); return Bf;}
00681 
00682   static TStr MkClone(const TStr& Str){return TStr(Str.CStr());}
00683   static TStr GetNullStr();
00684 
00685   friend TStr operator+(const TStr& LStr, const TStr& RStr);
00686   friend TStr operator+(const TStr& LStr, const char* RCStr);
00687 };
00688 
00690 // Input-String
00691 class TStrIn: public TSIn{
00692 private:
00693   TStr Str;
00694   char* Bf;
00695   int BfC, BfL;
00696 private:
00697   TStrIn();
00698   TStrIn(const TStrIn&);
00699   TStrIn& operator = (const TStrIn&);
00700 public:
00701   TStrIn(const TStr& _Str);
00702   static PSIn New(const TStr& Str){return PSIn(new TStrIn(Str));}
00703   ~TStrIn(){}
00704 
00705   bool Eof(){return BfC==BfL;}
00706   int Len() const {return BfL-BfC;}
00707   char GetCh(){Assert(BfC<BfL); return Bf[BfC++];}
00708   char PeekCh(){Assert(BfC<BfL); return Bf[BfC];}
00709   int GetBf(const void* LBf, const TSize& LBfL);
00710   void Reset(){Cs=TCs(); BfC=0;}
00711 };
00712 
00714 // Double-String
00715 class TDbStr{
00716 public:
00717   TStr Str1;
00718   TStr Str2;
00719 public:
00720   TDbStr(): Str1(), Str2(){}
00721   TDbStr(const TDbStr& DbStr): Str1(DbStr.Str1), Str2(DbStr.Str2){}
00722   TDbStr(const TStr& _Str1): Str1(_Str1), Str2(){}
00723   TDbStr(const TStr& _Str1, const TStr& _Str2): Str1(_Str1), Str2(_Str2){}
00724   explicit TDbStr(TSIn& SIn): Str1(SIn), Str2(SIn){}
00725   void Save(TSOut& SOut) const {Str1.Save(SOut); Str2.Save(SOut);}
00726 
00727   TDbStr& operator=(const TDbStr& DbStr){
00728     if (this!=&DbStr){Str1=DbStr.Str1; Str2=DbStr.Str2;} return *this;}
00729   bool operator==(const TDbStr& DbStr) const {
00730     return (Str1==DbStr.Str1)&&(Str2==DbStr.Str2);}
00731   bool operator<(const TDbStr& DbStr) const {
00732     return (Str1<DbStr.Str1)||((Str1==DbStr.Str1)&&(Str2<DbStr.Str2));}
00733 
00734   TStr GetStr(const TStr& MidStr=TStr()) const {
00735     if (Filled()){return Str1+MidStr+Str2;} else {return Str1+Str2;}}
00736   int GetPrimHashCd() const {
00737     return Str1.GetPrimHashCd()+Str2.GetPrimHashCd();}
00738   int GetSecHashCd() const {
00739     return Str1.GetSecHashCd()+Str2.GetSecHashCd();}
00740 
00741   bool Empty() const {return (Str1.Empty())&&(Str2.Empty());}
00742   bool Filled() const {return (!Str2.Empty())&&(!Str1.Empty());}
00743 };
00744 
00746 // Simple-String-Pool
00747 //ClassTP(TSStrPool, PSStrPool)//{
00748 //private:
00749 //  TMem Bf;
00750 //public:
00751 //  TSStrPool(const int& MxLen=0): Bf(MxLen){}
00752 //  TSStrPool(TSStrPool& StrPool): Bf(StrPool.Bf){}
00753 //  TSStrPool(TSIn& SIn): Bf(SIn){}
00754 //  void Save(TSOut& SOut) const {Bf.Save(SOut);}
00755 //
00756 //  TSStrPool& operator=(const TSStrPool& StrPool){
00757 //    Bf=StrPool.Bf; return *this;}
00758 //
00759 //  int Len() const {return Bf.Len();}
00760 //  void Clr(){Bf.Clr();}
00761 //  int AddStr(const TStr& Str){
00762 //    if (Str.Empty()){return -1;}
00763 //    else {int StrId=Bf.Len(); Bf+=Str; Bf+=char(0); return StrId;}}
00764 //  TStr GetStr(const int& StrId) const {
00765 //    if (StrId==-1){return "";}
00766 //    else {return TStr(Bf()+StrId);}}
00767 //};
00768 
00770 // String-Pool
00771 ClassTP(TStrPool, PStrPool)//{
00772 private:
00773   uint MxBfL, BfL, GrowBy;
00774   char *Bf;
00775 private:
00776   void Resize(const uint& _MxBfL);
00777 public:
00778   TStrPool(const uint& MxBfLen = 0, const uint& _GrowBy = 16*1024*1024);
00779   TStrPool(TSIn& SIn, bool LoadCompact = true);
00780   TStrPool(const TStrPool& Pool) : MxBfL(Pool.MxBfL), BfL(Pool.BfL), GrowBy(Pool.GrowBy) {
00781     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); }
00782   ~TStrPool() { if (Bf) free(Bf); else IAssertR(MxBfL == 0, TStr::Fmt("size: %u, expected size: 0", MxBfL).CStr());  Bf = 0; MxBfL = 0; BfL = 0; }
00783 
00784   static PStrPool New(const uint& _MxBfLen = 0, const uint& _GrowBy = 16*1024*1024) { return PStrPool(new TStrPool(_MxBfLen, _GrowBy)); }
00785   static PStrPool New(TSIn& SIn) { return new TStrPool(SIn); }
00786   static PStrPool New(const TStr& fileName) { PSIn SIn = TFIn::New(fileName); return new TStrPool(*SIn); }
00787   static PStrPool Load(TSIn& SIn, bool LoadCompacted = true) { return PStrPool(new TStrPool(SIn, LoadCompacted)); }
00788   void Save(TSOut& SOut) const;
00789   void Save(const TStr& FNm){PSOut SOut=TFOut::New(FNm); Save(*SOut);}
00790 
00791   uint Len() const { return BfL; }
00792   uint Size() const { return MxBfL; }
00793   bool Empty() const { return ! Len(); }
00794   char* operator () () const { return Bf; }
00795   TStrPool& operator = (const TStrPool& Pool);
00796 
00797   uint AddStr(const char *Str, const uint& Len);
00798   uint AddStr(const char *Str) { return AddStr(Str, uint(strlen(Str)) + 1); }
00799   uint AddStr(const TStr& Str) { return AddStr(Str.CStr(), Str.Len() + 1); }
00800 
00801   TStr GetStr(const uint& Offset) const { Assert(Offset < BfL);
00802     if (Offset == 0) return TStr::GetNullStr(); else return TStr(Bf + Offset); }
00803   const char *GetCStr(const uint& Offset) const { Assert(Offset < BfL);
00804     if (Offset == 0) return TStr::GetNullStr().CStr(); else return Bf + Offset; }
00805 
00806   // Clr() removes the empty string at the start.
00807   // Call AddStr("") after Clr(), if you want to use the pool again.
00808   void Clr(bool DoDel = false) { BfL = 0; if (DoDel && Bf) { free(Bf); Bf = 0; MxBfL = 0; } }
00809   int Cmp(const uint& Offset, const char *Str) const { Assert(Offset < BfL);
00810     if (Offset != 0) return strcmp(Bf + Offset, Str); else return strcmp("", Str); }
00811 
00812   static int GetPrimHashCd(const char *CStr);
00813   static int GetSecHashCd(const char *CStr);
00814   int GetPrimHashCd(const uint& Offset) { Assert(Offset < BfL);
00815     if (Offset != 0) return GetPrimHashCd(Bf + Offset); else return GetPrimHashCd(""); }
00816   int GetSecHashCd(const uint& Offset) { Assert(Offset < BfL);
00817     if (Offset != 0) return GetSecHashCd(Bf + Offset); else return GetSecHashCd(""); }
00818 };
00819 
00821 // String-Pool-64bit
00822 ClassTP(TStrPool64, PStrPool64)//{
00823 private:
00824   ::TSize MxBfL, BfL, GrowBy;
00825   char *Bf;
00826 private:
00827   void Resize(const ::TSize& _MxBfL);
00828 public:
00829   TStrPool64(::TSize _MxBfL = 0, ::TSize _GrowBy = 16*1024*1024);
00830   TStrPool64(const TStrPool64& StrPool);
00831   TStrPool64(TSIn& SIn, bool LoadCompact = true);
00832   ~TStrPool64() { Clr(true); }
00833   void Save(TSOut& SOut) const;
00834 
00835   static PStrPool64 New(::TSize MxBfL = 0, ::TSize GrowBy = 16*1024*1024) { 
00836       return PStrPool64(new TStrPool64(MxBfL, GrowBy)); }
00837   static PStrPool64 Load(TSIn& SIn, bool LoadCompact = true) { 
00838       return PStrPool64(new TStrPool64(SIn, LoadCompact)); }
00839 
00840   TStrPool64& operator=(const TStrPool64& StrPool);
00841 
00842   uint64 GetMemUsed() const { return 3*sizeof(::TSize) + uint64(MxBfL); }
00843 
00844   bool Empty() const { return (BfL == 0); }
00845   uint64 Len() const {return BfL;}
00846   uint64 Reserved() const { return MxBfL; }
00847   void Clr(bool DoDel = false);
00848   int Cmp(uint64 Offset, const char *Str) const { Assert(Offset < BfL);
00849     if (Offset != 0) return strcmp(Bf + Offset, Str); else return strcmp("", Str); }
00850 
00851   uint64 AddStr(const TStr& Str);
00852   TStr GetStr(const uint64& StrId) const;
00853 };
00854 
00856 // Void
00857 class TVoid{
00858 public:
00859   TVoid(){}
00860   TVoid(TSIn&){}
00861   void Save(TSOut&) const {}
00862   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm);
00863   void SaveXml(TSOut& SOut, const TStr& Nm) const;
00864 
00865   TVoid& operator=(const TVoid&){return *this;}
00866   bool operator==(const TVoid&) const {return true;}
00867   bool operator<(const TVoid&) const {Fail; return false;}
00868   int GetMemUsed() const {return sizeof(TVoid);}
00869 };
00870 
00872 // Boolean
00873 class TBool{
00874 public:
00875   bool Val;
00876 public:
00877   static const bool Mn;
00878   static const bool Mx;
00879   static const int Vals;
00880   static TRnd Rnd;
00881 
00882   static const TStr FalseStr;
00883   static const TStr TrueStr;
00884   static const TStr NStr;
00885   static const TStr YStr;
00886   static const TStr NoStr;
00887   static const TStr YesStr;
00888 
00889   TBool(): Val(false){}
00890   TBool(const bool& _Val): Val(_Val){}
00891   operator bool() const {return Val;}
00892   explicit TBool(TSIn& SIn){SIn.Load(Val);}
00893   void Load(TSIn& SIn){SIn.Load(Val);}
00894   void Save(TSOut& SOut) const {SOut.Save(Val);}
00895   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm);
00896   void SaveXml(TSOut& SOut, const TStr& Nm) const;
00897 
00898   TBool& operator=(const TBool& Bool){Val=Bool.Val; return *this;}
00899   bool operator==(const TBool& Bool) const {return Val==Bool.Val;}
00900   bool operator<(const TBool& Bool) const {//return Val<Bool.Val;
00901     return (Val==false)&&(Bool.Val==true);}
00902   bool operator()() const {return Val;}
00903   int GetMemUsed() const {return sizeof(TBool);}
00904 
00905   int GetPrimHashCd() const {return Val;}
00906   int GetSecHashCd() const {return Val;}
00907 
00908   static bool GetRnd(){return Rnd.GetUniDevInt(2)==1;}
00909 
00910   static TStr GetStr(const bool& Val){
00911     if (Val){return TrueStr;} else {return FalseStr;}}
00912   static TStr GetStr(const TBool& Bool){
00913     return GetStr(Bool.Val);}
00914   static TStr GetYNStr(const bool& Val){
00915     if (Val){return YStr;} else {return NStr;}}
00916   static TStr GetYesNoStr(const bool& Val){
00917     if (Val){return YesStr;} else {return NoStr;}}
00918   static TStr Get01Str(const bool& Val){
00919     if (Val){return "1";} else {return "0";}}
00920   static bool IsValStr(const TStr& Str);
00921   static bool GetValFromStr(const TStr& Str);
00922   static bool GetValFromStr(const TStr& Str, const bool& DfVal);
00923 };
00924 
00926 // Char
00927 class TCh{
00928 public:
00929   char Val;
00930 public:
00931   static const char Mn;
00932   static const char Mx;
00933   static const int Vals;
00934 
00935   static const char NullCh;
00936   static const char TabCh;
00937   static const char LfCh;
00938   static const char CrCh;
00939   static const char EofCh;
00940   static const char HashCh;
00941 
00942   TCh(): Val(TCh::NullCh){}
00943   TCh(const char& _Val): Val(_Val){}
00944   operator char() const {return Val;}
00945   explicit TCh(TSIn& SIn){SIn.Load(Val);}
00946   void Save(TSOut& SOut) const {SOut.Save(Val);}
00947   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm);
00948   void SaveXml(TSOut& SOut, const TStr& Nm) const;
00949 
00950   TCh& operator=(const TCh& Ch){Val=Ch.Val; return *this;}
00951   bool operator==(const TCh& Ch) const {return Val==Ch.Val;}
00952   bool operator<(const TCh& Ch) const {return Val<Ch.Val;}
00953   char operator()() const {return Val;}
00954   int GetMemUsed() const {return sizeof(TCh);}
00955 
00956   int GetPrimHashCd() const {return Val;}
00957   int GetSecHashCd() const {return Val;}
00958 
00959   static bool IsWs(const char& Ch){
00960     return (Ch==' ')||(Ch==TabCh)||(Ch==CrCh)||(Ch==LfCh);}
00961   static bool IsAlpha(const char& Ch){
00962     return (('A'<=Ch)&&(Ch<='Z'))||(('a'<=Ch)&&(Ch<='z'));}
00963   static bool IsNum(const char& Ch){return ('0'<=Ch)&&(Ch<='9');}
00964   static bool IsAlNum(const char& Ch){return IsAlpha(Ch)||IsNum(Ch);}
00965   static int GetNum(const char& Ch){Assert(IsNum(Ch)); return Ch-'0';}
00966   static bool IsHex(const char& Ch){return
00967     (('0'<=Ch)&&(Ch<='9'))||(('A'<=Ch)&&(Ch<='F'))||(('a'<=Ch)&&(Ch<='f'));}
00968   static int GetHex(const char& Ch){
00969     if (('0'<=Ch)&&(Ch<='9')){return Ch-'0';}
00970     else if (('A'<=Ch)&&(Ch<='F')){return Ch-'A'+10;}
00971     else if (('a'<=Ch)&&(Ch<='f')){return Ch-'a'+10;}
00972     else Fail; return 0;}
00973   static char GetHexCh(const int& Val){
00974     if ((0<=Val)&&(Val<=9)){return char('0'+char(Val));}
00975     else if ((10<=Val)&&(Val<=15)){return char('A'+char(Val-10));}
00976     else Fail; return 0;}
00977   static char IsUc(const char& Ch){
00978     return ('A'<=Ch)&&(Ch<='Z');}
00979   static char GetUc(const char& Ch){
00980     if (('a'<=Ch)&&(Ch<='z')){return Ch-'a'+'A';} else {return Ch;}}
00981   static char GetUsFromYuAscii(const char& Ch);
00982 
00983   static TStr GetStr(const TCh& Ch){
00984     return TStr(Ch.Val);}
00985 };
00986 
00988 // Unsigned-Char
00989 class TUCh{
00990 public:
00991   uchar Val;
00992 public:
00993   static const uchar Mn;
00994   static const uchar Mx;
00995   static const int Vals;
00996 
00997   TUCh(): Val(TCh::NullCh){}
00998   TUCh(const uchar& _Val): Val(_Val){}
00999   operator uchar() const {return Val;}
01000   explicit TUCh(TSIn& SIn){SIn.Load(Val);}
01001   void Save(TSOut& SOut) const {SOut.Save(Val);}
01002   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm);
01003   void SaveXml(TSOut& SOut, const TStr& Nm) const;
01004 
01005   TUCh& operator=(const TUCh& UCh){Val=UCh.Val; return *this;}
01006   bool operator==(const TUCh& UCh) const {return Val==UCh.Val;}
01007   bool operator<(const TUCh& UCh) const {return Val<UCh.Val;}
01008   uchar operator()() const {return Val;}
01009   int GetMemUsed() const {return sizeof(TUCh);}
01010 
01011   int GetPrimHashCd() const {return Val;}
01012   int GetSecHashCd() const {return Val;}
01013 };
01014 
01016 // Short-Integer
01017 class TSInt{
01018 public:
01019   int16 Val;
01020 public:
01021   TSInt(): Val(0){}
01022   TSInt(const int16& _Val): Val(_Val){}
01023   operator int16() const {return Val;}
01024   explicit TSInt(TSIn& SIn){SIn.Load(Val);}
01025   void Load(TSIn& SIn){SIn.Load(Val);}
01026   void Save(TSOut& SOut) const {SOut.Save(Val);}
01027   int GetPrimHashCd() const {return Val;}
01028   int GetSecHashCd() const {return Val/0x10;}
01029 };
01030 
01032 // Integer
01033 class TInt{
01034 public:
01035   int Val;
01036 public:
01037   static const int Mn;
01038   static const int Mx;
01039   static const int Kilo;
01040   static const int Mega;
01041   static const int Giga;
01042   static TRnd Rnd;
01043 
01044   TInt(): Val(0){}
01045   TInt(const int& _Val): Val(_Val){}
01046   operator int() const {return Val;}
01047   explicit TInt(TSIn& SIn){SIn.Load(Val);}
01048   void Load(TSIn& SIn){SIn.Load(Val);}
01049   void Save(TSOut& SOut) const {SOut.Save(Val);}
01050   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm);
01051   void SaveXml(TSOut& SOut, const TStr& Nm) const;
01052 
01053   TInt& operator=(const TInt& Int){Val=Int.Val; return *this;}
01054   TInt& operator=(const int& Int){Val=Int; return *this;}
01055   bool operator==(const TInt& Int) const {return Val==Int.Val;}
01056   bool operator==(const int& Int) const {return Val==Int;}
01057   bool operator!=(const int& Int) const {return Val!=Int;}
01058   bool operator<(const TInt& Int) const {return Val<Int.Val;}
01059   bool operator<(const int& Int) const {return Val<Int;}
01060   int operator()() const {return Val;}
01061   TInt& operator+=(const int& Int){Val+=Int; return *this;}
01062   TInt& operator-=(const int& Int){Val-=Int; return *this;}
01063   TInt operator++(int){Val++; return *this;}
01064   TInt operator--(int){Val--; return *this;}
01065   int GetMemUsed() const {return sizeof(TInt);}
01066 
01067   int GetPrimHashCd() const {return Val;}
01068   int GetSecHashCd() const {return Val/0x10;}
01069 
01070   static int Abs(const int& Int){return Int<0?-Int:Int;}
01071   static int Sign(const int& Int){return Int<0?-1:(Int>0?1:0);}
01072   static void Swap(int& Int1, int& Int2){
01073     int SwapInt1=Int1; Int1=Int2; Int2=SwapInt1;}
01074   static int GetRnd(const int& Range=0){return Rnd.GetUniDevInt(Range);}
01075 
01076   static bool IsOdd(const int& Int){return ((Int%2)==1);}
01077   static bool IsEven(const int& Int){return ((Int%2)==0);}
01078 
01079   static int GetMn(const int& Int1, const int& Int2){
01080     return Int1<Int2?Int1:Int2;}
01081   static int GetMx(const int& Int1, const int& Int2){
01082     return Int1>Int2?Int1:Int2;}
01083   static int GetMn(const int& Int1, const int& Int2, const int& Int3){
01084     return GetMn(Int1, GetMn(Int2, Int3));}
01085   static int GetMn(const int& Int1, const int& Int2,
01086    const int& Int3, const int& Int4){
01087     return GetMn(GetMn(Int1, Int2), GetMn(Int3, Int4));}
01088   static int GetMx(const int& Int1, const int& Int2, const int& Int3){
01089     return GetMx(Int1, GetMx(Int2, Int3));}
01090   static int GetMx(const int& Int1, const int& Int2,
01091    const int& Int3, const int& Int4){
01092     return GetMx(GetMx(Int1, Int2), GetMx(Int3, Int4));}
01093   static int GetInRng(const int& Val, const int& Mn, const int& Mx){
01094     IAssert(Mn<=Mx); return Val<Mn?Mn:(Val>Mx?Mx:Val);}
01095 
01096   TStr GetStr() const {return TInt::GetStr(Val);}
01097   static TStr GetStr(const int& Val){
01098     char Bf[255]; sprintf(Bf, "%d", Val); return TStr(Bf);}
01099   static TStr GetStr(const TInt& Int){
01100     return GetStr(Int.Val);}
01101   static TStr GetStr(const int& Val, const char* FmtStr);
01102   static TStr GetStr(const int& Val, const TStr& FmtStr){
01103     return GetStr(Val, FmtStr.CStr());}
01104 
01105   static TStr GetHexStr(const int& Val){
01106     char Bf[255]; sprintf(Bf, "%X", Val); return TStr(Bf);}
01107   static TStr GetHexStr(const TInt& Int){
01108     return GetHexStr(Int.Val);}
01109 
01110   static TStr GetKiloStr(const int& Val){
01111     if (Val>=100*1000){return GetStr(Val/1000)+"K";}
01112     else if (Val>=1000){return GetStr(Val/1000)+"."+GetStr((Val%1000)/100)+"K";}
01113     else {return GetStr(Val);}}
01114   static TStr GetMegaStr(const int& Val){
01115     if (Val>=100*1000000){return GetStr(Val/1000000)+"M";}
01116     else if (Val>=1000000){
01117       return GetStr(Val/1000000)+"."+GetStr((Val%1000000)/100000)+"M";}
01118     else {return GetKiloStr(Val);}}
01119 
01120   // frugal
01121   static char* SaveFrugalInt(char *pDest, int i);
01122   static char* LoadFrugalInt(char *pSrc, int& i);
01123   static void TestFrugalInt();
01124   static void SaveFrugalIntV(TSOut& SOut, const TVec<TInt>& IntV);
01125   static void LoadFrugalIntV(TSIn& SIn, TVec<TInt>& IntV, bool ClrP=true);
01126 };
01127 
01129 // Unsigned-Integer
01130 class TUInt{
01131 public:
01132   uint Val;
01133 public:
01134   static const uint Mn;
01135   static const uint Mx;
01136   static TRnd Rnd;
01137 
01138   TUInt(): Val(0){}
01139   TUInt(const uint& _Val): Val(_Val){}
01140   operator uint() const {return Val;}
01141   explicit TUInt(TSIn& SIn){SIn.Load(Val);}
01142   void Load(TSIn& SIn){SIn.Load(Val);}
01143   void Save(TSOut& SOut) const {SOut.Save(Val);}
01144   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm);
01145   void SaveXml(TSOut& SOut, const TStr& Nm) const;
01146 
01147   TUInt& operator=(const TUInt& UInt){Val=UInt.Val; return *this;}
01148   TUInt& operator=(const uint& _Val){Val=_Val; return *this;}
01149   TUInt operator++(int){Val++; return *this;}
01150   TUInt operator--(int){Val--; return *this;}
01151   //bool operator==(const TUInt& UInt) const {return Val==UInt.Val;}
01152   //bool operator==(const uint& UInt) const {return Val==UInt;}
01153   //bool operator!=(const uint& UInt) const {return Val!=UInt;}
01154   //bool operator<(const TUInt& UInt) const {return Val<UInt.Val;}
01155   uint operator()() const {return Val;}
01156   uint& operator()() {return Val;}
01157   TUInt& operator~(){Val=~Val; return *this;}
01158   TUInt& operator&=(const TUInt& UInt){Val&=UInt.Val; return *this;}
01159   TUInt& operator|=(const TUInt& UInt){Val|=UInt.Val; return *this;}
01160   TUInt& operator^=(const TUInt& UInt){Val^=UInt.Val; return *this;}
01161   TUInt& operator>>=(const int& ShiftBits){Val>>=ShiftBits; return *this;}
01162   TUInt& operator<<=(const int& ShiftBits){Val<<=ShiftBits; return *this;}
01163   int GetMemUsed() const {return sizeof(TUInt);}
01164 
01165   int GetPrimHashCd() const {return int(Val);}
01166   int GetSecHashCd() const {return Val/0x10;}
01167 
01168   static uint GetRnd(const uint& Range=0){return Rnd.GetUniDevUInt(Range);}
01169 
01170   TStr GetStr() const {return TUInt::GetStr(Val);}
01171   static TStr GetStr(const uint& Val){
01172     char Bf[255]; sprintf(Bf, "%u", Val); return TStr(Bf);}
01173   static TStr GetStr(const TInt& UInt){
01174     return GetStr(UInt.Val);}
01175   static TStr GetStr(const uint& Val, const char* FmtStr);
01176   static TStr GetStr(const uint& Val, const TStr& FmtStr){
01177     return GetStr(Val, FmtStr.CStr());}
01178 
01179   static TStr GetKiloStr(const uint& Val){
01180     if (Val>100*1000){return GetStr(Val/1000)+"K";}
01181     else if (Val>1000){return GetStr(Val/1000)+"."+GetStr((Val%1000)/100)+"K";}
01182     else {return GetStr(Val);}}
01183   static TStr GetMegaStr(const uint& Val){
01184     if (Val>100*1000000){return GetStr(Val/1000000)+"M";}
01185     else if (Val>1000000){
01186       return GetStr(Val/1000000)+"."+GetStr((Val%1000000)/100000)+"M";}
01187     else {return GetKiloStr(Val);}}
01188 
01189   static uint JavaUIntToCppUInt(const uint& JavaUInt){
01190     uint B1=(JavaUInt & 0xFF000000) >> 24;
01191     uint B2=(JavaUInt & 0x00FF0000) >> 16;
01192     uint B3=(JavaUInt & 0x0000FF00) >> 8;
01193     uint B4=(JavaUInt & 0x000000FF) >> 0;
01194     uint CppUInt=(B4<<24)+(B3<<16)+(B2<<8)+(B1<<0);
01195     return CppUInt;}
01196 
01197   static bool IsIpStr(const TStr& IpStr, uint& Ip, const char& SplitCh = '.');
01198   static bool IsIpStr(const TStr& IpStr, const char& SplitCh = '.') { uint Ip; return IsIpStr(IpStr, Ip, SplitCh); }
01199   static uint GetUIntFromIpStr(const TStr& IpStr, const char& SplitCh = '.');
01200   static TStr GetStrFromIpUInt(const uint& Ip);
01201 };
01202 
01204 // Unsigned-Integer-64Bit
01205 class TUInt64{
01206 public:
01207   uint64 Val;
01208 public:
01209   static const TUInt64 Mn;
01210   static const TUInt64 Mx;
01211 
01212   TUInt64(): Val(0){}
01213   TUInt64(const TUInt64& Int): Val(Int.Val){}
01214   TUInt64(const uint64& Int): Val(Int){}
01215   TUInt64(const uint& MsVal, const uint& LsVal): Val(0){
01216     Val=(((uint64)MsVal) << 32) | ((uint64)LsVal);}
01217   explicit TUInt64(void* Pt): Val(0){
01218      TConv_Pt64Ints32 Conv(Pt); Val=Conv.GetUInt64();}
01219   operator uint64() const {return Val;}
01220   explicit TUInt64(TSIn& SIn){SIn.Load(Val);}
01221   void Load(TSIn& SIn){SIn.Load(Val);}
01222   void Save(TSOut& SOut) const {SOut.Save(Val);}
01223   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm);
01224   void SaveXml(TSOut& SOut, const TStr& Nm) const;
01225 
01226   TUInt64& operator=(const TUInt64& Int){Val=Int.Val; return *this;}
01227   TUInt64& operator+=(const TUInt64& Int){Val+=Int.Val; return *this;}
01228   TUInt64& operator-=(const TUInt64& Int){Val-=Int.Val; return *this;}
01229   TUInt64 operator++(int){Val++; return *this;}
01230   TUInt64 operator--(int){Val--; return *this;}
01231   int GetMemUsed() const {return sizeof(TUInt64);}
01232 
01233   int GetPrimHashCd() const { return (int)GetMsVal() + (int)GetLsVal(); } //TODO: to check
01234   int GetSecHashCd() const { return ((int)GetMsVal() + (int)GetLsVal()) / 0x10; } //TODO: to check
01235 
01236   uint GetMsVal() const {
01237     return (uint)(Val >> 32);}
01238   uint GetLsVal() const {
01239     return (uint)(Val & 0xffffffff);}
01240 
01241   //TStr GetStr() const {return TStr::Fmt("%Lu", Val);}
01242   //static TStr GetStr(const TUInt64& Int){return TStr::Fmt("%Lu", Int.Val);}
01243   //static TStr GetHexStr(const TUInt64& Int){return TStr::Fmt("%LX", Int.Val);}
01244   #ifdef GLib_WIN
01245   TStr GetStr() const {return TStr::Fmt("%I64u", Val);}
01246   static TStr GetStr(const TUInt64& Int){return TStr::Fmt("%I64u", Int.Val);}
01247   static TStr GetHexStr(const TUInt64& Int){return TStr::Fmt("%I64X", Int.Val);}
01248   #else
01249   TStr GetStr() const {return TStr::Fmt("%llu", Val);}
01250   static TStr GetStr(const TUInt64& Int){return TStr::Fmt("%llu", Int.Val);}
01251   static TStr GetHexStr(const TUInt64& Int){return TStr::Fmt("%llX", Int.Val);}
01252   #endif
01253 
01254   static TStr GetKiloStr(const uint64& Val){
01255     if (Val>100*1000){return GetStr(Val/1000)+"K";}
01256     else if (Val>1000){return GetStr(Val/1000)+"."+GetStr((Val%1000)/100)+"K";}
01257     else {return GetStr(Val);}}
01258   static TStr GetMegaStr(const uint64& Val){
01259     if (Val>100*1000000){return GetStr(Val/1000000)+"M";}
01260     else if (Val>1000000){
01261       return GetStr(Val/1000000)+"."+GetStr((Val%1000000)/100000)+"M";}
01262     else {return GetKiloStr(Val);}}
01263   /*static TStr GetGigaStr(const uint64& Val){
01264     if (Val>100*1000000000){return GetStr(Val/1000000000)+"G";}
01265     else if (Val>1000000000){
01266       return GetStr(Val/1000000000)+"."+GetStr((Val%1000000000)/100000000)+"G";}
01267     else {return GetMegaStr(Val);}}*/
01268 };
01269 
01271 // Float
01272 class TFlt{
01273 public:
01274   double Val;
01275 public:
01276   static const double Mn;
01277   static const double Mx;
01278   static const double NInf;
01279   static const double PInf;
01280   static const double Eps;
01281   static const double EpsHalf;
01282   static TRnd Rnd;
01283 
01284   TFlt(): Val(0){}
01285   TFlt(const double& _Val): Val(_Val){}
01286   operator double() const {return Val;}
01287   explicit TFlt(TSIn& SIn){SIn.Load(Val);}
01288   void Save(TSOut& SOut) const {SOut.Save(Val);}
01289   explicit TFlt(TSIn& SIn, const bool& IsTxt){
01290     if (IsTxt){TStr Str(SIn, true); Val=Str.GetFlt(0);} else {SIn.Load(Val);}}
01291   void Load(TSIn& SIn){SIn.Load(Val);}
01292   void Save(TSOut& SOut, const bool& IsTxt) const {
01293     if (IsTxt){GetStr(Val).Save(SOut, true);} else {SOut.Save(Val);}}
01294   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm);
01295   void SaveXml(TSOut& SOut, const TStr& Nm) const;
01296 
01297   TFlt& operator=(const TFlt& Flt){Val=Flt.Val; return *this;}
01298   TFlt& operator=(const double& Flt){Val=Flt; return *this;}
01299   bool operator==(const TFlt& Flt) const _CMPWARN {return Val==Flt.Val;}
01300   bool operator==(const double& Flt) const _CMPWARN {return Val==Flt;}
01301   bool operator!=(const double& Flt) const _CMPWARN {return Val!=Flt;}
01302   double operator()() const {return Val;}
01303   TFlt& operator+=(const double& Flt){Val+=Flt; return *this;}
01304   TFlt& operator-=(const double& Flt){Val-=Flt; return *this;}
01305   TFlt& operator*=(const double& Flt){Val*=Flt; return *this;}
01306   TFlt& operator/=(const double& Flt){Val/=Flt; return *this;}
01307   TFlt operator++(int){Val++; return *this;}
01308   TFlt operator--(int){Val--; return *this;}
01309   int GetMemUsed() const {return sizeof(TFlt);}
01310 
01311   int GetPrimHashCd() const {
01312     int Expn; return int((frexp(Val, &Expn)-0.5)*double(TInt::Mx));}
01313   int GetSecHashCd() const {
01314     int Expn; frexp(Val, &Expn); return Expn;}
01315 
01316   static double Abs(const double& Flt){return Flt<0?-Flt:Flt;}
01317   static int Sign(const double& Flt){return Flt<0?-1:(Flt>0?1:0);}
01318   static int Round(const double& Flt){return int(floor(Flt+0.5));}
01319   static double GetRnd(){return Rnd.GetUniDev();}
01320   static bool Eq6(const double& LFlt, const double& RFlt){
01321     return fabs(LFlt-RFlt)<0.000001;}
01322 
01323   static double GetMn(const double& Flt1, const double& Flt2){
01324     return Flt1<Flt2?Flt1:Flt2;}
01325   static double GetMn(const double& Flt1, const double& Flt2, const double& Flt3){
01326     return GetMn(GetMn(Flt1, Flt2), Flt3); }
01327   static double GetMn(const double& Flt1, const double& Flt2, const double& Flt3, const double& Flt4){
01328     return GetMn(GetMn(Flt1, Flt2), GetMn(Flt3, Flt4)); }
01329 
01330   static double GetMx(const double& Flt1, const double& Flt2){
01331     return Flt1>Flt2?Flt1:Flt2;}
01332   static double GetMx(const double& Flt1, const double& Flt2, const double Flt3){
01333     return GetMx(GetMx(Flt1, Flt2), Flt3); }
01334   static double GetMx(const double& Flt1, const double& Flt2, const double Flt3, const double& Flt4){
01335     return GetMx(GetMx(Flt1, Flt2), GetMx(Flt3, Flt4)); }
01336 
01337   static double GetInRng(const double& Val, const double& Mn, const double& Mx){
01338     IAssert(Mn<=Mx); return Val<Mn?Mn:(Val>Mx?Mx:Val);}
01339   static double IsNum(const double& Val){
01340     return (Mn<=Val)&&(Val<=Mx);}
01341 
01342   TStr GetStr() const {return TFlt::GetStr(Val);}
01343   static TStr GetStr(const double& Val, const int& Width=-1, const int& Prec=-1);
01344   static TStr GetStr(const TFlt& Flt, const int& Width=-1, const int& Prec=-1){
01345     return GetStr(Flt.Val, Width, Prec);}
01346   static TStr GetStr(const double& Val, const char* FmtStr);
01347   static TStr GetStr(const double& Val, const TStr& FmtStr){
01348     return GetStr(Val, FmtStr.CStr());}
01349   static TStr GetPrcStr(const double& RelVal, const double& FullVal){
01350     return GetStr(100*RelVal/FullVal, "%3.0f%%");}
01351 
01352   static TStr GetKiloStr(const double& Val){
01353     if (fabs(Val)>100*1000){return TStr::Fmt("%.0fK", Val/1000);}
01354     else if (fabs(Val)>1000){return TStr::Fmt("%.1fK", Val/1000);}
01355     else {return TStr::Fmt("%.0f", Val);}}
01356   static TStr GetMegaStr(const double& Val){
01357     if (fabs(Val)>100*1000000){return TStr::Fmt("%.0fM", Val/1000000);}
01358     else if (fabs(Val)>1000000){return TStr::Fmt("%.1fM", Val/1000000);}
01359     else {return GetKiloStr(Val);}}
01360   static TStr GetGigaStr(const double& Val){
01361     if (fabs(Val)>100*1000000000.0){return TStr::Fmt("%.0fG", Val/1000000000.0);}
01362     else if (fabs(Val)>1000000000.0){return TStr::Fmt("%.1fG", Val/1000000000.0);}
01363     else {return GetMegaStr(Val);}}
01364 };
01365 
01367 // Ascii-Float
01368 class TAscFlt: public TFlt{
01369 public:
01370   TAscFlt(): TFlt(){}
01371   TAscFlt(const double& Val): TFlt(Val){}
01372   explicit TAscFlt(TSIn& SIn): TFlt(SIn, true){}
01373   void Save(TSOut& SOut) const {TFlt::Save(SOut, true);}
01374 };
01375 
01377 // Short-Float
01378 class TSFlt{
01379 public:
01380   sdouble Val;
01381 public:
01382   static const sdouble Mn;
01383   static const sdouble Mx;
01384 
01385   TSFlt(): Val(0){}
01386   TSFlt(const sdouble& _Val): Val(sdouble(_Val)){}
01387   //TSFlt(const double& _Val): Val(sdouble(_Val)){}
01388   operator sdouble() const {return Val;}
01389   //operator double() const {return Val;}
01390   explicit TSFlt(TSIn& SIn){SIn.Load(Val);}
01391   void Save(TSOut& SOut) const {SOut.Save(Val);}
01392   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm);
01393   void SaveXml(TSOut& SOut, const TStr& Nm) const;
01394 
01395   TSFlt& operator=(const TSFlt& SFlt){Val=SFlt.Val; return *this;}
01396   bool operator==(const TSFlt& SFlt) const _CMPWARN {return Val==SFlt.Val;}
01397   bool operator==(const double& Flt) const _CMPWARN {return Val==Flt;}
01398   bool operator!=(const double& Flt) const _CMPWARN {return Val!=Flt;}
01399   bool operator<(const TSFlt& SFlt) const {return Val<SFlt.Val;}
01400   sdouble operator()() const {return Val;}
01401   TSFlt& operator+=(const double& SFlt){Val+=sdouble(SFlt); return *this;}
01402   TSFlt& operator-=(const double& SFlt){Val-=sdouble(SFlt); return *this;}
01403   TSFlt& operator*=(const double& SFlt){Val*=sdouble(SFlt); return *this;}
01404   TSFlt& operator/=(const double& SFlt){Val/=sdouble(SFlt); return *this;}
01405   TSFlt operator++(int){Val++; return *this;}
01406   TSFlt operator--(int){Val--; return *this;}
01407   int GetMemUsed() const {return sizeof(TSFlt);}
01408 
01409   int GetPrimHashCd() const {
01410     int Expn; return int((frexp(Val, &Expn)-0.5)*double(TInt::Mx));}
01411   int GetSecHashCd() const {
01412     int Expn; frexp(Val, &Expn); return Expn;}
01413 };
01414 
01416 // Long-Float
01417 class TLFlt{
01418 public:
01419   ldouble Val;
01420 public:
01421   static const ldouble Mn;
01422   static const ldouble Mx;
01423 
01424   TLFlt(): Val(0){}
01425   TLFlt(const ldouble& _Val): Val(_Val){}
01426   operator ldouble() const {return Val;}
01427   explicit TLFlt(TSIn& SIn){SIn.Load(Val);}
01428   void Save(TSOut& SOut) const {SOut.Save(Val);}
01429   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm);
01430   void SaveXml(TSOut& SOut, const TStr& Nm) const;
01431 
01432   TLFlt& operator=(const TLFlt& LFlt){Val=LFlt.Val; return *this;}
01433   bool operator==(const TLFlt& LFlt) const _CMPWARN {return Val==LFlt.Val;}
01434   bool operator==(const ldouble& LFlt) const _CMPWARN {return Val==LFlt;}
01435   bool operator!=(const ldouble& LFlt) const _CMPWARN {return Val!=LFlt;}
01436   bool operator<(const TLFlt& LFlt) const {return Val<LFlt.Val;}
01437   ldouble operator()() const {return Val;}
01438   TLFlt& operator+=(const ldouble& LFlt){Val+=LFlt; return *this;}
01439   TLFlt& operator-=(const ldouble& LFlt){Val-=LFlt; return *this;}
01440   int GetMemUsed() const {return sizeof(TLFlt);}
01441 
01442   int GetPrimHashCd() const {Fail; return 0;}
01443   int GetSecHashCd() const {Fail; return 0;}
01444 
01445   static TStr GetStr(const ldouble& Val, const int& Width=-1, const int& Prec=-1);
01446   static TStr GetStr(const TLFlt& LFlt, const int& Width=-1, const int& Prec=-1){
01447     return GetStr(LFlt.Val, Width, Prec);}
01448   static TStr GetStr(const ldouble& Val, const char* FmtStr);
01449   static TStr GetStr(const ldouble& Val, const TStr& FmtStr){
01450     return GetStr(Val, FmtStr.CStr());}
01451 };
01452 
01454 // Float-Rectangle
01455 class TFltRect{
01456 public:
01457   TFlt MnX, MnY, MxX, MxY;
01458 public:
01459   TFltRect():
01460     MnX(), MnY(), MxX(), MxY(){}
01461   TFltRect(const TFltRect& FltRect):
01462     MnX(FltRect.MnX), MnY(FltRect.MnY), MxX(FltRect.MxX), MxY(FltRect.MxY){}
01463   TFltRect(
01464    const double& _MnX, const double& _MnY,
01465    const double& _MxX, const double& _MxY):
01466     MnX(_MnX), MnY(_MnY), MxX(_MxX), MxY(_MxY){}
01467   TFltRect(TSIn& SIn):
01468     MnX(SIn), MnY(SIn), MxX(SIn), MxY(SIn){}
01469   void Save(TSOut& SOut) const {
01470     MnX.Save(SOut); MnY.Save(SOut); MxX.Save(SOut); MxY.Save(SOut);}
01471   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm);
01472   void SaveXml(TSOut& SOut, const TStr& Nm) const;
01473 
01474   TFltRect& operator=(const TFltRect& FltRect){
01475     MnX=FltRect.MnX; MnY=FltRect.MnY; MxX=FltRect.MxX; MxY=FltRect.MxY;
01476     return *this;}
01477 
01478   // get coordinates
01479   double GetMnX() const {return MnX;}
01480   double GetMnY() const {return MnY;}
01481   double GetMxX() const {return MxX;}
01482   double GetMxY() const {return MxY;}
01483 
01484   // get lengths
01485   double GetXLen() const {return MxX-MnX;}
01486   double GetYLen() const {return MxY-MnY;}
01487 
01488   // get centers
01489   double GetXCenter() const {return MnX+(MxX-MnX)/2;}
01490   double GetYCenter() const {return MnY+(MxY-MnY)/2;}
01491 
01492   // tests
01493   bool IsXYIn(const double& X, const double& Y) const {
01494     return (MnX<=X)&&(X<=MxX)&&(MnY<=Y)&&(Y<=MxY);}
01495   static bool Intersection(const TFltRect& Rect1, const TFltRect& Rect2);
01496 
01497   // string
01498   TStr GetStr() const;
01499 };
01500