1 |
bendavid |
1.1 |
//--------------------------------------------------------------------------------------------------
|
2 |
loizides |
1.5 |
// $Id: StackArrayBasic.h,v 1.4 2008/12/18 13:34:16 loizides Exp $
|
3 |
bendavid |
1.1 |
//
|
4 |
|
|
// StackArrayBasic
|
5 |
|
|
//
|
6 |
loizides |
1.2 |
// Implementation of an array on the stack as opposed to on the heap
|
7 |
|
|
// memory. For various reasons, the array can not 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 |
loizides |
1.3 |
// classes. Note that this array as opposed to the others does not
|
11 |
|
|
// derive from the templated Collection interface.
|
12 |
bendavid |
1.1 |
//
|
13 |
|
|
// Authors: C.Loizides, J.Bendavid
|
14 |
|
|
//--------------------------------------------------------------------------------------------------
|
15 |
|
|
|
16 |
|
|
#ifndef MITANA_DATACONT_STACKARRAYBASIC
|
17 |
|
|
#define MITANA_DATACONT_STACKARRAYBASIC
|
18 |
|
|
|
19 |
|
|
#include <TObject.h>
|
20 |
loizides |
1.3 |
#include "MitAna/DataCont/interface/BaseCollection.h"
|
21 |
bendavid |
1.1 |
|
22 |
|
|
namespace mithep
|
23 |
|
|
{
|
24 |
|
|
template<class ArrayElement, UInt_t N>
|
25 |
loizides |
1.3 |
class StackArrayBasic : public BaseCollection
|
26 |
bendavid |
1.1 |
{
|
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 GetEntries(); }
|
35 |
loizides |
1.3 |
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() { fSize = 0; }
|
41 |
bendavid |
1.1 |
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 |
loizides |
1.2 |
UShort_t fSize; //size of array
|
49 |
|
|
ArrayElement fArray[N]; //storage for basic types
|
50 |
bendavid |
1.1 |
|
51 |
loizides |
1.3 |
ClassDef(StackArrayBasic, 1) // Array on stack for basic types
|
52 |
bendavid |
1.1 |
};
|
53 |
|
|
}
|
54 |
|
|
|
55 |
|
|
//--------------------------------------------------------------------------------------------------
|
56 |
|
|
template<class ArrayElement, UInt_t N>
|
57 |
|
|
inline mithep::StackArrayBasic<ArrayElement, N>::StackArrayBasic() :
|
58 |
|
|
fSize(0)
|
59 |
|
|
{
|
60 |
loizides |
1.2 |
// Default constructor.
|
61 |
bendavid |
1.1 |
}
|
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 |
loizides |
1.2 |
// Copy constructor. Copy only elements which are used.
|
69 |
|
|
|
70 |
bendavid |
1.1 |
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 |
loizides |
1.2 |
// Add element to array.
|
79 |
bendavid |
1.1 |
|
80 |
loizides |
1.3 |
if (fSize>=N) {
|
81 |
loizides |
1.2 |
TObject::Fatal("Add", "Maximum number of slots reached (%d>=%d): "
|
82 |
|
|
"To support more requires a different template!", fSize, N);
|
83 |
bendavid |
1.1 |
return;
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
fArray[fSize] = ae;
|
87 |
|
|
++fSize;
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
//--------------------------------------------------------------------------------------------------
|
91 |
|
|
template<class ArrayElement, UInt_t N>
|
92 |
loizides |
1.2 |
inline ArrayElement mithep::StackArrayBasic<ArrayElement, N>::At(UInt_t idx)
|
93 |
bendavid |
1.1 |
{
|
94 |
|
|
// Return entry at given index.
|
95 |
|
|
|
96 |
loizides |
1.3 |
if (idx<fSize)
|
97 |
bendavid |
1.1 |
return fArray[idx];
|
98 |
|
|
|
99 |
loizides |
1.2 |
ArrayElement tmp;
|
100 |
loizides |
1.4 |
TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
|
101 |
loizides |
1.2 |
idx, fSize, GetName(), typeid(tmp).name());
|
102 |
|
|
return 0;
|
103 |
bendavid |
1.1 |
}
|
104 |
|
|
|
105 |
|
|
//--------------------------------------------------------------------------------------------------
|
106 |
|
|
template<class ArrayElement, UInt_t N>
|
107 |
|
|
inline const ArrayElement mithep::StackArrayBasic<ArrayElement, N>::At(UInt_t idx) const
|
108 |
|
|
{
|
109 |
|
|
// Return entry at given index.
|
110 |
|
|
|
111 |
loizides |
1.3 |
if (idx<fSize)
|
112 |
bendavid |
1.1 |
return fArray[idx];
|
113 |
|
|
|
114 |
loizides |
1.2 |
ArrayElement tmp;
|
115 |
loizides |
1.4 |
TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
|
116 |
loizides |
1.2 |
idx, fSize, GetName(), typeid(tmp).name());
|
117 |
|
|
return 0;
|
118 |
bendavid |
1.1 |
}
|
119 |
|
|
|
120 |
|
|
//-------------------------------------------------------------------------------------------------
|
121 |
|
|
template<class ArrayElement, UInt_t N>
|
122 |
|
|
void mithep::StackArrayBasic<ArrayElement, N>::Streamer(TBuffer &b)
|
123 |
|
|
{
|
124 |
|
|
// Stream all objects in the array to or from the I/O buffer.
|
125 |
|
|
|
126 |
|
|
if (b.IsReading()) {
|
127 |
|
|
b >> fSize;
|
128 |
|
|
if (fSize) {
|
129 |
|
|
b.ReadFastArray(fArray,fSize);
|
130 |
|
|
}
|
131 |
|
|
} else { /*writing*/
|
132 |
|
|
b << fSize;
|
133 |
|
|
if (fSize) {
|
134 |
|
|
b.WriteFastArray(fArray,fSize);
|
135 |
|
|
}
|
136 |
|
|
}
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
//--------------------------------------------------------------------------------------------------
|
140 |
|
|
template<class ArrayElement, UInt_t N>
|
141 |
loizides |
1.2 |
inline ArrayElement mithep::StackArrayBasic<ArrayElement, N>::UncheckedAt(UInt_t idx)
|
142 |
bendavid |
1.1 |
{
|
143 |
|
|
// Return entry at given index.
|
144 |
|
|
|
145 |
|
|
return fArray[idx];
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
//--------------------------------------------------------------------------------------------------
|
149 |
|
|
template<class ArrayElement, UInt_t N>
|
150 |
loizides |
1.2 |
inline const ArrayElement mithep::StackArrayBasic<ArrayElement, N>::UncheckedAt(UInt_t idx) const
|
151 |
bendavid |
1.1 |
{
|
152 |
|
|
// Return entry at given index.
|
153 |
|
|
|
154 |
|
|
return fArray[idx];
|
155 |
|
|
}
|
156 |
loizides |
1.2 |
|
157 |
|
|
//--------------------------------------------------------------------------------------------------
|
158 |
|
|
template<class ArrayElement, UInt_t N>
|
159 |
|
|
inline const ArrayElement mithep::StackArrayBasic<ArrayElement, N>::operator[](UInt_t idx) const
|
160 |
|
|
{
|
161 |
|
|
// Return entry at given index.
|
162 |
|
|
|
163 |
|
|
return At(idx);
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
//--------------------------------------------------------------------------------------------------
|
167 |
|
|
template<class ArrayElement, UInt_t N>
|
168 |
|
|
inline ArrayElement mithep::StackArrayBasic<ArrayElement, N>::operator[](UInt_t idx)
|
169 |
|
|
{
|
170 |
|
|
// Return entry at given index.
|
171 |
|
|
|
172 |
|
|
return At(idx);
|
173 |
|
|
}
|
174 |
bendavid |
1.1 |
#endif
|