Logo ROOT  
Reference Guide
Go to the documentation of this file.
1 // @(#)root/cont:$Id$
2 // Author: Fons Rademakers 11/09/95
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  *************************************************************************/
12 #ifndef ROOT_TObjArray
13 #define ROOT_TObjArray
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 //////////////////////////////////////////////////////////////////////////
25 #include "TSeqCollection.h"
27 #include <iterator>
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
35 class TObjArrayIter;
37 class TObjArray : public TSeqCollection {
39 friend class TObjArrayIter;
40 friend class TClonesArray;
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
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;
52 public:
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 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; }
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);
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);
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 !!
105  ClassDef(TObjArray,3) //An array of objects
106 };
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
115 //////////////////////////////////////////////////////////////////////////
116 // //
117 // TObjArrayIter //
118 // //
119 // Iterator of object array. //
120 // //
121 //////////////////////////////////////////////////////////////////////////
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*&> {
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
136 public:
137  TObjArrayIter(const TObjArray *arr, Bool_t dir = kIterForward);
138  TObjArrayIter(const TObjArrayIter &iter);
140  TIterator &operator=(const TIterator &rhs);
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;
150  ClassDef(TObjArrayIter,0) //Object array iterator
151 };
153 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
154 #pragma GCC diagnostic pop
155 #endif
157 //---- inlines -----------------------------------------------------------------
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 }
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 }
175 #endif
TObjArray(Int_t s=TCollection::kInitCapacity, Int_t lowerBound=0)
Create an object array.
Definition: TObjArray.cxx:64
An array of TObjects.
Definition: TObjArray.h:37
virtual Int_t AddAtFree(TObject *obj)
Return the position of the new object.
Definition: TObjArray.cxx:269
virtual void Sort(Int_t upto=kMaxInt)
If objects in array are sortable (i.e.
Definition: TObjArray.cxx:818
Bool_t operator!=(const TIterator &aIter) const
This operator compares two TIterator objects.
Definition: TObjArray.cxx:969
virtual void Clear(Option_t *option="")
Remove all objects from the array.
Definition: TObjArray.cxx:321
const char Option_t
Definition: RtypesCore.h:64
virtual void Delete(Option_t *option="")
Remove all objects from the array AND delete all heap based objects.
Definition: TObjArray.cxx:356
virtual TObject * Remove(TObject *obj)
Remove object from array.
Definition: TObjArray.cxx:719
virtual void Randomize(Int_t ntimes=1)
Randomize objects inside the array, i.e.
Definition: TObjArray.cxx:795
Bool_t IsEmpty() const
Definition: TObjArray.h:71
int Int_t
Definition: RtypesCore.h:43
bool Bool_t
Definition: RtypesCore.h:61
Iterator of object array.
Definition: TObjArray.h:123
virtual void AddFirst(TObject *obj)
Add object in the first slot of the array.
Definition: TObjArray.cxx:164
const TObjArray * fArray
Definition: TObjArray.h:129
TObject * At(Int_t idx) const
Definition: TObjArray.h:166
TObject ** GetObjectRef() const
Definition: TObjArray.h:69
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:743
virtual TObject *& operator[](Int_t i)
Return the object at position i.
Definition: TObjArray.cxx:131
TObject * Last() const
Return the object in the last filled slot. Returns 0 if no entries.
Definition: TObjArray.cxx:506
Bool_t fDirection
Definition: TObjArray.h:132
Sequenceable collection abstract base class.
#define ClassDef(name, id)
Definition: Rtypes.h:322
void SetLast(Int_t last)
Set index of last object in array, effectively truncating the array.
Definition: TObjArray.cxx:775
virtual TObject * FindObject(const char *name) const
Find an object in this collection using its name.
Definition: TObjArray.cxx:415
virtual void AddAfter(const TObject *after, TObject *obj)
Add object in the slot after object after.
Definition: TObjArray.cxx:215
Bool_t OutOfBoundsError(const char *where, Int_t i) const
Generate an out-of-bounds error. Always returns false.
Definition: TObjArray.cxx:658
virtual ~TObjArray()
Delete an array.
Definition: TObjArray.cxx:94
TObjArray & operator=(const TObjArray &)
Assignment operator. Note, unsets the kIsOwner flag.
Definition: TObjArray.cxx:107
TObject * Next()
Return next object in array. Returns 0 when no more objects in array.
Definition: TObjArray.cxx:931
void Reset()
Reset array iterator.
Definition: TObjArray.cxx:956
const Bool_t kIterForward
Definition: TCollection.h:40
virtual Int_t BinarySearch(TObject *obj, Int_t upto=kMaxInt)
Find object using a binary search.
Definition: TObjArray.cxx:841
static constexpr double s
TObject * First() const
Return the object in the first slot.
Definition: TObjArray.cxx:496
Int_t GetLast() const
Return index of last object in array.
Definition: TObjArray.cxx:577
Int_t fLast
Definition: TObjArray.h:45
virtual void AddAtAndExpand(TObject *obj, Int_t idx)
Add object at position idx.
Definition: TObjArray.cxx:235
auto * a
Definition: textangle.C:12
virtual TObject * RemoveAt(Int_t idx)
Remove object at index idx.
Definition: TObjArray.cxx:694
Collection abstract base class.
Definition: TCollection.h:63
Int_t GetEntriesFast() const
Definition: TObjArray.h:64
Int_t fCurCursor
Definition: TObjArray.h:130
void Init(Int_t s, Int_t lowerBound)
Initialize a TObjArray.
Definition: TObjArray.cxx:627
TObject ** fCont
Definition: TObjArray.h:43
virtual void AddAt(TObject *obj, Int_t idx)
Add object at position ids.
Definition: TObjArray.cxx:254
Int_t fLowerBound
Array contents.
Definition: TObjArray.h:44
Int_t fCursor
Definition: TObjArray.h:131
Bool_t BoundsOk(const char *where, Int_t at) const
Definition: TObjArray.h:159
Int_t GetAbsLast() const
Return absolute index to last object in array.
Definition: TObjArray.cxx:539
TIterator * MakeIterator(Bool_t dir=kIterForward) const
Returns an array iterator.
Definition: TObjArray.cxx:649
TObject * UncheckedAt(Int_t i) const
Definition: TObjArray.h:90
TIterator & operator=(const TIterator &rhs)
Overridden assignment operator.
Definition: TObjArray.cxx:902
Int_t IndexOf(const TObject *obj) const
Definition: TObjArray.cxx:605
TObjArrayIter Iterator_t
Definition: TObjArray.h:53
Int_t LowerBound() const
Definition: TObjArray.h:97
virtual void Expand(Int_t newSize)
Expand or shrink the array to newSize elements.
Definition: TObjArray.cxx:387
TObject * Before(const TObject *obj) const
Return the object before obj. Returns 0 if obj is first object.
Definition: TObjArray.cxx:305
Mother of all ROOT objects.
Definition: TObject.h:37
const TCollection * GetCollection() const
Definition: TObjArray.h:143
An array of clone (identical) objects.
Definition: TClonesArray.h:32
TObject * operator*() const
Return current object or nullptr.
Definition: TObjArray.cxx:989
virtual void RecursiveRemove(TObject *obj)
Remove object from this collection and recursively remove the object from all other objects (and coll...
Definition: TObjArray.cxx:668
const Int_t kMaxInt
Definition: RtypesCore.h:101
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:523
TObject * After(const TObject *obj) const
Return the object after obj. Returns 0 if obj is last object.
Definition: TObjArray.cxx:290
virtual void AddLast(TObject *obj)
Add object in the next empty slot in the array.
Definition: TObjArray.cxx:178
void Add(TObject *obj)
Definition: TObjArray.h:74
Int_t GetEntriesUnsafe() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:565
virtual void Compress()
Remove empty slots from array.
Definition: TObjArray.cxx:334
const Bool_t kTRUE
Definition: RtypesCore.h:89
char name[80]
Definition: TGX11.cxx:109
virtual void AddBefore(const TObject *before, TObject *obj)
Add object in the slot before object before.
Definition: TObjArray.cxx:189