Logo ROOT  
Reference Guide
No Matches
Go to the documentation of this file.
1// @(#)root/cont:$Id$
2// Author: Fons Rademakers 11/09/95
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
17// //
18// TObjArray //
19// //
20// An array of TObjects. The array expands automatically when adding //
21// elements (shrinking can be done by hand). //
22// //
25#include "TSeqCollection.h"
27#include <iterator>
29class TObjArrayIter;
31class TObjArray : public TSeqCollection {
33friend class TObjArrayIter;
34friend class TClonesArray;
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
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;
50 TObjArray(const TObjArray &a);
51 virtual ~TObjArray();
53 void Clear(Option_t *option="") override;
54 virtual void Compress();
55 void Delete(Option_t *option="") override;
56 virtual void Expand(Int_t newSize); // expand or shrink an array
57 Int_t GetEntries() const override;
59 return GetAbsLast() + 1; //only OK when no gaps
60 }
61 Int_t GetEntriesUnsafe() const;
62 Int_t GetLast() const override;
63 TObject **GetObjectRef() const { return fCont; };
64 TObject **GetObjectRef(const TObject *obj) const override;
65 Bool_t IsEmpty() const override { return GetAbsLast() == -1; }
66 TIterator *MakeIterator(Bool_t dir = kIterForward) const override;
68 void Add(TObject *obj) override { AddLast(obj); }
69 void AddFirst(TObject *obj) override;
70 void AddLast(TObject *obj) override;
71 void AddAt(TObject *obj, Int_t idx) override;
72 virtual void AddAtAndExpand(TObject *obj, Int_t idx);
73 virtual Int_t AddAtFree(TObject *obj);
74 void AddAfter(const TObject *after, TObject *obj) override;
75 void AddBefore(const TObject *before, TObject *obj) override;
76 TObject *FindObject(const char *name) const override;
77 TObject *FindObject(const TObject *obj) const override;
78 TObject *RemoveAt(Int_t idx) override;
79 TObject *Remove(TObject *obj) override;
80 virtual void RemoveRange(Int_t idx1, Int_t idx2);
81 void RecursiveRemove(TObject *obj) override;
83 TObject *At(Int_t idx) const override;
84 TObject *UncheckedAt(Int_t i) const { return fCont[i-fLowerBound]; }
85 TObject *Before(const TObject *obj) const override;
86 TObject *After(const TObject *obj) const override;
87 TObject *First() const override;
88 TObject *Last() const override;
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 override;
93 void SetLast(Int_t last);
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 !!
99 ClassDefOverride(TObjArray,3) //An array of objects
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++"
110// //
111// TObjArrayIter //
112// //
113// Iterator of object array. //
114// //
117class TObjArrayIter : public TIterator {
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
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 *&;
135 TObjArrayIter(const TObjArray *arr, Bool_t dir = kIterForward);
136 TObjArrayIter(const TObjArrayIter &iter);
138 TIterator &operator=(const TIterator &rhs) override;
141 const TCollection *GetCollection() const override { return fArray; }
142 TObject *Next() override;
143 void Reset() override;
144 Bool_t operator!=(const TIterator &aIter) const override;
145 Bool_t operator!=(const TObjArrayIter &aIter) const;
146 TObject *operator*() const override;
148 ClassDefOverride(TObjArrayIter,0) //Object array iterator
151#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
152#pragma GCC diagnostic pop
155//---- inlines -----------------------------------------------------------------
157inline Bool_t TObjArray::BoundsOk(const char *where, Int_t at) const
159 return (at < fLowerBound || at-fLowerBound >= fSize)
160 ? OutOfBoundsError(where, at)
161 : kTRUE;
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 nullptr;
#define a(i)
Definition RSha256.hxx:99
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
constexpr Int_t kMaxInt
Definition RtypesCore.h:112
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassDefOverride(name, id)
Definition Rtypes.h:341
const Bool_t kIterForward
Definition TCollection.h:42
Option_t Option_t option
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
std::ptrdiff_t difference_type
Definition TObjArray.h:130
TIterator & operator=(const TIterator &rhs) override
Overridden assignment operator.
const TObjArray * fArray
Definition TObjArray.h:120
Bool_t operator!=(const TIterator &aIter) const override
This operator compares two TIterator objects.
TObject * operator*() const override
Return current object or nullptr.
TObject * Next() override
Return next object in array. Returns 0 when no more objects in array.
const TCollection * GetCollection() const override
Definition TObjArray.h:141
Int_t fCurCursor
Definition TObjArray.h:121
Bool_t fDirection
Definition TObjArray.h:123
void Reset() override
Reset array iterator.
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 GetEntriesFast() const
Definition TObjArray.h:58
TObject ** fCont
Definition TObjArray.h:37
Int_t IndexOf(const TObject *obj) const override
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 AddAt(TObject *obj, Int_t idx) override
Add object at position ids.
TObject * Last() const override
Return the object in the last filled slot. Returns 0 if no entries.
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.
void Clear(Option_t *option="") override
Remove all objects from the array.
virtual void AddAtAndExpand(TObject *obj, Int_t idx)
Add object at position idx.
void AddAfter(const TObject *after, TObject *obj) override
Add object in the slot after object after.
TIterator * MakeIterator(Bool_t dir=kIterForward) const override
Returns an array iterator.
virtual void Compress()
Remove empty slots from array.
Int_t GetEntries() const override
Return the number of objects in array (i.e.
Bool_t BoundsOk(const char *where, Int_t at) const
Definition TObjArray.h:157
void Delete(Option_t *option="") override
Remove all objects from the array AND delete all heap based objects.
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 * At(Int_t idx) const override
Definition TObjArray.h:164
void AddLast(TObject *obj) override
Add object in the next empty slot in the array.
TObject * UncheckedAt(Int_t i) const
Definition TObjArray.h:84
TObject * First() const override
Return the object in the first slot.
Bool_t IsEmpty() const override
Definition TObjArray.h:65
Int_t fLowerBound
Array contents.
Definition TObjArray.h:38
TObject * Remove(TObject *obj) override
Remove object from array.
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.
TObject * RemoveAt(Int_t idx) override
Remove object at index idx.
TObjArrayIter Iterator_t
Definition TObjArray.h:47
TObject * FindObject(const char *name) const override
Find an object in this collection using its name.
virtual void Randomize(Int_t ntimes=1)
Randomize objects inside the array, i.e.
TObject * After(const TObject *obj) const override
Return the object after obj. Returns 0 if obj is last object.
TObject * Before(const TObject *obj) const override
Return the object before obj. Returns 0 if obj is first object.
virtual void RemoveRange(Int_t idx1, Int_t idx2)
Remove objects from index idx1 to idx2 included.
Int_t GetLast() const override
Return index of last object in array.
Int_t LowerBound() const
Definition TObjArray.h:91
void AddFirst(TObject *obj) override
Add object in the first slot of the array.
void RecursiveRemove(TObject *obj) override
Remove object from this collection and recursively remove the object from all other objects (and coll...
void AddBefore(const TObject *before, TObject *obj) override
Add object in the slot before object before.
void Add(TObject *obj) override
Definition TObjArray.h:68
Int_t fLast
Definition TObjArray.h:39
Mother of all ROOT objects.
Definition TObject.h:41
Sequenceable collection abstract base class.