Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
29class TObjArrayIter;
30
31class TObjArray : public TSeqCollection {
32
33friend class TObjArrayIter;
34friend class TClonesArray;
35
36protected:
37 TObject **fCont; //!Array contents
38 Int_t fLowerBound; //Lower bound of the array
39 Int_t fLast; //Last element in array containing an object
40
41 Bool_t BoundsOk(const char *where, Int_t at) const;
42 void Init(Int_t s, Int_t lowerBound);
43 Bool_t OutOfBoundsError(const char *where, Int_t i) const;
44 Int_t GetAbsLast() const;
45
46public:
48
50 TObjArray(const TObjArray &a);
51 virtual ~TObjArray();
53 virtual void Clear(Option_t *option="");
54 virtual void Compress();
55 virtual void Delete(Option_t *option="");
56 virtual void Expand(Int_t newSize); // expand or shrink an array
57 Int_t GetEntries() const;
59 return GetAbsLast() + 1; //only OK when no gaps
60 }
61 Int_t GetEntriesUnsafe() const;
62 Int_t GetLast() const;
63 TObject **GetObjectRef() const { return fCont; };
64 TObject **GetObjectRef(const TObject *obj) const;
65 Bool_t IsEmpty() const { return GetAbsLast() == -1; }
67
68 void Add(TObject *obj) { AddLast(obj); }
69 virtual void AddFirst(TObject *obj);
70 virtual void AddLast(TObject *obj);
71 virtual void AddAt(TObject *obj, Int_t idx);
72 virtual void AddAtAndExpand(TObject *obj, Int_t idx);
73 virtual Int_t AddAtFree(TObject *obj);
74 virtual void AddAfter(const TObject *after, TObject *obj);
75 virtual void AddBefore(const TObject *before, TObject *obj);
76 virtual TObject *FindObject(const char *name) const;
77 virtual TObject *FindObject(const TObject *obj) const;
78 virtual TObject *RemoveAt(Int_t idx);
79 virtual TObject *Remove(TObject *obj);
80 virtual void RemoveRange(Int_t idx1, Int_t idx2);
81 virtual void RecursiveRemove(TObject *obj);
82
83 TObject *At(Int_t idx) const;
84 TObject *UncheckedAt(Int_t i) const { return fCont[i-fLowerBound]; }
85 TObject *Before(const TObject *obj) const;
86 TObject *After(const TObject *obj) const;
87 TObject *First() const;
88 TObject *Last() const;
89 virtual TObject *&operator[](Int_t i);
90 virtual TObject *operator[](Int_t i) const;
91 Int_t LowerBound() const { return fLowerBound; }
92 Int_t IndexOf(const TObject *obj) const;
93 void SetLast(Int_t last);
94
95 virtual void Randomize(Int_t ntimes=1);
96 virtual void Sort(Int_t upto = kMaxInt);
97 virtual Int_t BinarySearch(TObject *obj, Int_t upto = kMaxInt); // the TObjArray has to be sorted, -1 == not found !!
98
99 ClassDef(TObjArray,3) //An array of objects
100};
101
102
103// Preventing warnings with -Weffc++ in GCC since it is a false positive for the TObjArrayIter destructor.
104#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
105#pragma GCC diagnostic push
106#pragma GCC diagnostic ignored "-Weffc++"
107#endif
108
109//////////////////////////////////////////////////////////////////////////
110// //
111// TObjArrayIter //
112// //
113// Iterator of object array. //
114// //
115//////////////////////////////////////////////////////////////////////////
116
117class TObjArrayIter : public TIterator {
118
119private:
120 const TObjArray *fArray; //array being iterated
121 Int_t fCurCursor; //current position in array
122 Int_t fCursor; //next position in array
123 Bool_t fDirection; //iteration direction
124
126
127public:
128 using iterator_category = std::bidirectional_iterator_tag; // TODO: ideally it should be a randomaccess_iterator_tag
130 using difference_type = std::ptrdiff_t;
131 using pointer = TObject **;
132 using const_pointer = const TObject **;
133 using reference = const TObject *&;
134
135 TObjArrayIter(const TObjArray *arr, Bool_t dir = kIterForward);
136 TObjArrayIter(const TObjArrayIter &iter);
138 TIterator &operator=(const TIterator &rhs);
140
141 const TCollection *GetCollection() const { return fArray; }
142 TObject *Next();
143 void Reset();
144 Bool_t operator!=(const TIterator &aIter) const;
145 Bool_t operator!=(const TObjArrayIter &aIter) const;
146 TObject *operator*() const;
147
148 ClassDef(TObjArrayIter,0) //Object array iterator
149};
150
151#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
152#pragma GCC diagnostic pop
153#endif
154
155//---- inlines -----------------------------------------------------------------
156
157inline Bool_t TObjArray::BoundsOk(const char *where, Int_t at) const
158{
159 return (at < fLowerBound || at-fLowerBound >= fSize)
160 ? OutOfBoundsError(where, at)
161 : kTRUE;
162}
163
165{
166 // Return the object at position i. Returns 0 if i is out of bounds.
167 int j = i-fLowerBound;
168 if (j >= 0 && j < fSize) return fCont[j];
169 BoundsOk("At", i);
170 return 0;
171}
172
173#endif
#define a(i)
Definition RSha256.hxx:99
int Int_t
Definition RtypesCore.h:45
const Int_t kMaxInt
Definition RtypesCore.h:112
bool Bool_t
Definition RtypesCore.h:63
const Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassDef(name, id)
Definition Rtypes.h:325
const Bool_t kIterForward
Definition TCollection.h:42
char name[80]
Definition TGX11.cxx:110
An array of clone (identical) objects.
Collection abstract base class.
Definition TCollection.h:65
Iterator abstract base class.
Definition TIterator.h:30
Iterator of object array.
Definition TObjArray.h:117
std::bidirectional_iterator_tag iterator_category
Definition TObjArray.h:128
Bool_t operator!=(const TIterator &aIter) const
This operator compares two TIterator objects.
void Reset()
Reset array iterator.
std::ptrdiff_t difference_type
Definition TObjArray.h:130
TIterator & operator=(const TIterator &rhs)
Overridden assignment operator.
TObject * operator*() const
Return current object or nullptr.
const TObjArray * fArray
Definition TObjArray.h:120
TObject * Next()
Return next object in array. Returns 0 when no more objects in array.
const TCollection * GetCollection() const
Definition TObjArray.h:141
Int_t fCurCursor
Definition TObjArray.h:121
Bool_t fDirection
Definition TObjArray.h:123
An array of TObjects.
Definition TObjArray.h:31
virtual Int_t AddAtFree(TObject *obj)
Return the position of the new object.
TObjArray & operator=(const TObjArray &)
Assignment operator. Note, unsets the kIsOwner flag.
Int_t IndexOf(const TObject *obj) const
Int_t GetEntriesFast() const
Definition TObjArray.h:58
TObject ** fCont
Definition TObjArray.h:37
virtual void Expand(Int_t newSize)
Expand or shrink the array to newSize elements.
Int_t GetEntriesUnsafe() const
Return the number of objects in array (i.e.
virtual TObject *& operator[](Int_t i)
Return the object at position i.
TObject ** GetObjectRef() const
Definition TObjArray.h:63
virtual ~TObjArray()
Delete an array.
Definition TObjArray.cxx:94
void Init(Int_t s, Int_t lowerBound)
Initialize a TObjArray.
virtual void Sort(Int_t upto=kMaxInt)
If objects in array are sortable (i.e.
virtual void AddLast(TObject *obj)
Add object in the next empty slot in the array.
virtual void AddAtAndExpand(TObject *obj, Int_t idx)
Add object at position idx.
virtual void AddAfter(const TObject *after, TObject *obj)
Add object in the slot after object after.
void Add(TObject *obj)
Definition TObjArray.h:68
TObject * Last() const
Return the object in the last filled slot. Returns 0 if no entries.
virtual void Compress()
Remove empty slots from array.
Bool_t BoundsOk(const char *where, Int_t at) const
Definition TObjArray.h:157
Int_t GetEntries() const
Return the number of objects in array (i.e.
virtual void Clear(Option_t *option="")
Remove all objects from the array.
Bool_t OutOfBoundsError(const char *where, Int_t i) const
Generate an out-of-bounds error. Always returns false.
virtual Int_t BinarySearch(TObject *obj, Int_t upto=kMaxInt)
Find object using a binary search.
TObject * After(const TObject *obj) const
Return the object after obj. Returns 0 if obj is last object.
TObject * UncheckedAt(Int_t i) const
Definition TObjArray.h:84
TObject * First() const
Return the object in the first slot.
virtual void Delete(Option_t *option="")
Remove all objects from the array AND delete all heap based objects.
virtual void AddBefore(const TObject *before, TObject *obj)
Add object in the slot before object before.
virtual TObject * FindObject(const char *name) const
Find an object in this collection using its name.
Int_t fLowerBound
Array contents.
Definition TObjArray.h:38
Int_t GetAbsLast() const
Return absolute index to last object in array.
void SetLast(Int_t last)
Set index of last object in array, effectively truncating the array.
TIterator * MakeIterator(Bool_t dir=kIterForward) const
Returns an array iterator.
TObjArrayIter Iterator_t
Definition TObjArray.h:47
virtual void Randomize(Int_t ntimes=1)
Randomize objects inside the array, i.e.
virtual void RecursiveRemove(TObject *obj)
Remove object from this collection and recursively remove the object from all other objects (and coll...
Int_t GetLast() const
Return index of last object in array.
Bool_t IsEmpty() const
Definition TObjArray.h:65
TObject * Before(const TObject *obj) const
Return the object before obj. Returns 0 if obj is first object.
virtual TObject * Remove(TObject *obj)
Remove object from array.
virtual void AddFirst(TObject *obj)
Add object in the first slot of the array.
virtual void RemoveRange(Int_t idx1, Int_t idx2)
Remove objects from index idx1 to idx2 included.
Int_t LowerBound() const
Definition TObjArray.h:91
virtual void AddAt(TObject *obj, Int_t idx)
Add object at position ids.
virtual TObject * RemoveAt(Int_t idx)
Remove object at index idx.
TObject * At(Int_t idx) const
Definition TObjArray.h:164
Int_t fLast
Definition TObjArray.h:39
Mother of all ROOT objects.
Definition TObject.h:41
Sequenceable collection abstract base class.