Logo ROOT   6.16/01
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
35class TObjArrayIter;
36
37class TObjArray : public TSeqCollection {
38
39friend class TObjArrayIter;
40friend class TClonesArray;
41
42protected:
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
52public:
54
56 TObjArray(const TObjArray &a);
57 virtual ~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
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
127private:
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
134
135public:
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
158inline 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
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
int Int_t
Definition: RtypesCore.h:41
const Int_t kMaxInt
Definition: RtypesCore.h:99
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kTRUE
Definition: RtypesCore.h:87
const char Option_t
Definition: RtypesCore.h:62
#define ClassDef(name, id)
Definition: Rtypes.h:324
const Bool_t kIterForward
Definition: TCollection.h:40
An array of clone (identical) objects.
Definition: TClonesArray.h:32
Collection abstract base class.
Definition: TCollection.h:63
Iterator abstract base class.
Definition: TIterator.h:30
Iterator of object array.
Definition: TObjArray.h:125
Bool_t operator!=(const TIterator &aIter) const
This operator compares two TIterator objects.
Definition: TObjArray.cxx:953
void Reset()
Reset array iterator.
Definition: TObjArray.cxx:940
Int_t fCursor
Definition: TObjArray.h:130
TIterator & operator=(const TIterator &rhs)
Overridden assignment operator.
Definition: TObjArray.cxx:886
TObject * operator*() const
Return current object or nullptr.
Definition: TObjArray.cxx:973
const TObjArray * fArray
Definition: TObjArray.h:128
TObject * Next()
Return next object in array. Returns 0 when no more objects in array.
Definition: TObjArray.cxx:915
const TCollection * GetCollection() const
Definition: TObjArray.h:142
Int_t fCurCursor
Definition: TObjArray.h:129
Bool_t fDirection
Definition: TObjArray.h:131
An array of TObjects.
Definition: TObjArray.h:37
virtual Int_t AddAtFree(TObject *obj)
Return the position of the new object.
Definition: TObjArray.cxx:268
TObjArray & operator=(const TObjArray &)
Assignment operator. Note, unsets the kIsOwner flag.
Definition: TObjArray.cxx:106
Int_t IndexOf(const TObject *obj) const
Definition: TObjArray.cxx:589
Int_t GetEntriesFast() const
Definition: TObjArray.h:64
TObject ** fCont
Definition: TObjArray.h:43
virtual void Expand(Int_t newSize)
Expand or shrink the array to newSize elements.
Definition: TObjArray.cxx:386
virtual TObject *& operator[](Int_t i)
Return the object at position i.
Definition: TObjArray.cxx:130
TObject ** GetObjectRef() const
Definition: TObjArray.h:68
virtual ~TObjArray()
Delete an array.
Definition: TObjArray.cxx:93
void Init(Int_t s, Int_t lowerBound)
Initialize a TObjArray.
Definition: TObjArray.cxx:611
virtual void Sort(Int_t upto=kMaxInt)
If objects in array are sortable (i.e.
Definition: TObjArray.cxx:802
virtual void AddLast(TObject *obj)
Add object in the next empty slot in the array.
Definition: TObjArray.cxx:177
virtual void AddAtAndExpand(TObject *obj, Int_t idx)
Add object at position idx.
Definition: TObjArray.cxx:234
virtual void AddAfter(const TObject *after, TObject *obj)
Add object in the slot after object after.
Definition: TObjArray.cxx:214
void Add(TObject *obj)
Definition: TObjArray.h:73
TObject * Last() const
Return the object in the last filled slot. Returns 0 if no entries.
Definition: TObjArray.cxx:505
virtual void Compress()
Remove empty slots from array.
Definition: TObjArray.cxx:333
Bool_t BoundsOk(const char *where, Int_t at) const
Definition: TObjArray.h:158
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:522
virtual void Clear(Option_t *option="")
Remove all objects from the array.
Definition: TObjArray.cxx:320
Bool_t OutOfBoundsError(const char *where, Int_t i) const
Generate an out-of-bounds error. Always returns false.
Definition: TObjArray.cxx:642
virtual Int_t BinarySearch(TObject *obj, Int_t upto=kMaxInt)
Find object using a binary search.
Definition: TObjArray.cxx:825
TObject * After(const TObject *obj) const
Return the object after obj. Returns 0 if obj is last object.
Definition: TObjArray.cxx:289
TObject * UncheckedAt(Int_t i) const
Definition: TObjArray.h:89
TObject * First() const
Return the object in the first slot.
Definition: TObjArray.cxx:495
virtual void Delete(Option_t *option="")
Remove all objects from the array AND delete all heap based objects.
Definition: TObjArray.cxx:355
virtual void AddBefore(const TObject *before, TObject *obj)
Add object in the slot before object before.
Definition: TObjArray.cxx:188
virtual TObject * FindObject(const char *name) const
Find an object in this collection using its name.
Definition: TObjArray.cxx:414
Int_t fLowerBound
Array contents.
Definition: TObjArray.h:44
Int_t GetAbsLast() const
Return absolute index to last object in array.
Definition: TObjArray.cxx:538
void SetLast(Int_t last)
Set index of last object in array, effectively truncating the array.
Definition: TObjArray.cxx:759
TIterator * MakeIterator(Bool_t dir=kIterForward) const
Returns an array iterator.
Definition: TObjArray.cxx:633
TObjArrayIter Iterator_t
Definition: TObjArray.h:53
virtual void Randomize(Int_t ntimes=1)
Randomize objects inside the array, i.e.
Definition: TObjArray.cxx:779
virtual void RecursiveRemove(TObject *obj)
Remove object from this collection and recursively remove the object from all other objects (and coll...
Definition: TObjArray.cxx:652
Int_t GetLast() const
Return index of last object in array.
Definition: TObjArray.cxx:561
Bool_t IsEmpty() const
Definition: TObjArray.h:70
TObject * Before(const TObject *obj) const
Return the object before obj. Returns 0 if obj is first object.
Definition: TObjArray.cxx:304
virtual TObject * Remove(TObject *obj)
Remove object from array.
Definition: TObjArray.cxx:703
virtual void AddFirst(TObject *obj)
Add object in the first slot of the array.
Definition: TObjArray.cxx:163
virtual void RemoveRange(Int_t idx1, Int_t idx2)
Remove objects from index idx1 to idx2 included.
Definition: TObjArray.cxx:727
Int_t LowerBound() const
Definition: TObjArray.h:96
TObjArray(Int_t s=TCollection::kInitCapacity, Int_t lowerBound=0)
Create an object array.
Definition: TObjArray.cxx:63
virtual void AddAt(TObject *obj, Int_t idx)
Add object at position ids.
Definition: TObjArray.cxx:253
virtual TObject * RemoveAt(Int_t idx)
Remove object at index idx.
Definition: TObjArray.cxx:678
TObject * At(Int_t idx) const
Definition: TObjArray.h:165
Int_t fLast
Definition: TObjArray.h:45
Mother of all ROOT objects.
Definition: TObject.h:37
Sequenceable collection abstract base class.
static constexpr double s
auto * a
Definition: textangle.C:12