StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TIndexTable.h
1 // @(#)root/table:$Id$
2 // Author: Valery Fine(fine@bnl.gov) 01/03/2001
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. *
6  * Copyright (C) 2001 [BNL] Brookhaven National Laboratory. *
7  * All rights reserved. *
8  * *
9  * For the licensing terms see $ROOTSYS/LICENSE. *
10  * For the list of contributors see $ROOTSYS/README/CREDITS. *
11  *************************************************************************/
12 
13 #ifndef ROOT_TIndexTable
14 #define ROOT_TIndexTable
15 
16 #include "TTable.h"
17 
19 //
20 // Class TIndexTable
21 // Iterator of the table with extra index array
22 //
24 
25 
26 class TIndexTable : public TTable {
27 protected:
28  const TTable *fRefTable;
29 public:
30  class iterator {
31  protected:
32  const TTable *fTable;
33  const int *fCurrentRow;
34  iterator(): fTable(0), fCurrentRow(0) {}
35  public:
36  iterator(const TTable &t, const int &rowPtr): fTable(&t), fCurrentRow(&rowPtr){}
37  iterator(const TTable &t): fTable(&t),fCurrentRow(0){}
38  iterator(const iterator& iter) : fTable(iter.fTable), fCurrentRow(iter.fCurrentRow){}
39  iterator &operator=(const iterator& iter) {fTable = iter.fTable; fCurrentRow = iter.fCurrentRow; return *this;}
40  iterator &operator++() { if (fCurrentRow) ++fCurrentRow; return *this;}
41  void operator++(int) { if (fCurrentRow) fCurrentRow++;}
42  iterator &operator--() { if (fCurrentRow) --fCurrentRow; return *this;}
43  void operator--(int) { if (fCurrentRow) fCurrentRow--;}
44  iterator &operator+(Int_t idx) { if (fCurrentRow) fCurrentRow+=idx; return *this;}
45  iterator &operator-(Int_t idx) { if (fCurrentRow) fCurrentRow-=idx; return *this;}
46  Int_t operator-(const iterator &it) const { return fCurrentRow-it.fCurrentRow; }
47  void *operator *(){ return (void *)(fTable?((char *)fTable->GetArray())+(*fCurrentRow)*(fTable->GetRowSize()):0);}
48  operator int() { return *fCurrentRow;}
49  Bool_t operator==(const iterator &t) const { return (fCurrentRow == t.fCurrentRow); }
50  Bool_t operator!=(const iterator &t) const { return !operator==(t); }
51  };
52  TIndexTable(const TTable *table);
53  TIndexTable(const TIndexTable &indx): TTable(indx),fRefTable(indx.fRefTable) {}
54  int *GetTable(Int_t i=0);
55  Bool_t IsValid() const;
56  void push_back(Long_t next);
57 
58  const TTable *Table() const;
59  iterator begin() { return ((const TIndexTable *)this)->begin();}
60  iterator begin() const { return GetNRows() ? iterator(*Table(),*GetTable(0)):end();}
61  iterator end() { return ((const TIndexTable *)this)->end(); }
62  iterator end() const {Long_t i = GetNRows(); return i? iterator(*Table(), *GetTable(i)):iterator(*this);}
63 
64 protected:
66 
67 // define ClassDefTable(TIndexTable,int)
68 protected:
69  static TTableDescriptor *fgColDescriptors;
70  virtual TTableDescriptor *GetDescriptorPointer() const;
71  virtual void SetDescriptorPointer(TTableDescriptor *list);
72 public:
73  TIndexTable() : TTable("TIndexTable",sizeof(int)), fRefTable(0) {SetType("int");}
74  TIndexTable(const char *name) : TTable(name,sizeof(int)), fRefTable(0) {SetType("int");}
75  TIndexTable(Int_t n) : TTable("TIndexTable",n,sizeof(int)), fRefTable(0) {SetType("int");}
76  TIndexTable(const char *name,Int_t n) : TTable(name,n,sizeof(int)), fRefTable(0) {SetType("int");}
77  virtual ~TIndexTable() {}
78  const int *GetTable(Int_t i=0) const;
79  int &operator[](Int_t i){ assert(i>=0 && i < GetNRows()); return *GetTable(i); }
80  const int &operator[](Int_t i) const { assert(i>=0 && i < GetNRows()); return *((const int *)(GetTable(i))); }
81  ClassDef(TIndexTable,4) // "Index" array for TTable object
82 };
83 
84 //___________________________________________________________________________________________________________
85 inline int *TIndexTable::GetTable(Int_t i) { return ((int *)GetArray())+i;}
86 //___________________________________________________________________________________________________________
87 inline const int *TIndexTable::GetTable(Int_t i) const { return ((int *)GetArray())+i;}
88 //___________________________________________________________________________________________________________
89 inline Bool_t TIndexTable::IsValid() const
90 {
91  // Check whether all "map" values do belong the table
92  const TTable *cont= Table();
93  if (!cont) return kFALSE;
94 
95  iterator i = begin();
96  iterator finish = end();
97  Int_t totalSize = cont->GetNRows();
98 
99  for (; i != finish; i++) {
100  int th = i;
101  if ( th == -1 || (0 <= th && th < totalSize) ) continue;
102  return kFALSE;
103  }
104  return kTRUE;
105 }
106 //___________________________________________________________________________________________________________
107 inline void TIndexTable::push_back(Long_t next){ AddAt(&next); }
108 
109 #endif
virtual Long_t GetRowSize() const
Returns the size (in bytes) of one table row.
Definition: TTable.cxx:1395
const TTable * Table() const
to be documented
virtual void SetDescriptorPointer(TTableDescriptor *list)
set table descriptor
virtual void SetType(const char *const type)
to be documented
Definition: TTable.cxx:1973
virtual Long_t GetNRows() const
Returns the number of the used rows for the wrapped table.
Definition: TTable.cxx:1388
virtual Int_t AddAt(const void *c)
Definition: TTable.cxx:1122
Definition: TTable.h:48
static TTableDescriptor * CreateDescriptor()
to be documented
Definition: TIndexTable.cxx:92
virtual TTableDescriptor * GetDescriptorPointer() const
return column descriptor