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
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);
60 TRefArray(Int_t s, TProcessID *pid);
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
#define a(i)
Definition RSha256.hxx:99
int Int_t
Definition RtypesCore.h:45
const Int_t kMaxInt
Definition RtypesCore.h:103
unsigned int UInt_t
Definition RtypesCore.h:46
bool Bool_t
Definition RtypesCore.h:63
const Bool_t kTRUE
Definition RtypesCore.h:91
const char Option_t
Definition RtypesCore.h:66
#define ClassDef(name, id)
Definition Rtypes.h:325
const Bool_t kIterForward
Definition TCollection.h:40
Collection abstract base class.
Definition TCollection.h:63
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
TObject * GetObjectWithID(UInt_t uid)
returns the TObject with unique identifier uid in the table of objects
Iterator of object array.
Definition TRefArray.h:125
TObject * operator*() const
Return current object or nullptr.
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.
TIterator & operator=(const TIterator &rhs)
Overridden assignment operator.
Bool_t operator!=(const TIterator &aIter) const
This operator compares two TIterator objects.
Int_t fCurCursor
Definition TRefArray.h:129
const TCollection * GetCollection() const
Definition TRefArray.h:142
void Reset()
Reset array iterator.
An array of references to TObjects.
Definition TRefArray.h:39
TIterator * MakeIterator(Bool_t dir=kIterForward) const
Returns an array iterator.
void SetLast(Int_t last)
Set index of last object in array, effectively truncating the array.
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.
void Init(Int_t s, Int_t lowerBound)
Initialize a TRefArray.
void Add(TObject *obj)
Definition TRefArray.h:80
virtual void Clear(Option_t *option="")
Remove all objects from the array.
virtual Int_t AddAtFree(TObject *obj)
Return the position of the new object.
TObject * First() const
Return the object in the first slot.
TObject * GetFromTable(Int_t idx) const
the reference may be in the TRefTable
virtual TObject * Remove(TObject *obj)
Remove object from array.
UInt_t GetUID(Int_t at) const
Return UID of element at.
virtual void AddFirst(TObject *obj)
Add object in the first slot of the array.
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.
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
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.
Bool_t OutOfBoundsError(const char *where, Int_t i) const
Generate an out-of-bounds error. Always returns false.
TObject * Before(const TObject *obj) const
Return the object before obj. Returns 0 if obj is first object.
Int_t GetLast() const
Return index of last object in array.
virtual Int_t BinarySearch(TObject *obj, Int_t upto=kMaxInt)
Find object using a binary search.
virtual void AddAt(TObject *obj, Int_t idx)
Add object at position ids.
virtual void AddAfter(const TObject *after, TObject *obj)
Add object in the slot after object after.
TObject * Last() const
Return the object in the last filled slot. Returns 0 if no entries.
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.
Int_t GetEntries() const
Return the number of objects in array (i.e.
virtual void AddLast(TObject *obj)
Add object in the next empty slot in the array.
virtual void Delete(Option_t *option="")
Remove all objects from the array and free the internal memory.
TObject * At(Int_t idx) const
Definition TRefArray.h:180
Int_t fLast
Definition TRefArray.h:47
TRefArray & operator=(const TRefArray &a)
Assignment operator.
TRefArrayIter Iterator_t
Definition TRefArray.h:57
virtual void Sort(Int_t upto=kMaxInt)
If objects in array are sortable (i.e.
Bool_t GetObjectUID(Int_t &uid, TObject *obj, const char *methodname)
Private/static function, check for validity of pid.
virtual TObject * RemoveAt(Int_t idx)
Remove object at index idx.
TProcessID * GetPID() const
Definition TRefArray.h:75
virtual void Compress()
Remove empty slots from array.
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.
Sequenceable collection abstract base class.
Abstract base class defining a generic interface to the underlying Operating System.
Definition TSystem.h:266