SNAP Library 2.1, Developer Reference  2013-09-25 10:47:25
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
ds.h
Go to the documentation of this file.
00001 
00002 // Address-Pointer
00003 template <class TRec>
00004 class TAPt{
00005 private:
00006   TRec* Addr;
00007 public:
00008   TAPt(): Addr(NULL){}
00009   TAPt(const TAPt& Pt): Addr(Pt.Addr){}
00010   TAPt(TRec* _Addr): Addr(_Addr){}
00011   TAPt(TSIn&){Fail;}
00012   void Save(TSOut&) const {Fail;}
00013 
00014   TAPt& operator=(const TAPt& Pt){Addr=Pt.Addr; return *this;}
00015   TAPt& operator=(TRec* _Addr){Addr=_Addr; return *this;}
00016   bool operator==(const TAPt& Pt) const {return *Addr==*Pt.Addr;}
00017   bool operator!=(const TAPt& Pt) const {return *Addr!=*Pt.Addr;}
00018   bool operator<(const TAPt& Pt) const {return *Addr<*Pt.Addr;}
00019 
00020   TRec* operator->() const {Assert(Addr!=NULL); return Addr;}
00021   TRec& operator*() const {Assert(Addr!=NULL); return *Addr;}
00022   TRec& operator[](const int& RecN) const {
00023     Assert(Addr!=NULL); return Addr[RecN];}
00024   TRec* operator()() const {return Addr;}
00025 
00026   bool Empty() const {return Addr==NULL;}
00027 };
00028 
00030 // Pair
00031 template <class TVal1, class TVal2>
00032 class TPair{
00033 public:
00034   TVal1 Val1;
00035   TVal2 Val2;
00036 public:
00037   TPair(): Val1(), Val2(){}
00038   TPair(const TPair& Pair): Val1(Pair.Val1), Val2(Pair.Val2){}
00039   TPair(const TVal1& _Val1, const TVal2& _Val2): Val1(_Val1), Val2(_Val2){}
00040   explicit TPair(TSIn& SIn): Val1(SIn), Val2(SIn){}
00041   void Save(TSOut& SOut) const {
00042     Val1.Save(SOut); Val2.Save(SOut);}
00043   void Load(TSIn& SIn) {Val1.Load(SIn); Val2.Load(SIn);}
00044   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm="");
00045   void SaveXml(TSOut& SOut, const TStr& Nm) const;
00046 
00047   TPair& operator=(const TPair& Pair){
00048     if (this!=&Pair){Val1=Pair.Val1; Val2=Pair.Val2;} return *this;}
00049   bool operator==(const TPair& Pair) const {
00050     return (Val1==Pair.Val1)&&(Val2==Pair.Val2);}
00051   bool operator<(const TPair& Pair) const {
00052     return (Val1<Pair.Val1)||((Val1==Pair.Val1)&&(Val2<Pair.Val2));}
00053 
00054   int GetMemUsed() const {return Val1.GetMemUsed()+Val2.GetMemUsed();}
00055 
00056   int GetPrimHashCd() const {return TPairHashImpl::GetHashCd(Val1.GetPrimHashCd(), Val2.GetPrimHashCd()); }
00057   int GetSecHashCd() const {return TPairHashImpl::GetHashCd(Val2.GetSecHashCd(), Val1.GetSecHashCd()); }
00058 
00059   void GetVal(TVal1& _Val1, TVal2& _Val2) const {_Val1=Val1; _Val2=Val2;}
00060   const TVal1& GetVal1() const { return Val1;}
00061   const TVal2& GetVal2() const { return Val2;}
00062   TStr GetStr() const {
00063     return TStr("Pair(")+Val1.GetStr()+", "+Val2.GetStr()+")";}
00064 };
00065 
00066 template <class TVal1, class TVal2, class TSizeTy>
00067 void GetSwitchedPrV(const TVec<TPair<TVal1, TVal2>, TSizeTy>& SrcPrV, TVec<TPair<TVal2, TVal1>, TSizeTy>& DstPrV){
00068   const TSizeTy Prs = SrcPrV.Len();
00069   DstPrV.Gen(Prs, 0);
00070   for (TSizeTy PrN=0; PrN<Prs; PrN++){
00071     const TPair<TVal1, TVal2>& SrcPr=SrcPrV[PrN];
00072     DstPrV.Add(TPair<TVal2, TVal1>(SrcPr.Val2, SrcPr.Val1));
00073   }
00074 }
00075 
00076 typedef TPair<TBool, TCh> TBoolChPr;
00077 typedef TPair<TBool, TFlt> TBoolFltPr;
00078 typedef TPair<TUCh, TInt> TUChIntPr;
00079 typedef TPair<TUCh, TUInt64> TUChUInt64Pr;
00080 typedef TPair<TUCh, TStr> TUChStrPr;
00081 typedef TPair<TInt, TBool> TIntBoolPr;
00082 typedef TPair<TInt, TCh> TIntChPr;
00083 typedef TPair<TInt, TInt> TIntPr;
00084 typedef TPair<TInt, TUInt64> TIntUInt64Pr;
00085 typedef TPair<TInt, TIntPr> TIntIntPrPr;
00086 typedef TPair<TInt, TVec<TInt, int> > TIntIntVPr;
00087 typedef TPair<TInt, TFlt> TIntFltPr;
00088 typedef TPair<TInt, TStr> TIntStrPr;
00089 typedef TPair<TInt, TStrV> TIntStrVPr;
00090 typedef TPair<TIntPr, TInt> TIntPrIntPr;
00091 typedef TPair<TUInt, TUInt> TUIntUIntPr;
00092 typedef TPair<TUInt, TInt> TUIntIntPr;
00093 typedef TPair<TUInt64, TInt> TUInt64IntPr;
00094 typedef TPair<TUInt64, TUInt64> TUInt64Pr;
00095 typedef TPair<TUInt64, TFlt> TUInt64FltPr;
00096 typedef TPair<TUInt64, TStr> TUInt64StrPr;
00097 typedef TPair<TFlt, TInt> TFltIntPr;
00098 typedef TPair<TFlt, TUInt64> TFltUInt64Pr;
00099 typedef TPair<TFlt, TFlt> TFltPr;
00100 typedef TPair<TFlt, TStr> TFltStrPr;
00101 typedef TPair<TAscFlt, TInt> TAscFltIntPr;
00102 typedef TPair<TAscFlt, TAscFlt> TAscFltPr;
00103 typedef TPair<TFlt, TStr> TFltStrPr;
00104 typedef TPair<TAscFlt, TStr> TAscFltStrPr;
00105 typedef TPair<TStr, TInt> TStrIntPr;
00106 typedef TPair<TStr, TFlt> TStrFltPr;
00107 typedef TPair<TStr, TStr> TStrPr;
00108 typedef TPair<TStr, TStrV> TStrStrVPr;
00109 typedef TPair<TStrV, TInt> TStrVIntPr;
00110 typedef TPair<TInt, TIntPr> TIntIntPrPr;
00111 typedef TPair<TInt, TStrPr> TIntStrPrPr;
00112 typedef TPair<TFlt, TStrPr> TFltStrPrPr;
00113 
00115 template <class TVal1, class TVal2>
00116 class TCmpPairByVal2 {
00117 private:
00118   bool IsAsc;
00119 public:
00120   TCmpPairByVal2(const bool& AscSort=true) : IsAsc(AscSort) { }
00121   bool operator () (const TPair<TVal1, TVal2>& P1, const TPair<TVal1, TVal2>& P2) const {
00122     if (IsAsc) { return P1.Val2 < P2.Val2; } else { return P2.Val2 < P1.Val2; }
00123   }
00124 };
00125 
00127 // Triple
00128 template <class TVal1, class TVal2, class TVal3>
00129 class TTriple{
00130 public:
00131   TVal1 Val1;
00132   TVal2 Val2;
00133   TVal3 Val3;
00134 public:
00135   TTriple(): Val1(), Val2(), Val3(){}
00136   TTriple(const TTriple& Triple):
00137     Val1(Triple.Val1), Val2(Triple.Val2), Val3(Triple.Val3){}
00138   TTriple(const TVal1& _Val1, const TVal2& _Val2, const TVal3& _Val3):
00139     Val1(_Val1), Val2(_Val2), Val3(_Val3){}
00140   explicit TTriple(TSIn& SIn): Val1(SIn), Val2(SIn), Val3(SIn){}
00141   void Save(TSOut& SOut) const {
00142     Val1.Save(SOut); Val2.Save(SOut); Val3.Save(SOut);}
00143   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm="");
00144   void SaveXml(TSOut& SOut, const TStr& Nm) const;
00145 
00146   TTriple& operator=(const TTriple& Triple){
00147     if (this!=&Triple){Val1=Triple.Val1; Val2=Triple.Val2; Val3=Triple.Val3;}
00148     return *this;}
00149   bool operator==(const TTriple& Triple) const {
00150     return (Val1==Triple.Val1)&&(Val2==Triple.Val2)&&(Val3==Triple.Val3);}
00151   bool operator<(const TTriple& Triple) const {
00152     return (Val1<Triple.Val1)||((Val1==Triple.Val1)&&(Val2<Triple.Val2))||
00153      ((Val1==Triple.Val1)&&(Val2==Triple.Val2)&&(Val3<Triple.Val3));}
00154 
00155   int GetPrimHashCd() const {return  TPairHashImpl::GetHashCd(TPairHashImpl::GetHashCd(Val1.GetPrimHashCd(), Val2.GetPrimHashCd()), Val3.GetPrimHashCd()); }
00156   int GetSecHashCd() const {return TPairHashImpl::GetHashCd(TPairHashImpl::GetHashCd(Val2.GetSecHashCd(), Val3.GetSecHashCd()), Val1.GetSecHashCd()); }
00157   int GetMemUsed() const {return Val1.GetMemUsed()+Val2.GetMemUsed()+Val3.GetMemUsed();}
00158 
00159   void GetVal(TVal1& _Val1, TVal2& _Val2, TVal3& _Val3) const {
00160     _Val1=Val1; _Val2=Val2; _Val3=Val3;}
00161 };
00162 
00163 typedef TTriple<TCh, TCh, TCh> TChTr;
00164 typedef TTriple<TCh, TInt, TInt> TChIntIntTr;
00165 typedef TTriple<TUCh, TInt, TInt> TUChIntIntTr;
00166 typedef TTriple<TInt, TInt, TInt> TIntTr;
00167 typedef TTriple<TUInt64, TUInt64, TUInt64> TUInt64Tr;
00168 typedef TTriple<TInt, TStr, TInt> TIntStrIntTr;
00169 typedef TTriple<TInt, TInt, TStr> TIntIntStrTr;
00170 typedef TTriple<TInt, TInt, TFlt> TIntIntFltTr;
00171 typedef TTriple<TInt, TFlt, TInt> TIntFltIntTr;
00172 typedef TTriple<TInt, TFlt, TFlt> TIntFltFltTr;
00173 typedef TTriple<TInt, TVec<TInt, int>, TInt> TIntIntVIntTr;
00174 typedef TTriple<TInt, TInt, TVec<TInt, int> > TIntIntIntVTr;
00175 typedef TTriple<TFlt, TFlt, TFlt> TFltTr;
00176 typedef TTriple<TFlt, TInt, TInt> TFltIntIntTr;
00177 typedef TTriple<TFlt, TFlt, TInt> TFltFltIntTr;
00178 typedef TTriple<TFlt, TFlt, TStr> TFltFltStrTr;
00179 typedef TTriple<TChA, TChA, TChA> TChATr;
00180 typedef TTriple<TStr, TStr, TStr> TStrTr;
00181 typedef TTriple<TStr, TInt, TInt> TStrIntIntTr;
00182 typedef TTriple<TStr, TFlt, TFlt> TStrFltFltTr;
00183 typedef TTriple<TStr, TStr, TInt> TStrStrIntTr;
00184 typedef TTriple<TStr, TInt, TStrV> TStrIntStrVTr;
00185     
00187 template <class TVal1, class TVal2, class TVal3>
00188 class TCmpTripleByVal2 {
00189 private:
00190   bool IsAsc;
00191 public:
00192   TCmpTripleByVal2(const bool& AscSort=true) : IsAsc(AscSort) { }
00193   bool operator () (const TTriple<TVal1, TVal2, TVal3>& T1, const TTriple<TVal1, TVal2, TVal3>& T2) const {
00194     if (IsAsc) { return T1.Val2 < T2.Val2; } else { return T2.Val2 < T1.Val2; }
00195   }
00196 };
00197     
00199 template <class TVal1, class TVal2, class TVal3>
00200 class TCmpTripleByVal3 {
00201 private:
00202   bool IsAsc;
00203 public:
00204   TCmpTripleByVal3(const bool& AscSort=true) : IsAsc(AscSort) { }
00205   bool operator () (const TTriple<TVal1, TVal2, TVal3>& T1, const TTriple<TVal1, TVal2, TVal3>& T2) const {
00206     if (IsAsc) { return T1.Val3 < T2.Val3; } else { return T2.Val3 < T1.Val3; }
00207   }
00208 };
00209 
00211 // Quad
00212 template <class TVal1, class TVal2, class TVal3, class TVal4>
00213 class TQuad{
00214 public:
00215   TVal1 Val1;
00216   TVal2 Val2;
00217   TVal3 Val3;
00218   TVal4 Val4;
00219 public:
00220   TQuad():
00221     Val1(), Val2(), Val3(), Val4(){}
00222   TQuad(const TQuad& Quad):
00223     Val1(Quad.Val1), Val2(Quad.Val2), Val3(Quad.Val3), Val4(Quad.Val4){}
00224   TQuad(const TVal1& _Val1, const TVal2& _Val2, const TVal3& _Val3, const TVal4& _Val4):
00225     Val1(_Val1), Val2(_Val2), Val3(_Val3), Val4(_Val4){}
00226   explicit TQuad(TSIn& SIn):
00227     Val1(SIn), Val2(SIn), Val3(SIn), Val4(SIn){}
00228   void Save(TSOut& SOut) const {
00229     Val1.Save(SOut); Val2.Save(SOut); Val3.Save(SOut); Val4.Save(SOut);}
00230   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm="");
00231   void SaveXml(TSOut& SOut, const TStr& Nm) const;
00232 
00233   TQuad& operator=(const TQuad& Quad){
00234     if (this!=&Quad){
00235       Val1=Quad.Val1; Val2=Quad.Val2; Val3=Quad.Val3; Val4=Quad.Val4;}
00236     return *this;}
00237   bool operator==(const TQuad& Quad) const {
00238     return (Val1==Quad.Val1)&&(Val2==Quad.Val2)&&(Val3==Quad.Val3)&&(Val4==Quad.Val4);}
00239   bool operator<(const TQuad& Quad) const {
00240     return (Val1<Quad.Val1)||((Val1==Quad.Val1)&&(Val2<Quad.Val2))||
00241      ((Val1==Quad.Val1)&&(Val2==Quad.Val2)&&(Val3<Quad.Val3))||
00242      ((Val1==Quad.Val1)&&(Val2==Quad.Val2)&&(Val3==Quad.Val3)&&(Val4<Quad.Val4));}
00243 
00244   int GetPrimHashCd() const {return  TPairHashImpl::GetHashCd(TPairHashImpl::GetHashCd(Val1.GetPrimHashCd(), Val2.GetPrimHashCd()), TPairHashImpl::GetHashCd(Val3.GetPrimHashCd(), Val4.GetPrimHashCd())); }
00245   int GetSecHashCd() const {return TPairHashImpl::GetHashCd(TPairHashImpl::GetHashCd(Val2.GetSecHashCd(), Val3.GetSecHashCd()), TPairHashImpl::GetHashCd(Val4.GetSecHashCd(), Val1.GetSecHashCd())); }
00246 
00247   void GetVal(TVal1& _Val1, TVal2& _Val2, TVal3& _Val3, TVal4& _Val4) const {
00248     _Val1=Val1; _Val2=Val2; _Val3=Val3; _Val4=Val4;}
00249 };
00250 
00251 typedef TQuad<TStr, TStr, TInt, TInt> TStrStrIntIntQu;
00252 typedef TQuad<TStr, TStr, TStr, TStr> TStrQu;
00253 typedef TQuad<TInt, TInt, TInt, TInt> TIntQu;
00254 typedef TQuad<TFlt, TFlt, TFlt, TFlt> TFltQu;
00255 typedef TQuad<TFlt, TInt, TInt, TInt> TFltIntIntIntQu;
00256 typedef TQuad<TInt, TStr, TInt, TInt> TIntStrIntIntQu;
00257 typedef TQuad<TInt, TInt, TFlt, TFlt> TIntIntFltFltQu;
00258 
00260 // Tuple
00261 template<class TVal, int NVals>
00262 class TTuple {
00263 private:
00264   TVal ValV [NVals];
00265 public:
00266   TTuple(){}
00267   TTuple(const TVal& InitVal) { for (int i=0; i<Len(); i++) ValV[i]=InitVal; }
00268   TTuple(const TTuple& Tup) { for (int i=0; i<Len(); i++) ValV[i]=Tup[i]; }
00269   TTuple(TSIn& SIn) { for (int i=0; i<Len(); i++) ValV[i].Load(SIn); }
00270   void Save(TSOut& SOut) const { for (int i=0; i<Len(); i++) ValV[i].Save(SOut); }
00271   void Load(TSIn& SIn) { for (int i=0; i<Len(); i++) ValV[i].Load(SIn); }
00272 
00273   int Len() const { return NVals; }
00274   TVal& operator[] (const int& ValN) { return ValV[ValN]; }
00275   const TVal& operator[] (const int& ValN) const { return ValV[ValN]; }
00276   TTuple& operator = (const TTuple& Tup) { if (this != & Tup) {
00277     for (int i=0; i<Len(); i++) ValV[i]=Tup[i]; } return *this; }
00278   bool operator == (const TTuple& Tup) const {
00279     if (Len()!=Tup.Len()) { return false; }  if (&Tup==this) { return true; }
00280     for (int i=0; i<Len(); i++) if(ValV[i]!=Tup[i]){return false;} return true; }
00281   bool operator < (const TTuple& Tup) const {
00282     if (Len() == Tup.Len()) { for (int i=0; i<Len(); i++) {
00283       if(ValV[i]<Tup[i]){return true;} else if(ValV[i]>Tup[i]){return false;} } return false; }
00284     else { return Len() < Tup.Len(); } }
00285   void Sort(const bool& Asc=true);
00286   int FindMx() const;
00287   int FindMn() const;
00288   int GetPrimHashCd() const { int hc = 0;
00289     for (int i = 0; i < NVals; i++) { hc = TPairHashImpl::GetHashCd(hc, ValV[i].GetPrimHashCd()); }
00290     return hc; }
00291   int GetSecHashCd() const { int hc = 0;
00292     for (int i = 1; i < NVals; i++) { hc = TPairHashImpl::GetHashCd(hc, ValV[i].GetSecHashCd()); }
00293     if (NVals > 0) { hc = TPairHashImpl::GetHashCd(hc, ValV[0].GetSecHashCd()); }
00294     return hc; }
00295   
00296   TStr GetStr() const { TChA ValsStr;
00297     for (int i=0; i<Len(); i++) { ValsStr+=" "+ValV[i].GetStr(); }
00298     return TStr::Fmt("Tuple(%d):", Len())+ValsStr; }
00299 };
00300 
00301 template<class TVal, int NVals>
00302 void TTuple<TVal, NVals>::Sort(const bool& Asc) {
00303   TVec<TVal, int> V(NVals);
00304   for (int i=0; i<NVals; i++) { V.Add(ValV[i]); }
00305   V.Sort(Asc);
00306   for (int i=0; i<NVals; i++) { ValV[i] = V[i]; }
00307 }
00308 
00309 template<class TVal, int NVals>
00310 int TTuple<TVal, NVals>::FindMx() const {
00311   TVal MxVal = ValV[0];
00312   int ValN = 0;
00313   for (int i = 1; i < NVals; i++) {
00314     if (MxVal<ValV[i]) {
00315       MxVal=ValV[i]; ValN=i;
00316     }
00317   }
00318   return ValN;
00319 }
00320 
00321 template<class TVal, int NVals>
00322 int TTuple<TVal, NVals>::FindMn() const {
00323   TVal MnVal = ValV[0];
00324   int ValN = 0;
00325   for (int i = 1; i < NVals; i++) {
00326     if (MnVal>ValV[i]) {
00327       MnVal=ValV[i]; ValN=i;
00328     }
00329   }
00330   return ValN;
00331 }
00332 
00334 // Key-Data
00335 template <class TKey, class TDat>
00336 class TKeyDat{
00337 public:
00338   TKey Key;
00339   TDat Dat;
00340 public:
00341   TKeyDat(): Key(), Dat(){}
00342   TKeyDat(const TKeyDat& KeyDat): Key(KeyDat.Key), Dat(KeyDat.Dat){}
00343   explicit TKeyDat(const TKey& _Key): Key(_Key), Dat(){}
00344   TKeyDat(const TKey& _Key, const TDat& _Dat): Key(_Key), Dat(_Dat){}
00345   explicit TKeyDat(TSIn& SIn): Key(SIn), Dat(SIn){}
00346   void Save(TSOut& SOut) const {Key.Save(SOut); Dat.Save(SOut);}
00347   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm="");
00348   void SaveXml(TSOut& SOut, const TStr& Nm) const;
00349 
00350   TKeyDat& operator=(const TKeyDat& KeyDat){
00351     if (this!=&KeyDat){Key=KeyDat.Key; Dat=KeyDat.Dat;} return *this;}
00352   bool operator==(const TKeyDat& KeyDat) const {return Key==KeyDat.Key;}
00353   bool operator<(const TKeyDat& KeyDat) const {return Key<KeyDat.Key;}
00354 
00355   int GetPrimHashCd() const {return Key.GetPrimHashCd();}
00356   int GetSecHashCd() const {return Key.GetSecHashCd();}
00357 };
00358 
00359 template <class TKey, class TDat>
00360 void GetSwitchedKdV(const TVec<TKeyDat<TKey, TDat>, int>& SrcKdV, TVec<TKeyDat<TDat, TKey>, int>& DstKdV){
00361   const int Kds=SrcKdV.Len();
00362   DstKdV.Gen(Kds, 0);
00363   for (int KdN=0; KdN<Kds; KdN++){
00364     const TKeyDat<TKey, TDat>& SrcKd=SrcKdV[KdN];
00365     DstKdV.Add(TKeyDat<TDat, TKey>(SrcKd.Dat, SrcKd.Key));
00366   }
00367 }
00368 
00369 typedef TKeyDat<TInt, TInt> TIntKd;
00370 typedef TKeyDat<TInt, TUInt64> TIntUInt64Kd;
00371 typedef TKeyDat<TInt, TFlt> TIntFltKd;
00372 typedef TKeyDat<TIntPr, TFlt> TIntPrFltKd;
00373 typedef TKeyDat<TInt, TFltPr> TIntFltPrKd;
00374 typedef TKeyDat<TInt, TSFlt> TIntSFltKd;
00375 typedef TKeyDat<TInt, TStr> TIntStrKd;
00376 typedef TKeyDat<TUInt, TInt> TUIntIntKd;
00377 typedef TKeyDat<TUInt, TUInt> TUIntKd;
00378 typedef TKeyDat<TUInt64, TInt> TUInt64IntKd;
00379 typedef TKeyDat<TUInt64, TFlt> TUInt64FltKd;
00380 typedef TKeyDat<TUInt64, TStr> TUInt64StrKd;
00381 typedef TKeyDat<TFlt, TBool> TFltBoolKd;
00382 typedef TKeyDat<TFlt, TInt> TFltIntKd;
00383 typedef TKeyDat<TFlt, TUInt64> TFltUInt64Kd;
00384 typedef TKeyDat<TFlt, TIntPr> TFltIntPrKd;
00385 typedef TKeyDat<TFlt, TUInt> TFltUIntKd;
00386 typedef TKeyDat<TFlt, TFlt> TFltKd;
00387 typedef TKeyDat<TFlt, TStr> TFltStrKd;
00388 typedef TKeyDat<TFlt, TBool> TFltBoolKd;
00389 typedef TKeyDat<TFlt, TIntBoolPr> TFltIntBoolPrKd;
00390 typedef TKeyDat<TAscFlt, TInt> TAscFltIntKd;
00391 typedef TKeyDat<TStr, TBool> TStrBoolKd;
00392 typedef TKeyDat<TStr, TInt> TStrIntKd;
00393 typedef TKeyDat<TStr, TFlt> TStrFltKd;
00394 typedef TKeyDat<TStr, TAscFlt> TStrAscFltKd;
00395 typedef TKeyDat<TStr, TStr> TStrKd;
00396 
00397 // Key-Data comparator
00398 
00399 template <class TVal1, class TVal2>
00400 class TCmpKeyDatByDat {
00401 private:
00402   bool IsAsc;
00403 public:
00404   TCmpKeyDatByDat(const bool& AscSort=true) : IsAsc(AscSort) { }
00405   bool operator () (const TKeyDat<TVal1, TVal2>& P1, const TKeyDat<TVal1, TVal2>& P2) const {
00406     if (IsAsc) { return P1.Dat < P2.Dat; } else { return P2.Dat < P1.Dat; }
00407   }
00408 };
00409 
00410 //#//////////////////////////////////////////////
00412 
00419 template <class TVal, class TSizeTy = int>
00420 class TVec{
00421 public:
00422   typedef TVal* TIter;  
00423 protected:
00424   TSizeTy MxVals; 
00425   TSizeTy Vals;   
00426   TVal* ValT;     
00427 
00428   void Resize(const TSizeTy& _MxVals=-1);
00430   TStr GetXOutOfBoundsErrMsg(const TSizeTy& ValN) const;
00431 public:
00432   TVec(): MxVals(0), Vals(0), ValT(NULL){}
00433   TVec(const TVec<TVal, TSizeTy>& Vec);
00435   explicit TVec(const TSizeTy& _Vals){
00436     IAssert(0<=_Vals); MxVals=Vals=_Vals;
00437     if (_Vals==0){ValT=NULL;} else {ValT=new TVal[_Vals];}}
00439   TVec(const TSizeTy& _MxVals, const TSizeTy& _Vals){
00440     IAssert((0<=_Vals)&&(_Vals<=_MxVals)); MxVals=_MxVals; Vals=_Vals;
00441     if (_MxVals==0){ValT=NULL;} else {ValT=new TVal[_MxVals];}}
00443 
00446   explicit TVec(TVal *_ValT, const TSizeTy& _Vals):
00447     MxVals(-1), Vals(_Vals), ValT(_ValT){}
00448   ~TVec(){if ((ValT!=NULL) && (MxVals!=-1)){delete[] ValT;}}
00449   explicit TVec(TSIn& SIn): MxVals(0), Vals(0), ValT(NULL){Load(SIn);}
00450   void Load(TSIn& SIn);
00451   void Save(TSOut& SOut) const;
00452   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm="");
00453   void SaveXml(TSOut& SOut, const TStr& Nm) const;
00454   
00456   TVec<TVal, TSizeTy>& operator=(const TVec<TVal, TSizeTy>& Vec);
00458   TVec<TVal, TSizeTy>& operator+(const TVal& Val){Add(Val); return *this;}
00460   bool operator==(const TVec<TVal, TSizeTy>& Vec) const;
00462 
00464   bool operator<(const TVec<TVal, TSizeTy>& Vec) const;
00466   const TVal& operator[](const TSizeTy& ValN) const {
00467     AssertR((0<=ValN)&&(ValN<Vals), GetXOutOfBoundsErrMsg(ValN));
00468     return ValT[ValN];}
00470   TVal& operator[](const TSizeTy& ValN){
00471     AssertR((0<=ValN)&&(ValN<Vals), GetXOutOfBoundsErrMsg(ValN));
00472     return ValT[ValN];}
00474   TSizeTy GetMemUsed() const {
00475     return TSizeTy(2*sizeof(TSizeTy)+sizeof(TVal*)+MxVals*sizeof(TVal));}
00477   TSizeTy GetMemSize() const {
00478     return TSizeTy(2*sizeof(TVal)+sizeof(TSizeTy)*Vals);}
00479   
00481   int GetPrimHashCd() const;
00483   int GetSecHashCd() const;
00484   
00486   void Gen(const TSizeTy& _Vals){ IAssert(0<=_Vals);
00487     if (ValT!=NULL && MxVals!=-1){delete[] ValT;} MxVals=Vals=_Vals;
00488     if (MxVals==0){ValT=NULL;} else {ValT=new TVal[MxVals];}}
00490   void Gen(const TSizeTy& _MxVals, const TSizeTy& _Vals){ IAssert((0<=_Vals)&&(_Vals<=_MxVals));
00491     if (ValT!=NULL  && MxVals!=-1){delete[] ValT;} MxVals=_MxVals; Vals=_Vals;
00492     if (_MxVals==0){ValT=NULL;} else {ValT=new TVal[_MxVals];}}
00494 
00497   void GenExt(TVal *_ValT, const TSizeTy& _Vals){
00498     if (ValT!=NULL && MxVals!=-1){delete[] ValT;}
00499     MxVals=-1; Vals=_Vals; ValT=_ValT;}
00501 
00504   bool IsExt() const {return MxVals==-1;}
00506   void Reserve(const TSizeTy& _MxVals){Resize(_MxVals);}
00508   void Reserve(const TSizeTy& _MxVals, const TSizeTy& _Vals){ IAssert((0<=_Vals)&&(_Vals<=_MxVals)); Resize(_MxVals); Vals=_Vals;}
00510 
00513   void Clr(const bool& DoDel=true, const TSizeTy& NoDelLim=-1);
00515 
00517   void Trunc(const TSizeTy& _Vals=-1);
00519   void Pack();
00521 
00523   void MoveFrom(TVec<TVal, TSizeTy>& Vec);
00525   void Swap(TVec<TVal, TSizeTy>& Vec);
00526   
00528 
00530   bool Empty() const {return Vals==0;}
00532 
00535   TSizeTy Len() const {return Vals;}
00537   TSizeTy Reserved() const {return MxVals;}
00539   const TVal& Last() const {return GetVal(Len()-1);}
00541   TVal& Last(){return GetVal(Len()-1);}
00543   TSizeTy LastValN() const {return Len()-1;}
00545   const TVal& LastLast() const { AssertR(1<Vals, GetXOutOfBoundsErrMsg(Vals-2)); return ValT[Vals-2];}
00547   TVal& LastLast(){ AssertR(1<Vals, GetXOutOfBoundsErrMsg(Vals-2)); return ValT[Vals-2];}
00548   
00550   TIter BegI() const {return ValT;}
00552   TIter EndI() const {return ValT+Vals;}
00554   TIter GetI(const TSizeTy& ValN) const {return ValT+ValN;}
00555   
00557 
00559   TSizeTy Add(){ AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
00560     if (Vals==MxVals){Resize();} return Vals++;}
00562 
00564   TSizeTy Add(const TVal& Val){ AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
00565     if (Vals==MxVals){Resize();} ValT[Vals]=Val; return Vals++;}
00566   TSizeTy Add(TVal& Val){ AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
00567     if (Vals==MxVals){Resize();} ValT[Vals]=Val; return Vals++;}
00569   TSizeTy Add(const TVal& Val, const TSizeTy& ResizeLen){ AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
00570     if (Vals==MxVals){Resize(MxVals+ResizeLen);} ValT[Vals]=Val; return Vals++;}
00572   TSizeTy AddV(const TVec<TVal, TSizeTy>& ValV);
00574 
00576   TSizeTy AddSorted(const TVal& Val, const bool& Asc=true, const TSizeTy& _MxVals=-1);
00578 
00580   TSizeTy AddBackSorted(const TVal& Val, const bool& Asc);
00582 
00584   TSizeTy AddMerged(const TVal& Val);
00586 
00588   TSizeTy AddVMerged(const TVec<TVal, TSizeTy>& ValV);
00590 
00593   TSizeTy AddUnique(const TVal& Val);
00595   const TVal& GetVal(const TSizeTy& ValN) const {return operator[](ValN);}
00597   TVal& GetVal(const TSizeTy& ValN){return operator[](ValN);}
00599   void SetVal(const TSizeTy& ValN, const TVal& Val){AssertR((0<=ValN)&&(ValN<Vals), GetXOutOfBoundsErrMsg(ValN)); ValT[ValN] = Val;}
00601   void GetSubValV(const TSizeTy& BValN, const TSizeTy& EValN, TVec<TVal, TSizeTy>& ValV) const;
00603   void Ins(const TSizeTy& ValN, const TVal& Val);
00605   void Del(const TSizeTy& ValN);
00607   void Del(const TSizeTy& MnValN, const TSizeTy& MxValN);
00609   void DelLast(){Del(Len()-1);}
00611   bool DelIfIn(const TVal& Val);
00613   void DelAll(const TVal& Val);
00615   void PutAll(const TVal& Val);
00616   
00618   void Swap(const TSizeTy& ValN1, const TSizeTy& ValN2){ const TVal Val=ValT[ValN1]; ValT[ValN1]=ValT[ValN2]; ValT[ValN2]=Val;}
00620   static void SwapI(TIter LVal, TIter RVal){ const TVal Val=*LVal; *LVal=*RVal; *RVal=Val;}
00621   
00623 
00627   bool NextPerm();
00629 
00631   bool PrevPerm();
00632   
00633   // Sorting functions
00635   TSizeTy GetPivotValN(const TSizeTy& LValN, const TSizeTy& RValN) const;
00637 
00639   void BSort(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc);
00641 
00643   void ISort(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc);
00645 
00648   TSizeTy Partition(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc);
00650 
00653   void QSort(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc);
00655 
00658   void Sort(const bool& Asc=true);
00660   bool IsSorted(const bool& Asc=true) const;
00662   void Shuffle(TRnd& Rnd);
00664   void Reverse();
00666   void Reverse(TSizeTy LValN, TSizeTy RValN){ Assert(LValN>=0 && RValN<Len()); while (LValN < RValN){Swap(LValN++, RValN--);} }
00668   void Merge();
00669 
00671   template <class TCmp>
00672   static TIter GetPivotValNCmp(const TIter& BI, const TIter& EI, const TCmp& Cmp) {
00673     TSizeTy SubVals=TSizeTy(EI-BI); if (SubVals > TInt::Mx-1) { SubVals = TInt::Mx-1; }
00674     const TSizeTy ValN1=TInt::GetRnd(SubVals), ValN2=TInt::GetRnd(SubVals), ValN3=TInt::GetRnd(SubVals);
00675     const TVal& Val1 = *(BI+ValN1);  const TVal& Val2 = *(BI+ValN2); const TVal& Val3 = *(BI+ValN3);
00676     if (Cmp(Val1, Val2)) {
00677       if (Cmp(Val2, Val3)) return BI+ValN2;
00678       else if (Cmp(Val3, Val1)) return BI+ValN1;
00679       else return BI+ValN3;
00680     } else {
00681       if (Cmp(Val1, Val3)) return BI+ValN1;
00682       else if (Cmp(Val3, Val2)) return BI+ValN2;
00683       else return BI+ValN3; } }
00685   template <class TCmp>
00686   static TIter PartitionCmp(TIter BI, TIter EI, const TVal Pivot, const TCmp& Cmp) {
00687     forever {
00688       while (Cmp(*BI, Pivot)){++BI;}  --EI;
00689       while (Cmp(Pivot, *EI)){--EI;}
00690       if (!(BI<EI)){return BI;}  SwapI(BI, EI);  ++BI; } }
00692   template <class TCmp>
00693   static void BSortCmp(TIter BI, TIter EI, const TCmp& Cmp) {
00694     for (TIter i = BI; i != EI; ++i) {
00695       for (TIter j = EI-1; j != i; --j) {
00696         if (Cmp(*j, *(j-1))) { SwapI(j, j-1); } } } }
00698   template <class TCmp>
00699   static void ISortCmp(TIter BI, TIter EI, const TCmp& Cmp) {
00700     if (BI + 1 < EI) {
00701       for (TIter i = BI, j; i != EI; ++i) { TVal Tmp=*i;  j=i;
00702         while (j > BI && Cmp(Tmp, *(j-1))) { *j = *(j-1); --j; } *j=Tmp; } } }
00704   template <class TCmp>
00705   static void QSortCmp(TIter BI, TIter EI, const TCmp& Cmp) {
00706     if (BI + 1 < EI) {
00707       if (EI - BI < 20) { ISortCmp(BI, EI, Cmp); }
00708       else { const TVal Val = *GetPivotValNCmp(BI, EI, Cmp);
00709         TIter Split = PartitionCmp(BI, EI, Val, Cmp);
00710         QSortCmp(BI, Split, Cmp);  QSortCmp(Split, EI, Cmp); } } }
00712   template <class TCmp>
00713   void SortCmp(const TCmp& Cmp){ QSortCmp(BegI(), EndI(), Cmp);}
00715   template <class TCmp>
00716   bool IsSortedCmp(const TCmp& Cmp) const {
00717     if (EndI() == BegI()) return true;
00718     for (TIter i = BegI(); i != EndI()-1; ++i) {
00719       if (Cmp(*(i+1), *i)){return false;} } return true; }
00720   
00722 
00724   void Intrs(const TVec<TVal, TSizeTy>& ValV);
00726 
00728   void Union(const TVec<TVal, TSizeTy>& ValV);
00730 
00733   void Diff(const TVec<TVal, TSizeTy>& ValV);
00735 
00737   void Intrs(const TVec<TVal, TSizeTy>& ValV, TVec<TVal, TSizeTy>& DstValV) const;
00739 
00741   void Union(const TVec<TVal, TSizeTy>& ValV, TVec<TVal, TSizeTy>& DstValV) const;
00743 
00746   void Diff(const TVec<TVal, TSizeTy>& ValV, TVec<TVal, TSizeTy>& DstValV) const;
00748 
00750   TSizeTy IntrsLen(const TVec<TVal, TSizeTy>& ValV) const;
00752 
00754   TSizeTy UnionLen(const TVec<TVal, TSizeTy>& ValV) const;
00755   
00757   TSizeTy Count(const TVal& Val) const;
00759 
00762   TSizeTy SearchBin(const TVal& Val) const;
00764 
00766   TSizeTy SearchBin(const TVal& Val, TSizeTy& InsValN) const;
00768 
00771   TSizeTy SearchForw(const TVal& Val, const TSizeTy& BValN=0) const;
00773 
00775   TSizeTy SearchBack(const TVal& Val) const;
00777 
00779   TSizeTy SearchVForw(const TVec<TVal, TSizeTy>& ValV, const TSizeTy& BValN=0) const;
00780 
00782   bool IsIn(const TVal& Val) const {return SearchForw(Val)!=-1;}
00784 
00786   bool IsIn(const TVal& Val, TSizeTy& ValN) const { ValN=SearchForw(Val); return ValN!=-1;}
00788 
00790   bool IsInBin(const TVal& Val) const {return SearchBin(Val)!=-1;}
00792   const TVal& GetDat(const TVal& Val) const { return GetVal(SearchForw(Val));}
00794 
00796   TVal& GetAddDat(const TVal& Val){ AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
00797     const TSizeTy ValN=SearchForw(Val); if (ValN==-1){Add(Val); return Last();} else {return GetVal(ValN);}}
00799   TSizeTy GetMxValN() const;
00800   
00802   static TVec<TVal, TSizeTy> GetV(const TVal& Val1){
00803     TVec<TVal, TSizeTy> V(1, 0); V.Add(Val1); return V;}
00805   static TVec<TVal, TSizeTy> GetV(const TVal& Val1, const TVal& Val2){
00806     TVec<TVal, TSizeTy> V(2, 0); V.Add(Val1); V.Add(Val2); return V;}
00808   static TVec<TVal, TSizeTy> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3){
00809     TVec<TVal, TSizeTy> V(3, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); return V;}
00811   static TVec<TVal, TSizeTy> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4){
00812     TVec<TVal, TSizeTy> V(4, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); return V;}
00814   static TVec<TVal, TSizeTy> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4, const TVal& Val5){
00815     TVec<TVal, TSizeTy> V(5, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); V.Add(Val5); return V;}
00817   static TVec<TVal, TSizeTy> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4, const TVal& Val5, const TVal& Val6){
00818     TVec<TVal, TSizeTy> V(6, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); V.Add(Val5); V.Add(Val6); return V;}
00820   static TVec<TVal, TSizeTy> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4, const TVal& Val5, const TVal& Val6, const TVal& Val7){
00821     TVec<TVal, TSizeTy> V(7, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); V.Add(Val5); V.Add(Val6); V.Add(Val7); return V;}
00823   static TVec<TVal, TSizeTy> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4, const TVal& Val5, const TVal& Val6, const TVal& Val7, const TVal& Val8){
00824     TVec<TVal, TSizeTy> V(8, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); V.Add(Val5); V.Add(Val6); V.Add(Val7); V.Add(Val8); return V;}
00826   static TVec<TVal, TSizeTy> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4, const TVal& Val5, const TVal& Val6, const TVal& Val7, const TVal& Val8, const TVal& Val9){
00827     TVec<TVal, TSizeTy> V(9, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); V.Add(Val5); V.Add(Val6); V.Add(Val7); V.Add(Val8); V.Add(Val9); return V;}
00828 };
00829 
00830 template <class TVal, class TSizeTy>
00831 void TVec<TVal, TSizeTy>::Resize(const TSizeTy& _MxVals){
00832   IAssertR(MxVals!=-1, TStr::Fmt("Can not increase the capacity of the vector. %s. [Program failed to allocate more memory. Solution: Get a bigger machine and a 64-bit compiler.]", GetTypeNm(*this).CStr()).CStr());
00833   if (_MxVals==-1){
00834     if (Vals==0){MxVals=16;} else {MxVals*=2;}
00835   } else {
00836     if (_MxVals<=MxVals){return;} else {MxVals=_MxVals;}
00837   }
00838   if (ValT==NULL){
00839     try {ValT=new TVal[MxVals];}
00840     catch (std::exception Ex){
00841       FailR(TStr::Fmt("TVec::Resize: %s, Length:%s, Capacity:%s, New capacity:%s, Type:%s [Program failed to allocate more memory. Solution: Get a bigger machine and a 64-bit compiler.]",
00842         Ex.what(), TInt::GetStr(Vals).CStr(), TInt::GetStr(MxVals).CStr(), TInt::GetStr(_MxVals).CStr(), GetTypeNm(*this).CStr()).CStr());}
00843   } else {
00844     TVal* NewValT = NULL;
00845     try {
00846       NewValT=new TVal[MxVals];}
00847     catch (std::exception Ex){
00848       FailR(TStr::Fmt("TVec::Resize: %s, Length:%s, Capacity:%s, New capacity:%s, Type:%s [Program failed to allocate more memory. Solution: Get a bigger machine and a 64-bit compiler.]",
00849         Ex.what(), TInt::GetStr(Vals).CStr(), TInt::GetStr(MxVals).CStr(), TInt::GetStr(_MxVals).CStr(), GetTypeNm(*this).CStr()).CStr());}
00850     IAssert(NewValT!=NULL);
00851     for (TSizeTy ValN=0; ValN<Vals; ValN++){NewValT[ValN]=ValT[ValN];}
00852     delete[] ValT; ValT=NewValT;
00853   }
00854 }
00855 
00856 template <class TVal, class TSizeTy>
00857 TStr TVec<TVal, TSizeTy>::GetXOutOfBoundsErrMsg(const TSizeTy& ValN) const {
00858   return TStr()+
00859   "Index:"+TInt::GetStr(ValN)+
00860   " Vals:"+TInt::GetStr(Vals)+
00861   " MxVals:"+TInt::GetStr(MxVals)+
00862   " Type:"+GetTypeNm(*this);
00863 }
00864 
00865 template <class TVal, class TSizeTy>
00866 TVec<TVal, TSizeTy>::TVec(const TVec<TVal, TSizeTy>& Vec){
00867   MxVals=Vec.MxVals; Vals=Vec.Vals;
00868   if (MxVals==0){ValT=NULL;} else {ValT=new TVal[MxVals];}
00869   for (TSizeTy ValN=0; ValN<Vec.Vals; ValN++){ValT[ValN]=Vec.ValT[ValN];}
00870 }
00871 
00872 template <class TVal, class TSizeTy>
00873 void TVec<TVal, TSizeTy>::Load(TSIn& SIn){
00874   if ((ValT!=NULL)&&(MxVals!=-1)){delete[] ValT;}
00875   SIn.Load(MxVals); SIn.Load(Vals); MxVals=Vals;
00876   if (MxVals==0){ValT=NULL;} else {ValT=new TVal[MxVals];}
00877   for (TSizeTy ValN=0; ValN<Vals; ValN++){ValT[ValN]=TVal(SIn);}
00878 }
00879 
00880 template <class TVal, class TSizeTy>
00881 void TVec<TVal, TSizeTy>::Save(TSOut& SOut) const {
00882   if (MxVals!=-1){SOut.Save(MxVals);} else {SOut.Save(Vals);}
00883   SOut.Save(Vals);
00884   for (TSizeTy ValN=0; ValN<Vals; ValN++){ValT[ValN].Save(SOut);}
00885 }
00886 
00887 template <class TVal, class TSizeTy>
00888 TVec<TVal, TSizeTy>& TVec<TVal, TSizeTy>::operator=(const TVec<TVal, TSizeTy>& Vec){
00889   if (this!=&Vec){
00890     if ((ValT!=NULL)&&(MxVals!=-1)){delete[] ValT;}
00891     MxVals=Vals=Vec.Vals;
00892     if (MxVals==0){ValT=NULL;} else {ValT=new TVal[MxVals];}
00893     for (TSizeTy ValN=0; ValN<Vec.Vals; ValN++){ValT[ValN]=Vec.ValT[ValN];}
00894   }
00895   return *this;
00896 }
00897 
00898 template <class TVal, class TSizeTy>
00899 bool TVec<TVal, TSizeTy>::operator==(const TVec<TVal, TSizeTy>& Vec) const {
00900   if (this==&Vec){return true;}
00901   if (Len()!=Vec.Len()){return false;}
00902   for (TSizeTy ValN=0; ValN<Vals; ValN++){
00903     if (ValT[ValN]!=Vec.ValT[ValN]){return false;}}
00904   return true;
00905 }
00906 
00907 template <class TVal, class TSizeTy>
00908 bool TVec<TVal, TSizeTy>::operator<(const TVec<TVal, TSizeTy>& Vec) const {
00909   if (this==&Vec){return false;}
00910   if (Len()==Vec.Len()){
00911     for (TSizeTy ValN=0; ValN<Vals; ValN++){
00912       if (ValT[ValN]<Vec.ValT[ValN]){return true;}
00913       else if (ValT[ValN]>Vec.ValT[ValN]){return false;}
00914       else {}
00915     }
00916     return false;
00917   } else {
00918     return Len()<Vec.Len();
00919   }
00920 }
00921 
00922 // Improved hashing of vectors (Jure Apr 20 2013)
00923 // This change makes binary representation of vectors incompatible with previous code.
00924 // Previous hash functions are available for compatibility in class TVecHashF_OldGLib
00925 template <class TVal, class TSizeTy>
00926 int TVec<TVal, TSizeTy>::GetPrimHashCd() const {
00927   int hc = 0;
00928   for (TSizeTy i=0; i<Vals; i++){
00929     hc = TPairHashImpl::GetHashCd(hc, ValT[i].GetPrimHashCd()); 
00930   }
00931   return hc; 
00932 }
00933 
00934 // Improved hashing of vectors (Jure Apr 20 2013)
00935 // This change makes binary representation of vectors incompatible with previous code.
00936 // Previous hash functions are available for compatibility in class TVecHashF_OldGLib
00937 template <class TVal, class TSizeTy>
00938 int TVec<TVal, TSizeTy>::GetSecHashCd() const {
00939   int hc = 0;
00940   for (TSizeTy i=0; i<Vals; i++){
00941     hc = TPairHashImpl::GetHashCd(hc, ValT[i].GetSecHashCd()); 
00942   }
00943   if (Vals > 0) { 
00944     hc = TPairHashImpl::GetHashCd(hc, ValT[0].GetSecHashCd()); }
00945   return hc; 
00946 }
00947 
00948 template <class TVal, class TSizeTy>
00949 void TVec<TVal, TSizeTy>::Clr(const bool& DoDel, const TSizeTy& NoDelLim){
00950   if ((DoDel)||((!DoDel)&&(NoDelLim!=-1)&&(MxVals>NoDelLim))){
00951     if ((ValT!=NULL)&&(MxVals!=-1)){delete[] ValT;}
00952     MxVals=Vals=0; ValT=NULL;
00953   } else {
00954     IAssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!"); 
00955     Vals=0;
00956   }
00957 }
00958 
00959 template <class TVal, class TSizeTy>
00960 void TVec<TVal, TSizeTy>::Trunc(const TSizeTy& _Vals){
00961   IAssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
00962   IAssert((_Vals==-1)||(_Vals>=0));
00963   if ((_Vals!=-1)&&(_Vals>=Vals)){
00964     return;
00965   } else
00966     if (((_Vals==-1)&&(Vals==0))||(_Vals==0)){
00967       if (ValT!=NULL){delete[] ValT;}
00968       MxVals=Vals=0; ValT=NULL;
00969     } else {
00970       if (_Vals==-1){
00971         if (MxVals==Vals){return;} else {MxVals=Vals;}
00972       } else {
00973         MxVals=Vals=_Vals;
00974       }
00975       TVal* NewValT=new TVal[MxVals];
00976       IAssert(NewValT!=NULL);
00977       for (TSizeTy ValN=0; ValN<Vals; ValN++){NewValT[ValN]=ValT[ValN];}
00978       delete[] ValT; ValT=NewValT;
00979     }
00980 }
00981 
00982 template <class TVal, class TSizeTy>
00983 void TVec<TVal, TSizeTy>::Pack(){
00984   IAssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
00985   if (Vals==0){
00986     if (ValT!=NULL){delete[] ValT;} ValT=NULL;
00987   } else
00988     if (Vals<MxVals){
00989       MxVals=Vals;
00990       TVal* NewValT=new TVal[MxVals];
00991       IAssert(NewValT!=NULL);
00992       for (TSizeTy ValN=0; ValN<Vals; ValN++){NewValT[ValN]=ValT[ValN];}
00993       delete[] ValT; ValT=NewValT;
00994     }
00995 }
00996 
00997 template <class TVal, class TSizeTy>
00998 void TVec<TVal, TSizeTy>::MoveFrom(TVec<TVal, TSizeTy>& Vec){
00999   if (this!=&Vec){
01000     if (ValT!=NULL && MxVals!=-1){delete[] ValT;}
01001     MxVals=Vec.MxVals; Vals=Vec.Vals; ValT=Vec.ValT;
01002     Vec.MxVals=0; Vec.Vals=0; Vec.ValT=NULL;
01003   }
01004 }
01005 
01006 template <class TVal, class TSizeTy>
01007 void TVec<TVal, TSizeTy>::Swap(TVec<TVal, TSizeTy>& Vec){
01008   if (this!=&Vec){
01009     ::Swap(MxVals, Vec.MxVals);
01010     ::Swap(Vals, Vec.Vals);
01011     ::Swap(ValT, Vec.ValT);
01012   }
01013 }
01014 
01015 template <class TVal, class TSizeTy>
01016 TSizeTy TVec<TVal, TSizeTy>::AddV(const TVec<TVal, TSizeTy>& ValV){
01017   AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
01018   for (TSizeTy ValN=0; ValN<ValV.Vals; ValN++){Add(ValV[ValN]);}
01019   return Len();
01020 }
01021 
01022 template <class TVal, class TSizeTy>
01023 TSizeTy TVec<TVal, TSizeTy>::AddSorted(const TVal& Val, const bool& Asc, const TSizeTy& _MxVals){
01024   AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
01025   TSizeTy ValN=Add(Val);
01026   if (Asc){
01027     while ((ValN>0)&&(ValT[ValN]<ValT[ValN-1])){
01028       Swap(ValN, ValN-1); ValN--;}
01029   } else {
01030     while ((ValN>0)&&(ValT[ValN]>ValT[ValN-1])){
01031       Swap(ValN, ValN-1); ValN--;}
01032   }
01033   if ((_MxVals!=-1)&&(Len()>_MxVals)){Del(_MxVals, Len()-1);}
01034   return ValN;
01035 }
01036 
01037 template <class TVal, class TSizeTy>
01038 TSizeTy TVec<TVal, TSizeTy>::AddBackSorted(const TVal& Val, const bool& Asc){
01039   AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
01040   Add();
01041   TSizeTy ValN=Vals-2;
01042   while ((ValN>=0)&&((Asc&&(Val<ValT[ValN]))||(!Asc&&(Val>ValT[ValN])))){
01043     ValT[ValN+1]=ValT[ValN]; ValN--;}
01044   ValT[ValN+1]=Val;
01045   return ValN+1;
01046 }
01047 
01048 template <class TVal, class TSizeTy>
01049 TSizeTy TVec<TVal, TSizeTy>::AddMerged(const TVal& Val){
01050   AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
01051   TSizeTy ValN=SearchBin(Val);
01052   if (ValN==-1){return AddSorted(Val);}
01053   else {GetVal(ValN)=Val; return -1;}
01054 }
01055 
01056 template <class TVal, class TSizeTy>
01057 TSizeTy TVec<TVal, TSizeTy>::AddVMerged(const TVec<TVal, TSizeTy>& ValV){
01058   AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
01059   for (TSizeTy ValN=0; ValN<ValV.Vals; ValN++){AddMerged(ValV[ValN]);}
01060   return Len();
01061 }
01062 
01063 template <class TVal, class TSizeTy>
01064 TSizeTy TVec<TVal, TSizeTy>::AddUnique(const TVal& Val){
01065   AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
01066   TSizeTy ValN=SearchForw(Val);
01067   if (ValN==-1){return Add(Val);}
01068   else {GetVal(ValN)=Val; return -1;}
01069 }
01070 
01071 template <class TVal, class TSizeTy>
01072 void TVec<TVal, TSizeTy>::GetSubValV(const TSizeTy& _BValN, const TSizeTy& _EValN, TVec<TVal, TSizeTy>& SubValV) const {
01073   const TSizeTy BValN=TInt::GetInRng(_BValN, 0, Len()-1);
01074   const TSizeTy EValN=TInt::GetInRng(_EValN, 0, Len()-1);
01075   const TSizeTy SubVals=TInt::GetMx(0, EValN-BValN+1);
01076   SubValV.Gen(SubVals, 0);
01077   for (TSizeTy ValN=BValN; ValN<=EValN; ValN++){
01078     SubValV.Add(GetVal(ValN));}
01079 }
01080 
01081 template <class TVal, class TSizeTy>
01082 void TVec<TVal, TSizeTy>::Ins(const TSizeTy& ValN, const TVal& Val){
01083   AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
01084   Add();  Assert((0<=ValN)&&(ValN<Vals));
01085   for (TSizeTy MValN=Vals-2; MValN>=ValN; MValN--){ValT[MValN+1]=ValT[MValN];}
01086   ValT[ValN]=Val;
01087 }
01088 
01089 template <class TVal, class TSizeTy>
01090 void TVec<TVal, TSizeTy>::Del(const TSizeTy& ValN){
01091   AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
01092   Assert((0<=ValN)&&(ValN<Vals));
01093   for (TSizeTy MValN=ValN+1; MValN<Vals; MValN++){
01094     ValT[MValN-1]=ValT[MValN];}
01095   ValT[--Vals]=TVal();
01096 }
01097 
01098 template <class TVal, class TSizeTy>
01099 void TVec<TVal, TSizeTy>::Del(const TSizeTy& MnValN, const TSizeTy& MxValN){
01100   AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
01101   Assert((0<=MnValN)&&(MnValN<Vals)&&(0<=MxValN)&&(MxValN<Vals));
01102   Assert(MnValN<=MxValN);
01103   for (TSizeTy ValN=MxValN+1; ValN<Vals; ValN++){
01104     ValT[MnValN+ValN-MxValN-1]=ValT[ValN];}
01105   for (TSizeTy ValN=Vals-MxValN+MnValN-1; ValN<Vals; ValN++){
01106     ValT[ValN]=TVal();}
01107   Vals-=MxValN-MnValN+1;
01108 }
01109 
01110 template <class TVal, class TSizeTy>
01111 bool TVec<TVal, TSizeTy>::DelIfIn(const TVal& Val){
01112   AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
01113   TSizeTy ValN=SearchForw(Val);
01114   if (ValN!=-1){Del(ValN); return true;}
01115   else {return false;}
01116 }
01117 
01118 template <class TVal, class TSizeTy>
01119 void TVec<TVal, TSizeTy>::DelAll(const TVal& Val){
01120   AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
01121   TSizeTy ValN;
01122   while ((ValN=SearchForw(Val))!=-1){Del(ValN);}
01123 }
01124 
01125 template <class TVal, class TSizeTy>
01126 void TVec<TVal, TSizeTy>::PutAll(const TVal& Val){
01127   for (TSizeTy ValN=0; ValN<Vals; ValN++){ValT[ValN]=Val;}
01128 }
01129 
01130 template <class TVal, class TSizeTy>
01131 void TVec<TVal, TSizeTy>::BSort(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc){
01132   for (TSizeTy ValN1=MnLValN; ValN1<=MxRValN; ValN1++){
01133     for (TSizeTy ValN2=MxRValN; ValN2>ValN1; ValN2--){
01134       if (Asc){
01135         if (ValT[ValN2]<ValT[ValN2-1]){Swap(ValN2, ValN2-1);}
01136       } else {
01137         if (ValT[ValN2]>ValT[ValN2-1]){Swap(ValN2, ValN2-1);}
01138       }
01139     }
01140   }
01141 }
01142 
01143 template <class TVal, class TSizeTy>
01144 void TVec<TVal, TSizeTy>::ISort(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc){
01145   if (MnLValN<MxRValN){
01146     for (TSizeTy ValN1=MnLValN+1; ValN1<=MxRValN; ValN1++){
01147       TVal Val=ValT[ValN1]; TSizeTy ValN2=ValN1;
01148       if (Asc){
01149         while ((ValN2>MnLValN)&&(ValT[ValN2-1]>Val)){
01150           ValT[ValN2]=ValT[ValN2-1]; ValN2--;}
01151       } else {
01152         while ((ValN2>MnLValN)&&(ValT[ValN2-1]<Val)){
01153           ValT[ValN2]=ValT[ValN2-1]; ValN2--;}
01154       }
01155       ValT[ValN2]=Val;
01156     }
01157   }
01158 }
01159 
01160 template <class TVal, class TSizeTy>
01161 TSizeTy TVec<TVal, TSizeTy>::GetPivotValN(const TSizeTy& LValN, const TSizeTy& RValN) const {
01162   TSizeTy SubVals=RValN-LValN+1;
01163   if (SubVals > TInt::Mx-1) { SubVals = TInt::Mx-1; }
01164   const TSizeTy ValN1=LValN+TInt::GetRnd(int(SubVals));
01165   const TSizeTy ValN2=LValN+TInt::GetRnd(int(SubVals));
01166   const TSizeTy ValN3=LValN+TInt::GetRnd(int(SubVals));
01167   const TVal& Val1=ValT[ValN1];
01168   const TVal& Val2=ValT[ValN2];
01169   const TVal& Val3=ValT[ValN3];
01170   if (Val1<Val2){
01171     if (Val2<Val3){return ValN2;}
01172     else if (Val3<Val1){return ValN1;}
01173     else {return ValN3;}
01174   } else {
01175     if (Val1<Val3){return ValN1;}
01176     else if (Val3<Val2){return ValN2;}
01177     else {return ValN3;}
01178   }
01179 }
01180 
01181 template <class TVal, class TSizeTy>
01182 TSizeTy TVec<TVal, TSizeTy>::Partition(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc){
01183   TSizeTy PivotValN=GetPivotValN(MnLValN, MxRValN);
01184   Swap(PivotValN, MnLValN);
01185   TVal PivotVal=ValT[MnLValN];
01186   TSizeTy LValN=MnLValN-1;  TSizeTy RValN=MxRValN+1;
01187   forever {
01188     if (Asc){
01189       do {RValN--;} while (ValT[RValN]>PivotVal);
01190       do {LValN++;} while (ValT[LValN]<PivotVal);
01191     } else {
01192       do {RValN--;} while (ValT[RValN]<PivotVal);
01193       do {LValN++;} while (ValT[LValN]>PivotVal);
01194     }
01195     if (LValN<RValN){Swap(LValN, RValN);}
01196     else {return RValN;}
01197   };
01198 }
01199 
01200 template <class TVal, class TSizeTy>
01201 void TVec<TVal, TSizeTy>::QSort(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc){
01202   if (MnLValN<MxRValN){
01203     if (MxRValN-MnLValN<20){
01204       ISort(MnLValN, MxRValN, Asc);
01205     } else {
01206       TSizeTy SplitValN=Partition(MnLValN, MxRValN, Asc);
01207       QSort(MnLValN, SplitValN, Asc);
01208       QSort(SplitValN+1, MxRValN, Asc);
01209     }
01210   }
01211 }
01212 
01213 template <class TVal, class TSizeTy>
01214 void TVec<TVal, TSizeTy>::Sort(const bool& Asc){
01215   QSort(0, Len()-1, Asc);
01216 }
01217 
01218 template <class TVal, class TSizeTy>
01219 bool TVec<TVal, TSizeTy>::IsSorted(const bool& Asc) const {
01220   if (Asc){
01221     for (TSizeTy ValN=0; ValN<Vals-1; ValN++){
01222       if (ValT[ValN]>ValT[ValN+1]){return false;}}
01223   } else {
01224     for (TSizeTy ValN=0; ValN<Vals-1; ValN++){
01225       if (ValT[ValN]<ValT[ValN+1]){return false;}}
01226   }
01227   return true;
01228 }
01229 
01230 template <class TVal, class TSizeTy>
01231 void TVec<TVal, TSizeTy>::Shuffle(TRnd& Rnd){
01232   if (Len() < TInt::Mx) {
01233     for (TSizeTy ValN=0; ValN<Vals-1; ValN++){
01234       const int Range = int(Vals-ValN);
01235       Swap(ValN, ValN+Rnd.GetUniDevInt(Range));
01236     }
01237   } else {
01238     for (TSizeTy ValN=0; ValN<Vals-1; ValN++){
01239       const TSizeTy Range = Vals-ValN;
01240       Swap(ValN, TSizeTy(ValN+Rnd.GetUniDevInt64(Range)));
01241     }
01242   }
01243 }
01244 
01245 template <class TVal, class TSizeTy>
01246 void TVec<TVal, TSizeTy>::Reverse(){
01247   for (TSizeTy ValN=0; ValN<Vals/2; ValN++){
01248     Swap(ValN, Vals-ValN-1);}
01249 }
01250 
01251 template <class TVal, class TSizeTy>
01252 void TVec<TVal, TSizeTy>::Merge(){
01253   AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
01254   TVec<TVal, TSizeTy> SortedVec(*this); SortedVec.Sort();
01255   Clr();
01256   for (TSizeTy ValN=0; ValN<SortedVec.Len(); ValN++){
01257     if ((ValN==0)||(SortedVec[ValN-1]!=SortedVec[ValN])){
01258       Add(SortedVec[ValN]);}
01259   }
01260 }
01261 
01262 template <class TVal, class TSizeTy>
01263 bool TVec<TVal, TSizeTy>::NextPerm() {
01264   // start with a sorted sequence to obtain all permutations
01265   TSizeTy First = 0, Last = Len(), Next = Len()-1;
01266   if (Last < 2) return false;
01267   for(; ; ) {
01268     // find rightmost element smaller than successor
01269     TSizeTy Next1 = Next;
01270     if (GetVal(--Next) < GetVal(Next1)) { // swap with rightmost element that's smaller, flip suffix
01271       TSizeTy Mid = Last;
01272       for (; GetVal(Next) >= GetVal(--Mid); ) { }
01273       Swap(Next, Mid);
01274       Reverse(Next1, Last-1);
01275       return true;
01276     }
01277     if (Next == First) { // pure descending, flip all
01278       Reverse();
01279       return false;
01280     }
01281   }
01282 }
01283 
01284 template <class TVal, class TSizeTy>
01285 bool TVec<TVal, TSizeTy>::PrevPerm() {
01286   TSizeTy First = 0, Last = Len(), Next = Len()-1;
01287   if (Last < 2) return false;
01288   for(; ; ) {
01289     // find rightmost element not smaller than successor
01290     TSizeTy Next1 = Next;
01291     if (GetVal(--Next) >= GetVal(Next1)) { // swap with rightmost element that's not smaller, flip suffix
01292       TSizeTy Mid = Last;
01293       for (; GetVal(Next) < GetVal(--Mid); ) { }
01294       Swap(Next, Mid);
01295       Reverse(Next1, Last);
01296       return true;
01297     }
01298     if (Next == First) { // pure descending, flip all
01299       Reverse();
01300       return false;
01301     }
01302   }
01303 }
01304 
01305 template <class TVal, class TSizeTy>
01306 void TVec<TVal, TSizeTy>::Intrs(const TVec<TVal, TSizeTy>& ValV){
01307   TVec<TVal, TSizeTy> IntrsVec;
01308   Intrs(ValV, IntrsVec);
01309   MoveFrom(IntrsVec);
01310 }
01311 
01312 template <class TVal, class TSizeTy>
01313 void TVec<TVal, TSizeTy>::Union(const TVec<TVal, TSizeTy>& ValV){
01314   TVec<TVal, TSizeTy> UnionVec;
01315   Union(ValV, UnionVec);
01316   MoveFrom(UnionVec);
01317 }
01318 
01319 template <class TVal, class TSizeTy>
01320 void TVec<TVal, TSizeTy>::Diff(const TVec<TVal, TSizeTy>& ValV){
01321   TVec<TVal, TSizeTy> DiffVec;
01322   Diff(ValV, DiffVec);
01323   MoveFrom(DiffVec);
01324 }
01325 
01326 template <class TVal, class TSizeTy>
01327 void TVec<TVal, TSizeTy>::Intrs(const TVec<TVal, TSizeTy>& ValV, TVec<TVal, TSizeTy>& DstValV) const {
01328   DstValV.Clr();
01329   TSizeTy ValN1=0, ValN2=0;
01330   while ((ValN1<Len())&&(ValN2<ValV.Len())){
01331     const TVal& Val1=GetVal(ValN1);
01332     while ((ValN2<ValV.Len())&&(Val1>ValV.GetVal(ValN2))){
01333       ValN2++;}
01334     if ((ValN2<ValV.Len())&&(Val1==ValV.GetVal(ValN2))){
01335       DstValV.Add(Val1); ValN2++;}
01336     ValN1++;
01337   }
01338 }
01339 
01340 template <class TVal, class TSizeTy>
01341 void TVec<TVal, TSizeTy>::Union(const TVec<TVal, TSizeTy>& ValV, TVec<TVal, TSizeTy>& DstValV) const {
01342   DstValV.Gen(TInt::GetMx(Len(), ValV.Len()), 0);
01343   TSizeTy ValN1=0, ValN2=0;
01344   while ((ValN1<Len())&&(ValN2<ValV.Len())){
01345     const TVal& Val1=GetVal(ValN1);
01346     const TVal& Val2=ValV.GetVal(ValN2);
01347     if (Val1<Val2){DstValV.Add(Val1); ValN1++;}
01348     else if (Val1>Val2){DstValV.Add(Val2); ValN2++;}
01349     else {DstValV.Add(Val1); ValN1++; ValN2++;}
01350   }
01351   for (TSizeTy RestValN1=ValN1; RestValN1<Len(); RestValN1++){
01352     DstValV.Add(GetVal(RestValN1));}
01353   for (TSizeTy RestValN2=ValN2; RestValN2<ValV.Len(); RestValN2++){
01354     DstValV.Add(ValV.GetVal(RestValN2));}
01355 }
01356 
01357 template <class TVal, class TSizeTy>
01358 void TVec<TVal, TSizeTy>::Diff(const TVec<TVal, TSizeTy>& ValV, TVec<TVal, TSizeTy>& DstValV) const {
01359   DstValV.Clr();
01360   TSizeTy ValN1=0, ValN2=0;
01361   while (ValN1<Len() && ValN2<ValV.Len()) {
01362     const TVal& Val1 = GetVal(ValN1);
01363     while (ValN2<ValV.Len() && Val1>ValV.GetVal(ValN2)) ValN2++;
01364     if (ValN2<ValV.Len()) {
01365       if (Val1!=ValV.GetVal(ValN2)) { DstValV.Add(Val1); }
01366       ValN1++;
01367     }
01368   }
01369   for (TSizeTy RestValN1=ValN1; RestValN1<Len(); RestValN1++){
01370     DstValV.Add(GetVal(RestValN1));}
01371 }
01372 
01373 template <class TVal, class TSizeTy>
01374 TSizeTy TVec<TVal, TSizeTy>::IntrsLen(const TVec<TVal, TSizeTy>& ValV) const {
01375   TSizeTy Cnt=0, ValN1=0, ValN2=0;
01376   while ((ValN1<Len())&&(ValN2<ValV.Len())){
01377     const TVal& Val1=GetVal(ValN1);
01378     while ((ValN2<ValV.Len())&&(Val1>ValV.GetVal(ValN2))){
01379       ValN2++;}
01380     if ((ValN2<ValV.Len())&&(Val1==ValV.GetVal(ValN2))){
01381       ValN2++; Cnt++;}
01382     ValN1++;
01383   }
01384   return Cnt;
01385 }
01386     
01387 template <class TVal, class TSizeTy>
01388 TSizeTy TVec<TVal, TSizeTy>::UnionLen(const TVec<TVal, TSizeTy>& ValV) const {
01389   TSizeTy Cnt = 0, ValN1 = 0, ValN2 = 0;
01390   while ((ValN1 < Len()) && (ValN2 < ValV.Len())) {
01391     const TVal& Val1 = GetVal(ValN1);
01392     const TVal& Val2 = ValV.GetVal(ValN2);
01393     if (Val1 < Val2) {
01394       Cnt++; ValN1++;
01395     } else if (Val1 > Val2) {
01396       Cnt++; ValN2++;
01397     } else {
01398       Cnt++; ValN1++; ValN2++;
01399     }
01400   }
01401   Cnt += (Len() - ValN1) + (ValV.Len() - ValN2);
01402   return Cnt;
01403 }
01404 
01405 template <class TVal, class TSizeTy>
01406 TSizeTy TVec<TVal, TSizeTy>::Count(const TVal& Val) const {
01407   TSizeTy Count = 0;
01408   for (TSizeTy i = 0; i < Len(); i++){
01409     if (Val == ValT[i]){Count++;}}
01410   return Count;
01411 }
01412 
01413 template <class TVal, class TSizeTy>
01414 TSizeTy TVec<TVal, TSizeTy>::SearchBin(const TVal& Val) const {
01415   TSizeTy LValN=0, RValN=Len()-1;
01416   while (RValN>=LValN){
01417     TSizeTy ValN=(LValN+RValN)/2;
01418     if (Val==ValT[ValN]){return ValN;}
01419     if (Val<ValT[ValN]){RValN=ValN-1;} else {LValN=ValN+1;}
01420   }
01421   return -1;
01422 }
01423 
01424 template <class TVal, class TSizeTy>
01425 TSizeTy TVec<TVal, TSizeTy>::SearchBin(const TVal& Val, TSizeTy& InsValN) const {
01426   TSizeTy LValN=0, RValN=Len()-1;
01427   while (RValN>=LValN){
01428     TSizeTy ValN=(LValN+RValN)/2;
01429     if (Val==ValT[ValN]){InsValN=ValN; return ValN;}
01430     if (Val<ValT[ValN]){RValN=ValN-1;} else {LValN=ValN+1;}
01431   }
01432   InsValN=LValN; return -1;
01433 }
01434 
01435 template <class TVal, class TSizeTy>
01436 TSizeTy TVec<TVal, TSizeTy>::SearchForw(const TVal& Val, const TSizeTy& BValN) const {
01437   for (TSizeTy ValN=BValN; ValN<Vals; ValN++){
01438     if (Val==ValT[ValN]){return ValN;}}
01439   return -1;
01440 }
01441 
01442 template <class TVal, class TSizeTy>
01443 TSizeTy TVec<TVal, TSizeTy>::SearchBack(const TVal& Val) const {
01444   for (TSizeTy ValN=Vals-1; ValN>=0; ValN--){
01445     if (Val==ValT[ValN]){return ValN;}}
01446   return -1;
01447 }
01448 
01449 template <class TVal, class TSizeTy>
01450 TSizeTy TVec<TVal, TSizeTy>::SearchVForw(const TVec<TVal, TSizeTy>& ValV, const TSizeTy& BValN) const {
01451   TSizeTy ValVLen=ValV.Len();
01452   for (TSizeTy ValN=BValN; ValN<Vals-ValVLen+1; ValN++){
01453     bool Found=true;
01454     for (TSizeTy SubValN=0; SubValN<ValVLen; SubValN++){
01455       if (ValV[SubValN]!=GetVal(ValN+SubValN)){Found=false; break;}
01456     }
01457     if (Found){return ValN;}
01458   }
01459   return -1;
01460 }
01461 
01462 template <class TVal, class TSizeTy>
01463 TSizeTy TVec<TVal, TSizeTy>::GetMxValN() const {
01464   if (Vals==0){return -1;}
01465   TSizeTy MxValN=0;
01466   for (TSizeTy ValN=1; ValN<Vals; ValN++){
01467     if (ValT[ValN]>ValT[MxValN]){MxValN=ValN;}
01468   }
01469   return MxValN;
01470 }
01471 
01473 // Vector
01474 namespace TGLib_OLD {
01475 
01476 template <class TVal>
01477 class TVec{
01478 public:
01479   typedef TVal* TIter;
01480 protected:
01481   int MxVals; // if MxVals==-1, then ValT is not owned by us, we don't free it!
01482   int Vals;
01483   TVal* ValT;
01484   void Resize(const int& _MxVals=-1);
01485   TStr GetXOutOfBoundsErrMsg(const int& ValN) const;
01486 public:
01487   TVec(): MxVals(0), Vals(0), ValT(NULL){}
01488   TVec(const TVec& Vec);
01489   explicit TVec(const int& _Vals){
01490     IAssert(0<=_Vals); MxVals=Vals=_Vals;
01491     if (_Vals==0){ValT=NULL;} else {ValT=new TVal[_Vals];}}
01492   TVec(const int& _MxVals, const int& _Vals){
01493     IAssert((0<=_Vals)&&(_Vals<=_MxVals)); MxVals=_MxVals; Vals=_Vals;
01494     if (_MxVals==0){ValT=NULL;} else {ValT=new TVal[_MxVals];}}
01495   explicit TVec(TVal *_ValT, const int& _Vals):
01496     MxVals(-1), Vals(_Vals), ValT(_ValT){}
01497   ~TVec(){if ((ValT!=NULL) && (MxVals!=-1)){delete[] ValT;}}
01498   explicit TVec(TSIn& SIn): MxVals(0), Vals(0), ValT(NULL){Load(SIn);}
01499   void Load(TSIn& SIn);
01500   void Save(TSOut& SOut) const;
01501   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm="");
01502   void SaveXml(TSOut& SOut, const TStr& Nm) const;
01503 
01504   TVec<TVal>& operator=(const TVec<TVal>& Vec);
01505   TVec<TVal>& operator+(const TVal& Val){Add(Val); return *this;}
01506   bool operator==(const TVec<TVal>& Vec) const;
01507   bool operator<(const TVec<TVal>& Vec) const;
01508   const TVal& operator[](const int& ValN) const {
01509     AssertR((0<=ValN)&&(ValN<Vals), GetXOutOfBoundsErrMsg(ValN));
01510     return ValT[ValN];}
01511   TVal& operator[](const int& ValN){
01512     AssertR((0<=ValN)&&(ValN<Vals), GetXOutOfBoundsErrMsg(ValN));
01513     return ValT[ValN];}
01514   int GetMemUsed() const {
01515     return int(2*sizeof(int)+sizeof(TVal*)+MxVals*sizeof(TVal));}
01516 
01517   int GetPrimHashCd() const;
01518   int GetSecHashCd() const;
01519 
01520   void Gen(const int& _Vals){
01521     IAssert(0<=_Vals);
01522     if (ValT!=NULL && MxVals!=-1){delete[] ValT;} MxVals=Vals=_Vals;
01523     if (MxVals==0){ValT=NULL;} else {ValT=new TVal[MxVals];}}
01524   void Gen(const int& _MxVals, const int& _Vals){
01525     IAssert((0<=_Vals)&&(_Vals<=_MxVals));
01526     if (ValT!=NULL  && MxVals!=-1){delete[] ValT;} MxVals=_MxVals; Vals=_Vals;
01527     if (_MxVals==0){ValT=NULL;} else {ValT=new TVal[_MxVals];}}
01528   void GenExt(TVal *_ValT, const int& _Vals){
01529     if (ValT!=NULL && MxVals!=-1){delete[] ValT;}
01530     MxVals=-1; Vals=_Vals; ValT=_ValT;}
01531   bool IsExt() const {return MxVals==-1;}
01532   void Reserve(const int& _MxVals){Resize(_MxVals);}
01533   void Reserve(const int& _MxVals, const int& _Vals){
01534     IAssert((0<=_Vals)&&(_Vals<=_MxVals));
01535     Resize(_MxVals); Vals=_Vals;}
01536   void Clr(const bool& DoDel=true, const int& NoDelLim=-1);
01537   void Trunc(const int& _Vals=-1);
01538   void Pack();
01539   void MoveFrom(TVec<TVal>& Vec);
01540   void Swap(TVec<TVal>& Vec);
01541 
01542   bool Empty() const {return Vals==0;}
01543   int Len() const {return Vals;}
01544   int Reserved() const {return MxVals;}
01545   const TVal& Last() const {return GetVal(Len()-1);}
01546   TVal& Last(){return GetVal(Len()-1);}
01547   int LastValN() const {return Len()-1;}
01548   const TVal& LastLast() const {
01549     AssertR(1<Vals, GetXOutOfBoundsErrMsg(Vals-2));
01550     return ValT[Vals-2];}
01551   TVal& LastLast(){
01552     AssertR(1<Vals, GetXOutOfBoundsErrMsg(Vals-2));
01553     return ValT[Vals-2];}
01554 
01555   TIter BegI() const {return ValT;}
01556   TIter EndI() const {return ValT+Vals;}
01557   TIter GetI(const int& ValN) const {return ValT+ValN;}
01558 
01559   int Add(){
01560     Assert(MxVals!=-1); if (Vals==MxVals){Resize();} return Vals++;}
01561   int Add(const TVal& Val){
01562     Assert(MxVals!=-1); if (Vals==MxVals){Resize();} ValT[Vals]=Val; return Vals++;}
01563   int Add(const TVal& Val, const int& ResizeLen){
01564     Assert(MxVals!=-1); if (Vals==MxVals){Resize(MxVals+ResizeLen);} ValT[Vals]=Val; return Vals++;}
01565   int AddV(const TVec<TVal>& ValV);
01566   int AddSorted(const TVal& Val, const bool& Asc=true, const int& _MxVals=-1);
01567   int AddBackSorted(const TVal& Val, const bool& Asc);
01568   int AddMerged(const TVal& Val);
01569   int AddVMerged(const TVec<TVal>& ValV);
01570   int AddUnique(const TVal& Val);
01571   const TVal& GetVal(const int& ValN) const {return operator[](ValN);}
01572   TVal& GetVal(const int& ValN){return operator[](ValN);}
01573   void GetSubValV(const int& BValN, const int& EValN, TVec<TVal>& ValV) const;
01574   void Ins(const int& ValN, const TVal& Val);
01575   void Del(const int& ValN);
01576   void Del(const int& MnValN, const int& MxValN);
01577   void DelLast(){Del(Len()-1);}
01578   bool DelIfIn(const TVal& Val);
01579   void DelAll(const TVal& Val);
01580   void PutAll(const TVal& Val);
01581 
01582   void Swap(const int& ValN1, const int& ValN2){
01583     TVal Val=ValT[ValN1]; ValT[ValN1]=ValT[ValN2]; ValT[ValN2]=Val;}
01584   static void SwapI(TIter LVal, TIter RVal){
01585     TVal Val=*LVal; *LVal=*RVal; *RVal=Val;}
01586 
01587   int GetPivotValN(const int& LValN, const int& RValN) const;
01588   void BSort(const int& MnLValN, const int& MxRValN, const bool& Asc);
01589   void ISort(const int& MnLValN, const int& MxRValN, const bool& Asc);
01590   int Partition(const int& MnLValN, const int& MxRValN, const bool& Asc);
01591   void QSort(const int& MnLValN, const int& MxRValN, const bool& Asc);
01592   void Sort(const bool& Asc=true);
01593   bool IsSorted(const bool& Asc=true) const;
01594   void Shuffle(TRnd& Rnd);
01595   void Reverse();
01596   void Reverse(int First, int Last){
01597     Last--; while (First < Last){Swap(First++, Last--);}}
01598   void Merge();
01599   // permutations
01600   bool NextPerm();
01601   bool PrevPerm();
01602 
01603   // binary heap //J:
01604   void MakeHeap() { MakeHeap(TLss<TVal>()); }                     // build a heap
01605   void PushHeap(const TVal& Val) { PushHeap(Val, TLss<TVal>()); } // add element to the heap
01606   const TVal& TopHeap() const { return ValT[0]; }                 // get largest element
01607   TVal PopHeap() { return PopHeap(TLss<TVal>()); }                // remove largest element
01608   template <class TCmp> void MakeHeap(const TCmp& Cmp) { MakeHeap(0, Len(), Cmp); }
01609   template <class TCmp> void PushHeap(const TVal& Val, const TCmp& Cmp) { Add(Val); PushHeap(0, Len()-1, 0, Val, Cmp); }
01610   template <class TCmp> TVal PopHeap(const TCmp& Cmp) { IAssert(! Empty()); const TVal Top=ValT[0];
01611     ValT[0]=Last(); DelLast(); if (! Empty()) { AdjustHeap(0, 0, Len(), ValT[0], Cmp); } return Top; }
01612   // heap helper functions
01613   template <class TCmp>
01614   void PushHeap(const int& First, int HoleIdx, const int& Top, TVal Val, const TCmp& Cmp) {
01615     int Parent = (HoleIdx-1)/2;
01616     while (HoleIdx > Top && Cmp(ValT[First+Parent], Val)) {
01617       ValT[First+HoleIdx] = ValT[First+Parent];
01618       HoleIdx = Parent;  Parent = (HoleIdx-1)/2; }
01619     ValT[First+HoleIdx] = Val;
01620   }
01621   template <class TCmp>
01622   void AdjustHeap(const int& First, int HoleIdx, const int& Len, TVal Val, const TCmp& Cmp) {
01623     const int Top = HoleIdx;
01624     int Right = 2*HoleIdx+2;
01625     while (Right < Len) {
01626       if (Cmp(ValT[First+Right], ValT[First+Right-1])) { Right--; }
01627       ValT[First+HoleIdx] = ValT[First+Right];
01628       HoleIdx = Right;  Right = 2*(Right+1); }
01629     if (Right == Len) {
01630       ValT[First+HoleIdx] = ValT[First+Right-1];
01631       HoleIdx = Right-1; }
01632     PushHeap(First, HoleIdx, Top, Val, Cmp);
01633   }
01634   template <class TCmp>
01635   void MakeHeap(const int& First, const int& Len, const TCmp& Cmp) {
01636     if (Len < 2) { return; }
01637     int Parent = (Len-2)/2;
01638     while (true) {
01639       AdjustHeap(First, Parent, Len, ValT[First+Parent], Cmp);
01640       if (Parent == 0) { return; }  Parent--; }
01641   }
01642 
01643   template <class TCmp>
01644   static TIter GetPivotValNCmp(const TIter& BI, const TIter& EI, const TCmp& Cmp) {
01645     const int SubVals=int(EI-BI);
01646     const int ValN1=TInt::GetRnd(SubVals);
01647     const int ValN2=TInt::GetRnd(SubVals);
01648     const int ValN3=TInt::GetRnd(SubVals);
01649     const TVal& Val1 = *(BI+ValN1);
01650     const TVal& Val2 = *(BI+ValN2);
01651     const TVal& Val3 = *(BI+ValN3);
01652     if (Cmp(Val1, Val2)) {
01653       if (Cmp(Val2, Val3)) return BI+ValN2;
01654       else if (Cmp(Val3, Val1)) return BI+ValN1;
01655       else return BI+ValN3;
01656     } else {
01657       if (Cmp(Val1, Val3)) return BI+ValN1;
01658       else if (Cmp(Val3, Val2)) return BI+ValN2;
01659       else return BI+ValN3;
01660     }
01661   }
01662 
01663   template <class TCmp>
01664   static TIter PartitionCmp(TIter BI, TIter EI, const TVal Pivot, const TCmp& Cmp) {
01665     forever {
01666       while (Cmp(*BI, Pivot)) ++BI;
01667       --EI;
01668       while (Cmp(Pivot, *EI)) --EI;
01669       if (!(BI < EI)) return BI;
01670       SwapI(BI, EI);
01671       ++BI;
01672     }
01673   }
01674 
01675   template <class TCmp>
01676   static void BSortCmp(TIter BI, TIter EI, const TCmp& Cmp) {
01677     for (TIter i = BI; i != EI; ++i) {
01678       for (TIter j = EI-1; j != i; --j) {
01679         if (Cmp(*j, *(j-1))) SwapI(j, j-1); }
01680     }
01681   }
01682 
01683   template <class TCmp>
01684   static void ISortCmp(TIter BI, TIter EI, const TCmp& Cmp) {
01685     if (BI + 1 < EI) {
01686       for (TIter i = BI, j; i != EI; ++i) {
01687         TVal Tmp = *i; j = i;
01688         while (j > BI && Cmp(Tmp, *(j-1))) { *j = *(j-1); --j; }
01689         *j = Tmp;
01690       }
01691     }
01692   }
01693 
01694   template <class TCmp>
01695   static void QSortCmp(TIter BI, TIter EI, const TCmp& Cmp) {
01696     if (BI + 1 < EI) {
01697       if (EI - BI < 20) {
01698         ISortCmp(BI, EI, Cmp); }
01699       else {
01700         const TVal Val = *GetPivotValNCmp(BI, EI, Cmp);
01701         TIter Split = PartitionCmp(BI, EI, Val, Cmp);
01702         QSortCmp(BI, Split, Cmp);
01703         QSortCmp(Split, EI, Cmp);
01704       }
01705     }
01706   }
01707 
01708   //  void Sort(const bool& Asc = true) {
01709   //    if (Asc){QSortCmp(Beg(), End(), TLss<TVal>());}
01710   //    else {QSortCmp(Beg(), End(), TGtr<TVal>());}}
01711 
01712   template <class TCmp>
01713   void SortCmp(const TCmp& Cmp){
01714     QSortCmp(BegI(), EndI(), Cmp);}
01715 
01716   //  bool IsSorted(const bool& Asc = true) const {
01717   //    if (Asc){return IsSortedCmp(TLss<TVal>());}
01718   //    else {return IsSortedCmp(TGtr<TVal>());}}
01719 
01720   template <class TCmp>
01721   bool IsSortedCmp(const TCmp& Cmp) const {
01722     if (EndI() == BegI()) return true;
01723     for (TIter i = BegI(); i != EndI()-1; ++i) {
01724       if (Cmp(*(i+1), *i)){return false;}}
01725     return true;
01726   }
01727 
01728   void Intrs(const TVec<TVal>& ValV);
01729   void Union(const TVec<TVal>& ValV);
01730   void Diff(const TVec<TVal>& ValV); // union without intersection
01731   void Minus(const TVec<TVal>& ValV); // this without intersection
01732   void Intrs(const TVec<TVal>& ValV, TVec<TVal>& DstValV) const;
01733   void Union(const TVec<TVal>& ValV, TVec<TVal>& DstValV) const;
01734   void Diff(const TVec<TVal>& ValV, TVec<TVal>& DstValV) const;
01736   int IntrsLen(const TVec<TVal>& ValV) const;
01738   int UnionLen(const TVec<TVal>& ValV) const;
01739   void Minus(const TVec<TVal>& ValV, TVec<TVal>& DstValV) const;
01740 
01741   int Count(const TVal& Val) const;
01742   int SearchBin(const TVal& Val) const;
01743   int SearchBin(const TVal& Val, int& InsValN) const;
01744   int SearchForw(const TVal& Val, const int& BValN=0) const;
01745   int SearchBack(const TVal& Val) const;
01746   int SearchVForw(const TVec<TVal>& ValV, const int& BValN=0) const;
01747   bool IsIn(const TVal& Val) const {return SearchForw(Val)!=-1;}
01748   bool IsIn(const TVal& Val, int& ValN) const {
01749     ValN=SearchForw(Val); return ValN!=-1;}
01750   bool IsInBin(const TVal& Val) const {return SearchBin(Val)!=-1;}
01751   int GetMxValN() const;
01752   TVal& GetDat(const TVal& Val) const {
01753     int ValN=SearchForw(Val);
01754     return operator[](ValN);}
01755   TVal& GetAddDat(const TVal& Val){
01756     Assert(MxVals!=-1);
01757     int ValN=SearchForw(Val);
01758     if (ValN==-1){Add(Val); return Last();}
01759     else {return operator[](ValN);}}
01760 
01761   // short vectors
01762   static TVec<TVal> GetV(const TVal& Val1){
01763     TVec<TVal> V(1, 0); V.Add(Val1); return V;}
01764   static TVec<TVal> GetV(const TVal& Val1, const TVal& Val2){
01765     TVec<TVal> V(2, 0); V.Add(Val1); V.Add(Val2); return V;}
01766   static TVec<TVal> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3){
01767     TVec<TVal> V(3, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); return V;}
01768   static TVec<TVal> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4){
01769     TVec<TVal> V(4, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); return V;}
01770   static TVec<TVal> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4, const TVal& Val5){
01771     TVec<TVal> V(5, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); V.Add(Val5); return V;}
01772   static TVec<TVal> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4, const TVal& Val5, const TVal& Val6){
01773     TVec<TVal> V(6, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); V.Add(Val5); V.Add(Val6); return V;}
01774   static TVec<TVal> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4, const TVal& Val5, const TVal& Val6, const TVal& Val7){
01775     TVec<TVal> V(7, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); V.Add(Val5); V.Add(Val6); V.Add(Val7); return V;}
01776   static TVec<TVal> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4, const TVal& Val5, const TVal& Val6, const TVal& Val7, const TVal& Val8){
01777     TVec<TVal> V(8, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); V.Add(Val5); V.Add(Val6); V.Add(Val7); V.Add(Val8); return V;}
01778   static TVec<TVal> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4, const TVal& Val5, const TVal& Val6, const TVal& Val7, const TVal& Val8, const TVal& Val9){
01779     TVec<TVal> V(9, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); V.Add(Val5); V.Add(Val6); V.Add(Val7); V.Add(Val8); V.Add(Val9); return V;}
01780   //static TVec<TVal> GetV(const TVal* ValPt, const TVal& EndVal){
01781   //  TVec<TVal> V; while(*ValPt!=EndVal){V.Add(*ValPt); ValPt++;} return V;}
01782 };
01783 
01784 template <class TVal>
01785 void TVec<TVal>::Resize(const int& _MxVals){
01786   IAssertR(MxVals!=-1, TStr::Fmt("Can not resize buffer. %s. [Program failed to allocate more memory. Solution: Get a bigger machine and a 64-bit compiler.]", GetTypeNm(*this).CStr()).CStr());
01787   if (_MxVals==-1){
01788     if (Vals==0){MxVals=16;} else {MxVals*=2;}
01789   } else {
01790     if (_MxVals<=MxVals){return;} else {MxVals=_MxVals;}
01791   }
01792   if (ValT==NULL){
01793     try {ValT=new TVal[MxVals];}
01794     catch (std::exception Ex){
01795       FailR(TStr::Fmt("TVec::Resize 1: %s, Vals:%d, MxVals:%d, _MxVals:%d, Type:%s [Program failed to allocate more memory. Solution: Get a bigger machine and a 64-bit compiler.]",
01796        Ex.what(), Vals, MxVals, _MxVals, GetTypeNm(*this).CStr()).CStr());}
01797   } else {
01798     //if (Vals > 1000000) {
01799     //  printf("%s resize %d -> %d\n", GetTypeNm(*this).CStr(), Vals, MxVals); }
01800     TVal* NewValT = NULL;
01801     try {
01802       NewValT=new TVal[MxVals];}
01803     catch (std::exception Ex){
01804       FailR(TStr::Fmt("TVec::Resize 2: %s, Vals:%d, MxVals:%d, _MxVals:%d, Type:%s [Program failed to allocate more memory. Solution: Get a bigger machine and a 64-bit compiler.]",
01805        Ex.what(), Vals, MxVals, _MxVals, GetTypeNm(*this).CStr()).CStr());}
01806     Assert(NewValT!=NULL);
01807     for (int ValN=0; ValN<Vals; ValN++){NewValT[ValN]=ValT[ValN];}
01808     delete[] ValT; ValT=NewValT;
01809   }
01810 }
01811 
01812 template <class TVal>
01813 TStr TVec<TVal>::GetXOutOfBoundsErrMsg(const int& ValN) const {
01814   return TStr()+
01815     "Index:"+TInt::GetStr(ValN)+
01816     " Vals:"+TInt::GetStr(Vals)+
01817     " MxVals:"+TInt::GetStr(MxVals)+
01818     " Type:"+GetTypeNm(*this);
01819 }
01820 
01821 template <class TVal>
01822 TVec<TVal>::TVec(const TVec<TVal>& Vec){
01823   MxVals=Vec.MxVals; Vals=Vec.Vals;
01824   if (MxVals==0){ValT=NULL;} else {ValT=new TVal[MxVals];}
01825   for (int ValN=0; ValN<Vec.Vals; ValN++){ValT[ValN]=Vec.ValT[ValN];}
01826 }
01827 
01828 template <class TVal>
01829 void TVec<TVal>::Load(TSIn& SIn){
01830   if ((ValT!=NULL)&&(MxVals!=-1)){delete[] ValT;}
01831   SIn.Load(MxVals); SIn.Load(Vals); MxVals=Vals;
01832   if (MxVals==0){ValT=NULL;} else {ValT=new TVal[MxVals];}
01833   for (int ValN=0; ValN<Vals; ValN++){
01834     ValT[ValN]=TVal(SIn);}
01835 }
01836 
01837 template <class TVal>
01838 void TVec<TVal>::Save(TSOut& SOut) const {
01839   if (MxVals!=-1){SOut.Save(MxVals);} else {SOut.Save(Vals);}
01840   SOut.Save(Vals);
01841   for (int ValN=0; ValN<Vals; ValN++){ValT[ValN].Save(SOut);}
01842 }
01843 
01844 template <class TVal>
01845 TVec<TVal>& TVec<TVal>::operator=(const TVec<TVal>& Vec){
01846   if (this!=&Vec){
01847     if ((ValT!=NULL)&&(MxVals!=-1)){delete[] ValT;}
01848     MxVals=Vals=Vec.Vals;
01849     if (MxVals==0){ValT=NULL;} else {ValT=new TVal[MxVals];}
01850     for (int ValN=0; ValN<Vec.Vals; ValN++){ValT[ValN]=Vec.ValT[ValN];}
01851   }
01852   return *this;
01853 }
01854 
01855 template <class TVal>
01856 bool TVec<TVal>::operator==(const TVec<TVal>& Vec) const {
01857   if (this==&Vec){return true;}
01858   if (Len()!=Vec.Len()){return false;}
01859   for (int ValN=0; ValN<Vals; ValN++){
01860     if (ValT[ValN]!=Vec.ValT[ValN]){return false;}}
01861   return true;
01862 }
01863 
01864 template <class TVal>
01865 bool TVec<TVal>::operator<(const TVec<TVal>& Vec) const {
01866   if (this==&Vec){return false;}
01867   if (Len()==Vec.Len()){
01868     for (int ValN=0; ValN<Vals; ValN++){
01869       if (ValT[ValN]<Vec.ValT[ValN]){return true;}
01870       else if (ValT[ValN]>Vec.ValT[ValN]){return false;}
01871       else {}
01872     }
01873     return false;
01874   } else {
01875     return Len()<Vec.Len();
01876   }
01877 }
01878 
01879 template <class TVal>
01880 int TVec<TVal>::GetPrimHashCd() const {
01881   int HashCd=0;
01882   for (int ValN=0; ValN<Vals; ValN++){
01883     HashCd+=ValT[ValN].GetPrimHashCd();}
01884   return abs(HashCd);
01885 }
01886 
01887 template <class TVal>
01888 int TVec<TVal>::GetSecHashCd() const {
01889   int HashCd=0;
01890   for (int ValN=0; ValN<Vals; ValN++){
01891     HashCd+=ValT[ValN].GetSecHashCd();}
01892   return abs(HashCd);
01893 }
01894 
01895 template <class TVal>
01896 void TVec<TVal>::Clr(const bool& DoDel, const int& NoDelLim){
01897   if ((DoDel)||((!DoDel)&&(NoDelLim!=-1)&&(MxVals>NoDelLim))){
01898     if ((ValT!=NULL)&&(MxVals!=-1)){delete[] ValT;}
01899     MxVals=Vals=0; ValT=NULL;
01900   } else {
01901     IAssert(MxVals!=-1); Vals=0;
01902   }
01903 }
01904 
01905 template <class TVal>
01906 void TVec<TVal>::Trunc(const int& _Vals){
01907   IAssert(MxVals!=-1);
01908   IAssert((_Vals==-1)||(_Vals>=0));
01909   if ((_Vals!=-1)&&(_Vals>=Vals)){
01910     return;
01911   } else
01912   if (((_Vals==-1)&&(Vals==0))||(_Vals==0)){
01913     if (ValT!=NULL){delete[] ValT;}
01914     MxVals=Vals=0; ValT=NULL;
01915   } else {
01916     if (_Vals==-1){
01917       if (MxVals==Vals){return;} else {MxVals=Vals;}
01918     } else {
01919       MxVals=Vals=_Vals;
01920     }
01921     TVal* NewValT=new TVal[MxVals];
01922     IAssert(NewValT!=NULL);
01923     for (int ValN=0; ValN<Vals; ValN++){NewValT[ValN]=ValT[ValN];}
01924     delete[] ValT; ValT=NewValT;
01925   }
01926 }
01927 
01928 template <class TVal>
01929 void TVec<TVal>::Pack(){
01930   IAssert(MxVals!=-1);
01931   if (Vals==0){
01932     if (ValT!=NULL){delete[] ValT;} ValT=NULL;
01933   } else
01934   if (Vals<MxVals){
01935     MxVals=Vals;
01936     TVal* NewValT=new TVal[MxVals];
01937     IAssert(NewValT!=NULL);
01938     for (int ValN=0; ValN<Vals; ValN++){NewValT[ValN]=ValT[ValN];}
01939     delete[] ValT; ValT=NewValT;
01940   }
01941 }
01942 
01943 template <class TVal>
01944 void TVec<TVal>::MoveFrom(TVec<TVal>& Vec){
01945   if (this!=&Vec){
01946     if (ValT!=NULL && MxVals!=-1){delete[] ValT;}
01947     MxVals=Vec.MxVals; Vals=Vec.Vals; ValT=Vec.ValT;
01948     Vec.MxVals=0; Vec.Vals=0; Vec.ValT=NULL;
01949   }
01950 }
01951 
01952 template <class TVal>
01953 void TVec<TVal>::Swap(TVec<TVal>& Vec){
01954   if (this!=&Vec){
01955     ::Swap(MxVals, Vec.MxVals);
01956     ::Swap(Vals, Vec.Vals);
01957     ::Swap(ValT, Vec.ValT);
01958   }
01959 }
01960 
01961 template <class TVal>
01962 int TVec<TVal>::AddV(const TVec<TVal>& ValV){
01963   Assert(MxVals!=-1);
01964   for (int ValN=0; ValN<ValV.Vals; ValN++){Add(ValV[ValN]);}
01965   return Len();
01966 }
01967 
01968 template <class TVal>
01969 int TVec<TVal>::AddSorted(const TVal& Val, const bool& Asc, const int& _MxVals){
01970   Assert(MxVals!=-1);
01971   int ValN=Add(Val);
01972   if (Asc){
01973     while ((ValN>0)&&(ValT[ValN]<ValT[ValN-1])){
01974       Swap(ValN, ValN-1); ValN--;}
01975   } else {
01976     while ((ValN>0)&&(ValT[ValN]>ValT[ValN-1])){
01977       Swap(ValN, ValN-1); ValN--;}
01978   }
01979   if ((_MxVals!=-1)&&(Len()>_MxVals)){Del(_MxVals, Len()-1);}
01980   return ValN;
01981 }
01982 
01983 template <class TVal>
01984 int TVec<TVal>::AddBackSorted(const TVal& Val, const bool& Asc){
01985   Assert(MxVals!=-1);
01986   Add();
01987   int ValN=Vals-2;
01988   while ((ValN>=0)&&((Asc&&(Val<ValT[ValN]))||(!Asc&&(Val>ValT[ValN])))){
01989     ValT[ValN+1]=ValT[ValN]; ValN--;}
01990   ValT[ValN+1]=Val;
01991   return ValN+1;
01992 }
01993 
01994 template <class TVal>
01995 int TVec<TVal>::AddMerged(const TVal& Val){
01996   Assert(MxVals!=-1);
01997   int ValN=SearchBin(Val);
01998   if (ValN==-1){return AddSorted(Val);}
01999   else {GetVal(ValN)=Val; return -1;}
02000 }
02001 
02002 template <class TVal>
02003 int TVec<TVal>::AddVMerged(const TVec<TVal>& ValV){
02004   Assert(MxVals!=-1);
02005   for (int ValN=0; ValN<ValV.Vals; ValN++){AddMerged(ValV[ValN]);}
02006   return Len();
02007 }
02008 
02009 template <class TVal>
02010 int TVec<TVal>::AddUnique(const TVal& Val){
02011   Assert(MxVals!=-1);
02012   int ValN=SearchForw(Val);
02013   if (ValN==-1){return Add(Val);}
02014   else {GetVal(ValN)=Val; return -1;}
02015 }
02016 
02017 template <class TVal>
02018 void TVec<TVal>::GetSubValV(
02019  const int& _BValN, const int& _EValN, TVec<TVal>& SubValV) const {
02020   int BValN=TInt::GetInRng(_BValN, 0, Len()-1);
02021   int EValN=TInt::GetInRng(_EValN, 0, Len()-1);
02022   int SubVals=TInt::GetMx(0, EValN-BValN+1);
02023   SubValV.Gen(SubVals, 0);
02024   for (int ValN=BValN; ValN<=EValN; ValN++){
02025     SubValV.Add(GetVal(ValN));}
02026 }
02027 
02028 template <class TVal>
02029 void TVec<TVal>::Ins(const int& ValN, const TVal& Val){
02030   Assert(MxVals!=-1);
02031   Add(); Assert((0<=ValN)&&(ValN<Vals));
02032   for (int MValN=Vals-2; MValN>=ValN; MValN--){ValT[MValN+1]=ValT[MValN];}
02033   ValT[ValN]=Val;
02034 }
02035 
02036 template <class TVal>
02037 void TVec<TVal>::Del(const int& ValN){
02038   Assert(MxVals!=-1);
02039   Assert((0<=ValN)&&(ValN<Vals));
02040   for (int MValN=ValN+1; MValN<Vals; MValN++){
02041     ValT[MValN-1]=ValT[MValN];}
02042   ValT[--Vals]=TVal();
02043 }
02044 
02045 template <class TVal>
02046 void TVec<TVal>::Del(const int& MnValN, const int& MxValN){
02047   Assert(MxVals!=-1);
02048   Assert((0<=MnValN)&&(MnValN<Vals)&&(0<=MxValN)&&(MxValN<Vals));
02049   Assert(MnValN<=MxValN);
02050   for (int ValN=MxValN+1; ValN<Vals; ValN++){
02051     ValT[MnValN+ValN-MxValN-1]=ValT[ValN];}
02052   for (int ValN=Vals-MxValN+MnValN-1; ValN<Vals; ValN++){
02053     ValT[ValN]=TVal();}
02054   Vals-=MxValN-MnValN+1;
02055 }
02056 
02057 template <class TVal>
02058 bool TVec<TVal>::DelIfIn(const TVal& Val){
02059   Assert(MxVals!=-1);
02060   int ValN=SearchForw(Val);
02061   if (ValN!=-1){Del(ValN); return true;}
02062   else {return false;}
02063 }
02064 
02065 template <class TVal>
02066 void TVec<TVal>::DelAll(const TVal& Val){
02067   Assert(MxVals!=-1);
02068   int ValN;
02069   while ((ValN=SearchForw(Val))!=-1){
02070     Del(ValN);}
02071 }
02072 
02073 template <class TVal>
02074 void TVec<TVal>::PutAll(const TVal& Val){
02075   for (int ValN=0; ValN<Vals; ValN++){ValT[ValN]=Val;}
02076 }
02077 
02078 template <class TVal>
02079 void TVec<TVal>::BSort(
02080  const int& MnLValN, const int& MxRValN, const bool& Asc){
02081   for (int ValN1=MnLValN; ValN1<=MxRValN; ValN1++){
02082     for (int ValN2=MxRValN; ValN2>ValN1; ValN2--){
02083       if (Asc){
02084         if (ValT[ValN2]<ValT[ValN2-1]){Swap(ValN2, ValN2-1);}
02085       } else {
02086         if (ValT[ValN2]>ValT[ValN2-1]){Swap(ValN2, ValN2-1);}
02087       }
02088     }
02089   }
02090 }
02091 
02092 template <class TVal>
02093 void TVec<TVal>::ISort(
02094  const int& MnLValN, const int& MxRValN, const bool& Asc){
02095   if (MnLValN<MxRValN){
02096     for (int ValN1=MnLValN+1; ValN1<=MxRValN; ValN1++){
02097       TVal Val=ValT[ValN1]; int ValN2=ValN1;
02098       if (Asc){
02099         while ((ValN2>MnLValN)&&(ValT[ValN2-1]>Val)){
02100           ValT[ValN2]=ValT[ValN2-1]; ValN2--;}
02101       } else {
02102         while ((ValN2>MnLValN)&&(ValT[ValN2-1]<Val)){
02103           ValT[ValN2]=ValT[ValN2-1]; ValN2--;}
02104       }
02105       ValT[ValN2]=Val;
02106     }
02107   }
02108 }
02109 
02110 template <class TVal>
02111 int TVec<TVal>::GetPivotValN(const int& LValN, const int& RValN) const {
02112   int SubVals=RValN-LValN+1;
02113   int ValN1=LValN+TInt::GetRnd(SubVals);
02114   int ValN2=LValN+TInt::GetRnd(SubVals);
02115   int ValN3=LValN+TInt::GetRnd(SubVals);
02116   const TVal& Val1=ValT[ValN1];
02117   const TVal& Val2=ValT[ValN2];
02118   const TVal& Val3=ValT[ValN3];
02119   if (Val1<Val2){
02120     if (Val2<Val3){return ValN2;}
02121     else if (Val3<Val1){return ValN1;}
02122     else {return ValN3;}
02123   } else {
02124     if (Val1<Val3){return ValN1;}
02125     else if (Val3<Val2){return ValN2;}
02126     else {return ValN3;}
02127   }
02128 }
02129 
02130 template <class TVal>
02131 int TVec<TVal>::Partition(
02132  const int& MnLValN, const int& MxRValN, const bool& Asc){
02133   int PivotValN=GetPivotValN(MnLValN, MxRValN);
02134   Swap(PivotValN, MnLValN);
02135   TVal PivotVal=ValT[MnLValN];
02136   int LValN=MnLValN-1; int RValN=MxRValN+1;
02137   forever {
02138     if (Asc){
02139       do {RValN--;} while (ValT[RValN]>PivotVal);
02140       do {LValN++;} while (ValT[LValN]<PivotVal);
02141     } else {
02142       do {RValN--;} while (ValT[RValN]<PivotVal);
02143       do {LValN++;} while (ValT[LValN]>PivotVal);
02144     }
02145     if (LValN<RValN){Swap(LValN, RValN);}
02146     else {return RValN;}
02147   };
02148 }
02149 
02150 template <class TVal>
02151 void TVec<TVal>::QSort(
02152  const int& MnLValN, const int& MxRValN, const bool& Asc){
02153   if (MnLValN<MxRValN){
02154     if (MxRValN-MnLValN<20){
02155       ISort(MnLValN, MxRValN, Asc);
02156     } else {
02157       int SplitValN=Partition(MnLValN, MxRValN, Asc);
02158       QSort(MnLValN, SplitValN, Asc);
02159       QSort(SplitValN+1, MxRValN, Asc);
02160     }
02161   }
02162 }
02163 
02164 template <class TVal>
02165 void TVec<TVal>::Sort(const bool& Asc){
02166   QSort(0, Len()-1, Asc);
02167 }
02168 
02169 template <class TVal>
02170 bool TVec<TVal>::IsSorted(const bool& Asc) const {
02171   if (Asc){
02172     for (int ValN=0; ValN<Vals-1; ValN++){
02173       if (ValT[ValN]>ValT[ValN+1]){return false;}}
02174   } else {
02175     for (int ValN=0; ValN<Vals-1; ValN++){
02176       if (ValT[ValN]<ValT[ValN+1]){return false;}}
02177   }
02178   return true;
02179 }
02180 
02181 template <class TVal>
02182 void TVec<TVal>::Shuffle(TRnd& Rnd){
02183   for (int ValN=0; ValN<Vals-1; ValN++){
02184     Swap(ValN, ValN+Rnd.GetUniDevInt(Vals-ValN));}
02185 }
02186 
02187 template <class TVal>
02188 void TVec<TVal>::Reverse(){
02189   for (int ValN=0; ValN<Vals/2; ValN++){
02190     Swap(ValN, Vals-ValN-1);}
02191 }
02192 
02193 template <class TVal>
02194 void TVec<TVal>::Merge(){
02195   Assert(MxVals!=-1);
02196   TVec<TVal> SortedVec(*this); SortedVec.Sort();
02197   Clr();
02198   for (int ValN=0; ValN<SortedVec.Len(); ValN++){
02199     if ((ValN==0)||(SortedVec[ValN-1]!=SortedVec[ValN])){
02200       Add(SortedVec[ValN]);}
02201   }
02202 }
02203 
02204 template <class TVal>
02205 bool TVec<TVal>::NextPerm() {
02206   // start with a sorted sequence to obtain all permutations
02207   int First = 0, Last = Len(), Next = Len()-1;
02208   if (Last < 2) return false;
02209   for(; ; ) {
02210     // find rightmost element smaller than successor
02211     int Next1 = Next;
02212     if (GetVal(--Next) < GetVal(Next1)) { // swap with rightmost element that's smaller, flip suffix
02213       int Mid = Last;
02214       for (; GetVal(Next) >= GetVal(--Mid); ) { }
02215       Swap(Next, Mid);
02216       Reverse(Next1, Last);
02217       return true;
02218     }
02219     if (Next == First) { // pure descending, flip all
02220       Reverse();
02221       return false;
02222     }
02223   }
02224 }
02225 
02226 template <class TVal>
02227 bool TVec<TVal>::PrevPerm() {
02228   int First = 0, Last = Len(), Next = Len()-1;
02229   if (Last < 2) return false;
02230   for(; ; ) {
02231     // find rightmost element not smaller than successor
02232     int Next1 = Next;
02233     if (GetVal(--Next) >= GetVal(Next1)) { // swap with rightmost element that's not smaller, flip suffix
02234       int Mid = Last;
02235       for (; GetVal(Next) < GetVal(--Mid); ) { }
02236       Swap(Next, Mid);
02237       Reverse(Next1, Last);
02238       return true;
02239     }
02240     if (Next == First) { // pure descending, flip all
02241       Reverse();
02242       return false;
02243     }
02244   }
02245 }
02246 
02247 template <class TVal>
02248 void TVec<TVal>::Intrs(const TVec<TVal>& ValV){
02249   TVec<TVal> IntrsVec;
02250   Intrs(ValV, IntrsVec);
02251   MoveFrom(IntrsVec);
02252 }
02253 
02254 template <class TVal>
02255 void TVec<TVal>::Union(const TVec<TVal>& ValV){
02256   TVec<TVal> UnionVec;
02257   Union(ValV, UnionVec);
02258   MoveFrom(UnionVec);
02259 }
02260 
02261 template <class TVal>
02262 void TVec<TVal>::Diff(const TVec<TVal>& ValV){
02263   TVec<TVal> DiffVec;
02264   Diff(ValV, DiffVec);
02265   MoveFrom(DiffVec);
02266 }
02267 
02268 template <class TVal>
02269 void TVec<TVal>::Minus(const TVec<TVal>& ValV){
02270   TVec<TVal> MinusVec;
02271   Minus(ValV, MinusVec);
02272   MoveFrom(MinusVec);
02273 }
02274 
02275 template <class TVal>
02276 void TVec<TVal>::Intrs(const TVec<TVal>& ValV, TVec<TVal>& DstValV) const {
02277   DstValV.Clr();
02278   int ValN1=0; int ValN2=0;
02279   while ((ValN1<Len())&&(ValN2<ValV.Len())){
02280     const TVal& Val1=GetVal(ValN1);
02281     while ((ValN2<ValV.Len())&&(Val1>ValV.GetVal(ValN2))){
02282       ValN2++;}
02283     if ((ValN2<ValV.Len())&&(Val1==ValV.GetVal(ValN2))){
02284       DstValV.Add(Val1); ValN2++;}
02285     ValN1++;
02286   }
02287 }
02288 
02289 template <class TVal>
02290 void TVec<TVal>::Union(const TVec<TVal>& ValV, TVec<TVal>& DstValV) const {
02291   DstValV.Gen(TInt::GetMx(Len(), ValV.Len()), 0);
02292   int ValN1=0; int ValN2=0;
02293   while ((ValN1<Len())&&(ValN2<ValV.Len())){
02294     const TVal& Val1=GetVal(ValN1);
02295     const TVal& Val2=ValV.GetVal(ValN2);
02296     if (Val1<Val2){DstValV.Add(Val1); ValN1++;}
02297     else if (Val1>Val2){DstValV.Add(Val2); ValN2++;}
02298     else {DstValV.Add(Val1); ValN1++; ValN2++;}
02299   }
02300   for (int RestValN1=ValN1; RestValN1<Len(); RestValN1++){
02301     DstValV.Add(GetVal(RestValN1));}
02302   for (int RestValN2=ValN2; RestValN2<ValV.Len(); RestValN2++){
02303     DstValV.Add(ValV.GetVal(RestValN2));}
02304 }
02305 
02306 /*
02307 template <class TVal>
02308 void TVec<TVal>::Union(const TVec<TVal>& ValV, TVec<TVal>& DstValV){
02309   DstValV.Clr();
02310   int ValN1=0; int ValN2=0;
02311   while ((ValN1<Len())&&(ValN2<ValV.Len())){
02312     const TVal& Val1=GetVal(ValN1);
02313     const TVal& Val2=ValV.GetVal(ValN2);
02314     if (DstValV.Len()==0){
02315       if (Val1<Val2){DstValV.Add(Val1);} else {DstValV.Add(Val2);}
02316     } else {
02317       if (Val1<Val2){
02318         if (DstValV.Last()<Val1){DstValV.Add(Val1);} ValN1++;
02319       } else {
02320         if (DstValV.Last()<Val2){DstValV.Add(Val2);} ValN2++;
02321       }
02322     }
02323   }
02324   for (int RestValN1=ValN1; RestValN1<Len(); RestValN1++){
02325     const TVal& Val1=GetVal(RestValN1);
02326     if (DstValV.Len()==0){DstValV.Add(Val1);}
02327     else {if (DstValV.Last()<Val1){DstValV.Add(Val1);}}
02328   }
02329   for (int RestValN2=ValN2; RestValN2<ValV.Len(); RestValN2++){
02330     const TVal& Val2=ValV.GetVal(RestValN2);
02331     if (DstValV.Len()==0){DstValV.Add(Val2);}
02332     else {if (DstValV.Last()<Val2){DstValV.Add(Val2);}}
02333   }
02334 }
02335 */
02336 
02337 template <class TVal>
02338 void TVec<TVal>::Diff(const TVec<TVal>& ValV, TVec<TVal>& DstValV) const {
02339   DstValV.Clr();
02340   int ValN1=0; int ValN2=0;
02341   while (ValN1<Len() && ValN2<ValV.Len()) {
02342     const TVal& Val1 = GetVal(ValN1);
02343     while (ValN2<ValV.Len() && Val1>ValV.GetVal(ValN2)) ValN2++;
02344     if (ValN2<ValV.Len()) {
02345       if (Val1!=ValV.GetVal(ValN2)) { DstValV.Add(Val1); }
02346       ValN1++;
02347     }
02348   }
02349   for (int RestValN1=ValN1; RestValN1<Len(); RestValN1++){
02350     DstValV.Add(GetVal(RestValN1));}
02351 }
02352 
02353 template <class TVal>
02354 int TVec<TVal>::IntrsLen(const TVec<TVal>& ValV) const {
02355   int Cnt=0, ValN1=0; int ValN2=0;
02356   while ((ValN1<Len())&&(ValN2<ValV.Len())){
02357     const TVal& Val1=GetVal(ValN1);
02358     while ((ValN2<ValV.Len())&&(Val1>ValV.GetVal(ValN2))){
02359       ValN2++;}
02360     if ((ValN2<ValV.Len())&&(Val1==ValV.GetVal(ValN2))){
02361       ValN2++; Cnt++;}
02362     ValN1++;
02363   }
02364   return Cnt;
02365 }
02366   
02367 template <class TVal>
02368 int TVec<TVal>::UnionLen(const TVec<TVal>& ValV) const {
02369   int Cnt = 0, ValN1 = 0, ValN2 = 0;
02370   while ((ValN1 < Len()) && (ValN2 < ValV.Len())) {
02371     const TVal& Val1 = GetVal(ValN1);
02372     const TVal& Val2 = ValV.GetVal(ValN2);
02373     if (Val1 < Val2) {
02374       Cnt++; ValN1++;
02375     } else if (Val1 > Val2) {
02376       Cnt++; ValN2++;
02377     } else {
02378       Cnt++; ValN1++; ValN2++;
02379     }
02380   }
02381   Cnt += (Len() - ValN1) + (ValV.Len() - ValN2);
02382   return Cnt;
02383 }
02384 
02385 template <class TVal>
02386 void TVec<TVal>::Minus(const TVec<TVal>& ValV, TVec<TVal>& DstValV) const {
02387   Diff(ValV, DstValV);
02388 }
02389 
02390 template <class TVal>
02391 int TVec<TVal>::Count(const TVal& Val) const {
02392   int Count = 0;
02393   for (int i = 0; i < Len(); i++){
02394     if (Val == ValT[i]){Count++;}}
02395   return Count;
02396 }
02397 
02398 template <class TVal>
02399 int TVec<TVal>::SearchBin(const TVal& Val) const {
02400   int LValN=0; int RValN=Len()-1;
02401   while (RValN>=LValN){
02402     int ValN=(LValN+RValN)/2;
02403     if (Val==ValT[ValN]){return ValN;}
02404     if (Val<ValT[ValN]){RValN=ValN-1;} else {LValN=ValN+1;}
02405   }
02406   return -1;
02407 }
02408 
02409 template <class TVal>
02410 int TVec<TVal>::SearchBin(const TVal& Val, int& InsValN) const {
02411   int LValN=0; int RValN=Len()-1;
02412   while (RValN>=LValN){
02413     int ValN=(LValN+RValN)/2;
02414     if (Val==ValT[ValN]){InsValN=ValN; return ValN;}
02415     if (Val<ValT[ValN]){RValN=ValN-1;} else {LValN=ValN+1;}
02416   }
02417   InsValN=LValN; return -1;
02418 }
02419 
02420 template <class TVal>
02421 int TVec<TVal>::SearchForw(const TVal& Val, const int& BValN) const {
02422   for (int ValN=BValN; ValN<Vals; ValN++){
02423     if (Val==ValT[ValN]){return ValN;}}
02424   return -1;
02425 }
02426 
02427 template <class TVal>
02428 int TVec<TVal>::SearchBack(const TVal& Val) const {
02429   for (int ValN=Vals-1; ValN>=0; ValN--){
02430     if (Val==ValT[ValN]){return ValN;}}
02431   return -1;
02432 }
02433 
02434 template <class TVal>
02435 int TVec<TVal>::SearchVForw(const TVec<TVal>& ValV, const int& BValN) const {
02436   int ValVLen=ValV.Len();
02437   for (int ValN=BValN; ValN<Vals-ValVLen+1; ValN++){
02438     bool Found=true;
02439     for (int SubValN=0; SubValN<ValVLen; SubValN++){
02440       if (ValV[SubValN]!=GetVal(ValN+SubValN)){Found=false; break;}
02441     }
02442     if (Found){return ValN;}
02443   }
02444   return -1;
02445 }
02446 
02447 template <class TVal>
02448 int TVec<TVal>::GetMxValN() const {
02449   if (Vals==0){return -1;}
02450   int MxValN=0;
02451   for (int ValN=1; ValN<Vals; ValN++){
02452     if (ValT[ValN]>ValT[MxValN]){MxValN=ValN;}
02453   }
02454   return MxValN;
02455 }
02456 
02457 }; // namespace TGLib_OLD
02458 
02460 // Common-Vector-Types
02461 typedef TVec<TBool> TBoolV;
02462 typedef TVec<TCh> TChV;
02463 typedef TVec<TUCh> TUChV;
02464 typedef TVec<TUInt> TUIntV;
02465 typedef TVec<TInt> TIntV;
02466 typedef TVec<TUInt64> TUInt64V;
02467 typedef TVec<TFlt> TFltV;
02468 typedef TVec<TSFlt> TSFltV;
02469 typedef TVec<TAscFlt> TAscFltV;
02470 typedef TVec<TStr> TStrV;
02471 typedef TVec<TChA> TChAV;
02472 typedef TVec<TIntPr> TIntPrV;
02473 typedef TVec<TIntTr> TIntTrV;
02474 typedef TVec<TIntQu> TIntQuV;
02475 typedef TVec<TFltPr> TFltPrV;
02476 typedef TVec<TFltTr> TFltTrV;
02477 typedef TVec<TIntKd> TIntKdV;
02478 typedef TVec<TUChIntPr> TUChIntPrV;
02479 typedef TVec<TUChUInt64Pr> TUChUInt64PrV;
02480 typedef TVec<TIntUInt64Pr> TIntUInt64PrV;
02481 typedef TVec<TIntUInt64Kd> TIntUInt64KdV;
02482 typedef TVec<TIntFltPr> TIntFltPrV;
02483 typedef TVec<TIntFltPrKd> TIntFltPrKdV;
02484 typedef TVec<TFltIntPr> TFltIntPrV;
02485 typedef TVec<TFltUInt64Pr> TFltUInt64PrV;
02486 typedef TVec<TFltStrPr> TFltStrPrV;
02487 typedef TVec<TAscFltStrPr> TAscFltStrPrV;
02488 typedef TVec<TIntStrPr> TIntStrPrV;
02489 typedef TVec<TIntIntStrTr> TIntIntStrTrV;
02490 typedef TVec<TIntIntFltTr> TIntIntFltTrV;
02491 typedef TVec<TIntFltIntTr> TIntFltIntTrV;
02492 typedef TVec<TIntStrIntTr> TIntStrIntTrV;
02493 typedef TVec<TIntKd> TIntKdV;
02494 typedef TVec<TUIntIntKd> TUIntIntKdV;
02495 typedef TVec<TIntFltKd> TIntFltKdV;
02496 typedef TVec<TIntPrFltKd> TIntPrFltKdV;
02497 typedef TVec<TIntStrKd> TIntStrKdV;
02498 typedef TVec<TIntStrPrPr> TIntStrPrPrV;
02499 typedef TVec<TIntStrVPr> TIntStrVPrV;
02500 typedef TVec<TIntIntVIntTr> TIntIntVIntTrV;
02501 typedef TVec<TIntIntIntVTr> TIntIntIntVTrV;
02502 typedef TVec<TUInt64IntPr> TUInt64IntPrV;
02503 typedef TVec<TUInt64FltPr> TUInt64FltPrV;
02504 typedef TVec<TUInt64StrPr> TUInt64StrPrV;
02505 typedef TVec<TUInt64IntKd> TUInt64IntKdV;
02506 typedef TVec<TUInt64FltKd> TUInt64FltKdV;
02507 typedef TVec<TUInt64StrKd> TUInt64StrKdV;
02508 typedef TVec<TFltBoolKd> TFltBoolKdV;
02509 typedef TVec<TFltIntKd> TFltIntKdV;
02510 typedef TVec<TFltUInt64Kd> TFltUInt64KdV;
02511 typedef TVec<TFltIntPrKd> TFltIntPrKdV;
02512 typedef TVec<TFltKd> TFltKdV;
02513 typedef TVec<TFltStrKd> TFltStrKdV;
02514 typedef TVec<TFltStrPrPr> TFltStrPrPrV;
02515 typedef TVec<TFltIntIntTr> TFltIntIntTrV;
02516 typedef TVec<TFltFltStrTr> TFltFltStrTrV;
02517 typedef TVec<TAscFltIntPr> TAscFltIntPrV;
02518 typedef TVec<TAscFltIntKd> TAscFltIntKdV;
02519 typedef TVec<TStrPr> TStrPrV;
02520 typedef TVec<TStrIntPr> TStrIntPrV;
02521 typedef TVec<TStrFltPr> TStrFltPrV;
02522 typedef TVec<TStrIntKd> TStrIntKdV;
02523 typedef TVec<TStrFltKd> TStrFltKdV;
02524 typedef TVec<TStrAscFltKd> TStrAscFltKdV;
02525 typedef TVec<TStrTr> TStrTrV;
02526 typedef TVec<TStrQu> TStrQuV;
02527 typedef TVec<TStrFltFltTr> TStrFltFltTrV;
02528 typedef TVec<TStrStrIntTr> TStrStrIntTrV;
02529 typedef TVec<TStrKd> TStrKdV;
02530 typedef TVec<TStrStrVPr> TStrStrVPrV;
02531 typedef TVec<TStrVIntPr> TStrVIntPrV;
02532 typedef TVec<TFltIntIntIntQu> TFltIntIntIntQuV;
02533 typedef TVec<TIntStrIntIntQu> TIntStrIntIntQuV;
02534 typedef TVec<TIntIntPrPr> TIntIntPrPrV;
02535 
02536 //#//////////////////////////////////////////////
02538 
02541 template <class TVal, class TSizeTy=int>
02542 class TVecPool {
02543 public:
02544   typedef TPt<TVecPool<TVal, TSizeTy> > PVecPool;
02545   typedef TVec<TVal, TSizeTy> TValV;
02546 private:
02547   TCRef CRef;
02548   TBool FastCopy;
02549   TSize GrowBy, MxVals, Vals;
02550   TVal EmptyVal;                // Empty value/vector
02551   TVal *ValBf;                  // Buffer for storing all the values
02552   TVec<uint64, int> IdToOffV;   // Id to one past last (Vector starts at [id-1]). Vector length is IdToOff[id]-IdToOff[id-1]
02553 private:
02554   void Resize(const TSize& _MxVals);
02555 public:
02557 
02562   TVecPool(const TSize& ExpectVals=0, const TSize& _GrowBy=1000000, const bool& _FastCopy=false, const TVal& _EmptyVal=TVal());
02563   TVecPool(const TVecPool<TVal, TSizeTy>& Pool);
02564   TVecPool(TSIn& SIn);
02565   ~TVecPool() { if (ValBf != NULL) { delete [] ValBf; } ValBf=NULL; }
02566   static PVecPool New(const TSize& ExpectVals=0, const TSize& GrowBy=1000000, const bool& FastCopy=false) {
02567     return new TVecPool(ExpectVals, GrowBy, FastCopy); }
02568   static PVecPool Load(TSIn& SIn) { return new TVecPool(SIn); }
02569   static PVecPool Load(const TStr& FNm) { TFIn FIn(FNm); return Load(FIn); }
02570   void Save(TSOut& SOut) const;
02571   TVecPool& operator = (const TVecPool& Pool);
02572   
02574   int GetVecs() const { return IdToOffV.Len(); }
02576   TSize GetVals() const { return Vals; }
02578   bool IsVId(const int& VId) const { return (0 <= VId) && (VId < IdToOffV.Len()); }
02580   uint64 Reserved() const { return MxVals; }
02582   void Reserve(const TSize& MxVals) { Resize(MxVals); }
02584   const TVal& GetEmptyVal() const { return EmptyVal; }
02586   void SetEmptyVal(const TVal& _EmptyVal) { EmptyVal = _EmptyVal; }
02588   uint64 GetMemUsed() const {
02589     return sizeof(TCRef)+sizeof(TBool)+3*sizeof(TSize)+sizeof(TVal*)+MxVals*sizeof(TVal);}
02590   
02592   int AddV(const TValV& ValV);
02594 
02596   int AddEmptyV(const int& ValVLen);
02598   int GetVLen(const int& VId) const { if (VId==0){return 0;} else {return int(IdToOffV[VId]-IdToOffV[VId-1]);}}
02600   TVal* GetValVPt(const int& VId) const {
02601     if (GetVLen(VId)==0){return (TVal*)&EmptyVal;}
02602     else {return ValBf+IdToOffV[VId-1];}}
02604 
02606   void GetV(const int& VId, TValV& ValV) const {
02607     if (GetVLen(VId)==0){ValV.Clr();}
02608     else { ValV.GenExt(GetValVPt(VId), GetVLen(VId)); } }
02610   void PutV(const int& VId, const TValV& ValV) {
02611     IAssert(IsVId(VId) && GetVLen(VId) == ValV.Len());
02612     if (FastCopy) {
02613       memcpy(GetValVPt(VId), ValV.BegI(), sizeof(TVal)*ValV.Len()); }
02614     else { TVal* ValPt = GetValVPt(VId);
02615       for (::TSize ValN=0; ValN < ::TSize(ValV.Len()); ValN++, ValPt++) { *ValPt=ValV[ValN]; }
02616     } }
02618 
02620   void CompactPool(const TVal& DelVal);
02622 
02624   void ShuffleAll(TRnd& Rnd=TInt::Rnd);
02625   
02627 
02629   void Clr(bool DoDel = true) {
02630     IdToOffV.Clr(DoDel);  MxVals=0;  Vals=0;
02631     if (DoDel && ValBf!=NULL) { delete [] ValBf; ValBf=NULL;}
02632     if (! DoDel) { PutAll(EmptyVal); } }
02634   void PutAll(const TVal& Val) {
02635     for (TSize ValN = 0; ValN < MxVals; ValN++) { ValBf[ValN]=Val; } }
02636   friend class TPt<TVecPool<TVal> >;
02637 };
02638   
02639 template <class TVal, class TSizeTy>
02640 void TVecPool<TVal, TSizeTy>::Resize(const TSize& _MxVals){
02641   if (_MxVals <= MxVals){ return; } else { MxVals = _MxVals; }
02642   if (ValBf == NULL) {
02643     try { ValBf = new TVal [MxVals]; }
02644     catch (std::exception Ex) {
02645       FailR(TStr::Fmt("TVecPool::Resize 1: %s, MxVals: %s. [Program failed to allocate more memory. Solution: Get a bigger machine and a 64-bit compiler.]", Ex.what(), TInt::GetStr(uint64(_MxVals)).CStr()).CStr()); }
02646     IAssert(ValBf != NULL);
02647     if (EmptyVal != TVal()) { PutAll(EmptyVal); }
02648   } else {
02649     // printf("*** Resize vector pool: %llu -> %llu\n", uint64(Vals), uint64(MxVals));
02650     TVal* NewValBf = NULL;
02651     try { NewValBf = new TVal [MxVals]; }
02652     catch (std::exception Ex) {
02653       FailR(TStr::Fmt("TVecPool::Resize 1: %s, MxVals: %s. [Program failed to allocate more memory. Solution: Get a bigger machine and a 64-bit compiler.]", Ex.what(), TInt::GetStr(uint64(_MxVals)).CStr()).CStr()); }
02654     IAssert(NewValBf != NULL);
02655     if (FastCopy) {
02656       memcpy(NewValBf, ValBf, Vals*sizeof(TVal)); }
02657     else {
02658       for (TSize ValN = 0; ValN < Vals; ValN++){ NewValBf[ValN] = ValBf[ValN]; } }
02659     if (EmptyVal != TVal()) { // init empty values
02660       for (TSize ValN = Vals; ValN < MxVals; ValN++) { NewValBf[ValN] = EmptyVal; }
02661     }
02662     delete [] ValBf;
02663     ValBf = NewValBf;
02664   }
02665 }
02666 
02667 template <class TVal, class TSizeTy>
02668 TVecPool<TVal, TSizeTy>::TVecPool(const TSize& ExpectVals, const TSize& _GrowBy, const bool& _FastCopy, const TVal& _EmptyVal) : GrowBy(_GrowBy), MxVals(0), Vals(0), EmptyVal(_EmptyVal), ValBf(NULL) {
02669   IdToOffV.Add(0);
02670   Resize(ExpectVals);
02671 }
02672 
02673 template <class TVal, class TSizeTy>
02674 TVecPool<TVal, TSizeTy>::TVecPool(const TVecPool& Pool) : FastCopy(Pool.FastCopy), GrowBy(Pool.GrowBy), MxVals(Pool.MxVals), Vals(Pool.Vals), EmptyVal(Pool.EmptyVal), IdToOffV(Pool.IdToOffV) {
02675   try {
02676     ValBf = new TVal [MxVals]; }
02677   catch (std::exception Ex) {
02678     FailR(TStr::Fmt("TVecPool::TVecPool: %s, MxVals: %s. [Program failed to allocate memory. Solution: Get a bigger machine and a 64-bit compiler.]", Ex.what(), TInt::GetStr(uint64(MxVals)).CStr()).CStr()); }
02679   IAssert(ValBf != NULL);
02680   if (FastCopy) {
02681     memcpy(ValBf, Pool.ValBf, MxVals*sizeof(TVal)); }
02682   else {
02683     for (TSize ValN = 0; ValN < MxVals; ValN++){ ValBf[ValN] = Pool.ValBf[ValN]; } }
02684 }
02685 
02686 template <class TVal, class TSizeTy>
02687 TVecPool<TVal, TSizeTy>::TVecPool(TSIn& SIn) : FastCopy(SIn) {
02688   uint64 _GrowBy, _MxVals, _Vals;
02689   SIn.Load(_GrowBy); SIn.Load(_MxVals);  SIn.Load(_Vals);
02690   IAssertR(_GrowBy<TSizeMx && _MxVals<TSizeMx && _Vals<TSizeMx, "This is a 64-bit vector pool. Use a 64-bit compiler.");
02691   GrowBy=TSize(_GrowBy);  MxVals=TSize(_Vals);  Vals=TSize(_Vals); //note MxVals==Vals
02692   EmptyVal = TVal(SIn);
02693   if (MxVals==0) { ValBf = NULL; } else { ValBf = new TVal [MxVals]; }
02694   for (TSize ValN = 0; ValN < Vals; ValN++) { ValBf[ValN] = TVal(SIn); }
02695   { TInt MxVals(SIn), Vals(SIn);
02696     IdToOffV.Gen(Vals);
02697     for (int ValN = 0; ValN < Vals; ValN++) {
02698       uint64 Offset;  SIn.Load(Offset);  IAssert(Offset < TSizeMx);
02699       IdToOffV[ValN]=TSize(Offset);
02700     } }
02701 }
02702 
02703 template <class TVal, class TSizeTy>
02704 void TVecPool<TVal, TSizeTy>::Save(TSOut& SOut) const {
02705   SOut.Save(FastCopy);
02706   uint64 _GrowBy=GrowBy, _MxVals=MxVals, _Vals=Vals;
02707   SOut.Save(_GrowBy); SOut.Save(_MxVals);  SOut.Save(_Vals);
02708   SOut.Save(EmptyVal);
02709   for (TSize ValN = 0; ValN < Vals; ValN++) { ValBf[ValN].Save(SOut); }
02710   { SOut.Save(IdToOffV.Len());  SOut.Save(IdToOffV.Len());
02711     for (int ValN = 0; ValN < IdToOffV.Len(); ValN++) {
02712       const uint64 Offset=IdToOffV[ValN];  SOut.Save(Offset);
02713     } }
02714 }
02715 
02716 template <class TVal, class TSizeTy>
02717 TVecPool<TVal, TSizeTy>& TVecPool<TVal, TSizeTy>::operator = (const TVecPool& Pool) {
02718   if (this!=&Pool) {
02719     FastCopy = Pool.FastCopy;
02720     GrowBy = Pool.GrowBy;
02721     MxVals = Pool.MxVals;
02722     Vals = Pool.Vals;
02723     EmptyVal = Pool.EmptyVal;
02724     IdToOffV=Pool.IdToOffV;
02725     try {
02726       ValBf = new TVal [MxVals]; }
02727     catch (std::exception Ex) {
02728       FailR(TStr::Fmt("TVecPool::operator=: %s, MxVals: %s. [Program failed to allocate memory. Solution: Get a bigger machine and a 64-bit compiler.]", Ex.what(), TInt::GetStr(uint64(MxVals)).CStr()).CStr()); }
02729     IAssert(ValBf != NULL);
02730     if (FastCopy) {
02731       memcpy(ValBf, Pool.ValBf, Vals*sizeof(TVal)); }
02732     else {
02733       for (TSize ValN = 0; ValN < Vals; ValN++){ ValBf[ValN] = Pool.ValBf[ValN]; } }
02734   }
02735   return *this;
02736 }
02737 
02738 template <class TVal, class TSizeTy>
02739 int TVecPool<TVal, TSizeTy>::AddV(const TValV& ValV) {
02740   const TSizeTy ValVLen = ValV.Len();
02741   if (ValVLen == 0) { return 0; }
02742   if (MxVals < Vals+ValVLen) { Resize(Vals+max(ValVLen, GrowBy)); }
02743   if (FastCopy) { memcpy(ValBf+Vals, ValV.BegI(), sizeof(TVal)*ValV.Len()); }
02744   else { for (uint ValN=0; ValN < ValVLen; ValN++) { ValBf[Vals+ValN]=ValV[ValN]; } }
02745   Vals+=ValVLen;  IdToOffV.Add(Vals);
02746   return IdToOffV.Len()-1;
02747 }
02748 
02749 template <class TVal, class TSizeTy>
02750 int TVecPool<TVal, TSizeTy>::AddEmptyV(const int& ValVLen) {
02751   if (ValVLen==0){return 0;}
02752   if (MxVals < Vals+ValVLen){Resize(Vals+max(TSize(ValVLen), GrowBy)); }
02753   Vals+=ValVLen; IdToOffV.Add(Vals);
02754   return IdToOffV.Len()-1;
02755 }
02756 
02757 // Delete all elements of value DelVal from all vectors. Empty space is left at the end of the pool.
02758 template <class TVal, class TSizeTy>
02759 void TVecPool<TVal, TSizeTy>::CompactPool(const TVal& DelVal) {
02760   ::TSize TotalDel=0, NDel=0;
02761   // printf("Compacting %d vectors\n", IdToOffV.Len());
02762   for (int vid = 1; vid < IdToOffV.Len(); vid++) {
02763     // if (vid % 10000000 == 0) { printf(" %dm", vid/1000000);  fflush(stdout); }
02764     const uint Len = GetVLen(vid);
02765     TVal* ValV = GetValVPt(vid);
02766     if (TotalDel > 0) { IdToOffV[vid-1] -= TotalDel; } // update end of vector
02767     if (Len == 0) { continue; }
02768     NDel = 0;
02769     for (TVal* v = ValV; v < ValV+Len-NDel; v++) {
02770       if (*v == DelVal) {
02771         TVal* Beg = v;
02772         while (*v == DelVal && v < ValV+Len) { v++; NDel++; }
02773         memcpy(Beg, v, sizeof(TVal)*int(Len - ::TSize(v - ValV)));
02774         v -= NDel;
02775       }
02776     }
02777     memcpy(ValV-TotalDel, ValV, sizeof(TVal)*Len);  // move data
02778     TotalDel += NDel;
02779   }
02780   IdToOffV.Last() -= TotalDel;
02781   for (::TSize i = Vals-TotalDel; i < Vals; i++) { ValBf[i] = EmptyVal; }
02782   Vals -= TotalDel;
02783   // printf("  deleted %llu elements from the pool\n", TotalDel);
02784 }
02785 
02786 // shuffles all the order of elements in the pool (does not respect vector boundaries)
02787 template <class TVal, class TSizeTy>
02788 void TVecPool<TVal, TSizeTy>::ShuffleAll(TRnd& Rnd) {
02789   for (::TSize n = Vals-1; n > 0; n--) {
02790     const ::TSize k = ::TSize(((uint64(Rnd.GetUniDevInt())<<32) | uint64(Rnd.GetUniDevInt())) % (n+1));
02791     const TVal Tmp = ValBf[n];
02792     ValBf[n] = ValBf[k];
02793     ValBf[k] = Tmp;
02794   }
02795 }
02796 
02797 
02799 // Below are old 32-bit implementations of TVec and other classes.
02800 // Old TVec takes at most 2G elements.
02801 // The new vector class supports 64-bits for the number of elements,
02802 // but also allows 32-bits for backward compatibility.
02803 // by Jure (Jan 2013)
02804 namespace TGLib_OLD {
02806 // Vector Pool
02807 template<class TVal>
02808 class TVecPool {
02809 public:
02810   typedef TPt<TVecPool<TVal> > PVecPool;
02811   typedef TVec<TVal> TValV;
02812 private:
02813   TCRef CRef;
02814   TBool FastCopy;
02815   ::TSize GrowBy, MxVals, Vals;
02816   TVal EmptyVal;           // empty vector
02817   TVal *ValBf;             // buffer storing all the values
02818   TVec< ::TSize> IdToOffV; // id to one past last (vector starts at [id-1])
02819 private:
02820   void Resize(const ::TSize& _MxVals);
02821 public:
02822   TVecPool(const ::TSize& ExpectVals=0, const ::TSize& _GrowBy=1000000, const bool& _FastCopy=false, const TVal& _EmptyVal=TVal());
02823   TVecPool(const TVecPool& Pool);
02824   TVecPool(TSIn& SIn);
02825   ~TVecPool() { if (ValBf != NULL) { delete [] ValBf; } ValBf=NULL; }
02826   static PVecPool New(const ::TSize& ExpectVals=0, const ::TSize& GrowBy=1000000, const bool& FastCopy=false) {
02827     return new TVecPool(ExpectVals, GrowBy, FastCopy); }
02828   static PVecPool Load(TSIn& SIn) { return new TVecPool(SIn); }
02829   static PVecPool Load(const TStr& FNm) { TFIn FIn(FNm); return Load(FIn); }
02830   void Save(TSOut& SOut) const;
02831 
02832   TVecPool& operator = (const TVecPool& Pool);
02833 
02834   ::TSize GetVals() const { return Vals; }
02835   ::TSize GetVecs() const { return IdToOffV.Len(); }
02836   bool IsVId(const int& VId) const { return (0 <= VId) && (VId < IdToOffV.Len()); }
02837   ::TSize Reserved() const { return MxVals; }
02838   void Reserve(const ::TSize& MxVals) { Resize(MxVals); }
02839   const TVal& GetEmptyVal() const { return EmptyVal; }
02840   void SetEmptyVal(const TVal& _EmptyVal) { EmptyVal = _EmptyVal; }
02841   ::TSize GetMemUsed() const {
02842     return sizeof(TCRef)+sizeof(TBool)+3*sizeof(TSize)+sizeof(TVal*)+MxVals*sizeof(TVal);}
02843 
02844   int AddV(const TValV& ValV);
02845   int AddEmptyV(const int& ValVLen);
02846   uint GetVLen(const int& VId) const {
02847     if (VId==0){return 0;}
02848     else {return uint(IdToOffV[VId]-IdToOffV[VId-1]);}}
02849   TVal* GetValVPt(const int& VId) const {
02850     if (GetVLen(VId)==0){return (TVal*)&EmptyVal;}
02851     else {return ValBf+IdToOffV[VId-1];}}
02852   void GetV(const int& VId, TValV& ValV) const {
02853     if (GetVLen(VId)==0){ValV.Clr();}
02854     else { ValV.GenExt(GetValVPt(VId), GetVLen(VId)); } }
02855   void PutV(const int& VId, const TValV& ValV) {
02856     IAssert(IsVId(VId) && GetVLen(VId) == ValV.Len());
02857     if (FastCopy) {
02858       memcpy(GetValVPt(VId), ValV.BegI(), sizeof(TVal)*ValV.Len()); }
02859     else { TVal* ValPt = GetValVPt(VId);
02860       for (uint ValN=0; ValN < uint(ValV.Len()); ValN++, ValPt++) { *ValPt=ValV[ValN]; }
02861     }
02862   }
02863   void CompactPool(const TVal& DelVal); // delete all elements of value DelVal from all vectors
02864   void ShuffleAll(TRnd& Rnd=TInt::Rnd); // shuffles all the order of elements in the Pool (does not respect vector boundaries)
02865 
02866   //bool HasIdMap() const { return ! IdToOffV.Empty(); }
02867   //void ClrIdMap() { IdToOffV.Clr(true); }
02868   void Clr(bool DoDel = true) {
02869     IdToOffV.Clr(DoDel);  MxVals=0;  Vals=0;
02870     if (DoDel && ValBf!=NULL) { delete [] ValBf; ValBf=NULL;}
02871     if (! DoDel) { PutAll(EmptyVal); }
02872   }
02873   void PutAll(const TVal& Val) {
02874     for (TSize ValN = 0; ValN < MxVals; ValN++) { ValBf[ValN]=Val; } }
02875 
02876   friend class TPt<TVecPool<TVal> >;
02877 };
02878 
02879 template <class TVal>
02880 void TVecPool<TVal>::Resize(const ::TSize& _MxVals){
02881   if (_MxVals <= MxVals){ return; } else { MxVals = _MxVals; }
02882   if (ValBf == NULL) {
02883     try { ValBf = new TVal [MxVals]; }
02884     catch (std::exception Ex) {
02885       FailR(TStr::Fmt("TVecPool::Resize 1: %s, MxVals: %d. [Program failed to allocate more memory. Solution: Get a bigger machine and a 64-bit compiler.]", Ex.what(), _MxVals).CStr()); }
02886     IAssert(ValBf != NULL);
02887     if (EmptyVal != TVal()) { PutAll(EmptyVal); }
02888   } else {
02889     // printf("*** Resize vector pool: %llu -> %llu\n", uint64(Vals), uint64(MxVals));
02890     TVal* NewValBf = NULL;
02891     try { NewValBf = new TVal [MxVals]; }
02892     catch (std::exception Ex) { FailR(TStr::Fmt("TVecPool::Resize 2: %s, MxVals: %d. [Program failed to allocate more memory. Solution: Get a bigger machine and a 64-bit compiler.]", Ex.what(), _MxVals).CStr()); }
02893     IAssert(NewValBf != NULL);
02894     if (FastCopy) {
02895       memcpy(NewValBf, ValBf, Vals*sizeof(TVal)); }
02896     else {
02897       for (TSize ValN = 0; ValN < Vals; ValN++){ NewValBf[ValN] = ValBf[ValN]; } }
02898     if (EmptyVal != TVal()) { // init empty values
02899       for (TSize ValN = Vals; ValN < MxVals; ValN++) { NewValBf[ValN] = EmptyVal; }
02900     }
02901     delete [] ValBf;
02902     ValBf = NewValBf;
02903   }
02904 }
02905 
02906 template <class TVal>
02907 TVecPool<TVal>::TVecPool(const ::TSize& ExpectVals, const ::TSize& _GrowBy, const bool& _FastCopy, const TVal& _EmptyVal) :
02908   GrowBy(_GrowBy), MxVals(0), Vals(0), EmptyVal(_EmptyVal), ValBf(NULL) {
02909   IdToOffV.Add(0);
02910   Resize(ExpectVals);
02911 }
02912 
02913 template <class TVal>
02914 TVecPool<TVal>::TVecPool(const TVecPool& Pool):
02915   FastCopy(Pool.FastCopy), GrowBy(Pool.GrowBy),
02916   MxVals(Pool.MxVals), Vals(Pool.Vals), EmptyVal(Pool.EmptyVal), IdToOffV(Pool.IdToOffV) {
02917   try { ValBf = new TVal [MxVals]; }
02918   catch (std::exception Ex) { FailR(TStr::Fmt("TVecPool::TVecPool: %s, MxVals: %d", Ex.what(), MxVals).CStr()); }
02919   IAssert(ValBf != NULL);
02920   if (FastCopy) {
02921     memcpy(ValBf, Pool.ValBf, MxVals*sizeof(TVal)); }
02922   else {
02923     for (TSize ValN = 0; ValN < MxVals; ValN++){ ValBf[ValN] = Pool.ValBf[ValN]; } }
02924 }
02925 
02926 template <class TVal>
02927 TVecPool<TVal>::TVecPool(TSIn& SIn):
02928   FastCopy(SIn) {
02929   uint64 _GrowBy, _MxVals, _Vals;
02930   SIn.Load(_GrowBy); SIn.Load(_MxVals);  SIn.Load(_Vals);
02931   IAssert(_GrowBy<TSizeMx && _MxVals<TSizeMx && _Vals<TSizeMx);
02932   GrowBy=TSize(_GrowBy);  MxVals=TSize(_Vals);  Vals=TSize(_Vals); //note MxVals==Vals
02933   EmptyVal = TVal(SIn);
02934   if (MxVals==0) { ValBf = NULL; } else { ValBf = new TVal [MxVals]; }
02935   for (TSize ValN = 0; ValN < Vals; ValN++) { ValBf[ValN] = TVal(SIn); }
02936   { TInt MxVals(SIn), Vals(SIn);
02937   IdToOffV.Gen(Vals);
02938   for (int ValN = 0; ValN < Vals; ValN++) {
02939     uint64 Offset;  SIn.Load(Offset);  IAssert(Offset < TSizeMx);
02940     IdToOffV[ValN]=TSize(Offset);
02941   } }
02942 }
02943 
02944 template <class TVal>
02945 void TVecPool<TVal>::Save(TSOut& SOut) const {
02946   SOut.Save(FastCopy);
02947   uint64 _GrowBy=GrowBy, _MxVals=MxVals, _Vals=Vals;
02948   SOut.Save(_GrowBy); SOut.Save(_MxVals);  SOut.Save(_Vals);
02949   SOut.Save(EmptyVal);
02950   for (TSize ValN = 0; ValN < Vals; ValN++) { ValBf[ValN].Save(SOut); }
02951   { SOut.Save(IdToOffV.Len());  SOut.Save(IdToOffV.Len());
02952   for (int ValN = 0; ValN < IdToOffV.Len(); ValN++) {
02953     const uint64 Offset=IdToOffV[ValN];  SOut.Save(Offset);
02954   } }
02955 }
02956 
02957 template <class TVal>
02958 TVecPool<TVal>& TVecPool<TVal>::operator = (const TVecPool& Pool) {
02959   if (this!=&Pool) {
02960     FastCopy = Pool.FastCopy;
02961     GrowBy = Pool.GrowBy;
02962     MxVals = Pool.MxVals;
02963     Vals = Pool.Vals;
02964     EmptyVal = Pool.EmptyVal;
02965     IdToOffV=Pool.IdToOffV;
02966     try { ValBf = new TVal [MxVals]; }
02967     catch (std::exception Ex) { FailR(TStr::Fmt("TVec::operator= : %s, MxVals: %d", Ex.what(), MxVals).CStr()); }
02968     IAssert(ValBf != NULL);
02969     if (FastCopy) {
02970       memcpy(ValBf, Pool.ValBf, Vals*sizeof(TVal)); }
02971     else {
02972       for (uint64 ValN = 0; ValN < Vals; ValN++){ ValBf[ValN] = Pool.ValBf[ValN]; } }
02973   }
02974   return *this;
02975 }
02976 
02977 template<class TVal>
02978 int TVecPool<TVal>::AddV(const TValV& ValV) {
02979   const ::TSize ValVLen = ValV.Len();
02980   if (ValVLen == 0) { return 0; }
02981   if (MxVals < Vals+ValVLen) { Resize(Vals+max(ValVLen, GrowBy)); }
02982   if (FastCopy) { memcpy(ValBf+Vals, ValV.BegI(), sizeof(TVal)*ValV.Len()); }
02983   else { for (uint ValN=0; ValN < ValVLen; ValN++) { ValBf[Vals+ValN]=ValV[ValN]; } }
02984   Vals+=ValVLen;  IdToOffV.Add(Vals);
02985   return IdToOffV.Len()-1;
02986 }
02987 
02988 template<class TVal>
02989 int TVecPool<TVal>::AddEmptyV(const int& ValVLen) {
02990   if (ValVLen==0){return 0;}
02991   if (MxVals < Vals+ValVLen){Resize(Vals+max(TSize(ValVLen), GrowBy)); }
02992   Vals+=ValVLen; IdToOffV.Add(Vals);
02993   return IdToOffV.Len()-1;
02994 }
02995 
02996 // delete all elements of value DelVal from all vectors
02997 // empty space is left at the end of the pool
02998 template<class TVal>
02999 void TVecPool<TVal>::CompactPool(const TVal& DelVal) {
03000   ::TSize TotalDel=0, NDel=0;
03001   // printf("Compacting %d vectors\n", IdToOffV.Len());
03002   for (int vid = 1; vid < IdToOffV.Len(); vid++) {
03003     // if (vid % 10000000 == 0) { printf(" %dm", vid/1000000);  fflush(stdout); }
03004     const uint Len = GetVLen(vid);
03005     TVal* ValV = GetValVPt(vid);
03006     if (TotalDel > 0) { IdToOffV[vid-1] -= TotalDel; } // update end of vector
03007     if (Len == 0) { continue; }
03008     NDel = 0;
03009     for (TVal* v = ValV; v < ValV+Len-NDel; v++) {
03010       if (*v == DelVal) {
03011         TVal* Beg = v;
03012         while (*v == DelVal && v < ValV+Len) { v++; NDel++; }
03013         memcpy(Beg, v, sizeof(TVal)*int(Len - ::TSize(v - ValV)));
03014         v -= NDel;
03015       }
03016     }
03017     memcpy(ValV-TotalDel, ValV, sizeof(TVal)*Len);  // move data
03018     TotalDel += NDel;
03019   }
03020   IdToOffV.Last() -= TotalDel;
03021   for (::TSize i = Vals-TotalDel; i < Vals; i++) { ValBf[i] = EmptyVal; }
03022   Vals -= TotalDel;
03023   // printf("  deleted %llu elements from the pool\n", TotalDel);
03024 }
03025 
03026 // shuffles all the order of elements in the pool (does not respect vector boundaries)
03027 template<class TVal>
03028 void TVecPool<TVal>::ShuffleAll(TRnd& Rnd) {
03029   for (::TSize n = Vals-1; n > 0; n--) {
03030     const ::TSize k = ::TSize(((uint64(Rnd.GetUniDevInt())<<32) | uint64(Rnd.GetUniDevInt())) % (n+1));
03031     const TVal Tmp = ValBf[n];
03032     ValBf[n] = ValBf[k];
03033     ValBf[k] = Tmp;
03034   }
03035 }
03036 
03037 }; // namespace TGLib_OLD
03038 
03039 typedef TVecPool<TInt> TIntVecPool;
03040 typedef TPt<TIntVecPool> PIntVecPool;
03041 
03043 // Vector-Pointer
03044 template <class TVal>
03045 class PVec{
03046 private:
03047   TCRef CRef;
03048 public:
03049   TVec<TVal> V;
03050 public:
03051   PVec<TVal>(): V(){}
03052   PVec<TVal>(const PVec<TVal>& Vec): V(Vec.V){}
03053   static TPt<PVec<TVal> > New(){
03054     return new PVec<TVal>();}
03055   PVec<TVal>(const int& MxVals, const int& Vals): V(MxVals, Vals){}
03056   static TPt<PVec<TVal> > New(const int& MxVals, const int& Vals){
03057     return new PVec<TVal>(MxVals, Vals);}
03058   PVec<TVal>(const TVec<TVal>& _V): V(_V){}
03059   static TPt<PVec<TVal> > New(const TVec<TVal>& V){
03060     return new PVec<TVal>(V);}
03061   explicit PVec<TVal>(TSIn& SIn): V(SIn){}
03062   static TPt<PVec<TVal> > Load(TSIn& SIn){return new PVec<TVal>(SIn);}
03063   void Save(TSOut& SOut) const {V.Save(SOut);}
03064 
03065   PVec<TVal>& operator=(const PVec<TVal>& Vec){
03066     if (this!=&Vec){V=Vec.V;} return *this;}
03067   bool operator==(const PVec<TVal>& Vec) const {return V==Vec.V;}
03068   bool operator<(const PVec<TVal>& Vec) const {return V<Vec.V;}
03069   TVal& operator[](const int& ValN) const {return V[ValN];}
03070 
03071   bool Empty() const {return V.Empty();}
03072   int Len() const {return V.Len();}
03073   TVal GetVal(const int& ValN) const {return V[ValN];}
03074 
03075   int Add(const TVal& Val){return V.Add(Val);}
03076 
03077   friend class TPt<PVec<TVal> >;
03078 };
03079 
03081 // Common-Vector-Pointer-Types
03082 typedef PVec<TFlt> TFltVP;
03083 typedef TPt<TFltVP> PFltV;
03084 typedef PVec<TAscFlt> TAscFltVP;
03085 typedef TPt<TAscFltVP> PAscFltV;
03086 typedef PVec<TStr> TStrVP;
03087 typedef TPt<TStrVP> PStrV;
03088 
03090 // 2D-Vector
03091 template <class TVal>
03092 class TVVec{
03093 private:
03094   TInt XDim, YDim;
03095   TVec<TVal> ValV;
03096 public:
03097   TVVec(): XDim(), YDim(), ValV(){}
03098   TVVec(const TVVec& Vec):
03099     XDim(Vec.XDim), YDim(Vec.YDim), ValV(Vec.ValV){}
03100   TVVec(const int& _XDim, const int& _YDim):
03101     XDim(), YDim(), ValV(){Gen(_XDim, _YDim);}
03102   explicit TVVec(const TVec<TVal>& _ValV, const int& _XDim, const int& _YDim):
03103     XDim(_XDim), YDim(_YDim), ValV(_ValV){ IAssert(ValV.Len()==XDim*YDim); }
03104   explicit TVVec(TSIn& SIn) {Load(SIn);}
03105   void Load(TSIn& SIn){XDim.Load(SIn); YDim.Load(SIn); ValV.Load(SIn);}
03106   void Save(TSOut& SOut) const {
03107     XDim.Save(SOut); YDim.Save(SOut); ValV.Save(SOut);}
03108 
03109   TVVec<TVal>& operator=(const TVVec<TVal>& Vec){
03110     if (this!=&Vec){XDim=Vec.XDim; YDim=Vec.YDim; ValV=Vec.ValV;} return *this;}
03111   bool operator==(const TVVec& Vec) const {
03112     return (XDim==Vec.XDim)&&(YDim==Vec.YDim)&&(ValV==Vec.ValV);}
03113 
03114   bool Empty() const {return ValV.Len()==0;}
03115   void Clr(){XDim=0; YDim=0; ValV.Clr();}
03116   void Gen(const int& _XDim, const int& _YDim){
03117     Assert((_XDim>=0)&&(_YDim>=0));
03118     XDim=_XDim; YDim=_YDim; ValV.Gen(XDim*YDim);}
03119   int GetXDim() const {return XDim;}
03120   int GetYDim() const {return YDim;}
03121   int GetRows() const {return XDim;}
03122   int GetCols() const {return YDim;}
03123   TVec<TVal>& Get1DVec(){return ValV;}
03124 
03125   const TVal& At(const int& X, const int& Y) const {
03126     Assert((0<=X)&&(X<int(XDim))&&(0<=Y)&&(Y<int(YDim)));
03127     return ValV[X*YDim+Y];}
03128   TVal& At(const int& X, const int& Y){
03129     Assert((0<=X)&&(X<int(XDim))&&(0<=Y)&&(Y<int(YDim)));
03130     return ValV[X*YDim+Y];}
03131   TVal& operator()(const int& X, const int& Y){
03132     return At(X, Y);}
03133   const TVal& operator()(const int& X, const int& Y) const {
03134     return At(X, Y);}
03135 
03136   void PutXY(const int& X, const int& Y, const TVal& Val){At(X, Y)=Val;}
03137   void PutAll(const TVal& Val){ValV.PutAll(Val);}
03138   void PutX(const int& X, const TVal& Val){
03139     for (int Y=0; Y<int(YDim); Y++){At(X, Y)=Val;}}
03140   void PutY(const int& Y, const TVal& Val){
03141     for (int X=0; X<int(XDim); X++){At(X, Y)=Val;}}
03142   TVal GetXY(const int& X, const int& Y) const {
03143     Assert((0<=X)&&(X<int(XDim))&&(0<=Y)&&(Y<int(YDim)));
03144     return ValV[X*YDim+Y];}
03145   void GetRow(const int& RowN, TVec<TVal>& Vec) const;
03146   void GetCol(const int& ColN, TVec<TVal>& Vec) const;
03147 
03148   void SwapX(const int& X1, const int& X2);
03149   void SwapY(const int& Y1, const int& Y2);
03150   void Swap(TVVec<TVal>& Vec);
03151 
03152   void ShuffleX(TRnd& Rnd);
03153   void ShuffleY(TRnd& Rnd);
03154   void GetMxValXY(int& X, int& Y) const;
03155 
03156   void CopyFrom(const TVVec<TVal>& VVec);
03157   void AddXDim();
03158   void AddYDim();
03159   void DelX(const int& X);
03160   void DelY(const int& Y);
03161 };
03162 
03163 template <class TVal>
03164 void TVVec<TVal>::SwapX(const int& X1, const int& X2){
03165   for (int Y=0; Y<int(YDim); Y++){
03166     TVal Val=At(X1, Y); At(X1, Y)=At(X2, Y); At(X2, Y)=Val;}
03167 }
03168 
03169 template <class TVal>
03170 void TVVec<TVal>::SwapY(const int& Y1, const int& Y2){
03171   for (int X=0; X<int(XDim); X++){
03172     TVal Val=At(X, Y1); At(X, Y1)=At(X, Y2); At(X, Y2)=Val;}
03173 }
03174 
03175 template <class TVal>
03176 void TVVec<TVal>::Swap(TVVec<TVal>& Vec){  //J:
03177   if (this!=&Vec){
03178     ::Swap(XDim, Vec.XDim);
03179     ::Swap(YDim, Vec.YDim);
03180     ValV.Swap(Vec.ValV);
03181   }
03182 }
03183 
03184 template <class TVal>
03185 void TVVec<TVal>::ShuffleX(TRnd& Rnd){
03186   for (int X=0; X<XDim-1; X++){SwapX(X, X+Rnd.GetUniDevInt(XDim-X));}
03187 }
03188 
03189 template <class TVal>
03190 void TVVec<TVal>::ShuffleY(TRnd& Rnd){
03191   for (int Y=0; Y<YDim-1; Y++){SwapY(Y, Y+Rnd.GetUniDevInt(YDim-Y));}
03192 }
03193 
03194 template <class TVal>
03195 void TVVec<TVal>::GetMxValXY(int& X, int& Y) const {
03196   int MxValN=ValV.GetMxValN();
03197   Y=MxValN%YDim;
03198   X=MxValN/YDim;
03199 }
03200 
03201 template <class TVal>
03202 void TVVec<TVal>::CopyFrom(const TVVec<TVal>& VVec){
03203   int CopyXDim=TInt::GetMn(GetXDim(), VVec.GetXDim());
03204   int CopyYDim=TInt::GetMn(GetYDim(), VVec.GetYDim());
03205   for (int X=0; X<CopyXDim; X++){
03206     for (int Y=0; Y<CopyYDim; Y++){
03207       At(X, Y)=VVec.At(X, Y);
03208     }
03209   }
03210 }
03211 
03212 template <class TVal>
03213 void TVVec<TVal>::AddXDim(){
03214   TVVec<TVal> NewVVec(XDim+1, YDim);
03215   NewVVec.CopyFrom(*this);
03216   *this=NewVVec;
03217 }
03218 
03219 template <class TVal>
03220 void TVVec<TVal>::AddYDim(){
03221   TVVec<TVal> NewVVec(XDim, YDim+1);
03222   NewVVec.CopyFrom(*this);
03223   *this=NewVVec;
03224 }
03225 
03226 template <class TVal>
03227 void TVVec<TVal>::DelX(const int& X){
03228   TVVec<TVal> NewVVec(XDim-1, YDim);
03229   for (int Y=0; Y<YDim; Y++){
03230     for (int LX=0; LX<X; LX++){
03231       NewVVec.At(LX, Y)=At(LX, Y);}
03232     for (int RX=X+1; RX<XDim; RX++){
03233       NewVVec.At(RX-1, Y)=At(RX, Y);}
03234   }
03235   *this=NewVVec;
03236 }
03237 
03238 template <class TVal>
03239 void TVVec<TVal>::DelY(const int& Y){
03240   TVVec<TVal> NewVVec(XDim, YDim-1);
03241   for (int X=0; X<XDim; X++){
03242     for (int LY=0; LY<Y; LY++){
03243       NewVVec.At(X, LY)=At(X, LY);}
03244     for (int RY=Y+1; RY<YDim; RY++){
03245       NewVVec.At(X, RY-1)=At(X, RY);}
03246   }
03247   *this=NewVVec;
03248 }
03249 
03250 template <class TVal>
03251 void TVVec<TVal>::GetRow(const int& RowN, TVec<TVal>& Vec) const {
03252   Vec.Gen(GetCols(), 0);
03253   for (int col = 0; col < GetCols(); col++) {
03254     Vec.Add(At(RowN, col));
03255   }
03256 }
03257 
03258 template <class TVal>
03259 void TVVec<TVal>::GetCol(const int& ColN, TVec<TVal>& Vec) const {
03260   Vec.Gen(GetRows(), 0);
03261   for (int row = 0; row < GetRows(); row++) {
03262     Vec.Add(At(row, ColN));
03263   }
03264 }
03265 
03267 // Common-2D-Vector-Types
03268 typedef TVVec<TBool> TBoolVV;
03269 typedef TVVec<TCh> TChVV;
03270 typedef TVVec<TInt> TIntVV;
03271 typedef TVVec<TSFlt> TSFltVV;
03272 typedef TVVec<TFlt> TFltVV;
03273 typedef TVVec<TStr> TStrVV;
03274 typedef TVVec<TIntPr> TIntPrVV;
03275 
03277 // 3D-Vector
03278 template <class TVal>
03279 class TVVVec{
03280 private:
03281   TInt XDim, YDim, ZDim;
03282   TVec<TVal> ValV;
03283 public:
03284   TVVVec(): XDim(), YDim(), ZDim(), ValV(){}
03285   TVVVec(const TVVVec& Vec):
03286     XDim(Vec.XDim), YDim(Vec.YDim), ZDim(Vec.ZDim), ValV(Vec.ValV){}
03287   TVVVec(const int& _XDim, const int& _YDim, const int& _ZDim):
03288     XDim(), YDim(), ZDim(), ValV(){Gen(_XDim, _YDim, _ZDim);}
03289   explicit TVVVec(TSIn& SIn):
03290     XDim(SIn), YDim(SIn), ZDim(SIn), ValV(SIn){}
03291   void Save(TSOut& SOut) const {
03292     XDim.Save(SOut); YDim.Save(SOut); ZDim.Save(SOut); ValV.Save(SOut);}
03293 
03294   TVVVec<TVal>& operator=(const TVVVec<TVal>& Vec){
03295     XDim=Vec.XDim; YDim=Vec.YDim; ZDim=Vec.ZDim; ValV=Vec.ValV;
03296     return *this;
03297   }
03298   bool operator==(const TVVVec& Vec) const {
03299     return (XDim==Vec.XDim)&&(YDim==Vec.YDim)&&(ZDim==Vec.ZDim)&&
03300      (ValV==Vec.ValV);}
03301 
03302   bool Empty() const {return ValV.Len()==0;}
03303   void Clr(){XDim=0; YDim=0; ZDim=0; ValV.Clr();}
03304   void Gen(const int& _XDim, const int& _YDim, const int& _ZDim){
03305     Assert((_XDim>=0)&&(_YDim>=0)&&(_ZDim>=0));
03306     XDim=_XDim; YDim=_YDim; ZDim=_ZDim; ValV.Gen(XDim*YDim*ZDim);}
03307   TVal& At(const int& X, const int& Y, const int& Z){
03308     Assert((0<=X)&&(X<int(XDim))&&(0<=Y)&&(Y<int(YDim))&&(0<=Z)&&(Z<int(ZDim)));
03309     return ValV[X*YDim*ZDim+Y*ZDim+Z];}
03310   const TVal& At(const int& X, const int& Y, const int& Z) const {
03311     Assert((0<=X)&&(X<int(XDim))&&(0<=Y)&&(Y<int(YDim))&&(0<=Z)&&(Z<int(ZDim)));
03312     return ValV[X*YDim*ZDim+Y*ZDim+Z];}
03313   TVal& operator()(const int& X, const int& Y, const int& Z){
03314     return At(X, Y, Z);}
03315   const TVal& operator()(const int& X, const int& Y, const int& Z) const {
03316     return At(X, Y, Z);}
03317   int GetXDim() const {return XDim;}
03318   int GetYDim() const {return YDim;}
03319   int GetZDim() const {return ZDim;}
03320 };
03321 
03323 // Common-3D-Vector-Types
03324 typedef TVVVec<TInt> TIntVVV;
03325 typedef TVVVec<TFlt> TFltVVV;
03326 
03328 // Tree
03329 template <class TVal>
03330 class TTree{
03331 private:
03332   TVec<TTriple<TInt, TIntV, TVal> > NodeV; // (ParentNodeId, ChildNodeIdV, NodeVal)
03333 public:
03334   TTree(): NodeV(){}
03335   TTree(const TTree& Tree): NodeV(Tree.NodeV){}
03336   explicit TTree(TSIn& SIn): NodeV(SIn){}
03337   void Save(TSOut& SOut) const {NodeV.Save(SOut);}
03338   void LoadXml(const PXmlTok& XmlTok, const TStr& Nm="");
03339   void SaveXml(TSOut& SOut, const TStr& Nm) const;
03340 
03341   TTree& operator=(const TTree& Tree){if (this!=&Tree){NodeV=Tree.NodeV;} return *this;}
03342   bool operator==(const TTree& Tree) const {return NodeV==Tree.NodeV;}
03343   bool operator<(const TTree& Tree) const {return false;}
03344 
03345   int GetPrimHashCd() const {return NodeV.GetPrimHashCd();}
03346   int GetSecHashCd() const {return NodeV.GetSecHashCd();}
03347 
03348   int GetMemUsed() const {return NodeV.GetMemUsed();}
03349 
03350   void Clr(){NodeV.Clr();}
03351 
03352   int AddNode(const int& ParentNodeId, const TVal& NodeVal=TVal()){
03353     IAssert(((ParentNodeId==-1)&&(NodeV.Len()==0))||(NodeV.Len()>0));
03354     if (ParentNodeId!=-1){NodeV[ParentNodeId].Val2.Add(NodeV.Len());}
03355     return NodeV.Add(TTriple<TInt, TIntV, TVal>(ParentNodeId, TIntV(), NodeVal));}
03356   int AddRoot(const TVal& NodeVal=TVal()){
03357     return AddNode(-1, NodeVal);}
03358  
03359   int GetNodes() const {return NodeV.Len();}
03360   void GetNodeIdV(TIntV& NodeIdV, const int& NodeId=0);
03361   int GetParentNodeId(const int& NodeId) const {return NodeV[NodeId].Val1;} 
03362   int GetChildren(const int& NodeId) const {return NodeV[NodeId].Val2.Len();}
03363   int GetChildNodeId(const int& NodeId, const int& ChildN) const {return NodeV[NodeId].Val2[ChildN];}
03364   TVal& GetNodeVal(const int& NodeId){return NodeV[NodeId].Val3;}
03365 
03366   void GenRandomTree(const int& Nodes, TRnd& Rnd);
03367 
03368   void DelNode(const int& NodeId);
03369   void CopyTree(const int& SrcNodeId, TTree& DstTree, const int& DstParentNodeId=-1);
03370 
03371   void WrTree(const int& NodeId=0, const int& Lev=0);
03372 };
03373 
03374 template <class TVal>
03375 void TTree<TVal>::GetNodeIdV(TIntV& NodeIdV, const int& NodeId){
03376   if (NodeId==0){NodeIdV.Clr(); if (GetNodes()==0){return;}}
03377   else if (GetParentNodeId(NodeId)==-1){return;}
03378   NodeIdV.Add(NodeId);
03379   for (int ChildN=0; ChildN<GetChildren(NodeId); ChildN++){
03380     int ChildNodeId=GetChildNodeId(NodeId, ChildN);
03381     if (ChildNodeId!=-1){
03382       GetNodeIdV(NodeIdV, ChildNodeId);
03383     }
03384   }
03385 }
03386 
03387 template <class TVal>
03388 void TTree<TVal>::GenRandomTree(const int& Nodes, TRnd& Rnd){
03389   Clr();
03390   if (Nodes>0){
03391     AddRoot(TVal());
03392     for (int NodeN=1; NodeN<Nodes; NodeN++){
03393       int ParentNodeId=Rnd.GetUniDevInt(0, GetNodes()-1);
03394       AddNode(ParentNodeId, TVal());
03395     }
03396   }
03397 }
03398 
03399 template <class TVal>
03400 void TTree<TVal>::DelNode(const int& NodeId){
03401   if (NodeId==0){
03402     Clr();
03403   } else {
03404     TIntV& ChildNodeIdV=NodeV[GetParentNodeId(NodeId)].Val2;
03405     int ChildNodeIdN=ChildNodeIdV.SearchForw(NodeId);
03406     ChildNodeIdV[ChildNodeIdN]=-1;
03407   }
03408 }
03409 
03410 template <class TVal>
03411 void TTree<TVal>::CopyTree(const int& SrcNodeId, TTree& DstTree, const int& DstParentNodeId){
03412   int DstNodeId=DstTree.AddNode(DstParentNodeId, GetNodeVal(SrcNodeId));
03413   for (int ChildN=0; ChildN<GetChildren(SrcNodeId); ChildN++){
03414     int ChildNodeId=GetChildNodeId(SrcNodeId, ChildN);
03415     if (ChildNodeId!=-1){
03416       CopyTree(ChildNodeId, DstTree, DstNodeId);
03417     }
03418   }
03419 }
03420 
03421 template <class TVal>
03422 void TTree<TVal>::WrTree(const int& NodeId, const int& Lev){
03423   for (int LevN=0; LevN<Lev; LevN++){printf("| ");}
03424   printf("%d (%d)\n", NodeId, GetChildren(NodeId));
03425   for (int ChildN=0; ChildN<GetChildren(NodeId); ChildN++){
03426     int ChildNodeId=GetChildNodeId(NodeId, ChildN);
03427     if (ChildNodeId!=-1){
03428       WrTree(ChildNodeId, Lev+1);
03429     }
03430   }
03431 }
03432 
03434 // Common-Tree-Types
03435 typedef TTree<TInt> TIntTree;
03436 typedef TTree<TFlt> TFltTree;
03437 typedef TTree<TStr> TStrTree;
03438 typedef TTree<TStrIntPr> TStrIntPrTree;
03439 typedef TTree<TStrIntStrVTr> TStrIntStrVTrTree;
03440 
03442 // Stack
03443 template <class TVal>
03444 class TSStack{
03445 private:
03446   TVec<TVal> ValV;
03447 public:
03448   TSStack(): ValV(){}
03449   TSStack(const int& MxVals): ValV(MxVals, 0){}
03450   TSStack(const TSStack& Stack): ValV(Stack.ValV){}
03451   explicit TSStack(TSIn& SIn): ValV(SIn){}
03452   void Save(TSOut& SOut) const {ValV.Save(SOut);}
03453 
03454   TSStack& operator=(const TSStack& Stack){
03455     if (this!=&Stack){ValV=Stack.ValV;} return *this;}
03456   bool operator==(const TSStack& Stack) const {return this==&Stack;}
03457   const TVal& operator[](const int& ValN) const {return ValV[ValV.Len()-ValN-1];}
03458   TVal& operator[](const int& ValN) {return ValV[ValV.Len()-ValN-1];}
03459 
03460   bool Empty(){return ValV.Len()==0;}
03461   void Clr(const bool& DoDel=false) {ValV.Clr(DoDel);}
03462   bool IsIn(const TVal& Val) const {return ValV.IsIn(Val);}
03463   int Len(){return ValV.Len();}
03464   TVal& Top(){Assert(0<ValV.Len()); return ValV.Last();}
03465   const TVal& Top() const {Assert(0<ValV.Len()); return ValV.Last();}
03466   void Push(){ValV.Add();}
03467   void Push(const TVal& Val){ValV.Add(Val);}
03468   void Pop(){Assert(0<ValV.Len()); ValV.DelLast();}
03469 };
03470 
03472 // Common-Stack-Types
03473 typedef TSStack<TInt> TIntS;
03474 typedef TSStack<TBoolChPr> TBoolChS;
03475 
03477 // Queue
03478 template <class TVal>
03479 class TQQueue{
03480 private:
03481   TInt MxLast, MxLen;
03482   TInt First, Last;
03483   TVec<TVal> ValV;
03484 public:
03485   TQQueue(const int& _MxLast=64, const int& _MxLen=-1):
03486     MxLast(_MxLast), MxLen(_MxLen), First(0), Last(0), ValV(){
03487     Assert(int(MxLast)>0); Assert((MxLen==-1)||(int(MxLen)>0));}
03488   TQQueue(const TQQueue& Queue):
03489     MxLast(Queue.MxLast), MxLen(Queue.MxLen),
03490     First(Queue.First), Last(Queue.Last), ValV(Queue.ValV){}
03491   explicit TQQueue(TSIn& SIn):
03492     MxLast(SIn), MxLen(SIn), First(SIn), Last(SIn), ValV(SIn){}
03493   void Save(TSOut& SOut) const {
03494     MxLast.Save(SOut); MxLen.Save(SOut);
03495     First.Save(SOut); Last.Save(SOut); ValV.Save(SOut);}
03496 
03497   TQQueue& operator=(const TQQueue& Queue){
03498     if (this!=&Queue){MxLast=Queue.MxLast; MxLen=Queue.MxLen;
03499       First=Queue.First; Last=Queue.Last; ValV=Queue.ValV;}
03500     return *this;}
03501   const TVal& operator[](const int& ValN) const {Assert((0<=ValN)&&(ValN<Len()));
03502     return ValV[Last+ValN];}
03503 
03504   void Clr(const bool& DoDel=true){ValV.Clr(DoDel); First=Last=0;}
03505   void Gen(const int& _MxLast=64, const int& _MxLen=-1){
03506     MxLast=_MxLast; MxLen=_MxLen; First=0; Last=0; ValV.Clr();}
03507   void GetSubValV(const int& _BValN, const int& _EValN, TVec<TVal>& SubValV) const {
03508     int BValN=TInt::GetMx(0, _BValN);
03509     int EValN=TInt::GetMn(Len()-1, _EValN);
03510     SubValV.Gen(EValN-BValN+1);
03511     for (int ValN=BValN; ValN<=EValN; ValN++){
03512       SubValV[ValN-BValN]=ValV[Last+ValN];}
03513   }
03514 
03515   bool Empty() const {return First==Last;}
03516   int Len() const {return First-Last;}
03517   const TVal& Top() const {
03518     Assert(First!=Last); return ValV[Last];}
03519   void Pop(){
03520     IAssert(First!=Last); Last++;
03521     if (First==Last){ValV.Clr(); First=Last=0;}}
03522   void Push(const TVal& Val){
03523     if (Last>MxLast){ValV.Del(0, Last-1); First-=Last; Last=0;}
03524     if ((MxLen!=-1)&&(MxLen==Len())){Pop();}
03525     First++; ValV.Add(Val);}
03526 
03527   void Shuffle(TRnd& Rnd){
03528     TVec<TVal> ValV(Len(), 0); while (!Empty()){ValV.Add(Top()); Pop();}
03529     ValV.Shuffle(Rnd); Clr();
03530     for (int ValN=0; ValN<ValV.Len(); ValN++){Push(ValV[ValN]);}}
03531 };
03532 
03534 // Common-Queue-Types
03535 typedef TQQueue<TInt> TIntQ;
03536 typedef TQQueue<TFlt> TFltQ;
03537 typedef TQQueue<TStr> TStrQ;
03538 typedef TQQueue<TIntPr> TIntPrQ;
03539 typedef TQQueue<TIntStrPr> TIntStrPrQ;
03540 typedef TQQueue<TFltV> TFltVQ;
03541 typedef TQQueue<TAscFltV> TAscFltVQ;
03542 typedef TVec<TQQueue<TInt> > TIntQV;
03543 
03545 // List-Node
03546 template <class TVal>
03547 class TLstNd{
03548 public:
03549   TLstNd* PrevNd;
03550   TLstNd* NextNd;
03551   TVal Val;
03552 public:
03553   TLstNd(): PrevNd(NULL), NextNd(NULL), Val(){}
03554   TLstNd(const TLstNd&);
03555   TLstNd(TLstNd* _PrevNd, TLstNd* _NextNd, const TVal& _Val):
03556     PrevNd(_PrevNd), NextNd(_NextNd), Val(_Val){}
03557 
03558   TLstNd& operator=(const TLstNd&);
03559 
03560   bool IsPrev() const {return (PrevNd != NULL); }
03561   bool IsNext() const {return (NextNd != NULL); }
03562   TLstNd* Prev() const {Assert(this!=NULL); return PrevNd;}
03563   TLstNd* Next() const {Assert(this!=NULL); return NextNd;}
03564   TVal& GetVal(){Assert(this!=NULL); return Val;}
03565   const TVal& GetVal() const {Assert(this!=NULL); return Val;}
03566 };
03567 
03569 // List
03570 template <class TVal>
03571 class TLst{
03572 public:
03573   typedef TLstNd<TVal>* PLstNd;
03574 private:
03575   int Nds;
03576   PLstNd FirstNd;
03577   PLstNd LastNd;
03578 public:
03579   TLst(): Nds(0), FirstNd(NULL), LastNd(NULL){}
03580   TLst(const TLst&);
03581   ~TLst(){Clr();}
03582   explicit TLst(TSIn& SIn);
03583   void Save(TSOut& SOut) const;
03584 
03585   TLst& operator=(const TLst&);
03586 
03587   void Clr(){
03588     PLstNd Nd=FirstNd;
03589     while (Nd!=NULL){PLstNd NextNd=Nd->NextNd; delete Nd; Nd=NextNd;}
03590     Nds=0; FirstNd=NULL; LastNd=NULL;}
03591 
03592   bool Empty() const {return Nds==0;}
03593   int Len() const {return Nds;}
03594   PLstNd First() const {return FirstNd;}
03595   PLstNd Last() const {return LastNd;}
03596   TVal& FirstVal() const {return FirstNd->GetVal();}
03597   TVal& LastVal() const {return LastNd->GetVal();}
03598 
03599   PLstNd AddFront(const TVal& Val);
03600   PLstNd AddBack(const TVal& Val);
03601   PLstNd AddFrontSorted(const TVal& Val, const bool& Asc=true);
03602   PLstNd AddBackSorted(const TVal& Val, const bool& Asc=true);
03603   void PutFront(const PLstNd& Nd);
03604   void PutBack(const PLstNd& Nd);
03605   PLstNd Ins(const PLstNd& Nd, const TVal& Val);
03606   void Del(const TVal& Val);
03607   void Del(const PLstNd& Nd);
03608   void DelFirst() { PLstNd DelNd = FirstNd; Del(DelNd); }
03609   void DelLast() { PLstNd DelNd = LastNd; Del(DelNd); }
03610 
03611   PLstNd SearchForw(const TVal& Val);
03612   PLstNd SearchBack(const TVal& Val);
03613 
03614   friend class TLstNd<TVal>;
03615 };
03616 
03617 template <class TVal>
03618 TLst<TVal>::TLst(TSIn& SIn):
03619   Nds(0), FirstNd(NULL), LastNd(NULL){
03620   int CheckNds=0; SIn.Load(CheckNds);
03621   for (int NdN=0; NdN<CheckNds; NdN++){AddBack(TVal(SIn));}
03622   Assert(Nds==CheckNds);
03623 }
03624 
03625 template <class TVal>
03626 void TLst<TVal>::Save(TSOut& SOut) const {
03627   SOut.Save(Nds);
03628   PLstNd Nd=FirstNd; int CheckNds=0;
03629   while (Nd!=NULL){
03630     Nd->Val.Save(SOut); Nd=Nd->NextNd; CheckNds++;}
03631   IAssert(Nds==CheckNds);
03632 }
03633 
03634 template <class TVal>
03635 TLstNd<TVal>* TLst<TVal>::AddFront(const TVal& Val){
03636   PLstNd Nd=new TLstNd<TVal>(NULL, FirstNd, Val);
03637   if (FirstNd!=NULL){FirstNd->PrevNd=Nd; FirstNd=Nd;}
03638   else {FirstNd=Nd; LastNd=Nd;}
03639   Nds++; return Nd;
03640 }
03641 
03642 template <class TVal>
03643 TLstNd<TVal>* TLst<TVal>::AddBack(const TVal& Val){
03644   PLstNd Nd=new TLstNd<TVal>(LastNd, NULL, Val);
03645   if (LastNd!=NULL){LastNd->NextNd=Nd; LastNd=Nd;}
03646   else {FirstNd=Nd; LastNd=Nd;}
03647   Nds++; return Nd;
03648 }
03649 
03650 template <class TVal>
03651 TLstNd<TVal>* TLst<TVal>::AddFrontSorted(const TVal& Val, const bool& Asc){
03652   PLstNd Nd=First();
03653   if (Nd==NULL){
03654     return Ins(Nd, Val);
03655   } else {
03656     while ((Nd!=NULL)&&((Asc&&(Val>Nd()))||(!Asc&&(Val<Nd())))){
03657       Nd=Nd->Next();}
03658     if (Nd==NULL){return Ins(Nd->Last(), Val);}
03659     else {return Ins(Nd->Prev(), Val);}
03660   }
03661 }
03662 
03663 template <class TVal>
03664 TLstNd<TVal>* TLst<TVal>::AddBackSorted(const TVal& Val, const bool& Asc){
03665   PLstNd Nd=Last();
03666   while ((Nd!=NULL)&&((Asc&&(Val<Nd->Val))||(!Asc&&(Val>Nd->Val)))){
03667     Nd=Nd->Prev();}
03668   return Ins(Nd, Val);
03669 }
03670 
03671 template <class TVal>
03672 void TLst<TVal>::PutFront(const PLstNd& Nd){
03673   Assert(Nd!=NULL);
03674   // unchain
03675   if (Nd->PrevNd==NULL){FirstNd=Nd->NextNd;}
03676   else {Nd->PrevNd->NextNd=Nd->NextNd;}
03677   if (Nd->NextNd==NULL){LastNd=Nd->PrevNd;}
03678   else {Nd->NextNd->PrevNd=Nd->PrevNd;}
03679   // add to front
03680   Nd->PrevNd=NULL; Nd->NextNd=FirstNd;
03681   if (FirstNd!=NULL){FirstNd->PrevNd=Nd; FirstNd=Nd;}
03682   else {FirstNd=Nd; LastNd=Nd;}
03683 }
03684 
03685 template <class TVal>
03686 void TLst<TVal>::PutBack(const PLstNd& Nd){
03687   Assert(Nd!=NULL);
03688   // unchain
03689   if (Nd->PrevNd==NULL){FirstNd=Nd->NextNd;}
03690   else {Nd->PrevNd->NextNd=Nd->NextNd;}
03691   if (Nd->NextNd==NULL){LastNd=Nd->PrevNd;}
03692   else {Nd->NextNd->PrevNd=Nd->PrevNd;}
03693   // add to back
03694   Nd->PrevNd=LastNd; Nd->NextNd=NULL;
03695   if (LastNd!=NULL){LastNd->NextNd=Nd; LastNd=Nd;}
03696   else {FirstNd=Nd; LastNd=Nd;}
03697 }
03698 
03699 template <class TVal>
03700 TLstNd<TVal>* TLst<TVal>::Ins(const PLstNd& Nd, const TVal& Val){
03701   if (Nd==NULL){return AddFront(Val);}
03702   else if (Nd->NextNd==NULL){return AddBack(Val);}
03703   else {
03704     PLstNd NewNd=new TLstNd<TVal>(Nd, Nd->NextNd, Val);
03705     Nd->NextNd=NewNd; NewNd->NextNd->PrevNd=Nd;
03706     Nds++; return Nd;
03707   }
03708 }
03709 
03710 template <class TVal>
03711 void TLst<TVal>::Del(const TVal& Val){
03712   PLstNd Nd=SearchForw(Val);
03713   if (Nd!=NULL){Del(Nd);}
03714 }
03715 
03716 template <class TVal>
03717 void TLst<TVal>::Del(const PLstNd& Nd){
03718   Assert(Nd!=NULL);
03719   if (Nd->PrevNd==NULL){FirstNd=Nd->NextNd;}
03720   else {Nd->PrevNd->NextNd=Nd->NextNd;}
03721   if (Nd->NextNd==NULL){LastNd=Nd->PrevNd;}
03722   else {Nd->NextNd->PrevNd=Nd->PrevNd;}
03723   Nds--; delete Nd;
03724 }
03725 
03726 template <class TVal>
03727 TLstNd<TVal>* TLst<TVal>::SearchForw(const TVal& Val){
03728   PLstNd Nd=First();
03729   while (Nd!=NULL){
03730     if (Nd->GetVal()==Val){return Nd;}
03731     Nd=Nd->Next();
03732   }
03733   return NULL;
03734 }
03735 
03736 template <class TVal>
03737 TLstNd<TVal>* TLst<TVal>::SearchBack(const TVal& Val){
03738   PLstNd Nd=Last();
03739   while (Nd!=NULL){
03740     if (Nd->GetVal()==Val){return Nd;}
03741     Nd=Nd->Prev();
03742   }
03743   return NULL;
03744 }
03745 
03747 // Common-List-Types
03748 typedef TLst<TInt> TIntL;
03749 typedef TLstNd<TInt>* PIntLN;
03750 typedef TLst<TIntKd> TIntKdL;
03751 typedef TLstNd<TIntKd>* PIntKdLN;
03752 typedef TLst<TFlt> TFltL;
03753 typedef TLstNd<TFlt>* PFltLN;
03754 typedef TLst<TFltIntKd> TFltIntKdL;
03755 typedef TLstNd<TFltIntKd>* PFltIntKdLN;
03756 typedef TLst<TAscFltIntKd> TAscFltIntKdL;
03757 typedef TLstNd<TAscFltIntKd>* PAscFltIntKdLN;
03758 typedef TLst<TStr> TStrL;
03759 typedef TLstNd<TStr>* PStrLN;
03760 
03762 // Record-File
03763 template <class THd, class TRec>
03764 class TFRec{
03765 private:
03766   PFRnd FRnd;
03767 public:
03768   TFRec(const TStr& FNm, const TFAccess& FAccess, const bool& CreateIfNo):
03769     FRnd(PFRnd(new TFRnd(FNm, FAccess, CreateIfNo, sizeof(THd), sizeof(TRec)))){}
03770   TFRec(const TFRec&);
03771 
03772   TFRec& operator=(const TFRec&);
03773 
03774   void SetRecN(const int& RecN){FRnd->SetRecN(RecN);}
03775   int GetRecN(){return FRnd->GetRecN();}
03776   int GetRecs(){return FRnd->GetRecs();}
03777 
03778   void GetHd(THd& Hd){FRnd->GetHd(&Hd);}
03779   void PutHd(const THd& Hd){FRnd->PutHd(&Hd);}
03780   void GetRec(TRec& Rec, const int& RecN=-1){FRnd->GetRec(&Rec, RecN);}
03781   void PutRec(const TRec& Rec, const int& RecN=-1){FRnd->PutRec(&Rec, RecN);}
03782 };
03783 
03785 // Function
03786 template <class TFuncPt>
03787 class TFunc{
03788 private:
03789   TFuncPt FuncPt;
03790 public:
03791   TFunc(): FuncPt(NULL){}
03792   TFunc(const TFunc& Func): FuncPt(Func.FuncPt){}
03793   TFunc(const TFuncPt& _FuncPt): FuncPt(_FuncPt){}
03794   TFunc(TSIn&){Fail;}
03795   void Save(TSOut&) const {Fail;}
03796 
03797   TFunc& operator=(const TFunc& Func){
03798     if (this!=&Func){FuncPt=Func.FuncPt;} return *this;}
03799   bool operator==(const TFunc& Func) const {
03800     return FuncPt==Func.FuncPt;}
03801   bool operator<(const TFunc&) const {
03802     Fail; return false;}
03803   TFuncPt operator()() const {return FuncPt;}
03804 };