SNAP Library , Developer Reference
2013-01-07 14:03:36
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 Val1.GetPrimHashCd()+Val2.GetPrimHashCd();} //J: terrible hash function! 00057 //int GetSecHashCd() const {return Val1.GetSecHashCd()+Val2.GetSecHashCd();} //J: terrible hash function! 00058 int GetPrimHashCd() const {return 12289*Val1.GetPrimHashCd() ^ Val2.GetPrimHashCd();} //J: multiply by prime and xor 00059 int GetSecHashCd() const {return Val1.GetSecHashCd() ^ 24593*Val2.GetSecHashCd();} //J: multiply by prime and xor 00060 00061 void GetVal(TVal1& _Val1, TVal2& _Val2) const {_Val1=Val1; _Val2=Val2;} 00062 TStr GetStr() const { 00063 return TStr("Pair(")+Val1.GetStr()+", "+Val2.GetStr()+")";} 00064 }; 00065 00066 template <class TVal1, class TVal2> 00067 void GetSwitchedPrV( 00068 const TVec<TPair<TVal1, TVal2> >& SrcPrV, 00069 TVec<TPair<TVal2, TVal1> >& DstPrV){ 00070 int Prs=SrcPrV.Len(); 00071 DstPrV.Gen(Prs, 0); 00072 for (int PrN=0; PrN<Prs; PrN++){ 00073 const TPair<TVal1, TVal2>& SrcPr=SrcPrV[PrN]; 00074 DstPrV.Add(TPair<TVal2, TVal1>(SrcPr.Val2, SrcPr.Val1)); 00075 } 00076 } 00077 00078 typedef TPair<TBool, TCh> TBoolChPr; 00079 typedef TPair<TBool, TFlt> TBoolFltPr; 00080 typedef TPair<TUCh, TInt> TUChIntPr; 00081 typedef TPair<TUCh, TUInt64> TUChUInt64Pr; 00082 typedef TPair<TUCh, TStr> TUChStrPr; 00083 typedef TPair<TInt, TBool> TIntBoolPr; 00084 typedef TPair<TInt, TCh> TIntChPr; 00085 typedef TPair<TInt, TInt> TIntPr; 00086 typedef TPair<TInt, TUInt64> TIntUInt64Pr; 00087 typedef TPair<TInt, TIntPr> TIntIntPrPr; 00088 typedef TPair<TInt, TVec<TInt> > TIntIntVPr; 00089 typedef TPair<TInt, TFlt> TIntFltPr; 00090 typedef TPair<TInt, TStr> TIntStrPr; 00091 typedef TPair<TInt, TStrV> TIntStrVPr; 00092 typedef TPair<TIntPr, TInt> TIntPrIntPr; 00093 typedef TPair<TUInt, TUInt> TUIntUIntPr; 00094 typedef TPair<TUInt, TInt> TUIntIntPr; 00095 typedef TPair<TUInt64, TInt> TUInt64IntPr; 00096 typedef TPair<TUInt64, TUInt64> TUInt64Pr; 00097 typedef TPair<TUInt64, TFlt> TUInt64FltPr; 00098 typedef TPair<TUInt64, TStr> TUInt64StrPr; 00099 typedef TPair<TFlt, TInt> TFltIntPr; 00100 typedef TPair<TFlt, TUInt64> TFltUInt64Pr; 00101 typedef TPair<TFlt, TFlt> TFltPr; 00102 typedef TPair<TFlt, TStr> TFltStrPr; 00103 typedef TPair<TAscFlt, TInt> TAscFltIntPr; 00104 typedef TPair<TAscFlt, TAscFlt> TAscFltPr; 00105 typedef TPair<TFlt, TStr> TFltStrPr; 00106 typedef TPair<TAscFlt, TStr> TAscFltStrPr; 00107 typedef TPair<TStr, TInt> TStrIntPr; 00108 typedef TPair<TStr, TFlt> TStrFltPr; 00109 typedef TPair<TStr, TStr> TStrPr; 00110 typedef TPair<TStr, TStrV> TStrStrVPr; 00111 typedef TPair<TStrV, TInt> TStrVIntPr; 00112 typedef TPair<TInt, TIntPr> TIntIntPrPr; 00113 typedef TPair<TInt, TStrPr> TIntStrPrPr; 00114 typedef TPair<TFlt, TStrPr> TFltStrPrPr; 00115 00116 // Pair comparator 00117 template <class TVal1, class TVal2> 00118 class TCmpPairByVal2 { 00119 private: 00120 bool IsAsc; 00121 public: 00122 TCmpPairByVal2(const bool& AscSort=true) : IsAsc(AscSort) { } 00123 bool operator () (const TPair<TVal1, TVal2>& P1, const TPair<TVal1, TVal2>& P2) const { 00124 if (IsAsc) { return P1.Val2 < P2.Val2; } else { return P2.Val2 < P1.Val2; } 00125 } 00126 }; 00127 00129 // Triple 00130 template <class TVal1, class TVal2, class TVal3> 00131 class TTriple{ 00132 public: 00133 TVal1 Val1; 00134 TVal2 Val2; 00135 TVal3 Val3; 00136 public: 00137 TTriple(): Val1(), Val2(), Val3(){} 00138 TTriple(const TTriple& Triple): 00139 Val1(Triple.Val1), Val2(Triple.Val2), Val3(Triple.Val3){} 00140 TTriple(const TVal1& _Val1, const TVal2& _Val2, const TVal3& _Val3): 00141 Val1(_Val1), Val2(_Val2), Val3(_Val3){} 00142 explicit TTriple(TSIn& SIn): Val1(SIn), Val2(SIn), Val3(SIn){} 00143 void Save(TSOut& SOut) const { 00144 Val1.Save(SOut); Val2.Save(SOut); Val3.Save(SOut);} 00145 void LoadXml(const PXmlTok& XmlTok, const TStr& Nm=""); 00146 void SaveXml(TSOut& SOut, const TStr& Nm) const; 00147 00148 TTriple& operator=(const TTriple& Triple){ 00149 if (this!=&Triple){Val1=Triple.Val1; Val2=Triple.Val2; Val3=Triple.Val3;} 00150 return *this;} 00151 bool operator==(const TTriple& Triple) const { 00152 return (Val1==Triple.Val1)&&(Val2==Triple.Val2)&&(Val3==Triple.Val3);} 00153 bool operator<(const TTriple& Triple) const { 00154 return (Val1<Triple.Val1)||((Val1==Triple.Val1)&&(Val2<Triple.Val2))|| 00155 ((Val1==Triple.Val1)&&(Val2==Triple.Val2)&&(Val3<Triple.Val3));} 00156 00157 int GetPrimHashCd() const { 00158 return Val1.GetPrimHashCd()+Val2.GetPrimHashCd()+Val3.GetPrimHashCd();} 00159 int GetSecHashCd() const { 00160 return Val1.GetSecHashCd()+Val2.GetSecHashCd()+Val3.GetSecHashCd();} 00161 //int GetPrimHashCd() const {return 193*Val1.GetPrimHashCd() ^ 24593 * Val2.GetPrimHashCd() ^ ; } //J: multiply by prime and xor 00162 //int GetSecHashCd() const {return Val1.GetSecHashCd() ^ 24593*Val2.GetSecHashCd();} //J: multiply by prime and xor 00163 int GetMemUsed() const {return Val1.GetMemUsed()+Val2.GetMemUsed()+Val3.GetMemUsed();} 00164 00165 void GetVal(TVal1& _Val1, TVal2& _Val2, TVal3& _Val3) const { 00166 _Val1=Val1; _Val2=Val2; _Val3=Val3;} 00167 }; 00168 00169 typedef TTriple<TCh, TCh, TCh> TChTr; 00170 typedef TTriple<TCh, TInt, TInt> TChIntIntTr; 00171 typedef TTriple<TUCh, TInt, TInt> TUChIntIntTr; 00172 typedef TTriple<TInt, TInt, TInt> TIntTr; 00173 typedef TTriple<TUInt64, TUInt64, TUInt64> TUInt64Tr; 00174 typedef TTriple<TInt, TStr, TInt> TIntStrIntTr; 00175 typedef TTriple<TInt, TInt, TStr> TIntIntStrTr; 00176 typedef TTriple<TInt, TInt, TFlt> TIntIntFltTr; 00177 typedef TTriple<TInt, TFlt, TInt> TIntFltIntTr; 00178 typedef TTriple<TInt, TFlt, TFlt> TIntFltFltTr; 00179 typedef TTriple<TInt, TVec<TInt>, TInt> TIntIntVIntTr; 00180 typedef TTriple<TInt, TInt, TVec<TInt> > TIntIntIntVTr; 00181 typedef TTriple<TFlt, TFlt, TFlt> TFltTr; 00182 typedef TTriple<TFlt, TInt, TInt> TFltIntIntTr; 00183 typedef TTriple<TFlt, TFlt, TInt> TFltFltIntTr; 00184 typedef TTriple<TFlt, TFlt, TStr> TFltFltStrTr; 00185 typedef TTriple<TChA, TChA, TChA> TChATr; 00186 typedef TTriple<TStr, TStr, TStr> TStrTr; 00187 typedef TTriple<TStr, TInt, TInt> TStrIntIntTr; 00188 typedef TTriple<TStr, TFlt, TFlt> TStrFltFltTr; 00189 typedef TTriple<TStr, TStr, TInt> TStrStrIntTr; 00190 typedef TTriple<TStr, TInt, TStrV> TStrIntStrVTr; 00191 00193 // Quad 00194 template <class TVal1, class TVal2, class TVal3, class TVal4> 00195 class TQuad{ 00196 public: 00197 TVal1 Val1; 00198 TVal2 Val2; 00199 TVal3 Val3; 00200 TVal4 Val4; 00201 public: 00202 TQuad(): 00203 Val1(), Val2(), Val3(), Val4(){} 00204 TQuad(const TQuad& Quad): 00205 Val1(Quad.Val1), Val2(Quad.Val2), Val3(Quad.Val3), Val4(Quad.Val4){} 00206 TQuad(const TVal1& _Val1, const TVal2& _Val2, const TVal3& _Val3, const TVal4& _Val4): 00207 Val1(_Val1), Val2(_Val2), Val3(_Val3), Val4(_Val4){} 00208 explicit TQuad(TSIn& SIn): 00209 Val1(SIn), Val2(SIn), Val3(SIn), Val4(SIn){} 00210 void Save(TSOut& SOut) const { 00211 Val1.Save(SOut); Val2.Save(SOut); Val3.Save(SOut); Val4.Save(SOut);} 00212 void LoadXml(const PXmlTok& XmlTok, const TStr& Nm=""); 00213 void SaveXml(TSOut& SOut, const TStr& Nm) const; 00214 00215 TQuad& operator=(const TQuad& Quad){ 00216 if (this!=&Quad){ 00217 Val1=Quad.Val1; Val2=Quad.Val2; Val3=Quad.Val3; Val4=Quad.Val4;} 00218 return *this;} 00219 bool operator==(const TQuad& Quad) const { 00220 return (Val1==Quad.Val1)&&(Val2==Quad.Val2)&&(Val3==Quad.Val3)&&(Val4==Quad.Val4);} 00221 bool operator<(const TQuad& Quad) const { 00222 return (Val1<Quad.Val1)||((Val1==Quad.Val1)&&(Val2<Quad.Val2))|| 00223 ((Val1==Quad.Val1)&&(Val2==Quad.Val2)&&(Val3<Quad.Val3))|| 00224 ((Val1==Quad.Val1)&&(Val2==Quad.Val2)&&(Val3==Quad.Val3)&&(Val4<Quad.Val4));} 00225 00226 int GetPrimHashCd() const { 00227 return Val1.GetPrimHashCd()+Val2.GetPrimHashCd()+Val3.GetPrimHashCd()+Val4.GetPrimHashCd();} 00228 int GetSecHashCd() const { 00229 return Val1.GetSecHashCd()+Val2.GetSecHashCd()+Val3.GetSecHashCd()+Val4.GetSecHashCd();} 00230 00231 void GetVal(TVal1& _Val1, TVal2& _Val2, TVal3& _Val3, TVal4& _Val4) const { 00232 _Val1=Val1; _Val2=Val2; _Val3=Val3; _Val4=Val4;} 00233 }; 00234 00235 typedef TQuad<TStr, TStr, TInt, TInt> TStrStrIntIntQu; 00236 typedef TQuad<TStr, TStr, TStr, TStr> TStrQu; 00237 typedef TQuad<TInt, TInt, TInt, TInt> TIntQu; 00238 typedef TQuad<TFlt, TFlt, TFlt, TFlt> TFltQu; 00239 typedef TQuad<TFlt, TInt, TInt, TInt> TFltIntIntIntQu; 00240 typedef TQuad<TInt, TStr, TInt, TInt> TIntStrIntIntQu; 00241 typedef TQuad<TInt, TInt, TFlt, TFlt> TIntIntFltFltQu; 00242 00244 // Tuple 00245 template<class TVal, int NVals> 00246 class TTuple { 00247 private: 00248 TVal ValV [NVals]; 00249 public: 00250 TTuple(){} 00251 TTuple(const TVal& InitVal) { for (int i=0; i<Len(); i++) ValV[i]=InitVal; } 00252 TTuple(const TTuple& Tup) { for (int i=0; i<Len(); i++) ValV[i]=Tup[i]; } 00253 TTuple(TSIn& SIn) { for (int i=0; i<Len(); i++) ValV[i].Load(SIn); } 00254 void Save(TSOut& SOut) const { for (int i=0; i<Len(); i++) ValV[i].Save(SOut); } 00255 void Load(TSIn& SIn) { for (int i=0; i<Len(); i++) ValV[i].Load(SIn); } 00256 00257 int Len() const { return NVals; } 00258 TVal& operator[] (const int& ValN) { return ValV[ValN]; } 00259 const TVal& operator[] (const int& ValN) const { return ValV[ValN]; } 00260 TTuple& operator = (const TTuple& Tup) { if (this != & Tup) { 00261 for (int i=0; i<Len(); i++) ValV[i]=Tup[i]; } return *this; } 00262 bool operator == (const TTuple& Tup) const { 00263 if (Len()!=Tup.Len()) { return false; } if (&Tup==this) { return true; } 00264 for (int i=0; i<Len(); i++) if(ValV[i]!=Tup[i]){return false;} return true; } 00265 bool operator < (const TTuple& Tup) const { 00266 if (Len() == Tup.Len()) { for (int i=0; i<Len(); i++) { 00267 if(ValV[i]<Tup[i]){return true;} else if(ValV[i]>Tup[i]){return false;} } return false; } 00268 else { return Len() < Tup.Len(); } } 00269 void Sort(const bool& Asc=true); 00270 int FindMx() const; 00271 int FindMn() const; 00272 //int GetPrimHashCd() const { int HashCd=0; 00273 // for (int i=0; i<Len(); i++) { HashCd += ValV[i].GetPrimHashCd(); } return HashCd; } 00274 //int GetSecHashCd() const { int HashCd=0; 00275 // for (int i=0; i<Len(); i++) { HashCd += ValV[i].GetSecHashCd(); } return HashCd; } 00276 int GetPrimHashCd() const { uint HashCd = 5381; 00277 for (int i=0; i<Len(); i++) { HashCd = ((HashCd << 13) + HashCd) + ValV[i].GetPrimHashCd(); } return int(HashCd&0x7fffffff); } 00278 int GetSecHashCd() const { uint HashCd = 5381; 00279 for (int i=0; i<Len(); i++) { HashCd = ((HashCd << 19) + HashCd) + ValV[i].GetSecHashCd(); } return int(HashCd&0x7fffffff); } 00280 TStr GetStr() const { TChA ValsStr; 00281 for (int i=0; i<Len(); i++) { ValsStr+=" "+ValV[i].GetStr(); } 00282 return TStr::Fmt("Tuple(%d):", Len())+ValsStr; } 00283 }; 00284 00285 template<class TVal, int NVals> 00286 void TTuple<TVal, NVals>::Sort(const bool& Asc) { 00287 TVec<TVal> V(NVals); 00288 for (int i=0; i<NVals; i++) { V.Add(ValV[i]); } 00289 V.Sort(Asc); 00290 for (int i=0; i<NVals; i++) { ValV[i] = V[i]; } 00291 } 00292 00293 template<class TVal, int NVals> 00294 int TTuple<TVal, NVals>::FindMx() const { 00295 TVal MxVal = ValV[0]; 00296 int ValN = 0; 00297 for (int i = 1; i < NVals; i++) { 00298 if (MxVal<ValV[i]) { 00299 MxVal=ValV[i]; ValN=i; 00300 } 00301 } 00302 return ValN; 00303 } 00304 00305 template<class TVal, int NVals> 00306 int TTuple<TVal, NVals>::FindMn() const { 00307 TVal MnVal = ValV[0]; 00308 int ValN = 0; 00309 for (int i = 1; i < NVals; i++) { 00310 if (MnVal>ValV[i]) { 00311 MnVal=ValV[i]; ValN=i; 00312 } 00313 } 00314 return ValN; 00315 } 00316 00318 // Key-Data 00319 template <class TKey, class TDat> 00320 class TKeyDat{ 00321 public: 00322 TKey Key; 00323 TDat Dat; 00324 public: 00325 TKeyDat(): Key(), Dat(){} 00326 TKeyDat(const TKeyDat& KeyDat): Key(KeyDat.Key), Dat(KeyDat.Dat){} 00327 explicit TKeyDat(const TKey& _Key): Key(_Key), Dat(){} 00328 TKeyDat(const TKey& _Key, const TDat& _Dat): Key(_Key), Dat(_Dat){} 00329 explicit TKeyDat(TSIn& SIn): Key(SIn), Dat(SIn){} 00330 void Save(TSOut& SOut) const {Key.Save(SOut); Dat.Save(SOut);} 00331 void LoadXml(const PXmlTok& XmlTok, const TStr& Nm=""); 00332 void SaveXml(TSOut& SOut, const TStr& Nm) const; 00333 00334 TKeyDat& operator=(const TKeyDat& KeyDat){ 00335 if (this!=&KeyDat){Key=KeyDat.Key; Dat=KeyDat.Dat;} return *this;} 00336 bool operator==(const TKeyDat& KeyDat) const {return Key==KeyDat.Key;} 00337 bool operator<(const TKeyDat& KeyDat) const {return Key<KeyDat.Key;} 00338 00339 int GetPrimHashCd() const {return Key.GetPrimHashCd();} 00340 int GetSecHashCd() const {return Key.GetSecHashCd();} 00341 }; 00342 00343 template <class TKey, class TDat> 00344 void GetSwitchedKdV( 00345 const TVec<TKeyDat<TKey, TDat> >& SrcKdV, 00346 TVec<TKeyDat<TDat, TKey> >& DstKdV){ 00347 int Kds=SrcKdV.Len(); 00348 DstKdV.Gen(Kds, 0); 00349 for (int KdN=0; KdN<Kds; KdN++){ 00350 const TKeyDat<TKey, TDat>& SrcKd=SrcKdV[KdN]; 00351 DstKdV.Add(TKeyDat<TDat, TKey>(SrcKd.Dat, SrcKd.Key)); 00352 } 00353 } 00354 00355 typedef TKeyDat<TInt, TInt> TIntKd; 00356 typedef TKeyDat<TInt, TUInt64> TIntUInt64Kd; 00357 typedef TKeyDat<TInt, TFlt> TIntFltKd; 00358 typedef TKeyDat<TIntPr, TFlt> TIntPrFltKd; 00359 typedef TKeyDat<TInt, TFltPr> TIntFltPrKd; 00360 typedef TKeyDat<TInt, TSFlt> TIntSFltKd; 00361 typedef TKeyDat<TInt, TStr> TIntStrKd; 00362 typedef TKeyDat<TUInt, TInt> TUIntIntKd; 00363 typedef TKeyDat<TUInt, TUInt> TUIntKd; 00364 typedef TKeyDat<TUInt64, TInt> TUInt64IntKd; 00365 typedef TKeyDat<TUInt64, TFlt> TUInt64FltKd; 00366 typedef TKeyDat<TUInt64, TStr> TUInt64StrKd; 00367 typedef TKeyDat<TFlt, TBool> TFltBoolKd; 00368 typedef TKeyDat<TFlt, TInt> TFltIntKd; 00369 typedef TKeyDat<TFlt, TUInt64> TFltUInt64Kd; 00370 typedef TKeyDat<TFlt, TIntPr> TFltIntPrKd; 00371 typedef TKeyDat<TFlt, TUInt> TFltUIntKd; 00372 typedef TKeyDat<TFlt, TFlt> TFltKd; 00373 typedef TKeyDat<TFlt, TStr> TFltStrKd; 00374 typedef TKeyDat<TFlt, TBool> TFltBoolKd; 00375 typedef TKeyDat<TFlt, TIntBoolPr> TFltIntBoolPrKd; 00376 typedef TKeyDat<TAscFlt, TInt> TAscFltIntKd; 00377 typedef TKeyDat<TStr, TBool> TStrBoolKd; 00378 typedef TKeyDat<TStr, TInt> TStrIntKd; 00379 typedef TKeyDat<TStr, TFlt> TStrFltKd; 00380 typedef TKeyDat<TStr, TAscFlt> TStrAscFltKd; 00381 typedef TKeyDat<TStr, TStr> TStrKd; 00382 00383 // Key-Data comparator 00384 00385 template <class TVal1, class TVal2> 00386 class TCmpKeyDatByDat { 00387 private: 00388 bool IsAsc; 00389 public: 00390 TCmpKeyDatByDat(const bool& AscSort=true) : IsAsc(AscSort) { } 00391 bool operator () (const TKeyDat<TVal1, TVal2>& P1, const TKeyDat<TVal1, TVal2>& P2) const { 00392 if (IsAsc) { return P1.Dat < P2.Dat; } else { return P2.Dat < P1.Dat; } 00393 } 00394 }; 00395 00397 // Vector 00398 template <class TVal> 00399 class TVec{ 00400 public: 00401 typedef TVal* TIter; 00402 protected: 00403 int MxVals; // if MxVals==-1, then ValT is not owned by us, we don't free it! 00404 int Vals; 00405 TVal* ValT; 00406 void Resize(const int& _MxVals=-1); 00407 TStr GetXOutOfBoundsErrMsg(const int& ValN) const; 00408 public: 00409 TVec(): MxVals(0), Vals(0), ValT(NULL){} 00410 TVec(const TVec& Vec); 00411 explicit TVec(const int& _Vals){ 00412 IAssert(0<=_Vals); MxVals=Vals=_Vals; 00413 if (_Vals==0){ValT=NULL;} else {ValT=new TVal[_Vals];}} 00414 TVec(const int& _MxVals, const int& _Vals){ 00415 IAssert((0<=_Vals)&&(_Vals<=_MxVals)); MxVals=_MxVals; Vals=_Vals; 00416 if (_MxVals==0){ValT=NULL;} else {ValT=new TVal[_MxVals];}} 00417 explicit TVec(TVal *_ValT, const int& _Vals): 00418 MxVals(-1), Vals(_Vals), ValT(_ValT){} 00419 ~TVec(){if ((ValT!=NULL) && (MxVals!=-1)){delete[] ValT;}} 00420 explicit TVec(TSIn& SIn): MxVals(0), Vals(0), ValT(NULL){Load(SIn);} 00421 void Load(TSIn& SIn); 00422 void Save(TSOut& SOut) const; 00423 void LoadXml(const PXmlTok& XmlTok, const TStr& Nm=""); 00424 void SaveXml(TSOut& SOut, const TStr& Nm) const; 00425 00426 TVec<TVal>& operator=(const TVec<TVal>& Vec); 00427 TVec<TVal>& operator+(const TVal& Val){Add(Val); return *this;} 00428 bool operator==(const TVec<TVal>& Vec) const; 00429 bool operator<(const TVec<TVal>& Vec) const; 00430 const TVal& operator[](const int& ValN) const { 00431 AssertR((0<=ValN)&&(ValN<Vals), GetXOutOfBoundsErrMsg(ValN)); 00432 return ValT[ValN];} 00433 TVal& operator[](const int& ValN){ 00434 AssertR((0<=ValN)&&(ValN<Vals), GetXOutOfBoundsErrMsg(ValN)); 00435 return ValT[ValN];} 00436 int GetMemUsed() const { 00437 return 2*sizeof(int)+sizeof(TVal*)+MxVals*sizeof(TVal);} 00438 00439 int GetPrimHashCd() const; 00440 int GetSecHashCd() const; 00441 00442 void Gen(const int& _Vals){ 00443 IAssert(0<=_Vals); 00444 if (ValT!=NULL && MxVals!=-1){delete[] ValT;} MxVals=Vals=_Vals; 00445 if (MxVals==0){ValT=NULL;} else {ValT=new TVal[MxVals];}} 00446 void Gen(const int& _MxVals, const int& _Vals){ 00447 IAssert((0<=_Vals)&&(_Vals<=_MxVals)); 00448 if (ValT!=NULL && MxVals!=-1){delete[] ValT;} MxVals=_MxVals; Vals=_Vals; 00449 if (_MxVals==0){ValT=NULL;} else {ValT=new TVal[_MxVals];}} 00450 void GenExt(TVal *_ValT, const int& _Vals){ 00451 if (ValT!=NULL && MxVals!=-1){delete[] ValT;} 00452 MxVals=-1; Vals=_Vals; ValT=_ValT;} 00453 bool IsExt() const {return MxVals==-1;} 00454 void Reserve(const int& _MxVals){Resize(_MxVals);} 00455 void Reserve(const int& _MxVals, const int& _Vals){ 00456 IAssert((0<=_Vals)&&(_Vals<=_MxVals)); 00457 Resize(_MxVals); Vals=_Vals;} 00458 void Clr(const bool& DoDel=true, const int& NoDelLim=-1); 00459 void Trunc(const int& _Vals=-1); 00460 void Pack(); 00461 void MoveFrom(TVec<TVal>& Vec); 00462 void Swap(TVec<TVal>& Vec); 00463 00464 bool Empty() const {return Vals==0;} 00465 int Len() const {return Vals;} 00466 int Reserved() const {return MxVals;} 00467 const TVal& Last() const {return GetVal(Len()-1);} 00468 TVal& Last(){return GetVal(Len()-1);} 00469 int LastValN() const {return Len()-1;} 00470 const TVal& LastLast() const { 00471 AssertR(1<Vals, GetXOutOfBoundsErrMsg(Vals-2)); 00472 return ValT[Vals-2];} 00473 TVal& LastLast(){ 00474 AssertR(1<Vals, GetXOutOfBoundsErrMsg(Vals-2)); 00475 return ValT[Vals-2];} 00476 00477 TIter BegI() const {return ValT;} 00478 TIter EndI() const {return ValT+Vals;} 00479 TIter GetI(const int& ValN) const {return ValT+ValN;} 00480 00481 int Add(){ 00482 Assert(MxVals!=-1); if (Vals==MxVals){Resize();} return Vals++;} 00483 int Add(const TVal& Val){ 00484 Assert(MxVals!=-1); if (Vals==MxVals){Resize();} ValT[Vals]=Val; return Vals++;} 00485 int Add(const TVal& Val, const int& ResizeLen){ 00486 Assert(MxVals!=-1); if (Vals==MxVals){Resize(MxVals+ResizeLen);} ValT[Vals]=Val; return Vals++;} 00487 int AddV(const TVec<TVal>& ValV); 00488 int AddSorted(const TVal& Val, const bool& Asc=true, const int& _MxVals=-1); 00489 int AddBackSorted(const TVal& Val, const bool& Asc); 00490 int AddMerged(const TVal& Val); 00491 int AddVMerged(const TVec<TVal>& ValV); 00492 int AddUnique(const TVal& Val); 00493 const TVal& GetVal(const int& ValN) const {return operator[](ValN);} 00494 TVal& GetVal(const int& ValN){return operator[](ValN);} 00495 void GetSubValV(const int& BValN, const int& EValN, TVec<TVal>& ValV) const; 00496 void Ins(const int& ValN, const TVal& Val); 00497 void Del(const int& ValN); 00498 void Del(const int& MnValN, const int& MxValN); 00499 void DelLast(){Del(Len()-1);} 00500 bool DelIfIn(const TVal& Val); 00501 void DelAll(const TVal& Val); 00502 void PutAll(const TVal& Val); 00503 00504 void Swap(const int& ValN1, const int& ValN2){ 00505 TVal Val=ValT[ValN1]; ValT[ValN1]=ValT[ValN2]; ValT[ValN2]=Val;} 00506 static void SwapI(TIter LVal, TIter RVal){ 00507 TVal Val=*LVal; *LVal=*RVal; *RVal=Val;} 00508 00509 int GetPivotValN(const int& LValN, const int& RValN) const; 00510 void BSort(const int& MnLValN, const int& MxRValN, const bool& Asc); 00511 void ISort(const int& MnLValN, const int& MxRValN, const bool& Asc); 00512 int Partition(const int& MnLValN, const int& MxRValN, const bool& Asc); 00513 void QSort(const int& MnLValN, const int& MxRValN, const bool& Asc); 00514 void Sort(const bool& Asc=true); 00515 bool IsSorted(const bool& Asc=true) const; 00516 void Shuffle(TRnd& Rnd); 00517 void Reverse(); 00518 void Reverse(int First, int Last){ 00519 Last--; while (First < Last){Swap(First++, Last--);}} 00520 void Merge(); 00521 // permutations 00522 bool NextPerm(); 00523 bool PrevPerm(); 00524 00525 // binary heap //J: 00526 void MakeHeap() { MakeHeap(TLss<TVal>()); } // build a heap 00527 void PushHeap(const TVal& Val) { PushHeap(Val, TLss<TVal>()); } // add element to the heap 00528 const TVal& TopHeap() const { return ValT[0]; } // get largest element 00529 TVal PopHeap() { return PopHeap(TLss<TVal>()); } // remove largest element 00530 template <class TCmp> void MakeHeap(const TCmp& Cmp) { MakeHeap(0, Len(), Cmp); } 00531 template <class TCmp> void PushHeap(const TVal& Val, const TCmp& Cmp) { Add(Val); PushHeap(0, Len()-1, 0, Val, Cmp); } 00532 template <class TCmp> TVal PopHeap(const TCmp& Cmp) { IAssert(! Empty()); const TVal Top=ValT[0]; 00533 ValT[0]=Last(); DelLast(); if (! Empty()) { AdjustHeap(0, 0, Len(), ValT[0], Cmp); } return Top; } 00534 // heap helper functions 00535 template <class TCmp> 00536 void PushHeap(const int& First, int HoleIdx, const int& Top, TVal Val, const TCmp& Cmp) { 00537 int Parent = (HoleIdx-1)/2; 00538 while (HoleIdx > Top && Cmp(ValT[First+Parent], Val)) { 00539 ValT[First+HoleIdx] = ValT[First+Parent]; 00540 HoleIdx = Parent; Parent = (HoleIdx-1)/2; } 00541 ValT[First+HoleIdx] = Val; 00542 } 00543 template <class TCmp> 00544 void AdjustHeap(const int& First, int HoleIdx, const int& Len, TVal Val, const TCmp& Cmp) { 00545 const int Top = HoleIdx; 00546 int Right = 2*HoleIdx+2; 00547 while (Right < Len) { 00548 if (Cmp(ValT[First+Right], ValT[First+Right-1])) { Right--; } 00549 ValT[First+HoleIdx] = ValT[First+Right]; 00550 HoleIdx = Right; Right = 2*(Right+1); } 00551 if (Right == Len) { 00552 ValT[First+HoleIdx] = ValT[First+Right-1]; 00553 HoleIdx = Right-1; } 00554 PushHeap(First, HoleIdx, Top, Val, Cmp); 00555 } 00556 template <class TCmp> 00557 void MakeHeap(const int& First, const int& Len, const TCmp& Cmp) { 00558 if (Len < 2) { return; } 00559 int Parent = (Len-2)/2; 00560 while (true) { 00561 AdjustHeap(First, Parent, Len, ValT[First+Parent], Cmp); 00562 if (Parent == 0) { return; } Parent--; } 00563 } 00564 00565 template <class TCmp> 00566 static TIter GetPivotValNCmp(const TIter& BI, const TIter& EI, const TCmp& Cmp) { 00567 const int SubVals=int(EI-BI); 00568 const int ValN1=TInt::GetRnd(SubVals); 00569 const int ValN2=TInt::GetRnd(SubVals); 00570 const int ValN3=TInt::GetRnd(SubVals); 00571 const TVal& Val1 = *(BI+ValN1); 00572 const TVal& Val2 = *(BI+ValN2); 00573 const TVal& Val3 = *(BI+ValN3); 00574 if (Cmp(Val1, Val2)) { 00575 if (Cmp(Val2, Val3)) return BI+ValN2; 00576 else if (Cmp(Val3, Val1)) return BI+ValN1; 00577 else return BI+ValN3; 00578 } else { 00579 if (Cmp(Val1, Val3)) return BI+ValN1; 00580 else if (Cmp(Val3, Val2)) return BI+ValN2; 00581 else return BI+ValN3; 00582 } 00583 } 00584 00585 template <class TCmp> 00586 static TIter PartitionCmp(TIter BI, TIter EI, const TVal Pivot, const TCmp& Cmp) { 00587 forever { 00588 while (Cmp(*BI, Pivot)) ++BI; 00589 --EI; 00590 while (Cmp(Pivot, *EI)) --EI; 00591 if (!(BI < EI)) return BI; 00592 SwapI(BI, EI); 00593 ++BI; 00594 } 00595 } 00596 00597 template <class TCmp> 00598 static void BSortCmp(TIter BI, TIter EI, const TCmp& Cmp) { 00599 for (TIter i = BI; i != EI; ++i) { 00600 for (TIter j = EI-1; j != i; --j) { 00601 if (Cmp(*j, *(j-1))) SwapI(j, j-1); } 00602 } 00603 } 00604 00605 template <class TCmp> 00606 static void ISortCmp(TIter BI, TIter EI, const TCmp& Cmp) { 00607 if (BI + 1 < EI) { 00608 for (TIter i = BI, j; i != EI; ++i) { 00609 TVal Tmp = *i; j = i; 00610 while (j > BI && Cmp(Tmp, *(j-1))) { *j = *(j-1); --j; } 00611 *j = Tmp; 00612 } 00613 } 00614 } 00615 00616 template <class TCmp> 00617 static void QSortCmp(TIter BI, TIter EI, const TCmp& Cmp) { 00618 if (BI + 1 < EI) { 00619 if (EI - BI < 20) { 00620 ISortCmp(BI, EI, Cmp); } 00621 else { 00622 const TVal Val = *GetPivotValNCmp(BI, EI, Cmp); 00623 TIter Split = PartitionCmp(BI, EI, Val, Cmp); 00624 QSortCmp(BI, Split, Cmp); 00625 QSortCmp(Split, EI, Cmp); 00626 } 00627 } 00628 } 00629 00630 // void Sort(const bool& Asc = true) { 00631 // if (Asc){QSortCmp(Beg(), End(), TLss<TVal>());} 00632 // else {QSortCmp(Beg(), End(), TGtr<TVal>());}} 00633 00634 template <class TCmp> 00635 void SortCmp(const TCmp& Cmp){ 00636 QSortCmp(BegI(), EndI(), Cmp);} 00637 00638 // bool IsSorted(const bool& Asc = true) const { 00639 // if (Asc){return IsSortedCmp(TLss<TVal>());} 00640 // else {return IsSortedCmp(TGtr<TVal>());}} 00641 00642 template <class TCmp> 00643 bool IsSortedCmp(const TCmp& Cmp) const { 00644 if (EndI() == BegI()) return true; 00645 for (TIter i = BegI(); i != EndI()-1; ++i) { 00646 if (Cmp(*(i+1), *i)){return false;}} 00647 return true; 00648 } 00649 00650 void Intrs(const TVec<TVal>& ValV); 00651 void Union(const TVec<TVal>& ValV); 00652 void Diff(const TVec<TVal>& ValV); // union without intersection 00653 void Minus(const TVec<TVal>& ValV); // this without intersection 00654 void Intrs(const TVec<TVal>& ValV, TVec<TVal>& DstValV) const; 00655 void Union(const TVec<TVal>& ValV, TVec<TVal>& DstValV) const; 00656 void Diff(const TVec<TVal>& ValV, TVec<TVal>& DstValV) const; 00658 int IntrsLen(const TVec<TVal>& ValV) const; 00659 void Minus(const TVec<TVal>& ValV, TVec<TVal>& DstValV) const; 00660 00661 int Count(const TVal& Val) const; 00662 int SearchBin(const TVal& Val) const; 00663 int SearchBin(const TVal& Val, int& InsValN) const; 00664 int SearchForw(const TVal& Val, const int& BValN=0) const; 00665 int SearchBack(const TVal& Val) const; 00666 int SearchVForw(const TVec<TVal>& ValV, const int& BValN=0) const; 00667 bool IsIn(const TVal& Val) const {return SearchForw(Val)!=-1;} 00668 bool IsIn(const TVal& Val, int& ValN) const { 00669 ValN=SearchForw(Val); return ValN!=-1;} 00670 bool IsInBin(const TVal& Val) const {return SearchBin(Val)!=-1;} 00671 int GetMxValN() const; 00672 TVal& GetDat(const TVal& Val) const { 00673 int ValN=SearchForw(Val); 00674 return operator[](ValN);} 00675 TVal& GetAddDat(const TVal& Val){ 00676 Assert(MxVals!=-1); 00677 int ValN=SearchForw(Val); 00678 if (ValN==-1){Add(Val); return Last();} 00679 else {return operator[](ValN);}} 00680 00681 // short vectors 00682 static TVec<TVal> GetV(const TVal& Val1){ 00683 TVec<TVal> V(1, 0); V.Add(Val1); return V;} 00684 static TVec<TVal> GetV(const TVal& Val1, const TVal& Val2){ 00685 TVec<TVal> V(2, 0); V.Add(Val1); V.Add(Val2); return V;} 00686 static TVec<TVal> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3){ 00687 TVec<TVal> V(3, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); return V;} 00688 static TVec<TVal> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4){ 00689 TVec<TVal> V(4, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); return V;} 00690 static TVec<TVal> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4, const TVal& Val5){ 00691 TVec<TVal> V(5, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); V.Add(Val5); return V;} 00692 static TVec<TVal> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4, const TVal& Val5, const TVal& Val6){ 00693 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;} 00694 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){ 00695 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;} 00696 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){ 00697 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;} 00698 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){ 00699 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;} 00700 //static TVec<TVal> GetV(const TVal* ValPt, const TVal& EndVal){ 00701 // TVec<TVal> V; while(*ValPt!=EndVal){V.Add(*ValPt); ValPt++;} return V;} 00702 }; 00703 00704 template <class TVal> 00705 void TVec<TVal>::Resize(const int& _MxVals){ 00706 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()); 00707 if (_MxVals==-1){ 00708 if (Vals==0){MxVals=16;} else {MxVals*=2;} 00709 } else { 00710 if (_MxVals<=MxVals){return;} else {MxVals=_MxVals;} 00711 } 00712 if (ValT==NULL){ 00713 try {ValT=new TVal[MxVals];} 00714 catch (std::exception Ex){ 00715 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.]", 00716 Ex.what(), Vals, MxVals, _MxVals, GetTypeNm(*this).CStr()).CStr());} 00717 } else { 00718 //if (Vals > 1000000) { 00719 // printf("%s resize %d -> %d\n", GetTypeNm(*this).CStr(), Vals, MxVals); } 00720 TVal* NewValT = NULL; 00721 try { 00722 NewValT=new TVal[MxVals];} 00723 catch (std::exception Ex){ 00724 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.]", 00725 Ex.what(), Vals, MxVals, _MxVals, GetTypeNm(*this).CStr()).CStr());} 00726 Assert(NewValT!=NULL); 00727 for (int ValN=0; ValN<Vals; ValN++){NewValT[ValN]=ValT[ValN];} 00728 delete[] ValT; ValT=NewValT; 00729 } 00730 } 00731 00732 template <class TVal> 00733 TStr TVec<TVal>::GetXOutOfBoundsErrMsg(const int& ValN) const { 00734 return TStr()+ 00735 "Index:"+TInt::GetStr(ValN)+ 00736 " Vals:"+TInt::GetStr(Vals)+ 00737 " MxVals:"+TInt::GetStr(MxVals)+ 00738 " Type:"+GetTypeNm(*this); 00739 } 00740 00741 template <class TVal> 00742 TVec<TVal>::TVec(const TVec<TVal>& Vec){ 00743 MxVals=Vec.MxVals; Vals=Vec.Vals; 00744 if (MxVals==0){ValT=NULL;} else {ValT=new TVal[MxVals];} 00745 for (int ValN=0; ValN<Vec.Vals; ValN++){ValT[ValN]=Vec.ValT[ValN];} 00746 } 00747 00748 template <class TVal> 00749 void TVec<TVal>::Load(TSIn& SIn){ 00750 if ((ValT!=NULL)&&(MxVals!=-1)){delete[] ValT;} 00751 SIn.Load(MxVals); SIn.Load(Vals); MxVals=Vals; 00752 if (MxVals==0){ValT=NULL;} else {ValT=new TVal[MxVals];} 00753 for (int ValN=0; ValN<Vals; ValN++){ 00754 ValT[ValN]=TVal(SIn);} 00755 } 00756 00757 template <class TVal> 00758 void TVec<TVal>::Save(TSOut& SOut) const { 00759 if (MxVals!=-1){SOut.Save(MxVals);} else {SOut.Save(Vals);} 00760 SOut.Save(Vals); 00761 for (int ValN=0; ValN<Vals; ValN++){ValT[ValN].Save(SOut);} 00762 } 00763 00764 template <class TVal> 00765 TVec<TVal>& TVec<TVal>::operator=(const TVec<TVal>& Vec){ 00766 if (this!=&Vec){ 00767 if ((ValT!=NULL)&&(MxVals!=-1)){delete[] ValT;} 00768 MxVals=Vals=Vec.Vals; 00769 if (MxVals==0){ValT=NULL;} else {ValT=new TVal[MxVals];} 00770 for (int ValN=0; ValN<Vec.Vals; ValN++){ValT[ValN]=Vec.ValT[ValN];} 00771 } 00772 return *this; 00773 } 00774 00775 template <class TVal> 00776 bool TVec<TVal>::operator==(const TVec<TVal>& Vec) const { 00777 if (this==&Vec){return true;} 00778 if (Len()!=Vec.Len()){return false;} 00779 for (int ValN=0; ValN<Vals; ValN++){ 00780 if (ValT[ValN]!=Vec.ValT[ValN]){return false;}} 00781 return true; 00782 } 00783 00784 template <class TVal> 00785 bool TVec<TVal>::operator<(const TVec<TVal>& Vec) const { 00786 if (this==&Vec){return false;} 00787 if (Len()==Vec.Len()){ 00788 for (int ValN=0; ValN<Vals; ValN++){ 00789 if (ValT[ValN]<Vec.ValT[ValN]){return true;} 00790 else if (ValT[ValN]>Vec.ValT[ValN]){return false;} 00791 else {} 00792 } 00793 return false; 00794 } else { 00795 return Len()<Vec.Len(); 00796 } 00797 } 00798 00799 template <class TVal> 00800 int TVec<TVal>::GetPrimHashCd() const { 00801 int HashCd=0; 00802 for (int ValN=0; ValN<Vals; ValN++){ 00803 HashCd+=ValT[ValN].GetPrimHashCd();} 00804 return abs(HashCd); 00805 } 00806 00807 template <class TVal> 00808 int TVec<TVal>::GetSecHashCd() const { 00809 int HashCd=0; 00810 for (int ValN=0; ValN<Vals; ValN++){ 00811 HashCd+=ValT[ValN].GetSecHashCd();} 00812 return abs(HashCd); 00813 } 00814 00815 template <class TVal> 00816 void TVec<TVal>::Clr(const bool& DoDel, const int& NoDelLim){ 00817 if ((DoDel)||((!DoDel)&&(NoDelLim!=-1)&&(MxVals>NoDelLim))){ 00818 if ((ValT!=NULL)&&(MxVals!=-1)){delete[] ValT;} 00819 MxVals=Vals=0; ValT=NULL; 00820 } else { 00821 IAssert(MxVals!=-1); Vals=0; 00822 } 00823 } 00824 00825 template <class TVal> 00826 void TVec<TVal>::Trunc(const int& _Vals){ 00827 IAssert(MxVals!=-1); 00828 IAssert((_Vals==-1)||(_Vals>=0)); 00829 if ((_Vals!=-1)&&(_Vals>=Vals)){ 00830 return; 00831 } else 00832 if (((_Vals==-1)&&(Vals==0))||(_Vals==0)){ 00833 if (ValT!=NULL){delete[] ValT;} 00834 MxVals=Vals=0; ValT=NULL; 00835 } else { 00836 if (_Vals==-1){ 00837 if (MxVals==Vals){return;} else {MxVals=Vals;} 00838 } else { 00839 MxVals=Vals=_Vals; 00840 } 00841 TVal* NewValT=new TVal[MxVals]; 00842 IAssert(NewValT!=NULL); 00843 for (int ValN=0; ValN<Vals; ValN++){NewValT[ValN]=ValT[ValN];} 00844 delete[] ValT; ValT=NewValT; 00845 } 00846 } 00847 00848 template <class TVal> 00849 void TVec<TVal>::Pack(){ 00850 IAssert(MxVals!=-1); 00851 if (Vals==0){ 00852 if (ValT!=NULL){delete[] ValT;} ValT=NULL; 00853 } else 00854 if (Vals<MxVals){ 00855 MxVals=Vals; 00856 TVal* NewValT=new TVal[MxVals]; 00857 IAssert(NewValT!=NULL); 00858 for (int ValN=0; ValN<Vals; ValN++){NewValT[ValN]=ValT[ValN];} 00859 delete[] ValT; ValT=NewValT; 00860 } 00861 } 00862 00863 template <class TVal> 00864 void TVec<TVal>::MoveFrom(TVec<TVal>& Vec){ 00865 if (this!=&Vec){ 00866 if (ValT!=NULL && MxVals!=-1){delete[] ValT;} 00867 MxVals=Vec.MxVals; Vals=Vec.Vals; ValT=Vec.ValT; 00868 Vec.MxVals=0; Vec.Vals=0; Vec.ValT=NULL; 00869 } 00870 } 00871 00872 template <class TVal> 00873 void TVec<TVal>::Swap(TVec<TVal>& Vec){ 00874 if (this!=&Vec){ 00875 ::Swap(MxVals, Vec.MxVals); 00876 ::Swap(Vals, Vec.Vals); 00877 ::Swap(ValT, Vec.ValT); 00878 } 00879 } 00880 00881 template <class TVal> 00882 int TVec<TVal>::AddV(const TVec<TVal>& ValV){ 00883 Assert(MxVals!=-1); 00884 for (int ValN=0; ValN<ValV.Vals; ValN++){Add(ValV[ValN]);} 00885 return Len(); 00886 } 00887 00888 template <class TVal> 00889 int TVec<TVal>::AddSorted(const TVal& Val, const bool& Asc, const int& _MxVals){ 00890 Assert(MxVals!=-1); 00891 int ValN=Add(Val); 00892 if (Asc){ 00893 while ((ValN>0)&&(ValT[ValN]<ValT[ValN-1])){ 00894 Swap(ValN, ValN-1); ValN--;} 00895 } else { 00896 while ((ValN>0)&&(ValT[ValN]>ValT[ValN-1])){ 00897 Swap(ValN, ValN-1); ValN--;} 00898 } 00899 if ((_MxVals!=-1)&&(Len()>_MxVals)){Del(_MxVals, Len()-1);} 00900 return ValN; 00901 } 00902 00903 template <class TVal> 00904 int TVec<TVal>::AddBackSorted(const TVal& Val, const bool& Asc){ 00905 Assert(MxVals!=-1); 00906 Add(); 00907 int ValN=Vals-2; 00908 while ((ValN>=0)&&((Asc&&(Val<ValT[ValN]))||(!Asc&&(Val>ValT[ValN])))){ 00909 ValT[ValN+1]=ValT[ValN]; ValN--;} 00910 ValT[ValN+1]=Val; 00911 return ValN+1; 00912 } 00913 00914 template <class TVal> 00915 int TVec<TVal>::AddMerged(const TVal& Val){ 00916 Assert(MxVals!=-1); 00917 int ValN=SearchBin(Val); 00918 if (ValN==-1){return AddSorted(Val);} 00919 else {GetVal(ValN)=Val; return -1;} 00920 } 00921 00922 template <class TVal> 00923 int TVec<TVal>::AddVMerged(const TVec<TVal>& ValV){ 00924 Assert(MxVals!=-1); 00925 for (int ValN=0; ValN<ValV.Vals; ValN++){AddMerged(ValV[ValN]);} 00926 return Len(); 00927 } 00928 00929 template <class TVal> 00930 int TVec<TVal>::AddUnique(const TVal& Val){ 00931 Assert(MxVals!=-1); 00932 int ValN=SearchForw(Val); 00933 if (ValN==-1){return Add(Val);} 00934 else {GetVal(ValN)=Val; return -1;} 00935 } 00936 00937 template <class TVal> 00938 void TVec<TVal>::GetSubValV( 00939 const int& _BValN, const int& _EValN, TVec<TVal>& SubValV) const { 00940 int BValN=TInt::GetInRng(_BValN, 0, Len()-1); 00941 int EValN=TInt::GetInRng(_EValN, 0, Len()-1); 00942 int SubVals=TInt::GetMx(0, EValN-BValN+1); 00943 SubValV.Gen(SubVals, 0); 00944 for (int ValN=BValN; ValN<=EValN; ValN++){ 00945 SubValV.Add(GetVal(ValN));} 00946 } 00947 00948 template <class TVal> 00949 void TVec<TVal>::Ins(const int& ValN, const TVal& Val){ 00950 Assert(MxVals!=-1); 00951 Add(); Assert((0<=ValN)&&(ValN<Vals)); 00952 for (int MValN=Vals-2; MValN>=ValN; MValN--){ValT[MValN+1]=ValT[MValN];} 00953 ValT[ValN]=Val; 00954 } 00955 00956 template <class TVal> 00957 void TVec<TVal>::Del(const int& ValN){ 00958 Assert(MxVals!=-1); 00959 Assert((0<=ValN)&&(ValN<Vals)); 00960 for (int MValN=ValN+1; MValN<Vals; MValN++){ 00961 ValT[MValN-1]=ValT[MValN];} 00962 ValT[--Vals]=TVal(); 00963 } 00964 00965 template <class TVal> 00966 void TVec<TVal>::Del(const int& MnValN, const int& MxValN){ 00967 Assert(MxVals!=-1); 00968 Assert((0<=MnValN)&&(MnValN<Vals)&&(0<=MxValN)&&(MxValN<Vals)); 00969 Assert(MnValN<=MxValN); 00970 for (int ValN=MxValN+1; ValN<Vals; ValN++){ 00971 ValT[MnValN+ValN-MxValN-1]=ValT[ValN];} 00972 for (int ValN=Vals-MxValN+MnValN-1; ValN<Vals; ValN++){ 00973 ValT[ValN]=TVal();} 00974 Vals-=MxValN-MnValN+1; 00975 } 00976 00977 template <class TVal> 00978 bool TVec<TVal>::DelIfIn(const TVal& Val){ 00979 Assert(MxVals!=-1); 00980 int ValN=SearchForw(Val); 00981 if (ValN!=-1){Del(ValN); return true;} 00982 else {return false;} 00983 } 00984 00985 template <class TVal> 00986 void TVec<TVal>::DelAll(const TVal& Val){ 00987 Assert(MxVals!=-1); 00988 int ValN; 00989 while ((ValN=SearchForw(Val))!=-1){ 00990 Del(ValN);} 00991 } 00992 00993 template <class TVal> 00994 void TVec<TVal>::PutAll(const TVal& Val){ 00995 for (int ValN=0; ValN<Vals; ValN++){ValT[ValN]=Val;} 00996 } 00997 00998 template <class TVal> 00999 void TVec<TVal>::BSort( 01000 const int& MnLValN, const int& MxRValN, const bool& Asc){ 01001 for (int ValN1=MnLValN; ValN1<=MxRValN; ValN1++){ 01002 for (int ValN2=MxRValN; ValN2>ValN1; ValN2--){ 01003 if (Asc){ 01004 if (ValT[ValN2]<ValT[ValN2-1]){Swap(ValN2, ValN2-1);} 01005 } else { 01006 if (ValT[ValN2]>ValT[ValN2-1]){Swap(ValN2, ValN2-1);} 01007 } 01008 } 01009 } 01010 } 01011 01012 template <class TVal> 01013 void TVec<TVal>::ISort( 01014 const int& MnLValN, const int& MxRValN, const bool& Asc){ 01015 if (MnLValN<MxRValN){ 01016 for (int ValN1=MnLValN+1; ValN1<=MxRValN; ValN1++){ 01017 TVal Val=ValT[ValN1]; int ValN2=ValN1; 01018 if (Asc){ 01019 while ((ValN2>MnLValN)&&(ValT[ValN2-1]>Val)){ 01020 ValT[ValN2]=ValT[ValN2-1]; ValN2--;} 01021 } else { 01022 while ((ValN2>MnLValN)&&(ValT[ValN2-1]<Val)){ 01023 ValT[ValN2]=ValT[ValN2-1]; ValN2--;} 01024 } 01025 ValT[ValN2]=Val; 01026 } 01027 } 01028 } 01029 01030 template <class TVal> 01031 int TVec<TVal>::GetPivotValN(const int& LValN, const int& RValN) const { 01032 int SubVals=RValN-LValN+1; 01033 int ValN1=LValN+TInt::GetRnd(SubVals); 01034 int ValN2=LValN+TInt::GetRnd(SubVals); 01035 int ValN3=LValN+TInt::GetRnd(SubVals); 01036 const TVal& Val1=ValT[ValN1]; 01037 const TVal& Val2=ValT[ValN2]; 01038 const TVal& Val3=ValT[ValN3]; 01039 if (Val1<Val2){ 01040 if (Val2<Val3){return ValN2;} 01041 else if (Val3<Val1){return ValN1;} 01042 else {return ValN3;} 01043 } else { 01044 if (Val1<Val3){return ValN1;} 01045 else if (Val3<Val2){return ValN2;} 01046 else {return ValN3;} 01047 } 01048 } 01049 01050 template <class TVal> 01051 int TVec<TVal>::Partition( 01052 const int& MnLValN, const int& MxRValN, const bool& Asc){ 01053 int PivotValN=GetPivotValN(MnLValN, MxRValN); 01054 Swap(PivotValN, MnLValN); 01055 TVal PivotVal=ValT[MnLValN]; 01056 int LValN=MnLValN-1; int RValN=MxRValN+1; 01057 forever { 01058 if (Asc){ 01059 do {RValN--;} while (ValT[RValN]>PivotVal); 01060 do {LValN++;} while (ValT[LValN]<PivotVal); 01061 } else { 01062 do {RValN--;} while (ValT[RValN]<PivotVal); 01063 do {LValN++;} while (ValT[LValN]>PivotVal); 01064 } 01065 if (LValN<RValN){Swap(LValN, RValN);} 01066 else {return RValN;} 01067 }; 01068 } 01069 01070 template <class TVal> 01071 void TVec<TVal>::QSort( 01072 const int& MnLValN, const int& MxRValN, const bool& Asc){ 01073 if (MnLValN<MxRValN){ 01074 if (MxRValN-MnLValN<20){ 01075 ISort(MnLValN, MxRValN, Asc); 01076 } else { 01077 int SplitValN=Partition(MnLValN, MxRValN, Asc); 01078 QSort(MnLValN, SplitValN, Asc); 01079 QSort(SplitValN+1, MxRValN, Asc); 01080 } 01081 } 01082 } 01083 01084 template <class TVal> 01085 void TVec<TVal>::Sort(const bool& Asc){ 01086 QSort(0, Len()-1, Asc); 01087 } 01088 01089 template <class TVal> 01090 bool TVec<TVal>::IsSorted(const bool& Asc) const { 01091 if (Asc){ 01092 for (int ValN=0; ValN<Vals-1; ValN++){ 01093 if (ValT[ValN]>ValT[ValN+1]){return false;}} 01094 } else { 01095 for (int ValN=0; ValN<Vals-1; ValN++){ 01096 if (ValT[ValN]<ValT[ValN+1]){return false;}} 01097 } 01098 return true; 01099 } 01100 01101 template <class TVal> 01102 void TVec<TVal>::Shuffle(TRnd& Rnd){ 01103 for (int ValN=0; ValN<Vals-1; ValN++){ 01104 Swap(ValN, ValN+Rnd.GetUniDevInt(Vals-ValN));} 01105 } 01106 01107 template <class TVal> 01108 void TVec<TVal>::Reverse(){ 01109 for (int ValN=0; ValN<Vals/2; ValN++){ 01110 Swap(ValN, Vals-ValN-1);} 01111 } 01112 01113 template <class TVal> 01114 void TVec<TVal>::Merge(){ 01115 Assert(MxVals!=-1); 01116 TVec<TVal> SortedVec(*this); SortedVec.Sort(); 01117 Clr(); 01118 for (int ValN=0; ValN<SortedVec.Len(); ValN++){ 01119 if ((ValN==0)||(SortedVec[ValN-1]!=SortedVec[ValN])){ 01120 Add(SortedVec[ValN]);} 01121 } 01122 } 01123 01124 template <class TVal> 01125 bool TVec<TVal>::NextPerm() { 01126 // start with a sorted sequence to obtain all permutations 01127 int First = 0, Last = Len(), Next = Len()-1; 01128 if (Last < 2) return false; 01129 for(; ; ) { 01130 // find rightmost element smaller than successor 01131 int Next1 = Next; 01132 if (GetVal(--Next) < GetVal(Next1)) { // swap with rightmost element that's smaller, flip suffix 01133 int Mid = Last; 01134 for (; GetVal(Next) >= GetVal(--Mid); ) { } 01135 Swap(Next, Mid); 01136 Reverse(Next1, Last); 01137 return true; 01138 } 01139 if (Next == First) { // pure descending, flip all 01140 Reverse(); 01141 return false; 01142 } 01143 } 01144 } 01145 01146 template <class TVal> 01147 bool TVec<TVal>::PrevPerm() { 01148 int First = 0, Last = Len(), Next = Len()-1; 01149 if (Last < 2) return false; 01150 for(; ; ) { 01151 // find rightmost element not smaller than successor 01152 int Next1 = Next; 01153 if (GetVal(--Next) >= GetVal(Next1)) { // swap with rightmost element that's not smaller, flip suffix 01154 int Mid = Last; 01155 for (; GetVal(Next) < GetVal(--Mid); ) { } 01156 Swap(Next, Mid); 01157 Reverse(Next1, Last); 01158 return true; 01159 } 01160 if (Next == First) { // pure descending, flip all 01161 Reverse(); 01162 return false; 01163 } 01164 } 01165 } 01166 01167 template <class TVal> 01168 void TVec<TVal>::Intrs(const TVec<TVal>& ValV){ 01169 TVec<TVal> IntrsVec; 01170 Intrs(ValV, IntrsVec); 01171 MoveFrom(IntrsVec); 01172 } 01173 01174 template <class TVal> 01175 void TVec<TVal>::Union(const TVec<TVal>& ValV){ 01176 TVec<TVal> UnionVec; 01177 Union(ValV, UnionVec); 01178 MoveFrom(UnionVec); 01179 } 01180 01181 template <class TVal> 01182 void TVec<TVal>::Diff(const TVec<TVal>& ValV){ 01183 TVec<TVal> DiffVec; 01184 Diff(ValV, DiffVec); 01185 MoveFrom(DiffVec); 01186 } 01187 01188 template <class TVal> 01189 void TVec<TVal>::Minus(const TVec<TVal>& ValV){ 01190 TVec<TVal> MinusVec; 01191 Minus(ValV, MinusVec); 01192 MoveFrom(MinusVec); 01193 } 01194 01195 template <class TVal> 01196 void TVec<TVal>::Intrs(const TVec<TVal>& ValV, TVec<TVal>& DstValV) const { 01197 DstValV.Clr(); 01198 int ValN1=0; int ValN2=0; 01199 while ((ValN1<Len())&&(ValN2<ValV.Len())){ 01200 const TVal& Val1=GetVal(ValN1); 01201 while ((ValN2<ValV.Len())&&(Val1>ValV.GetVal(ValN2))){ 01202 ValN2++;} 01203 if ((ValN2<ValV.Len())&&(Val1==ValV.GetVal(ValN2))){ 01204 DstValV.Add(Val1); ValN2++;} 01205 ValN1++; 01206 } 01207 } 01208 01209 template <class TVal> 01210 void TVec<TVal>::Union(const TVec<TVal>& ValV, TVec<TVal>& DstValV) const { 01211 DstValV.Gen(TInt::GetMx(Len(), ValV.Len()), 0); 01212 int ValN1=0; int ValN2=0; 01213 while ((ValN1<Len())&&(ValN2<ValV.Len())){ 01214 const TVal& Val1=GetVal(ValN1); 01215 const TVal& Val2=ValV.GetVal(ValN2); 01216 if (Val1<Val2){DstValV.Add(Val1); ValN1++;} 01217 else if (Val1>Val2){DstValV.Add(Val2); ValN2++;} 01218 else {DstValV.Add(Val1); ValN1++; ValN2++;} 01219 } 01220 for (int RestValN1=ValN1; RestValN1<Len(); RestValN1++){ 01221 DstValV.Add(GetVal(RestValN1));} 01222 for (int RestValN2=ValN2; RestValN2<ValV.Len(); RestValN2++){ 01223 DstValV.Add(ValV.GetVal(RestValN2));} 01224 } 01225 01226 /* 01227 template <class TVal> 01228 void TVec<TVal>::Union(const TVec<TVal>& ValV, TVec<TVal>& DstValV){ 01229 DstValV.Clr(); 01230 int ValN1=0; int ValN2=0; 01231 while ((ValN1<Len())&&(ValN2<ValV.Len())){ 01232 const TVal& Val1=GetVal(ValN1); 01233 const TVal& Val2=ValV.GetVal(ValN2); 01234 if (DstValV.Len()==0){ 01235 if (Val1<Val2){DstValV.Add(Val1);} else {DstValV.Add(Val2);} 01236 } else { 01237 if (Val1<Val2){ 01238 if (DstValV.Last()<Val1){DstValV.Add(Val1);} ValN1++; 01239 } else { 01240 if (DstValV.Last()<Val2){DstValV.Add(Val2);} ValN2++; 01241 } 01242 } 01243 } 01244 for (int RestValN1=ValN1; RestValN1<Len(); RestValN1++){ 01245 const TVal& Val1=GetVal(RestValN1); 01246 if (DstValV.Len()==0){DstValV.Add(Val1);} 01247 else {if (DstValV.Last()<Val1){DstValV.Add(Val1);}} 01248 } 01249 for (int RestValN2=ValN2; RestValN2<ValV.Len(); RestValN2++){ 01250 const TVal& Val2=ValV.GetVal(RestValN2); 01251 if (DstValV.Len()==0){DstValV.Add(Val2);} 01252 else {if (DstValV.Last()<Val2){DstValV.Add(Val2);}} 01253 } 01254 } 01255 */ 01256 01257 template <class TVal> 01258 void TVec<TVal>::Diff(const TVec<TVal>& ValV, TVec<TVal>& DstValV) const { 01259 DstValV.Clr(); 01260 int ValN1=0; int ValN2=0; 01261 while (ValN1<Len() && ValN2<ValV.Len()) { 01262 const TVal& Val1 = GetVal(ValN1); 01263 while (ValN2<ValV.Len() && Val1>ValV.GetVal(ValN2)) ValN2++; 01264 if (ValN2<ValV.Len()) { 01265 if (Val1!=ValV.GetVal(ValN2)) { DstValV.Add(Val1); } 01266 ValN1++; 01267 } 01268 } 01269 for (int RestValN1=ValN1; RestValN1<Len(); RestValN1++){ 01270 DstValV.Add(GetVal(RestValN1));} 01271 } 01272 01273 template <class TVal> 01274 int TVec<TVal>::IntrsLen(const TVec<TVal>& ValV) const { 01275 int Cnt=0, ValN1=0; int ValN2=0; 01276 while ((ValN1<Len())&&(ValN2<ValV.Len())){ 01277 const TVal& Val1=GetVal(ValN1); 01278 while ((ValN2<ValV.Len())&&(Val1>ValV.GetVal(ValN2))){ 01279 ValN2++;} 01280 if ((ValN2<ValV.Len())&&(Val1==ValV.GetVal(ValN2))){ 01281 ValN2++; Cnt++;} 01282 ValN1++; 01283 } 01284 return Cnt; 01285 } 01286 01287 template <class TVal> 01288 void TVec<TVal>::Minus(const TVec<TVal>& ValV, TVec<TVal>& DstValV) const { 01289 Diff(ValV, DstValV); 01290 } 01291 01292 template <class TVal> 01293 int TVec<TVal>::Count(const TVal& Val) const { 01294 int Count = 0; 01295 for (int i = 0; i < Len(); i++){ 01296 if (Val == ValT[i]){Count++;}} 01297 return Count; 01298 } 01299 01300 template <class TVal> 01301 int TVec<TVal>::SearchBin(const TVal& Val) const { 01302 int LValN=0; int RValN=Len()-1; 01303 while (RValN>=LValN){ 01304 int ValN=(LValN+RValN)/2; 01305 if (Val==ValT[ValN]){return ValN;} 01306 if (Val<ValT[ValN]){RValN=ValN-1;} else {LValN=ValN+1;} 01307 } 01308 return -1; 01309 } 01310 01311 template <class TVal> 01312 int TVec<TVal>::SearchBin(const TVal& Val, int& InsValN) const { 01313 int LValN=0; int RValN=Len()-1; 01314 while (RValN>=LValN){ 01315 int ValN=(LValN+RValN)/2; 01316 if (Val==ValT[ValN]){InsValN=ValN; return ValN;} 01317 if (Val<ValT[ValN]){RValN=ValN-1;} else {LValN=ValN+1;} 01318 } 01319 InsValN=LValN; return -1; 01320 } 01321 01322 template <class TVal> 01323 int TVec<TVal>::SearchForw(const TVal& Val, const int& BValN) const { 01324 for (int ValN=BValN; ValN<Vals; ValN++){ 01325 if (Val==ValT[ValN]){return ValN;}} 01326 return -1; 01327 } 01328 01329 template <class TVal> 01330 int TVec<TVal>::SearchBack(const TVal& Val) const { 01331 for (int ValN=Vals-1; ValN>=0; ValN--){ 01332 if (Val==ValT[ValN]){return ValN;}} 01333 return -1; 01334 } 01335 01336 template <class TVal> 01337 int TVec<TVal>::SearchVForw(const TVec<TVal>& ValV, const int& BValN) const { 01338 int ValVLen=ValV.Len(); 01339 for (int ValN=BValN; ValN<Vals-ValVLen+1; ValN++){ 01340 bool Found=true; 01341 for (int SubValN=0; SubValN<ValVLen; SubValN++){ 01342 if (ValV[SubValN]!=GetVal(ValN+SubValN)){Found=false; break;} 01343 } 01344 if (Found){return ValN;} 01345 } 01346 return -1; 01347 } 01348 01349 template <class TVal> 01350 int TVec<TVal>::GetMxValN() const { 01351 if (Vals==0){return -1;} 01352 int MxValN=0; 01353 for (int ValN=1; ValN<Vals; ValN++){ 01354 if (ValT[ValN]>ValT[MxValN]){MxValN=ValN;} 01355 } 01356 return MxValN; 01357 } 01358 01360 // Common-Vector-Types 01361 typedef TVec<TBool> TBoolV; 01362 typedef TVec<TCh> TChV; 01363 typedef TVec<TUCh> TUChV; 01364 typedef TVec<TUInt> TUIntV; 01365 typedef TVec<TInt> TIntV; 01366 typedef TVec<TUInt64> TUInt64V; 01367 typedef TVec<TFlt> TFltV; 01368 typedef TVec<TSFlt> TSFltV; 01369 typedef TVec<TAscFlt> TAscFltV; 01370 typedef TVec<TStr> TStrV; 01371 typedef TVec<TChA> TChAV; 01372 typedef TVec<TIntPr> TIntPrV; 01373 typedef TVec<TIntTr> TIntTrV; 01374 typedef TVec<TIntQu> TIntQuV; 01375 typedef TVec<TFltPr> TFltPrV; 01376 typedef TVec<TFltTr> TFltTrV; 01377 typedef TVec<TIntKd> TIntKdV; 01378 typedef TVec<TUChIntPr> TUChIntPrV; 01379 typedef TVec<TUChUInt64Pr> TUChUInt64PrV; 01380 typedef TVec<TIntUInt64Pr> TIntUInt64PrV; 01381 typedef TVec<TIntUInt64Kd> TIntUInt64KdV; 01382 typedef TVec<TIntFltPr> TIntFltPrV; 01383 typedef TVec<TIntFltPrKd> TIntFltPrKdV; 01384 typedef TVec<TFltIntPr> TFltIntPrV; 01385 typedef TVec<TFltUInt64Pr> TFltUInt64PrV; 01386 typedef TVec<TFltStrPr> TFltStrPrV; 01387 typedef TVec<TAscFltStrPr> TAscFltStrPrV; 01388 typedef TVec<TIntStrPr> TIntStrPrV; 01389 typedef TVec<TIntIntStrTr> TIntIntStrTrV; 01390 typedef TVec<TIntIntFltTr> TIntIntFltTrV; 01391 typedef TVec<TIntFltIntTr> TIntFltIntTrV; 01392 typedef TVec<TIntStrIntTr> TIntStrIntTrV; 01393 typedef TVec<TIntKd> TIntKdV; 01394 typedef TVec<TUIntIntKd> TUIntIntKdV; 01395 typedef TVec<TIntFltKd> TIntFltKdV; 01396 typedef TVec<TIntPrFltKd> TIntPrFltKdV; 01397 typedef TVec<TIntStrKd> TIntStrKdV; 01398 typedef TVec<TIntStrPrPr> TIntStrPrPrV; 01399 typedef TVec<TIntStrVPr> TIntStrVPrV; 01400 typedef TVec<TIntIntVIntTr> TIntIntVIntTrV; 01401 typedef TVec<TIntIntIntVTr> TIntIntIntVTrV; 01402 typedef TVec<TUInt64IntPr> TUInt64IntPrV; 01403 typedef TVec<TUInt64FltPr> TUInt64FltPrV; 01404 typedef TVec<TUInt64StrPr> TUInt64StrPrV; 01405 typedef TVec<TUInt64IntKd> TUInt64IntKdV; 01406 typedef TVec<TUInt64FltKd> TUInt64FltKdV; 01407 typedef TVec<TUInt64StrKd> TUInt64StrKdV; 01408 typedef TVec<TFltBoolKd> TFltBoolKdV; 01409 typedef TVec<TFltIntKd> TFltIntKdV; 01410 typedef TVec<TFltUInt64Kd> TFltUInt64KdV; 01411 typedef TVec<TFltIntPrKd> TFltIntPrKdV; 01412 typedef TVec<TFltKd> TFltKdV; 01413 typedef TVec<TFltStrKd> TFltStrKdV; 01414 typedef TVec<TFltStrPrPr> TFltStrPrPrV; 01415 typedef TVec<TFltIntIntTr> TFltIntIntTrV; 01416 typedef TVec<TFltFltStrTr> TFltFltStrTrV; 01417 typedef TVec<TAscFltIntPr> TAscFltIntPrV; 01418 typedef TVec<TAscFltIntKd> TAscFltIntKdV; 01419 typedef TVec<TStrPr> TStrPrV; 01420 typedef TVec<TStrIntPr> TStrIntPrV; 01421 typedef TVec<TStrFltPr> TStrFltPrV; 01422 typedef TVec<TStrIntKd> TStrIntKdV; 01423 typedef TVec<TStrFltKd> TStrFltKdV; 01424 typedef TVec<TStrAscFltKd> TStrAscFltKdV; 01425 typedef TVec<TStrTr> TStrTrV; 01426 typedef TVec<TStrQu> TStrQuV; 01427 typedef TVec<TStrFltFltTr> TStrFltFltTrV; 01428 typedef TVec<TStrStrIntTr> TStrStrIntTrV; 01429 typedef TVec<TStrKd> TStrKdV; 01430 typedef TVec<TStrStrVPr> TStrStrVPrV; 01431 typedef TVec<TStrVIntPr> TStrVIntPrV; 01432 typedef TVec<TFltIntIntIntQu> TFltIntIntIntQuV; 01433 typedef TVec<TIntStrIntIntQu> TIntStrIntIntQuV; 01434 typedef TVec<TIntIntPrPr> TIntIntPrPrV; 01435 01437 // Vector Pool 01438 template<class TVal> 01439 class TVecPool { 01440 public: 01441 typedef TPt<TVecPool<TVal> > PVecPool; 01442 typedef TVec<TVal> TValV; 01443 private: 01444 TCRef CRef; 01445 TBool FastCopy; 01446 ::TSize GrowBy, MxVals, Vals; 01447 TVal EmptyVal; // empty vector 01448 TVal *ValBf; // buffer storing all the values 01449 TVec< ::TSize> IdToOffV; // id to one past last (vector starts at [id-1]) 01450 private: 01451 void Resize(const ::TSize& _MxVals); 01452 public: 01453 TVecPool(const ::TSize& ExpectVals=0, const ::TSize& _GrowBy=1000000, const bool& _FastCopy=false, const TVal& _EmptyVal=TVal()); 01454 TVecPool(const TVecPool& Pool); 01455 TVecPool(TSIn& SIn); 01456 ~TVecPool() { if (ValBf != NULL) { delete [] ValBf; } ValBf=NULL; } 01457 static PVecPool New(const ::TSize& ExpectVals=0, const ::TSize& GrowBy=1000000, const bool& FastCopy=false) { 01458 return new TVecPool(ExpectVals, GrowBy, FastCopy); } 01459 static PVecPool Load(TSIn& SIn) { return new TVecPool(SIn); } 01460 static PVecPool Load(const TStr& FNm) { TFIn FIn(FNm); return Load(FIn); } 01461 void Save(TSOut& SOut) const; 01462 01463 TVecPool& operator = (const TVecPool& Pool); 01464 01465 ::TSize GetVals() const { return Vals; } 01466 ::TSize GetVecs() const { return IdToOffV.Len(); } 01467 bool IsVId(const int& VId) const { return (0 <= VId) && (VId < IdToOffV.Len()); } 01468 ::TSize Reserved() const { return MxVals; } 01469 void Reserve(const ::TSize& MxVals) { Resize(MxVals); } 01470 const TVal& GetEmptyVal() const { return EmptyVal; } 01471 void SetEmptyVal(const TVal& _EmptyVal) { EmptyVal = _EmptyVal; } 01472 ::TSize GetMemUsed() const { 01473 return sizeof(TCRef)+sizeof(TBool)+3*sizeof(TSize)+sizeof(TVal*)+MxVals*sizeof(TVal);} 01474 01475 int AddV(const TValV& ValV); 01476 int AddEmptyV(const int& ValVLen); 01477 uint GetVLen(const int& VId) const { 01478 if (VId==0){return 0;} 01479 else {return uint(IdToOffV[VId]-IdToOffV[VId-1]);}} 01480 TVal* GetValVPt(const int& VId) const { 01481 if (GetVLen(VId)==0){return (TVal*)&EmptyVal;} 01482 else {return ValBf+IdToOffV[VId-1];}} 01483 void GetV(const int& VId, TValV& ValV) const { 01484 if (GetVLen(VId)==0){ValV.Clr();} 01485 else { ValV.GenExt(GetValVPt(VId), GetVLen(VId)); } } 01486 void PutV(const int& VId, const TValV& ValV) { 01487 IAssert(IsVId(VId) && GetVLen(VId) == ValV.Len()); 01488 if (FastCopy) { 01489 memcpy(GetValVPt(VId), ValV.BegI(), sizeof(TVal)*ValV.Len()); } 01490 else { TVal* ValPt = GetValVPt(VId); 01491 for (uint ValN=0; ValN < uint(ValV.Len()); ValN++, ValPt++) { *ValPt=ValV[ValN]; } 01492 } 01493 } 01494 void CompactPool(const TVal& DelVal); // delete all elements of value DelVal from all vectors 01495 void ShuffleAll(TRnd& Rnd=TInt::Rnd); // shuffles all the order of elements in the Pool (does not respect vector boundaries) 01496 01497 //bool HasIdMap() const { return ! IdToOffV.Empty(); } 01498 //void ClrIdMap() { IdToOffV.Clr(true); } 01499 void Clr(bool DoDel = true) { 01500 IdToOffV.Clr(DoDel); MxVals=0; Vals=0; 01501 if (DoDel && ValBf!=NULL) { delete [] ValBf; ValBf=NULL;} 01502 if (! DoDel) { PutAll(EmptyVal); } 01503 } 01504 void PutAll(const TVal& Val) { 01505 for (TSize ValN = 0; ValN < MxVals; ValN++) { ValBf[ValN]=Val; } } 01506 01507 friend class TPt<TVecPool<TVal> >; 01508 }; 01509 01510 template <class TVal> 01511 void TVecPool<TVal>::Resize(const ::TSize& _MxVals){ 01512 if (_MxVals <= MxVals){ return; } else { MxVals = _MxVals; } 01513 if (ValBf == NULL) { 01514 try { ValBf = new TVal [MxVals]; } 01515 catch (std::exception Ex) { 01516 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()); } 01517 IAssert(ValBf != NULL); 01518 if (EmptyVal != TVal()) { PutAll(EmptyVal); } 01519 } else { 01520 // printf("*** Resize vector pool: %llu -> %llu\n", uint64(Vals), uint64(MxVals)); 01521 TVal* NewValBf = NULL; 01522 try { NewValBf = new TVal [MxVals]; } 01523 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()); } 01524 IAssert(NewValBf != NULL); 01525 if (FastCopy) { 01526 memcpy(NewValBf, ValBf, Vals*sizeof(TVal)); } 01527 else { 01528 for (TSize ValN = 0; ValN < Vals; ValN++){ NewValBf[ValN] = ValBf[ValN]; } } 01529 if (EmptyVal != TVal()) { // init empty values 01530 for (TSize ValN = Vals; ValN < MxVals; ValN++) { NewValBf[ValN] = EmptyVal; } 01531 } 01532 delete [] ValBf; 01533 ValBf = NewValBf; 01534 } 01535 } 01536 01537 template <class TVal> 01538 TVecPool<TVal>::TVecPool(const ::TSize& ExpectVals, const ::TSize& _GrowBy, const bool& _FastCopy, const TVal& _EmptyVal) : 01539 GrowBy(_GrowBy), MxVals(0), Vals(0), EmptyVal(_EmptyVal), ValBf(NULL) { 01540 IdToOffV.Add(0); 01541 Resize(ExpectVals); 01542 } 01543 01544 template <class TVal> 01545 TVecPool<TVal>::TVecPool(const TVecPool& Pool): 01546 FastCopy(Pool.FastCopy), GrowBy(Pool.GrowBy), 01547 MxVals(Pool.MxVals), Vals(Pool.Vals), EmptyVal(Pool.EmptyVal), IdToOffV(Pool.IdToOffV) { 01548 try { ValBf = new TVal [MxVals]; } 01549 catch (std::exception Ex) { FailR(TStr::Fmt("TVecPool::TVecPool: %s, MxVals: %d", Ex.what(), MxVals).CStr()); } 01550 IAssert(ValBf != NULL); 01551 if (FastCopy) { 01552 memcpy(ValBf, Pool.ValBf, MxVals*sizeof(TVal)); } 01553 else { 01554 for (TSize ValN = 0; ValN < MxVals; ValN++){ ValBf[ValN] = Pool.ValBf[ValN]; } } 01555 } 01556 01557 template <class TVal> 01558 TVecPool<TVal>::TVecPool(TSIn& SIn): 01559 FastCopy(SIn) { 01560 uint64 _GrowBy, _MxVals, _Vals; 01561 SIn.Load(_GrowBy); SIn.Load(_MxVals); SIn.Load(_Vals); 01562 IAssert(_GrowBy<TSizeMx && _MxVals<TSizeMx && _Vals<TSizeMx); 01563 GrowBy=TSize(_GrowBy); MxVals=TSize(_Vals); Vals=TSize(_Vals); //note MxVals==Vals 01564 EmptyVal = TVal(SIn); 01565 if (MxVals==0) { ValBf = NULL; } else { ValBf = new TVal [MxVals]; } 01566 for (TSize ValN = 0; ValN < Vals; ValN++) { ValBf[ValN] = TVal(SIn); } 01567 { TInt MxVals(SIn), Vals(SIn); 01568 IdToOffV.Gen(Vals); 01569 for (int ValN = 0; ValN < Vals; ValN++) { 01570 uint64 Offset; SIn.Load(Offset); IAssert(Offset < TSizeMx); 01571 IdToOffV[ValN]=TSize(Offset); 01572 } } 01573 } 01574 01575 template <class TVal> 01576 void TVecPool<TVal>::Save(TSOut& SOut) const { 01577 SOut.Save(FastCopy); 01578 uint64 _GrowBy=GrowBy, _MxVals=MxVals, _Vals=Vals; 01579 SOut.Save(_GrowBy); SOut.Save(_MxVals); SOut.Save(_Vals); 01580 SOut.Save(EmptyVal); 01581 for (TSize ValN = 0; ValN < Vals; ValN++) { ValBf[ValN].Save(SOut); } 01582 { SOut.Save(IdToOffV.Len()); SOut.Save(IdToOffV.Len()); 01583 for (int ValN = 0; ValN < IdToOffV.Len(); ValN++) { 01584 const uint64 Offset=IdToOffV[ValN]; SOut.Save(Offset); 01585 } } 01586 } 01587 01588 template <class TVal> 01589 TVecPool<TVal>& TVecPool<TVal>::operator = (const TVecPool& Pool) { 01590 if (this!=&Pool) { 01591 FastCopy = Pool.FastCopy; 01592 GrowBy = Pool.GrowBy; 01593 MxVals = Pool.MxVals; 01594 Vals = Pool.Vals; 01595 EmptyVal = Pool.EmptyVal; 01596 IdToOffV=Pool.IdToOffV; 01597 try { ValBf = new TVal [MxVals]; } 01598 catch (std::exception Ex) { FailR(TStr::Fmt("TVec::operator= : %s, MxVals: %d", Ex.what(), MxVals).CStr()); } 01599 IAssert(ValBf != NULL); 01600 if (FastCopy) { 01601 memcpy(ValBf, Pool.ValBf, Vals*sizeof(TVal)); } 01602 else { 01603 for (uint64 ValN = 0; ValN < Vals; ValN++){ ValBf[ValN] = Pool.ValBf[ValN]; } } 01604 } 01605 return *this; 01606 } 01607 01608 template<class TVal> 01609 int TVecPool<TVal>::AddV(const TValV& ValV) { 01610 const ::TSize ValVLen = ValV.Len(); 01611 if (ValVLen == 0) { return 0; } 01612 if (MxVals < Vals+ValVLen) { Resize(Vals+max(ValVLen, GrowBy)); } 01613 if (FastCopy) { memcpy(ValBf+Vals, ValV.BegI(), sizeof(TVal)*ValV.Len()); } 01614 else { for (uint ValN=0; ValN < ValVLen; ValN++) { ValBf[Vals+ValN]=ValV[ValN]; } } 01615 Vals+=ValVLen; IdToOffV.Add(Vals); 01616 return IdToOffV.Len()-1; 01617 } 01618 01619 template<class TVal> 01620 int TVecPool<TVal>::AddEmptyV(const int& ValVLen) { 01621 if (ValVLen==0){return 0;} 01622 if (MxVals < Vals+ValVLen){Resize(Vals+max(TSize(ValVLen), GrowBy)); } 01623 Vals+=ValVLen; IdToOffV.Add(Vals); 01624 return IdToOffV.Len()-1; 01625 } 01626 01627 // delete all elements of value DelVal from all vectors 01628 // empty space is left at the end of the pool 01629 template<class TVal> 01630 void TVecPool<TVal>::CompactPool(const TVal& DelVal) { 01631 ::TSize TotalDel=0, NDel=0; 01632 // printf("Compacting %d vectors\n", IdToOffV.Len()); 01633 for (int vid = 1; vid < IdToOffV.Len(); vid++) { 01634 // if (vid % 10000000 == 0) { printf(" %dm", vid/1000000); fflush(stdout); } 01635 const uint Len = GetVLen(vid); 01636 TVal* ValV = GetValVPt(vid); 01637 if (TotalDel > 0) { IdToOffV[vid-1] -= TotalDel; } // update end of vector 01638 if (Len == 0) { continue; } 01639 NDel = 0; 01640 for (TVal* v = ValV; v < ValV+Len-NDel; v++) { 01641 if (*v == DelVal) { 01642 TVal* Beg = v; 01643 while (*v == DelVal && v < ValV+Len) { v++; NDel++; } 01644 memcpy(Beg, v, sizeof(TVal)*int(Len - ::TSize(v - ValV))); 01645 v -= NDel; 01646 } 01647 } 01648 memcpy(ValV-TotalDel, ValV, sizeof(TVal)*Len); // move data 01649 TotalDel += NDel; 01650 } 01651 IdToOffV.Last() -= TotalDel; 01652 for (::TSize i = Vals-TotalDel; i < Vals; i++) { ValBf[i] = EmptyVal; } 01653 Vals -= TotalDel; 01654 // printf(" deleted %llu elements from the pool\n", TotalDel); 01655 } 01656 01657 // shuffles all the order of elements in the pool (does not respect vector boundaries) 01658 template<class TVal> 01659 void TVecPool<TVal>::ShuffleAll(TRnd& Rnd) { 01660 for (::TSize n = Vals-1; n > 0; n--) { 01661 const ::TSize k = ::TSize(((uint64(Rnd.GetUniDevInt())<<32) | uint64(Rnd.GetUniDevInt())) % (n+1)); 01662 const TVal Tmp = ValBf[n]; 01663 ValBf[n] = ValBf[k]; 01664 ValBf[k] = Tmp; 01665 } 01666 } 01667 01668 typedef TVecPool<TInt> TIntVecPool; 01669 typedef TPt<TIntVecPool> PIntVecPool; 01670 01672 // Vector-Pointer 01673 template <class TVal> 01674 class PVec{ 01675 private: 01676 TCRef CRef; 01677 public: 01678 TVec<TVal> V; 01679 public: 01680 PVec<TVal>(): V(){} 01681 PVec<TVal>(const PVec<TVal>& Vec): V(Vec.V){} 01682 static TPt<PVec<TVal> > New(){ 01683 return new PVec<TVal>();} 01684 PVec<TVal>(const int& MxVals, const int& Vals): V(MxVals, Vals){} 01685 static TPt<PVec<TVal> > New(const int& MxVals, const int& Vals){ 01686 return new PVec<TVal>(MxVals, Vals);} 01687 PVec<TVal>(const TVec<TVal>& _V): V(_V){} 01688 static TPt<PVec<TVal> > New(const TVec<TVal>& V){ 01689 return new PVec<TVal>(V);} 01690 explicit PVec<TVal>(TSIn& SIn): V(SIn){} 01691 static TPt<PVec<TVal> > Load(TSIn& SIn){return new PVec<TVal>(SIn);} 01692 void Save(TSOut& SOut) const {V.Save(SOut);} 01693 01694 PVec<TVal>& operator=(const PVec<TVal>& Vec){ 01695 if (this!=&Vec){V=Vec.V;} return *this;} 01696 bool operator==(const PVec<TVal>& Vec) const {return V==Vec.V;} 01697 bool operator<(const PVec<TVal>& Vec) const {return V<Vec.V;} 01698 TVal& operator[](const int& ValN) const {return V[ValN];} 01699 01700 bool Empty() const {return V.Empty();} 01701 int Len() const {return V.Len();} 01702 TVal GetVal(const int& ValN) const {return V[ValN];} 01703 01704 int Add(const TVal& Val){return V.Add(Val);} 01705 01706 friend class TPt<PVec<TVal> >; 01707 }; 01708 01710 // Common-Vector-Pointer-Types 01711 typedef PVec<TFlt> TFltVP; 01712 typedef TPt<TFltVP> PFltV; 01713 typedef PVec<TAscFlt> TAscFltVP; 01714 typedef TPt<TAscFltVP> PAscFltV; 01715 typedef PVec<TStr> TStrVP; 01716 typedef TPt<TStrVP> PStrV; 01717 01719 // 2D-Vector 01720 template <class TVal> 01721 class TVVec{ 01722 private: 01723 TInt XDim, YDim; 01724 TVec<TVal> ValV; 01725 public: 01726 TVVec(): XDim(), YDim(), ValV(){} 01727 TVVec(const TVVec& Vec): 01728 XDim(Vec.XDim), YDim(Vec.YDim), ValV(Vec.ValV){} 01729 TVVec(const int& _XDim, const int& _YDim): 01730 XDim(), YDim(), ValV(){Gen(_XDim, _YDim);} 01731 explicit TVVec(const TVec<TVal>& _ValV, const int& _XDim, const int& _YDim): 01732 XDim(_XDim), YDim(_YDim), ValV(_ValV){ IAssert(ValV.Len()==XDim*YDim); } 01733 explicit TVVec(TSIn& SIn) {Load(SIn);} 01734 void Load(TSIn& SIn){XDim.Load(SIn); YDim.Load(SIn); ValV.Load(SIn);} 01735 void Save(TSOut& SOut) const { 01736 XDim.Save(SOut); YDim.Save(SOut); ValV.Save(SOut);} 01737 01738 TVVec<TVal>& operator=(const TVVec<TVal>& Vec){ 01739 if (this!=&Vec){XDim=Vec.XDim; YDim=Vec.YDim; ValV=Vec.ValV;} return *this;} 01740 bool operator==(const TVVec& Vec) const { 01741 return (XDim==Vec.XDim)&&(YDim==Vec.YDim)&&(ValV==Vec.ValV);} 01742 01743 bool Empty() const {return ValV.Len()==0;} 01744 void Clr(){XDim=0; YDim=0; ValV.Clr();} 01745 void Gen(const int& _XDim, const int& _YDim){ 01746 Assert((_XDim>=0)&&(_YDim>=0)); 01747 XDim=_XDim; YDim=_YDim; ValV.Gen(XDim*YDim);} 01748 int GetXDim() const {return XDim;} 01749 int GetYDim() const {return YDim;} 01750 int GetRows() const {return XDim;} 01751 int GetCols() const {return YDim;} 01752 TVec<TVal>& Get1DVec(){return ValV;} 01753 01754 const TVal& At(const int& X, const int& Y) const { 01755 Assert((0<=X)&&(X<int(XDim))&&(0<=Y)&&(Y<int(YDim))); 01756 return ValV[X*YDim+Y];} 01757 TVal& At(const int& X, const int& Y){ 01758 Assert((0<=X)&&(X<int(XDim))&&(0<=Y)&&(Y<int(YDim))); 01759 return ValV[X*YDim+Y];} 01760 TVal& operator()(const int& X, const int& Y){ 01761 return At(X, Y);} 01762 const TVal& operator()(const int& X, const int& Y) const { 01763 return At(X, Y);} 01764 01765 void PutXY(const int& X, const int& Y, const TVal& Val){At(X, Y)=Val;} 01766 void PutAll(const TVal& Val){ValV.PutAll(Val);} 01767 void PutX(const int& X, const TVal& Val){ 01768 for (int Y=0; Y<int(YDim); Y++){At(X, Y)=Val;}} 01769 void PutY(const int& Y, const TVal& Val){ 01770 for (int X=0; X<int(XDim); X++){At(X, Y)=Val;}} 01771 TVal GetXY(const int& X, const int& Y) const { 01772 Assert((0<=X)&&(X<int(XDim))&&(0<=Y)&&(Y<int(YDim))); 01773 return ValV[X*YDim+Y];} 01774 void GetRow(const int& RowN, TVec<TVal>& Vec) const; 01775 void GetCol(const int& ColN, TVec<TVal>& Vec) const; 01776 01777 void SwapX(const int& X1, const int& X2); 01778 void SwapY(const int& Y1, const int& Y2); 01779 void Swap(TVVec<TVal>& Vec); 01780 01781 void ShuffleX(TRnd& Rnd); 01782 void ShuffleY(TRnd& Rnd); 01783 void GetMxValXY(int& X, int& Y) const; 01784 01785 void CopyFrom(const TVVec<TVal>& VVec); 01786 void AddXDim(); 01787 void AddYDim(); 01788 void DelX(const int& X); 01789 void DelY(const int& Y); 01790 }; 01791 01792 template <class TVal> 01793 void TVVec<TVal>::SwapX(const int& X1, const int& X2){ 01794 for (int Y=0; Y<int(YDim); Y++){ 01795 TVal Val=At(X1, Y); At(X1, Y)=At(X2, Y); At(X2, Y)=Val;} 01796 } 01797 01798 template <class TVal> 01799 void TVVec<TVal>::SwapY(const int& Y1, const int& Y2){ 01800 for (int X=0; X<int(XDim); X++){ 01801 TVal Val=At(X, Y1); At(X, Y1)=At(X, Y2); At(X, Y2)=Val;} 01802 } 01803 01804 template <class TVal> 01805 void TVVec<TVal>::Swap(TVVec<TVal>& Vec){ //J: 01806 if (this!=&Vec){ 01807 ::Swap(XDim, Vec.XDim); 01808 ::Swap(YDim, Vec.YDim); 01809 ValV.Swap(Vec.ValV); 01810 } 01811 } 01812 01813 template <class TVal> 01814 void TVVec<TVal>::ShuffleX(TRnd& Rnd){ 01815 for (int X=0; X<XDim-1; X++){SwapX(X, X+Rnd.GetUniDevInt(XDim-X));} 01816 } 01817 01818 template <class TVal> 01819 void TVVec<TVal>::ShuffleY(TRnd& Rnd){ 01820 for (int Y=0; Y<YDim-1; Y++){SwapY(Y, Y+Rnd.GetUniDevInt(YDim-Y));} 01821 } 01822 01823 template <class TVal> 01824 void TVVec<TVal>::GetMxValXY(int& X, int& Y) const { 01825 int MxValN=ValV.GetMxValN(); 01826 Y=MxValN%YDim; 01827 X=MxValN/YDim; 01828 } 01829 01830 template <class TVal> 01831 void TVVec<TVal>::CopyFrom(const TVVec<TVal>& VVec){ 01832 int CopyXDim=TInt::GetMn(GetXDim(), VVec.GetXDim()); 01833 int CopyYDim=TInt::GetMn(GetYDim(), VVec.GetYDim()); 01834 for (int X=0; X<CopyXDim; X++){ 01835 for (int Y=0; Y<CopyYDim; Y++){ 01836 At(X, Y)=VVec.At(X, Y); 01837 } 01838 } 01839 } 01840 01841 template <class TVal> 01842 void TVVec<TVal>::AddXDim(){ 01843 TVVec<TVal> NewVVec(XDim+1, YDim); 01844 NewVVec.CopyFrom(*this); 01845 *this=NewVVec; 01846 } 01847 01848 template <class TVal> 01849 void TVVec<TVal>::AddYDim(){ 01850 TVVec<TVal> NewVVec(XDim, YDim+1); 01851 NewVVec.CopyFrom(*this); 01852 *this=NewVVec; 01853 } 01854 01855 template <class TVal> 01856 void TVVec<TVal>::DelX(const int& X){ 01857 TVVec<TVal> NewVVec(XDim-1, YDim); 01858 for (int Y=0; Y<YDim; Y++){ 01859 for (int LX=0; LX<X; LX++){ 01860 NewVVec.At(LX, Y)=At(LX, Y);} 01861 for (int RX=X+1; RX<XDim; RX++){ 01862 NewVVec.At(RX-1, Y)=At(RX, Y);} 01863 } 01864 *this=NewVVec; 01865 } 01866 01867 template <class TVal> 01868 void TVVec<TVal>::DelY(const int& Y){ 01869 TVVec<TVal> NewVVec(XDim, YDim-1); 01870 for (int X=0; X<XDim; X++){ 01871 for (int LY=0; LY<Y; LY++){ 01872 NewVVec.At(X, LY)=At(X, LY);} 01873 for (int RY=Y+1; RY<YDim; RY++){ 01874 NewVVec.At(X, RY-1)=At(X, RY);} 01875 } 01876 *this=NewVVec; 01877 } 01878 01879 template <class TVal> 01880 void TVVec<TVal>::GetRow(const int& RowN, TVec<TVal>& Vec) const { 01881 Vec.Gen(GetCols(), 0); 01882 for (int col = 0; col < GetCols(); col++) { 01883 Vec.Add(At(RowN, col)); 01884 } 01885 } 01886 01887 template <class TVal> 01888 void TVVec<TVal>::GetCol(const int& ColN, TVec<TVal>& Vec) const { 01889 Vec.Gen(GetRows(), 0); 01890 for (int row = 0; row < GetRows(); row++) { 01891 Vec.Add(At(row, ColN)); 01892 } 01893 } 01894 01896 // Common-2D-Vector-Types 01897 typedef TVVec<TBool> TBoolVV; 01898 typedef TVVec<TCh> TChVV; 01899 typedef TVVec<TInt> TIntVV; 01900 typedef TVVec<TSFlt> TSFltVV; 01901 typedef TVVec<TFlt> TFltVV; 01902 typedef TVVec<TStr> TStrVV; 01903 typedef TVVec<TIntPr> TIntPrVV; 01904 01906 // 3D-Vector 01907 template <class TVal> 01908 class TVVVec{ 01909 private: 01910 TInt XDim, YDim, ZDim; 01911 TVec<TVal> ValV; 01912 public: 01913 TVVVec(): XDim(), YDim(), ZDim(), ValV(){} 01914 TVVVec(const TVVVec& Vec): 01915 XDim(Vec.XDim), YDim(Vec.YDim), ZDim(Vec.ZDim), ValV(Vec.ValV){} 01916 TVVVec(const int& _XDim, const int& _YDim, const int& _ZDim): 01917 XDim(), YDim(), ZDim(), ValV(){Gen(_XDim, _YDim, _ZDim);} 01918 explicit TVVVec(TSIn& SIn): 01919 XDim(SIn), YDim(SIn), ZDim(SIn), ValV(SIn){} 01920 void Save(TSOut& SOut) const { 01921 XDim.Save(SOut); YDim.Save(SOut); ZDim.Save(SOut); ValV.Save(SOut);} 01922 01923 TVVVec<TVal>& operator=(const TVVVec<TVal>& Vec){ 01924 XDim=Vec.XDim; YDim=Vec.YDim; ZDim=Vec.ZDim; ValV=Vec.ValV; 01925 return *this; 01926 } 01927 bool operator==(const TVVVec& Vec) const { 01928 return (XDim==Vec.XDim)&&(YDim==Vec.YDim)&&(ZDim==Vec.ZDim)&& 01929 (ValV==Vec.ValV);} 01930 01931 bool Empty() const {return ValV.Len()==0;} 01932 void Clr(){XDim=0; YDim=0; ZDim=0; ValV.Clr();} 01933 void Gen(const int& _XDim, const int& _YDim, const int& _ZDim){ 01934 Assert((_XDim>=0)&&(_YDim>=0)&&(_ZDim>=0)); 01935 XDim=_XDim; YDim=_YDim; ZDim=_ZDim; ValV.Gen(XDim*YDim*ZDim);} 01936 TVal& At(const int& X, const int& Y, const int& Z){ 01937 Assert((0<=X)&&(X<int(XDim))&&(0<=Y)&&(Y<int(YDim))&&(0<=Z)&&(Z<int(ZDim))); 01938 return ValV[X*YDim*ZDim+Y*ZDim+Z];} 01939 const TVal& At(const int& X, const int& Y, const int& Z) const { 01940 Assert((0<=X)&&(X<int(XDim))&&(0<=Y)&&(Y<int(YDim))&&(0<=Z)&&(Z<int(ZDim))); 01941 return ValV[X*YDim*ZDim+Y*ZDim+Z];} 01942 TVal& operator()(const int& X, const int& Y, const int& Z){ 01943 return At(X, Y, Z);} 01944 const TVal& operator()(const int& X, const int& Y, const int& Z) const { 01945 return At(X, Y, Z);} 01946 int GetXDim() const {return XDim;} 01947 int GetYDim() const {return YDim;} 01948 int GetZDim() const {return ZDim;} 01949 }; 01950 01952 // Common-3D-Vector-Types 01953 typedef TVVVec<TInt> TIntVVV; 01954 typedef TVVVec<TFlt> TFltVVV; 01955 01957 // Tree 01958 template <class TVal> 01959 class TTree{ 01960 private: 01961 TVec<TTriple<TInt, TIntV, TVal> > NodeV; // (ParentNodeId, ChildNodeIdV, NodeVal) 01962 public: 01963 TTree(): NodeV(){} 01964 TTree(const TTree& Tree): NodeV(Tree.NodeV){} 01965 explicit TTree(TSIn& SIn): NodeV(SIn){} 01966 void Save(TSOut& SOut) const {NodeV.Save(SOut);} 01967 void LoadXml(const PXmlTok& XmlTok, const TStr& Nm=""); 01968 void SaveXml(TSOut& SOut, const TStr& Nm) const; 01969 01970 TTree& operator=(const TTree& Tree){if (this!=&Tree){NodeV=Tree.NodeV;} return *this;} 01971 bool operator==(const TTree& Tree) const {return NodeV==Tree.NodeV;} 01972 bool operator<(const TTree& Tree) const {return false;} 01973 01974 int GetPrimHashCd() const {return NodeV.GetPrimHashCd();} 01975 int GetSecHashCd() const {return NodeV.GetSecHashCd();} 01976 01977 int GetMemUsed() const {return NodeV.GetMemUsed();} 01978 01979 void Clr(){NodeV.Clr();} 01980 01981 int AddNode(const int& ParentNodeId, const TVal& NodeVal=TVal()){ 01982 IAssert(((ParentNodeId==-1)&&(NodeV.Len()==0))||(NodeV.Len()>0)); 01983 if (ParentNodeId!=-1){NodeV[ParentNodeId].Val2.Add(NodeV.Len());} 01984 return NodeV.Add(TTriple<TInt, TIntV, TVal>(ParentNodeId, TIntV(), NodeVal));} 01985 int AddRoot(const TVal& NodeVal=TVal()){ 01986 return AddNode(-1, NodeVal);} 01987 01988 int GetNodes() const {return NodeV.Len();} 01989 void GetNodeIdV(TIntV& NodeIdV, const int& NodeId=0); 01990 int GetParentNodeId(const int& NodeId) const {return NodeV[NodeId].Val1;} 01991 int GetChildren(const int& NodeId) const {return NodeV[NodeId].Val2.Len();} 01992 int GetChildNodeId(const int& NodeId, const int& ChildN) const {return NodeV[NodeId].Val2[ChildN];} 01993 TVal& GetNodeVal(const int& NodeId){return NodeV[NodeId].Val3;} 01994 01995 void GenRandomTree(const int& Nodes, TRnd& Rnd); 01996 01997 void DelNode(const int& NodeId); 01998 void CopyTree(const int& SrcNodeId, TTree& DstTree, const int& DstParentNodeId=-1); 01999 02000 void WrTree(const int& NodeId=0, const int& Lev=0); 02001 }; 02002 02003 template <class TVal> 02004 void TTree<TVal>::GetNodeIdV(TIntV& NodeIdV, const int& NodeId){ 02005 if (NodeId==0){NodeIdV.Clr(); if (GetNodes()==0){return;}} 02006 else if (GetParentNodeId(NodeId)==-1){return;} 02007 NodeIdV.Add(NodeId); 02008 for (int ChildN=0; ChildN<GetChildren(NodeId); ChildN++){ 02009 int ChildNodeId=GetChildNodeId(NodeId, ChildN); 02010 if (ChildNodeId!=-1){ 02011 GetNodeIdV(NodeIdV, ChildNodeId); 02012 } 02013 } 02014 } 02015 02016 template <class TVal> 02017 void TTree<TVal>::GenRandomTree(const int& Nodes, TRnd& Rnd){ 02018 Clr(); 02019 if (Nodes>0){ 02020 AddRoot(TVal()); 02021 for (int NodeN=1; NodeN<Nodes; NodeN++){ 02022 int ParentNodeId=Rnd.GetUniDevInt(0, GetNodes()-1); 02023 AddNode(ParentNodeId, TVal()); 02024 } 02025 } 02026 } 02027 02028 template <class TVal> 02029 void TTree<TVal>::DelNode(const int& NodeId){ 02030 if (NodeId==0){ 02031 Clr(); 02032 } else { 02033 TIntV& ChildNodeIdV=NodeV[GetParentNodeId(NodeId)].Val2; 02034 int ChildNodeIdN=ChildNodeIdV.SearchForw(NodeId); 02035 ChildNodeIdV[ChildNodeIdN]=-1; 02036 } 02037 } 02038 02039 template <class TVal> 02040 void TTree<TVal>::CopyTree(const int& SrcNodeId, TTree& DstTree, const int& DstParentNodeId){ 02041 int DstNodeId=DstTree.AddNode(DstParentNodeId, GetNodeVal(SrcNodeId)); 02042 for (int ChildN=0; ChildN<GetChildren(SrcNodeId); ChildN++){ 02043 int ChildNodeId=GetChildNodeId(SrcNodeId, ChildN); 02044 if (ChildNodeId!=-1){ 02045 CopyTree(ChildNodeId, DstTree, DstNodeId); 02046 } 02047 } 02048 } 02049 02050 template <class TVal> 02051 void TTree<TVal>::WrTree(const int& NodeId, const int& Lev){ 02052 for (int LevN=0; LevN<Lev; LevN++){printf("| ");} 02053 printf("%d (%d)\n", NodeId, GetChildren(NodeId)); 02054 for (int ChildN=0; ChildN<GetChildren(NodeId); ChildN++){ 02055 int ChildNodeId=GetChildNodeId(NodeId, ChildN); 02056 if (ChildNodeId!=-1){ 02057 WrTree(ChildNodeId, Lev+1); 02058 } 02059 } 02060 } 02061 02063 // Common-Tree-Types 02064 typedef TTree<TInt> TIntTree; 02065 typedef TTree<TFlt> TFltTree; 02066 typedef TTree<TStr> TStrTree; 02067 typedef TTree<TStrIntPr> TStrIntPrTree; 02068 typedef TTree<TStrIntStrVTr> TStrIntStrVTrTree; 02069 02071 // Stack 02072 template <class TVal> 02073 class TSStack{ 02074 private: 02075 TVec<TVal> ValV; 02076 public: 02077 TSStack(): ValV(){} 02078 TSStack(const int& MxVals): ValV(MxVals, 0){} 02079 TSStack(const TSStack& Stack): ValV(Stack.ValV){} 02080 explicit TSStack(TSIn& SIn): ValV(SIn){} 02081 void Save(TSOut& SOut) const {ValV.Save(SOut);} 02082 02083 TSStack& operator=(const TSStack& Stack){ 02084 if (this!=&Stack){ValV=Stack.ValV;} return *this;} 02085 bool operator==(const TSStack& Stack) const {return this==&Stack;} 02086 const TVal& operator[](const int& ValN) const {return ValV[ValV.Len()-ValN-1];} 02087 TVal& operator[](const int& ValN) {return ValV[ValV.Len()-ValN-1];} 02088 02089 bool Empty(){return ValV.Len()==0;} 02090 void Clr(const bool& DoDel=false) {ValV.Clr(DoDel);} 02091 bool IsIn(const TVal& Val) const {return ValV.IsIn(Val);} 02092 int Len(){return ValV.Len();} 02093 TVal& Top(){Assert(0<ValV.Len()); return ValV.Last();} 02094 const TVal& Top() const {Assert(0<ValV.Len()); return ValV.Last();} 02095 void Push(){ValV.Add();} 02096 void Push(const TVal& Val){ValV.Add(Val);} 02097 void Pop(){Assert(0<ValV.Len()); ValV.DelLast();} 02098 }; 02099 02101 // Common-Stack-Types 02102 typedef TSStack<TInt> TIntS; 02103 typedef TSStack<TBoolChPr> TBoolChS; 02104 02106 // Queue 02107 template <class TVal> 02108 class TQQueue{ 02109 private: 02110 TInt MxLast, MxLen; 02111 TInt First, Last; 02112 TVec<TVal> ValV; 02113 public: 02114 TQQueue(const int& _MxLast=64, const int& _MxLen=-1): 02115 MxLast(_MxLast), MxLen(_MxLen), First(0), Last(0), ValV(){ 02116 Assert(int(MxLast)>0); Assert((MxLen==-1)||(int(MxLen)>0));} 02117 TQQueue(const TQQueue& Queue): 02118 MxLast(Queue.MxLast), MxLen(Queue.MxLen), 02119 First(Queue.First), Last(Queue.Last), ValV(Queue.ValV){} 02120 explicit TQQueue(TSIn& SIn): 02121 MxLast(SIn), MxLen(SIn), First(SIn), Last(SIn), ValV(SIn){} 02122 void Save(TSOut& SOut) const { 02123 MxLast.Save(SOut); MxLen.Save(SOut); 02124 First.Save(SOut); Last.Save(SOut); ValV.Save(SOut);} 02125 02126 TQQueue& operator=(const TQQueue& Queue){ 02127 if (this!=&Queue){MxLast=Queue.MxLast; MxLen=Queue.MxLen; 02128 First=Queue.First; Last=Queue.Last; ValV=Queue.ValV;} 02129 return *this;} 02130 const TVal& operator[](const int& ValN) const {Assert((0<=ValN)&&(ValN<Len())); 02131 return ValV[Last+ValN];} 02132 02133 void Clr(const bool& DoDel=true){ValV.Clr(DoDel); First=Last=0;} 02134 void Gen(const int& _MxLast=64, const int& _MxLen=-1){ 02135 MxLast=_MxLast; MxLen=_MxLen; First=0; Last=0; ValV.Clr();} 02136 void GetSubValV(const int& _BValN, const int& _EValN, TVec<TVal>& SubValV) const { 02137 int BValN=TInt::GetMx(0, _BValN); 02138 int EValN=TInt::GetMn(Len()-1, _EValN); 02139 SubValV.Gen(EValN-BValN+1); 02140 for (int ValN=BValN; ValN<=EValN; ValN++){ 02141 SubValV[ValN-BValN]=ValV[Last+ValN];} 02142 } 02143 02144 bool Empty() const {return First==Last;} 02145 int Len() const {return First-Last;} 02146 const TVal& Top() const { 02147 Assert(First!=Last); return ValV[Last];} 02148 void Pop(){ 02149 IAssert(First!=Last); Last++; 02150 if (First==Last){ValV.Clr(); First=Last=0;}} 02151 void Push(const TVal& Val){ 02152 if (Last>MxLast){ValV.Del(0, Last-1); First-=Last; Last=0;} 02153 if ((MxLen!=-1)&&(MxLen==Len())){Pop();} 02154 First++; ValV.Add(Val);} 02155 02156 void Shuffle(TRnd& Rnd){ 02157 TVec<TVal> ValV(Len(), 0); while (!Empty()){ValV.Add(Top()); Pop();} 02158 ValV.Shuffle(Rnd); Clr(); 02159 for (int ValN=0; ValN<ValV.Len(); ValN++){Push(ValV[ValN]);}} 02160 }; 02161 02163 // Common-Queue-Types 02164 typedef TQQueue<TInt> TIntQ; 02165 typedef TQQueue<TFlt> TFltQ; 02166 typedef TQQueue<TStr> TStrQ; 02167 typedef TQQueue<TIntPr> TIntPrQ; 02168 typedef TQQueue<TIntStrPr> TIntStrPrQ; 02169 typedef TQQueue<TFltV> TFltVQ; 02170 typedef TQQueue<TAscFltV> TAscFltVQ; 02171 typedef TVec<TQQueue<TInt> > TIntQV; 02172 02174 // List-Node 02175 template <class TVal> 02176 class TLstNd{ 02177 public: 02178 TLstNd* PrevNd; 02179 TLstNd* NextNd; 02180 TVal Val; 02181 public: 02182 TLstNd(): PrevNd(NULL), NextNd(NULL), Val(){} 02183 TLstNd(const TLstNd&); 02184 TLstNd(TLstNd* _PrevNd, TLstNd* _NextNd, const TVal& _Val): 02185 PrevNd(_PrevNd), NextNd(_NextNd), Val(_Val){} 02186 02187 TLstNd& operator=(const TLstNd&); 02188 02189 TLstNd* Prev() const {Assert(this!=NULL); return PrevNd;} 02190 TLstNd* Next() const {Assert(this!=NULL); return NextNd;} 02191 TVal& GetVal(){Assert(this!=NULL); return Val;} 02192 const TVal& GetVal() const {Assert(this!=NULL); return Val;} 02193 }; 02194 02196 // List 02197 template <class TVal> 02198 class TLst{ 02199 public: 02200 typedef TLstNd<TVal>* PLstNd; 02201 private: 02202 int Nds; 02203 PLstNd FirstNd; 02204 PLstNd LastNd; 02205 public: 02206 TLst(): Nds(0), FirstNd(NULL), LastNd(NULL){} 02207 TLst(const TLst&); 02208 ~TLst(){Clr();} 02209 explicit TLst(TSIn& SIn); 02210 void Save(TSOut& SOut) const; 02211 02212 TLst& operator=(const TLst&); 02213 02214 void Clr(){ 02215 PLstNd Nd=FirstNd; 02216 while (Nd!=NULL){PLstNd NextNd=Nd->NextNd; delete Nd; Nd=NextNd;} 02217 Nds=0; FirstNd=NULL; LastNd=NULL;} 02218 02219 bool Empty() const {return Nds==0;} 02220 int Len() const {return Nds;} 02221 PLstNd First() const {return FirstNd;} 02222 PLstNd Last() const {return LastNd;} 02223 TVal& FirstVal() const {return FirstNd->GetVal();} 02224 TVal& LastVal() const {return LastNd->GetVal();} 02225 02226 PLstNd AddFront(const TVal& Val); 02227 PLstNd AddBack(const TVal& Val); 02228 PLstNd AddFrontSorted(const TVal& Val, const bool& Asc=true); 02229 PLstNd AddBackSorted(const TVal& Val, const bool& Asc=true); 02230 void PutFront(const PLstNd& Nd); 02231 void PutBack(const PLstNd& Nd); 02232 PLstNd Ins(const PLstNd& Nd, const TVal& Val); 02233 void Del(const TVal& Val); 02234 void Del(const PLstNd& Nd); 02235 void DelFirst() { PLstNd DelNd = FirstNd; Del(DelNd); } 02236 void DelLast() { PLstNd DelNd = LastNd; Del(DelNd); } 02237 02238 PLstNd SearchForw(const TVal& Val); 02239 PLstNd SearchBack(const TVal& Val); 02240 02241 friend class TLstNd<TVal>; 02242 }; 02243 02244 template <class TVal> 02245 TLst<TVal>::TLst(TSIn& SIn): 02246 Nds(0), FirstNd(NULL), LastNd(NULL){ 02247 int CheckNds=0; SIn.Load(CheckNds); 02248 for (int NdN=0; NdN<CheckNds; NdN++){AddBack(TVal(SIn));} 02249 Assert(Nds==CheckNds); 02250 } 02251 02252 template <class TVal> 02253 void TLst<TVal>::Save(TSOut& SOut) const { 02254 SOut.Save(Nds); 02255 PLstNd Nd=FirstNd; int CheckNds=0; 02256 while (Nd!=NULL){ 02257 Nd->Val.Save(SOut); Nd=Nd->NextNd; CheckNds++;} 02258 IAssert(Nds==CheckNds); 02259 } 02260 02261 template <class TVal> 02262 TLstNd<TVal>* TLst<TVal>::AddFront(const TVal& Val){ 02263 PLstNd Nd=new TLstNd<TVal>(NULL, FirstNd, Val); 02264 if (FirstNd!=NULL){FirstNd->PrevNd=Nd; FirstNd=Nd;} 02265 else {FirstNd=Nd; LastNd=Nd;} 02266 Nds++; return Nd; 02267 } 02268 02269 template <class TVal> 02270 TLstNd<TVal>* TLst<TVal>::AddBack(const TVal& Val){ 02271 PLstNd Nd=new TLstNd<TVal>(LastNd, NULL, Val); 02272 if (LastNd!=NULL){LastNd->NextNd=Nd; LastNd=Nd;} 02273 else {FirstNd=Nd; LastNd=Nd;} 02274 Nds++; return Nd; 02275 } 02276 02277 template <class TVal> 02278 TLstNd<TVal>* TLst<TVal>::AddFrontSorted(const TVal& Val, const bool& Asc){ 02279 PLstNd Nd=First(); 02280 if (Nd==NULL){ 02281 return Ins(Nd, Val); 02282 } else { 02283 while ((Nd!=NULL)&&((Asc&&(Val>Nd()))||(!Asc&&(Val<Nd())))){ 02284 Nd=Nd->Next();} 02285 if (Nd==NULL){return Ins(Nd->Last(), Val);} 02286 else {return Ins(Nd->Prev(), Val);} 02287 } 02288 } 02289 02290 template <class TVal> 02291 TLstNd<TVal>* TLst<TVal>::AddBackSorted(const TVal& Val, const bool& Asc){ 02292 PLstNd Nd=Last(); 02293 while ((Nd!=NULL)&&((Asc&&(Val<Nd->Val))||(!Asc&&(Val>Nd->Val)))){ 02294 Nd=Nd->Prev();} 02295 return Ins(Nd, Val); 02296 } 02297 02298 template <class TVal> 02299 void TLst<TVal>::PutFront(const PLstNd& Nd){ 02300 Assert(Nd!=NULL); 02301 // unchain 02302 if (Nd->PrevNd==NULL){FirstNd=Nd->NextNd;} 02303 else {Nd->PrevNd->NextNd=Nd->NextNd;} 02304 if (Nd->NextNd==NULL){LastNd=Nd->PrevNd;} 02305 else {Nd->NextNd->PrevNd=Nd->PrevNd;} 02306 // add to front 02307 Nd->PrevNd=NULL; Nd->NextNd=FirstNd; 02308 if (FirstNd!=NULL){FirstNd->PrevNd=Nd; FirstNd=Nd;} 02309 else {FirstNd=Nd; LastNd=Nd;} 02310 } 02311 02312 template <class TVal> 02313 void TLst<TVal>::PutBack(const PLstNd& Nd){ 02314 Assert(Nd!=NULL); 02315 // unchain 02316 if (Nd->PrevNd==NULL){FirstNd=Nd->NextNd;} 02317 else {Nd->PrevNd->NextNd=Nd->NextNd;} 02318 if (Nd->NextNd==NULL){LastNd=Nd->PrevNd;} 02319 else {Nd->NextNd->PrevNd=Nd->PrevNd;} 02320 // add to back 02321 Nd->PrevNd=LastNd; Nd->NextNd=NULL; 02322 if (LastNd!=NULL){LastNd->NextNd=Nd; LastNd=Nd;} 02323 else {FirstNd=Nd; LastNd=Nd;} 02324 } 02325 02326 template <class TVal> 02327 TLstNd<TVal>* TLst<TVal>::Ins(const PLstNd& Nd, const TVal& Val){ 02328 if (Nd==NULL){return AddFront(Val);} 02329 else if (Nd->NextNd==NULL){return AddBack(Val);} 02330 else { 02331 PLstNd NewNd=new TLstNd<TVal>(Nd, Nd->NextNd, Val); 02332 Nd->NextNd=NewNd; NewNd->NextNd->PrevNd=Nd; 02333 Nds++; return Nd; 02334 } 02335 } 02336 02337 template <class TVal> 02338 void TLst<TVal>::Del(const TVal& Val){ 02339 PLstNd Nd=SearchForw(Val); 02340 if (Nd!=NULL){Del(Nd);} 02341 } 02342 02343 template <class TVal> 02344 void TLst<TVal>::Del(const PLstNd& Nd){ 02345 Assert(Nd!=NULL); 02346 if (Nd->PrevNd==NULL){FirstNd=Nd->NextNd;} 02347 else {Nd->PrevNd->NextNd=Nd->NextNd;} 02348 if (Nd->NextNd==NULL){LastNd=Nd->PrevNd;} 02349 else {Nd->NextNd->PrevNd=Nd->PrevNd;} 02350 Nds--; delete Nd; 02351 } 02352 02353 template <class TVal> 02354 TLstNd<TVal>* TLst<TVal>::SearchForw(const TVal& Val){ 02355 PLstNd Nd=First(); 02356 while (Nd!=NULL){ 02357 if (Nd->GetVal()==Val){return Nd;} 02358 Nd=Nd->Next(); 02359 } 02360 return NULL; 02361 } 02362 02363 template <class TVal> 02364 TLstNd<TVal>* TLst<TVal>::SearchBack(const TVal& Val){ 02365 PLstNd Nd=Last(); 02366 while (Nd!=NULL){ 02367 if (Nd->GetVal()==Val){return Nd;} 02368 Nd=Nd->Prev(); 02369 } 02370 return NULL; 02371 } 02372 02374 // Common-List-Types 02375 typedef TLst<TInt> TIntL; typedef TLstNd<TInt>* PIntLN; 02376 typedef TLst<TIntKd> TIntKdL; typedef TLstNd<TIntKd>* PIntKdLN; 02377 typedef TLst<TFlt> TFltL; typedef TLstNd<TFlt>* PFltLN; 02378 typedef TLst<TFltIntKd> TFltIntKdL; typedef TLstNd<TFltIntKd>* PFltIntKdLN; 02379 typedef TLst<TAscFltIntKd> TAscFltIntKdL; typedef TLstNd<TAscFltIntKd>* PAscFltIntKdLN; 02380 typedef TLst<TStr> TStrL; typedef TLstNd<TStr>* PStrLN; 02381 02383 // Record-File 02384 template <class THd, class TRec> 02385 class TFRec{ 02386 private: 02387 PFRnd FRnd; 02388 public: 02389 TFRec(const TStr& FNm, const TFAccess& FAccess, const bool& CreateIfNo): 02390 FRnd(PFRnd(new TFRnd(FNm, FAccess, CreateIfNo, sizeof(THd), sizeof(TRec)))){} 02391 TFRec(const TFRec&); 02392 02393 TFRec& operator=(const TFRec&); 02394 02395 void SetRecN(const int& RecN){FRnd->SetRecN(RecN);} 02396 int GetRecN(){return FRnd->GetRecN();} 02397 int GetRecs(){return FRnd->GetRecs();} 02398 02399 void GetHd(THd& Hd){FRnd->GetHd(&Hd);} 02400 void PutHd(const THd& Hd){FRnd->PutHd(&Hd);} 02401 void GetRec(TRec& Rec, const int& RecN=-1){FRnd->GetRec(&Rec, RecN);} 02402 void PutRec(const TRec& Rec, const int& RecN=-1){FRnd->PutRec(&Rec, RecN);} 02403 }; 02404 02406 // Function 02407 template <class TFuncPt> 02408 class TFunc{ 02409 private: 02410 TFuncPt FuncPt; 02411 public: 02412 TFunc(): FuncPt(NULL){} 02413 TFunc(const TFunc& Func): FuncPt(Func.FuncPt){} 02414 TFunc(const TFuncPt& _FuncPt): FuncPt(_FuncPt){} 02415 TFunc(TSIn&){Fail;} 02416 void Save(TSOut&) const {Fail;} 02417 02418 TFunc& operator=(const TFunc& Func){ 02419 if (this!=&Func){FuncPt=Func.FuncPt;} return *this;} 02420 bool operator==(const TFunc& Func) const { 02421 return FuncPt==Func.FuncPt;} 02422 bool operator<(const TFunc&) const { 02423 Fail; return false;} 02424 TFuncPt operator()() const {return FuncPt;} 02425 };