Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TRefArray.h
Go to the documentation of this file.
1// @(#)root/cont:$Id$
2// Author: Rene Brun 02/10/2001
3
4/*************************************************************************
5 * Copyright (C) 1995-2001, 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_TRefArray
13#define ROOT_TRefArray
14
15
16//////////////////////////////////////////////////////////////////////////
17// //
18// TRefArray //
19// //
20// An array of references to TObjects. //
21// The array expands automatically when adding elements. //
22// //
23//////////////////////////////////////////////////////////////////////////
24
25#include "TSeqCollection.h"
26#include "TProcessID.h"
27
28#include <iterator>
29
30class TSystem;
31class TRefArrayIter;
32
33class TRefArray : public TSeqCollection {
34
35friend class TRefArrayIter;
36
37protected:
38 TProcessID *fPID; //Pointer to Process Unique Identifier
39 UInt_t *fUIDs; //[fSize] To store uids of referenced objects
40 Int_t fLowerBound; //Lower bound of the array
41 Int_t fLast; //Last element in array containing an object
42
43 Bool_t BoundsOk(const char *where, Int_t at) const;
44 void Init(Int_t s, Int_t lowerBound);
45 Bool_t OutOfBoundsError(const char *where, Int_t i) const;
46 Int_t GetAbsLast() const;
47 TObject *GetFromTable(Int_t idx) const;
48 Bool_t GetObjectUID(Int_t &uid, TObject *obj, const char *methodname);
49
50public:
52
53 TRefArray(TProcessID *pid = nullptr);
54 TRefArray(Int_t s, TProcessID *pid);
55 TRefArray(Int_t s, Int_t lowerBound = 0, TProcessID *pid = nullptr);
56 TRefArray(const TRefArray &a);
58 virtual ~TRefArray();
59 void Clear(Option_t *option="") override;
60 virtual void Compress();
61 void Delete(Option_t *option="") override;
62 virtual void Expand(Int_t newSize); // expand or shrink an array
63 Int_t GetEntries() const override;
65 return GetAbsLast() + 1; //only OK when no gaps
66 }
67 Int_t GetLast() const override;
68 TObject **GetObjectRef(const TObject *obj) const override;
69 TProcessID *GetPID() const {return fPID;}
70 UInt_t GetUID(Int_t at) const;
71 Bool_t IsEmpty() const override { return GetAbsLast() == -1; }
72 TIterator *MakeIterator(Bool_t dir = kIterForward) const override;
73
74 void Add(TObject *obj) override { AddLast(obj); }
75 void AddFirst(TObject *obj) override;
76 void AddLast(TObject *obj) override;
77 void AddAt(TObject *obj, Int_t idx) override;
78 virtual void AddAtAndExpand(TObject *obj, Int_t idx);
79 virtual Int_t AddAtFree(TObject *obj);
80 void AddAfter(const TObject *after, TObject *obj) override;
81 void AddBefore(const TObject *before, TObject *obj) override;
82 TObject *RemoveAt(Int_t idx) override;
83 TObject *Remove(TObject *obj) override;
84
85 TObject *At(Int_t idx) const override;
86 TObject *Before(const TObject *obj) const override;
87 TObject *After(const TObject *obj) const override;
88 TObject *First() const override;
89 TObject *Last() const override;
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);
94
95 virtual void Sort(Int_t upto = kMaxInt);
96 virtual Int_t BinarySearch(TObject *obj, Int_t upto = kMaxInt); // the TRefArray has to be sorted, -1 == not found !!
97
98 ClassDefOverride(TRefArray,1) //An array of references to TObjects
99};
100
101
102// Preventing warnings with -Weffc++ in GCC since it is a false positive for the TRefArrayIter destructor.
103#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
104#pragma GCC diagnostic push
105#pragma GCC diagnostic ignored "-Weffc++"
106#endif
107
108//////////////////////////////////////////////////////////////////////////
109// //
110// TRefArrayIter //
111// //
112// Iterator of object array. //
113// //
114//////////////////////////////////////////////////////////////////////////
115
116class TRefArrayIter : public TIterator {
117
118private:
119 const TRefArray *fArray; //array being iterated
120 Int_t fCurCursor; //current position in array
121 Int_t fCursor; //next position in array
122 Bool_t fDirection; //iteration direction
123
125
126public:
127 using iterator_category = std::bidirectional_iterator_tag; // TODO: ideally it should be a randomaccess_iterator_tag
129 using difference_type = std::ptrdiff_t;
130 using pointer = TObject **;
131 using const_pointer = const TObject **;
132 using reference = const TObject *&;
133
134 TRefArrayIter(const TRefArray *arr, Bool_t dir = kIterForward);
135 TRefArrayIter(const TRefArrayIter &iter);
137 TIterator &operator=(const TIterator &rhs) override;
139
140 const TCollection *GetCollection() const override { return fArray; }
141 TObject *Next() override;
142 void Reset() override;
143 Bool_t operator!=(const TIterator &aIter) const override;
144 Bool_t operator!=(const TRefArrayIter &aIter) const;
145 TObject *operator*() const override;
146
147 ClassDefOverride(TRefArrayIter,0) //Object array iterator
148};
149
150#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
151#pragma GCC diagnostic pop
152#endif
153
154
155//---- inlines -----------------------------------------------------------------
156
157inline Bool_t TRefArray::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 int j = at-fLowerBound;
167 if (j >= 0 && j < fSize) {
168 if (!fPID) return nullptr;
169 if (!TProcessID::IsValid(fPID)) return nullptr;
170 TObject *obj = fPID->GetObjectWithID(fUIDs[j]);
171 if (!obj) obj = GetFromTable(j);
172 return obj;
173 }
174 BoundsOk("At", at);
175 return nullptr;
176}
177
178inline TObject *TRefArray::At(Int_t at) const
179{
180 // Return the object at position i. Returns 0 if i is out of bounds.
181 int j = at-fLowerBound;
182 if (j >= 0 && j < fSize) {
183 if (!fPID) return nullptr;
184 if (!TProcessID::IsValid(fPID)) return nullptr;
185 TObject *obj = fPID->GetObjectWithID(fUIDs[j]);
186 if (!obj) obj = GetFromTable(j);
187 return obj;
188 }
189 BoundsOk("At", at);
190 return nullptr;
191}
192
193#endif
#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:105
unsigned int UInt_t
Definition RtypesCore.h:46
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
const char Option_t
Definition RtypesCore.h:66
#define ClassDefOverride(name, id)
Definition Rtypes.h:346
const Bool_t kIterForward
Definition TCollection.h:42
Option_t Option_t option
Collection abstract base class.
Definition TCollection.h:65
Iterator abstract base class.
Definition TIterator.h:30
Mother of all ROOT objects.
Definition TObject.h:41
A TProcessID identifies a ROOT job in a unique way in time and space.
Definition TProcessID.h:74
static Bool_t IsValid(TProcessID *pid)
static function. return kTRUE if pid is a valid TProcessID
TObject * GetObjectWithID(UInt_t uid)
returns the TObject with unique identifier uid in the table of objects
Iterator of object array.
Definition TRefArray.h:116
TObject * Next() override
Return next object in array. Returns 0 when no more objects in array.
Bool_t fDirection
Definition TRefArray.h:122
void Reset() override
Reset array iterator.
const TRefArray * fArray
Definition TRefArray.h:119
const TCollection * GetCollection() const override
Definition TRefArray.h:140
std::ptrdiff_t difference_type
Definition TRefArray.h:129
TIterator & operator=(const TIterator &rhs) override
Overridden assignment operator.
Bool_t operator!=(const TIterator &aIter) const override
This operator compares two TIterator objects.
std::bidirectional_iterator_tag iterator_category
Definition TRefArray.h:127
Int_t fCurCursor
Definition TRefArray.h:120
TObject * operator*() const override
Return current object or nullptr.
An array of references to TObjects.
Definition TRefArray.h:33
void AddAt(TObject *obj, Int_t idx) override
Add object at position ids.
TObject * At(Int_t idx) const override
Definition TRefArray.h:178
void SetLast(Int_t last)
Set index of last object in array, effectively truncating the array.
Int_t fLowerBound
Definition TRefArray.h:40
TProcessID * fPID
Definition TRefArray.h:38
TObject * RemoveAt(Int_t idx) override
Remove object at index idx.
void Init(Int_t s, Int_t lowerBound)
Initialize a TRefArray.
virtual Int_t AddAtFree(TObject *obj)
Return the position of the new object.
TObject * GetFromTable(Int_t idx) const
the reference may be in the TRefTable
void AddFirst(TObject *obj) override
Add object in the first slot of the array.
UInt_t GetUID(Int_t at) const
Return UID of element at.
virtual ~TRefArray()
Usual destructor (The object pointed to by the array are never deleted).
virtual void Expand(Int_t newSize)
Expand or shrink the array to newSize elements.
void Delete(Option_t *option="") override
Remove all objects from the array and free the internal memory.
Int_t GetEntriesFast() const
Definition TRefArray.h:64
virtual TObject * operator[](Int_t i) const
Definition TRefArray.h:164
UInt_t * fUIDs
Definition TRefArray.h:39
Bool_t BoundsOk(const char *where, Int_t at) const
Definition TRefArray.h:157
Bool_t OutOfBoundsError(const char *where, Int_t i) const
Generate an out-of-bounds error. Always returns false.
void Add(TObject *obj) override
Definition TRefArray.h:74
Int_t GetLast() const override
Return index of last object in array.
TIterator * MakeIterator(Bool_t dir=kIterForward) const override
Returns an array iterator.
void AddLast(TObject *obj) override
Add object in the next empty slot in the array.
virtual Int_t BinarySearch(TObject *obj, Int_t upto=kMaxInt)
Find object using a binary search.
TObject * After(const TObject *obj) const override
Return the object after obj. Returns 0 if obj is last object.
void AddAfter(const TObject *after, TObject *obj) override
Add object in the slot after object after.
void AddBefore(const TObject *before, TObject *obj) override
Add object in the slot before object before.
void Clear(Option_t *option="") override
Remove all objects from the array.
Bool_t IsEmpty() const override
Definition TRefArray.h:71
virtual void AddAtAndExpand(TObject *obj, Int_t idx)
Add object at position idx.
Int_t GetAbsLast() const
Return absolute index to last object in array.
TObject * Remove(TObject *obj) override
Remove object from array.
Int_t IndexOf(const TObject *obj) const override
Int_t fLast
Definition TRefArray.h:41
TRefArray & operator=(const TRefArray &a)
Assignment operator.
TRefArrayIter Iterator_t
Definition TRefArray.h:51
virtual void Sort(Int_t upto=kMaxInt)
If objects in array are sortable (i.e.
TObject * First() const override
Return the object in the first slot.
TObject ** GetObjectRef(const TObject *obj) const override
Return address of pointer obj.
Bool_t GetObjectUID(Int_t &uid, TObject *obj, const char *methodname)
Private/static function, check for validity of pid.
Int_t GetEntries() const override
Return the number of objects in array (i.e.
TProcessID * GetPID() const
Definition TRefArray.h:69
virtual void Compress()
Remove empty slots from array.
Int_t LowerBound() const
Definition TRefArray.h:91
TObject * Before(const TObject *obj) const override
Return the object before obj. Returns 0 if obj is first object.
TObject * Last() const override
Return the object in the last filled slot. Returns 0 if no entries.
Sequenceable collection abstract base class.
Abstract base class defining a generic interface to the underlying Operating System.
Definition TSystem.h:266