Logo ROOT  
Reference Guide
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);
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 0;
169 if (!TProcessID::IsValid(fPID)) return 0;
170 TObject *obj = fPID->GetObjectWithID(fUIDs[j]);
171 if (obj==0) obj = GetFromTable(j);
172 return obj;
173 }
174 BoundsOk("At", at);
175 return 0;
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 0;
184 if (!TProcessID::IsValid(fPID)) return 0;
185 TObject *obj = fPID->GetObjectWithID(fUIDs[j]);
186 if (obj==0) obj = GetFromTable(j);
187 return obj;
188 }
189 BoundsOk("At", at);
190 return 0;
191}
192
193#endif
bool Bool_t
Definition: RtypesCore.h:63
int Int_t
Definition: RtypesCore.h:45
const Int_t kMaxInt
Definition: RtypesCore.h:112
unsigned int UInt_t
Definition: RtypesCore.h:46
const Bool_t kTRUE
Definition: RtypesCore.h:100
const char Option_t
Definition: RtypesCore.h:66
#define ClassDefOverride(name, id)
Definition: Rtypes.h:339
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:37
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
Definition: TProcessID.cxx:360
TObject * GetObjectWithID(UInt_t uid)
returns the TObject with unique identifier uid in the table of objects
Definition: TProcessID.cxx:332
Iterator of object array.
Definition: TRefArray.h:116
TObject * Next() override
Return next object in array. Returns 0 when no more objects in array.
Definition: TRefArray.cxx:901
Bool_t fDirection
Definition: TRefArray.h:122
void Reset() override
Reset array iterator.
Definition: TRefArray.cxx:928
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.
Definition: TRefArray.cxx:872
Bool_t operator!=(const TIterator &aIter) const override
This operator compares two TIterator objects.
Definition: TRefArray.cxx:941
std::bidirectional_iterator_tag iterator_category
Definition: TRefArray.h:127
Int_t fCursor
Definition: TRefArray.h:121
Int_t fCurCursor
Definition: TRefArray.h:120
TObject * operator*() const override
Return current object or nullptr.
Definition: TRefArray.cxx:961
An array of references to TObjects.
Definition: TRefArray.h:33
void AddAt(TObject *obj, Int_t idx) override
Add object at position ids.
Definition: TRefArray.cxx:358
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.
Definition: TRefArray.cxx:774
Int_t fLowerBound
Definition: TRefArray.h:40
TProcessID * fPID
Definition: TRefArray.h:38
TRefArray(TProcessID *pid=nullptr)
default constructor
Definition: TRefArray.cxx:109
TObject * RemoveAt(Int_t idx) override
Remove object at index idx.
Definition: TRefArray.cxx:723
void Init(Int_t s, Int_t lowerBound)
Initialize a TRefArray.
Definition: TRefArray.cxx:683
virtual Int_t AddAtFree(TObject *obj)
Return the position of the new object.
Definition: TRefArray.cxx:376
TObject * GetFromTable(Int_t idx) const
the reference may be in the TRefTable
Definition: TRefArray.cxx:501
void AddFirst(TObject *obj) override
Add object in the first slot of the array.
Definition: TRefArray.cxx:266
UInt_t GetUID(Int_t at) const
Return UID of element at.
Definition: TRefArray.cxx:643
virtual ~TRefArray()
Usual destructor (The object pointed to by the array are never deleted).
Definition: TRefArray.cxx:196
virtual void Expand(Int_t newSize)
Expand or shrink the array to newSize elements.
Definition: TRefArray.cxx:473
void Delete(Option_t *option="") override
Remove all objects from the array and free the internal memory.
Definition: TRefArray.cxx:457
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.
Definition: TRefArray.cxx:714
void Add(TObject *obj) override
Definition: TRefArray.h:74
Int_t GetLast() const override
Return index of last object in array.
Definition: TRefArray.cxx:625
TIterator * MakeIterator(Bool_t dir=kIterForward) const override
Returns an array iterator.
Definition: TRefArray.cxx:706
void AddLast(TObject *obj) override
Add object in the next empty slot in the array.
Definition: TRefArray.cxx:282
virtual Int_t BinarySearch(TObject *obj, Int_t upto=kMaxInt)
Find object using a binary search.
Definition: TRefArray.cxx:810
TObject * After(const TObject *obj) const override
Return the object after obj. Returns 0 if obj is last object.
Definition: TRefArray.cxx:400
void AddAfter(const TObject *after, TObject *obj) override
Add object in the slot after object after.
Definition: TRefArray.cxx:317
void AddBefore(const TObject *before, TObject *obj) override
Add object in the slot before object before.
Definition: TRefArray.cxx:293
void Clear(Option_t *option="") override
Remove all objects from the array.
Definition: TRefArray.cxx:426
Bool_t IsEmpty() const override
Definition: TRefArray.h:71
virtual void AddAtAndExpand(TObject *obj, Int_t idx)
Add object at position idx.
Definition: TRefArray.cxx:335
Int_t GetAbsLast() const
Return absolute index to last object in array.
Definition: TRefArray.cxx:605
TObject * Remove(TObject *obj) override
Remove object from array.
Definition: TRefArray.cxx:748
Int_t IndexOf(const TObject *obj) const override
Definition: TRefArray.cxx:661
Int_t fLast
Definition: TRefArray.h:41
TRefArray & operator=(const TRefArray &a)
Assignment operator.
Definition: TRefArray.cxx:172
TRefArrayIter Iterator_t
Definition: TRefArray.h:51
virtual void Sort(Int_t upto=kMaxInt)
If objects in array are sortable (i.e.
Definition: TRefArray.cxx:786
TObject * First() const override
Return the object in the first slot.
Definition: TRefArray.cxx:568
TObject ** GetObjectRef(const TObject *obj) const override
Return address of pointer obj.
Definition: TRefArray.cxx:633
Bool_t GetObjectUID(Int_t &uid, TObject *obj, const char *methodname)
Private/static function, check for validity of pid.
Definition: TRefArray.cxx:207
Int_t GetEntries() const override
Return the number of objects in array (i.e.
Definition: TRefArray.cxx:591
TProcessID * GetPID() const
Definition: TRefArray.h:69
virtual void Compress()
Remove empty slots from array.
Definition: TRefArray.cxx:438
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.
Definition: TRefArray.cxx:413
TObject * Last() const override
Return the object in the last filled slot. Returns 0 if no entries.
Definition: TRefArray.cxx:576
Sequenceable collection abstract base class.
Abstract base class defining a generic interface to the underlying Operating System.
Definition: TSystem.h:266
static constexpr double s
TArc a
Definition: textangle.C:12