TIndexTable.h

Go to the documentation of this file.
00001 // @(#)root/table:$Id: TIndexTable.h 35145 2010-09-04 19:01:19Z pcanal $
00002 // Author: Valery Fine(fine@bnl.gov)   01/03/2001
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers.               *
00006  * Copyright (C) 2001 [BNL] Brookhaven National Laboratory.              *
00007  * All rights reserved.                                                  *
00008  *                                                                       *
00009  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00010  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00011  *************************************************************************/
00012 
00013 #ifndef ROOT_TIndexTable
00014 #define ROOT_TIndexTable
00015 
00016 #ifndef ROOT_TTable
00017 #include "TTable.h"
00018 #endif
00019 
00020 //////////////////////////////////////////////////////
00021 //
00022 // Class TIndexTable
00023 // Iterator of the table with extra index array
00024 //
00025 //////////////////////////////////////////////////////
00026 
00027 
00028 class TIndexTable : public TTable {
00029 protected:
00030    const TTable *fRefTable;
00031 public:
00032    class iterator {
00033    protected:
00034       const TTable *fTable;
00035       const int   *fCurrentRow;
00036       iterator(): fTable(0), fCurrentRow(0) {}
00037    public:
00038       iterator(const TTable &t, const int &rowPtr): fTable(&t), fCurrentRow(&rowPtr){}
00039       iterator(const TTable &t): fTable(&t),fCurrentRow(0){}
00040       iterator(const iterator& iter) : fTable(iter.fTable), fCurrentRow(iter.fCurrentRow){}
00041       iterator &operator=(const iterator& iter) {fTable = iter.fTable; fCurrentRow = iter.fCurrentRow; return *this;}
00042       iterator &operator++()    { if (fCurrentRow) ++fCurrentRow; return *this;}
00043       void     operator++(int)  { if (fCurrentRow) fCurrentRow++;}
00044       iterator &operator--()    { if (fCurrentRow) --fCurrentRow; return *this;}
00045       void     operator--(int) { if (fCurrentRow) fCurrentRow--;}
00046       iterator &operator+(Int_t idx) { if (fCurrentRow) fCurrentRow+=idx; return *this;}
00047       iterator &operator-(Int_t idx) { if (fCurrentRow) fCurrentRow-=idx; return *this;}
00048       Int_t operator-(const iterator &it) const { return fCurrentRow-it.fCurrentRow; }
00049       void *operator *(){ return (void *)(fTable?((char *)fTable->GetArray())+(*fCurrentRow)*(fTable->GetRowSize()):0);}
00050       operator int()  { return *fCurrentRow;}
00051       Bool_t operator==(const iterator &t) const { return (fCurrentRow == t.fCurrentRow); }
00052       Bool_t operator!=(const iterator &t) const { return !operator==(t); }
00053    };
00054    TIndexTable(const TTable *table);
00055    TIndexTable(const TIndexTable &indx): TTable(indx){}
00056    int  *GetTable(Int_t i=0);
00057    Bool_t  IsValid() const;
00058    void    push_back(Long_t next);
00059 
00060    const TTable *Table() const;
00061    iterator begin()        { return ((const TIndexTable *)this)->begin();}
00062    iterator begin() const  { return GetNRows() ? iterator(*Table(),*GetTable(0)):end();}
00063    iterator end()   { return ((const TIndexTable *)this)->end(); }
00064    iterator end()   const  {Long_t i = GetNRows(); return i? iterator(*Table(), *GetTable(i)):iterator(*this);}
00065 
00066 protected:
00067    static TTableDescriptor *CreateDescriptor();
00068 
00069 // define ClassDefTable(TIndexTable,int)
00070 protected:
00071    static TTableDescriptor *fgColDescriptors;
00072    virtual TTableDescriptor *GetDescriptorPointer() const;
00073    virtual void SetDescriptorPointer(TTableDescriptor *list);
00074 public:
00075    TIndexTable() : TTable("TIndexTable",sizeof(int))    {SetType("int");}
00076    TIndexTable(const char *name) : TTable(name,sizeof(int)) {SetType("int");}
00077    TIndexTable(Int_t n) : TTable("TIndexTable",n,sizeof(int)) {SetType("int");}
00078    TIndexTable(const char *name,Int_t n) : TTable(name,n,sizeof(int)) {SetType("int");}
00079    virtual ~TIndexTable() {}
00080    const int *GetTable(Int_t i=0) const;
00081    int &operator[](Int_t i){ assert(i>=0 && i < GetNRows()); return *GetTable(i); }
00082    const int &operator[](Int_t i) const { assert(i>=0 && i < GetNRows()); return *((const int *)(GetTable(i))); }
00083    ClassDef(TIndexTable,4) // "Index" array for TTable object
00084 };
00085 
00086 //___________________________________________________________________________________________________________
00087 inline  int  *TIndexTable::GetTable(Int_t i) { return ((int *)GetArray())+i;}
00088 //___________________________________________________________________________________________________________
00089 inline  const int *TIndexTable::GetTable(Int_t i) const { return ((int *)GetArray())+i;}
00090 //___________________________________________________________________________________________________________
00091 inline  Bool_t TIndexTable::IsValid() const
00092 {
00093    // Check whether all "map" values do belong the table
00094    const TTable *cont= Table();
00095    if (!cont) return kFALSE;
00096 
00097    iterator i      = begin();
00098    iterator finish = end();
00099    Int_t totalSize         = cont->GetNRows();
00100 
00101    for (; i != finish; i++) {
00102       int th = i;
00103       if (  th == -1 || (0 <= th && th < totalSize) ) continue;
00104       return kFALSE;
00105    }
00106    return kTRUE;
00107 }
00108 //___________________________________________________________________________________________________________
00109 inline void TIndexTable::push_back(Long_t next){ AddAt(&next); }
00110 
00111 #endif

Generated on Tue Jul 5 14:26:58 2011 for ROOT_528-00b_version by  doxygen 1.5.1