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 Add(TObject *obj, Option_t *opt) override { AddLast(obj, opt); }
76 void AddFirst(TObject *obj) override;
77 void AddFirst(TObject *obj, Option_t *) override { AddFirst(obj); }
78 void AddLast(TObject *obj) override;
79 void AddLast(TObject *obj, Option_t *) override { AddLast(obj); }
80 void AddAt(TObject *obj, Int_t idx) override;
81 void AddAt(TObject *obj, Int_t idx, Option_t *) override { AddAt(obj, idx); }
82 virtual void AddAtAndExpand(TObject *obj, Int_t idx);
83 virtual Int_t AddAtFree(TObject *obj);
84 void AddAfter(const TObject *after, TObject *obj) override;
85 void AddAfter(const TObject *after, TObject *obj, Option_t *) override { AddAfter(after, obj); }
86 void AddBefore(const TObject *before, TObject *obj) override;
87 void AddBefore(const TObject *before, TObject *obj, Option_t *) override { AddBefore(before, obj); }
88 TObject *RemoveAt(Int_t idx) override;
89 TObject *Remove(TObject *obj) override;
90
91 TObject *At(Int_t idx) const override;
92 TObject *Before(const TObject *obj) const override;
93 TObject *After(const TObject *obj) const override;
94 TObject *First() const override;
95 TObject *Last() const override;
96 virtual TObject *operator[](Int_t i) const;
97 Int_t LowerBound() const { return fLowerBound; }
98 Int_t IndexOf(const TObject *obj) const override;
99 void SetLast(Int_t last);
100
101 virtual void Sort(Int_t upto = kMaxInt);
102 virtual Int_t BinarySearch(TObject *obj, Int_t upto = kMaxInt); // the TRefArray has to be sorted, -1 == not found !!
103
104 ClassDefOverride(TRefArray,1) //An array of references to TObjects
105};
106
107
108// Preventing warnings with -Weffc++ in GCC since it is a false positive for the TRefArrayIter 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// TRefArrayIter //
117// //
118// Iterator of object array. //
119// //
120//////////////////////////////////////////////////////////////////////////
121
122class TRefArrayIter : public TIterator {
123
124private:
125 const TRefArray *fArray; //array being iterated
126 Int_t fCurCursor; //current position in array
127 Int_t fCursor; //next position in array
128 Bool_t fDirection; //iteration direction
129
131
132public:
133 using iterator_category = std::bidirectional_iterator_tag; // TODO: ideally it should be a randomaccess_iterator_tag
135 using difference_type = std::ptrdiff_t;
136 using pointer = TObject **;
137 using const_pointer = const TObject **;
138 using reference = const TObject *&;
139
141 TRefArrayIter(const TRefArrayIter &iter);
143 TIterator &operator=(const TIterator &rhs) override;
145
146 const TCollection *GetCollection() const override { return fArray; }
147 TObject *Next() override;
148 void Reset() override;
149 Bool_t operator!=(const TIterator &aIter) const override;
150 Bool_t operator!=(const TRefArrayIter &aIter) const;
151 TObject *operator*() const override;
152
153 ClassDefOverride(TRefArrayIter,0) //Object array iterator
154};
155
156#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
157#pragma GCC diagnostic pop
158#endif
159
160
161//---- inlines -----------------------------------------------------------------
162
163inline Bool_t TRefArray::BoundsOk(const char *where, Int_t at) const
164{
167 : kTRUE;
168}
169
171{
172 int j = at-fLowerBound;
173 if (j >= 0 && j < fSize) {
174 if (!fPID) return nullptr;
175 if (!TProcessID::IsValid(fPID)) return nullptr;
177 if (!obj) obj = GetFromTable(j);
178 return obj;
179 }
180 BoundsOk("At", at);
181 return nullptr;
182}
183
184inline TObject *TRefArray::At(Int_t at) const
185{
186 // Return the object at position i. Returns 0 if i is out of bounds.
187 int j = at-fLowerBound;
188 if (j >= 0 && j < fSize) {
189 if (!fPID) return nullptr;
190 if (!TProcessID::IsValid(fPID)) return nullptr;
192 if (!obj) obj = GetFromTable(j);
193 return obj;
194 }
195 BoundsOk("At", at);
196 return nullptr;
197}
198
199#endif
#define a(i)
Definition RSha256.hxx:99
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:77
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
constexpr Int_t kMaxInt
Definition RtypesCore.h:119
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:60
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
#define ClassDefOverride(name, id)
Definition Rtypes.h:348
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:122
TObject * Next() override
Return next object in array. Returns 0 when no more objects in array.
Bool_t fDirection
Definition TRefArray.h:128
void Reset() override
Reset array iterator.
const TRefArray * fArray
Definition TRefArray.h:125
const TCollection * GetCollection() const override
Definition TRefArray.h:146
std::ptrdiff_t difference_type
Definition TRefArray.h:135
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:133
Int_t fCurCursor
Definition TRefArray.h:126
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:184
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
TRefArray(TProcessID *pid=nullptr)
default constructor
void AddBefore(const TObject *before, TObject *obj, Option_t *) override
Definition TRefArray.h:87
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:170
UInt_t * fUIDs
Definition TRefArray.h:39
Bool_t BoundsOk(const char *where, Int_t at) const
Definition TRefArray.h:163
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.
void AddAfter(const TObject *after, TObject *obj, Option_t *) override
Definition TRefArray.h:85
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 AddFirst(TObject *obj, Option_t *) override
Definition TRefArray.h:77
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
void AddLast(TObject *obj, Option_t *) override
Definition TRefArray.h:79
Int_t fLast
Definition TRefArray.h:41
void AddAt(TObject *obj, Int_t idx, Option_t *) override
Definition TRefArray.h:81
TRefArray & operator=(const TRefArray &a)
Assignment operator.
void Add(TObject *obj, Option_t *opt) override
Definition TRefArray.h:75
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:97
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:276