TRefArray.h

Go to the documentation of this file.
00001 // @(#)root/cont:$Id: TRefArray.h 34744 2010-08-07 06:16:36Z brun $
00002 // Author: Rene Brun    02/10/2001
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2001, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOT_TRefArray
00013 #define ROOT_TRefArray
00014 
00015 
00016 //////////////////////////////////////////////////////////////////////////
00017 //                                                                      //
00018 // TRefArray                                                            //
00019 //                                                                      //
00020 // An array of references to TObjects.                                  //
00021 // The array expands automatically when adding elements.                //
00022 //                                                                      //
00023 //////////////////////////////////////////////////////////////////////////
00024 
00025 #ifndef ROOT_TSeqCollection
00026 #include "TSeqCollection.h"
00027 #endif
00028 #ifndef ROOT_TProcessID
00029 #include "TProcessID.h"
00030 #endif
00031 
00032 #include <iterator>
00033 
00034 #if (__GNUC__ >= 3) && !defined(__INTEL_COMPILER)
00035 // Prevent -Weffc++ from complaining about the inheritance
00036 // TRefArrayIter from std::iterator.
00037 #pragma GCC system_header
00038 #endif
00039 
00040 class TSystem;
00041 class TRefArrayIter;
00042 
00043 class TRefArray : public TSeqCollection {
00044 
00045 friend class TRefArrayIter;
00046 
00047 protected:
00048    TProcessID   *fPID;         //Pointer to Process Unique Identifier
00049    UInt_t       *fUIDs;        //[fSize] To store uids of referenced objects
00050    Int_t         fLowerBound;  //Lower bound of the array
00051    Int_t         fLast;        //Last element in array containing an object
00052 
00053    Bool_t        BoundsOk(const char *where, Int_t at) const;
00054    void          Init(Int_t s, Int_t lowerBound);
00055    Bool_t        OutOfBoundsError(const char *where, Int_t i) const;
00056    Int_t         GetAbsLast() const;
00057    TObject      *GetFromTable(Int_t idx) const;
00058 
00059 public:
00060    typedef TRefArrayIter Iterator_t;
00061 
00062    TRefArray(TProcessID *pid = 0);
00063    TRefArray(Int_t s, TProcessID *pid);
00064    TRefArray(Int_t s, Int_t lowerBound = 0, TProcessID *pid = 0);
00065    TRefArray(const TRefArray &a);
00066    TRefArray& operator=(const TRefArray &a);
00067    virtual          ~TRefArray();
00068    virtual void     Clear(Option_t *option="");
00069    virtual void     Compress();
00070    virtual void     Delete(Option_t *option="");
00071    virtual void     Expand(Int_t newSize);   // expand or shrink an array
00072    Int_t            GetEntries() const;
00073    Int_t            GetEntriesFast() const {
00074       return GetAbsLast() + 1;   //only OK when no gaps
00075    }
00076    Int_t            GetLast() const;
00077    TObject        **GetObjectRef(const TObject *obj) const;
00078    TProcessID      *GetPID() const {return fPID;}
00079    UInt_t           GetUID(Int_t at) const;
00080    Bool_t           IsEmpty() const { return GetAbsLast() == -1; }
00081    TIterator       *MakeIterator(Bool_t dir = kIterForward) const;
00082 
00083    void             Add(TObject *obj) { AddLast(obj); }
00084    virtual void     AddFirst(TObject *obj);
00085    virtual void     AddLast(TObject *obj);
00086    virtual void     AddAt(TObject *obj, Int_t idx);
00087    virtual void     AddAtAndExpand(TObject *obj, Int_t idx);
00088    virtual Int_t    AddAtFree(TObject *obj);
00089    virtual void     AddAfter(const TObject *after, TObject *obj);
00090    virtual void     AddBefore(const TObject *before, TObject *obj);
00091    virtual TObject *RemoveAt(Int_t idx);
00092    virtual TObject *Remove(TObject *obj);
00093 
00094    TObject         *At(Int_t idx) const;
00095    TObject         *Before(const TObject *obj) const;
00096    TObject         *After(const TObject *obj) const;
00097    TObject         *First() const;
00098    TObject         *Last() const;
00099    virtual TObject *operator[](Int_t i) const;
00100    Int_t            LowerBound() const { return fLowerBound; }
00101    Int_t            IndexOf(const TObject *obj) const;
00102    void             SetLast(Int_t last);
00103 
00104    virtual void     Sort(Int_t upto = kMaxInt);
00105    virtual Int_t    BinarySearch(TObject *obj, Int_t upto = kMaxInt); // the TRefArray has to be sorted, -1 == not found !!
00106 
00107    ClassDef(TRefArray,1)  //An array of references to TObjects
00108 };
00109 
00110 
00111 // Preventing warnings with -Weffc++ in GCC since it is a false positive for the TRefArrayIter destructor.
00112 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
00113 #pragma GCC diagnostic push
00114 #pragma GCC diagnostic ignored "-Weffc++"
00115 #endif
00116 
00117 //////////////////////////////////////////////////////////////////////////
00118 //                                                                      //
00119 // TRefArrayIter                                                        //
00120 //                                                                      //
00121 // Iterator of object array.                                            //
00122 //                                                                      //
00123 //////////////////////////////////////////////////////////////////////////
00124 
00125 class TRefArrayIter : public TIterator,
00126                       public std::iterator<std::bidirectional_iterator_tag, // TODO: ideally it should be a  randomaccess_iterator_tag
00127                                            TObject*, std::ptrdiff_t,
00128                                            const TObject**, const TObject*&> {
00129 
00130 private:
00131    const TRefArray  *fArray;      //array being iterated
00132    Int_t             fCurCursor;  //current position in array
00133    Int_t             fCursor;     //next position in array
00134    Bool_t            fDirection;  //iteration direction
00135 
00136    TRefArrayIter() : fArray(0), fCurCursor(0), fCursor(0), fDirection(kIterForward) { }
00137 
00138 public:
00139    TRefArrayIter(const TRefArray *arr, Bool_t dir = kIterForward);
00140    TRefArrayIter(const TRefArrayIter &iter);
00141    ~TRefArrayIter() { }
00142    TIterator         &operator=(const TIterator &rhs);
00143    TRefArrayIter     &operator=(const TRefArrayIter &rhs);
00144 
00145    const TCollection *GetCollection() const { return fArray; }
00146    TObject           *Next();
00147    void               Reset();
00148    bool               operator!=(const TIterator &aIter) const;
00149    bool               operator!=(const TRefArrayIter &aIter) const;
00150    TObject           *operator*() const;
00151 
00152    ClassDef(TRefArrayIter,0)  //Object array iterator
00153 };
00154 
00155 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
00156 #pragma GCC diagnostic pop
00157 #endif
00158 
00159 
00160 //---- inlines -----------------------------------------------------------------
00161 
00162 inline Bool_t TRefArray::BoundsOk(const char *where, Int_t at) const
00163 {
00164    return (at < fLowerBound || at-fLowerBound >= fSize)
00165                   ? OutOfBoundsError(where, at)
00166                   : kTRUE;
00167 }
00168 
00169 inline TObject *TRefArray::operator[](Int_t at) const
00170 {
00171    int j = at-fLowerBound;
00172    if (j >= 0 && j < fSize) {
00173       if (!fPID) return 0;
00174       TObject *obj = fPID->GetObjectWithID(fUIDs[j]);
00175       if (obj==0) obj = GetFromTable(j);
00176       return obj;
00177    }
00178    BoundsOk("At", at);
00179    return 0;
00180 }
00181 
00182 inline TObject *TRefArray::At(Int_t at) const
00183 {
00184    // Return the object at position i. Returns 0 if i is out of bounds.
00185    int j = at-fLowerBound;
00186    if (j >= 0 && j < fSize) {
00187       if (!fPID) return 0;
00188       TObject *obj = fPID->GetObjectWithID(fUIDs[j]);
00189       if (obj==0) obj = GetFromTable(j);
00190       return obj;
00191    }
00192    BoundsOk("At", at);
00193    return 0;
00194 }
00195 
00196 #endif

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