Logo ROOT   6.14/05
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 
36 class TSystem;
37 class TRefArrayIter;
38 
39 class TRefArray : public TSeqCollection {
40 
41 friend class TRefArrayIter;
42 
43 protected:
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 
56 public:
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);
63  TRefArray& operator=(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 
122 class TRefArrayIter : public TIterator,
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 
127 private:
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 
133  TRefArrayIter() : fArray(0), fCurCursor(0), fCursor(0), fDirection(kIterForward) { }
134 
135 public:
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 
159 inline 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 
180 inline 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
virtual Int_t AddAtFree(TObject *obj)
Return the position of the new object.
Definition: TRefArray.cxx:377
void Init(Int_t s, Int_t lowerBound)
Initialize a TRefArray.
Definition: TRefArray.cxx:681
TObject * GetFromTable(Int_t idx) const
the reference may be in the TRefTable
Definition: TRefArray.cxx:499
TObject ** GetObjectRef(const TObject *obj) const
Return address of pointer obj.
Definition: TRefArray.cxx:631
const TRefArray * fArray
Definition: TRefArray.h:128
virtual void Sort(Int_t upto=kMaxInt)
If objects in array are sortable (i.e.
Definition: TRefArray.cxx:784
void Add(TObject *obj)
Definition: TRefArray.h:80
const char Option_t
Definition: RtypesCore.h:62
virtual void Expand(Int_t newSize)
Expand or shrink the array to newSize elements.
Definition: TRefArray.cxx:474
Int_t GetLast() const
Return index of last object in array.
Definition: TRefArray.cxx:623
TObject * After(const TObject *obj) const
Return the object after obj. Returns 0 if obj is last object.
Definition: TRefArray.cxx:401
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
An array of references to TObjects.
Definition: TRefArray.h:39
void SetLast(Int_t last)
Set index of last object in array, effectively truncating the array.
Definition: TRefArray.cxx:772
virtual Int_t BinarySearch(TObject *obj, Int_t upto=kMaxInt)
Find object using a binary search.
Definition: TRefArray.cxx:808
virtual void AddLast(TObject *obj)
Add object in the next empty slot in the array.
Definition: TRefArray.cxx:283
Iterator abstract base class.
Definition: TIterator.h:30
virtual ~TRefArray()
Usual destructor (The object pointed to by the array are never deleted).
Definition: TRefArray.cxx:197
Sequenceable collection abstract base class.
#define ClassDef(name, id)
Definition: Rtypes.h:320
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TRefArray.cxx:589
TObject * At(Int_t idx) const
Definition: TRefArray.h:180
Bool_t OutOfBoundsError(const char *where, Int_t i) const
Generate an out-of-bounds error. Always returns false.
Definition: TRefArray.cxx:712
Int_t GetEntriesFast() const
Definition: TRefArray.h:70
virtual void AddFirst(TObject *obj)
Add object in the first slot of the array.
Definition: TRefArray.cxx:267
UInt_t * fUIDs
Definition: TRefArray.h:45
const Bool_t kIterForward
Definition: TCollection.h:40
Bool_t fDirection
Definition: TRefArray.h:131
A TProcessID identifies a ROOT job in a unique way in time and space.
Definition: TProcessID.h:69
Bool_t GetObjectUID(Int_t &uid, TObject *obj, const char *methodname)
Private/static function, check for validity of pid.
Definition: TRefArray.cxx:208
TObject * Last() const
Return the object in the last filled slot. Returns 0 if no entries.
Definition: TRefArray.cxx:574
Bool_t operator!=(const TDatime &d1, const TDatime &d2)
Definition: TDatime.h:104
Int_t LowerBound() const
Definition: TRefArray.h:97
Int_t fCurCursor
Definition: TRefArray.h:129
TProcessID * GetPID() const
Definition: TRefArray.h:75
Iterator of object array.
Definition: TRefArray.h:122
Int_t fCursor
Definition: TRefArray.h:130
virtual void AddAt(TObject *obj, Int_t idx)
Add object at position ids.
Definition: TRefArray.cxx:359
virtual void Compress()
Remove empty slots from array.
Definition: TRefArray.cxx:439
auto * a
Definition: textangle.C:12
Int_t fLowerBound
Definition: TRefArray.h:46
TTime operator*(const TTime &t1, const TTime &t2)
Definition: TTime.h:85
Collection abstract base class.
Definition: TCollection.h:63
Int_t GetAbsLast() const
Return absolute index to last object in array.
Definition: TRefArray.cxx:603
unsigned int UInt_t
Definition: RtypesCore.h:42
const TCollection * GetCollection() const
Definition: TRefArray.h:142
void Reset(Detail::TBranchProxy *x)
UInt_t GetUID(Int_t at) const
Return UID of element at.
Definition: TRefArray.cxx:641
TObject * Before(const TObject *obj) const
Return the object before obj. Returns 0 if obj is first object.
Definition: TRefArray.cxx:414
TObject * GetObjectWithID(UInt_t uid)
returns the TObject with unique identifier uid in the table of objects
Definition: TProcessID.cxx:330
virtual void AddAtAndExpand(TObject *obj, Int_t idx)
Add object at position idx.
Definition: TRefArray.cxx:336
virtual void Delete(Option_t *option="")
Remove all objects from the array and free the internal memory.
Definition: TRefArray.cxx:458
virtual void Clear(Option_t *option="")
Remove all objects from the array.
Definition: TRefArray.cxx:427
TRefArray(TProcessID *pid=0)
default constructor
Definition: TRefArray.cxx:110
virtual void AddAfter(const TObject *after, TObject *obj)
Add object in the slot after object after.
Definition: TRefArray.cxx:318
static constexpr double s
friend class TRefArrayIter
Definition: TRefArray.h:41
Bool_t BoundsOk(const char *where, Int_t at) const
Definition: TRefArray.h:159
virtual TObject * operator[](Int_t i) const
Definition: TRefArray.h:166
Mother of all ROOT objects.
Definition: TObject.h:37
Int_t IndexOf(const TObject *obj) const
Definition: TRefArray.cxx:659
TObject * First() const
Return the object in the first slot.
Definition: TRefArray.cxx:566
TProcessID * fPID
Definition: TRefArray.h:44
const Int_t kMaxInt
Definition: RtypesCore.h:99
TIterator * MakeIterator(Bool_t dir=kIterForward) const
Returns an array iterator.
Definition: TRefArray.cxx:704
virtual TObject * RemoveAt(Int_t idx)
Remove object at index idx.
Definition: TRefArray.cxx:721
Bool_t IsEmpty() const
Definition: TRefArray.h:77
TRefArrayIter Iterator_t
Definition: TRefArray.h:57
static Bool_t IsValid(TProcessID *pid)
static function. return kTRUE if pid is a valid TProcessID
Definition: TProcessID.cxx:358
Abstract base class defining a generic interface to the underlying Operating System.
Definition: TSystem.h:248
virtual TObject * Remove(TObject *obj)
Remove object from array.
Definition: TRefArray.cxx:746
Int_t fLast
Definition: TRefArray.h:47
const Bool_t kTRUE
Definition: RtypesCore.h:87
TRefArray & operator=(const TRefArray &a)
Assignment operator.
Definition: TRefArray.cxx:173
virtual void AddBefore(const TObject *before, TObject *obj)
Add object in the slot before object before.
Definition: TRefArray.cxx:294