1 |
bendavid |
1.1 |
//--------------------------------------------------------------------------------------------------
|
2 |
bendavid |
1.5 |
// $Id: StackArray.h,v 1.4 2008/11/20 17:49:15 loizides Exp $
|
3 |
bendavid |
1.1 |
//
|
4 |
|
|
// StackArray
|
5 |
|
|
//
|
6 |
loizides |
1.4 |
// Implementation of an array on the stack as opposed to on the heap
|
7 |
|
|
// memory. For various reasons, the array can not be written in split
|
8 |
|
|
// mode. Maximum size is set by template parameter. Array is meant to
|
9 |
|
|
// store classes as opposed to StackArrayBasic which should be used to
|
10 |
|
|
// hold basic types.
|
11 |
bendavid |
1.1 |
//
|
12 |
|
|
// Authors: C.Loizides, J.Bendavid
|
13 |
|
|
//--------------------------------------------------------------------------------------------------
|
14 |
|
|
|
15 |
|
|
#ifndef MITANA_DATACONT_STACKARRAY
|
16 |
|
|
#define MITANA_DATACONT_STACKARRAY
|
17 |
|
|
|
18 |
|
|
#include <TObject.h>
|
19 |
|
|
#include <TClass.h>
|
20 |
|
|
#include "MitAna/DataCont/interface/Collection.h"
|
21 |
|
|
|
22 |
|
|
namespace mithep
|
23 |
|
|
{
|
24 |
|
|
template<class ArrayElement, UInt_t N>
|
25 |
|
|
class StackArray : public Collection<ArrayElement>
|
26 |
|
|
{
|
27 |
|
|
public:
|
28 |
|
|
StackArray();
|
29 |
bendavid |
1.3 |
StackArray(const StackArray &a);
|
30 |
bendavid |
1.1 |
~StackArray() {}
|
31 |
|
|
|
32 |
|
|
void AddCopy(const ArrayElement &ae);
|
33 |
|
|
ArrayElement *AddNew();
|
34 |
|
|
ArrayElement *Allocate();
|
35 |
|
|
ArrayElement *At(UInt_t idx);
|
36 |
|
|
const ArrayElement *At(UInt_t idx) const;
|
37 |
|
|
void Clear(Option_t */*opt*/="") {}
|
38 |
|
|
UInt_t Entries() const { return GetEntries(); }
|
39 |
|
|
UInt_t GetEntries() const { return fSize; }
|
40 |
loizides |
1.4 |
UInt_t GetSize() const { return N; }
|
41 |
bendavid |
1.5 |
Bool_t HasObject(const ArrayElement *obj) const;
|
42 |
bendavid |
1.1 |
Bool_t IsOwner() const { return kTRUE; }
|
43 |
|
|
void Reset() { fSize = 0; }
|
44 |
|
|
void Trim() {}
|
45 |
|
|
ArrayElement *UncheckedAt(UInt_t idx);
|
46 |
|
|
const ArrayElement *UncheckedAt(UInt_t idx) const;
|
47 |
|
|
ArrayElement *operator[](UInt_t idx);
|
48 |
|
|
const ArrayElement *operator[](UInt_t idx) const;
|
49 |
|
|
|
50 |
|
|
protected:
|
51 |
loizides |
1.2 |
TClass *fClass; //!pointer to TClass object used by streamer
|
52 |
|
|
UShort_t fSize; //size of array
|
53 |
loizides |
1.4 |
ArrayElement fArray[N]; //storage for objects
|
54 |
bendavid |
1.1 |
|
55 |
loizides |
1.4 |
ClassDef(StackArray,1) // Array on stack for arbitrary classes
|
56 |
bendavid |
1.1 |
};
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
//--------------------------------------------------------------------------------------------------
|
60 |
|
|
template<class ArrayElement, UInt_t N>
|
61 |
|
|
inline mithep::StackArray<ArrayElement, N>::StackArray() :
|
62 |
bendavid |
1.3 |
fClass(TClass::GetClass(typeid(ArrayElement))),
|
63 |
bendavid |
1.1 |
fSize(0)
|
64 |
|
|
{
|
65 |
|
|
// Default constructor.
|
66 |
bendavid |
1.3 |
}
|
67 |
|
|
|
68 |
|
|
//--------------------------------------------------------------------------------------------------
|
69 |
|
|
template<class ArrayElement, UInt_t N>
|
70 |
|
|
inline mithep::StackArray<ArrayElement, N>::StackArray(const StackArray &a) :
|
71 |
|
|
fClass(a.fClass),
|
72 |
|
|
fSize(a.fSize)
|
73 |
|
|
{
|
74 |
loizides |
1.4 |
// Copy constructor. Copy only elements which are used.
|
75 |
|
|
|
76 |
bendavid |
1.3 |
for (UInt_t i=0; i<fSize; ++i)
|
77 |
|
|
fArray[i] = a.fArray[i];
|
78 |
bendavid |
1.1 |
}
|
79 |
|
|
|
80 |
|
|
//--------------------------------------------------------------------------------------------------
|
81 |
|
|
template<class ArrayElement, UInt_t N>
|
82 |
|
|
void mithep::StackArray<ArrayElement, N>::AddCopy(const ArrayElement &ae)
|
83 |
|
|
{
|
84 |
|
|
// Add a copy of an existing object.
|
85 |
|
|
|
86 |
|
|
if(fSize>=N) {
|
87 |
loizides |
1.4 |
TObject::Fatal("AddCopy", "Maximum number of slots reached (%d>=%d): "
|
88 |
|
|
"To support more requires a different template!", fSize, N);
|
89 |
bendavid |
1.1 |
return;
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
fArray[fSize] = ae;
|
93 |
|
|
++fSize;
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
//--------------------------------------------------------------------------------------------------
|
97 |
|
|
template<class ArrayElement, UInt_t N>
|
98 |
|
|
ArrayElement* mithep::StackArray<ArrayElement, N>::AddNew()
|
99 |
|
|
{
|
100 |
|
|
// Add new object.
|
101 |
|
|
|
102 |
|
|
return new(Allocate()) ArrayElement();
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
//--------------------------------------------------------------------------------------------------
|
106 |
|
|
template<class ArrayElement, UInt_t N>
|
107 |
|
|
ArrayElement* mithep::StackArray<ArrayElement, N>::Allocate()
|
108 |
|
|
{
|
109 |
loizides |
1.4 |
// Return next slot in the array, *only* to be used in placement new operator.
|
110 |
bendavid |
1.1 |
|
111 |
|
|
if(fSize>=N) {
|
112 |
loizides |
1.4 |
TObject::Fatal("Allocate", "Maximum number of slots reached (%d>=%d): "
|
113 |
|
|
"To support more requires a different template!", fSize, N);
|
114 |
bendavid |
1.1 |
return 0;
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
++fSize;
|
118 |
|
|
return &fArray[fSize-1];
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
//--------------------------------------------------------------------------------------------------
|
122 |
|
|
template<class ArrayElement, UInt_t N>
|
123 |
|
|
inline ArrayElement *mithep::StackArray<ArrayElement, N>::At(UInt_t idx)
|
124 |
|
|
{
|
125 |
|
|
// Return entry at given index.
|
126 |
|
|
|
127 |
|
|
if(idx<fSize)
|
128 |
|
|
return static_cast<ArrayElement*>(&fArray[idx]);
|
129 |
|
|
|
130 |
loizides |
1.4 |
ArrayElement tmp;
|
131 |
|
|
TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
|
132 |
|
|
idx, fSize, this->GetName(), typeid(tmp).name());
|
133 |
bendavid |
1.1 |
return 0;
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
//--------------------------------------------------------------------------------------------------
|
137 |
|
|
template<class ArrayElement, UInt_t N>
|
138 |
|
|
inline const ArrayElement *mithep::StackArray<ArrayElement, N>::At(UInt_t idx) const
|
139 |
|
|
{
|
140 |
|
|
// Return entry at given index.
|
141 |
|
|
|
142 |
|
|
if(idx<fSize)
|
143 |
|
|
return static_cast<const ArrayElement*>(&fArray[idx]);
|
144 |
|
|
|
145 |
loizides |
1.4 |
ArrayElement tmp;
|
146 |
|
|
TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
|
147 |
|
|
idx, fSize, this->GetName(), typeid(tmp).name());
|
148 |
bendavid |
1.1 |
return 0;
|
149 |
|
|
}
|
150 |
|
|
|
151 |
bendavid |
1.5 |
//--------------------------------------------------------------------------------------------------
|
152 |
|
|
template<class ArrayElement, UInt_t N>
|
153 |
|
|
inline Bool_t mithep::StackArray<ArrayElement, N>::HasObject(const ArrayElement *obj) const
|
154 |
|
|
{
|
155 |
|
|
// Check whether object is in array.
|
156 |
|
|
|
157 |
|
|
// Check whether object is in array. If ArrayElement inherits from TObject, use the
|
158 |
|
|
// isEqual function for the comparison, otherwise use the default pointer comparison.
|
159 |
|
|
|
160 |
|
|
const TObject *tobj = 0;
|
161 |
|
|
if (TClass::GetClass(typeid(ArrayElement))->IsTObject())
|
162 |
|
|
tobj = reinterpret_cast<const TObject*>(obj);
|
163 |
|
|
|
164 |
|
|
for (UInt_t i=0; i<fSize; ++i) {
|
165 |
|
|
if (tobj)
|
166 |
|
|
if ( reinterpret_cast<const TObject*>(&fArray[i])->IsEqual(tobj) )
|
167 |
|
|
return true;
|
168 |
|
|
else if ( &fArray[i] == obj )
|
169 |
|
|
return true;
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
return false;
|
173 |
|
|
}
|
174 |
|
|
|
175 |
bendavid |
1.1 |
//-------------------------------------------------------------------------------------------------
|
176 |
|
|
template<class ArrayElement, UInt_t N>
|
177 |
|
|
void mithep::StackArray<ArrayElement, N>::Streamer(TBuffer &b)
|
178 |
|
|
{
|
179 |
|
|
// Stream all objects in the array to or from the I/O buffer.
|
180 |
|
|
|
181 |
|
|
if (b.IsReading()) {
|
182 |
|
|
b >> fSize;
|
183 |
|
|
if (fSize) {
|
184 |
|
|
b.ReadFastArray(fArray,fClass,fSize);
|
185 |
|
|
}
|
186 |
|
|
} else { /*writing*/
|
187 |
|
|
b << fSize;
|
188 |
|
|
if (fSize) {
|
189 |
|
|
b.WriteFastArray(fArray,fClass,fSize);
|
190 |
|
|
}
|
191 |
|
|
}
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
//--------------------------------------------------------------------------------------------------
|
195 |
|
|
template<class ArrayElement, UInt_t N>
|
196 |
|
|
inline ArrayElement *mithep::StackArray<ArrayElement, N>::UncheckedAt(UInt_t idx)
|
197 |
|
|
{
|
198 |
|
|
// Return entry at given index.
|
199 |
|
|
|
200 |
|
|
return static_cast<ArrayElement*>(&fArray[idx]);
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
//--------------------------------------------------------------------------------------------------
|
204 |
|
|
template<class ArrayElement, UInt_t N>
|
205 |
|
|
inline const ArrayElement *mithep::StackArray<ArrayElement, N>::UncheckedAt(UInt_t idx) const
|
206 |
|
|
{
|
207 |
|
|
// Return entry at given index.
|
208 |
|
|
|
209 |
|
|
return static_cast<const ArrayElement*>(&fArray[idx]);
|
210 |
|
|
}
|
211 |
loizides |
1.4 |
|
212 |
|
|
//--------------------------------------------------------------------------------------------------
|
213 |
|
|
template<class ArrayElement, UInt_t N>
|
214 |
|
|
inline const ArrayElement *mithep::StackArray<ArrayElement, N>::operator[](UInt_t idx) const
|
215 |
|
|
{
|
216 |
|
|
// Return entry at given index.
|
217 |
|
|
|
218 |
|
|
return At(idx);
|
219 |
|
|
}
|
220 |
|
|
|
221 |
|
|
//--------------------------------------------------------------------------------------------------
|
222 |
|
|
template<class ArrayElement, UInt_t N>
|
223 |
|
|
inline ArrayElement *mithep::StackArray<ArrayElement, N>::operator[](UInt_t idx)
|
224 |
|
|
{
|
225 |
|
|
// Return entry at given index.
|
226 |
|
|
|
227 |
|
|
return At(idx);
|
228 |
|
|
}
|
229 |
bendavid |
1.1 |
#endif
|