ROOT logo
// @(#)root/table:$Id: TIndexTable.h 35145 2010-09-04 19:01:19Z pcanal $
// Author: Valery Fine(fine@bnl.gov)   01/03/2001

/*************************************************************************
 * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers.               *
 * Copyright (C) 2001 [BNL] Brookhaven National Laboratory.              *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TIndexTable
#define ROOT_TIndexTable

#ifndef ROOT_TTable
#include "TTable.h"
#endif

//////////////////////////////////////////////////////
//
// Class TIndexTable
// Iterator of the table with extra index array
//
//////////////////////////////////////////////////////


class TIndexTable : public TTable {
protected:
   const TTable *fRefTable;
public:
   class iterator {
   protected:
      const TTable *fTable;
      const int   *fCurrentRow;
      iterator(): fTable(0), fCurrentRow(0) {}
   public:
      iterator(const TTable &t, const int &rowPtr): fTable(&t), fCurrentRow(&rowPtr){}
      iterator(const TTable &t): fTable(&t),fCurrentRow(0){}
      iterator(const iterator& iter) : fTable(iter.fTable), fCurrentRow(iter.fCurrentRow){}
      iterator &operator=(const iterator& iter) {fTable = iter.fTable; fCurrentRow = iter.fCurrentRow; return *this;}
      iterator &operator++()    { if (fCurrentRow) ++fCurrentRow; return *this;}
      void     operator++(int)  { if (fCurrentRow) fCurrentRow++;}
      iterator &operator--()    { if (fCurrentRow) --fCurrentRow; return *this;}
      void     operator--(int) { if (fCurrentRow) fCurrentRow--;}
      iterator &operator+(Int_t idx) { if (fCurrentRow) fCurrentRow+=idx; return *this;}
      iterator &operator-(Int_t idx) { if (fCurrentRow) fCurrentRow-=idx; return *this;}
      Int_t operator-(const iterator &it) const { return fCurrentRow-it.fCurrentRow; }
      void *operator *(){ return (void *)(fTable?((char *)fTable->GetArray())+(*fCurrentRow)*(fTable->GetRowSize()):0);}
      operator int()  { return *fCurrentRow;}
      Bool_t operator==(const iterator &t) const { return (fCurrentRow == t.fCurrentRow); }
      Bool_t operator!=(const iterator &t) const { return !operator==(t); }
   };
   TIndexTable(const TTable *table);
   TIndexTable(const TIndexTable &indx): TTable(indx){}
   int  *GetTable(Int_t i=0);
   Bool_t  IsValid() const;
   void    push_back(Long_t next);

   const TTable *Table() const;
   iterator begin()        { return ((const TIndexTable *)this)->begin();}
   iterator begin() const  { return GetNRows() ? iterator(*Table(),*GetTable(0)):end();}
   iterator end()   { return ((const TIndexTable *)this)->end(); }
   iterator end()   const  {Long_t i = GetNRows(); return i? iterator(*Table(), *GetTable(i)):iterator(*this);}

protected:
   static TTableDescriptor *CreateDescriptor();

// define ClassDefTable(TIndexTable,int)
protected:
   static TTableDescriptor *fgColDescriptors;
   virtual TTableDescriptor *GetDescriptorPointer() const;
   virtual void SetDescriptorPointer(TTableDescriptor *list);
public:
   TIndexTable() : TTable("TIndexTable",sizeof(int))    {SetType("int");}
   TIndexTable(const char *name) : TTable(name,sizeof(int)) {SetType("int");}
   TIndexTable(Int_t n) : TTable("TIndexTable",n,sizeof(int)) {SetType("int");}
   TIndexTable(const char *name,Int_t n) : TTable(name,n,sizeof(int)) {SetType("int");}
   virtual ~TIndexTable() {}
   const int *GetTable(Int_t i=0) const;
   int &operator[](Int_t i){ assert(i>=0 && i < GetNRows()); return *GetTable(i); }
   const int &operator[](Int_t i) const { assert(i>=0 && i < GetNRows()); return *((const int *)(GetTable(i))); }
   ClassDef(TIndexTable,4) // "Index" array for TTable object
};

//___________________________________________________________________________________________________________
inline  int  *TIndexTable::GetTable(Int_t i) { return ((int *)GetArray())+i;}
//___________________________________________________________________________________________________________
inline  const int *TIndexTable::GetTable(Int_t i) const { return ((int *)GetArray())+i;}
//___________________________________________________________________________________________________________
inline  Bool_t TIndexTable::IsValid() const
{
   // Check whether all "map" values do belong the table
   const TTable *cont= Table();
   if (!cont) return kFALSE;

   iterator i      = begin();
   iterator finish = end();
   Int_t totalSize         = cont->GetNRows();

   for (; i != finish; i++) {
      int th = i;
      if (  th == -1 || (0 <= th && th < totalSize) ) continue;
      return kFALSE;
   }
   return kTRUE;
}
//___________________________________________________________________________________________________________
inline void TIndexTable::push_back(Long_t next){ AddAt(&next); }

#endif
 TIndexTable.h:1
 TIndexTable.h:2
 TIndexTable.h:3
 TIndexTable.h:4
 TIndexTable.h:5
 TIndexTable.h:6
 TIndexTable.h:7
 TIndexTable.h:8
 TIndexTable.h:9
 TIndexTable.h:10
 TIndexTable.h:11
 TIndexTable.h:12
 TIndexTable.h:13
 TIndexTable.h:14
 TIndexTable.h:15
 TIndexTable.h:16
 TIndexTable.h:17
 TIndexTable.h:18
 TIndexTable.h:19
 TIndexTable.h:20
 TIndexTable.h:21
 TIndexTable.h:22
 TIndexTable.h:23
 TIndexTable.h:24
 TIndexTable.h:25
 TIndexTable.h:26
 TIndexTable.h:27
 TIndexTable.h:28
 TIndexTable.h:29
 TIndexTable.h:30
 TIndexTable.h:31
 TIndexTable.h:32
 TIndexTable.h:33
 TIndexTable.h:34
 TIndexTable.h:35
 TIndexTable.h:36
 TIndexTable.h:37
 TIndexTable.h:38
 TIndexTable.h:39
 TIndexTable.h:40
 TIndexTable.h:41
 TIndexTable.h:42
 TIndexTable.h:43
 TIndexTable.h:44
 TIndexTable.h:45
 TIndexTable.h:46
 TIndexTable.h:47
 TIndexTable.h:48
 TIndexTable.h:49
 TIndexTable.h:50
 TIndexTable.h:51
 TIndexTable.h:52
 TIndexTable.h:53
 TIndexTable.h:54
 TIndexTable.h:55
 TIndexTable.h:56
 TIndexTable.h:57
 TIndexTable.h:58
 TIndexTable.h:59
 TIndexTable.h:60
 TIndexTable.h:61
 TIndexTable.h:62
 TIndexTable.h:63
 TIndexTable.h:64
 TIndexTable.h:65
 TIndexTable.h:66
 TIndexTable.h:67
 TIndexTable.h:68
 TIndexTable.h:69
 TIndexTable.h:70
 TIndexTable.h:71
 TIndexTable.h:72
 TIndexTable.h:73
 TIndexTable.h:74
 TIndexTable.h:75
 TIndexTable.h:76
 TIndexTable.h:77
 TIndexTable.h:78
 TIndexTable.h:79
 TIndexTable.h:80
 TIndexTable.h:81
 TIndexTable.h:82
 TIndexTable.h:83
 TIndexTable.h:84
 TIndexTable.h:85
 TIndexTable.h:86
 TIndexTable.h:87
 TIndexTable.h:88
 TIndexTable.h:89
 TIndexTable.h:90
 TIndexTable.h:91
 TIndexTable.h:92
 TIndexTable.h:93
 TIndexTable.h:94
 TIndexTable.h:95
 TIndexTable.h:96
 TIndexTable.h:97
 TIndexTable.h:98
 TIndexTable.h:99
 TIndexTable.h:100
 TIndexTable.h:101
 TIndexTable.h:102
 TIndexTable.h:103
 TIndexTable.h:104
 TIndexTable.h:105
 TIndexTable.h:106
 TIndexTable.h:107
 TIndexTable.h:108
 TIndexTable.h:109
 TIndexTable.h:110
 TIndexTable.h:111