SNAP Library 3.0, User Reference  2016-07-20 17:56:49
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ds.h
Go to the documentation of this file.
1 // Address-Pointer
3 template <class TRec>
4 class TAPt{
5 private:
6  TRec* Addr;
7 public:
8  TAPt(): Addr(NULL){}
9  TAPt(const TAPt& Pt): Addr(Pt.Addr){}
10  TAPt(TRec* _Addr): Addr(_Addr){}
12  void Save(TSOut&) const {Fail;}
13 
14  TAPt& operator=(const TAPt& Pt){Addr=Pt.Addr; return *this;}
15  TAPt& operator=(TRec* _Addr){Addr=_Addr; return *this;}
16  bool operator==(const TAPt& Pt) const {return *Addr==*Pt.Addr;}
17  bool operator!=(const TAPt& Pt) const {return *Addr!=*Pt.Addr;}
18  bool operator<(const TAPt& Pt) const {return *Addr<*Pt.Addr;}
19 
20  TRec* operator->() const {Assert(Addr!=NULL); return Addr;}
21  TRec& operator*() const {Assert(Addr!=NULL); return *Addr;}
22  TRec& operator[](const int& RecN) const {
23  Assert(Addr!=NULL); return Addr[RecN];}
24  TRec* operator()() const {return Addr;}
25 
26  bool Empty() const {return Addr==NULL;}
27 };
28 
30 // Pair
31 template <class TVal1, class TVal2>
32 class TPair{
33 public:
34  TVal1 Val1;
35  TVal2 Val2;
36 public:
37  TPair(): Val1(), Val2(){}
38  TPair(const TPair& Pair): Val1(Pair.Val1), Val2(Pair.Val2){}
39  TPair(const TVal1& _Val1, const TVal2& _Val2): Val1(_Val1), Val2(_Val2){}
40  explicit TPair(TSIn& SIn): Val1(SIn), Val2(SIn){}
41  void Save(TSOut& SOut) const {
42  Val1.Save(SOut); Val2.Save(SOut);}
43  void Load(TSIn& SIn) {Val1.Load(SIn); Val2.Load(SIn);}
44  void LoadXml(const PXmlTok& XmlTok, const TStr& Nm="");
45  void SaveXml(TSOut& SOut, const TStr& Nm) const;
46 
47  TPair& operator=(const TPair& Pair){
48  if (this!=&Pair){Val1=Pair.Val1; Val2=Pair.Val2;} return *this;}
49  bool operator==(const TPair& Pair) const {
50  return (Val1==Pair.Val1)&&(Val2==Pair.Val2);}
51  bool operator<(const TPair& Pair) const {
52  return (Val1<Pair.Val1)||((Val1==Pair.Val1)&&(Val2<Pair.Val2));}
53 
54  int GetMemUsed() const {return Val1.GetMemUsed()+Val2.GetMemUsed();}
55 
56  int GetPrimHashCd() const {return TPairHashImpl::GetHashCd(Val1.GetPrimHashCd(), Val2.GetPrimHashCd()); }
57  int GetSecHashCd() const {return TPairHashImpl::GetHashCd(Val2.GetSecHashCd(), Val1.GetSecHashCd()); }
58 
59  void GetVal(TVal1& _Val1, TVal2& _Val2) const {_Val1=Val1; _Val2=Val2;}
60  const TVal1& GetVal1() const { return Val1;}
61  const TVal2& GetVal2() const { return Val2;}
62  TStr GetStr() const {
63  return TStr("Pair(")+Val1.GetStr()+", "+Val2.GetStr()+")";}
64 };
65 
66 template <class TVal1, class TVal2, class TSizeTy>
67 void GetSwitchedPrV(const TVec<TPair<TVal1, TVal2>, TSizeTy>& SrcPrV, TVec<TPair<TVal2, TVal1>, TSizeTy>& DstPrV){
68  const TSizeTy Prs = SrcPrV.Len();
69  DstPrV.Gen(Prs, 0);
70  for (TSizeTy PrN=0; PrN<Prs; PrN++){
71  const TPair<TVal1, TVal2>& SrcPr=SrcPrV[PrN];
72  DstPrV.Add(TPair<TVal2, TVal1>(SrcPr.Val2, SrcPr.Val1));
73  }
74 }
75 
113 
115 template <class TVal1, class TVal2>
117 private:
118  bool IsAsc;
119 public:
120  TCmpPairByVal2(const bool& AscSort=true) : IsAsc(AscSort) { }
121  bool operator () (const TPair<TVal1, TVal2>& P1, const TPair<TVal1, TVal2>& P2) const {
122  if (IsAsc) { return P1.Val2 < P2.Val2; } else { return P2.Val2 < P1.Val2; }
123  }
124 };
125 
127 // Triple
128 template <class TVal1, class TVal2, class TVal3>
129 class TTriple{
130 public:
131  TVal1 Val1;
132  TVal2 Val2;
133  TVal3 Val3;
134 public:
135  TTriple(): Val1(), Val2(), Val3(){}
136  TTriple(const TTriple& Triple):
137  Val1(Triple.Val1), Val2(Triple.Val2), Val3(Triple.Val3){}
138  TTriple(const TVal1& _Val1, const TVal2& _Val2, const TVal3& _Val3):
139  Val1(_Val1), Val2(_Val2), Val3(_Val3){}
140  explicit TTriple(TSIn& SIn): Val1(SIn), Val2(SIn), Val3(SIn){}
141  void Save(TSOut& SOut) const {
142  Val1.Save(SOut); Val2.Save(SOut); Val3.Save(SOut);}
143  void LoadXml(const PXmlTok& XmlTok, const TStr& Nm="");
144  void SaveXml(TSOut& SOut, const TStr& Nm) const;
145 
146  TTriple& operator=(const TTriple& Triple){
147  if (this!=&Triple){Val1=Triple.Val1; Val2=Triple.Val2; Val3=Triple.Val3;}
148  return *this;}
149  bool operator==(const TTriple& Triple) const {
150  return (Val1==Triple.Val1)&&(Val2==Triple.Val2)&&(Val3==Triple.Val3);}
151  bool operator<(const TTriple& Triple) const {
152  return (Val1<Triple.Val1)||((Val1==Triple.Val1)&&(Val2<Triple.Val2))||
153  ((Val1==Triple.Val1)&&(Val2==Triple.Val2)&&(Val3<Triple.Val3));}
154 
155  int GetPrimHashCd() const {return TPairHashImpl::GetHashCd(TPairHashImpl::GetHashCd(Val1.GetPrimHashCd(), Val2.GetPrimHashCd()), Val3.GetPrimHashCd()); }
156  int GetSecHashCd() const {return TPairHashImpl::GetHashCd(TPairHashImpl::GetHashCd(Val2.GetSecHashCd(), Val3.GetSecHashCd()), Val1.GetSecHashCd()); }
157  int GetMemUsed() const {return Val1.GetMemUsed()+Val2.GetMemUsed()+Val3.GetMemUsed();}
158 
159  void GetVal(TVal1& _Val1, TVal2& _Val2, TVal3& _Val3) const {
160  _Val1=Val1; _Val2=Val2; _Val3=Val3;}
161 
162  const TVal1& GetVal1() const { return Val1;}
163  const TVal2& GetVal2() const { return Val2;}
164  const TVal3& GetVal3() const { return Val3;}
165 };
166 
190 
192 template <class TVal1, class TVal2, class TVal3>
194 private:
195  bool IsAsc;
196 public:
197  TCmpTripleByVal2(const bool& AscSort=true) : IsAsc(AscSort) { }
199  if (IsAsc) { return T1.Val2 < T2.Val2; } else { return T2.Val2 < T1.Val2; }
200  }
201 };
202 
204 template <class TVal1, class TVal2, class TVal3>
206 private:
207  bool IsAsc;
208 public:
209  TCmpTripleByVal3(const bool& AscSort=true) : IsAsc(AscSort) { }
211  if (IsAsc) { return T1.Val3 < T2.Val3; } else { return T2.Val3 < T1.Val3; }
212  }
213 };
214 
216 // Quad
217 template <class TVal1, class TVal2, class TVal3, class TVal4>
218 class TQuad{
219 public:
220  TVal1 Val1;
221  TVal2 Val2;
222  TVal3 Val3;
223  TVal4 Val4;
224 public:
225  TQuad():
226  Val1(), Val2(), Val3(), Val4(){}
227  TQuad(const TQuad& Quad):
228  Val1(Quad.Val1), Val2(Quad.Val2), Val3(Quad.Val3), Val4(Quad.Val4){}
229  TQuad(const TVal1& _Val1, const TVal2& _Val2, const TVal3& _Val3, const TVal4& _Val4):
230  Val1(_Val1), Val2(_Val2), Val3(_Val3), Val4(_Val4){}
231  explicit TQuad(TSIn& SIn):
232  Val1(SIn), Val2(SIn), Val3(SIn), Val4(SIn){}
233  void Save(TSOut& SOut) const {
234  Val1.Save(SOut); Val2.Save(SOut); Val3.Save(SOut); Val4.Save(SOut);}
235  void LoadXml(const PXmlTok& XmlTok, const TStr& Nm="");
236  void SaveXml(TSOut& SOut, const TStr& Nm) const;
237 
238  TQuad& operator=(const TQuad& Quad){
239  if (this!=&Quad){
240  Val1=Quad.Val1; Val2=Quad.Val2; Val3=Quad.Val3; Val4=Quad.Val4;}
241  return *this;}
242  bool operator==(const TQuad& Quad) const {
243  return (Val1==Quad.Val1)&&(Val2==Quad.Val2)&&(Val3==Quad.Val3)&&(Val4==Quad.Val4);}
244  bool operator<(const TQuad& Quad) const {
245  return (Val1<Quad.Val1)||((Val1==Quad.Val1)&&(Val2<Quad.Val2))||
246  ((Val1==Quad.Val1)&&(Val2==Quad.Val2)&&(Val3<Quad.Val3))||
247  ((Val1==Quad.Val1)&&(Val2==Quad.Val2)&&(Val3==Quad.Val3)&&(Val4<Quad.Val4));}
248 
249  int GetPrimHashCd() const {return TPairHashImpl::GetHashCd(TPairHashImpl::GetHashCd(Val1.GetPrimHashCd(), Val2.GetPrimHashCd()), TPairHashImpl::GetHashCd(Val3.GetPrimHashCd(), Val4.GetPrimHashCd())); }
250  int GetSecHashCd() const {return TPairHashImpl::GetHashCd(TPairHashImpl::GetHashCd(Val2.GetSecHashCd(), Val3.GetSecHashCd()), TPairHashImpl::GetHashCd(Val4.GetSecHashCd(), Val1.GetSecHashCd())); }
251 
252  void GetVal(TVal1& _Val1, TVal2& _Val2, TVal3& _Val3, TVal4& _Val4) const {
253  _Val1=Val1; _Val2=Val2; _Val3=Val3; _Val4=Val4;}
254  const TVal1& GetVal1() const { return Val1;}
255  const TVal2& GetVal2() const { return Val2;}
256  const TVal3& GetVal3() const { return Val3;}
257  const TVal4& GetVal4() const { return Val4;}
258 };
259 
267 
269 // Tuple
270 template<class TVal, int NVals>
271 class TTuple {
272 private:
273  TVal ValV [NVals];
274 public:
275  TTuple(){}
276  TTuple(const TVal& InitVal) { for (int i=0; i<Len(); i++) ValV[i]=InitVal; }
277  TTuple(const TTuple& Tup) { for (int i=0; i<Len(); i++) ValV[i]=Tup[i]; }
278  TTuple(TSIn& SIn) { for (int i=0; i<Len(); i++) ValV[i].Load(SIn); }
279  void Save(TSOut& SOut) const { for (int i=0; i<Len(); i++) ValV[i].Save(SOut); }
280  void Load(TSIn& SIn) { for (int i=0; i<Len(); i++) ValV[i].Load(SIn); }
281 
282  int Len() const { return NVals; }
283  TVal& operator[] (const int& ValN) { return ValV[ValN]; }
284  const TVal& operator[] (const int& ValN) const { return ValV[ValN]; }
285  TTuple& operator = (const TTuple& Tup) { if (this != & Tup) {
286  for (int i=0; i<Len(); i++) ValV[i]=Tup[i]; } return *this; }
287  bool operator == (const TTuple& Tup) const {
288  if (Len()!=Tup.Len()) { return false; } if (&Tup==this) { return true; }
289  for (int i=0; i<Len(); i++) if(ValV[i]!=Tup[i]){return false;} return true; }
290  bool operator < (const TTuple& Tup) const {
291  if (Len() == Tup.Len()) { for (int i=0; i<Len(); i++) {
292  if(ValV[i]<Tup[i]){return true;} else if(ValV[i]>Tup[i]){return false;} } return false; }
293  else { return Len() < Tup.Len(); } }
294  void Sort(const bool& Asc=true);
295  int FindMx() const;
296  int FindMn() const;
297  int GetPrimHashCd() const { int hc = 0;
298  for (int i = 0; i < NVals; i++) { hc = TPairHashImpl::GetHashCd(hc, ValV[i].GetPrimHashCd()); }
299  return hc; }
300  int GetSecHashCd() const { int hc = 0;
301  for (int i = 1; i < NVals; i++) { hc = TPairHashImpl::GetHashCd(hc, ValV[i].GetSecHashCd()); }
302  if (NVals > 0) { hc = TPairHashImpl::GetHashCd(hc, ValV[0].GetSecHashCd()); }
303  return hc; }
304 
305  TStr GetStr() const { TChA ValsStr;
306  for (int i=0; i<Len(); i++) { ValsStr+=" "+ValV[i].GetStr(); }
307  return TStr::Fmt("Tuple(%d):", Len())+ValsStr; }
308 };
309 
310 template<class TVal, int NVals>
311 void TTuple<TVal, NVals>::Sort(const bool& Asc) {
312  TVec<TVal, int> V(NVals);
313  for (int i=0; i<NVals; i++) { V.Add(ValV[i]); }
314  V.Sort(Asc);
315  for (int i=0; i<NVals; i++) { ValV[i] = V[i]; }
316 }
317 
318 template<class TVal, int NVals>
320  TVal MxVal = ValV[0];
321  int ValN = 0;
322  for (int i = 1; i < NVals; i++) {
323  if (MxVal<ValV[i]) {
324  MxVal=ValV[i]; ValN=i;
325  }
326  }
327  return ValN;
328 }
329 
330 template<class TVal, int NVals>
332  TVal MnVal = ValV[0];
333  int ValN = 0;
334  for (int i = 1; i < NVals; i++) {
335  if (MnVal>ValV[i]) {
336  MnVal=ValV[i]; ValN=i;
337  }
338  }
339  return ValN;
340 }
341 
343 // Key-Data
344 template <class TKey, class TDat>
345 class TKeyDat{
346 public:
347  TKey Key;
348  TDat Dat;
349 public:
350  TKeyDat(): Key(), Dat(){}
351  TKeyDat(const TKeyDat& KeyDat): Key(KeyDat.Key), Dat(KeyDat.Dat){}
352  explicit TKeyDat(const TKey& _Key): Key(_Key), Dat(){}
353  TKeyDat(const TKey& _Key, const TDat& _Dat): Key(_Key), Dat(_Dat){}
354  explicit TKeyDat(TSIn& SIn): Key(SIn), Dat(SIn){}
355  void Save(TSOut& SOut) const {Key.Save(SOut); Dat.Save(SOut);}
356  void LoadXml(const PXmlTok& XmlTok, const TStr& Nm="");
357  void SaveXml(TSOut& SOut, const TStr& Nm) const;
358 
359  TKeyDat& operator=(const TKeyDat& KeyDat){
360  if (this!=&KeyDat){Key=KeyDat.Key; Dat=KeyDat.Dat;} return *this;}
361  bool operator==(const TKeyDat& KeyDat) const {return Key==KeyDat.Key;}
362  bool operator<(const TKeyDat& KeyDat) const {return Key<KeyDat.Key;}
363 
364  int GetPrimHashCd() const {return Key.GetPrimHashCd();}
365  int GetSecHashCd() const {return Key.GetSecHashCd();}
366 };
367 
368 template <class TKey, class TDat>
369 void GetSwitchedKdV(const TVec<TKeyDat<TKey, TDat>, int>& SrcKdV, TVec<TKeyDat<TDat, TKey>, int>& DstKdV){
370  const int Kds=SrcKdV.Len();
371  DstKdV.Gen(Kds, 0);
372  for (int KdN=0; KdN<Kds; KdN++){
373  const TKeyDat<TKey, TDat>& SrcKd=SrcKdV[KdN];
374  DstKdV.Add(TKeyDat<TDat, TKey>(SrcKd.Dat, SrcKd.Key));
375  }
376 }
377 
405 
406 // Key-Data comparator
407 
408 template <class TVal1, class TVal2>
410 private:
411  bool IsAsc;
412 public:
413  TCmpKeyDatByDat(const bool& AscSort=true) : IsAsc(AscSort) { }
414  bool operator () (const TKeyDat<TVal1, TVal2>& P1, const TKeyDat<TVal1, TVal2>& P2) const {
415  if (IsAsc) { return P1.Dat < P2.Dat; } else { return P2.Dat < P1.Dat; }
416  }
417 };
418 
419 //#//////////////////////////////////////////////
421 
428 template <class TVal, class TSizeTy = int>
429 class TVec{
430 public:
431  typedef TVal* TIter;
432 protected:
433  TSizeTy MxVals;
434  TSizeTy Vals;
435  TVal* ValT;
436  void Resize(const TSizeTy& _MxVals=-1);
439  TStr GetXOutOfBoundsErrMsg(const TSizeTy& ValN) const;
440 public:
441  TVec(): MxVals(0), Vals(0), ValT(NULL){}
442  TVec(const TVec<TVal, TSizeTy>& Vec);
444  explicit TVec(const TSizeTy& _Vals){
445  IAssert(0<=_Vals); MxVals=Vals=_Vals;
446  if (_Vals==0){ValT=NULL;} else {ValT=new TVal[_Vals];}}
448  TVec(const TSizeTy& _MxVals, const TSizeTy& _Vals){
449  IAssert((0<=_Vals)&&(_Vals<=_MxVals)); MxVals=_MxVals; Vals=_Vals;
450  if (_MxVals==0){ValT=NULL;} else {ValT=new TVal[_MxVals];}}
452 
455  explicit TVec(TVal *_ValT, const TSizeTy& _Vals):
456  MxVals(-1), Vals(_Vals), ValT(_ValT){}
457  ~TVec(){if ((ValT!=NULL) && (MxVals!=-1)){delete[] ValT;}}
458  explicit TVec(TSIn& SIn): MxVals(0), Vals(0), ValT(NULL){Load(SIn);}
459  void Load(TSIn& SIn);
460  void Save(TSOut& SOut) const;
461  void LoadXml(const PXmlTok& XmlTok, const TStr& Nm="");
462  void SaveXml(TSOut& SOut, const TStr& Nm) const;
463 
467  TVec<TVal, TSizeTy>& operator+(const TVal& Val){Add(Val); return *this;}
469  bool operator==(const TVec<TVal, TSizeTy>& Vec) const;
471 
473  bool operator<(const TVec<TVal, TSizeTy>& Vec) const;
475  const TVal& operator[](const TSizeTy& ValN) const {
476  AssertR((0<=ValN)&&(ValN<Vals), GetXOutOfBoundsErrMsg(ValN));
477  return ValT[ValN];}
479  TVal& operator[](const TSizeTy& ValN){
480  AssertR((0<=ValN)&&(ValN<Vals), GetXOutOfBoundsErrMsg(ValN));
481  return ValT[ValN];}
483  TSizeTy GetMemUsed() const {
484  return TSizeTy(2*sizeof(TSizeTy)+sizeof(TVal*)+MxVals*sizeof(TVal));}
486  TSizeTy GetMemSize() const {
487  return TSizeTy(2*sizeof(TVal)+sizeof(TSizeTy)*Vals);}
488 
490  int GetPrimHashCd() const;
492  int GetSecHashCd() const;
493 
495  void Gen(const TSizeTy& _Vals){ IAssert(0<=_Vals);
496  if (ValT!=NULL && MxVals!=-1){delete[] ValT;} MxVals=Vals=_Vals;
497  if (MxVals==0){ValT=NULL;} else {ValT=new TVal[MxVals];}}
499  void Gen(const TSizeTy& _MxVals, const TSizeTy& _Vals){ IAssert((0<=_Vals)&&(_Vals<=_MxVals));
500  if (ValT!=NULL && MxVals!=-1){delete[] ValT;} MxVals=_MxVals; Vals=_Vals;
501  if (_MxVals==0){ValT=NULL;} else {ValT=new TVal[_MxVals];}}
503 
506  void GenExt(TVal *_ValT, const TSizeTy& _Vals){
507  if (ValT!=NULL && MxVals!=-1){delete[] ValT;}
508  MxVals=-1; Vals=_Vals; ValT=_ValT;}
510 
513  bool IsExt() const {return MxVals==-1;}
515  void Reserve(const TSizeTy& _MxVals){Resize(_MxVals);}
517  void Reserve(const TSizeTy& _MxVals, const TSizeTy& _Vals){ IAssert((0<=_Vals)&&(_Vals<=_MxVals)); Resize(_MxVals); Vals=_Vals;}
519 
522  void Clr(const bool& DoDel=true, const TSizeTy& NoDelLim=-1);
524 
526  void Trunc(const TSizeTy& _Vals=-1);
528  void Reduce(const TSizeTy& _Vals=-1) {Vals = _Vals;}
530  void Pack();
532 
534  void MoveFrom(TVec<TVal, TSizeTy>& Vec);
536  void CopyUniqueFrom(TVec<TVal, TSizeTy>& Vec, TInt Offset, TInt Sz);
538  void Swap(TVec<TVal, TSizeTy>& Vec);
540 
542  bool Empty() const {return Vals==0;}
544 
547  TSizeTy Len() const {return Vals;}
549  TSizeTy Reserved() const {return MxVals;}
551  const TVal& Last() const {return GetVal(Len()-1);}
553  TVal& Last(){return GetVal(Len()-1);}
555  TSizeTy LastValN() const {return Len()-1;}
557  const TVal& LastLast() const { AssertR(1<Vals, GetXOutOfBoundsErrMsg(Vals-2)); return ValT[Vals-2];}
559  TVal& LastLast(){ AssertR(1<Vals, GetXOutOfBoundsErrMsg(Vals-2)); return ValT[Vals-2];}
561  const TVal& GetRndVal(TRnd& Rnd=TInt::Rnd) const { return GetVal(Rnd.GetUniDevInt(Len())); }
563  TVal& GetRndVal(TRnd& Rnd=TInt::Rnd) { return GetVal(Rnd.GetUniDevInt(Len())); }
565  TIter BegI() const {return ValT;}
567  TIter EndI() const {return ValT+Vals;}
569  TIter GetI(const TSizeTy& ValN) const {return ValT+ValN;}
570 
572 
574  TSizeTy Add(){ AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
575  if (Vals==MxVals){Resize();} return Vals++;}
576 
578 
580  TSizeTy Add(const TVal& Val){ AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
581  if (Vals==MxVals){Resize();} ValT[Vals]=Val; return Vals++;}
582  TSizeTy Add(TVal& Val){ AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
583  if (Vals==MxVals){Resize();} ValT[Vals]=Val; return Vals++;}
585  TSizeTy Add(const TVal& Val, const TSizeTy& ResizeLen){ AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
586  if (Vals==MxVals){Resize(MxVals+ResizeLen);} ValT[Vals]=Val; return Vals++;}
587 #ifdef USE_OPENMP
588  TSizeTy AddMP(const TVal& Val){ const int Idx = __sync_fetch_and_add(&Vals, 1);
590  ValT[Idx]=Val; return Idx;}
592 
594  TSizeTy MoveLastMP(const TVal& Val, int Inc){ const int Idx = __sync_fetch_and_add(&Vals, Inc);
595  return Idx;}
596 #endif
597  TSizeTy AddV(const TVec<TVal, TSizeTy>& ValV);
600 
602  TSizeTy AddSorted(const TVal& Val, const bool& Asc=true, const TSizeTy& _MxVals=-1);
604 
606  TSizeTy AddBackSorted(const TVal& Val, const bool& Asc);
608 
610  TSizeTy AddMerged(const TVal& Val);
612 
614  TSizeTy AddVMerged(const TVec<TVal, TSizeTy>& ValV);
616 
619  TSizeTy AddUnique(const TVal& Val);
621  const TVal& GetVal(const TSizeTy& ValN) const {return operator[](ValN);}
623  TVal& GetVal(const TSizeTy& ValN){return operator[](ValN);}
625  void SetVal(const TSizeTy& ValN, const TVal& Val){AssertR((0<=ValN)&&(ValN<Vals), GetXOutOfBoundsErrMsg(ValN)); ValT[ValN] = Val;}
627  void GetSubValV(const TSizeTy& BValN, const TSizeTy& EValN, TVec<TVal, TSizeTy>& ValV) const;
629  void Ins(const TSizeTy& ValN, const TVal& Val);
631  void Del(const TSizeTy& ValN);
633  void Del(const TSizeTy& MnValN, const TSizeTy& MxValN);
635  void DelLast(){Del(Len()-1);}
637  bool DelIfIn(const TVal& Val);
639  void DelAll(const TVal& Val);
641  void PutAll(const TVal& Val);
642 
644  void Swap(const TSizeTy& ValN1, const TSizeTy& ValN2){ const TVal Val=ValT[ValN1]; ValT[ValN1]=ValT[ValN2]; ValT[ValN2]=Val;}
646  static void SwapI(TIter LVal, TIter RVal){ const TVal Val=*LVal; *LVal=*RVal; *RVal=Val;}
647 
649 
653  bool NextPerm();
655 
657  bool PrevPerm();
658 
659  // Sorting functions
661  TSizeTy GetPivotValN(const TSizeTy& LValN, const TSizeTy& RValN) const;
663 
665  void BSort(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc);
667 
669  void ISort(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc);
671 
674  TSizeTy Partition(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc);
676 
679  void QSort(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc);
681 
684  void Sort(const bool& Asc=true);
686  bool IsSorted(const bool& Asc=true) const;
688  void Shuffle(TRnd& Rnd);
690  void Reverse();
692  void Reverse(TSizeTy LValN, TSizeTy RValN){ Assert(LValN>=0 && RValN<Len()); while (LValN < RValN){Swap(LValN++, RValN--);} }
694  void Merge();
695 
697  template <class TCmp>
698  static TIter GetPivotValNCmp(const TIter& BI, const TIter& EI, const TCmp& Cmp) {
699  TSizeTy SubVals=TSizeTy(EI-BI); if (SubVals > TInt::Mx-1) { SubVals = TInt::Mx-1; }
700  const TSizeTy ValN1=TInt::GetRnd(SubVals), ValN2=TInt::GetRnd(SubVals), ValN3=TInt::GetRnd(SubVals);
701  const TVal& Val1 = *(BI+ValN1); const TVal& Val2 = *(BI+ValN2); const TVal& Val3 = *(BI+ValN3);
702  if (Cmp(Val1, Val2)) {
703  if (Cmp(Val2, Val3)) return BI+ValN2;
704  else if (Cmp(Val3, Val1)) return BI+ValN1;
705  else return BI+ValN3;
706  } else {
707  if (Cmp(Val1, Val3)) return BI+ValN1;
708  else if (Cmp(Val3, Val2)) return BI+ValN2;
709  else return BI+ValN3; } }
711  template <class TCmp>
712  static TIter PartitionCmp(TIter BI, TIter EI, const TVal Pivot, const TCmp& Cmp) {
713  forever {
714  while (Cmp(*BI, Pivot)){++BI;} --EI;
715  while (Cmp(Pivot, *EI)){--EI;}
716  if (!(BI<EI)){return BI;} SwapI(BI, EI); ++BI; } }
718  template <class TCmp>
719  static void BSortCmp(TIter BI, TIter EI, const TCmp& Cmp) {
720  for (TIter i = BI; i != EI; ++i) {
721  for (TIter j = EI-1; j != i; --j) {
722  if (Cmp(*j, *(j-1))) { SwapI(j, j-1); } } } }
724  template <class TCmp>
725  static void ISortCmp(TIter BI, TIter EI, const TCmp& Cmp) {
726  if (BI + 1 < EI) {
727  for (TIter i = BI, j; i != EI; ++i) { TVal Tmp=*i; j=i;
728  while (j > BI && Cmp(Tmp, *(j-1))) { *j = *(j-1); --j; } *j=Tmp; } } }
730  template <class TCmp>
731  static void QSortCmp(TIter BI, TIter EI, const TCmp& Cmp) {
732  if (BI + 1 < EI) {
733  if (EI - BI < 20) { ISortCmp(BI, EI, Cmp); }
734  else { const TVal Val = *GetPivotValNCmp(BI, EI, Cmp);
735  TIter Split = PartitionCmp(BI, EI, Val, Cmp);
736  QSortCmp(BI, Split, Cmp); QSortCmp(Split, EI, Cmp); } } }
738  template <class TCmp>
739  void SortCmp(const TCmp& Cmp){ QSortCmp(BegI(), EndI(), Cmp);}
741  template <class TCmp>
742  bool IsSortedCmp(const TCmp& Cmp) const {
743  if (EndI() == BegI()) return true;
744  for (TIter i = BegI(); i != EndI()-1; ++i) {
745  if (Cmp(*(i+1), *i)){return false;} } return true; }
747  void Intrs(const TVec<TVal, TSizeTy>& ValV);
749  void Union(const TVec<TVal, TSizeTy>& ValV);
751 
753  void Diff(const TVec<TVal, TSizeTy>& ValV);
755  void Intrs(const TVec<TVal, TSizeTy>& ValV, TVec<TVal, TSizeTy>& DstValV) const;
757  void Union(const TVec<TVal, TSizeTy>& ValV, TVec<TVal, TSizeTy>& DstValV) const;
759 
761  void Diff(const TVec<TVal, TSizeTy>& ValV, TVec<TVal, TSizeTy>& DstValV) const;
763  TSizeTy IntrsLen(const TVec<TVal, TSizeTy>& ValV) const;
765  TSizeTy UnionLen(const TVec<TVal, TSizeTy>& ValV) const;
766 
768  TSizeTy Count(const TVal& Val) const;
770 
773  TSizeTy SearchBin(const TVal& Val) const;
775 
777  TSizeTy SearchBin(const TVal& Val, TSizeTy& InsValN) const;
779 
781  TSizeTy SearchBinLeft(const TVal& Val, TSizeTy& InsValN) const;
783 
786  TSizeTy SearchForw(const TVal& Val, const TSizeTy& BValN=0) const;
788 
790  TSizeTy SearchBack(const TVal& Val) const;
792 
794  TSizeTy SearchVForw(const TVec<TVal, TSizeTy>& ValV, const TSizeTy& BValN=0) const;
795 
797  bool IsIn(const TVal& Val) const {return SearchForw(Val)!=-1;}
799 
801  bool IsIn(const TVal& Val, TSizeTy& ValN) const { ValN=SearchForw(Val); return ValN!=-1;}
803 
805  bool IsInBin(const TVal& Val) const {return SearchBin(Val)!=-1;}
807  const TVal& GetDat(const TVal& Val) const { return GetVal(SearchForw(Val));}
809 
811  TVal& GetAddDat(const TVal& Val){ AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
812  const TSizeTy ValN=SearchForw(Val); if (ValN==-1){Add(Val); return Last();} else {return GetVal(ValN);}}
814  TSizeTy GetMxValN() const;
815 
817  static TVec<TVal, TSizeTy> GetV(const TVal& Val1){
818  TVec<TVal, TSizeTy> V(1, 0); V.Add(Val1); return V;}
820  static TVec<TVal, TSizeTy> GetV(const TVal& Val1, const TVal& Val2){
821  TVec<TVal, TSizeTy> V(2, 0); V.Add(Val1); V.Add(Val2); return V;}
823  static TVec<TVal, TSizeTy> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3){
824  TVec<TVal, TSizeTy> V(3, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); return V;}
826  static TVec<TVal, TSizeTy> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4){
827  TVec<TVal, TSizeTy> V(4, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); return V;}
829  static TVec<TVal, TSizeTy> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4, const TVal& Val5){
830  TVec<TVal, TSizeTy> V(5, 0); V.Add(Val1); V.Add(Val2); V.Add(Val3); V.Add(Val4); V.Add(Val5); return V;}
832  static TVec<TVal, TSizeTy> GetV(const TVal& Val1, const TVal& Val2, const TVal& Val3, const TVal& Val4, const TVal& Val5, const TVal& Val6){
833  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;}
835  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){
836  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;}
838  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){
839  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;}
841  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){
842  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;}
843 };
844 
845 template <class TVal, class TSizeTy>
846 void TVec<TVal, TSizeTy>::Resize(const TSizeTy& _MxVals){
847  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());
848  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());
849  if (_MxVals==-1){
850  if (Vals==0){MxVals=16;} else {MxVals*=2;}
851  } else {
852  if (_MxVals<=MxVals){return;} else {MxVals=_MxVals;}
853  }
854  if (MxVals < 0) {
855  MxVals = TInt::Mx-1024;
856  }
857  if (ValT==NULL){
858  try {
859  ValT=new TVal[MxVals];
860  }
861  catch (std::exception Ex){
862  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.]",
863  Ex.what(), TInt::GetStr(Vals).CStr(), TInt::GetStr(MxVals).CStr(), TInt::GetStr(_MxVals).CStr(), GetTypeNm(*this).CStr()).CStr());}
864  } else {
865  TVal* NewValT = NULL;
866  try {
867  NewValT=new TVal[MxVals];
868  }
869  catch (std::exception Ex){
870  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.]",
871  Ex.what(), TInt::GetStr(Vals).CStr(), TInt::GetStr(MxVals).CStr(), TInt::GetStr(_MxVals).CStr(), GetTypeNm(*this).CStr()).CStr());}
872  IAssert(NewValT!=NULL);
873  for (TSizeTy ValN=0; ValN<Vals; ValN++){NewValT[ValN]=ValT[ValN];}
874  delete[] ValT; ValT=NewValT;
875  }
876 }
877 
878 template <class TVal, class TSizeTy>
880  return TStr()+
881  "Index:"+TInt::GetStr(ValN)+
882  " Vals:"+TInt::GetStr(Vals)+
883  " MxVals:"+TInt::GetStr(MxVals)+
884  " Type:"+GetTypeNm(*this);
885 }
886 
887 template <class TVal, class TSizeTy>
889  MxVals=Vec.MxVals; Vals=Vec.Vals;
890  if (MxVals==0){ValT=NULL;} else {ValT=new TVal[MxVals];}
891  for (TSizeTy ValN=0; ValN<Vec.Vals; ValN++){ValT[ValN]=Vec.ValT[ValN];}
892 }
893 
894 template <class TVal, class TSizeTy>
896  if ((ValT!=NULL)&&(MxVals!=-1)){delete[] ValT;}
897  SIn.Load(MxVals); SIn.Load(Vals); MxVals=Vals;
898  if (MxVals==0){ValT=NULL;} else {ValT=new TVal[MxVals];}
899  for (TSizeTy ValN=0; ValN<Vals; ValN++){ValT[ValN]=TVal(SIn);}
900 }
901 
902 template <class TVal, class TSizeTy>
903 void TVec<TVal, TSizeTy>::Save(TSOut& SOut) const {
904  if (MxVals!=-1){SOut.Save(MxVals);} else {SOut.Save(Vals);}
905  SOut.Save(Vals);
906  for (TSizeTy ValN=0; ValN<Vals; ValN++){ValT[ValN].Save(SOut);}
907 }
908 
909 template <class TVal, class TSizeTy>
911  if (this!=&Vec){
912  if ((ValT!=NULL)&&(MxVals!=-1)){delete[] ValT;}
913  MxVals=Vals=Vec.Vals;
914  if (MxVals==0){ValT=NULL;} else {ValT=new TVal[MxVals];}
915  for (TSizeTy ValN=0; ValN<Vec.Vals; ValN++){ValT[ValN]=Vec.ValT[ValN];}
916  }
917  return *this;
918 }
919 
920 template <class TVal, class TSizeTy>
922  if (this==&Vec){return true;}
923  if (Len()!=Vec.Len()){return false;}
924  for (TSizeTy ValN=0; ValN<Vals; ValN++){
925  if (ValT[ValN]!=Vec.ValT[ValN]){return false;}}
926  return true;
927 }
928 
929 template <class TVal, class TSizeTy>
931  if (this==&Vec){return false;}
932  if (Len()==Vec.Len()){
933  for (TSizeTy ValN=0; ValN<Vals; ValN++){
934  if (ValT[ValN]<Vec.ValT[ValN]){return true;}
935  else if (ValT[ValN]>Vec.ValT[ValN]){return false;}
936  else {}
937  }
938  return false;
939  } else {
940  return Len()<Vec.Len();
941  }
942 }
943 
944 // Improved hashing of vectors (Jure Apr 20 2013)
945 // This change makes binary representation of vectors incompatible with previous code.
946 // Previous hash functions are available for compatibility in class TVecHashF_OldGLib
947 template <class TVal, class TSizeTy>
949  int hc = 0;
950  for (TSizeTy i=0; i<Vals; i++){
951  hc = TPairHashImpl::GetHashCd(hc, ValT[i].GetPrimHashCd());
952  }
953  return hc;
954 }
955 
956 // Improved hashing of vectors (Jure Apr 20 2013)
957 // This change makes binary representation of vectors incompatible with previous code.
958 // Previous hash functions are available for compatibility in class TVecHashF_OldGLib
959 template <class TVal, class TSizeTy>
961  int hc = 0;
962  for (TSizeTy i=0; i<Vals; i++){
963  hc = TPairHashImpl::GetHashCd(hc, ValT[i].GetSecHashCd());
964  }
965  if (Vals > 0) {
966  hc = TPairHashImpl::GetHashCd(hc, ValT[0].GetSecHashCd()); }
967  return hc;
968 }
969 
970 template <class TVal, class TSizeTy>
971 void TVec<TVal, TSizeTy>::Clr(const bool& DoDel, const TSizeTy& NoDelLim){
972  if ((DoDel)||((!DoDel)&&(NoDelLim!=-1)&&(MxVals>NoDelLim))){
973  if ((ValT!=NULL)&&(MxVals!=-1)){delete[] ValT;}
974  MxVals=Vals=0; ValT=NULL;
975  } else {
976  IAssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
977  Vals=0;
978  }
979 }
980 
981 template <class TVal, class TSizeTy>
982 void TVec<TVal, TSizeTy>::Trunc(const TSizeTy& _Vals){
983  IAssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
984  IAssert((_Vals==-1)||(_Vals>=0));
985  if ((_Vals!=-1)&&(_Vals>=Vals)){
986  return;
987  } else
988  if (((_Vals==-1)&&(Vals==0))||(_Vals==0)){
989  if (ValT!=NULL){delete[] ValT;}
990  MxVals=Vals=0; ValT=NULL;
991  } else {
992  if (_Vals==-1){
993  if (MxVals==Vals){return;} else {MxVals=Vals;}
994  } else {
995  MxVals=Vals=_Vals;
996  }
997  TVal* NewValT=new TVal[MxVals];
998  IAssert(NewValT!=NULL);
999  for (TSizeTy ValN=0; ValN<Vals; ValN++){NewValT[ValN]=ValT[ValN];}
1000  delete[] ValT; ValT=NewValT;
1001  }
1002 }
1003 
1004 template <class TVal, class TSizeTy>
1006  IAssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
1007  if (Vals==0){
1008  if (ValT!=NULL){delete[] ValT;} ValT=NULL;
1009  } else
1010  if (Vals<MxVals){
1011  MxVals=Vals;
1012  TVal* NewValT=new TVal[MxVals];
1013  IAssert(NewValT!=NULL);
1014  for (TSizeTy ValN=0; ValN<Vals; ValN++){NewValT[ValN]=ValT[ValN];}
1015  delete[] ValT; ValT=NewValT;
1016  }
1017 }
1018 
1019 template <class TVal, class TSizeTy>
1021  if (this!=&Vec){
1022  if (ValT!=NULL && MxVals!=-1){delete[] ValT;}
1023  MxVals=Vec.MxVals; Vals=Vec.Vals; ValT=Vec.ValT;
1024  Vec.MxVals=0; Vec.Vals=0; Vec.ValT=NULL;
1025  }
1026 }
1027 
1028 template <class TVal, class TSizeTy>
1030  if (this!=&Vec){
1031  if (ValT!=NULL && MxVals!=-1 && MxVals < Sz){
1032  delete[] ValT;
1033  ValT=new TVal[Sz];
1034  }
1035  if (Sz == 0) { Vals = 0; return; }
1036  ValT[0] = Vec.ValT[Offset];
1037  Vals = 1;
1038  for (TSizeTy ValN=1; ValN<Sz; ValN++){
1039  if (ValT[Vals-1] != Vec.ValT[Offset+ValN]) {
1040  ValT[Vals++] = Vec.ValT[Offset+ValN];
1041  }
1042  }
1043  }
1044 }
1045 
1046 template <class TVal, class TSizeTy>
1048  if (this!=&Vec){
1049  ::Swap(MxVals, Vec.MxVals);
1050  ::Swap(Vals, Vec.Vals);
1051  ::Swap(ValT, Vec.ValT);
1052  }
1053 }
1054 
1055 template <class TVal, class TSizeTy>
1057  AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
1058  for (TSizeTy ValN=0; ValN<ValV.Vals; ValN++){Add(ValV[ValN]);}
1059  return Len();
1060 }
1061 
1062 template <class TVal, class TSizeTy>
1063 TSizeTy TVec<TVal, TSizeTy>::AddSorted(const TVal& Val, const bool& Asc, const TSizeTy& _MxVals){
1064  AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
1065  TSizeTy ValN=Add(Val);
1066  if (Asc){
1067  while ((ValN>0)&&(ValT[ValN]<ValT[ValN-1])){
1068  Swap(ValN, ValN-1); ValN--;}
1069  } else {
1070  while ((ValN>0)&&(ValT[ValN]>ValT[ValN-1])){
1071  Swap(ValN, ValN-1); ValN--;}
1072  }
1073  if ((_MxVals!=-1)&&(Len()>_MxVals)){Del(_MxVals, Len()-1);}
1074  return ValN;
1075 }
1076 
1077 template <class TVal, class TSizeTy>
1078 TSizeTy TVec<TVal, TSizeTy>::AddBackSorted(const TVal& Val, const bool& Asc){
1079  AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
1080  Add();
1081  TSizeTy ValN=Vals-2;
1082  while ((ValN>=0)&&((Asc&&(Val<ValT[ValN]))||(!Asc&&(Val>ValT[ValN])))){
1083  ValT[ValN+1]=ValT[ValN]; ValN--;}
1084  ValT[ValN+1]=Val;
1085  return ValN+1;
1086 }
1087 
1088 template <class TVal, class TSizeTy>
1089 TSizeTy TVec<TVal, TSizeTy>::AddMerged(const TVal& Val){
1090  AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
1091  TSizeTy ValN=SearchBin(Val);
1092  if (ValN==-1){return AddSorted(Val);}
1093  else {GetVal(ValN)=Val; return -1;}
1094 }
1095 
1096 template <class TVal, class TSizeTy>
1098  AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
1099  for (TSizeTy ValN=0; ValN<ValV.Vals; ValN++){AddMerged(ValV[ValN]);}
1100  return Len();
1101 }
1102 
1103 template <class TVal, class TSizeTy>
1104 TSizeTy TVec<TVal, TSizeTy>::AddUnique(const TVal& Val){
1105  AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
1106  TSizeTy ValN=SearchForw(Val);
1107  if (ValN==-1){return Add(Val);}
1108  else {GetVal(ValN)=Val; return -1;}
1109 }
1110 
1111 template <class TVal, class TSizeTy>
1112 void TVec<TVal, TSizeTy>::GetSubValV(const TSizeTy& _BValN, const TSizeTy& _EValN, TVec<TVal, TSizeTy>& SubValV) const {
1113  const TSizeTy BValN=TInt::GetInRng(_BValN, 0, Len()-1);
1114  const TSizeTy EValN=TInt::GetInRng(_EValN, 0, Len()-1);
1115  const TSizeTy SubVals=TInt::GetMx(0, EValN-BValN+1);
1116  SubValV.Gen(SubVals, 0);
1117  for (TSizeTy ValN=BValN; ValN<=EValN; ValN++){
1118  SubValV.Add(GetVal(ValN));}
1119 }
1120 
1121 template <class TVal, class TSizeTy>
1122 void TVec<TVal, TSizeTy>::Ins(const TSizeTy& ValN, const TVal& Val){
1123  AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
1124  Add(); Assert((0<=ValN)&&(ValN<Vals));
1125  for (TSizeTy MValN=Vals-2; MValN>=ValN; MValN--){ValT[MValN+1]=ValT[MValN];}
1126  ValT[ValN]=Val;
1127 }
1128 
1129 template <class TVal, class TSizeTy>
1130 void TVec<TVal, TSizeTy>::Del(const TSizeTy& ValN){
1131  AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
1132  Assert((0<=ValN)&&(ValN<Vals));
1133  for (TSizeTy MValN=ValN+1; MValN<Vals; MValN++){
1134  ValT[MValN-1]=ValT[MValN];}
1135  ValT[--Vals]=TVal();
1136 }
1137 
1138 template <class TVal, class TSizeTy>
1139 void TVec<TVal, TSizeTy>::Del(const TSizeTy& MnValN, const TSizeTy& MxValN){
1140  AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
1141  Assert((0<=MnValN)&&(MnValN<Vals)&&(0<=MxValN)&&(MxValN<Vals));
1142  Assert(MnValN<=MxValN);
1143  for (TSizeTy ValN=MxValN+1; ValN<Vals; ValN++){
1144  ValT[MnValN+ValN-MxValN-1]=ValT[ValN];}
1145  for (TSizeTy ValN=Vals-MxValN+MnValN-1; ValN<Vals; ValN++){
1146  ValT[ValN]=TVal();}
1147  Vals-=MxValN-MnValN+1;
1148 }
1149 
1150 template <class TVal, class TSizeTy>
1151 bool TVec<TVal, TSizeTy>::DelIfIn(const TVal& Val){
1152  AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
1153  TSizeTy ValN=SearchForw(Val);
1154  if (ValN!=-1){Del(ValN); return true;}
1155  else {return false;}
1156 }
1157 
1158 template <class TVal, class TSizeTy>
1159 void TVec<TVal, TSizeTy>::DelAll(const TVal& Val){
1160  AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
1161  TSizeTy ValN;
1162  while ((ValN=SearchForw(Val))!=-1){Del(ValN);}
1163 }
1164 
1165 template <class TVal, class TSizeTy>
1166 void TVec<TVal, TSizeTy>::PutAll(const TVal& Val){
1167  for (TSizeTy ValN=0; ValN<Vals; ValN++){ValT[ValN]=Val;}
1168 }
1169 
1170 template <class TVal, class TSizeTy>
1171 void TVec<TVal, TSizeTy>::BSort(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc){
1172  for (TSizeTy ValN1=MnLValN; ValN1<=MxRValN; ValN1++){
1173  for (TSizeTy ValN2=MxRValN; ValN2>ValN1; ValN2--){
1174  if (Asc){
1175  if (ValT[ValN2]<ValT[ValN2-1]){Swap(ValN2, ValN2-1);}
1176  } else {
1177  if (ValT[ValN2]>ValT[ValN2-1]){Swap(ValN2, ValN2-1);}
1178  }
1179  }
1180  }
1181 }
1182 
1183 template <class TVal, class TSizeTy>
1184 void TVec<TVal, TSizeTy>::ISort(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc){
1185  if (MnLValN<MxRValN){
1186  for (TSizeTy ValN1=MnLValN+1; ValN1<=MxRValN; ValN1++){
1187  TVal Val=ValT[ValN1]; TSizeTy ValN2=ValN1;
1188  if (Asc){
1189  while ((ValN2>MnLValN)&&(ValT[ValN2-1]>Val)){
1190  ValT[ValN2]=ValT[ValN2-1]; ValN2--;}
1191  } else {
1192  while ((ValN2>MnLValN)&&(ValT[ValN2-1]<Val)){
1193  ValT[ValN2]=ValT[ValN2-1]; ValN2--;}
1194  }
1195  ValT[ValN2]=Val;
1196  }
1197  }
1198 }
1199 
1200 template <class TVal, class TSizeTy>
1201 TSizeTy TVec<TVal, TSizeTy>::GetPivotValN(const TSizeTy& LValN, const TSizeTy& RValN) const {
1202  TSizeTy SubVals=RValN-LValN+1;
1203  if (SubVals > TInt::Mx-1) { SubVals = TInt::Mx-1; }
1204  const TSizeTy ValN1=LValN+TInt::GetRnd(int(SubVals));
1205  const TSizeTy ValN2=LValN+TInt::GetRnd(int(SubVals));
1206  const TSizeTy ValN3=LValN+TInt::GetRnd(int(SubVals));
1207  const TVal& Val1=ValT[ValN1];
1208  const TVal& Val2=ValT[ValN2];
1209  const TVal& Val3=ValT[ValN3];
1210  if (Val1<Val2){
1211  if (Val2<Val3){return ValN2;}
1212  else if (Val3<Val1){return ValN1;}
1213  else {return ValN3;}
1214  } else {
1215  if (Val1<Val3){return ValN1;}
1216  else if (Val3<Val2){return ValN2;}
1217  else {return ValN3;}
1218  }
1219 }
1220 
1221 template <class TVal, class TSizeTy>
1222 TSizeTy TVec<TVal, TSizeTy>::Partition(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc){
1223  TSizeTy PivotValN=GetPivotValN(MnLValN, MxRValN);
1224  Swap(PivotValN, MnLValN);
1225  TVal PivotVal=ValT[MnLValN];
1226  TSizeTy LValN=MnLValN-1; TSizeTy RValN=MxRValN+1;
1227  forever {
1228  if (Asc){
1229  do {RValN--;} while (ValT[RValN]>PivotVal);
1230  do {LValN++;} while (ValT[LValN]<PivotVal);
1231  } else {
1232  do {RValN--;} while (ValT[RValN]<PivotVal);
1233  do {LValN++;} while (ValT[LValN]>PivotVal);
1234  }
1235  if (LValN<RValN){Swap(LValN, RValN);}
1236  else {return RValN;}
1237  };
1238 }
1239 
1240 template <class TVal, class TSizeTy>
1241 void TVec<TVal, TSizeTy>::QSort(const TSizeTy& MnLValN, const TSizeTy& MxRValN, const bool& Asc){
1242  if (MnLValN<MxRValN){
1243  if (MxRValN-MnLValN<20){
1244  ISort(MnLValN, MxRValN, Asc);
1245  } else {
1246  TSizeTy SplitValN=Partition(MnLValN, MxRValN, Asc);
1247  QSort(MnLValN, SplitValN, Asc);
1248  QSort(SplitValN+1, MxRValN, Asc);
1249  }
1250  }
1251 }
1252 
1253 template <class TVal, class TSizeTy>
1254 void TVec<TVal, TSizeTy>::Sort(const bool& Asc){
1255  QSort(0, Len()-1, Asc);
1256 }
1257 
1258 template <class TVal, class TSizeTy>
1259 bool TVec<TVal, TSizeTy>::IsSorted(const bool& Asc) const {
1260  if (Asc){
1261  for (TSizeTy ValN=0; ValN<Vals-1; ValN++){
1262  if (ValT[ValN]>ValT[ValN+1]){return false;}}
1263  } else {
1264  for (TSizeTy ValN=0; ValN<Vals-1; ValN++){
1265  if (ValT[ValN]<ValT[ValN+1]){return false;}}
1266  }
1267  return true;
1268 }
1269 
1270 template <class TVal, class TSizeTy>
1272  if (Len() < TInt::Mx) {
1273  for (TSizeTy ValN=0; ValN<Vals-1; ValN++){
1274  const int Range = int(Vals-ValN);
1275  Swap(ValN, ValN+Rnd.GetUniDevInt(Range));
1276  }
1277  } else {
1278  for (TSizeTy ValN=0; ValN<Vals-1; ValN++){
1279  const TSizeTy Range = Vals-ValN;
1280  Swap(ValN, TSizeTy(ValN+Rnd.GetUniDevInt64(Range)));
1281  }
1282  }
1283 }
1284 
1285 template <class TVal, class TSizeTy>
1287  for (TSizeTy ValN=0; ValN<Vals/2; ValN++){
1288  Swap(ValN, Vals-ValN-1);}
1289 }
1290 
1291 template <class TVal, class TSizeTy>
1293  AssertR(MxVals!=-1, "This vector was obtained from TVecPool. Such vectors cannot change its size!");
1294  TVec<TVal, TSizeTy> SortedVec(*this); SortedVec.Sort();
1295  Clr();
1296  for (TSizeTy ValN=0; ValN<SortedVec.Len(); ValN++){
1297  if ((ValN==0)||(SortedVec[ValN-1]!=SortedVec[ValN])){
1298  Add(SortedVec[ValN]);}
1299  }
1300 }
1301 
1302 template <class TVal, class TSizeTy>
1304  // start with a sorted sequence to obtain all permutations
1305  TSizeTy First = 0, Last = Len(), Next = Len()-1;
1306  if (Last < 2) return false;
1307  for(; ; ) {
1308  // find rightmost element smaller than successor
1309  TSizeTy Next1 = Next;
1310  if (GetVal(--Next) < GetVal(Next1)) { // swap with rightmost element that's smaller, flip suffix
1311  TSizeTy Mid = Last;
1312  for (; GetVal(Next) >= GetVal(--Mid); ) { }
1313  Swap(Next, Mid);
1314  Reverse(Next1, Last-1);
1315  return true;
1316  }
1317  if (Next == First) { // pure descending, flip all
1318  Reverse();
1319  return false;
1320  }
1321  }
1322 }
1323 
1324 template <class TVal, class TSizeTy>
1326  TSizeTy First = 0, Last = Len(), Next = Len()-1;
1327  if (Last < 2) return false;
1328  for(; ; ) {
1329  // find rightmost element not smaller than successor
1330  TSizeTy Next1 = Next;
1331  if (GetVal(--Next) >= GetVal(Next1)) { // swap with rightmost element that's not smaller, flip suffix
1332  TSizeTy Mid = Last;
1333  for (; GetVal(Next) < GetVal(--Mid); ) { }
1334  Swap(Next, Mid);
1335  Reverse(Next1, Last);
1336  return true;
1337  }
1338  if (Next == First) { // pure descending, flip all
1339  Reverse();
1340  return false;
1341  }
1342  }
1343 }
1344 
1345 template <class TVal, class TSizeTy>
1347  TVec<TVal, TSizeTy> IntrsVec;
1348  Intrs(ValV, IntrsVec);
1349  MoveFrom(IntrsVec);
1350 }
1351 
1352 template <class TVal, class TSizeTy>
1354  TVec<TVal, TSizeTy> UnionVec;
1355  Union(ValV, UnionVec);
1356  MoveFrom(UnionVec);
1357 }
1358 
1359 template <class TVal, class TSizeTy>
1361  TVec<TVal, TSizeTy> DiffVec;
1362  Diff(ValV, DiffVec);
1363  MoveFrom(DiffVec);
1364 }
1365 
1366 template <class TVal, class TSizeTy>
1368  DstValV.Clr();
1369  TSizeTy ValN1=0, ValN2=0;
1370  while ((ValN1<Len())&&(ValN2<ValV.Len())){
1371  const TVal& Val1=GetVal(ValN1);
1372  while ((ValN2<ValV.Len())&&(Val1>ValV.GetVal(ValN2))){
1373  ValN2++;}
1374  if ((ValN2<ValV.Len())&&(Val1==ValV.GetVal(ValN2))){
1375  DstValV.Add(Val1); ValN2++;}
1376  ValN1++;
1377  }
1378 }
1379 
1380 template <class TVal, class TSizeTy>
1382  DstValV.Gen(TInt::GetMx(Len(), ValV.Len()), 0);
1383  TSizeTy ValN1=0, ValN2=0;
1384  while ((ValN1<Len())&&(ValN2<ValV.Len())){
1385  const TVal& Val1=GetVal(ValN1);
1386  const TVal& Val2=ValV.GetVal(ValN2);
1387  if (Val1<Val2){DstValV.Add(Val1); ValN1++;}
1388  else if (Val1>Val2){DstValV.Add(Val2); ValN2++;}
1389  else {DstValV.Add(Val1); ValN1++; ValN2++;}
1390  }
1391  for (TSizeTy RestValN1=ValN1; RestValN1<Len(); RestValN1++){
1392  DstValV.Add(GetVal(RestValN1));}
1393  for (TSizeTy RestValN2=ValN2; RestValN2<ValV.Len(); RestValN2++){
1394  DstValV.Add(ValV.GetVal(RestValN2));}
1395 }
1396 
1397 template <class TVal, class TSizeTy>
1399  DstValV.Clr();
1400  TSizeTy ValN1=0, ValN2=0;
1401  while (ValN1<Len() && ValN2<ValV.Len()) {
1402  const TVal& Val1 = GetVal(ValN1);
1403  while (ValN2<ValV.Len() && Val1>ValV.GetVal(ValN2)) ValN2++;
1404  if (ValN2<ValV.Len()) {
1405  if (Val1!=ValV.GetVal(ValN2)) { DstValV.Add(Val1); }
1406  ValN1++;
1407  }
1408  }
1409  for (TSizeTy RestValN1=ValN1; RestValN1<Len(); RestValN1++){
1410  DstValV.Add(GetVal(RestValN1));}
1411 }
1412 
1413 template <class TVal, class TSizeTy>
1415  TSizeTy Cnt=0, ValN1=0, ValN2=0;
1416  while ((ValN1<Len())&&(ValN2<ValV.Len())){
1417  const TVal& Val1=GetVal(ValN1);
1418  while ((ValN2<ValV.Len())&&(Val1>ValV.GetVal(ValN2))){
1419  ValN2++;}
1420  if ((ValN2<ValV.Len())&&(Val1==ValV.GetVal(ValN2))){
1421  ValN2++; Cnt++;}
1422  ValN1++;
1423  }
1424  return Cnt;
1425 }
1426 
1427 template <class TVal, class TSizeTy>
1429  TSizeTy Cnt = 0, ValN1 = 0, ValN2 = 0;
1430  while ((ValN1 < Len()) && (ValN2 < ValV.Len())) {
1431  const TVal& Val1 = GetVal(ValN1);
1432  const TVal& Val2 = ValV.GetVal(ValN2);
1433  if (Val1 < Val2) {
1434  Cnt++; ValN1++;
1435  } else if (Val1 > Val2) {
1436  Cnt++; ValN2++;
1437  } else {
1438  Cnt++; ValN1++; ValN2++;
1439  }
1440  }
1441  Cnt += (Len() - ValN1) + (ValV.Len() - ValN2);
1442  return Cnt;
1443 }
1444 
1445 template <class TVal, class TSizeTy>
1446 TSizeTy TVec<TVal, TSizeTy>::Count(const TVal& Val) const {
1447  TSizeTy Count = 0;
1448  for (TSizeTy i = 0; i < Len(); i++){
1449  if (Val == ValT[i]){Count++;}}
1450  return Count;
1451 }
1452 
1453 template <class TVal, class TSizeTy>
1454 TSizeTy TVec<TVal, TSizeTy>::SearchBin(const TVal& Val) const {
1455  TSizeTy LValN=0, RValN=Len()-1;
1456  while (RValN>=LValN){
1457  TSizeTy ValN=(LValN+RValN)/2;
1458  if (Val==ValT[ValN]){return ValN;}
1459  if (Val<ValT[ValN]){RValN=ValN-1;} else {LValN=ValN+1;}
1460  }
1461  return -1;
1462 }
1463 
1464 template <class TVal, class TSizeTy>
1465 TSizeTy TVec<TVal, TSizeTy>::SearchBin(const TVal& Val, TSizeTy& InsValN) const {
1466  TSizeTy LValN=0, RValN=Len()-1;
1467  while (RValN>=LValN){
1468  TSizeTy ValN=(LValN+RValN)/2;
1469  if (Val==ValT[ValN]){InsValN=ValN; return ValN;}
1470  if (Val<ValT[ValN]){RValN=ValN-1;} else {LValN=ValN+1;}
1471  }
1472  InsValN=LValN; return -1;
1473 }
1474 
1475 template <class TVal, class TSizeTy>
1476 TSizeTy TVec<TVal, TSizeTy>::SearchBinLeft(const TVal& Val, TSizeTy& InsValN) const {
1477  TSizeTy LValN=0, RValN=Len()-1;
1478  while (RValN>=LValN){
1479  TSizeTy ValN=(LValN+RValN)/2;
1480  if (Val==ValT[ValN]){InsValN=ValN; return ValN;}
1481  if (Val<ValT[ValN]){RValN=ValN-1;} else {LValN=ValN+1;}
1482  }
1483  InsValN=RValN; return -1;
1484 }
1485 
1486 template <class TVal, class TSizeTy>
1487 TSizeTy TVec<TVal, TSizeTy>::SearchForw(const TVal& Val, const TSizeTy& BValN) const {
1488  for (TSizeTy ValN=BValN; ValN<Vals; ValN++){
1489  if (Val==ValT[ValN]){return ValN;}}
1490  return -1;
1491 }
1492 
1493 template <class TVal, class TSizeTy>
1494 TSizeTy TVec<TVal, TSizeTy>::SearchBack(const TVal& Val) const {
1495  for (TSizeTy ValN=Vals-1; ValN>=0; ValN--){
1496  if (Val==ValT[ValN]){return ValN;}}
1497  return -1;
1498 }
1499 
1500 template <class TVal, class TSizeTy>
1501 TSizeTy TVec<TVal, TSizeTy>::SearchVForw(const TVec<TVal, TSizeTy>& ValV, const TSizeTy& BValN) const {
1502  TSizeTy ValVLen=ValV.Len();
1503  for (TSizeTy ValN=BValN; ValN<Vals-ValVLen+1; ValN++){
1504  bool Found=true;
1505  for (TSizeTy SubValN=0; SubValN<ValVLen; SubValN++){
1506  if (ValV[SubValN]!=GetVal(ValN+SubValN)){Found=false; break;}
1507  }
1508  if (Found){return ValN;}
1509  }
1510  return -1;
1511 }
1512 
1513 template <class TVal, class TSizeTy>
1515  if (Vals==0){return -1;}
1516  TSizeTy MxValN=0;
1517  for (TSizeTy ValN=1; ValN<Vals; ValN++){
1518  if (ValT[ValN]>ValT[MxValN]){MxValN=ValN;}
1519  }
1520  return MxValN;
1521 }
1522 
1524 // Common-Vector-Types
1526 typedef TVec<TCh> TChV;
1558 typedef TVec<TIntKd> TIntKdV;
1600 
1601 //#//////////////////////////////////////////////
1603 
1606 template <class TVal, class TSizeTy=int>
1607 class TVecPool {
1608 public:
1611 private:
1615  TVal EmptyVal; // Empty value/vector
1616  TVal *ValBf; // Buffer for storing all the values
1617  TVec<uint64, int> IdToOffV; // Id to one past last (Vector starts at [id-1]). Vector length is IdToOff[id]-IdToOff[id-1]
1618 private:
1619  void Resize(const TSize& _MxVals);
1620 public:
1622 
1627  TVecPool(const TSize& ExpectVals=0, const TSize& _GrowBy=1000000, const bool& _FastCopy=false, const TVal& _EmptyVal=TVal());
1628  TVecPool(const TVecPool<TVal, TSizeTy>& Pool);
1629  TVecPool(TSIn& SIn);
1630  ~TVecPool() { if (ValBf != NULL) { delete [] ValBf; } ValBf=NULL; }
1631  static PVecPool New(const TSize& ExpectVals=0, const TSize& GrowBy=1000000, const bool& FastCopy=false) {
1632  return new TVecPool(ExpectVals, GrowBy, FastCopy); }
1633  static PVecPool Load(TSIn& SIn) { return new TVecPool(SIn); }
1634  static PVecPool Load(const TStr& FNm) { TFIn FIn(FNm); return Load(FIn); }
1635  void Save(TSOut& SOut) const;
1636  TVecPool& operator = (const TVecPool& Pool);
1637 
1639  int GetVecs() const { return IdToOffV.Len(); }
1641  TSize GetVals() const { return Vals; }
1643  bool IsVId(const int& VId) const { return (0 <= VId) && (VId < IdToOffV.Len()); }
1645  uint64 Reserved() const { return MxVals; }
1647  void Reserve(const TSize& MxVals) { Resize(MxVals); }
1649  const TVal& GetEmptyVal() const { return EmptyVal; }
1651  void SetEmptyVal(const TVal& _EmptyVal) { EmptyVal = _EmptyVal; }
1653  uint64 GetMemUsed() const {
1654  return sizeof(TCRef)+sizeof(TBool)+3*sizeof(TSize)+sizeof(TVal*)+MxVals*sizeof(TVal);}
1655 
1657  int AddV(const TValV& ValV);
1659 
1661  int AddEmptyV(const int& ValVLen);
1663  int GetVLen(const int& VId) const { if (VId==0){return 0;} else {return int(IdToOffV[VId]-IdToOffV[VId-1]);}}
1665  TVal* GetValVPt(const int& VId) const {
1666  if (GetVLen(VId)==0){return (TVal*)&EmptyVal;}
1667  else {return ValBf+IdToOffV[VId-1];}}
1669 
1671  void GetV(const int& VId, TValV& ValV) const {
1672  if (GetVLen(VId)==0){ValV.Clr();}
1673  else { ValV.GenExt(GetValVPt(VId), GetVLen(VId)); } }
1675  void PutV(const int& VId, const TValV& ValV) {
1676  IAssert(IsVId(VId) && GetVLen(VId) == ValV.Len());
1677  if (FastCopy) {
1678  memcpy(GetValVPt(VId), ValV.BegI(), sizeof(TVal)*ValV.Len()); }
1679  else { TVal* ValPt = GetValVPt(VId);
1680  for (::TSize ValN=0; ValN < ::TSize(ValV.Len()); ValN++, ValPt++) { *ValPt=ValV[ValN]; }
1681  } }
1683 
1685  void CompactPool(const TVal& DelVal);
1687 
1689  void ShuffleAll(TRnd& Rnd=TInt::Rnd);
1690 
1692 
1694  void Clr(bool DoDel = true) {
1695  IdToOffV.Clr(DoDel); MxVals=0; Vals=0;
1696  if (DoDel && ValBf!=NULL) { delete [] ValBf; ValBf=NULL;}
1697  if (! DoDel) { PutAll(EmptyVal); } }
1699  void PutAll(const TVal& Val) {
1700  for (TSize ValN = 0; ValN < MxVals; ValN++) { ValBf[ValN]=Val; } }
1701  friend class TPt<TVecPool<TVal> >;
1702 };
1703 
1704 template <class TVal, class TSizeTy>
1706  if (_MxVals <= MxVals){ return; } else { MxVals = _MxVals; }
1707  if (ValBf == NULL) {
1708  try { ValBf = new TVal [MxVals]; }
1709  catch (std::exception Ex) {
1710  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()); }
1711  IAssert(ValBf != NULL);
1712  if (EmptyVal != TVal()) { PutAll(EmptyVal); }
1713  } else {
1714  // printf("*** Resize vector pool: %llu -> %llu\n", uint64(Vals), uint64(MxVals));
1715  TVal* NewValBf = NULL;
1716  try { NewValBf = new TVal [MxVals]; }
1717  catch (std::exception Ex) {
1718  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()); }
1719  IAssert(NewValBf != NULL);
1720  if (FastCopy) {
1721  memcpy(NewValBf, ValBf, Vals*sizeof(TVal)); }
1722  else {
1723  for (TSize ValN = 0; ValN < Vals; ValN++){ NewValBf[ValN] = ValBf[ValN]; } }
1724  if (EmptyVal != TVal()) { // init empty values
1725  for (TSize ValN = Vals; ValN < MxVals; ValN++) { NewValBf[ValN] = EmptyVal; }
1726  }
1727  delete [] ValBf;
1728  ValBf = NewValBf;
1729  }
1730 }
1731 
1732 template <class TVal, class TSizeTy>
1733 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) {
1734  IdToOffV.Add(0);
1735  Resize(ExpectVals);
1736 }
1737 
1738 template <class TVal, class TSizeTy>
1739 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) {
1740  try {
1741  ValBf = new TVal [MxVals]; }
1742  catch (std::exception Ex) {
1743  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()); }
1744  IAssert(ValBf != NULL);
1745  if (FastCopy) {
1746  memcpy(ValBf, Pool.ValBf, MxVals*sizeof(TVal)); }
1747  else {
1748  for (TSize ValN = 0; ValN < MxVals; ValN++){ ValBf[ValN] = Pool.ValBf[ValN]; } }
1749 }
1750 
1751 template <class TVal, class TSizeTy>
1753  uint64 _GrowBy, _MxVals, _Vals;
1754  SIn.Load(_GrowBy); SIn.Load(_MxVals); SIn.Load(_Vals);
1755  IAssertR(_GrowBy<TSizeMx && _MxVals<TSizeMx && _Vals<TSizeMx, "This is a 64-bit vector pool. Use a 64-bit compiler.");
1756  GrowBy=TSize(_GrowBy); MxVals=TSize(_Vals); Vals=TSize(_Vals); //note MxVals==Vals
1757  EmptyVal = TVal(SIn);
1758  if (MxVals==0) { ValBf = NULL; } else { ValBf = new TVal [MxVals]; }
1759  for (TSize ValN = 0; ValN < Vals; ValN++) { ValBf[ValN] = TVal(SIn); }
1760  { TInt MxVals(SIn), Vals(SIn);
1761  IdToOffV.Gen(Vals);
1762  for (int ValN = 0; ValN < Vals; ValN++) {
1763  uint64 Offset; SIn.Load(Offset); IAssert(Offset < TSizeMx);
1764  IdToOffV[ValN]=TSize(Offset);
1765  } }
1766 }
1767 
1768 template <class TVal, class TSizeTy>
1770  SOut.Save(FastCopy);
1771  uint64 _GrowBy=GrowBy, _MxVals=MxVals, _Vals=Vals;
1772  SOut.Save(_GrowBy); SOut.Save(_MxVals); SOut.Save(_Vals);
1773  SOut.Save(EmptyVal);
1774  for (TSize ValN = 0; ValN < Vals; ValN++) { ValBf[ValN].Save(SOut); }
1775  { SOut.Save(IdToOffV.Len()); SOut.Save(IdToOffV.Len());
1776  for (int ValN = 0; ValN < IdToOffV.Len(); ValN++) {
1777  const uint64 Offset=IdToOffV[ValN]; SOut.Save(Offset);
1778  } }
1779 }
1780 
1781 template <class TVal, class TSizeTy>
1783  if (this!=&Pool) {
1784  FastCopy = Pool.FastCopy;
1785  GrowBy = Pool.GrowBy;
1786  MxVals = Pool.MxVals;
1787  Vals = Pool.Vals;
1788  EmptyVal = Pool.EmptyVal;
1789  IdToOffV=Pool.IdToOffV;
1790  try {
1791  ValBf = new TVal [MxVals]; }
1792  catch (std::exception Ex) {
1793  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()); }
1794  IAssert(ValBf != NULL);
1795  if (FastCopy) {
1796  memcpy(ValBf, Pool.ValBf, Vals*sizeof(TVal)); }
1797  else {
1798  for (TSize ValN = 0; ValN < Vals; ValN++){ ValBf[ValN] = Pool.ValBf[ValN]; } }
1799  }
1800  return *this;
1801 }
1802 
1803 template <class TVal, class TSizeTy>
1805  const TSize ValVLen = ValV.Len();
1806  if (ValVLen == 0) { return 0; }
1807  if (MxVals < Vals+ValVLen) { Resize(Vals+MAX(ValVLen, GrowBy)); }
1808  if (FastCopy) { memcpy(ValBf+Vals, ValV.BegI(), sizeof(TVal)*ValV.Len()); }
1809  else { for (TSize ValN=0; ValN < ValVLen; ValN++) { ValBf[Vals+ValN]=ValV[ValN]; } }
1810  Vals+=ValVLen; IdToOffV.Add(Vals);
1811  return IdToOffV.Len()-1;
1812 }
1813 
1814 template <class TVal, class TSizeTy>
1815 int TVecPool<TVal, TSizeTy>::AddEmptyV(const int& ValVLen) {
1816  if (ValVLen==0){return 0;}
1817  if (MxVals < Vals+ValVLen){Resize(Vals+MAX(TSize(ValVLen), GrowBy)); }
1818  Vals+=ValVLen; IdToOffV.Add(Vals);
1819  return IdToOffV.Len()-1;
1820 }
1821 
1822 // Delete all elements of value DelVal from all vectors. Empty space is left at the end of the pool.
1823 template <class TVal, class TSizeTy>
1824 void TVecPool<TVal, TSizeTy>::CompactPool(const TVal& DelVal) {
1825  ::TSize TotalDel=0, NDel=0;
1826  // printf("Compacting %d vectors\n", IdToOffV.Len());
1827  for (int vid = 1; vid < IdToOffV.Len(); vid++) {
1828  // if (vid % 10000000 == 0) { printf(" %dm", vid/1000000); fflush(stdout); }
1829  const uint Len = GetVLen(vid);
1830  TVal* ValV = GetValVPt(vid);
1831  if (TotalDel > 0) { IdToOffV[vid-1] -= TotalDel; } // update end of vector
1832  if (Len == 0) { continue; }
1833  NDel = 0;
1834  for (TVal* v = ValV; v < ValV+Len-NDel; v++) {
1835  if (*v == DelVal) {
1836  TVal* Beg = v;
1837  while (*v == DelVal && v < ValV+Len) { v++; NDel++; }
1838  memcpy(Beg, v, sizeof(TVal)*int(Len - ::TSize(v - ValV)));
1839  v -= NDel;
1840  }
1841  }
1842  memcpy(ValV-TotalDel, ValV, sizeof(TVal)*Len); // move data
1843  TotalDel += NDel;
1844  }
1845  IdToOffV.Last() -= TotalDel;
1846  for (::TSize i = Vals-TotalDel; i < Vals; i++) { ValBf[i] = EmptyVal; }
1847  Vals -= TotalDel;
1848  // printf(" deleted %llu elements from the pool\n", TotalDel);
1849 }
1850 
1851 // shuffles all the order of elements in the pool (does not respect vector boundaries)
1852 template <class TVal, class TSizeTy>
1854  for (::TSize n = Vals-1; n > 0; n--) {
1855  const ::TSize k = ::TSize(((uint64(Rnd.GetUniDevInt())<<32) | uint64(Rnd.GetUniDevInt())) % (n+1));
1856  const TVal Tmp = ValBf[n];
1857  ValBf[n] = ValBf[k];
1858  ValBf[k] = Tmp;
1859  }
1860 }
1861 
1862 
1864 // Below are old 32-bit implementations of TVec and other classes.
1865 // Old TVec takes at most 2G elements.
1866 // The new vector class supports 64-bits for the number of elements,
1867 // but also allows 32-bits for backward compatibility.
1868 // by Jure (Jan 2013)
1869 namespace TGLib_OLD {
1871 // Vector Pool
1872 template<class TVal>
1873 class TVecPool {
1874 public:
1877 private:
1881  TVal EmptyVal; // empty vector
1882  TVal *ValBf; // buffer storing all the values
1883  TVec< ::TSize> IdToOffV; // id to one past last (vector starts at [id-1])
1884 private:
1885  void Resize(const ::TSize& _MxVals);
1886 public:
1887  TVecPool(const ::TSize& ExpectVals=0, const ::TSize& _GrowBy=1000000, const bool& _FastCopy=false, const TVal& _EmptyVal=TVal());
1888  TVecPool(const TVecPool& Pool);
1889  TVecPool(TSIn& SIn);
1890  ~TVecPool() { if (ValBf != NULL) { delete [] ValBf; } ValBf=NULL; }
1891  static PVecPool New(const ::TSize& ExpectVals=0, const ::TSize& GrowBy=1000000, const bool& FastCopy=false) {
1892  return new TVecPool(ExpectVals, GrowBy, FastCopy); }
1893  static PVecPool Load(TSIn& SIn) { return new TVecPool(SIn); }
1894  static PVecPool Load(const TStr& FNm) { TFIn FIn(FNm); return Load(FIn); }
1895  void Save(TSOut& SOut) const;
1896 
1897  TVecPool& operator = (const TVecPool& Pool);
1898 
1899  ::TSize GetVals() const { return Vals; }
1900  ::TSize GetVecs() const { return IdToOffV.Len(); }
1901  bool IsVId(const int& VId) const { return (0 <= VId) && (VId < IdToOffV.Len()); }
1902  ::TSize Reserved() const { return MxVals; }
1903  void Reserve(const ::TSize& MxVals) { Resize(MxVals); }
1904  const TVal& GetEmptyVal() const { return EmptyVal; }
1905  void SetEmptyVal(const TVal& _EmptyVal) { EmptyVal = _EmptyVal; }
1907  return sizeof(TCRef)+sizeof(TBool)+3*sizeof(TSize)+sizeof(TVal*)+MxVals*sizeof(TVal);}
1908 
1909  int AddV(const TValV& ValV);
1910  int AddEmptyV(const int& ValVLen);
1911  uint GetVLen(const int& VId) const {
1912  if (VId==0){return 0;}
1913  else {return uint(IdToOffV[VId]-IdToOffV[VId-1]);}}
1914  TVal* GetValVPt(const int& VId) const {
1915  if (GetVLen(VId)==0){return (TVal*)&EmptyVal;}
1916  else {return ValBf+IdToOffV[VId-1];}}
1917  void GetV(const int& VId, TValV& ValV) const {
1918  if (GetVLen(VId)==0){ValV.Clr();}
1919  else { ValV.GenExt(GetValVPt(VId), GetVLen(VId)); } }
1920  void PutV(const int& VId, const TValV& ValV) {
1921  IAssert(IsVId(VId) && GetVLen(VId) == ValV.Len());
1922  if (FastCopy) {
1923  memcpy(GetValVPt(VId), ValV.BegI(), sizeof(TVal)*ValV.Len()); }
1924  else { TVal* ValPt = GetValVPt(VId);
1925  for (uint ValN=0; ValN < uint(ValV.Len()); ValN++, ValPt++) { *ValPt=ValV[ValN]; }
1926  }
1927  }
1928  void CompactPool(const TVal& DelVal); // delete all elements of value DelVal from all vectors
1929  void ShuffleAll(TRnd& Rnd=TInt::Rnd); // shuffles all the order of elements in the Pool (does not respect vector boundaries)
1930 
1931  //bool HasIdMap() const { return ! IdToOffV.Empty(); }
1932  //void ClrIdMap() { IdToOffV.Clr(true); }
1933  void Clr(bool DoDel = true) {
1934  IdToOffV.Clr(DoDel); MxVals=0; Vals=0;
1935  if (DoDel && ValBf!=NULL) { delete [] ValBf; ValBf=NULL;}
1936  if (! DoDel) { PutAll(EmptyVal); }
1937  }
1938  void PutAll(const TVal& Val) {
1939  for (TSize ValN = 0; ValN < MxVals; ValN++) { ValBf[ValN]=Val; } }
1940 
1941  friend class TPt<TVecPool<TVal> >;
1942 };
1943 
1944 template <class TVal>
1946  if (_MxVals <= MxVals){ return; } else { MxVals = _MxVals; }
1947  if (ValBf == NULL) {
1948  try { ValBf = new TVal [MxVals]; }
1949  catch (std::exception Ex) {
1950  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()); }
1951  IAssert(ValBf != NULL);
1952  if (EmptyVal != TVal()) { PutAll(EmptyVal); }
1953  } else {
1954  // printf("*** Resize vector pool: %llu -> %llu\n", uint64(Vals), uint64(MxVals));
1955  TVal* NewValBf = NULL;
1956  try { NewValBf = new TVal [MxVals]; }
1957  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()); }
1958  IAssert(NewValBf != NULL);
1959  if (FastCopy) {
1960  memcpy(NewValBf, ValBf, Vals*sizeof(TVal)); }
1961  else {
1962  for (TSize ValN = 0; ValN < Vals; ValN++){ NewValBf[ValN] = ValBf[ValN]; } }
1963  if (EmptyVal != TVal()) { // init empty values
1964  for (TSize ValN = Vals; ValN < MxVals; ValN++) { NewValBf[ValN] = EmptyVal; }
1965  }
1966  delete [] ValBf;
1967  ValBf = NewValBf;
1968  }
1969 }
1970 
1971 template <class TVal>
1972 TVecPool<TVal>::TVecPool(const ::TSize& ExpectVals, const ::TSize& _GrowBy, const bool& _FastCopy, const TVal& _EmptyVal) :
1973  GrowBy(_GrowBy), MxVals(0), Vals(0), EmptyVal(_EmptyVal), ValBf(NULL) {
1974  IdToOffV.Add(0);
1975  Resize(ExpectVals);
1976 }
1977 
1978 template <class TVal>
1980  FastCopy(Pool.FastCopy), GrowBy(Pool.GrowBy),
1981  MxVals(Pool.MxVals), Vals(Pool.Vals), EmptyVal(Pool.EmptyVal), IdToOffV(Pool.IdToOffV) {
1982  try { ValBf = new TVal [MxVals]; }
1983  catch (std::exception Ex) { FailR(TStr::Fmt("TVecPool::TVecPool: %s, MxVals: %d", Ex.what(), MxVals).CStr()); }
1984  IAssert(ValBf != NULL);
1985  if (FastCopy) {
1986  memcpy(ValBf, Pool.ValBf, MxVals*sizeof(TVal)); }
1987  else {
1988  for (TSize ValN = 0; ValN < MxVals; ValN++){ ValBf[ValN] = Pool.ValBf[ValN]; } }
1989 }
1990 
1991 template <class TVal>
1993  FastCopy(SIn) {
1994  uint64 _GrowBy, _MxVals, _Vals;
1995  SIn.Load(_GrowBy); SIn.Load(_MxVals); SIn.Load(_Vals);
1996  IAssert(_GrowBy<TSizeMx && _MxVals<TSizeMx && _Vals<TSizeMx);
1997  GrowBy=TSize(_GrowBy); MxVals=TSize(_Vals); Vals=TSize(_Vals); //note MxVals==Vals
1998  EmptyVal = TVal(SIn);
1999  if (MxVals==0) { ValBf = NULL; } else { ValBf = new TVal [MxVals]; }
2000  for (TSize ValN = 0; ValN < Vals; ValN++) { ValBf[ValN] = TVal(SIn); }
2001  { TInt MxVals(SIn), Vals(SIn);
2002  IdToOffV.Gen(Vals);
2003  for (int ValN = 0; ValN < Vals; ValN++) {
2004  uint64 Offset; SIn.Load(Offset); IAssert(Offset < TSizeMx);
2005  IdToOffV[ValN]=TSize(Offset);
2006  } }
2007 }
2008 
2009 template <class TVal>
2010 void TVecPool<TVal>::Save(TSOut& SOut) const {
2011  SOut.Save(FastCopy);
2012  uint64 _GrowBy=GrowBy, _MxVals=MxVals, _Vals=Vals;
2013  SOut.Save(_GrowBy); SOut.Save(_MxVals); SOut.Save(_Vals);
2014  SOut.Save(EmptyVal);
2015  for (TSize ValN = 0; ValN < Vals; ValN++) { ValBf[ValN].Save(SOut); }
2016  { SOut.Save(IdToOffV.Len()); SOut.Save(IdToOffV.Len());
2017  for (int ValN = 0; ValN < IdToOffV.Len(); ValN++) {
2018  const uint64 Offset=IdToOffV[ValN]; SOut.Save(Offset);
2019  } }
2020 }
2021 
2022 template <class TVal>
2024  if (this!=&Pool) {
2025  FastCopy = Pool.FastCopy;
2026  GrowBy = Pool.GrowBy;
2027  MxVals = Pool.MxVals;
2028  Vals = Pool.Vals;
2029  EmptyVal = Pool.EmptyVal;
2030  IdToOffV=Pool.IdToOffV;
2031  try { ValBf = new TVal [MxVals]; }
2032  catch (std::exception Ex) { FailR(TStr::Fmt("TVec::operator= : %s, MxVals: %d", Ex.what(), MxVals).CStr()); }
2033  IAssert(ValBf != NULL);
2034  if (FastCopy) {
2035  memcpy(ValBf, Pool.ValBf, Vals*sizeof(TVal)); }
2036  else {
2037  for (uint64 ValN = 0; ValN < Vals; ValN++){ ValBf[ValN] = Pool.ValBf[ValN]; } }
2038  }
2039  return *this;
2040 }
2041 
2042 template<class TVal>
2043 int TVecPool<TVal>::AddV(const TValV& ValV) {
2044  const ::TSize ValVLen = ValV.Len();
2045  if (ValVLen == 0) { return 0; }
2046  if (MxVals < Vals+ValVLen) { Resize(Vals+MAX(ValVLen, GrowBy)); }
2047  if (FastCopy) { memcpy(ValBf+Vals, ValV.BegI(), sizeof(TVal)*ValV.Len()); }
2048  else { for (uint ValN=0; ValN < ValVLen; ValN++) { ValBf[Vals+ValN]=ValV[ValN]; } }
2049  Vals+=ValVLen; IdToOffV.Add(Vals);
2050  return IdToOffV.Len()-1;
2051 }
2052 
2053 template<class TVal>
2054 int TVecPool<TVal>::AddEmptyV(const int& ValVLen) {
2055  if (ValVLen==0){return 0;}
2056  if (MxVals < Vals+ValVLen){Resize(Vals+MAX(TSize(ValVLen), GrowBy)); }
2057  Vals+=ValVLen; IdToOffV.Add(Vals);
2058  return IdToOffV.Len()-1;
2059 }
2060 
2061 // delete all elements of value DelVal from all vectors
2062 // empty space is left at the end of the pool
2063 template<class TVal>
2064 void TVecPool<TVal>::CompactPool(const TVal& DelVal) {
2065  ::TSize TotalDel=0, NDel=0;
2066  // printf("Compacting %d vectors\n", IdToOffV.Len());
2067  for (int vid = 1; vid < IdToOffV.Len(); vid++) {
2068  // if (vid % 10000000 == 0) { printf(" %dm", vid/1000000); fflush(stdout); }
2069  const uint Len = GetVLen(vid);
2070  TVal* ValV = GetValVPt(vid);
2071  if (TotalDel > 0) { IdToOffV[vid-1] -= TotalDel; } // update end of vector
2072  if (Len == 0) { continue; }
2073  NDel = 0;
2074  for (TVal* v = ValV; v < ValV+Len-NDel; v++) {
2075  if (*v == DelVal) {
2076  TVal* Beg = v;
2077  while (*v == DelVal && v < ValV+Len) { v++; NDel++; }
2078  memcpy(Beg, v, sizeof(TVal)*int(Len - ::TSize(v - ValV)));
2079  v -= NDel;
2080  }
2081  }
2082  memcpy(ValV-TotalDel, ValV, sizeof(TVal)*Len); // move data
2083  TotalDel += NDel;
2084  }
2085  IdToOffV.Last() -= TotalDel;
2086  for (::TSize i = Vals-TotalDel; i < Vals; i++) { ValBf[i] = EmptyVal; }
2087  Vals -= TotalDel;
2088  // printf(" deleted %llu elements from the pool\n", TotalDel);
2089 }
2090 
2091 // shuffles all the order of elements in the pool (does not respect vector boundaries)
2092 template<class TVal>
2094  for (::TSize n = Vals-1; n > 0; n--) {
2095  const ::TSize k = ::TSize(((uint64(Rnd.GetUniDevInt())<<32) | uint64(Rnd.GetUniDevInt())) % (n+1));
2096  const TVal Tmp = ValBf[n];
2097  ValBf[n] = ValBf[k];
2098  ValBf[k] = Tmp;
2099  }
2100 }
2101 
2102 }; // namespace TGLib_OLD
2103 
2104 typedef TVecPool<TInt> TIntVecPool;
2106 
2108 // Vector-Pointer
2109 template <class TVal>
2110 class PVec{
2111 private:
2113 public:
2115 public:
2116  PVec<TVal>(): V(){}
2117  PVec<TVal>(const PVec<TVal>& Vec): V(Vec.V){}
2118  static TPt<PVec<TVal> > New(){
2119  return new PVec<TVal>();}
2120  PVec<TVal>(const int& MxVals, const int& Vals): V(MxVals, Vals){}
2121  static TPt<PVec<TVal> > New(const int& MxVals, const int& Vals){
2122  return new PVec<TVal>(MxVals, Vals);}
2123  PVec<TVal>(const TVec<TVal>& _V): V(_V){}
2124  static TPt<PVec<TVal> > New(const TVec<TVal>& V){
2125  return new PVec<TVal>(V);}
2126  explicit PVec<TVal>(TSIn& SIn): V(SIn){}
2127  static TPt<PVec<TVal> > Load(TSIn& SIn){return new PVec<TVal>(SIn);}
2128  void Save(TSOut& SOut) const {V.Save(SOut);}
2129 
2131  if (this!=&Vec){V=Vec.V;} return *this;}
2132  bool operator==(const PVec<TVal>& Vec) const {return V==Vec.V;}
2133  bool operator<(const PVec<TVal>& Vec) const {return V<Vec.V;}
2134  TVal& operator[](const int& ValN) const {return V[ValN];}
2135 
2136  bool Empty() const {return V.Empty();}
2137  int Len() const {return V.Len();}
2138  TVal GetVal(const int& ValN) const {return V[ValN];}
2139 
2140  int Add(const TVal& Val){return V.Add(Val);}
2141 
2142  friend class TPt<PVec<TVal> >;
2143 };
2144 
2146 // Common-Vector-Pointer-Types
2153 
2155 // 2D-Vector
2156 template <class TVal>
2157 class TVVec{
2158 private:
2161 public:
2162  TVVec(): XDim(), YDim(), ValV(){}
2163  TVVec(const TVVec& Vec):
2164  XDim(Vec.XDim), YDim(Vec.YDim), ValV(Vec.ValV){}
2165  TVVec(const int& _XDim, const int& _YDim):
2166  XDim(), YDim(), ValV(){Gen(_XDim, _YDim);}
2167  explicit TVVec(const TVec<TVal>& _ValV, const int& _XDim, const int& _YDim):
2168  XDim(_XDim), YDim(_YDim), ValV(_ValV){ IAssert(ValV.Len()==XDim*YDim); }
2169  explicit TVVec(TSIn& SIn) {Load(SIn);}
2170  void Load(TSIn& SIn){XDim.Load(SIn); YDim.Load(SIn); ValV.Load(SIn);}
2171  void Save(TSOut& SOut) const {
2172  XDim.Save(SOut); YDim.Save(SOut); ValV.Save(SOut);}
2173 
2175  if (this!=&Vec){XDim=Vec.XDim; YDim=Vec.YDim; ValV=Vec.ValV;} return *this;}
2176  bool operator==(const TVVec& Vec) const {
2177  return (XDim==Vec.XDim)&&(YDim==Vec.YDim)&&(ValV==Vec.ValV);}
2178 
2179  bool Empty() const {return ValV.Len()==0;}
2180  void Clr(){XDim=0; YDim=0; ValV.Clr();}
2181  void Gen(const int& _XDim, const int& _YDim){
2182  Assert((_XDim>=0)&&(_YDim>=0));
2183  XDim=_XDim; YDim=_YDim; ValV.Gen(XDim*YDim);}
2184  int GetXDim() const {return XDim;}
2185  int GetYDim() const {return YDim;}
2186  int GetRows() const {return XDim;}
2187  int GetCols() const {return YDim;}
2189 
2190  const TVal& At(const int& X, const int& Y) const {
2191  Assert((0<=X)&&(X<int(XDim))&&(0<=Y)&&(Y<int(YDim)));
2192  return ValV[X*YDim+Y];}
2193  TVal& At(const int& X, const int& Y){
2194  Assert((0<=X)&&(X<int(XDim))&&(0<=Y)&&(Y<int(YDim)));
2195  return ValV[X*YDim+Y];}
2196  TVal& operator()(const int& X, const int& Y){
2197  return At(X, Y);}
2198  const TVal& operator()(const int& X, const int& Y) const {
2199  return At(X, Y);}
2200 
2201  void PutXY(const int& X, const int& Y, const TVal& Val){At(X, Y)=Val;}
2202  void PutAll(const TVal& Val){ValV.PutAll(Val);}
2203  void PutX(const int& X, const TVal& Val){
2204  for (int Y=0; Y<int(YDim); Y++){At(X, Y)=Val;}}
2205  void PutY(const int& Y, const TVal& Val){
2206  for (int X=0; X<int(XDim); X++){At(X, Y)=Val;}}
2207  TVal GetXY(const int& X, const int& Y) const {
2208  Assert((0<=X)&&(X<int(XDim))&&(0<=Y)&&(Y<int(YDim)));
2209  return ValV[X*YDim+Y];}
2210  void GetRow(const int& RowN, TVec<TVal>& Vec) const;
2211  void GetCol(const int& ColN, TVec<TVal>& Vec) const;
2212 
2213  void SwapX(const int& X1, const int& X2);
2214  void SwapY(const int& Y1, const int& Y2);
2215  void Swap(TVVec<TVal>& Vec);
2216 
2217  void ShuffleX(TRnd& Rnd);
2218  void ShuffleY(TRnd& Rnd);
2219  void GetMxValXY(int& X, int& Y) const;
2220 
2221  void CopyFrom(const TVVec<TVal>& VVec);
2222  void AddXDim();
2223  void AddYDim();
2224  void DelX(const int& X);
2225  void DelY(const int& Y);
2226 };
2227 
2228 template <class TVal>
2229 void TVVec<TVal>::SwapX(const int& X1, const int& X2){
2230  for (int Y=0; Y<int(YDim); Y++){
2231  TVal Val=At(X1, Y); At(X1, Y)=At(X2, Y); At(X2, Y)=Val;}
2232 }
2233 
2234 template <class TVal>
2235 void TVVec<TVal>::SwapY(const int& Y1, const int& Y2){
2236  for (int X=0; X<int(XDim); X++){
2237  TVal Val=At(X, Y1); At(X, Y1)=At(X, Y2); At(X, Y2)=Val;}
2238 }
2239 
2240 template <class TVal>
2242  if (this!=&Vec){
2243  ::Swap(XDim, Vec.XDim);
2244  ::Swap(YDim, Vec.YDim);
2245  ValV.Swap(Vec.ValV);
2246  }
2247 }
2248 
2249 template <class TVal>
2251  for (int X=0; X<XDim-1; X++){SwapX(X, X+Rnd.GetUniDevInt(XDim-X));}
2252 }
2253 
2254 template <class TVal>
2256  for (int Y=0; Y<YDim-1; Y++){SwapY(Y, Y+Rnd.GetUniDevInt(YDim-Y));}
2257 }
2258 
2259 template <class TVal>
2260 void TVVec<TVal>::GetMxValXY(int& X, int& Y) const {
2261  int MxValN=ValV.GetMxValN();
2262  Y=MxValN%YDim;
2263  X=MxValN/YDim;
2264 }
2265 
2266 template <class TVal>
2268  int CopyXDim=TInt::GetMn(GetXDim(), VVec.GetXDim());
2269  int CopyYDim=TInt::GetMn(GetYDim(), VVec.GetYDim());
2270  for (int X=0; X<CopyXDim; X++){
2271  for (int Y=0; Y<CopyYDim; Y++){
2272  At(X, Y)=VVec.At(X, Y);
2273  }
2274  }
2275 }
2276 
2277 template <class TVal>
2279  TVVec<TVal> NewVVec(XDim+1, YDim);
2280  NewVVec.CopyFrom(*this);
2281  *this=NewVVec;
2282 }
2283 
2284 template <class TVal>
2286  TVVec<TVal> NewVVec(XDim, YDim+1);
2287  NewVVec.CopyFrom(*this);
2288  *this=NewVVec;
2289 }
2290 
2291 template <class TVal>
2292 void TVVec<TVal>::DelX(const int& X){
2293  TVVec<TVal> NewVVec(XDim-1, YDim);
2294  for (int Y=0; Y<YDim; Y++){
2295  for (int LX=0; LX<X; LX++){
2296  NewVVec.At(LX, Y)=At(LX, Y);}
2297  for (int RX=X+1; RX<XDim; RX++){
2298  NewVVec.At(RX-1, Y)=At(RX, Y);}
2299  }
2300  *this=NewVVec;
2301 }
2302 
2303 template <class TVal>
2304 void TVVec<TVal>::DelY(const int& Y){
2305  TVVec<TVal> NewVVec(XDim, YDim-1);
2306  for (int X=0; X<XDim; X++){
2307  for (int LY=0; LY<Y; LY++){
2308  NewVVec.At(X, LY)=At(X, LY);}
2309  for (int RY=Y+1; RY<YDim; RY++){
2310  NewVVec.At(X, RY-1)=At(X, RY);}
2311  }
2312  *this=NewVVec;
2313 }
2314 
2315 template <class TVal>
2316 void TVVec<TVal>::GetRow(const int& RowN, TVec<TVal>& Vec) const {
2317  Vec.Gen(GetCols(), 0);
2318  for (int col = 0; col < GetCols(); col++) {
2319  Vec.Add(At(RowN, col));
2320  }
2321 }
2322 
2323 template <class TVal>
2324 void TVVec<TVal>::GetCol(const int& ColN, TVec<TVal>& Vec) const {
2325  Vec.Gen(GetRows(), 0);
2326  for (int row = 0; row < GetRows(); row++) {
2327  Vec.Add(At(row, ColN));
2328  }
2329 }
2330 
2332 // Common-2D-Vector-Types
2340 
2342 // 3D-Vector
2343 template <class TVal>
2344 class TVVVec{
2345 private:
2348 public:
2349  TVVVec(): XDim(), YDim(), ZDim(), ValV(){}
2350  TVVVec(const TVVVec& Vec):
2351  XDim(Vec.XDim), YDim(Vec.YDim), ZDim(Vec.ZDim), ValV(Vec.ValV){}
2352  TVVVec(const int& _XDim, const int& _YDim, const int& _ZDim):
2353  XDim(), YDim(), ZDim(), ValV(){Gen(_XDim, _YDim, _ZDim);}
2354  explicit TVVVec(TSIn& SIn):
2355  XDim(SIn), YDim(SIn), ZDim(SIn), ValV(SIn){}
2356  void Save(TSOut& SOut) const {
2357  XDim.Save(SOut); YDim.Save(SOut); ZDim.Save(SOut); ValV.Save(SOut);}
2358 
2360  XDim=Vec.XDim; YDim=Vec.YDim; ZDim=Vec.ZDim; ValV=Vec.ValV;
2361  return *this;
2362  }
2363  bool operator==(const TVVVec& Vec) const {
2364  return (XDim==Vec.XDim)&&(YDim==Vec.YDim)&&(ZDim==Vec.ZDim)&&
2365  (ValV==Vec.ValV);}
2366 
2367  bool Empty() const {return ValV.Len()==0;}
2368  void Clr(){XDim=0; YDim=0; ZDim=0; ValV.Clr();}
2369  void Gen(const int& _XDim, const int& _YDim, const int& _ZDim){
2370  Assert((_XDim>=0)&&(_YDim>=0)&&(_ZDim>=0));
2371  XDim=_XDim; YDim=_YDim; ZDim=_ZDim; ValV.Gen(XDim*YDim*ZDim);}
2372  TVal& At(const int& X, const int& Y, const int& Z){
2373  Assert((0<=X)&&(X<int(XDim))&&(0<=Y)&&(Y<int(YDim))&&(0<=Z)&&(Z<int(ZDim)));
2374  return ValV[X*YDim*ZDim+Y*ZDim+Z];}
2375  const TVal& At(const int& X, const int& Y, const int& Z) const {
2376  Assert((0<=X)&&(X<int(XDim))&&(0<=Y)&&(Y<int(YDim))&&(0<=Z)&&(Z<int(ZDim)));
2377  return ValV[X*YDim*ZDim+Y*ZDim+Z];}
2378  TVal& operator()(const int& X, const int& Y, const int& Z){
2379  return At(X, Y, Z);}
2380  const TVal& operator()(const int& X, const int& Y, const int& Z) const {
2381  return At(X, Y, Z);}
2382  int GetXDim() const {return XDim;}
2383  int GetYDim() const {return YDim;}
2384  int GetZDim() const {return ZDim;}
2385 };
2386 
2388 // Common-3D-Vector-Types
2391 
2393 // Tree
2394 template <class TVal>
2395 class TTree{
2396 private:
2397  TVec<TTriple<TInt, TIntV, TVal> > NodeV; // (ParentNodeId, ChildNodeIdV, NodeVal)
2398 public:
2399  TTree(): NodeV(){}
2400  TTree(const TTree& Tree): NodeV(Tree.NodeV){}
2401  explicit TTree(TSIn& SIn): NodeV(SIn){}
2402  void Save(TSOut& SOut) const {NodeV.Save(SOut);}
2403  void LoadXml(const PXmlTok& XmlTok, const TStr& Nm="");
2404  void SaveXml(TSOut& SOut, const TStr& Nm) const;
2405 
2406  TTree& operator=(const TTree& Tree){if (this!=&Tree){NodeV=Tree.NodeV;} return *this;}
2407  bool operator==(const TTree& Tree) const {return NodeV==Tree.NodeV;}
2408  bool operator<(const TTree& Tree) const {return false;}
2409 
2410  int GetPrimHashCd() const {return NodeV.GetPrimHashCd();}
2411  int GetSecHashCd() const {return NodeV.GetSecHashCd();}
2412 
2413  int GetMemUsed() const {return NodeV.GetMemUsed();}
2414 
2415  void Clr(){NodeV.Clr();}
2416 
2417  int AddNode(const int& ParentNodeId, const TVal& NodeVal=TVal()){
2418  IAssert(((ParentNodeId==-1)&&(NodeV.Len()==0))||(NodeV.Len()>0));
2419  if (ParentNodeId!=-1){NodeV[ParentNodeId].Val2.Add(NodeV.Len());}
2420  return NodeV.Add(TTriple<TInt, TIntV, TVal>(ParentNodeId, TIntV(), NodeVal));}
2421  int AddRoot(const TVal& NodeVal=TVal()){
2422  return AddNode(-1, NodeVal);}
2423 
2424  int GetNodes() const {return NodeV.Len();}
2425  void GetNodeIdV(TIntV& NodeIdV, const int& NodeId=0);
2426  int GetParentNodeId(const int& NodeId) const {return NodeV[NodeId].Val1;}
2427  int GetChildren(const int& NodeId) const {return NodeV[NodeId].Val2.Len();}
2428  int GetChildNodeId(const int& NodeId, const int& ChildN) const {return NodeV[NodeId].Val2[ChildN];}
2429  TVal& GetNodeVal(const int& NodeId){return NodeV[NodeId].Val3;}
2430 
2431  void GenRandomTree(const int& Nodes, TRnd& Rnd);
2432 
2433  void DelNode(const int& NodeId);
2434  void CopyTree(const int& SrcNodeId, TTree& DstTree, const int& DstParentNodeId=-1);
2435 
2436  void WrTree(const int& NodeId=0, const int& Lev=0);
2437 };
2438 
2439 template <class TVal>
2440 void TTree<TVal>::GetNodeIdV(TIntV& NodeIdV, const int& NodeId){
2441  if (NodeId==0){NodeIdV.Clr(); if (GetNodes()==0){return;}}
2442  else if (GetParentNodeId(NodeId)==-1){return;}
2443  NodeIdV.Add(NodeId);
2444  for (int ChildN=0; ChildN<GetChildren(NodeId); ChildN++){
2445  int ChildNodeId=GetChildNodeId(NodeId, ChildN);
2446  if (ChildNodeId!=-1){
2447  GetNodeIdV(NodeIdV, ChildNodeId);
2448  }
2449  }
2450 }
2451 
2452 template <class TVal>
2453 void TTree<TVal>::GenRandomTree(const int& Nodes, TRnd& Rnd){
2454  Clr();
2455  if (Nodes>0){
2456  AddRoot(TVal());
2457  for (int NodeN=1; NodeN<Nodes; NodeN++){
2458  int ParentNodeId=Rnd.GetUniDevInt(0, GetNodes()-1);
2459  AddNode(ParentNodeId, TVal());
2460  }
2461  }
2462 }
2463 
2464 template <class TVal>
2465 void TTree<TVal>::DelNode(const int& NodeId){
2466  if (NodeId==0){
2467  Clr();
2468  } else {
2469  TIntV& ChildNodeIdV=NodeV[GetParentNodeId(NodeId)].Val2;
2470  int ChildNodeIdN=ChildNodeIdV.SearchForw(NodeId);
2471  ChildNodeIdV[ChildNodeIdN]=-1;
2472  }
2473 }
2474 
2475 template <class TVal>
2476 void TTree<TVal>::CopyTree(const int& SrcNodeId, TTree& DstTree, const int& DstParentNodeId){
2477  int DstNodeId=DstTree.AddNode(DstParentNodeId, GetNodeVal(SrcNodeId));
2478  for (int ChildN=0; ChildN<GetChildren(SrcNodeId); ChildN++){
2479  int ChildNodeId=GetChildNodeId(SrcNodeId, ChildN);
2480  if (ChildNodeId!=-1){
2481  CopyTree(ChildNodeId, DstTree, DstNodeId);
2482  }
2483  }
2484 }
2485 
2486 template <class TVal>
2487 void TTree<TVal>::WrTree(const int& NodeId, const int& Lev){
2488  for (int LevN=0; LevN<Lev; LevN++){printf("| ");}
2489  printf("%d (%d)\n", NodeId, GetChildren(NodeId));
2490  for (int ChildN=0; ChildN<GetChildren(NodeId); ChildN++){
2491  int ChildNodeId=GetChildNodeId(NodeId, ChildN);
2492  if (ChildNodeId!=-1){
2493  WrTree(ChildNodeId, Lev+1);
2494  }
2495  }
2496 }
2497 
2499 // Common-Tree-Types
2505 
2507 // Stack
2508 template <class TVal>
2509 class TSStack{
2510 private:
2512 public:
2513  TSStack(): ValV(){}
2514  TSStack(const int& MxVals): ValV(MxVals, 0){}
2515  TSStack(const TSStack& Stack): ValV(Stack.ValV){}
2516  explicit TSStack(TSIn& SIn): ValV(SIn){}
2517  void Save(TSOut& SOut) const {ValV.Save(SOut);}
2518 
2519  TSStack& operator=(const TSStack& Stack){
2520  if (this!=&Stack){ValV=Stack.ValV;} return *this;}
2521  bool operator==(const TSStack& Stack) const {return this==&Stack;}
2522  const TVal& operator[](const int& ValN) const {return ValV[ValV.Len()-ValN-1];}
2523  TVal& operator[](const int& ValN) {return ValV[ValV.Len()-ValN-1];}
2524 
2525  bool Empty(){return ValV.Len()==0;}
2526  void Clr(const bool& DoDel=false) {ValV.Clr(DoDel);}
2527  bool IsIn(const TVal& Val) const {return ValV.IsIn(Val);}
2528  int Len(){return ValV.Len();}
2529  TVal& Top(){Assert(0<ValV.Len()); return ValV.Last();}
2530  const TVal& Top() const {Assert(0<ValV.Len()); return ValV.Last();}
2531  void Push(){ValV.Add();}
2532  void Push(const TVal& Val){ValV.Add(Val);}
2533  void Pop(){Assert(0<ValV.Len()); ValV.DelLast();}
2534 };
2535 
2537 // Common-Stack-Types
2540 
2542 // Queue
2543 template <class TVal>
2544 class TQQueue{
2545 private:
2549 public:
2550  TQQueue(const int& _MxLast=64, const int& _MxLen=-1):
2551  MxLast(_MxLast), MxLen(_MxLen), First(0), Last(0), ValV(){
2552  Assert(int(MxLast)>0); Assert((MxLen==-1)||(int(MxLen)>0));}
2553  TQQueue(const TQQueue& Queue):
2554  MxLast(Queue.MxLast), MxLen(Queue.MxLen),
2555  First(Queue.First), Last(Queue.Last), ValV(Queue.ValV){}
2556  explicit TQQueue(TSIn& SIn):
2557  MxLast(SIn), MxLen(SIn), First(SIn), Last(SIn), ValV(SIn){}
2558  void Save(TSOut& SOut) const {
2559  MxLast.Save(SOut); MxLen.Save(SOut);
2560  First.Save(SOut); Last.Save(SOut); ValV.Save(SOut);}
2561 
2562  TQQueue& operator=(const TQQueue& Queue){
2563  if (this!=&Queue){MxLast=Queue.MxLast; MxLen=Queue.MxLen;
2564  First=Queue.First; Last=Queue.Last; ValV=Queue.ValV;}
2565  return *this;}
2566  const TVal& operator[](const int& ValN) const {Assert((0<=ValN)&&(ValN<Len()));
2567  return ValV[Last+ValN];}
2568 
2569  void Clr(const bool& DoDel=true){ValV.Clr(DoDel); First=Last=0;}
2570  void Gen(const int& _MxLast=64, const int& _MxLen=-1){
2571  MxLast=_MxLast; MxLen=_MxLen; First=0; Last=0; ValV.Clr();}
2572  void GetSubValV(const int& _BValN, const int& _EValN, TVec<TVal>& SubValV) const {
2573  int BValN=TInt::GetMx(0, _BValN);
2574  int EValN=TInt::GetMn(Len()-1, _EValN);
2575  SubValV.Gen(EValN-BValN+1);
2576  for (int ValN=BValN; ValN<=EValN; ValN++){
2577  SubValV[ValN-BValN]=ValV[Last+ValN];}
2578  }
2579 
2580  bool Empty() const {return First==Last;}
2581  int Len() const {return First-Last;}
2582  const TVal& Top() const {
2583  Assert(First!=Last); return ValV[Last];}
2584  void Pop(){
2585  IAssert(First!=Last); Last++;
2586  if (First==Last){ValV.Clr(); First=Last=0;}}
2587  void Push(const TVal& Val){
2588  if (Last>MxLast){ValV.Del(0, Last-1); First-=Last; Last=0;}
2589  if ((MxLen!=-1)&&(MxLen==Len())){Pop();}
2590  First++; ValV.Add(Val);}
2591 
2592  void Shuffle(TRnd& Rnd){
2593  TVec<TVal> ValV(Len(), 0); while (!Empty()){ValV.Add(Top()); Pop();}
2594  ValV.Shuffle(Rnd); Clr();
2595  for (int ValN=0; ValN<ValV.Len(); ValN++){Push(ValV[ValN]);}}
2596 };
2597 
2599 // Common-Queue-Types
2608 
2610 // List-Node
2611 template <class TVal>
2612 class TLstNd{
2613 public:
2616  TVal Val;
2617 public:
2618  TLstNd(): PrevNd(NULL), NextNd(NULL), Val(){}
2619  TLstNd(const TLstNd&);
2620  TLstNd(TLstNd* _PrevNd, TLstNd* _NextNd, const TVal& _Val):
2621  PrevNd(_PrevNd), NextNd(_NextNd), Val(_Val){}
2622 
2623  TLstNd& operator=(const TLstNd&);
2624 
2625  bool IsPrev() const {return (PrevNd != NULL); }
2626  bool IsNext() const {return (NextNd != NULL); }
2627  TLstNd* Prev() const {Assert(this!=NULL); return PrevNd;}
2628  TLstNd* Next() const {Assert(this!=NULL); return NextNd;}
2629  TVal& GetVal(){Assert(this!=NULL); return Val;}
2630  const TVal& GetVal() const {Assert(this!=NULL); return Val;}
2631 };
2632 
2634 // List
2635 template <class TVal>
2636 class TLst{
2637 public:
2639 private:
2640  int Nds;
2643 public:
2644  TLst(): Nds(0), FirstNd(NULL), LastNd(NULL){}
2645  TLst(const TLst&);
2646  ~TLst(){Clr();}
2647  explicit TLst(TSIn& SIn);
2648  void Save(TSOut& SOut) const;
2649 
2650  TLst& operator=(const TLst&);
2651 
2652  void Clr(){
2653  PLstNd Nd=FirstNd;
2654  while (Nd!=NULL){PLstNd NextNd=Nd->NextNd; delete Nd; Nd=NextNd;}
2655  Nds=0; FirstNd=NULL; LastNd=NULL;}
2656 
2657  bool Empty() const {return Nds==0;}
2658  int Len() const {return Nds;}
2659  PLstNd First() const {return FirstNd;}
2660  PLstNd Last() const {return LastNd;}
2661  TVal& FirstVal() const {return FirstNd->GetVal();}
2662  TVal& LastVal() const {return LastNd->GetVal();}
2663 
2664  PLstNd AddFront(const TVal& Val);
2665  PLstNd AddBack(const TVal& Val);
2666  PLstNd AddFrontSorted(const TVal& Val, const bool& Asc=true);
2667  PLstNd AddBackSorted(const TVal& Val, const bool& Asc=true);
2668  void PutFront(const PLstNd& Nd);
2669  void PutBack(const PLstNd& Nd);
2670  PLstNd Ins(const PLstNd& Nd, const TVal& Val);
2671  void Del(const TVal& Val);
2672  void Del(const PLstNd& Nd);
2673  void DelFirst() { PLstNd DelNd = FirstNd; Del(DelNd); }
2674  void DelLast() { PLstNd DelNd = LastNd; Del(DelNd); }
2675 
2676  PLstNd SearchForw(const TVal& Val);
2677  PLstNd SearchBack(const TVal& Val);
2678 
2679  friend class TLstNd<TVal>;
2680 };
2681 
2682 template <class TVal>
2684  Nds(0), FirstNd(NULL), LastNd(NULL){
2685  int CheckNds=0; SIn.Load(CheckNds);
2686  for (int NdN=0; NdN<CheckNds; NdN++){AddBack(TVal(SIn));}
2687  Assert(Nds==CheckNds);
2688 }
2689 
2690 template <class TVal>
2691 void TLst<TVal>::Save(TSOut& SOut) const {
2692  SOut.Save(Nds);
2693  PLstNd Nd=FirstNd; int CheckNds=0;
2694  while (Nd!=NULL){
2695  Nd->Val.Save(SOut); Nd=Nd->NextNd; CheckNds++;}
2696  IAssert(Nds==CheckNds);
2697 }
2698 
2699 template <class TVal>
2701  PLstNd Nd=new TLstNd<TVal>(NULL, FirstNd, Val);
2702  if (FirstNd!=NULL){FirstNd->PrevNd=Nd; FirstNd=Nd;}
2703  else {FirstNd=Nd; LastNd=Nd;}
2704  Nds++; return Nd;
2705 }
2706 
2707 template <class TVal>
2709  PLstNd Nd=new TLstNd<TVal>(LastNd, NULL, Val);
2710  if (LastNd!=NULL){LastNd->NextNd=Nd; LastNd=Nd;}
2711  else {FirstNd=Nd; LastNd=Nd;}
2712  Nds++; return Nd;
2713 }
2714 
2715 template <class TVal>
2716 TLstNd<TVal>* TLst<TVal>::AddFrontSorted(const TVal& Val, const bool& Asc){
2717  PLstNd Nd=First();
2718  if (Nd==NULL){
2719  return Ins(Nd, Val);
2720  } else {
2721  while ((Nd!=NULL)&&((Asc&&(Val>Nd()))||(!Asc&&(Val<Nd())))){
2722  Nd=Nd->Next();}
2723  if (Nd==NULL){return Ins(Nd->Last(), Val);}
2724  else {return Ins(Nd->Prev(), Val);}
2725  }
2726 }
2727 
2728 template <class TVal>
2729 TLstNd<TVal>* TLst<TVal>::AddBackSorted(const TVal& Val, const bool& Asc){
2730  PLstNd Nd=Last();
2731  while ((Nd!=NULL)&&((Asc&&(Val<Nd->Val))||(!Asc&&(Val>Nd->Val)))){
2732  Nd=Nd->Prev();}
2733  return Ins(Nd, Val);
2734 }
2735 
2736 template <class TVal>
2738  Assert(Nd!=NULL);
2739  // unchain
2740  if (Nd->PrevNd==NULL){FirstNd=Nd->NextNd;}
2741  else {Nd->PrevNd->NextNd=Nd->NextNd;}
2742  if (Nd->NextNd==NULL){LastNd=Nd->PrevNd;}
2743  else {Nd->NextNd->PrevNd=Nd->PrevNd;}
2744  // add to front
2745  Nd->PrevNd=NULL; Nd->NextNd=FirstNd;
2746  if (FirstNd!=NULL){FirstNd->PrevNd=Nd; FirstNd=Nd;}
2747  else {FirstNd=Nd; LastNd=Nd;}
2748 }
2749 
2750 template <class TVal>
2751 void TLst<TVal>::PutBack(const PLstNd& Nd){
2752  Assert(Nd!=NULL);
2753  // unchain
2754  if (Nd->PrevNd==NULL){FirstNd=Nd->NextNd;}
2755  else {Nd->PrevNd->NextNd=Nd->NextNd;}
2756  if (Nd->NextNd==NULL){LastNd=Nd->PrevNd;}
2757  else {Nd->NextNd->PrevNd=Nd->PrevNd;}
2758  // add to back
2759  Nd->PrevNd=LastNd; Nd->NextNd=NULL;
2760  if (LastNd!=NULL){LastNd->NextNd=Nd; LastNd=Nd;}
2761  else {FirstNd=Nd; LastNd=Nd;}
2762 }
2763 
2764 template <class TVal>
2765 TLstNd<TVal>* TLst<TVal>::Ins(const PLstNd& Nd, const TVal& Val){
2766  if (Nd==NULL){return AddFront(Val);}
2767  else if (Nd->NextNd==NULL){return AddBack(Val);}
2768  else {
2769  PLstNd NewNd=new TLstNd<TVal>(Nd, Nd->NextNd, Val);
2770  Nd->NextNd=NewNd; NewNd->NextNd->PrevNd=Nd;
2771  Nds++; return Nd;
2772  }
2773 }
2774 
2775 template <class TVal>
2776 void TLst<TVal>::Del(const TVal& Val){
2777  PLstNd Nd=SearchForw(Val);
2778  if (Nd!=NULL){Del(Nd);}
2779 }
2780 
2781 template <class TVal>
2782 void TLst<TVal>::Del(const PLstNd& Nd){
2783  Assert(Nd!=NULL);
2784  if (Nd->PrevNd==NULL){FirstNd=Nd->NextNd;}
2785  else {Nd->PrevNd->NextNd=Nd->NextNd;}
2786  if (Nd->NextNd==NULL){LastNd=Nd->PrevNd;}
2787  else {Nd->NextNd->PrevNd=Nd->PrevNd;}
2788  Nds--; delete Nd;
2789 }
2790 
2791 template <class TVal>
2793  PLstNd Nd=First();
2794  while (Nd!=NULL){
2795  if (Nd->GetVal()==Val){return Nd;}
2796  Nd=Nd->Next();
2797  }
2798  return NULL;
2799 }
2800 
2801 template <class TVal>
2803  PLstNd Nd=Last();
2804  while (Nd!=NULL){
2805  if (Nd->GetVal()==Val){return Nd;}
2806  Nd=Nd->Prev();
2807  }
2808  return NULL;
2809 }
2810 
2812 // Common-List-Types
2825 
2827 // Record-File
2828 template <class THd, class TRec>
2829 class TFRec{
2830 private:
2832 public:
2833  TFRec(const TStr& FNm, const TFAccess& FAccess, const bool& CreateIfNo):
2834  FRnd(PFRnd(new TFRnd(FNm, FAccess, CreateIfNo, sizeof(THd), sizeof(TRec)))){}
2835  TFRec(const TFRec&);
2836 
2837  TFRec& operator=(const TFRec&);
2838 
2839  void SetRecN(const int& RecN){FRnd->SetRecN(RecN);}
2840  int GetRecN(){return FRnd->GetRecN();}
2841  int GetRecs(){return FRnd->GetRecs();}
2842 
2843  void GetHd(THd& Hd){FRnd->GetHd(&Hd);}
2844  void PutHd(const THd& Hd){FRnd->PutHd(&Hd);}
2845  void GetRec(TRec& Rec, const int& RecN=-1){FRnd->GetRec(&Rec, RecN);}
2846  void PutRec(const TRec& Rec, const int& RecN=-1){FRnd->PutRec(&Rec, RecN);}
2847 };
2848 
2850 // Function
2851 template <class TFuncPt>
2852 class TFunc{
2853 private:
2854  TFuncPt FuncPt;
2855 public:
2856  TFunc(): FuncPt(NULL){}
2857  TFunc(const TFunc& Func): FuncPt(Func.FuncPt){}
2858  TFunc(const TFuncPt& _FuncPt): FuncPt(_FuncPt){}
2860  void Save(TSOut&) const {Fail;}
2861 
2862  TFunc& operator=(const TFunc& Func){
2863  if (this!=&Func){FuncPt=Func.FuncPt;} return *this;}
2864  bool operator==(const TFunc& Func) const {
2865  return FuncPt==Func.FuncPt;}
2866  bool operator<(const TFunc&) const {
2867  Fail; return false;}
2868  TFuncPt operator()() const {return FuncPt;}
2869 };
Definition: ds.h:345
TSizeTy AddUnique(const TVal &Val)
Adds element Val to a vector only if the element Val is not already in the vector.
Definition: ds.h:1104
~TVec()
Definition: ds.h:457
void Save(TSOut &SOut) const
Definition: ds.h:2010
bool operator==(const TTree &Tree) const
Definition: ds.h:2407
Definition: bd.h:440
#define IAssert(Cond)
Definition: bd.h:262
TQuad< TStr, TStr, TStr, TStr > TStrQu
Definition: ds.h:261
TVec< TFltIntKd > TFltIntKdV
Definition: ds.h:1574
TFuncPt FuncPt
Definition: ds.h:2854
TVec< TVal > V
Definition: ds.h:2114
bool operator==(const TVVec &Vec) const
Definition: ds.h:2176
void SaveXml(TSOut &SOut, const TStr &Nm) const
Definition: xmlser.h:113
void GetV(const int &VId, TValV &ValV) const
Returns ValV which is a reference (not a copy) to vector with id VId.
Definition: ds.h:1671
const TVal & GetVal() const
Definition: ds.h:2630
TQQueue< TAscFltV > TAscFltVQ
Definition: ds.h:2606
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
TStr GetTypeNm(const Type &Var)
Definition: ut.h:16
bool operator==(const PVec< TVal > &Vec) const
Definition: ds.h:2132
::TSize GetVecs() const
Definition: ds.h:1900
TTriple(const TTriple &Triple)
Definition: ds.h:136
TPair(const TPair &Pair)
Definition: ds.h:38
TVec< TUInt64IntPr > TUInt64IntPrV
Definition: ds.h:1567
::TSize Vals
Definition: ds.h:1880
bool DelIfIn(const TVal &Val)
Removes the first occurrence of element Val.
Definition: ds.h:1151
TTree< TStrIntStrVTr > TStrIntStrVTrTree
Definition: ds.h:2504
TIter EndI() const
Returns an iterator referring to the past-the-end element in the vector.
Definition: ds.h:567
TStr GetStr() const
Definition: ds.h:62
TPair< TUCh, TStr > TUChStrPr
Definition: ds.h:80
TVec< TSFlt > TSFltV
Definition: ds.h:1532
TVec< TUInt > TUIntV
Definition: ds.h:1528
TTriple(TSIn &SIn)
Definition: ds.h:140
bool operator()(const TTriple< TVal1, TVal2, TVal3 > &T1, const TTriple< TVal1, TVal2, TVal3 > &T2) const
Definition: ds.h:210
bool Empty()
Definition: ds.h:2525
TTriple< TStr, TStr, TInt > TStrStrIntTr
Definition: ds.h:188
TPair(const TVal1 &_Val1, const TVal2 &_Val2)
Definition: ds.h:39
TVal & operator()(const int &X, const int &Y)
Definition: ds.h:2196
TVVec< TCh > TChVV
Definition: ds.h:2334
TSizeTy Reserved() const
Returns the size of allocated storage capacity.
Definition: ds.h:549
TVec< TIntIntVIntTr > TIntIntVIntTrV
Definition: ds.h:1565
TStr GetStr() const
Definition: dt.h:1107
#define IAssertR(Cond, Reason)
Definition: bd.h:265
TPair< TUInt, TUInt > TUIntUIntPr
Definition: ds.h:91
TVec< TFltIntIntTr > TFltIntIntTrV
Definition: ds.h:1580
TVec< TIntIntFltTr > TIntIntFltTrV
Definition: ds.h:1554
void Save(TSOut &) const
Definition: ds.h:2860
TPair< TFlt, TInt > TFltIntPr
Definition: ds.h:97
PVec< TStr > TStrVP
Definition: ds.h:2151
int GetPrimHashCd() const
Returns primary hash code of the vector. Used by THash.
Definition: ds.h:948
TTriple< TInt, TStr, TInt > TIntStrIntTr
Definition: ds.h:172
void PutX(const int &X, const TVal &Val)
Definition: ds.h:2203
bool operator<(const TFunc &) const
Definition: ds.h:2866
void SaveXml(TSOut &SOut, const TStr &Nm) const
Definition: xmlser.h:99
bool operator==(const TQuad &Quad) const
Definition: ds.h:242
PLstNd Ins(const PLstNd &Nd, const TVal &Val)
Definition: ds.h:2765
TPair()
Definition: ds.h:37
bool operator()(const TPair< TVal1, TVal2 > &P1, const TPair< TVal1, TVal2 > &P2) const
Definition: ds.h:121
void Save(TSOut &SOut) const
Definition: ds.h:41
void Merge()
Sorts the vector and only keeps a single element of each value.
Definition: ds.h:1292
TVec< TAscFltIntKd > TAscFltIntKdV
Definition: ds.h:1583
TInt Last
Definition: ds.h:2547
TTree()
Definition: ds.h:2399
Definition: ds.h:2110
TTuple & operator=(const TTuple &Tup)
Definition: ds.h:285
int64 GetUniDevInt64(const int64 &Range=0)
Definition: dt.cpp:51
void Save(TSOut &SOut) const
Definition: ds.h:2171
static int GetInRng(const int &Val, const int &Mn, const int &Mx)
Definition: dt.h:1104
bool operator==(const TVVVec &Vec) const
Definition: ds.h:2363
TVec< TAscFlt > TAscFltV
Definition: ds.h:1533
TQQueue & operator=(const TQQueue &Queue)
Definition: ds.h:2562
Definition: ds.h:271
TLstNd< TFlt > * PFltLN
Definition: ds.h:2818
TTriple< TStr, TInt, TStrV > TStrIntStrVTr
Definition: ds.h:189
bool Empty() const
Definition: ds.h:2580
TPair< TStr, TStr > TStrPr
Definition: ds.h:107
TVVec< TVal > & operator=(const TVVec< TVal > &Vec)
Definition: ds.h:2174
TPair< TStr, TFlt > TStrFltPr
Definition: ds.h:106
TVal & At(const int &X, const int &Y)
Definition: ds.h:2193
PLstNd SearchBack(const TVal &Val)
Definition: ds.h:2802
Definition: dt.h:11
TSStack(const TSStack &Stack)
Definition: ds.h:2515
uint64 Reserved() const
Returns the total capacity of the pool.
Definition: ds.h:1645
TPair< TUInt64, TFlt > TUInt64FltPr
Definition: ds.h:95
void DelNode(const int &NodeId)
Definition: ds.h:2465
static PVecPool New(const TSize &ExpectVals=0, const TSize &GrowBy=1000000, const bool &FastCopy=false)
Definition: ds.h:1631
TFunc(TSIn &)
Definition: ds.h:2859
TInt YDim
Definition: ds.h:2159
void LoadXml(const PXmlTok &XmlTok, const TStr &Nm="")
Definition: xmlser.h:95
Definition: ds.h:129
void Del(const TSizeTy &ValN)
Removes the element at position ValN.
Definition: ds.h:1130
int GetYDim() const
Definition: ds.h:2383
TTuple(TSIn &SIn)
Definition: ds.h:278
TVec< TStrPr > TStrPrV
Definition: ds.h:1584
void Save(TSOut &SOut) const
Definition: ds.h:2128
TVec< TStrFltKd > TStrFltKdV
Definition: ds.h:1588
TVec< TVal > ValV
Definition: ds.h:2347
int AddV(const TValV &ValV)
Adds vector ValV to the pool and returns its id.
Definition: ds.h:1804
int AddNode(const int &ParentNodeId, const TVal &NodeVal=TVal())
Definition: ds.h:2417
TVVVec(TSIn &SIn)
Definition: ds.h:2354
TKeyDat< TFlt, TBool > TFltBoolKd
Definition: ds.h:390
TTree< TStr > TStrTree
Definition: ds.h:2502
TVec< TIntIntStrTr > TIntIntStrTrV
Definition: ds.h:1553
void Push(const TVal &Val)
Definition: ds.h:2532
TVal & FirstVal() const
Definition: ds.h:2661
void Save(TSOut &SOut) const
Definition: dt.h:1060
bool operator()(const TKeyDat< TVal1, TVal2 > &P1, const TKeyDat< TVal1, TVal2 > &P2) const
Definition: ds.h:414
bool operator==(const TSStack &Stack) const
Definition: ds.h:2521
bool IsIn(const TVal &Val) const
Checks whether element Val is a member of the vector.
Definition: ds.h:797
bool operator<(const TVec< TVal, TSizeTy > &Vec) const
Lexicographically compares two vectors.
Definition: ds.h:930
void GetHd(THd &Hd)
Definition: ds.h:2843
TVecPool & operator=(const TVecPool &Pool)
Definition: ds.h:2023
#define forever
Definition: bd.h:6
void Reserve(const ::TSize &MxVals)
Definition: ds.h:1903
int Nds
Definition: ds.h:2640
TKeyDat(const TKey &_Key)
Definition: ds.h:352
unsigned int uint
Definition: bd.h:11
TVec< TIntStrKd > TIntStrKdV
Definition: ds.h:1562
int GetParentNodeId(const int &NodeId) const
Definition: ds.h:2426
void Save(TSOut &) const
Definition: ds.h:12
int GetChildNodeId(const int &NodeId, const int &ChildN) const
Definition: ds.h:2428
TFunc(const TFuncPt &_FuncPt)
Definition: ds.h:2858
static int GetMx(const int &Int1, const int &Int2)
Definition: dt.h:1092
TVal & Top()
Definition: ds.h:2529
TVVVec(const int &_XDim, const int &_YDim, const int &_ZDim)
Definition: ds.h:2352
static const int Mx
Definition: dt.h:1049
#define Fail
Definition: bd.h:238
void QSort(const TSizeTy &MnLValN, const TSizeTy &MxRValN, const bool &Asc)
Quick sorts the values between positions MnLValN...MxLValN.
Definition: ds.h:1241
PLstNd AddBackSorted(const TVal &Val, const bool &Asc=true)
Definition: ds.h:2729
void Load(TSIn &SIn)
Definition: ds.h:2170
::TSize GetVals() const
Definition: ds.h:1899
const TVal1 & GetVal1() const
Definition: ds.h:60
TVal * ValBf
Definition: ds.h:1616
uint64 GetMemUsed() const
Returns the total memory footprint (in bytes) of the pool.
Definition: ds.h:1653
bool IsSortedCmp(const TCmp &Cmp) const
Checks whether the vector is sorted according to the comparator Cmp.
Definition: ds.h:742
TKeyDat< TUInt64, TFlt > TUInt64FltKd
Definition: ds.h:388
void SetEmptyVal(const TVal &_EmptyVal)
Definition: ds.h:1905
PVec< TAscFlt > TAscFltVP
Definition: ds.h:2149
TVal2 Val2
Definition: ds.h:221
TFunc(const TFunc &Func)
Definition: ds.h:2857
TVal & LastVal() const
Definition: ds.h:2662
TVec< TVal > TValV
Definition: ds.h:1876
int Len() const
Definition: ds.h:2658
void CopyFrom(const TVVec< TVal > &VVec)
Definition: ds.h:2267
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
TKeyDat< TInt, TFlt > TIntFltKd
Definition: ds.h:380
TVec< TIntIntIntVTr > TIntIntIntVTrV
Definition: ds.h:1566
TVec< TFltTr > TFltTrV
Definition: ds.h:1540
void PutRec(const void *Rec, const int &RecN=-1)
Definition: fl.h:561
TLstNd * Prev() const
Definition: ds.h:2627
int GetSecHashCd() const
Returns secondary hash code of the vector. Used by THash.
Definition: ds.h:960
TVVec(const int &_XDim, const int &_YDim)
Definition: ds.h:2165
void GetSwitchedKdV(const TVec< TKeyDat< TKey, TDat >, int > &SrcKdV, TVec< TKeyDat< TDat, TKey >, int > &DstKdV)
Definition: ds.h:369
void Diff(const TVec< TVal, TSizeTy > &ValV)
Subtracts ValV from this vector. Assumes the vectors are sorted!
Definition: ds.h:1360
const TVal3 & GetVal3() const
Definition: ds.h:256
void Save(TSOut &SOut) const
Definition: ds.h:2691
void GenExt(TVal *_ValT, const TSizeTy &_Vals)
Constructs a vector of _Vals elements of memory array _ValT.
Definition: ds.h:506
TTriple< TInt, TInt, TFlt > TIntIntFltTr
Definition: ds.h:174
Definition: ds.h:2636
TVec< TIntKd > TIntKdV
Definition: ds.h:1541
void GetVal(TVal1 &_Val1, TVal2 &_Val2) const
Definition: ds.h:59
TKeyDat< TInt, TFltPr > TIntFltPrKd
Definition: ds.h:382
TVec< TFltStrPr > TFltStrPrV
Definition: ds.h:1550
TKeyDat< TUInt64, TStr > TUInt64StrKd
Definition: ds.h:389
TTree(TSIn &SIn)
Definition: ds.h:2401
TInt ZDim
Definition: ds.h:2346
TVec< TIntFltPrKd > TIntFltPrKdV
Definition: ds.h:1547
TLstNd * NextNd
Definition: ds.h:2615
TVec< TStrTr > TStrTrV
Definition: ds.h:1590
TTriple(const TVal1 &_Val1, const TVal2 &_Val2, const TVal3 &_Val3)
Definition: ds.h:138
TRec * operator->() const
Definition: ds.h:20
TKeyDat< TFlt, TStr > TFltStrKd
Definition: ds.h:396
TVal & operator()(const int &X, const int &Y, const int &Z)
Definition: ds.h:2378
void Save(TSOut &SOut) const
Definition: ds.h:141
bool IsVId(const int &VId) const
Definition: ds.h:1901
TSizeTy AddMP(const TVal &Val)
Adds element Val at the end of the vector in a thread safe manner, returns the element index in the v...
Definition: ds.h:589
TVal3 Val3
Definition: ds.h:222
void Save(TSOut &SOut) const
Definition: ds.h:2558
void PutRec(const TRec &Rec, const int &RecN=-1)
Definition: ds.h:2846
TVal & GetRndVal(TRnd &Rnd=TInt::Rnd)
Returns a reference to a random element in the vector.
Definition: ds.h:563
int AddV(const TValV &ValV)
Definition: ds.h:2043
TVec< TFltPr > TFltPrV
Definition: ds.h:1539
void SetEmptyVal(const TVal &_EmptyVal)
Sets the empty value.
Definition: ds.h:1651
TVec< TStrQu > TStrQuV
Definition: ds.h:1591
Vector Pool.
Definition: ds.h:1607
void Save(TSOut &SOut) const
Definition: ds.h:1769
TCRef CRef
Definition: ds.h:2112
TVec< TStrAscFltKd > TStrAscFltKdV
Definition: ds.h:1589
TDat Dat
Definition: ds.h:348
TKeyDat< TStr, TInt > TStrIntKd
Definition: ds.h:401
void SetRecN(const int &RecN)
Definition: ds.h:2839
TTriple< TInt, TFlt, TInt > TIntFltIntTr
Definition: ds.h:175
int GetXDim() const
Definition: ds.h:2184
TVec< TIntIntPrPr > TIntIntPrPrV
Definition: ds.h:1599
Definition: ds.h:2612
TPair< TStrV, TInt > TStrVIntPr
Definition: ds.h:109
int GetSecHashCd() const
Definition: ds.h:300
TVec< ::TSize > IdToOffV
Definition: ds.h:1883
TQQueue< TIntPr > TIntPrQ
Definition: ds.h:2603
const TVal2 & GetVal2() const
Definition: ds.h:61
TVal1 Val1
Definition: ds.h:131
void Load(TSIn &SIn)
Definition: ds.h:280
TSizeTy Add(const TVal &Val, const TSizeTy &ResizeLen)
Adds element Val at the end of the vector. #TVec::Add2.
Definition: ds.h:585
PLstNd LastNd
Definition: ds.h:2642
TVec< TVal > ValV
Definition: ds.h:2548
TSizeTy SearchBinLeft(const TVal &Val, TSizeTy &InsValN) const
Returns the position of an element with value Val.
Definition: ds.h:1476
void Clr(const bool &DoDel=false)
Definition: ds.h:2526
bool operator==(const TKeyDat &KeyDat) const
Definition: ds.h:361
int GetSecHashCd() const
Definition: ds.h:250
void Swap(const TSizeTy &ValN1, const TSizeTy &ValN2)
Swaps elements at positions ValN1 and ValN2.
Definition: ds.h:644
TKeyDat< TFlt, TFlt > TFltKd
Definition: ds.h:395
TVec< TStrStrVPr > TStrStrVPrV
Definition: ds.h:1595
TTriple< TStr, TInt, TInt > TStrIntIntTr
Definition: ds.h:186
void CompactPool(const TVal &DelVal)
Deletes all elements of value DelVal from all vectors.
Definition: ds.h:1824
TVal & At(const int &X, const int &Y, const int &Z)
Definition: ds.h:2372
TSStack()
Definition: ds.h:2513
TPair< TUInt, TInt > TUIntIntPr
Definition: ds.h:92
void Load(TSIn &SIn)
Definition: ds.h:895
PLstNd AddFront(const TVal &Val)
Definition: ds.h:2700
TQQueue(TSIn &SIn)
Definition: ds.h:2556
void CopyUniqueFrom(TVec< TVal, TSizeTy > &Vec, TInt Offset, TInt Sz)
Copy Sz values from Vec starting at Offset.
Definition: ds.h:1029
void Resize(const TSize &_MxVals)
Definition: ds.h:1705
static TRnd Rnd
Definition: dt.h:1053
int FindMx() const
Definition: ds.h:319
TVec(TVal *_ValT, const TSizeTy &_Vals)
Constructs a vector of _Vals elements of memory array _ValT.
Definition: ds.h:455
TPair< TInt, TVec< TInt, int > > TIntIntVPr
Definition: ds.h:86
::TSize GrowBy
Definition: ds.h:1880
TVal & LastLast()
Returns a reference to the one before last element of the vector.
Definition: ds.h:559
TRec * operator()() const
Definition: ds.h:24
TSizeTy GetMemUsed() const
Returns the memory footprint (the number of bytes) of the vector.
Definition: ds.h:483
TVal & operator[](const int &ValN)
Definition: ds.h:2523
void PutFront(const PLstNd &Nd)
Definition: ds.h:2737
Definition: fl.h:275
TVec< TIntTr > TIntTrV
Definition: ds.h:1537
TPair(TSIn &SIn)
Definition: ds.h:40
TSizeTy AddVMerged(const TVec< TVal, TSizeTy > &ValV)
Adds elements of ValV to a sorted vector only if a particular element is not already in the vector...
Definition: ds.h:1097
TVal1 Val1
Definition: ds.h:220
TLst & operator=(const TLst &)
bool operator<(const TKeyDat &KeyDat) const
Definition: ds.h:362
void Reduce(const TSizeTy &_Vals=-1)
Reduces the vector's length to _Vals elements, which must be less than the current length...
Definition: ds.h:528
static PVecPool Load(const TStr &FNm)
Definition: ds.h:1894
TPt< TVecPool< TVal, TSizeTy > > PVecPool
Definition: ds.h:1609
void GetCol(const int &ColN, TVec< TVal > &Vec) const
Definition: ds.h:2324
bool operator()(const TTriple< TVal1, TVal2, TVal3 > &T1, const TTriple< TVal1, TVal2, TVal3 > &T2) const
Definition: ds.h:198
TPair< TInt, TUInt64 > TIntUInt64Pr
Definition: ds.h:84
void GetNodeIdV(TIntV &NodeIdV, const int &NodeId=0)
Definition: ds.h:2440
TSize GetVals() const
Returns the total number of values stored in the vector pool.
Definition: ds.h:1641
void PutAll(const TVal &Val)
Definition: ds.h:1938
TVec< TStrKd > TStrKdV
Definition: ds.h:1594
TInt XDim
Definition: ds.h:2159
TVecPool & operator=(const TVecPool &Pool)
Definition: ds.h:1782
TSize GrowBy
Definition: ds.h:1614
TKeyDat< TFlt, TIntBoolPr > TFltIntBoolPrKd
Definition: ds.h:398
const TVal & GetEmptyVal() const
Definition: ds.h:1904
bool operator==(const TAPt &Pt) const
Definition: ds.h:16
TSizeTy AddSorted(const TVal &Val, const bool &Asc=true, const TSizeTy &_MxVals=-1)
Adds element Val to a sorted vector.
Definition: ds.h:1063
void DelLast()
Definition: ds.h:2674
TPair< TAscFlt, TInt > TAscFltIntPr
Definition: ds.h:101
TInt First
Definition: ds.h:2547
TKeyDat< TInt, TStr > TIntStrKd
Definition: ds.h:384
int GetSecHashCd() const
Definition: ds.h:156
void PutXY(const int &X, const int &Y, const TVal &Val)
Definition: ds.h:2201
int AddEmptyV(const int &ValVLen)
Adds a vector of length ValVLen to the pool and returns its id.
Definition: ds.h:1815
TQuad< TInt, TInt, TInt, TInt > TIntQu
Definition: ds.h:262
void Clr(bool DoDel=true)
Definition: ds.h:1933
TSStack(TSIn &SIn)
Definition: ds.h:2516
bool Empty() const
Definition: ds.h:2657
TPair< TInt, TBool > TIntBoolPr
Definition: ds.h:81
TVal & operator[](const int &ValN)
Definition: ds.h:283
TVVec(TSIn &SIn)
Definition: ds.h:2169
Definition: fl.h:58
void Save(TSOut &SOut) const
Definition: ds.h:903
const TVal & Top() const
Definition: ds.h:2530
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:542
int GetRecs()
Definition: ds.h:2841
void LoadXml(const PXmlTok &XmlTok, const TStr &Nm="")
Definition: xmlser.h:87
void Save(TSOut &SOut) const
Definition: ds.h:233
TPair< TInt, TStrV > TIntStrVPr
Definition: ds.h:89
void Reverse(TSizeTy LValN, TSizeTy RValN)
Reverses the order of elements between LValN...RValN.
Definition: ds.h:692
TTriple< TFlt, TFlt, TStr > TFltFltStrTr
Definition: ds.h:183
void PutV(const int &VId, const TValV &ValV)
Sets the values of vector VId with those in ValV.
Definition: ds.h:1675
void DelAll(const TVal &Val)
Removes all occurrences of element Val.
Definition: ds.h:1159
void Intrs(const TVec< TVal, TSizeTy > &ValV)
Sets this vector to its intersection with ValV. Assumes the vectors are sorted!
Definition: ds.h:1346
TVec< TIntUInt64Pr > TIntUInt64PrV
Definition: ds.h:1544
int GetRecN()
Definition: ds.h:2840
TKeyDat< TStr, TFlt > TStrFltKd
Definition: ds.h:402
const TVal & operator[](const int &ValN) const
Definition: ds.h:2522
void Clr()
Definition: ds.h:2368
TLstNd & operator=(const TLstNd &)
int GetNodes() const
Definition: ds.h:2424
TPair< TInt, TFlt > TIntFltPr
Definition: ds.h:87
uint GetVLen(const int &VId) const
Definition: ds.h:1911
void Swap(TVec< TVal, TSizeTy > &Vec)
Swaps the contents of the vector with Vec.
Definition: ds.h:1047
TVal Val
Definition: ds.h:2616
TVec< TIntFltPr > TIntFltPrV
Definition: ds.h:1546
void GetVal(TVal1 &_Val1, TVal2 &_Val2, TVal3 &_Val3, TVal4 &_Val4) const
Definition: ds.h:252
const TVal & GetRndVal(TRnd &Rnd=TInt::Rnd) const
Returns a reference to a random element in the vector.
Definition: ds.h:561
void Shuffle(TRnd &Rnd)
Definition: ds.h:2592
int Len() const
Definition: ds.h:2137
TVec< TFltIntPrKd > TFltIntPrKdV
Definition: ds.h:1576
void Clr()
Definition: ds.h:2180
TSStack(const int &MxVals)
Definition: ds.h:2514
int FindMn() const
Definition: ds.h:331
TTree & operator=(const TTree &Tree)
Definition: ds.h:2406
TQQueue< TInt > TIntQ
Definition: ds.h:2600
TSizeTy Add(TVal &Val)
Definition: ds.h:582
void Pop()
Definition: ds.h:2584
TVec< TStrFltPr > TStrFltPrV
Definition: ds.h:1586
TQuad< TStr, TStr, TInt, TInt > TStrStrIntIntQu
Definition: ds.h:260
int GetMemUsed() const
Definition: ds.h:54
TKeyDat()
Definition: ds.h:350
int GetPrimHashCd() const
Definition: ds.h:249
TVec< TVal, TSizeTy > TValV
Definition: ds.h:1610
TSizeTy MoveLastMP(const TVal &Val, int Inc)
Reserves space after the current last element in a thread safe manner, returning the old vector size...
Definition: ds.h:594
TVal2 Val2
Definition: ds.h:132
TVal EmptyVal
Definition: ds.h:1615
TSizeTy GetPivotValN(const TSizeTy &LValN, const TSizeTy &RValN) const
Picks three random elements at positions LValN...RValN and returns the middle one.
Definition: ds.h:1201
const TVal & GetVal(const TSizeTy &ValN) const
Returns a reference to the element at position ValN in the vector.
Definition: ds.h:621
TTriple< TFlt, TFlt, TInt > TFltFltIntTr
Definition: ds.h:182
void Gen(const int &_MxLast=64, const int &_MxLen=-1)
Definition: ds.h:2570
TVec< TStrFltFltTr > TStrFltFltTrV
Definition: ds.h:1592
TAPt(TSIn &)
Definition: ds.h:11
void Save(TSOut &SOut) const
Definition: ds.h:2356
TKeyDat< TAscFlt, TInt > TAscFltIntKd
Definition: ds.h:399
void SaveXml(TSOut &SOut, const TStr &Nm) const
Definition: xmlser.h:83
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:971
PLstNd FirstNd
Definition: ds.h:2641
TAPt()
Definition: ds.h:8
bool IsAsc
Definition: ds.h:207
::TSize MxVals
Definition: ds.h:1880
void PutHd(const void *Hd)
Definition: fl.h:557
TVec< TUInt64IntKd > TUInt64IntKdV
Definition: ds.h:1570
TPair< TFlt, TStrPr > TFltStrPrPr
Definition: ds.h:112
bool operator==(const TFunc &Func) const
Definition: ds.h:2864
TVec< TIntPrFltKd > TIntPrFltKdV
Definition: ds.h:1561
TTriple< TCh, TCh, TCh > TChTr
Definition: ds.h:167
TVal * GetValVPt(const int &VId) const
Returns pointer to the first element of the vector with id VId.
Definition: ds.h:1665
TVec< TAscFltStrPr > TAscFltStrPrV
Definition: ds.h:1551
TVec< TStrVIntPr > TStrVIntPrV
Definition: ds.h:1596
static int GetMn(const int &Int1, const int &Int2)
Definition: dt.h:1090
TTree< TStrIntPr > TStrIntPrTree
Definition: ds.h:2503
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
bool operator==(const TTuple &Tup) const
Definition: ds.h:287
bool NextPerm()
Generates next permutation of the elements in the vector.
Definition: ds.h:1303
TVVec(const TVVec &Vec)
Definition: ds.h:2163
TVec< TIntPr > TIntPrV
Definition: ds.h:1536
TVec< TVal, TSizeTy > & operator=(const TVec< TVal, TSizeTy > &Vec)
Assigns new contents to the vector, replacing its current content.
Definition: ds.h:910
TVec< TUIntIntKd > TUIntIntKdV
Definition: ds.h:1559
Definition: ds.h:2157
TAPt(TRec *_Addr)
Definition: ds.h:10
TPt< TVecPool< TVal > > PVecPool
Definition: ds.h:1875
const TVal1 & GetVal1() const
Definition: ds.h:162
TPt< TFltVP > PFltV
Definition: ds.h:2148
~TLst()
Definition: ds.h:2646
int GetSecHashCd() const
Definition: ds.h:2411
void PutAll(const TVal &Val)
Sets all elements of the vector to value Val.
Definition: ds.h:1166
TVec< TStrStrIntTr > TStrStrIntTrV
Definition: ds.h:1593
TFRec(const TStr &FNm, const TFAccess &FAccess, const bool &CreateIfNo)
Definition: ds.h:2833
unsigned long long uint64
Definition: bd.h:38
void GetHd(void *Hd)
Definition: fl.h:555
TLst< TFlt > TFltL
Definition: ds.h:2817
TVal & operator[](const int &ValN) const
Definition: ds.h:2134
const TVal & LastLast() const
Returns a reference to the one before last element of the vector.
Definition: ds.h:557
TVec< TFltFltStrTr > TFltFltStrTrV
Definition: ds.h:1581
bool operator==(const TPair &Pair) const
Definition: ds.h:49
TPair< TInt, TIntPr > TIntIntPrPr
Definition: ds.h:85
TInt MxLen
Definition: ds.h:2546
void SaveXml(TSOut &SOut, const TStr &Nm) const
Definition: xmlser.h:91
void Load(bool &Bool)
Definition: fl.h:84
TQuad< TFlt, TInt, TInt, TInt > TFltIntIntIntQu
Definition: ds.h:264
static PVecPool Load(TSIn &SIn)
Definition: ds.h:1893
int GetVLen(const int &VId) const
Returns the number of elements in the vector with id VId.
Definition: ds.h:1663
TLstNd()
Definition: ds.h:2618
bool IsInBin(const TVal &Val) const
Checks whether element Val is a member of the vector.
Definition: ds.h:805
TInt XDim
Definition: ds.h:2346
bool IsExt() const
Returns true if the vector was created using the GenExt().
Definition: ds.h:513
void CopyTree(const int &SrcNodeId, TTree &DstTree, const int &DstParentNodeId=-1)
Definition: ds.h:2476
TVec< TUInt64FltKd > TUInt64FltKdV
Definition: ds.h:1571
const TVal & GetDat(const TVal &Val) const
Returns reference to the first occurrence of element Val.
Definition: ds.h:807
bool Empty() const
Definition: ds.h:2179
TQuad< TInt, TInt, TFlt, TFlt > TIntIntFltFltQu
Definition: ds.h:266
TPair< TBool, TCh > TBoolChPr
Definition: ds.h:76
TVec< TFltUInt64Kd > TFltUInt64KdV
Definition: ds.h:1575
#define TSizeMx
Definition: bd.h:59
void Clr(bool DoDel=true)
Clears the contents of the pool.
Definition: ds.h:1694
TCmpTripleByVal2(const bool &AscSort=true)
Definition: ds.h:197
TVVec< TFlt > TFltVV
Definition: ds.h:2337
int GetSecHashCd() const
Definition: ds.h:365
TCmpTripleByVal3(const bool &AscSort=true)
Definition: ds.h:209
bool IsNext() const
Definition: ds.h:2626
TVVVec< TFlt > TFltVVV
Definition: ds.h:2390
const TVal2 & GetVal2() const
Definition: ds.h:255
Definition: bd.h:402
static TIter GetPivotValNCmp(const TIter &BI, const TIter &EI, const TCmp &Cmp)
Picks three random elements at positions BI...EI and returns the middle one under the comparator Cmp...
Definition: ds.h:698
void Resize(const ::TSize &_MxVals)
Definition: ds.h:1945
static TVec< TVal, TSizeTy > GetV(const TVal &Val1, const TVal &Val2, const TVal &Val3)
Returns a vector on elements Val1...Val3.
Definition: ds.h:823
void Save(TSOut &SOut) const
Definition: ds.h:355
void Load(TSIn &SIn)
Definition: dt.h:1059
size_t TSize
Definition: bd.h:58
#define Assert(Cond)
Definition: bd.h:251
TTree< TFlt > TFltTree
Definition: ds.h:2501
TSStack< TInt > TIntS
Definition: ds.h:2538
TInt YDim
Definition: ds.h:2346
int GetPrimHashCd() const
Definition: ds.h:2410
TVal & Last()
Returns a reference to the last element of the vector.
Definition: ds.h:553
TVec< TUInt64StrKd > TUInt64StrKdV
Definition: ds.h:1572
TSizeTy SearchVForw(const TVec< TVal, TSizeTy > &ValV, const TSizeTy &BValN=0) const
Returns the starting position of vector ValV.
Definition: ds.h:1501
static void BSortCmp(TIter BI, TIter EI, const TCmp &Cmp)
Bubble sorts the values between positions BI...EI under the comparator Cmp.
Definition: ds.h:719
PVec< TVal > & operator=(const PVec< TVal > &Vec)
Definition: ds.h:2130
void SwapY(const int &Y1, const int &Y2)
Definition: ds.h:2235
bool IsAsc
Definition: ds.h:118
const TVal & GetEmptyVal() const
Returns the reference to an empty value.
Definition: ds.h:1649
const TVal2 & GetVal2() const
Definition: ds.h:163
TTriple< TInt, TStr, TStr > TIntStrStrTr
Definition: ds.h:177
void Save(TSOut &SOut) const
Definition: ds.h:2517
TPair< TUInt64, TUInt64 > TUInt64Pr
Definition: ds.h:94
TVecPool< TInt > TIntVecPool
Definition: ds.h:2102
TVal4 Val4
Definition: ds.h:223
static TVec< TVal, TSizeTy > GetV(const TVal &Val1, const TVal &Val2, const TVal &Val3, const TVal &Val4, const TVal &Val5)
Returns a vector on elements Val1...Val5.
Definition: ds.h:829
void PutAll(const TVal &Val)
Sets the values of all elements in the pool to Val.
Definition: ds.h:1699
int Len() const
Definition: ds.h:2581
Compares the triple by the second value.
Definition: ds.h:193
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:551
TQQueue< TFlt > TFltQ
Definition: ds.h:2601
TPt< TAscFltVP > PAscFltV
Definition: ds.h:2150
static int GetHashCd(const int hc1, const int hc2)
Definition: bd.h:590
static PVecPool Load(TSIn &SIn)
Definition: ds.h:1633
void Clr()
Definition: ds.h:2652
TLst< TStr > TStrL
Definition: ds.h:2823
TSizeTy MxVals
Vector capacity. Capacity is the size of allocated storage. If MxVals==-1, then ValT is not owned by ...
Definition: ds.h:433
void LoadXml(const PXmlTok &XmlTok, const TStr &Nm="")
Definition: xmlser.h:71
Definition: ds.h:4
TVec< TCh > TChV
Definition: ds.h:1526
TTriple< TInt, TInt, TVec< TInt, int > > TIntIntIntVTr
Definition: ds.h:179
bool operator<(const TTuple &Tup) const
Definition: ds.h:290
void LoadXml(const PXmlTok &XmlTok, const TStr &Nm="")
TQuad< TFlt, TFlt, TFlt, TFlt > TFltQu
Definition: ds.h:263
TRec * Addr
Definition: ds.h:6
TTriple< TFlt, TFlt, TFlt > TFltTr
Definition: ds.h:180
const TVal & operator()(const int &X, const int &Y, const int &Z) const
Definition: ds.h:2380
void LoadXml(const PXmlTok &XmlTok, const TStr &Nm="")
Definition: xmlser.h:103
TQQueue(const TQQueue &Queue)
Definition: ds.h:2553
::TSize Reserved() const
Definition: ds.h:1902
TPair< TInt, TStr > TIntStrPr
Definition: ds.h:88
TVec< TStrIntKd > TStrIntKdV
Definition: ds.h:1587
TVVec()
Definition: ds.h:2162
void AddXDim()
Definition: ds.h:2278
void Gen(const int &_XDim, const int &_YDim)
Definition: ds.h:2181
const TVal & operator[](const TSizeTy &ValN) const
Returns a reference to the element at position ValN in the vector.
Definition: ds.h:475
int Len()
Definition: ds.h:2528
void GetMxValXY(int &X, int &Y) const
Definition: ds.h:2260
Definition: fl.h:516
TAPt & operator=(TRec *_Addr)
Definition: ds.h:15
TTriple< TUInt64, TUInt64, TUInt64 > TUInt64Tr
Definition: ds.h:171
#define FailR(Reason)
Definition: bd.h:240
PVec< TFlt > TFltVP
Definition: ds.h:2147
TLstNd< TVal > * PLstNd
Definition: ds.h:2638
TSizeTy IntrsLen(const TVec< TVal, TSizeTy > &ValV) const
Returns the size of the intersection of vectors this and ValV. Assumes the vectors are sorted! ...
Definition: ds.h:1414
TFRec & operator=(const TFRec &)
TVal GetVal(const int &ValN) const
Definition: ds.h:2138
TVVec(const TVec< TVal > &_ValV, const int &_XDim, const int &_YDim)
Definition: ds.h:2167
TVVec< TBool > TBoolVV
Definition: ds.h:2333
const TVal & Top() const
Definition: ds.h:2582
TPair< TInt, TCh > TIntChPr
Definition: ds.h:82
TPair< TIntPr, TInt > TIntPrIntPr
Definition: ds.h:90
const TVal & operator()(const int &X, const int &Y) const
Definition: ds.h:2198
void PutHd(const THd &Hd)
Definition: ds.h:2844
Definition: ds.h:2544
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
Compares the triple by the third value.
Definition: ds.h:205
void GetRow(const int &RowN, TVec< TVal > &Vec) const
Definition: ds.h:2316
Definition: ds.h:2509
Definition: fl.h:128
TVec< TAscFltIntPr > TAscFltIntPrV
Definition: ds.h:1582
TVec< TFltUInt64Pr > TFltUInt64PrV
Definition: ds.h:1549
TPair< TBool, TFlt > TBoolFltPr
Definition: ds.h:77
TVVVec(const TVVVec &Vec)
Definition: ds.h:2350
int GetRecs()
Definition: fl.cpp:989
void DelY(const int &Y)
Definition: ds.h:2304
PLstNd AddFrontSorted(const TVal &Val, const bool &Asc=true)
Definition: ds.h:2716
TVal & GetNodeVal(const int &NodeId)
Definition: ds.h:2429
TKeyDat< TUInt, TInt > TUIntIntKd
Definition: ds.h:385
TSizeTy SearchBin(const TVal &Val) const
Returns the position of an element with value Val.
Definition: ds.h:1454
TTriple()
Definition: ds.h:135
TVal & GetAddDat(const TVal &Val)
Returns reference to the first occurrence of element Val.
Definition: ds.h:811
void SetVal(const TSizeTy &ValN, const TVal &Val)
Sets the value of element at position ValN to Val.
Definition: ds.h:625
int GetRecN()
Definition: fl.cpp:982
TFunc()
Definition: ds.h:2856
void Save(const bool &Bool)
Definition: fl.h:173
static TPt< PVec< TVal > > Load(TSIn &SIn)
Definition: ds.h:2127
TPair< TStr, TStrV > TStrStrVPr
Definition: ds.h:108
TPair< TAscFlt, TAscFlt > TAscFltPr
Definition: ds.h:102
TVec< TIntStrIntIntQu > TIntStrIntIntQuV
Definition: ds.h:1598
TVec< TVal > ValV
Definition: ds.h:2160
TLstNd< TAscFltIntKd > * PAscFltIntKdLN
Definition: ds.h:2822
TPair< TUCh, TUInt64 > TUChUInt64Pr
Definition: ds.h:79
~TVecPool()
Definition: ds.h:1630
TTriple< TInt, TFlt, TFlt > TIntFltFltTr
Definition: ds.h:176
Definition: dt.h:1044
TQuad()
Definition: ds.h:225
TVal * TIter
Random access iterator to TVal.
Definition: ds.h:431
TVVec< TInt > TIntVV
Definition: ds.h:2335
PLstNd Last() const
Definition: ds.h:2660
int GetPrimHashCd() const
Definition: ds.h:155
TFuncPt operator()() const
Definition: ds.h:2868
static TPt< PVec< TVal > > New(const int &MxVals, const int &Vals)
Definition: ds.h:2121
TVec< TFltIntIntIntQu > TFltIntIntIntQuV
Definition: ds.h:1597
TVec< TUChIntPr > TUChIntPrV
Definition: ds.h:1542
void Gen(const int &_XDim, const int &_YDim, const int &_ZDim)
Definition: ds.h:2369
TVec< TStrIntPr > TStrIntPrV
Definition: ds.h:1585
const TVal & operator[](const int &ValN) const
Definition: ds.h:2566
void ShuffleAll(TRnd &Rnd=TInt::Rnd)
Definition: ds.h:2093
TVec< TChA > TChAV
Definition: ds.h:1535
TPair< TAscFlt, TStr > TAscFltStrPr
Definition: ds.h:104
TLstNd< TStr > * PStrLN
Definition: ds.h:2824
const TVal & At(const int &X, const int &Y, const int &Z) const
Definition: ds.h:2375
static TVec< TVal, TSizeTy > GetV(const TVal &Val1, const TVal &Val2, const TVal &Val3, const TVal &Val4)
Returns a vector on elements Val1...Val4.
Definition: ds.h:826
TVVec< TStr > TStrVV
Definition: ds.h:2338
TTriple< TInt, TVec< TInt, int >, TInt > TIntIntVIntTr
Definition: ds.h:178
TVec< TIntStrVPr > TIntStrVPrV
Definition: ds.h:1564
TQQueue< TIntStrPr > TIntStrPrQ
Definition: ds.h:2604
bool PrevPerm()
Generates previous permutation of the elements in the vector.
Definition: ds.h:1325
Definition: dt.h:201
TSizeTy Add(const TVal &Val)
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:580
void SaveXml(TSOut &SOut, const TStr &Nm) const
Definition: xmlser.h:75
Definition: ds.h:2344
TVal & GetVal(const TSizeTy &ValN)
Returns a reference to the element at position ValN in the vector.
Definition: ds.h:623
int GetSecHashCd() const
Definition: ds.h:57
void ISort(const TSizeTy &MnLValN, const TSizeTy &MxRValN, const bool &Asc)
Insertion sorts the values between positions MnLValN...MxLValN.
Definition: ds.h:1184
TKeyDat(const TKey &_Key, const TDat &_Dat)
Definition: ds.h:353
void Ins(const TSizeTy &ValN, const TVal &Val)
Inserts new element Val before the element at position ValN.
Definition: ds.h:1122
int GetPrimHashCd() const
Definition: ds.h:364
TVec< TStr > TStrV
Definition: ds.h:1534
void Save(TSOut &SOut) const
Definition: ds.h:2402
void Push()
Definition: ds.h:2531
static TVec< TVal, TSizeTy > GetV(const TVal &Val1, const TVal &Val2, const TVal &Val3, const TVal &Val4, const TVal &Val5, const TVal &Val6)
Returns a vector on elements Val1...Val6.
Definition: ds.h:832
TKeyDat< TFlt, TIntPr > TFltIntPrKd
Definition: ds.h:393
static TIter PartitionCmp(TIter BI, TIter EI, const TVal Pivot, const TCmp &Cmp)
Partitions the values between positions BI...EI under the comparator Cmp.
Definition: ds.h:712
static int GetRnd(const int &Range=0)
Definition: dt.h:1085
void Reserve(const TSize &MxVals)
Reserves enough capacity for the pool to store MxVals elements.
Definition: ds.h:1647
void DelFirst()
Definition: ds.h:2673
TLstNd< TIntKd > * PIntKdLN
Definition: ds.h:2816
TQuad(const TQuad &Quad)
Definition: ds.h:227
TKeyDat & operator=(const TKeyDat &KeyDat)
Definition: ds.h:359
Definition: ds.h:32
TKeyDat< TUInt64, TInt > TUInt64IntKd
Definition: ds.h:387
void GetRec(void *Rec, const int &RecN=-1)
Definition: fl.h:559
TVec< TFltBoolKd > TFltBoolKdV
Definition: ds.h:1573
TSizeTy LastValN() const
Returns the position of the last element.
Definition: ds.h:555
TVec< TUInt64StrPr > TUInt64StrPrV
Definition: ds.h:1569
PLstNd AddBack(const TVal &Val)
Definition: ds.h:2708
bool Empty() const
Definition: ds.h:2136
void ShuffleY(TRnd &Rnd)
Definition: ds.h:2255
TVec< TIntStrPrPr > TIntStrPrPrV
Definition: ds.h:1563
void PutV(const int &VId, const TValV &ValV)
Definition: ds.h:1920
static PVecPool New(const ::TSize &ExpectVals=0, const ::TSize &GrowBy=1000000, const bool &FastCopy=false)
Definition: ds.h:1891
TKeyDat< TIntPr, TFlt > TIntPrFltKd
Definition: ds.h:381
void AddYDim()
Definition: ds.h:2285
TKeyDat< TStr, TBool > TStrBoolKd
Definition: ds.h:400
TStr GetStr() const
Definition: ds.h:305
TVec(const TSizeTy &_Vals)
Constructs a vector (an array) of length _Vals.
Definition: ds.h:444
bool Empty() const
Definition: ds.h:26
TKey Key
Definition: ds.h:347
TPt< TStrVP > PStrV
Definition: ds.h:2152
void GetSwitchedPrV(const TVec< TPair< TVal1, TVal2 >, TSizeTy > &SrcPrV, TVec< TPair< TVal2, TVal1 >, TSizeTy > &DstPrV)
Definition: ds.h:67
TTree< TInt > TIntTree
Definition: ds.h:2500
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)
Returns a vector on elements Val1...Val7.
Definition: ds.h:835
void Load(TSIn &SIn)
Definition: ds.h:43
TVec< TUInt64 > TUInt64V
Definition: ds.h:1530
TVec< TFlt > TFltV
Definition: ds.h:1531
bool Empty() const
Definition: ds.h:2367
int AddRoot(const TVal &NodeVal=TVal())
Definition: ds.h:2421
PFRnd FRnd
Definition: ds.h:2831
TVec< TTriple< TInt, TIntV, TVal > > NodeV
Definition: ds.h:2397
TVal GetXY(const int &X, const int &Y) const
Definition: ds.h:2207
int GetPrimHashCd() const
Definition: ds.h:56
TVal & operator[](const TSizeTy &ValN)
Returns a reference to the element at position ValN in the vector.
Definition: ds.h:479
TSizeTy UnionLen(const TVec< TVal, TSizeTy > &ValV) const
Returns the size of the union of vectors this and ValV. Assumes the vectors are sorted! ...
Definition: ds.h:1428
void Del(const TVal &Val)
Definition: ds.h:2776
TVVec< TSFlt > TSFltVV
Definition: ds.h:2336
TLst()
Definition: ds.h:2644
TVec< TIntQu > TIntQuV
Definition: ds.h:1538
void Union(const TVec< TVal, TSizeTy > &ValV)
Sets this vector to its union with ValV. Assumes the vectors are sorted!
Definition: ds.h:1353
void Pop()
Definition: ds.h:2533
TLstNd(TLstNd *_PrevNd, TLstNd *_NextNd, const TVal &_Val)
Definition: ds.h:2620
TKeyDat< TUInt, TUInt > TUIntKd
Definition: ds.h:386
TVec< TUChUInt64Pr > TUChUInt64PrV
Definition: ds.h:1543
const TVal1 & GetVal1() const
Definition: ds.h:254
Definition: dt.h:412
Definition: ds.h:218
void PutBack(const PLstNd &Nd)
Definition: ds.h:2751
TSizeTy Partition(const TSizeTy &MnLValN, const TSizeTy &MxRValN, const bool &Asc)
Partitions the values between positions MnLValN...MxLValN.
Definition: ds.h:1222
TSStack< TBoolChPr > TBoolChS
Definition: ds.h:2539
TTriple< TStr, TStr, TStr > TStrTr
Definition: ds.h:185
TKeyDat(const TKeyDat &KeyDat)
Definition: ds.h:351
TIter BegI() const
Returns an iterator pointing to the first element in the vector.
Definition: ds.h:565
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TQuad & operator=(const TQuad &Quad)
Definition: ds.h:238
bool IsSorted(const bool &Asc=true) const
Checks whether the vector is sorted in ascending (if Asc=true) or descending (if Asc=false) order...
Definition: ds.h:1259
void Pack()
Reduces vector capacity (frees memory) to match its size.
Definition: ds.h:1005
TCRef CRef
Definition: ds.h:1612
bool IsPrev() const
Definition: ds.h:2625
bool operator<(const TAPt &Pt) const
Definition: ds.h:18
TQQueue(const int &_MxLast=64, const int &_MxLen=-1)
Definition: ds.h:2550
TVec< TFltStrPrPr > TFltStrPrPrV
Definition: ds.h:1579
TAPt & operator=(const TAPt &Pt)
Definition: ds.h:14
int Add(const TVal &Val)
Definition: ds.h:2140
TVec< TVal, TSizeTy > & operator+(const TVal &Val)
Appends value Val to the vector.
Definition: ds.h:467
int GetVecs() const
Returns the total number of vectors stored in the vector pool.
Definition: ds.h:1639
TPair< TInt, TStrPr > TIntStrPrPr
Definition: ds.h:111
int Len() const
Definition: ds.h:282
static void QSortCmp(TIter BI, TIter EI, const TCmp &Cmp)
Quick sorts the values between positions BI...EI under the comparator Cmp.
Definition: ds.h:731
void DelX(const int &X)
Definition: ds.h:2292
TCmpPairByVal2(const bool &AscSort=true)
Definition: ds.h:120
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1271
TSizeTy SearchForw(const TVal &Val, const TSizeTy &BValN=0) const
Returns the position of an element with value Val.
Definition: ds.h:1487
int GetXDim() const
Definition: ds.h:2382
static PVecPool Load(const TStr &FNm)
Definition: ds.h:1634
TPair< TStr, TInt > TStrIntPr
Definition: ds.h:105
TQQueue< TStr > TStrQ
Definition: ds.h:2602
void ShuffleAll(TRnd &Rnd=TInt::Rnd)
Shuffles the order of all elements in the pool.
Definition: ds.h:1853
TBool FastCopy
Definition: ds.h:1613
TKeyDat< TFlt, TUInt > TFltUIntKd
Definition: ds.h:394
TTuple(const TVal &InitVal)
Definition: ds.h:276
TVecPool(const ::TSize &ExpectVals=0, const ::TSize &_GrowBy=1000000, const bool &_FastCopy=false, const TVal &_EmptyVal=TVal())
Definition: ds.h:1972
TKeyDat< TInt, TInt > TIntKd
Definition: ds.h:378
bool IsIn(const TVal &Val, TSizeTy &ValN) const
Checks whether element Val is a member of the vector.
Definition: ds.h:801
TTriple< TCh, TInt, TInt > TChIntIntTr
Definition: ds.h:168
void GetV(const int &VId, TValV &ValV) const
Definition: ds.h:1917
TVal * ValBf
Definition: ds.h:1882
TVec< uint64, int > IdToOffV
Definition: ds.h:1617
TSStack & operator=(const TSStack &Stack)
Definition: ds.h:2519
TVecPool(const TSize &ExpectVals=0, const TSize &_GrowBy=1000000, const bool &_FastCopy=false, const TVal &_EmptyVal=TVal())
Vector pool constructor.
Definition: ds.h:1733
static TVec< TVal, TSizeTy > GetV(const TVal &Val1, const TVal &Val2)
Returns a vector on elements Val1, Val2.
Definition: ds.h:820
TVal1 Val1
Definition: ds.h:34
TVec< TInt > TIntV
Definition: ds.h:1529
TVal2 Val2
Definition: ds.h:35
TKeyDat< TInt, TSFlt > TIntSFltKd
Definition: ds.h:383
TAPt(const TAPt &Pt)
Definition: ds.h:9
TVal ValV[NVals]
Definition: ds.h:273
int GetYDim() const
Definition: ds.h:2185
Definition: bd.h:196
TKeyDat(TSIn &SIn)
Definition: ds.h:354
void Push(const TVal &Val)
Definition: ds.h:2587
void Sort(const bool &Asc=true)
Definition: ds.h:311
TCmpKeyDatByDat(const bool &AscSort=true)
Definition: ds.h:413
void Reverse()
Reverses the order of the elements in the vector.
Definition: ds.h:1286
bool operator<(const TPair &Pair) const
Definition: ds.h:51
#define AssertR(Cond, Reason)
Definition: bd.h:258
void SwapX(const int &X1, const int &X2)
Definition: ds.h:2229
TQuad< TInt, TStr, TInt, TInt > TIntStrIntIntQu
Definition: ds.h:265
void GenRandomTree(const int &Nodes, TRnd &Rnd)
Definition: ds.h:2453
int GetZDim() const
Definition: ds.h:2384
void Swap(TVVec< TVal > &Vec)
Definition: ds.h:2241
TSizeTy AddMerged(const TVal &Val)
Adds element Val to a sorted vector only if the element Val is not already in the vector...
Definition: ds.h:1089
int GetChildren(const int &NodeId) const
Definition: ds.h:2427
TInt MxLast
Definition: ds.h:2546
void ShuffleX(TRnd &Rnd)
Definition: ds.h:2250
TVec()
Definition: ds.h:441
TTriple< TInt, TInt, TInt > TIntTr
Definition: ds.h:170
TSizeTy AddBackSorted(const TVal &Val, const bool &Asc)
Adds element Val to a sorted vector.
Definition: ds.h:1078
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:495
TVal & GetVal()
Definition: ds.h:2629
TRec & operator[](const int &RecN) const
Definition: ds.h:22
TPair< TFlt, TStr > TFltStrPr
Definition: ds.h:100
void SetRecN(const int &RecN)
Definition: fl.cpp:977
Definition: ds.h:2852
int GetUniDevInt(const int &Range=0)
Definition: dt.cpp:39
TSizeTy Count(const TVal &Val) const
Counts the number of occurrences of Val in the vector.
Definition: ds.h:1446
bool operator==(const TVec< TVal, TSizeTy > &Vec) const
Checks that the two vectors have the same contents.
Definition: ds.h:921
TQQueue< TFltV > TFltVQ
Definition: ds.h:2605
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:515
void MoveFrom(TVec< TVal, TSizeTy > &Vec)
Takes over the data and the capacity from Vec.
Definition: ds.h:1020
bool operator==(const TTriple &Triple) const
Definition: ds.h:149
bool Cmp(const int &RelOp, const TRec &Rec1, const TRec &Rec2)
Definition: bd.h:426
void SaveXml(TSOut &SOut, const TStr &Nm) const
TLst< TIntKd > TIntKdL
Definition: ds.h:2815
TVec(const TSizeTy &_MxVals, const TSizeTy &_Vals)
Constructs a vector (an array) of length _Vals, while reserving enough memory to store _MxVals elemen...
Definition: ds.h:448
TVec< TIntFltIntTr > TIntFltIntTrV
Definition: ds.h:1555
static TPt< PVec< TVal > > New()
Definition: ds.h:2118
Definition: ds.h:2829
TTuple()
Definition: ds.h:275
void Clr(const bool &DoDel=true)
Definition: ds.h:2569
TLst< TFltIntKd > TFltIntKdL
Definition: ds.h:2819
TFunc & operator=(const TFunc &Func)
Definition: ds.h:2862
TVVec< TIntPr > TIntPrVV
Definition: ds.h:2339
void Resize(const TSizeTy &_MxVals=-1)
Resizes the vector so that it can store at least _MxVals.
Definition: ds.h:846
int GetMemUsed() const
Definition: ds.h:2413
static TVec< TVal, TSizeTy > GetV(const TVal &Val1)
Returns a vector on element Val1.
Definition: ds.h:817
TRec & operator*() const
Definition: ds.h:21
TVec< TUInt64FltPr > TUInt64FltPrV
Definition: ds.h:1568
TVec< TBool > TBoolV
Definition: ds.h:1525
TVVVec< TVal > & operator=(const TVVVec< TVal > &Vec)
Definition: ds.h:2359
static TPt< PVec< TVal > > New(const TVec< TVal > &V)
Definition: ds.h:2124
TKeyDat< TFlt, TInt > TFltIntKd
Definition: ds.h:391
void SortCmp(const TCmp &Cmp)
Sorts the elements of the vector using the comparator Cmp.
Definition: ds.h:739
TPair< TFlt, TUInt64 > TFltUInt64Pr
Definition: ds.h:98
char * CStr()
Definition: dt.h:476
void LoadXml(const PXmlTok &XmlTok, const TStr &Nm="")
Definition: xmlser.h:79
bool operator<(const TQuad &Quad) const
Definition: ds.h:244
void Gen(const TSizeTy &_MxVals, const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements, while reserving enough memory for _MxVals elements...
Definition: ds.h:499
Definition: ds.h:2395
void GetVal(TVal1 &_Val1, TVal2 &_Val2, TVal3 &_Val3) const
Definition: ds.h:159
void BSort(const TSizeTy &MnLValN, const TSizeTy &MxRValN, const bool &Asc)
Bubble sorts the values between positions MnLValN...MxLValN.
Definition: ds.h:1171
TKeyDat< TInt, TUInt64 > TIntUInt64Kd
Definition: ds.h:379
TSizeTy Vals
Vector length. Length is the number of elements stored in the vector.
Definition: ds.h:434
TVec(TSIn &SIn)
Definition: ds.h:458
TVec< TVal > & Get1DVec()
Definition: ds.h:2188
TVec< TFltIntPr > TFltIntPrV
Definition: ds.h:1548
bool IsVId(const int &VId) const
Tests whether vector of id VId is in the pool.
Definition: ds.h:1643
void Clr()
Definition: ds.h:2415
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
TTuple(const TTuple &Tup)
Definition: ds.h:277
TSizeTy GetMemSize() const
Returns the memory size (the number of bytes) of a binary representation.
Definition: ds.h:486
TPt< TIntVecPool > PIntVecPool
Definition: ds.h:2105
void CompactPool(const TVal &DelVal)
Definition: ds.h:2064
TLstNd< TInt > * PIntLN
Definition: ds.h:2814
TVVVec< TInt > TIntVVV
Definition: ds.h:2389
Definition: dt.h:881
#define MAX(a, b)
Definition: bd.h:350
TVec< TFltKd > TFltKdV
Definition: ds.h:1577
void DelLast()
Removes the last element of the vector.
Definition: ds.h:635
bool IsAsc
Definition: ds.h:411
TTree(const TTree &Tree)
Definition: ds.h:2400
PLstNd SearchForw(const TVal &Val)
Definition: ds.h:2792
TTriple< TInt, TInt, TStr > TIntIntStrTr
Definition: ds.h:173
void Trunc(const TSizeTy &_Vals=-1)
Truncates the vector's length and capacity to _Vals elements.
Definition: ds.h:982
static void SwapI(TIter LVal, TIter RVal)
Swaps the elements that iterators LVal and RVal point to.
Definition: ds.h:646
bool IsAsc
Definition: ds.h:195
TVec< TIntFltKd > TIntFltKdV
Definition: ds.h:1560
TPair & operator=(const TPair &Pair)
Definition: ds.h:47
TKeyDat< TStr, TAscFlt > TStrAscFltKd
Definition: ds.h:403
TVec< TQQueue< TInt > > TIntQV
Definition: ds.h:2607
int GetCols() const
Definition: ds.h:2187
TBool FastCopy
Definition: ds.h:1879
bool operator<(const TTree &Tree) const
Definition: ds.h:2408
TFAccess
Definition: fl.h:347
TVec< TIntStrStrTr > TIntStrStrTrV
Definition: ds.h:1557
TIter GetI(const TSizeTy &ValN) const
Returns an iterator an element at position ValN.
Definition: ds.h:569
int GetRows() const
Definition: ds.h:2186
TVal * GetValVPt(const int &VId) const
Definition: ds.h:1914
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)
Returns a vector on elements Val1...Val9.
Definition: ds.h:841
TPair< TUCh, TInt > TUChIntPr
Definition: ds.h:78
TLstNd * PrevNd
Definition: ds.h:2614
TLst< TAscFltIntKd > TAscFltIntKdL
Definition: ds.h:2821
TPair< TUInt64, TInt > TUInt64IntPr
Definition: ds.h:93
const TVal & At(const int &X, const int &Y) const
Definition: ds.h:2190
TSize Vals
Definition: ds.h:1614
TSize MxVals
Definition: ds.h:1614
void GetRec(TRec &Rec, const int &RecN=-1)
Definition: ds.h:2845
TQuad(TSIn &SIn)
Definition: ds.h:231
void PutY(const int &Y, const TVal &Val)
Definition: ds.h:2205
int GetPrimHashCd() const
Definition: ds.h:297
TVec< TIntStrIntTr > TIntStrIntTrV
Definition: ds.h:1556
void PutAll(const TVal &Val)
Definition: ds.h:2202
TLstNd< TFltIntKd > * PFltIntKdLN
Definition: ds.h:2820
void GetSubValV(const int &_BValN, const int &_EValN, TVec< TVal > &SubValV) const
Definition: ds.h:2572
TStr GetXOutOfBoundsErrMsg(const TSizeTy &ValN) const
Constructs the out of bounds error message.
Definition: ds.h:879
TSizeTy SearchBack(const TVal &Val) const
Returns the position of an element with value Val.
Definition: ds.h:1494
TKeyDat< TFlt, TUInt64 > TFltUInt64Kd
Definition: ds.h:392
::TSize GetMemUsed() const
Definition: ds.h:1906
TVal * ValT
Definition: ds.h:435
TLst< TInt > TIntL
Definition: ds.h:2813
void Reserve(const TSizeTy &_MxVals, const TSizeTy &_Vals)
Reserves enough memory for the vector to store _MxVals elements and sets its length to _Vals...
Definition: ds.h:517
TTriple< TFlt, TInt, TInt > TFltIntIntTr
Definition: ds.h:181
TQuad(const TVal1 &_Val1, const TVal2 &_Val2, const TVal3 &_Val3, const TVal4 &_Val4)
Definition: ds.h:229
bool operator!=(const TAPt &Pt) const
Definition: ds.h:17
bool IsIn(const TVal &Val) const
Definition: ds.h:2527
TVec< TIntStrPr > TIntStrPrV
Definition: ds.h:1552
TVal3 Val3
Definition: ds.h:133
PLstNd First() const
Definition: ds.h:2659
Compares the pair by the second value.
Definition: ds.h:116
TVec< TIntUInt64Kd > TIntUInt64KdV
Definition: ds.h:1545
TVVVec()
Definition: ds.h:2349
void Save(TSOut &SOut) const
Definition: ds.h:279
TSizeTy GetMxValN() const
Returns the position of the largest element in the vector.
Definition: ds.h:1514
TVec< TUCh > TUChV
Definition: ds.h:1527
void Swap(TRec &Rec1, TRec &Rec2)
Definition: bd.h:568
TKeyDat< TStr, TStr > TStrKd
Definition: ds.h:404
TTriple & operator=(const TTriple &Triple)
Definition: ds.h:146
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:429
const TVal4 & GetVal4() const
Definition: ds.h:257
TTriple< TUCh, TInt, TInt > TUChIntIntTr
Definition: ds.h:169
int AddEmptyV(const int &ValVLen)
Definition: ds.h:2054
TVec< TFltStrKd > TFltStrKdV
Definition: ds.h:1578
const TVal3 & GetVal3() const
Definition: ds.h:164
int GetMemUsed() const
Definition: ds.h:157
TPair< TUInt64, TStr > TUInt64StrPr
Definition: ds.h:96
void WrTree(const int &NodeId=0, const int &Lev=0)
Definition: ds.h:2487
TTriple< TStr, TFlt, TFlt > TStrFltFltTr
Definition: ds.h:187
TTriple< TChA, TChA, TChA > TChATr
Definition: ds.h:184
TVec< TVal > ValV
Definition: ds.h:2511
TSizeTy AddV(const TVec< TVal, TSizeTy > &ValV)
Adds the elements of the vector ValV to the to end of the vector.
Definition: ds.h:1056
void GetSubValV(const TSizeTy &BValN, const TSizeTy &EValN, TVec< TVal, TSizeTy > &ValV) const
Fills ValV with elements at positions BValN...EValN.
Definition: ds.h:1112
bool operator<(const TTriple &Triple) const
Definition: ds.h:151
TLstNd * Next() const
Definition: ds.h:2628
static void ISortCmp(TIter BI, TIter EI, const TCmp &Cmp)
Insertion sorts the values between positions BI...EI under the comparator Cmp.
Definition: ds.h:725
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)
Returns a vector on elements Val1...Val8.
Definition: ds.h:838