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