1 |
//--------------------------------------------------------------------------------------------------
|
2 |
// $Id: StackArrayBasic.h,v 1.9 2009/03/23 13:07:18 loizides Exp $
|
3 |
//
|
4 |
// StackArrayBasic
|
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 basic data types as opposed to StackArray which can hold
|
10 |
// classes. Note that this array as opposed to the others does not
|
11 |
// derive from the templated Collection interface.
|
12 |
//
|
13 |
// Authors: C.Loizides, J.Bendavid
|
14 |
//--------------------------------------------------------------------------------------------------
|
15 |
|
16 |
#ifndef MITANA_DATACONT_STACKARRAYBASIC_H
|
17 |
#define MITANA_DATACONT_STACKARRAYBASIC_H
|
18 |
|
19 |
#include <TObject.h>
|
20 |
#include "MitAna/DataCont/interface/BaseCollection.h"
|
21 |
|
22 |
namespace mithep
|
23 |
{
|
24 |
template<class ArrayElement, UInt_t N>
|
25 |
class StackArrayBasic : public BaseCollection
|
26 |
{
|
27 |
public:
|
28 |
StackArrayBasic();
|
29 |
StackArrayBasic(const StackArrayBasic &a);
|
30 |
|
31 |
void Add(const ArrayElement &ae);
|
32 |
ArrayElement At(UInt_t idx);
|
33 |
const ArrayElement At(UInt_t idx) const;
|
34 |
UInt_t Entries() const { return fSize; }
|
35 |
UInt_t GetEntries() const { return fSize; }
|
36 |
UInt_t GetSize() const { return N; }
|
37 |
Bool_t IsOwner() const { return kTRUE; }
|
38 |
TObject *ObjAt(UInt_t idx) { return 0; }
|
39 |
const TObject *ObjAt(UInt_t idx) const { return 0; }
|
40 |
void Reset();
|
41 |
void Trim() {}
|
42 |
ArrayElement UncheckedAt(UInt_t idx);
|
43 |
const ArrayElement UncheckedAt(UInt_t idx) const;
|
44 |
ArrayElement operator[](UInt_t idx);
|
45 |
const ArrayElement operator[](UInt_t idx) const;
|
46 |
|
47 |
protected:
|
48 |
UShort_t fSize; //size of array
|
49 |
ArrayElement fArray[N]; //storage for basic types
|
50 |
|
51 |
ClassDef(StackArrayBasic, 1) // Array on stack for basic types
|
52 |
};
|
53 |
}
|
54 |
|
55 |
//--------------------------------------------------------------------------------------------------
|
56 |
template<class ArrayElement, UInt_t N>
|
57 |
inline mithep::StackArrayBasic<ArrayElement, N>::StackArrayBasic() :
|
58 |
fSize(0)
|
59 |
{
|
60 |
// Default constructor.
|
61 |
}
|
62 |
|
63 |
//--------------------------------------------------------------------------------------------------
|
64 |
template<class ArrayElement, UInt_t N>
|
65 |
inline mithep::StackArrayBasic<ArrayElement, N>::StackArrayBasic(const StackArrayBasic &a) :
|
66 |
fSize(a.fSize)
|
67 |
{
|
68 |
// Copy constructor. Copy only elements which are used.
|
69 |
|
70 |
for (UInt_t i=0; i<fSize; ++i)
|
71 |
fArray[i] = a.fArray[i];
|
72 |
}
|
73 |
|
74 |
//--------------------------------------------------------------------------------------------------
|
75 |
template<class ArrayElement, UInt_t N>
|
76 |
void mithep::StackArrayBasic<ArrayElement, N>::Add(const ArrayElement &ae)
|
77 |
{
|
78 |
// Add element to array.
|
79 |
|
80 |
if (fSize>=N) {
|
81 |
TObject::Fatal("Add", "Maximum number of slots reached (%d>=%d): "
|
82 |
"To support more requires a different template!", fSize, N);
|
83 |
return;
|
84 |
}
|
85 |
|
86 |
fArray[fSize] = ae;
|
87 |
++fSize;
|
88 |
BaseCollection::Clear();
|
89 |
}
|
90 |
|
91 |
//--------------------------------------------------------------------------------------------------
|
92 |
template<class ArrayElement, UInt_t N>
|
93 |
inline ArrayElement mithep::StackArrayBasic<ArrayElement, N>::At(UInt_t idx)
|
94 |
{
|
95 |
// Return entry at given index.
|
96 |
|
97 |
if (idx<fSize)
|
98 |
return fArray[idx];
|
99 |
|
100 |
ArrayElement tmp;
|
101 |
TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
|
102 |
idx, fSize, GetName(), typeid(tmp).name());
|
103 |
return 0;
|
104 |
}
|
105 |
|
106 |
//--------------------------------------------------------------------------------------------------
|
107 |
template<class ArrayElement, UInt_t N>
|
108 |
inline const ArrayElement mithep::StackArrayBasic<ArrayElement, N>::At(UInt_t idx) const
|
109 |
{
|
110 |
// Return entry at given index.
|
111 |
|
112 |
if (idx<fSize)
|
113 |
return fArray[idx];
|
114 |
|
115 |
ArrayElement tmp;
|
116 |
TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
|
117 |
idx, fSize, GetName(), typeid(tmp).name());
|
118 |
return 0;
|
119 |
}
|
120 |
|
121 |
//-------------------------------------------------------------------------------------------------
|
122 |
template<class ArrayElement, UInt_t N>
|
123 |
void mithep::StackArrayBasic<ArrayElement, N>::Reset()
|
124 |
{
|
125 |
// Reset this array.
|
126 |
|
127 |
fSize = 0;
|
128 |
BaseCollection::Clear();
|
129 |
}
|
130 |
|
131 |
//-------------------------------------------------------------------------------------------------
|
132 |
template<class ArrayElement, UInt_t N>
|
133 |
void mithep::StackArrayBasic<ArrayElement, N>::Streamer(TBuffer &b)
|
134 |
{
|
135 |
// Stream all objects in the array to or from the I/O buffer.
|
136 |
|
137 |
if (b.IsReading()) {
|
138 |
b >> fSize;
|
139 |
if (fSize) {
|
140 |
b.ReadFastArray(fArray,fSize);
|
141 |
}
|
142 |
} else { /*writing*/
|
143 |
b << fSize;
|
144 |
if (fSize) {
|
145 |
b.WriteFastArray(fArray,fSize);
|
146 |
}
|
147 |
}
|
148 |
}
|
149 |
|
150 |
//--------------------------------------------------------------------------------------------------
|
151 |
template<class ArrayElement, UInt_t N>
|
152 |
inline ArrayElement mithep::StackArrayBasic<ArrayElement, N>::UncheckedAt(UInt_t idx)
|
153 |
{
|
154 |
// Return entry at given index.
|
155 |
|
156 |
return fArray[idx];
|
157 |
}
|
158 |
|
159 |
//--------------------------------------------------------------------------------------------------
|
160 |
template<class ArrayElement, UInt_t N>
|
161 |
inline const ArrayElement mithep::StackArrayBasic<ArrayElement, N>::UncheckedAt(UInt_t idx) const
|
162 |
{
|
163 |
// Return entry at given index.
|
164 |
|
165 |
return fArray[idx];
|
166 |
}
|
167 |
|
168 |
//--------------------------------------------------------------------------------------------------
|
169 |
template<class ArrayElement, UInt_t N>
|
170 |
inline const ArrayElement mithep::StackArrayBasic<ArrayElement, N>::operator[](UInt_t idx) const
|
171 |
{
|
172 |
// Return entry at given index.
|
173 |
|
174 |
return At(idx);
|
175 |
}
|
176 |
|
177 |
//--------------------------------------------------------------------------------------------------
|
178 |
template<class ArrayElement, UInt_t N>
|
179 |
inline ArrayElement mithep::StackArrayBasic<ArrayElement, N>::operator[](UInt_t idx)
|
180 |
{
|
181 |
// Return entry at given index.
|
182 |
|
183 |
return At(idx);
|
184 |
}
|
185 |
#endif
|