1 |
//--------------------------------------------------------------------------------------------------
|
2 |
// $Id: FastArrayBasic.h,v 1.10 2009/04/06 13:44:08 loizides Exp $
|
3 |
//
|
4 |
// FastArrayBasic
|
5 |
//
|
6 |
// Implementation of a "fast" array on the heap: Memory is dynamically allocated,
|
7 |
// but there is an optimization in the read streamer similar to the TClonesArray
|
8 |
// where the heap memory of an existing object is reused.
|
9 |
// This class is meant to be used as a datamember for objects which are contained
|
10 |
// inside a TClonesArray. For various reasons, the array cannot be written in split mode.
|
11 |
// This means you have to make sure that you declare it using "||" if you write out with
|
12 |
// high split level.
|
13 |
// Array is meant to store basic data types as opposed to FastArray
|
14 |
// which can hold arbitrary (non-heap using) classes.
|
15 |
// Since it stores basic types it can not derive from the Collection<ArrayElement>
|
16 |
// interface, or else the At() member functions would have to return pointers to
|
17 |
// basic elements. Something we did not want to do.
|
18 |
//
|
19 |
// Authors: J.Bendavid
|
20 |
//--------------------------------------------------------------------------------------------------
|
21 |
|
22 |
#ifndef MITANA_DATACONT_FASTARRAYBASIC_H
|
23 |
#define MITANA_DATACONT_FASTARRAYBASIC_H
|
24 |
|
25 |
#include <TObject.h>
|
26 |
#include <TClass.h>
|
27 |
#include <TStorage.h>
|
28 |
#include "MitAna/DataCont/interface/Collection.h"
|
29 |
|
30 |
typedef UInt_t unsignedint;
|
31 |
typedef UChar_t unsignedchar;
|
32 |
typedef UShort_t unsignedshort;
|
33 |
typedef ULong64_t unsignedlonglong;
|
34 |
|
35 |
namespace mithep
|
36 |
{
|
37 |
template<class ArrayElement, Bool_t IsDouble32 = kFALSE>
|
38 |
class FastArrayBasic : public BaseCollection
|
39 |
{
|
40 |
public:
|
41 |
FastArrayBasic(UShort_t icap=0);
|
42 |
FastArrayBasic(const FastArrayBasic &a);
|
43 |
~FastArrayBasic() { Init(0); }
|
44 |
|
45 |
Int_t Add(const ArrayElement &ae);
|
46 |
ArrayElement At(UInt_t idx);
|
47 |
const ArrayElement At(UInt_t idx) const;
|
48 |
void Clear(Option_t */*opt*/="") { fSize=0; Init(0); }
|
49 |
UInt_t Entries() const { return fSize; }
|
50 |
UInt_t GetEntries() const { return fSize; }
|
51 |
UInt_t GetSize() const { return fCapacity; }
|
52 |
Bool_t IsOwner() const { return kTRUE; }
|
53 |
TObject *ObjAt(UInt_t /*idx*/) { return 0; }
|
54 |
const TObject *ObjAt(UInt_t /*idx*/) const { return 0; }
|
55 |
void Reset();
|
56 |
void Trim() { Expand(fSize); }
|
57 |
ArrayElement UncheckedAt(UInt_t idx);
|
58 |
const ArrayElement UncheckedAt(UInt_t idx) const;
|
59 |
ArrayElement operator[](UInt_t idx);
|
60 |
const ArrayElement operator[](UInt_t idx) const;
|
61 |
|
62 |
protected:
|
63 |
void Init(UShort_t s);
|
64 |
void Expand(UShort_t s);
|
65 |
|
66 |
UShort_t fSize; //size of array
|
67 |
UShort_t fCapacity; //!size of heap allocated
|
68 |
ArrayElement *fArray; //!heap storage for objects
|
69 |
|
70 |
ClassDef(FastArrayBasic,1) // Fast array for basic types
|
71 |
};
|
72 |
}
|
73 |
|
74 |
//--------------------------------------------------------------------------------------------------
|
75 |
template<class ArrayElement, Bool_t IsDouble32>
|
76 |
inline mithep::FastArrayBasic<ArrayElement, IsDouble32>::FastArrayBasic(UShort_t icap) :
|
77 |
fSize(0),
|
78 |
fCapacity(0),
|
79 |
fArray(0)
|
80 |
{
|
81 |
// Default constructor.
|
82 |
|
83 |
if (icap)
|
84 |
Init(icap);
|
85 |
}
|
86 |
|
87 |
//--------------------------------------------------------------------------------------------------
|
88 |
template<class ArrayElement, Bool_t IsDouble32>
|
89 |
inline mithep::FastArrayBasic<ArrayElement, IsDouble32>::FastArrayBasic(const FastArrayBasic &a) :
|
90 |
fSize(0),
|
91 |
fCapacity(0),
|
92 |
fArray(0)
|
93 |
{
|
94 |
// Copy constructor. Copy only elements which are used.
|
95 |
|
96 |
Init(a.fSize);
|
97 |
for (UInt_t i=0; i<a.fSize; ++i)
|
98 |
Add(a.fArray[i]);
|
99 |
}
|
100 |
|
101 |
//--------------------------------------------------------------------------------------------------
|
102 |
template<class ArrayElement, Bool_t IsDouble32>
|
103 |
Int_t mithep::FastArrayBasic<ArrayElement, IsDouble32>::Add(const ArrayElement &ae)
|
104 |
{
|
105 |
// Add a copy of an existing object.
|
106 |
|
107 |
if (fSize >= fCapacity)
|
108 |
Expand(TMath::Max(16,2*fCapacity));
|
109 |
|
110 |
++fSize;
|
111 |
fArray[fSize-1] = ae;
|
112 |
BaseCollection::Clear();
|
113 |
|
114 |
return fSize;
|
115 |
}
|
116 |
|
117 |
//--------------------------------------------------------------------------------------------------
|
118 |
template<class ArrayElement, Bool_t IsDouble32>
|
119 |
inline ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::At(UInt_t idx)
|
120 |
{
|
121 |
// Return entry at given index.
|
122 |
|
123 |
if (idx<fSize)
|
124 |
return fArray[idx];
|
125 |
|
126 |
ArrayElement tmp;
|
127 |
TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
|
128 |
idx, fSize, this->GetName(), typeid(tmp).name());
|
129 |
return 0;
|
130 |
}
|
131 |
|
132 |
//--------------------------------------------------------------------------------------------------
|
133 |
template<class ArrayElement, Bool_t IsDouble32>
|
134 |
inline const ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::At(UInt_t idx) const
|
135 |
{
|
136 |
// Return entry at given index.
|
137 |
|
138 |
if (idx<fSize)
|
139 |
return fArray[idx];
|
140 |
|
141 |
ArrayElement tmp;
|
142 |
TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
|
143 |
idx, fSize, this->GetName(), typeid(tmp).name());
|
144 |
return 0;
|
145 |
}
|
146 |
|
147 |
//--------------------------------------------------------------------------------------------------
|
148 |
template<class ArrayElement, Bool_t IsDouble32>
|
149 |
inline void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Expand(UShort_t s)
|
150 |
{
|
151 |
|
152 |
// Expand or shrink the array to given number of elements.
|
153 |
|
154 |
if (s < fSize) {
|
155 |
TObject::Fatal("Expand", "Cannot shrink FastArrayBasic to less than fSize");
|
156 |
return;
|
157 |
}
|
158 |
|
159 |
if (!fArray || s==0) {
|
160 |
Init(s);
|
161 |
return;
|
162 |
}
|
163 |
|
164 |
if (fCapacity == s)
|
165 |
return;
|
166 |
|
167 |
fArray = static_cast<ArrayElement*>(TStorage::ReAlloc(fArray, s * sizeof(ArrayElement),
|
168 |
fCapacity * sizeof(ArrayElement)));
|
169 |
fCapacity = s;
|
170 |
}
|
171 |
|
172 |
//--------------------------------------------------------------------------------------------------
|
173 |
template<class ArrayElement, Bool_t IsDouble32>
|
174 |
inline void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Init(UShort_t s)
|
175 |
{
|
176 |
|
177 |
// Initialize the array the heap.
|
178 |
|
179 |
if (fArray && fCapacity != s) {
|
180 |
TStorage::Dealloc(fArray);
|
181 |
fArray = 0;
|
182 |
}
|
183 |
|
184 |
fCapacity = s;
|
185 |
|
186 |
if (!fArray && fCapacity > 0)
|
187 |
fArray = static_cast<ArrayElement*>(TStorage::Alloc(fCapacity*sizeof(ArrayElement)));
|
188 |
}
|
189 |
|
190 |
//-------------------------------------------------------------------------------------------------
|
191 |
template<class ArrayElement, Bool_t IsDouble32>
|
192 |
void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Reset()
|
193 |
{
|
194 |
// Reset this array.
|
195 |
|
196 |
fSize = 0;
|
197 |
BaseCollection::Clear();
|
198 |
}
|
199 |
|
200 |
//-------------------------------------------------------------------------------------------------
|
201 |
template<class ArrayElement, Bool_t IsDouble32>
|
202 |
void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Streamer(TBuffer &b)
|
203 |
{
|
204 |
// Stream all objects in the array to or from the I/O buffer.
|
205 |
// Ugly special case handling for Double32
|
206 |
|
207 |
if (b.IsReading()) {
|
208 |
b >> fSize;
|
209 |
if (fSize) {
|
210 |
if (fSize > fCapacity)
|
211 |
Expand(TMath::Max(static_cast<Int_t>(fSize),2*fCapacity));
|
212 |
|
213 |
if (IsDouble32)
|
214 |
b.ReadFastArrayDouble32(reinterpret_cast<Double_t*>(fArray),fSize);
|
215 |
else
|
216 |
b.ReadFastArray(fArray,fSize);
|
217 |
BaseCollection::Clear();
|
218 |
}
|
219 |
} else { /*writing*/
|
220 |
b << fSize;
|
221 |
if (fSize) {
|
222 |
if (IsDouble32)
|
223 |
b.WriteFastArrayDouble32(reinterpret_cast<Double_t*>(fArray),fSize);
|
224 |
else
|
225 |
b.WriteFastArray(fArray,fSize);
|
226 |
}
|
227 |
}
|
228 |
}
|
229 |
|
230 |
//--------------------------------------------------------------------------------------------------
|
231 |
template<class ArrayElement, Bool_t IsDouble32>
|
232 |
inline ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::UncheckedAt(UInt_t idx)
|
233 |
{
|
234 |
// Return entry at given index.
|
235 |
|
236 |
return fArray[idx];
|
237 |
}
|
238 |
|
239 |
//--------------------------------------------------------------------------------------------------
|
240 |
template<class ArrayElement, Bool_t IsDouble32>
|
241 |
inline const ArrayElement
|
242 |
mithep::FastArrayBasic<ArrayElement, IsDouble32>::UncheckedAt(UInt_t idx) const
|
243 |
{
|
244 |
// Return entry at given index.
|
245 |
|
246 |
return fArray[idx];
|
247 |
}
|
248 |
|
249 |
//--------------------------------------------------------------------------------------------------
|
250 |
template<class ArrayElement, Bool_t IsDouble32>
|
251 |
inline const ArrayElement
|
252 |
mithep::FastArrayBasic<ArrayElement, IsDouble32>::operator[](UInt_t idx) const
|
253 |
{
|
254 |
// Return entry at given index.
|
255 |
|
256 |
return At(idx);
|
257 |
}
|
258 |
|
259 |
//--------------------------------------------------------------------------------------------------
|
260 |
template<class ArrayElement, Bool_t IsDouble32>
|
261 |
inline ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::operator[](UInt_t idx)
|
262 |
{
|
263 |
// Return entry at given index.
|
264 |
|
265 |
return At(idx);
|
266 |
}
|
267 |
#endif
|