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