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