Logo ROOT   6.18/05
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 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; }
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
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
128private:
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
135
136public:
137 TObjArrayIter(const TObjArray *arr, Bool_t dir = kIterForward);
138 TObjArrayIter(const TObjArrayIter &iter);
140 TIterator &operator=(const TIterator &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
159inline 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
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
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:326
const Bool_t kIterForward
Definition: TCollection.h:40
char name[80]
Definition: TGX11.cxx:109
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:126
Bool_t operator!=(const TIterator &aIter) const
This operator compares two TIterator objects.
Definition: TObjArray.cxx:968
void Reset()
Reset array iterator.
Definition: TObjArray.cxx:955
Int_t fCursor
Definition: TObjArray.h:131
TIterator & operator=(const TIterator &rhs)
Overridden assignment operator.
Definition: TObjArray.cxx:901
TObject * operator*() const
Return current object or nullptr.
Definition: TObjArray.cxx:988
const TObjArray * fArray
Definition: TObjArray.h:129
TObject * Next()
Return next object in array. Returns 0 when no more objects in array.
Definition: TObjArray.cxx:930
const TCollection * GetCollection() const
Definition: TObjArray.h:143
Int_t fCurCursor
Definition: TObjArray.h:130
Bool_t fDirection
Definition: TObjArray.h:132
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:604
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
Int_t GetEntriesUnsafe() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:564
virtual TObject *& operator[](Int_t i)
Return the object at position i.
Definition: TObjArray.cxx:130
TObject ** GetObjectRef() const
Definition: TObjArray.h:69
virtual ~TObjArray()
Delete an array.
Definition: TObjArray.cxx:93
void Init(Int_t s, Int_t lowerBound)
Initialize a TObjArray.
Definition: TObjArray.cxx:626
virtual void Sort(Int_t upto=kMaxInt)
If objects in array are sortable (i.e.
Definition: TObjArray.cxx:817
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:74
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:159
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:657
virtual Int_t BinarySearch(TObject *obj, Int_t upto=kMaxInt)
Find object using a binary search.
Definition: TObjArray.cxx:840
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:90
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:774
TIterator * MakeIterator(Bool_t dir=kIterForward) const
Returns an array iterator.
Definition: TObjArray.cxx:648
TObjArrayIter Iterator_t
Definition: TObjArray.h:53
virtual void Randomize(Int_t ntimes=1)
Randomize objects inside the array, i.e.
Definition: TObjArray.cxx:794
virtual void RecursiveRemove(TObject *obj)
Remove object from this collection and recursively remove the object from all other objects (and coll...
Definition: TObjArray.cxx:667
Int_t GetLast() const
Return index of last object in array.
Definition: TObjArray.cxx:576
Bool_t IsEmpty() const
Definition: TObjArray.h:71
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:718
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:742
Int_t LowerBound() const
Definition: TObjArray.h:97
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:693
TObject * At(Int_t idx) const
Definition: TObjArray.h:166
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