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