TObjArray.h

Go to the documentation of this file.
00001 // @(#)root/cont:$Id: TObjArray.h 34744 2010-08-07 06:16:36Z brun $
00002 // Author: Fons Rademakers   11/09/95
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, 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_TObjArray
00013 #define ROOT_TObjArray
00014 
00015 
00016 //////////////////////////////////////////////////////////////////////////
00017 //                                                                      //
00018 // TObjArray                                                            //
00019 //                                                                      //
00020 // An array of TObjects. The array expands automatically when adding    //
00021 // elements (shrinking can be done by hand).                            //
00022 //                                                                      //
00023 //////////////////////////////////////////////////////////////////////////
00024 
00025 #ifndef ROOT_TSeqCollection
00026 #include "TSeqCollection.h"
00027 #endif
00028 
00029 #include <iterator>
00030 
00031 #if (__GNUC__ >= 3) && !defined(__INTEL_COMPILER)
00032 // Prevent -Weffc++ from complaining about the inheritance
00033 // TObjArrayIter from std::iterator.
00034 #pragma GCC system_header
00035 #endif
00036 
00037 class TObjArrayIter;
00038 
00039 class TObjArray : public TSeqCollection {
00040 
00041 friend class TObjArrayIter;
00042 friend class TClonesArray;
00043 
00044 protected:
00045    TObject     **fCont;        //!Array contents
00046    Int_t         fLowerBound;  //Lower bound of the array
00047    Int_t         fLast;        //Last element in array containing an object
00048 
00049    Bool_t        BoundsOk(const char *where, Int_t at) const;
00050    void          Init(Int_t s, Int_t lowerBound);
00051    Bool_t        OutOfBoundsError(const char *where, Int_t i) const;
00052    Int_t         GetAbsLast() const;
00053 
00054 public:
00055    typedef TObjArrayIter Iterator_t;
00056 
00057    TObjArray(Int_t s = TCollection::kInitCapacity, Int_t lowerBound = 0);
00058    TObjArray(const TObjArray &a);
00059    virtual          ~TObjArray();
00060    TObjArray& operator=(const TObjArray&);
00061    virtual void     Clear(Option_t *option="");
00062    virtual void     Compress();
00063    virtual void     Delete(Option_t *option="");
00064    virtual void     Expand(Int_t newSize);   // expand or shrink an array
00065    Int_t            GetEntries() const;
00066    Int_t            GetEntriesFast() const {
00067       return GetAbsLast() + 1;   //only OK when no gaps
00068    }
00069    Int_t            GetLast() const;
00070    TObject        **GetObjectRef(const TObject *obj) const;
00071    Bool_t           IsEmpty() const { return GetAbsLast() == -1; }
00072    TIterator       *MakeIterator(Bool_t dir = kIterForward) const;
00073 
00074    void             Add(TObject *obj) { AddLast(obj); }
00075    virtual void     AddFirst(TObject *obj);
00076    virtual void     AddLast(TObject *obj);
00077    virtual void     AddAt(TObject *obj, Int_t idx);
00078    virtual void     AddAtAndExpand(TObject *obj, Int_t idx);
00079    virtual Int_t    AddAtFree(TObject *obj);
00080    virtual void     AddAfter(const TObject *after, TObject *obj);
00081    virtual void     AddBefore(const TObject *before, TObject *obj);
00082    virtual TObject *FindObject(const char *name) const;
00083    virtual TObject *FindObject(const TObject *obj) const;
00084    virtual TObject *RemoveAt(Int_t idx);
00085    virtual TObject *Remove(TObject *obj);
00086    virtual void     RemoveRange(Int_t idx1, Int_t idx2);
00087    virtual void     RecursiveRemove(TObject *obj);
00088 
00089    TObject         *At(Int_t idx) const;
00090    TObject         *UncheckedAt(Int_t i) const { return fCont[i-fLowerBound]; }
00091    TObject         *Before(const TObject *obj) const;
00092    TObject         *After(const TObject *obj) const;
00093    TObject         *First() const;
00094    TObject         *Last() const;
00095    virtual TObject *&operator[](Int_t i);
00096    virtual TObject *operator[](Int_t i) const;
00097    Int_t            LowerBound() const { return fLowerBound; }
00098    Int_t            IndexOf(const TObject *obj) const;
00099    void             SetLast(Int_t last);
00100 
00101    virtual void     Randomize(Int_t ntimes=1);
00102    virtual void     Sort(Int_t upto = kMaxInt);
00103    virtual Int_t    BinarySearch(TObject *obj, Int_t upto = kMaxInt); // the TObjArray has to be sorted, -1 == not found !!
00104 
00105    ClassDef(TObjArray,3)  //An array of objects
00106 };
00107 
00108 
00109 // Preventing warnings with -Weffc++ in GCC since it is a false positive for the TObjArrayIter destructor.
00110 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
00111 #pragma GCC diagnostic push
00112 #pragma GCC diagnostic ignored "-Weffc++"
00113 #endif
00114 
00115 //////////////////////////////////////////////////////////////////////////
00116 //                                                                      //
00117 // TObjArrayIter                                                        //
00118 //                                                                      //
00119 // Iterator of object array.                                            //
00120 //                                                                      //
00121 //////////////////////////////////////////////////////////////////////////
00122 
00123 class TObjArrayIter : public TIterator,
00124                       public std::iterator<std::bidirectional_iterator_tag, // TODO: ideally it should be a  randomaccess_iterator_tag
00125                                            TObject*, std::ptrdiff_t,
00126                                            const TObject**, const TObject*&> {
00127 
00128 private:
00129    const TObjArray  *fArray;     //array being iterated
00130    Int_t             fCurCursor; //current position in array
00131    Int_t             fCursor;    //next position in array
00132    Bool_t            fDirection; //iteration direction
00133 
00134    TObjArrayIter() : fArray(0), fCurCursor(0), fCursor(0), fDirection(kIterForward) { }
00135 
00136 public:
00137    TObjArrayIter(const TObjArray *arr, Bool_t dir = kIterForward);
00138    TObjArrayIter(const TObjArrayIter &iter);
00139    ~TObjArrayIter() { }
00140    TIterator     &operator=(const TIterator &rhs);
00141    TObjArrayIter &operator=(const TObjArrayIter &rhs);
00142 
00143    const TCollection *GetCollection() const { return fArray; }
00144    TObject           *Next();
00145    void               Reset();
00146    bool               operator!=(const TIterator &aIter) const;
00147    bool               operator!=(const TObjArrayIter &aIter) const;
00148    TObject           *operator*() const;
00149 
00150    ClassDef(TObjArrayIter,0)  //Object array iterator
00151 };
00152 
00153 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
00154 #pragma GCC diagnostic pop
00155 #endif
00156 
00157 //---- inlines -----------------------------------------------------------------
00158 
00159 inline Bool_t TObjArray::BoundsOk(const char *where, Int_t at) const
00160 {
00161    return (at < fLowerBound || at-fLowerBound >= fSize)
00162                   ? OutOfBoundsError(where, at)
00163                   : kTRUE;
00164 }
00165 
00166 inline TObject *TObjArray::At(Int_t i) const
00167 {
00168    // Return the object at position i. Returns 0 if i is out of bounds.
00169    int j = i-fLowerBound;
00170    if (j >= 0 && j < fSize) return fCont[j];
00171    BoundsOk("At", i);
00172    return 0;
00173 }
00174 
00175 #endif

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