1 |
bendavid |
1.1 |
//--------------------------------------------------------------------------------------------------
|
2 |
|
|
// $Id: StackArrayBasic.h,v 1.1 2008/09/19 11:56:08 bendavid Exp $
|
3 |
|
|
//
|
4 |
|
|
// StackArrayBasic
|
5 |
|
|
//
|
6 |
|
|
// Implementation of a TStackArrayBasic using stack (and not heap) memory.
|
7 |
|
|
// For various reasons, the array can not be written in split mode.
|
8 |
|
|
// Maximum size of references is set to 1024 (but this could be
|
9 |
|
|
// changed if there is need for it).
|
10 |
|
|
//
|
11 |
|
|
// Authors: C.Loizides, J.Bendavid
|
12 |
|
|
//--------------------------------------------------------------------------------------------------
|
13 |
|
|
|
14 |
|
|
#ifndef MITANA_DATACONT_STACKARRAYBASIC
|
15 |
|
|
#define MITANA_DATACONT_STACKARRAYBASIC
|
16 |
|
|
|
17 |
|
|
#include <TObject.h>
|
18 |
|
|
#include <TError.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 StackArrayBasic : public TObject
|
26 |
|
|
{
|
27 |
|
|
public:
|
28 |
|
|
StackArrayBasic();
|
29 |
|
|
StackArrayBasic(const StackArrayBasic &a);
|
30 |
|
|
~StackArrayBasic() {}
|
31 |
|
|
|
32 |
|
|
void Add(const ArrayElement &ae);
|
33 |
|
|
ArrayElement At(UInt_t idx);
|
34 |
|
|
const ArrayElement At(UInt_t idx) const;
|
35 |
|
|
void Clear(Option_t */*opt*/="") {}
|
36 |
|
|
UInt_t Entries() const { return GetEntries(); }
|
37 |
|
|
UInt_t GetEntries() const { return fSize; }
|
38 |
|
|
Bool_t IsOwner() const { return kTRUE; }
|
39 |
|
|
void Reset() { fSize = 0; }
|
40 |
|
|
void Trim() {}
|
41 |
|
|
ArrayElement UncheckedAt(UInt_t idx);
|
42 |
|
|
const ArrayElement UncheckedAt(UInt_t idx) const;
|
43 |
|
|
|
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 of uids of referenced objects
|
50 |
|
|
|
51 |
|
|
ClassDef(StackArrayBasic,1) // Implementation of our own TStackArrayBasic
|
52 |
|
|
};
|
53 |
|
|
}
|
54 |
|
|
|
55 |
|
|
//--------------------------------------------------------------------------------------------------
|
56 |
|
|
template<class ArrayElement, UInt_t N>
|
57 |
|
|
inline mithep::StackArrayBasic<ArrayElement, N>::StackArrayBasic() :
|
58 |
|
|
fSize(0)
|
59 |
|
|
{
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
//--------------------------------------------------------------------------------------------------
|
63 |
|
|
template<class ArrayElement, UInt_t N>
|
64 |
|
|
inline mithep::StackArrayBasic<ArrayElement, N>::StackArrayBasic(const StackArrayBasic &a) :
|
65 |
|
|
fSize(a.fSize)
|
66 |
|
|
{
|
67 |
|
|
//Copy constructor. Copy only elements which are used.
|
68 |
|
|
for (UInt_t i=0; i<fSize; ++i)
|
69 |
|
|
fArray[i] = a.fArray[i];
|
70 |
|
|
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
//--------------------------------------------------------------------------------------------------
|
74 |
|
|
template<class ArrayElement, UInt_t N>
|
75 |
|
|
void mithep::StackArrayBasic<ArrayElement, N>::Add(const ArrayElement &ae)
|
76 |
|
|
{
|
77 |
|
|
// Add a copy of an existing object.
|
78 |
|
|
|
79 |
|
|
if(fSize>=N) {
|
80 |
|
|
Fatal("Add", "Maximum number of references reached: To support more requires change in code!");
|
81 |
|
|
return;
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
fArray[fSize] = ae;
|
85 |
|
|
++fSize;
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
//--------------------------------------------------------------------------------------------------
|
89 |
|
|
template<class ArrayElement, UInt_t N>
|
90 |
|
|
inline ArrayElement mithep::StackArrayBasic<ArrayElement, N>::At(UInt_t idx)
|
91 |
|
|
{
|
92 |
|
|
// Return entry at given index.
|
93 |
|
|
|
94 |
|
|
if(idx<fSize)
|
95 |
|
|
return fArray[idx];
|
96 |
|
|
|
97 |
|
|
Error("At", "Given index (%ud) is larger than array size (%ud)", idx, fSize);
|
98 |
|
|
return fArray[fSize];
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
//--------------------------------------------------------------------------------------------------
|
102 |
|
|
template<class ArrayElement, UInt_t N>
|
103 |
|
|
inline const ArrayElement mithep::StackArrayBasic<ArrayElement, N>::At(UInt_t idx) const
|
104 |
|
|
{
|
105 |
|
|
// Return entry at given index.
|
106 |
|
|
|
107 |
|
|
if(idx<fSize)
|
108 |
|
|
return fArray[idx];
|
109 |
|
|
|
110 |
|
|
Error("At", "Given index (%ud) is larger than array size (%ud)", idx, fSize);
|
111 |
|
|
return fArray[fSize];
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
//--------------------------------------------------------------------------------------------------
|
115 |
|
|
template<class ArrayElement, UInt_t N>
|
116 |
|
|
inline const ArrayElement mithep::StackArrayBasic<ArrayElement, N>::operator[](UInt_t idx) const
|
117 |
|
|
{
|
118 |
|
|
// Return entry at given index.
|
119 |
|
|
|
120 |
|
|
return At(idx);
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
//--------------------------------------------------------------------------------------------------
|
124 |
|
|
template<class ArrayElement, UInt_t N>
|
125 |
|
|
inline ArrayElement mithep::StackArrayBasic<ArrayElement, N>::operator[](UInt_t idx)
|
126 |
|
|
{
|
127 |
|
|
// Return entry at given index.
|
128 |
|
|
|
129 |
|
|
return At(idx);
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
//-------------------------------------------------------------------------------------------------
|
133 |
|
|
template<class ArrayElement, UInt_t N>
|
134 |
|
|
void mithep::StackArrayBasic<ArrayElement, N>::Streamer(TBuffer &b)
|
135 |
|
|
{
|
136 |
|
|
// Stream all objects in the array to or from the I/O buffer.
|
137 |
|
|
|
138 |
|
|
if (b.IsReading()) {
|
139 |
|
|
//UInt_t sv, cv;
|
140 |
|
|
//b.ReadVersion(&sv, &cv);
|
141 |
|
|
//TObject::Streamer(b);
|
142 |
|
|
b >> fSize;
|
143 |
|
|
if (fSize) {
|
144 |
|
|
b.ReadFastArray(fArray,fSize);
|
145 |
|
|
}
|
146 |
|
|
//b.CheckByteCount(sv, cv, StackArrayBasic::IsA());
|
147 |
|
|
} else { /*writing*/
|
148 |
|
|
//UInt_t cv;
|
149 |
|
|
//cv = b.WriteVersion(StackArrayBasic::IsA(), kTRUE);
|
150 |
|
|
//TObject::Streamer(b);
|
151 |
|
|
b << fSize;
|
152 |
|
|
if (fSize) {
|
153 |
|
|
b.WriteFastArray(fArray,fSize);
|
154 |
|
|
}
|
155 |
|
|
//b.SetByteCount(cv, kTRUE);
|
156 |
|
|
}
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
//--------------------------------------------------------------------------------------------------
|
160 |
|
|
template<class ArrayElement, UInt_t N>
|
161 |
|
|
inline ArrayElement mithep::StackArrayBasic<ArrayElement, N>::UncheckedAt(UInt_t idx)
|
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>::UncheckedAt(UInt_t idx) const
|
171 |
|
|
{
|
172 |
|
|
// Return entry at given index.
|
173 |
|
|
|
174 |
|
|
return fArray[idx];
|
175 |
|
|
}
|
176 |
|
|
#endif
|