Logo ROOT   6.10/09
Reference Guide
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:
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;
65  return GetAbsLast() + 1; //only OK when no gaps
66  }
67  Int_t GetLast() const;
68  TObject **GetObjectRef() const { return fCont; };
69  TObject **GetObjectRef(const TObject *obj) const;
70  Bool_t IsEmpty() const { return GetAbsLast() == -1; }
72 
73  void Add(TObject *obj) { AddLast(obj); }
74  virtual void AddFirst(TObject *obj);
75  virtual void AddLast(TObject *obj);
76  virtual void AddAt(TObject *obj, Int_t idx);
77  virtual void AddAtAndExpand(TObject *obj, Int_t idx);
78  virtual Int_t AddAtFree(TObject *obj);
79  virtual void AddAfter(const TObject *after, TObject *obj);
80  virtual void AddBefore(const TObject *before, TObject *obj);
81  virtual TObject *FindObject(const char *name) const;
82  virtual TObject *FindObject(const TObject *obj) const;
83  virtual TObject *RemoveAt(Int_t idx);
84  virtual TObject *Remove(TObject *obj);
85  virtual void RemoveRange(Int_t idx1, Int_t idx2);
86  virtual void RecursiveRemove(TObject *obj);
87 
88  TObject *At(Int_t idx) const;
89  TObject *UncheckedAt(Int_t i) const { return fCont[i-fLowerBound]; }
90  TObject *Before(const TObject *obj) const;
91  TObject *After(const TObject *obj) const;
92  TObject *First() const;
93  TObject *Last() const;
94  virtual TObject *&operator[](Int_t i);
95  virtual TObject *operator[](Int_t i) const;
96  Int_t LowerBound() const { return fLowerBound; }
97  Int_t IndexOf(const TObject *obj) const;
98  void SetLast(Int_t last);
99 
100  virtual void Randomize(Int_t ntimes=1);
101  virtual void Sort(Int_t upto = kMaxInt);
102  virtual Int_t BinarySearch(TObject *obj, Int_t upto = kMaxInt); // the TObjArray has to be sorted, -1 == not found !!
103 
104  ClassDef(TObjArray,3) //An array of objects
105 };
106 
107 
108 // Preventing warnings with -Weffc++ in GCC since it is a false positive for the TObjArrayIter destructor.
109 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
110 #pragma GCC diagnostic push
111 #pragma GCC diagnostic ignored "-Weffc++"
112 #endif
113 
114 //////////////////////////////////////////////////////////////////////////
115 // //
116 // TObjArrayIter //
117 // //
118 // Iterator of object array. //
119 // //
120 //////////////////////////////////////////////////////////////////////////
121 
122 class TObjArrayIter : public TIterator,
123  public std::iterator<std::bidirectional_iterator_tag, // TODO: ideally it should be a randomaccess_iterator_tag
124  TObject*, std::ptrdiff_t,
125  const TObject**, const TObject*&> {
126 
127 private:
128  const TObjArray *fArray; //array being iterated
129  Int_t fCurCursor; //current position in array
130  Int_t fCursor; //next position in array
131  Bool_t fDirection; //iteration direction
132 
133  TObjArrayIter() : fArray(0), fCurCursor(0), fCursor(0), fDirection(kIterForward) { }
134 
135 public:
136  TObjArrayIter(const TObjArray *arr, Bool_t dir = kIterForward);
137  TObjArrayIter(const TObjArrayIter &iter);
139  TIterator &operator=(const TIterator &rhs);
141 
142  const TCollection *GetCollection() const { return fArray; }
143  TObject *Next();
144  void Reset();
145  Bool_t operator!=(const TIterator &aIter) const;
146  Bool_t operator!=(const TObjArrayIter &aIter) const;
147  TObject *operator*() const;
148 
149  ClassDef(TObjArrayIter,0) //Object array iterator
150 };
151 
152 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
153 #pragma GCC diagnostic pop
154 #endif
155 
156 //---- inlines -----------------------------------------------------------------
157 
158 inline Bool_t TObjArray::BoundsOk(const char *where, Int_t at) const
159 {
160  return (at < fLowerBound || at-fLowerBound >= fSize)
161  ? OutOfBoundsError(where, at)
162  : kTRUE;
163 }
164 
165 inline TObject *TObjArray::At(Int_t i) const
166 {
167  // Return the object at position i. Returns 0 if i is out of bounds.
168  int j = i-fLowerBound;
169  if (j >= 0 && j < fSize) return fCont[j];
170  BoundsOk("At", i);
171  return 0;
172 }
173 
174 #endif
TObjArray(Int_t s=TCollection::kInitCapacity, Int_t lowerBound=0)
Create an object array.
Definition: TObjArray.cxx:63
An array of TObjects.
Definition: TObjArray.h:37
virtual Int_t AddAtFree(TObject *obj)
Return the position of the new object.
Definition: TObjArray.cxx:252
virtual void Sort(Int_t upto=kMaxInt)
If objects in array are sortable (i.e.
Definition: TObjArray.cxx:744
virtual void Clear(Option_t *option="")
Remove all objects from the array.
Definition: TObjArray.cxx:298
const char Option_t
Definition: RtypesCore.h:62
virtual void Delete(Option_t *option="")
Remove all objects from the array AND delete all heap based objects.
Definition: TObjArray.cxx:329
virtual TObject * Remove(TObject *obj)
Remove object from array.
Definition: TObjArray.cxx:653
virtual void Randomize(Int_t ntimes=1)
Randomize objects inside the array, i.e.
Definition: TObjArray.cxx:723
Bool_t IsEmpty() const
Definition: TObjArray.h:70
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TArc * a
Definition: textangle.C:12
Iterator of object array.
Definition: TObjArray.h:122
virtual void AddFirst(TObject *obj)
Add object in the first slot of the array.
Definition: TObjArray.cxx:157
const TObjArray * fArray
Definition: TObjArray.h:128
TObject * At(Int_t idx) const
Definition: TObjArray.h:165
TObject ** GetObjectRef() const
Definition: TObjArray.h:68
Iterator abstract base class.
Definition: TIterator.h:30
virtual void RemoveRange(Int_t idx1, Int_t idx2)
Remove objects from index idx1 to idx2 included.
Definition: TObjArray.cxx:675
virtual TObject *& operator[](Int_t i)
Return the object at position i.
Definition: TObjArray.cxx:128
TObject * Last() const
Return the object in the last filled slot. Returns 0 if no entries.
Definition: TObjArray.cxx:479
Bool_t fDirection
Definition: TObjArray.h:131
Sequenceable collection abstract base class.
#define ClassDef(name, id)
Definition: Rtypes.h:297
void SetLast(Int_t last)
Set index of last object in array, effectively truncating the array.
Definition: TObjArray.cxx:705
virtual TObject * FindObject(const char *name) const
Find an object in this collection using its name.
Definition: TObjArray.cxx:396
virtual void AddAfter(const TObject *after, TObject *obj)
Add object in the slot after object after.
Definition: TObjArray.cxx:204
Bool_t OutOfBoundsError(const char *where, Int_t i) const
Generate an out-of-bounds error. Always returns false.
Definition: TObjArray.cxx:599
virtual ~TObjArray()
Delete an array.
Definition: TObjArray.cxx:93
TObjArray & operator=(const TObjArray &)
Assignment operator. Note, unsets the kIsOwner flag.
Definition: TObjArray.cxx:106
const Bool_t kIterForward
Definition: TCollection.h:37
virtual Int_t BinarySearch(TObject *obj, Int_t upto=kMaxInt)
Find object using a binary search.
Definition: TObjArray.cxx:765
Bool_t operator!=(const TDatime &d1, const TDatime &d2)
Definition: TDatime.h:104
TObject * First() const
Return the object in the first slot.
Definition: TObjArray.cxx:471
Int_t GetLast() const
Return index of last object in array.
Definition: TObjArray.cxx:528
friend class TObjArrayIter
Definition: TObjArray.h:39
Int_t fLast
Definition: TObjArray.h:45
virtual void AddAtAndExpand(TObject *obj, Int_t idx)
Add object at position idx.
Definition: TObjArray.cxx:222
virtual TObject * RemoveAt(Int_t idx)
Remove object at index idx.
Definition: TObjArray.cxx:630
TTime operator*(const TTime &t1, const TTime &t2)
Definition: TTime.h:85
Collection abstract base class.
Definition: TCollection.h:42
Int_t GetEntriesFast() const
Definition: TObjArray.h:64
Int_t fCurCursor
Definition: TObjArray.h:129
Int_t fSize
Definition: TCollection.h:57
void Init(Int_t s, Int_t lowerBound)
Initialize a TObjArray.
Definition: TObjArray.cxx:571
void Reset(Detail::TBranchProxy *x)
TObject ** fCont
Definition: TObjArray.h:43
virtual void AddAt(TObject *obj, Int_t idx)
Add object at position ids.
Definition: TObjArray.cxx:239
Int_t fLowerBound
Array contents.
Definition: TObjArray.h:44
Int_t fCursor
Definition: TObjArray.h:130
Bool_t BoundsOk(const char *where, Int_t at) const
Definition: TObjArray.h:158
Int_t GetAbsLast() const
Return absolute index to last object in array.
Definition: TObjArray.cxx:508
TIterator * MakeIterator(Bool_t dir=kIterForward) const
Returns an array iterator.
Definition: TObjArray.cxx:591
TObject * UncheckedAt(Int_t i) const
Definition: TObjArray.h:89
Int_t IndexOf(const TObject *obj) const
Definition: TObjArray.cxx:552
TObjArrayIter Iterator_t
Definition: TObjArray.h:53
Int_t LowerBound() const
Definition: TObjArray.h:96
virtual void Expand(Int_t newSize)
Expand or shrink the array to newSize elements.
Definition: TObjArray.cxx:370
TObject * Before(const TObject *obj) const
Return the object before obj. Returns 0 if obj is first object.
Definition: TObjArray.cxx:284
Mother of all ROOT objects.
Definition: TObject.h:37
const TCollection * GetCollection() const
Definition: TObjArray.h:142
An array of clone (identical) objects.
Definition: TClonesArray.h:32
virtual void RecursiveRemove(TObject *obj)
Remove object from this collection and recursively remove the object from all other objects (and coll...
Definition: TObjArray.cxx:609
const Int_t kMaxInt
Definition: RtypesCore.h:103
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:494
TObject * After(const TObject *obj) const
Return the object after obj. Returns 0 if obj is last object.
Definition: TObjArray.cxx:271
virtual void AddLast(TObject *obj)
Add object in the next empty slot in the array.
Definition: TObjArray.cxx:169
void Add(TObject *obj)
Definition: TObjArray.h:73
virtual void Compress()
Remove empty slots from array.
Definition: TObjArray.cxx:309
const Bool_t kTRUE
Definition: RtypesCore.h:91
virtual void AddBefore(const TObject *before, TObject *obj)
Add object in the slot before object before.
Definition: TObjArray.cxx:180