Logo ROOT   6.08/07
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 #ifndef ROOT_TSeqCollection
26 #include "TSeqCollection.h"
27 #endif
28 #ifndef ROOT_TProcessID
29 #include "TProcessID.h"
30 #endif
31 
32 #include <iterator>
33 
34 #if (__GNUC__ >= 3) && !defined(__INTEL_COMPILER)
35 // Prevent -Weffc++ from complaining about the inheritance
36 // TRefArrayIter from std::iterator.
37 #pragma GCC system_header
38 #endif
39 
40 class TSystem;
41 class TRefArrayIter;
42 
43 class TRefArray : public TSeqCollection {
44 
45 friend class TRefArrayIter;
46 
47 protected:
48  TProcessID *fPID; //Pointer to Process Unique Identifier
49  UInt_t *fUIDs; //[fSize] To store uids of referenced objects
50  Int_t fLowerBound; //Lower bound of the array
51  Int_t fLast; //Last element in array containing an object
52 
53  Bool_t BoundsOk(const char *where, Int_t at) const;
54  void Init(Int_t s, Int_t lowerBound);
55  Bool_t OutOfBoundsError(const char *where, Int_t i) const;
56  Int_t GetAbsLast() const;
57  TObject *GetFromTable(Int_t idx) const;
58  Bool_t GetObjectUID(Int_t &uid, TObject *obj, const char *methodname);
59 
60 public:
62 
63  TRefArray(TProcessID *pid = 0);
64  TRefArray(Int_t s, TProcessID *pid);
65  TRefArray(Int_t s, Int_t lowerBound = 0, TProcessID *pid = 0);
66  TRefArray(const TRefArray &a);
67  TRefArray& operator=(const TRefArray &a);
68  virtual ~TRefArray();
69  virtual void Clear(Option_t *option="");
70  virtual void Compress();
71  virtual void Delete(Option_t *option="");
72  virtual void Expand(Int_t newSize); // expand or shrink an array
73  Int_t GetEntries() const;
75  return GetAbsLast() + 1; //only OK when no gaps
76  }
77  Int_t GetLast() const;
78  TObject **GetObjectRef(const TObject *obj) const;
79  TProcessID *GetPID() const {return fPID;}
80  UInt_t GetUID(Int_t at) const;
81  Bool_t IsEmpty() const { return GetAbsLast() == -1; }
83 
84  void Add(TObject *obj) { AddLast(obj); }
85  virtual void AddFirst(TObject *obj);
86  virtual void AddLast(TObject *obj);
87  virtual void AddAt(TObject *obj, Int_t idx);
88  virtual void AddAtAndExpand(TObject *obj, Int_t idx);
89  virtual Int_t AddAtFree(TObject *obj);
90  virtual void AddAfter(const TObject *after, TObject *obj);
91  virtual void AddBefore(const TObject *before, TObject *obj);
92  virtual TObject *RemoveAt(Int_t idx);
93  virtual TObject *Remove(TObject *obj);
94 
95  TObject *At(Int_t idx) const;
96  TObject *Before(const TObject *obj) const;
97  TObject *After(const TObject *obj) const;
98  TObject *First() const;
99  TObject *Last() const;
100  virtual TObject *operator[](Int_t i) const;
101  Int_t LowerBound() const { return fLowerBound; }
102  Int_t IndexOf(const TObject *obj) const;
103  void SetLast(Int_t last);
104 
105  virtual void Sort(Int_t upto = kMaxInt);
106  virtual Int_t BinarySearch(TObject *obj, Int_t upto = kMaxInt); // the TRefArray has to be sorted, -1 == not found !!
107 
108  ClassDef(TRefArray,1) //An array of references to TObjects
109 };
110 
111 
112 // Preventing warnings with -Weffc++ in GCC since it is a false positive for the TRefArrayIter destructor.
113 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
114 #pragma GCC diagnostic push
115 #pragma GCC diagnostic ignored "-Weffc++"
116 #endif
117 
118 //////////////////////////////////////////////////////////////////////////
119 // //
120 // TRefArrayIter //
121 // //
122 // Iterator of object array. //
123 // //
124 //////////////////////////////////////////////////////////////////////////
125 
126 class TRefArrayIter : public TIterator,
127  public std::iterator<std::bidirectional_iterator_tag, // TODO: ideally it should be a randomaccess_iterator_tag
128  TObject*, std::ptrdiff_t,
129  const TObject**, const TObject*&> {
130 
131 private:
132  const TRefArray *fArray; //array being iterated
133  Int_t fCurCursor; //current position in array
134  Int_t fCursor; //next position in array
135  Bool_t fDirection; //iteration direction
136 
137  TRefArrayIter() : fArray(0), fCurCursor(0), fCursor(0), fDirection(kIterForward) { }
138 
139 public:
140  TRefArrayIter(const TRefArray *arr, Bool_t dir = kIterForward);
141  TRefArrayIter(const TRefArrayIter &iter);
143  TIterator &operator=(const TIterator &rhs);
145 
146  const TCollection *GetCollection() const { return fArray; }
147  TObject *Next();
148  void Reset();
149  Bool_t operator!=(const TIterator &aIter) const;
150  Bool_t operator!=(const TRefArrayIter &aIter) const;
151  TObject *operator*() const;
152 
153  ClassDef(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 
163 inline Bool_t TRefArray::BoundsOk(const char *where, Int_t at) const
164 {
165  return (at < fLowerBound || at-fLowerBound >= fSize)
166  ? OutOfBoundsError(where, at)
167  : kTRUE;
168 }
169 
171 {
172  int j = at-fLowerBound;
173  if (j >= 0 && j < fSize) {
174  if (!fPID) return 0;
175  if (!TProcessID::IsValid(fPID)) return 0;
176  TObject *obj = fPID->GetObjectWithID(fUIDs[j]);
177  if (obj==0) obj = GetFromTable(j);
178  return obj;
179  }
180  BoundsOk("At", at);
181  return 0;
182 }
183 
184 inline 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 0;
190  if (!TProcessID::IsValid(fPID)) return 0;
191  TObject *obj = fPID->GetObjectWithID(fUIDs[j]);
192  if (obj==0) obj = GetFromTable(j);
193  return obj;
194  }
195  BoundsOk("At", at);
196  return 0;
197 }
198 
199 #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:132
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:84
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
TArc * a
Definition: textangle.C:12
An array of references to TObjects.
Definition: TRefArray.h:43
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:32
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:254
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:184
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:74
virtual void AddFirst(TObject *obj)
Add object in the first slot of the array.
Definition: TRefArray.cxx:267
UInt_t * fUIDs
Definition: TRefArray.h:49
const Bool_t kIterForward
Definition: TCollection.h:43
Bool_t fDirection
Definition: TRefArray.h:135
A TProcessID identifies a ROOT job in a unique way in time and space.
Definition: TProcessID.h:73
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:103
Int_t LowerBound() const
Definition: TRefArray.h:101
Int_t fCurCursor
Definition: TRefArray.h:133
TProcessID * GetPID() const
Definition: TRefArray.h:79
Iterator of object array.
Definition: TRefArray.h:126
Int_t fCursor
Definition: TRefArray.h:134
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
Int_t fLowerBound
Definition: TRefArray.h:50
TTime operator*(const TTime &t1, const TTime &t2)
Definition: TTime.h:87
Collection abstract base class.
Definition: TCollection.h:48
Int_t GetAbsLast() const
Return absolute index to last object in array.
Definition: TRefArray.cxx:603
unsigned int UInt_t
Definition: RtypesCore.h:42
Int_t fSize
Definition: TCollection.h:63
const TCollection * GetCollection() const
Definition: TRefArray.h:146
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:302
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
friend class TRefArrayIter
Definition: TRefArray.h:45
Bool_t BoundsOk(const char *where, Int_t at) const
Definition: TRefArray.h:163
virtual TObject * operator[](Int_t i) const
Definition: TRefArray.h:170
Mother of all ROOT objects.
Definition: TObject.h:37
const Int_t kMaxInt
Definition: Rtypes.h:103
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:48
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:81
TRefArrayIter Iterator_t
Definition: TRefArray.h:61
static Bool_t IsValid(TProcessID *pid)
static function. return kTRUE if pid is a valid TProcessID
Definition: TProcessID.cxx:330
Abstract base class defining a generic interface to the underlying Operating System.
Definition: TSystem.h:258
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual TObject * Remove(TObject *obj)
Remove object from array.
Definition: TRefArray.cxx:746
Int_t fLast
Definition: TRefArray.h:51
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