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