ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/FastArrayBasic.h
Revision: 1.9
Committed: Mon Mar 23 22:15:09 2009 UTC (16 years, 1 month ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_008
Changes since 1.8: +2 -2 lines
Log Message:
Cosmetics

File Contents

# Content
1 //--------------------------------------------------------------------------------------------------
2 // $Id: FastArrayBasic.h,v 1.8 2009/03/23 13:07:17 loizides Exp $
3 //
4 // FastArrayBasic
5 //
6 // Implementation of a "fast" array on the heap: Memory is dynamically allocated,
7 // but there is an optimization in the read streamer similar to the TClonesArray
8 // 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 cannot 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 //
18 // Authors: J.Bendavid
19 //--------------------------------------------------------------------------------------------------
20
21 #ifndef MITANA_DATACONT_FASTARRAYBASIC_H
22 #define MITANA_DATACONT_FASTARRAYBASIC_H
23
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 template<class ArrayElement, Bool_t IsDouble32 = kFALSE>
32 class FastArrayBasic : public BaseCollection
33 {
34 public:
35 FastArrayBasic(UShort_t icap=0);
36 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 void Clear(Option_t */*opt*/="") { fSize=0; Init(0); }
43 UInt_t Entries() const { return fSize; }
44 UInt_t GetEntries() const { return fSize; }
45 UInt_t GetSize() const { return fCapacity; }
46 Bool_t IsOwner() const { return kTRUE; }
47 TObject *ObjAt(UInt_t /*idx*/) { return 0; }
48 const TObject *ObjAt(UInt_t /*idx*/) const { return 0; }
49 void Reset();
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 ClassDef(FastArrayBasic,1) // Fast array for basic types
65 };
66 }
67
68 //--------------------------------------------------------------------------------------------------
69 template<class ArrayElement, Bool_t IsDouble32>
70 inline mithep::FastArrayBasic<ArrayElement, IsDouble32>::FastArrayBasic(UShort_t icap) :
71 fSize(0),
72 fCapacity(0),
73 fArray(0)
74 {
75 // Default constructor.
76
77 if (icap)
78 Init(icap);
79 }
80
81 //--------------------------------------------------------------------------------------------------
82 template<class ArrayElement, Bool_t IsDouble32>
83 inline mithep::FastArrayBasic<ArrayElement, IsDouble32>::FastArrayBasic(const FastArrayBasic &a) :
84 fSize(0),
85 fCapacity(0),
86 fArray(0)
87 {
88 // Copy constructor. Copy only elements which are used.
89
90 Init(a.fSize);
91 for (UInt_t i=0; i<a.fSize; ++i)
92 Add(a.fArray[i]);
93 }
94
95 //--------------------------------------------------------------------------------------------------
96 template<class ArrayElement, Bool_t IsDouble32>
97 void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Add(const ArrayElement &ae)
98 {
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 BaseCollection::Clear();
107 }
108
109 //--------------------------------------------------------------------------------------------------
110 template<class ArrayElement, Bool_t IsDouble32>
111 inline ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::At(UInt_t idx)
112 {
113 // Return entry at given index.
114
115 if (idx<fSize)
116 return fArray[idx];
117
118 ArrayElement tmp;
119 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
120 idx, fSize, this->GetName(), typeid(tmp).name());
121 return 0;
122 }
123
124 //--------------------------------------------------------------------------------------------------
125 template<class ArrayElement, Bool_t IsDouble32>
126 inline const ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::At(UInt_t idx) const
127 {
128 // Return entry at given index.
129
130 if (idx<fSize)
131 return 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, Bool_t IsDouble32>
141 inline void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Expand(UShort_t s)
142 {
143
144 // Expand or shrink the array to given number of elements.
145
146 if (s < fSize) {
147 TObject::Fatal("Expand", "Cannot shrink FastArrayBasic to less than fSize");
148 return;
149 }
150
151 if (!fArray || s==0) {
152 Init(s);
153 return;
154 }
155
156 if (fCapacity == s)
157 return;
158
159 fArray = static_cast<ArrayElement*>(TStorage::ReAlloc(fArray, s * sizeof(ArrayElement),
160 fCapacity * sizeof(ArrayElement)));
161 fCapacity = s;
162 }
163
164 //--------------------------------------------------------------------------------------------------
165 template<class ArrayElement, Bool_t IsDouble32>
166 inline void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Init(UShort_t s)
167 {
168
169 // Initialize the array the heap.
170
171 if (fArray && fCapacity != s) {
172 TStorage::Dealloc(fArray);
173 fArray = 0;
174 }
175
176 fCapacity = s;
177
178 if (!fArray && fCapacity > 0)
179 fArray = static_cast<ArrayElement*>(TStorage::Alloc(fCapacity*sizeof(ArrayElement)));
180 }
181
182 //-------------------------------------------------------------------------------------------------
183 template<class ArrayElement, Bool_t IsDouble32>
184 void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Reset()
185 {
186 // Reset this array.
187
188 fSize = 0;
189 BaseCollection::Clear();
190 }
191
192 //-------------------------------------------------------------------------------------------------
193 template<class ArrayElement, Bool_t IsDouble32>
194 void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Streamer(TBuffer &b)
195 {
196 // Stream all objects in the array to or from the I/O buffer.
197 // Ugly special case handling for Double32
198
199 if (b.IsReading()) {
200 b >> fSize;
201 if (fSize) {
202 if (fSize > fCapacity)
203 Expand(TMath::Max(static_cast<Int_t>(fSize),2*fCapacity));
204
205 if (IsDouble32)
206 b.ReadFastArrayDouble32(reinterpret_cast<Double_t*>(fArray),fSize);
207 else
208 b.ReadFastArray(fArray,fSize);
209 BaseCollection::Clear();
210 }
211 } else { /*writing*/
212 b << fSize;
213 if (fSize) {
214 if (IsDouble32)
215 b.WriteFastArrayDouble32(reinterpret_cast<Double_t*>(fArray),fSize);
216 else
217 b.WriteFastArray(fArray,fSize);
218 }
219 }
220 }
221
222 //--------------------------------------------------------------------------------------------------
223 template<class ArrayElement, Bool_t IsDouble32>
224 inline ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::UncheckedAt(UInt_t idx)
225 {
226 // Return entry at given index.
227
228 return fArray[idx];
229 }
230
231 //--------------------------------------------------------------------------------------------------
232 template<class ArrayElement, Bool_t IsDouble32>
233 inline const ArrayElement
234 mithep::FastArrayBasic<ArrayElement, IsDouble32>::UncheckedAt(UInt_t idx) const
235 {
236 // Return entry at given index.
237
238 return fArray[idx];
239 }
240
241 //--------------------------------------------------------------------------------------------------
242 template<class ArrayElement, Bool_t IsDouble32>
243 inline const ArrayElement
244 mithep::FastArrayBasic<ArrayElement, IsDouble32>::operator[](UInt_t idx) const
245 {
246 // Return entry at given index.
247
248 return At(idx);
249 }
250
251 //--------------------------------------------------------------------------------------------------
252 template<class ArrayElement, Bool_t IsDouble32>
253 inline ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::operator[](UInt_t idx)
254 {
255 // Return entry at given index.
256
257 return At(idx);
258 }
259 #endif