Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TObjArray.h
Go to the documentation of this file.
1 // @(#)root/cont:$Id$
2 // Author: Fons Rademakers 11/09/95
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TObjArray
13 #define ROOT_TObjArray
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TObjArray //
19 // //
20 // An array of TObjects. The array expands automatically when adding //
21 // elements (shrinking can be done by hand). //
22 // //
23 //////////////////////////////////////////////////////////////////////////
24 
25 #include "TSeqCollection.h"
26 
27 #include <iterator>
28 
29 #if (__GNUC__ >= 3) && !defined(__INTEL_COMPILER)
30 // Prevent -Weffc++ from complaining about the inheritance
31 // TObjArrayIter from std::iterator.
32 #pragma GCC system_header
33 #endif
34 
35 class TObjArrayIter;
36 
37 class TObjArray : public TSeqCollection {
38 
39 friend class TObjArrayIter;
40 friend class TClonesArray;
41 
42 protected:
43  TObject **fCont; //!Array contents
44  Int_t fLowerBound; //Lower bound of the array
45  Int_t fLast; //Last element in array containing an object
46 
47  Bool_t BoundsOk(const char *where, Int_t at) const;
48  void Init(Int_t s, Int_t lowerBound);
49  Bool_t OutOfBoundsError(const char *where, Int_t i) const;
50  Int_t GetAbsLast() const;
51 
52 public:
53  typedef TObjArrayIter Iterator_t;
54 
55  TObjArray(Int_t s = TCollection::kInitCapacity, Int_t lowerBound = 0);
56  TObjArray(const TObjArray &a);
57  virtual ~TObjArray();
58  TObjArray& operator=(const TObjArray&);
59  virtual void Clear(Option_t *option="");
60  virtual void Compress();
61  virtual void Delete(Option_t *option="");
62  virtual void Expand(Int_t newSize); // expand or shrink an array
63  Int_t GetEntries() const;
64  Int_t GetEntriesFast() const {
65  return GetAbsLast() + 1; //only OK when no gaps
66  }
67  Int_t GetEntriesUnsafe() const;
68  Int_t GetLast() const;
69  TObject **GetObjectRef() const { return fCont; };
70  TObject **GetObjectRef(const TObject *obj) const;
71  Bool_t IsEmpty() const { return GetAbsLast() == -1; }
72  TIterator *MakeIterator(Bool_t dir = kIterForward) const;
73 
74  void Add(TObject *obj) { AddLast(obj); }
75  virtual void AddFirst(TObject *obj);
76  virtual void AddLast(TObject *obj);
77  virtual void AddAt(TObject *obj, Int_t idx);
78  virtual void AddAtAndExpand(TObject *obj, Int_t idx);
79  virtual Int_t AddAtFree(TObject *obj);
80  virtual void AddAfter(const TObject *after, TObject *obj);
81  virtual void AddBefore(const TObject *before, TObject *obj);
82  virtual TObject *FindObject(const char *name) const;
83  virtual TObject *FindObject(const TObject *obj) const;
84  virtual TObject *RemoveAt(Int_t idx);
85  virtual TObject *Remove(TObject *obj);
86  virtual void RemoveRange(Int_t idx1, Int_t idx2);
87  virtual void RecursiveRemove(TObject *obj);
88 
89  TObject *At(Int_t idx) const;
90  TObject *UncheckedAt(Int_t i) const { return fCont[i-fLowerBound]; }
91  TObject *Before(const TObject *obj) const;
92  TObject *After(const TObject *obj) const;
93  TObject *First() const;
94  TObject *Last() const;
95  virtual TObject *&operator[](Int_t i);
96  virtual TObject *operator[](Int_t i) const;
97  Int_t LowerBound() const { return fLowerBound; }
98  Int_t IndexOf(const TObject *obj) const;
99  void SetLast(Int_t last);
100 
101  virtual void Randomize(Int_t ntimes=1);
102  virtual void Sort(Int_t upto = kMaxInt);
103  virtual Int_t BinarySearch(TObject *obj, Int_t upto = kMaxInt); // the TObjArray has to be sorted, -1 == not found !!
104 
105  ClassDef(TObjArray,3) //An array of objects
106 };
107 
108 
109 // Preventing warnings with -Weffc++ in GCC since it is a false positive for the TObjArrayIter destructor.
110 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
111 #pragma GCC diagnostic push
112 #pragma GCC diagnostic ignored "-Weffc++"
113 #endif
114 
115 //////////////////////////////////////////////////////////////////////////
116 // //
117 // TObjArrayIter //
118 // //
119 // Iterator of object array. //
120 // //
121 //////////////////////////////////////////////////////////////////////////
122 
123 class TObjArrayIter : public TIterator,
124  public std::iterator<std::bidirectional_iterator_tag, // TODO: ideally it should be a randomaccess_iterator_tag
125  TObject*, std::ptrdiff_t,
126  const TObject**, const TObject*&> {
127 
128 private:
129  const TObjArray *fArray; //array being iterated
130  Int_t fCurCursor; //current position in array
131  Int_t fCursor; //next position in array
132  Bool_t fDirection; //iteration direction
133 
134  TObjArrayIter() : fArray(0), fCurCursor(0), fCursor(0), fDirection(kIterForward) { }
135 
136 public:
137  TObjArrayIter(const TObjArray *arr, Bool_t dir = kIterForward);
138  TObjArrayIter(const TObjArrayIter &iter);
139  ~TObjArrayIter() { }
140  TIterator &operator=(const TIterator &rhs);
141  TObjArrayIter &operator=(const TObjArrayIter &rhs);
142 
143  const TCollection *GetCollection() const { return fArray; }
144  TObject *Next();
145  void Reset();
146  Bool_t operator!=(const TIterator &aIter) const;
147  Bool_t operator!=(const TObjArrayIter &aIter) const;
148  TObject *operator*() const;
149 
150  ClassDef(TObjArrayIter,0) //Object array iterator
151 };
152 
153 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
154 #pragma GCC diagnostic pop
155 #endif
156 
157 //---- inlines -----------------------------------------------------------------
158 
159 inline Bool_t TObjArray::BoundsOk(const char *where, Int_t at) const
160 {
161  return (at < fLowerBound || at-fLowerBound >= fSize)
162  ? OutOfBoundsError(where, at)
163  : kTRUE;
164 }
165 
166 inline TObject *TObjArray::At(Int_t i) const
167 {
168  // Return the object at position i. Returns 0 if i is out of bounds.
169  int j = i-fLowerBound;
170  if (j >= 0 && j < fSize) return fCont[j];
171  BoundsOk("At", i);
172  return 0;
173 }
174 
175 #endif