TOrdCollection.h

Go to the documentation of this file.
00001 // @(#)root/cont:$Id: TOrdCollection.h 23531 2008-04-24 16:23:42Z rdm $
00002 // Author: Fons Rademakers   13/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_TOrdCollection
00013 #define ROOT_TOrdCollection
00014 
00015 
00016 //////////////////////////////////////////////////////////////////////////
00017 //                                                                      //
00018 // TOrdCollection                                                       //
00019 //                                                                      //
00020 // Ordered collection.                                                  //
00021 //                                                                      //
00022 //////////////////////////////////////////////////////////////////////////
00023 
00024 #ifndef ROOT_TSeqCollection
00025 #include "TSeqCollection.h"
00026 #endif
00027 
00028 #include <iterator>
00029 
00030 
00031 class TOrdCollectionIter;
00032 
00033 
00034 class TOrdCollection : public TSeqCollection {
00035 
00036 friend class  TOrdCollectionIter;
00037 
00038 private:
00039    TObject  **fCont;
00040    Int_t      fCapacity;
00041    Int_t      fGapStart;
00042    Int_t      fGapSize;
00043 
00044    Int_t      PhysIndex(Int_t idx) const;
00045    Int_t      LogIndex(Int_t idx) const;
00046    void       MoveGapTo(Int_t newGapStart);
00047    Bool_t     IllegalIndex(const char *method, Int_t idx) const;
00048    void       Init(Int_t capacity);
00049    Bool_t     LowWaterMark() const;
00050    void       SetCapacity(Int_t newCapacity);
00051 
00052    TOrdCollection(const TOrdCollection&); // Not implemented
00053    TOrdCollection& operator=(const TOrdCollection&); // Not implemented
00054 
00055 public:
00056    enum { kDefaultCapacity = 1, kMinExpand = 8, kShrinkFactor = 2 };
00057 
00058    typedef TOrdCollectionIter Iterator_t;
00059 
00060    TOrdCollection(Int_t capacity = kDefaultCapacity);
00061    ~TOrdCollection();
00062    void          Clear(Option_t *option="");
00063    void          Delete(Option_t *option="");
00064    TObject     **GetObjectRef(const TObject *obj) const;
00065    Int_t         IndexOf(const TObject *obj) const;
00066    TIterator    *MakeIterator(Bool_t dir = kIterForward) const;
00067 
00068    void          AddFirst(TObject *obj);
00069    void          AddLast(TObject *obj);
00070    void          AddAt(TObject *obj, Int_t idx);
00071    void          AddAfter(const TObject *after, TObject *obj);
00072    void          AddBefore(const TObject *before, TObject *obj);
00073    void          PutAt(TObject *obj, Int_t idx);
00074    TObject      *RemoveAt(Int_t idx);
00075    TObject      *Remove(TObject *obj);
00076 
00077    TObject      *At(Int_t idx) const;
00078    TObject      *Before(const TObject *obj) const;
00079    TObject      *After(const TObject *obj) const;
00080    TObject      *First() const;
00081    TObject      *Last() const;
00082 
00083    void          Sort();
00084    Int_t         BinarySearch(TObject *obj);
00085 
00086    ClassDef(TOrdCollection,0)  //An ordered collection
00087 };
00088 
00089 
00090 //////////////////////////////////////////////////////////////////////////
00091 //                                                                      //
00092 // TOrdCollectionIter                                                   //
00093 //                                                                      //
00094 // Iterator of ordered collection.                                      //
00095 //                                                                      //
00096 //////////////////////////////////////////////////////////////////////////
00097 
00098 class TOrdCollectionIter : public TIterator,
00099                            public std::iterator<std::bidirectional_iterator_tag,
00100                                                 TObject*, std::ptrdiff_t,
00101                                                 const TObject**, const TObject*&> {
00102 
00103 private:
00104    const TOrdCollection  *fCol;       //collection being iterated
00105    Int_t                  fCurCursor; //current position in collection
00106    Int_t                  fCursor;    //next position in collection
00107    Bool_t                 fDirection; //iteration direction
00108 
00109    TOrdCollectionIter() : fCol(0), fCurCursor(0), fCursor(0), fDirection(kIterForward) { }
00110 
00111 public:
00112    TOrdCollectionIter(const TOrdCollection *col, Bool_t dir = kIterForward);
00113    TOrdCollectionIter(const TOrdCollectionIter &iter);
00114    ~TOrdCollectionIter() { }
00115    TIterator          &operator=(const TIterator &rhs);
00116    TOrdCollectionIter &operator=(const TOrdCollectionIter &rhs);
00117 
00118    const TCollection *GetCollection() const { return fCol; }
00119    TObject           *Next();
00120    void               Reset();
00121    bool               operator!=(const TIterator &aIter) const;
00122    bool               operator!=(const TOrdCollectionIter &aIter) const;
00123    TObject           *operator*() const;
00124 
00125    ClassDef(TOrdCollectionIter,0)  //Ordered collection iterator
00126 };
00127 
00128 
00129 //---- inlines -----------------------------------------------------------------
00130 
00131 inline Bool_t TOrdCollection::LowWaterMark() const
00132 {
00133    return (fSize < (fCapacity / 4) && fSize > TCollection::kInitCapacity);
00134 }
00135 
00136 inline Int_t TOrdCollection::PhysIndex(Int_t idx) const
00137    { return (idx < fGapStart) ? idx : idx + fGapSize; }
00138 
00139 inline Int_t TOrdCollection::LogIndex(Int_t idx) const
00140    { return (idx < fGapStart) ? idx : idx - fGapSize; }
00141 
00142 #endif

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