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
30#if (__GNUC__ >= 3) && !defined(__INTEL_COMPILER)
31// Prevent -Weffc++ from complaining about the inheritance
32// TRefArrayIter from std::iterator.
33#pragma GCC system_header
34#endif
35
36class TSystem;
37class TRefArrayIter;
38
39class TRefArray : public TSeqCollection {
40
41friend class TRefArrayIter;
42
43protected:
44 TProcessID *fPID; //Pointer to Process Unique Identifier
45 UInt_t *fUIDs; //[fSize] To store uids of referenced objects
46 Int_t fLowerBound; //Lower bound of the array
47 Int_t fLast; //Last element in array containing an object
48
49 Bool_t BoundsOk(const char *where, Int_t at) const;
50 void Init(Int_t s, Int_t lowerBound);
51 Bool_t OutOfBoundsError(const char *where, Int_t i) const;
52 Int_t GetAbsLast() const;
53 TObject *GetFromTable(Int_t idx) const;
54 Bool_t GetObjectUID(Int_t &uid, TObject *obj, const char *methodname);
55
56public:
58
59 TRefArray(TProcessID *pid = 0);
61 TRefArray(Int_t s, Int_t lowerBound = 0, TProcessID *pid = 0);
62 TRefArray(const TRefArray &a);
64 virtual ~TRefArray();
65 virtual void Clear(Option_t *option="");
66 virtual void Compress();
67 virtual void Delete(Option_t *option="");
68 virtual void Expand(Int_t newSize); // expand or shrink an array
69 Int_t GetEntries() const;
71 return GetAbsLast() + 1; //only OK when no gaps
72 }
73 Int_t GetLast() const;
74 TObject **GetObjectRef(const TObject *obj) const;
75 TProcessID *GetPID() const {return fPID;}
76 UInt_t GetUID(Int_t at) const;
77 Bool_t IsEmpty() const { return GetAbsLast() == -1; }
79
80 void Add(TObject *obj) { AddLast(obj); }
81 virtual void AddFirst(TObject *obj);
82 virtual void AddLast(TObject *obj);
83 virtual void AddAt(TObject *obj, Int_t idx);
84 virtual void AddAtAndExpand(TObject *obj, Int_t idx);
85 virtual Int_t AddAtFree(TObject *obj);
86 virtual void AddAfter(const TObject *after, TObject *obj);
87 virtual void AddBefore(const TObject *before, TObject *obj);
88 virtual TObject *RemoveAt(Int_t idx);
89 virtual TObject *Remove(TObject *obj);
90
91 TObject *At(Int_t idx) const;
92 TObject *Before(const TObject *obj) const;
93 TObject *After(const TObject *obj) const;
94 TObject *First() const;
95 TObject *Last() const;
96 virtual TObject *operator[](Int_t i) const;
97 Int_t LowerBound() const { return fLowerBound; }
98 Int_t IndexOf(const TObject *obj) const;
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 ClassDef(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
123 public std::iterator<std::bidirectional_iterator_tag, // TODO: ideally it should be a randomaccess_iterator_tag
124 TObject*, std::ptrdiff_t,
125 const TObject**, const TObject*&> {
126
127private:
128 const TRefArray *fArray; //array being iterated
129 Int_t fCurCursor; //current position in array
130 Int_t fCursor; //next position in array
131 Bool_t fDirection; //iteration direction
132
134
135public:
136 TRefArrayIter(const TRefArray *arr, Bool_t dir = kIterForward);
137 TRefArrayIter(const TRefArrayIter &iter);
139 TIterator &operator=(const TIterator &rhs);
141
142 const TCollection *GetCollection() const { return fArray; }
143 TObject *Next();
144 void Reset();
145 Bool_t operator!=(const TIterator &aIter) const;
146 Bool_t operator!=(const TRefArrayIter &aIter) const;
147 TObject *operator*() const;
148
149 ClassDef(TRefArrayIter,0) //Object array iterator
150};
151
152#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
153#pragma GCC diagnostic pop
154#endif
155
156
157//---- inlines -----------------------------------------------------------------
158
159inline Bool_t TRefArray::BoundsOk(const char *where, Int_t at) const
160{
161 return (at < fLowerBound || at-fLowerBound >= fSize)
162 ? OutOfBoundsError(where, at)
163 : kTRUE;
164}
165
167{
168 int j = at-fLowerBound;
169 if (j >= 0 && j < fSize) {
170 if (!fPID) return 0;
171 if (!TProcessID::IsValid(fPID)) return 0;
172 TObject *obj = fPID->GetObjectWithID(fUIDs[j]);
173 if (obj==0) obj = GetFromTable(j);
174 return obj;
175 }
176 BoundsOk("At", at);
177 return 0;
178}
179
180inline TObject *TRefArray::At(Int_t at) const
181{
182 // Return the object at position i. Returns 0 if i is out of bounds.
183 int j = at-fLowerBound;
184 if (j >= 0 && j < fSize) {
185 if (!fPID) return 0;
186 if (!TProcessID::IsValid(fPID)) return 0;
187 TObject *obj = fPID->GetObjectWithID(fUIDs[j]);
188 if (obj==0) obj = GetFromTable(j);
189 return obj;
190 }
191 BoundsOk("At", at);
192 return 0;
193}
194
195#endif
int Int_t
Definition: RtypesCore.h:45
const Int_t kMaxInt
Definition: RtypesCore.h:112
unsigned int UInt_t
Definition: RtypesCore.h:46
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
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:125
TObject * operator*() const
Return current object or nullptr.
Definition: TRefArray.cxx:961
Bool_t fDirection
Definition: TRefArray.h:131
const TRefArray * fArray
Definition: TRefArray.h:128
TObject * Next()
Return next object in array. Returns 0 when no more objects in array.
Definition: TRefArray.cxx:901
TIterator & operator=(const TIterator &rhs)
Overridden assignment operator.
Definition: TRefArray.cxx:872
Bool_t operator!=(const TIterator &aIter) const
This operator compares two TIterator objects.
Definition: TRefArray.cxx:941
Int_t fCursor
Definition: TRefArray.h:130
Int_t fCurCursor
Definition: TRefArray.h:129
const TCollection * GetCollection() const
Definition: TRefArray.h:142
void Reset()
Reset array iterator.
Definition: TRefArray.cxx:928
An array of references to TObjects.
Definition: TRefArray.h:39
TIterator * MakeIterator(Bool_t dir=kIterForward) const
Returns an array iterator.
Definition: TRefArray.cxx:706
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:46
TProcessID * fPID
Definition: TRefArray.h:44
virtual void AddBefore(const TObject *before, TObject *obj)
Add object in the slot before object before.
Definition: TRefArray.cxx:293
void Init(Int_t s, Int_t lowerBound)
Initialize a TRefArray.
Definition: TRefArray.cxx:683
void Add(TObject *obj)
Definition: TRefArray.h:80
virtual void Clear(Option_t *option="")
Remove all objects from the array.
Definition: TRefArray.cxx:426
virtual Int_t AddAtFree(TObject *obj)
Return the position of the new object.
Definition: TRefArray.cxx:376
TObject * First() const
Return the object in the first slot.
Definition: TRefArray.cxx:568
TObject * GetFromTable(Int_t idx) const
the reference may be in the TRefTable
Definition: TRefArray.cxx:501
virtual TObject * Remove(TObject *obj)
Remove object from array.
Definition: TRefArray.cxx:748
UInt_t GetUID(Int_t at) const
Return UID of element at.
Definition: TRefArray.cxx:643
virtual void AddFirst(TObject *obj)
Add object in the first slot of the array.
Definition: TRefArray.cxx:266
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
TRefArray(TProcessID *pid=0)
default constructor
Definition: TRefArray.cxx:109
Int_t GetEntriesFast() const
Definition: TRefArray.h:70
virtual TObject * operator[](Int_t i) const
Definition: TRefArray.h:166
Int_t IndexOf(const TObject *obj) const
Definition: TRefArray.cxx:661
UInt_t * fUIDs
Definition: TRefArray.h:45
Bool_t BoundsOk(const char *where, Int_t at) const
Definition: TRefArray.h:159
TObject * After(const TObject *obj) const
Return the object after obj. Returns 0 if obj is last object.
Definition: TRefArray.cxx:400
Bool_t OutOfBoundsError(const char *where, Int_t i) const
Generate an out-of-bounds error. Always returns false.
Definition: TRefArray.cxx:714
TObject * Before(const TObject *obj) const
Return the object before obj. Returns 0 if obj is first object.
Definition: TRefArray.cxx:413
Int_t GetLast() const
Return index of last object in array.
Definition: TRefArray.cxx:625
virtual Int_t BinarySearch(TObject *obj, Int_t upto=kMaxInt)
Find object using a binary search.
Definition: TRefArray.cxx:810
virtual void AddAt(TObject *obj, Int_t idx)
Add object at position ids.
Definition: TRefArray.cxx:358
virtual void AddAfter(const TObject *after, TObject *obj)
Add object in the slot after object after.
Definition: TRefArray.cxx:317
TObject * Last() const
Return the object in the last filled slot. Returns 0 if no entries.
Definition: TRefArray.cxx:576
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
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TRefArray.cxx:591
virtual void AddLast(TObject *obj)
Add object in the next empty slot in the array.
Definition: TRefArray.cxx:282
virtual void Delete(Option_t *option="")
Remove all objects from the array and free the internal memory.
Definition: TRefArray.cxx:457
TObject * At(Int_t idx) const
Definition: TRefArray.h:180
Int_t fLast
Definition: TRefArray.h:47
TRefArray & operator=(const TRefArray &a)
Assignment operator.
Definition: TRefArray.cxx:172
TRefArrayIter Iterator_t
Definition: TRefArray.h:57
virtual void Sort(Int_t upto=kMaxInt)
If objects in array are sortable (i.e.
Definition: TRefArray.cxx:786
Bool_t GetObjectUID(Int_t &uid, TObject *obj, const char *methodname)
Private/static function, check for validity of pid.
Definition: TRefArray.cxx:207
virtual TObject * RemoveAt(Int_t idx)
Remove object at index idx.
Definition: TRefArray.cxx:723
TProcessID * GetPID() const
Definition: TRefArray.h:75
virtual void Compress()
Remove empty slots from array.
Definition: TRefArray.cxx:438
Int_t LowerBound() const
Definition: TRefArray.h:97
Bool_t IsEmpty() const
Definition: TRefArray.h:77
TObject ** GetObjectRef(const TObject *obj) const
Return address of pointer obj.
Definition: TRefArray.cxx:633
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
auto * a
Definition: textangle.C:12