1 // @(#)root/table:$Id$ 2 // Author: Valery Fine(fine@mail.cern.ch) 03/07/98 3 4 /************************************************************************* 5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. * 6 * All rights reserved. * 7 * * 8 * For the licensing terms see $ROOTSYS/LICENSE. * 9 * For the list of contributors see $ROOTSYS/README/CREDITS. * 10 *************************************************************************/ 11 12 #ifndef ROOT_TTable 13 #define ROOT_TTable 14 15 ////////////////////////////////////////////////////////////////////////// 16 // // 17 // TTable // 18 // // 19 // It is a base class to create a "wrapper" class // 20 // holding the plain C-structure array // 21 // (1 element of the structure per element) // 22 // // 23 ////////////////////////////////////////////////////////////////////////// 24 25 #ifdef __CINT__ 26 #pragma Ccomment on 27 #endif 28 29 #include "Ttypes.h" 30 #include "TDataSet.h" 31 #include "tableDescriptor.h" 32 #ifndef ROOT_TCut 33 # include "TCut.h" 34 #endif 35 36 # ifndef ROOT_Riosfwd 37 # include "Riosfwd.h" 38 # endif 39 40 #ifndef __CINT__ 41 # include <string.h> 42 # include <assert.h> 43 #endif 44 45 #include <vector> 46 47 class TTableDescriptor; 48 class TH1; 49 class TTableMap; 50 typedef TTableMap* Ptr_t; 51 52 class TTable : public TDataSet { 53 friend class TDataSet; 54 friend class St_XDFFile; 55 protected: 56 Long_t fSize; // Size of the one element (row) of the table 57 58 protected: 59 60 Int_t fN; //Number of array elements 61 Char_t *fTable; // Array of (fN*fSize) longs 62 Long_t fMaxIndex; // The used capacity of this array 63 64 Bool_t BoundsOk(const char *where, Int_t at) const; 65 Bool_t OutOfBoundsError(const char *where, Int_t i) const; 66 67 void CopyStruct(Char_t *dest, const Char_t *src); 68 Char_t *Create(); 69 virtual void Clear(Option_t *opt=""); 70 virtual void Delete(Option_t *opt=""); 71 virtual Bool_t EntryLoop(const Char_t *exprFileName,Int_t &action, TObject *obj, Int_t nentries=1000000000, Int_t firstentry=0, Option_t *option=""); 72 Int_t SetfN(Long_t len); 73 void SetTablePointer(void *table); 74 void SetUsedRows(Int_t n); 75 virtual void SetType(const char *const type); 76 void StreamerHeader(TBuffer &b,Version_t version=3); 77 void StreamerTable(TBuffer &b,Version_t version=3); 78 virtual TTableDescriptor *GetDescriptorPointer() const; 79 virtual void SetDescriptorPointer(TTableDescriptor *list); 80 81 void ReAlloc(Int_t newsize); 82 static const char *TableDictionary(const char *className,const char *structName,TTableDescriptor *&ColDescriptors); 83 84 public: 85 86 enum EColumnType {kNAN, kFloat, kInt, kLong, kShort, kDouble, kUInt 87 ,kULong, kUShort, kUChar, kChar, kPtr, kBool 88 ,kEndColumnType }; 89 enum ETableBits { 90 kIsNotOwn = BIT(23) // if the TTable wrapper doesn't own the STAF table 91 // As result of the Update() method for example 92 }; 93 static const char *fgTypeName[kEndColumnType]; 94 TTable(const char *name=0, Int_t size=0); 95 TTable(const char *name, Int_t n,Int_t size); 96 TTable(const char *name, Int_t n, Char_t *array,Int_t size); 97 TTable(const char *name, const char *type, Int_t n, Char_t *array, Int_t size); 98 TTable(const TTable &table); 99 TTable &operator=(const TTable &rhs); 100 virtual ~TTable(); 101 102 virtual void Adopt(Int_t n, void *array); 103 virtual Int_t AddAt(const void *c); 104 virtual void AddAt(const void *c, Int_t i); 105 virtual void AddAt(TDataSet *dataset,Int_t idx=0); 106 virtual Long_t AppendRows(const void *row, UInt_t nRows); 107 virtual void AsString(void *buf, EColumnType type, Int_t width, ostream &out) const; 108 const void *At(Int_t i) const; 109 virtual void Browse(TBrowser *b); 110 virtual void CopySet(TTable &array); 111 Int_t CopyRows(const TTable *srcTable,Long_t srcRow=0, Long_t dstRow=0, Long_t nRows=0, Bool_t expand=kFALSE); 112 virtual void DeleteRows(Long_t indx,UInt_t nRows=1); 113 virtual void Draw(Option_t *opt); 114 virtual TH1 *Draw(TCut varexp, TCut selection, Option_t *option="" 115 ,Int_t nentries=1000000000, Int_t firstentry=0); 116 virtual TH1 *Draw(const char *varexp, const char *selection, Option_t *option="" 117 ,Int_t nentries=1000000000, Int_t firstentry=0); // *MENU* 118 void *GetArray() const ; 119 virtual TClass *GetRowClass() const ; 120 Int_t GetSize() const { return fN; } 121 virtual Long_t GetNRows() const; 122 virtual Long_t GetRowSize() const; 123 virtual Long_t GetTableSize() const; 124 virtual TTableDescriptor *GetTableDescriptors() const; 125 virtual TTableDescriptor *GetRowDescriptors() const; 126 virtual const Char_t *GetType() const; 127 virtual void Fit(const char *formula ,const char *varexp, const char *selection="",Option_t *option="" ,Option_t *goption="" 128 ,Int_t nentries=1000000000, Int_t firstentry=0); // *MENU* 129 130 virtual Long_t HasData() const { return 1; } 131 virtual Long_t InsertRows(const void *rows, Long_t indx, UInt_t nRows=1); 132 virtual Bool_t IsFolder() const; 133 Int_t NaN(); 134 static TTable *New(const Char_t *name, const Char_t *type, void *array, UInt_t size); 135 virtual Char_t *MakeExpression(const Char_t *expressions[],Int_t nExpressions); 136 virtual Char_t *Print(Char_t *buf,Int_t n) const ; 137 virtual void Print(Option_t *opt="") const; 138 virtual const Char_t *Print(Int_t row, Int_t rownumber=10, 139 const Char_t *colfirst="", const Char_t *collast="") const; // *MENU* 140 virtual void PrintContents(Option_t *opt="") const; 141 virtual const Char_t *PrintHeader() const; // *MENU* 142 virtual void Project(const char *hname, const char *varexp, const char *selection="", Option_t *option="" 143 ,Int_t nentries=1000000000, Int_t firstentry=0); 144 145 virtual Int_t Purge(Option_t *opt=""); 146 147 void *ReAllocate(Int_t newsize); 148 void *ReAllocate(); 149 virtual void SavePrimitive(ostream &out, Option_t *option = ""); 150 virtual void Set(Int_t n); 151 virtual void Set(Int_t n, Char_t *array); 152 virtual void SetNRows(Int_t n); 153 virtual void Reset(Int_t c=0); 154 virtual void ResetMap(Bool_t wipe=kTRUE); 155 virtual void Update(); 156 virtual void Update(TDataSet *set,UInt_t opt=0); 157 void *operator[](Int_t i); 158 const void *operator[](Int_t i) const; 159 160 161 // ---- Table descriptor service ------ 162 163 virtual Int_t GetColumnIndex(const Char_t *columnName) const; 164 virtual const Char_t *GetColumnName(Int_t columnIndex) const; 165 virtual const UInt_t *GetIndexArray(Int_t columnIndex) const; 166 virtual UInt_t GetNumberOfColumns() const; 167 virtual UInt_t GetOffset(Int_t columnIndex) const; 168 virtual Int_t GetOffset(const Char_t *columnName=0) const; 169 virtual UInt_t GetColumnSize(Int_t columnIndex) const; 170 virtual Int_t GetColumnSize(const Char_t *columnName=0) const; 171 virtual UInt_t GetTypeSize(Int_t columnIndex) const; 172 virtual Int_t GetTypeSize(const Char_t *columnName=0) const ; 173 virtual UInt_t GetDimensions(Int_t columnIndex) const; 174 virtual Int_t GetDimensions(const Char_t *columnName=0) const ; 175 virtual EColumnType GetColumnType(Int_t columnIndex) const; 176 virtual EColumnType GetColumnType(const Char_t *columnName=0) const; 177 virtual const Char_t *GetColumnComment(Int_t columnIndex) const; 178 179 static const char *GetTypeName(EColumnType type); 180 static EColumnType GetTypeId(const char *typeName); 181 182 // Table index iterator: 183 class iterator { 184 public: 185 typedef std::vector<Long_t>::iterator vec_iterator; 186 typedef std::vector<Long_t>::const_iterator vec_const_iterator; 187 private: 188 Long_t fRowSize; 189 const TTable *fThisTable; 190 vec_iterator fCurrentRow; 191 public: 192 iterator(): fRowSize(0), fThisTable(0) {;} 193 iterator(const TTable &table, vec_iterator &arowPtr) : 194 fRowSize(table.GetRowSize()), fThisTable(&table), fCurrentRow(arowPtr) {;} 195 iterator(const TTable &table, vec_const_iterator &arowPtr) : 196 fRowSize(table.GetRowSize()), fThisTable(&table), 197 fCurrentRow(*(std::vector<Long_t>::iterator *)(void *)&arowPtr) {;} 198 //fCurrentRow(* const_cast<vector<Long_t>::iterator *>(&arowPtr) ) {;} 199 iterator(const iterator& iter) : fRowSize (iter.fRowSize), fThisTable(iter.fThisTable),fCurrentRow(iter.fCurrentRow){} 200 iterator &operator=(const iterator& iter) { fRowSize = iter.fRowSize; fThisTable = iter.fThisTable; fCurrentRow=iter.fCurrentRow; return *this; } 201 void operator++() { ++fCurrentRow; } 202 void operator++(int) { fCurrentRow++; } 203 void operator--() { --fCurrentRow; } 204 void operator--(int) { fCurrentRow--; } 205 iterator operator+(Int_t idx) { std::vector<Long_t>::iterator addition = fCurrentRow+idx; return iterator(*fThisTable,addition); } 206 iterator operator-(Int_t idx) { std::vector<Long_t>::iterator subtraction = fCurrentRow-idx; return iterator(*fThisTable,subtraction); } 207 void operator+=(Int_t idx) { fCurrentRow+=idx; } 208 void operator-=(Int_t idx) { fCurrentRow-=idx; } 209 void *rowPtr() const { return (void *)(((const char *)fThisTable->GetArray()) + (*fCurrentRow)*fRowSize ); } 210 operator void *() const { return rowPtr(); } 211 Int_t operator-(const iterator &it) const { return (*fCurrentRow)-(*(it.fCurrentRow)); } 212 Long_t operator *() const { return *fCurrentRow; } 213 Bool_t operator==(const iterator &t) const { return ( (fCurrentRow == t.fCurrentRow) && (fThisTable == t.fThisTable) ); } 214 Bool_t operator!=(const iterator &t) const { return !operator==(t); } 215 216 const TTable &Table() const { return *fThisTable;} 217 const Long_t &RowSize() const { return fRowSize;} 218 #ifndef __CINT__ 219 const std::vector<Long_t>::iterator &Row() const { return fCurrentRow;} 220 #endif 221 }; 222 223 #ifndef __CINT__ 224 // pointer iterator 225 // This create an iterator to iterate over all table column of the 226 // type provided. 227 // For example" piterator(table,kPtr) is to iterate over 228 // all cells of (TTableMap *) type 229 230 class piterator { 231 private: 232 std::vector<ULong_t> fPtrs; 233 UInt_t fCurrentRowIndex; 234 UInt_t fCurrentColIndex; 235 UInt_t fRowSize; 236 const Char_t *fCurrentRowPtr; 237 void **fCurrentColPtr; 238 239 protected: 240 void **column() {return fCurrentColPtr = (void **)(fCurrentRowPtr + fPtrs[fCurrentColIndex]);} 241 242 public: 243 piterator(const TTable *t=0,EColumnType type=kPtr); 244 piterator(const piterator& iter); 245 void operator=(const piterator& iter); 246 247 void operator++(); 248 void operator++(int); 249 void operator--(); 250 void operator--(int); 251 252 // operator const char *() const; 253 void **operator *(); 254 255 Bool_t operator==(const piterator &t) const; 256 Bool_t operator!=(const piterator &t) const; 257 258 UInt_t Row() const; 259 UInt_t Column() const; 260 261 void MakeEnd(UInt_t lastRowIndex); 262 }; // class iterator over pointers 263 264 piterator pbegin(){ return piterator(this); } 265 piterator pend(){ piterator pLast(this); pLast.MakeEnd(GetNRows()); return pLast; } 266 #endif 267 static const char *TableDictionary() { return 0; }; 268 ClassDef(TTable,4) // vector of the C structures 269 }; 270 271 //________________________________________________________________________ 272 inline void TTable::AddAt(TDataSet *dataset,Int_t idx) 273 { TDataSet::AddAt(dataset,idx); } 274 275 //________________________________________________________________________ 276 inline Bool_t TTable::BoundsOk(const char *where, Int_t at) const 277 { 278 return (at < 0 || at >= fN) 279 ? OutOfBoundsError(where, at) 280 : kTRUE; 281 } 282 283 //________________________________________________________________________ 284 inline void *TTable::GetArray() const { return (void *)fTable;} 285 286 //________________________________________________________________________ 287 inline void TTable::Print(Option_t *) const { Print((Char_t *)0,Int_t(0)); } 288 289 //________________________________________________________________________ 290 inline void TTable::SetUsedRows(Int_t n) { fMaxIndex = n;} 291 //________________________________________________________________________ 292 inline void TTable::SetNRows(Int_t n) {SetUsedRows(n);} 293 // ULong_t &operator(){ return GetTable();} 294 295 //________________________________________________________________________ 296 inline void *TTable::operator[](Int_t i) 297 { 298 if (!BoundsOk("TTable::operator[]", i)) 299 i = 0; 300 return (void *)(fTable+i*fSize); 301 } 302 303 //________________________________________________________________________ 304 inline const void *TTable::operator[](Int_t i) const 305 { 306 if (!BoundsOk("TTable::operator[]", i)) 307 i = 0; 308 return (const void *)(fTable+i*fSize); 309 } 310 311 //________________________________________________________________________ 312 inline void TTable::Draw(Option_t *opt) 313 { Draw(opt, "", "", 1000000000, 0); } 314 315 #ifndef __CINT__ 316 //________________________________________________________________________________________________________________ 317 inline TTable::piterator::piterator(const piterator& iter) : 318 fPtrs (iter.fPtrs), 319 fCurrentRowIndex(iter.fCurrentRowIndex), 320 fCurrentColIndex(iter.fCurrentColIndex), 321 fRowSize(iter.fRowSize), 322 fCurrentRowPtr(iter.fCurrentRowPtr), 323 fCurrentColPtr(iter.fCurrentColPtr) 324 {} 325 //________________________________________________________________________________________________________________ 326 inline void TTable::piterator::operator=(const piterator& iter){ 327 fPtrs = iter.fPtrs; 328 fCurrentRowIndex = iter.fCurrentRowIndex; 329 fCurrentColIndex = iter.fCurrentColIndex; 330 fRowSize = iter.fRowSize; 331 fCurrentRowPtr = iter.fCurrentRowPtr; 332 fCurrentColPtr = iter.fCurrentColPtr; 333 } 334 //________________________________________________________________________________________________________________ 335 inline void TTable::piterator::operator++() 336 { 337 ++fCurrentColIndex; 338 if (fCurrentColIndex >= fPtrs.size()) { 339 fCurrentColIndex = 0; 340 ++fCurrentRowIndex; 341 fCurrentRowPtr += fRowSize; 342 } 343 column(); 344 } 345 //________________________________________________________________________________________________________________ 346 inline void TTable::piterator::operator++(int) { operator++(); } 347 //________________________________________________________________________________________________________________ 348 inline void TTable::piterator::operator--() 349 { 350 if (fCurrentColIndex > 0) { 351 fCurrentColIndex--; 352 fCurrentColIndex = fPtrs.size()-1; 353 --fCurrentRowIndex; 354 fCurrentRowPtr -= fRowSize; 355 } else { 356 fCurrentColIndex--; 357 } 358 column(); 359 } 360 //________________________________________________________________________________________________________________ 361 inline void TTable::piterator::operator--(int) { operator--(); } 362 //________________________________________________________________________________________________________________ 363 // inline TTable::piterator::operator const char *() const { return fCurrentColPtr; } 364 //________________________________________________________________________________________________________________ 365 inline void **TTable::piterator::operator *() { return fCurrentColPtr; } 366 //________________________________________________________________________________________________________________ 367 inline Bool_t TTable::piterator::operator==(const piterator &t) const { 368 return ( 369 (fCurrentRowIndex== t.fCurrentRowIndex) 370 && (fCurrentColIndex == t.fCurrentColIndex) 371 // && (fCurrentRowPtr == t.fCurrentRowPtr ) 372 // && (fCurrentColPtr == t.fCurrentColPtr ) 373 ); 374 } 375 //________________________________________________________________________________________________________________ 376 inline Bool_t TTable::piterator::operator!=(const piterator &t) const { return !operator==(t); } 377 //________________________________________________________________________________________________________________ 378 inline void TTable::piterator::MakeEnd(UInt_t lastRowIndex){fCurrentColIndex = 0; fCurrentRowIndex = lastRowIndex;} 379 //________________________________________________________________________________________________________________ 380 inline UInt_t TTable::piterator::Row() const { return fCurrentRowIndex;} 381 //________________________________________________________________________________________________________________ 382 inline UInt_t TTable::piterator::Column() const { return fCurrentColIndex;} 383 #endif 384 #include "TTableDescriptor.h" 385 386 #endif 387