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 
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 
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
TRefArrayIter::fCursor
Int_t fCursor
Definition: TRefArray.h:130
TRefArray::Remove
virtual TObject * Remove(TObject *obj)
Remove object from array.
Definition: TRefArray.cxx:745
TRefArray::fLast
Int_t fLast
Definition: TRefArray.h:47
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TRefArray::BoundsOk
Bool_t BoundsOk(const char *where, Int_t at) const
Definition: TRefArray.h:159
TRefArray::AddFirst
virtual void AddFirst(TObject *obj)
Add object in the first slot of the array.
Definition: TRefArray.cxx:266
TRefArrayIter::~TRefArrayIter
~TRefArrayIter()
Definition: TRefArray.h:138
TRefArray::GetEntries
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TRefArray.cxx:588
TProcessID::IsValid
static Bool_t IsValid(TProcessID *pid)
static function. return kTRUE if pid is a valid TProcessID
Definition: TProcessID.cxx:360
TRefArray::IsEmpty
Bool_t IsEmpty() const
Definition: TRefArray.h:77
TRefArrayIter
Definition: TRefArray.h:122
TRefArray::GetObjectRef
TObject ** GetObjectRef(const TObject *obj) const
Return address of pointer obj.
Definition: TRefArray.cxx:630
TRefArray::GetPID
TProcessID * GetPID() const
Definition: TRefArray.h:75
TProcessID
Definition: TProcessID.h:74
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:168
TSystem
Definition: TSystem.h:266
Int_t
int Int_t
Definition: RtypesCore.h:45
TRefArray::Expand
virtual void Expand(Int_t newSize)
Expand or shrink the array to newSize elements.
Definition: TRefArray.cxx:473
TRefArray::fLowerBound
Int_t fLowerBound
Definition: TRefArray.h:46
TRefArray::Delete
virtual void Delete(Option_t *option="")
Remove all objects from the array and free the internal memory.
Definition: TRefArray.cxx:457
TRefArray::Clear
virtual void Clear(Option_t *option="")
Remove all objects from the array.
Definition: TRefArray.cxx:426
TRefArray
Definition: TRefArray.h:39
TRefArrayIter::GetCollection
const TCollection * GetCollection() const
Definition: TRefArray.h:142
TRefArray::GetFromTable
TObject * GetFromTable(Int_t idx) const
the reference may be in the TRefTable
Definition: TRefArray.cxx:498
TRefArrayIter::Reset
void Reset()
Reset array iterator.
Definition: TRefArray.cxx:925
TRefArrayIter::fCurCursor
Int_t fCurCursor
Definition: TRefArray.h:129
TRefArrayIter::TRefArrayIter
TRefArrayIter()
Definition: TRefArray.h:133
TRefArray::GetAbsLast
Int_t GetAbsLast() const
Return absolute index to last object in array.
Definition: TRefArray.cxx:602
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
bool
TIterator
Definition: TIterator.h:30
TRefArray::GetLast
Int_t GetLast() const
Return index of last object in array.
Definition: TRefArray.cxx:622
kIterForward
const Bool_t kIterForward
Definition: TCollection.h:40
TRefArray::AddAt
virtual void AddAt(TObject *obj, Int_t idx)
Add object at position ids.
Definition: TRefArray.cxx:358
TRefArray::AddAtAndExpand
virtual void AddAtAndExpand(TObject *obj, Int_t idx)
Add object at position idx.
Definition: TRefArray.cxx:335
TProcessID.h
TRefArrayIter::Next
TObject * Next()
Return next object in array. Returns 0 when no more objects in array.
Definition: TRefArray.cxx:898
Option_t
const typedef char Option_t
Definition: RtypesCore.h:66
TSeqCollection.h
TRefArray::operator[]
virtual TObject * operator[](Int_t i) const
Definition: TRefArray.h:166
TRefArrayIter::fDirection
Bool_t fDirection
Definition: TRefArray.h:131
TRefArray::IndexOf
Int_t IndexOf(const TObject *obj) const
Definition: TRefArray.cxx:658
TRefArray::GetEntriesFast
Int_t GetEntriesFast() const
Definition: TRefArray.h:70
TRefArray::fUIDs
UInt_t * fUIDs
Definition: TRefArray.h:45
a
auto * a
Definition: textangle.C:12
TRefArrayIter::operator!=
Bool_t operator!=(const TIterator &aIter) const
This operator compares two TIterator objects.
Definition: TRefArray.cxx:938
TRefArray::GetObjectUID
Bool_t GetObjectUID(Int_t &uid, TObject *obj, const char *methodname)
Private/static function, check for validity of pid.
Definition: TRefArray.cxx:207
TCollection::fSize
Int_t fSize
Definition: TCollection.h:148
TRefArrayIter::operator=
TIterator & operator=(const TIterator &rhs)
Overridden assignment operator.
Definition: TRefArray.cxx:869
TRefArray::BinarySearch
virtual Int_t BinarySearch(TObject *obj, Int_t upto=kMaxInt)
Find object using a binary search.
Definition: TRefArray.cxx:807
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
TRefArray::MakeIterator
TIterator * MakeIterator(Bool_t dir=kIterForward) const
Returns an array iterator.
Definition: TRefArray.cxx:703
TRefArray::~TRefArray
virtual ~TRefArray()
Usual destructor (The object pointed to by the array are never deleted).
Definition: TRefArray.cxx:196
TRefArray::Iterator_t
TRefArrayIter Iterator_t
Definition: TRefArray.h:57
unsigned int
TRefArray::AddBefore
virtual void AddBefore(const TObject *before, TObject *obj)
Add object in the slot before object before.
Definition: TRefArray.cxx:293
TSeqCollection
Definition: TSeqCollection.h:28
TRefArray::OutOfBoundsError
Bool_t OutOfBoundsError(const char *where, Int_t i) const
Generate an out-of-bounds error. Always returns false.
Definition: TRefArray.cxx:711
TRefArray::Init
void Init(Int_t s, Int_t lowerBound)
Initialize a TRefArray.
Definition: TRefArray.cxx:680
TRefArray::LowerBound
Int_t LowerBound() const
Definition: TRefArray.h:97
TRefArrayIter::fArray
const TRefArray * fArray
Definition: TRefArray.h:128
TRefArray::RemoveAt
virtual TObject * RemoveAt(Int_t idx)
Remove object at index idx.
Definition: TRefArray.cxx:720
TRefArray::GetUID
UInt_t GetUID(Int_t at) const
Return UID of element at.
Definition: TRefArray.cxx:640
TRefArray::Before
TObject * Before(const TObject *obj) const
Return the object before obj. Returns 0 if obj is first object.
Definition: TRefArray.cxx:413
TObject
Definition: TObject.h:37
TRefArray::Add
void Add(TObject *obj)
Definition: TRefArray.h:80
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TRefArrayIter::operator*
TObject * operator*() const
Return current object or nullptr.
Definition: TRefArray.cxx:958
TRefArray::At
TObject * At(Int_t idx) const
Definition: TRefArray.h:180
kMaxInt
const Int_t kMaxInt
Definition: RtypesCore.h:103
TRefArray::TRefArray
TRefArray(TProcessID *pid=0)
default constructor
Definition: TRefArray.cxx:109
TRefArray::Compress
virtual void Compress()
Remove empty slots from array.
Definition: TRefArray.cxx:438
TCollection
Definition: TCollection.h:63
TRefArray::operator=
TRefArray & operator=(const TRefArray &a)
Assignment operator.
Definition: TRefArray.cxx:172
TRefArray::After
TObject * After(const TObject *obj) const
Return the object after obj. Returns 0 if obj is last object.
Definition: TRefArray.cxx:400
TRefArray::Last
TObject * Last() const
Return the object in the last filled slot. Returns 0 if no entries.
Definition: TRefArray.cxx:573
TRefArray::AddAtFree
virtual Int_t AddAtFree(TObject *obj)
Return the position of the new object.
Definition: TRefArray.cxx:376
TProcessID::GetObjectWithID
TObject * GetObjectWithID(UInt_t uid)
returns the TObject with unique identifier uid in the table of objects
Definition: TProcessID.cxx:332
TRefArray::First
TObject * First() const
Return the object in the first slot.
Definition: TRefArray.cxx:565
TRefArray::SetLast
void SetLast(Int_t last)
Set index of last object in array, effectively truncating the array.
Definition: TRefArray.cxx:771
TRefArray::fPID
TProcessID * fPID
Definition: TRefArray.h:44
TRefArray::AddLast
virtual void AddLast(TObject *obj)
Add object in the next empty slot in the array.
Definition: TRefArray.cxx:282
TRefArray::Sort
virtual void Sort(Int_t upto=kMaxInt)
If objects in array are sortable (i.e.
Definition: TRefArray.cxx:783
TRefArray::AddAfter
virtual void AddAfter(const TObject *after, TObject *obj)
Add object in the slot after object after.
Definition: TRefArray.cxx:317
int