ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/FastArrayBasic.h
Revision: 1.10
Committed: Mon Apr 6 13:44:08 2009 UTC (16 years, 1 month ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_018, Mit_017, Mit_017pre3, Mit_017pre2, Mit_017pre1, Mit_016, Mit_015b, Mit_015a, Mit_015, Mit_014e, Mit_014d, Mit_014c, Mit_014b, Mit_014a, Mit_014, Mit_014pre3, Mit_014pre2, Mit_014pre1, Mit_013d, Mit_013c, Mit_013b, Mit_013a, Mit_013, Mit_013pre1, Mit_012i, Mit_012h, Mit_012g, Mit_012f, Mit_012e, Mit_012d, Mit_012c, Mit_012b, Mit_012a, Mit_012, Mit_011a, Mit_011, Mit_010a, Mit_010, Mit_009c, Mit_009b, Mit_009a, Mit_009
Changes since 1.9: +5 -4 lines
Log Message:
Improved comment

File Contents

# Content
1 //--------------------------------------------------------------------------------------------------
2 // $Id: FastArrayBasic.h,v 1.9 2009/03/23 22:15:09 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. For various reasons, the array cannot be written in split mode.
11 // This means you have to make sure that you declare it using "||" if you write out with
12 // high split level.
13 // Array is meant to store basic data types as opposed to FastArray
14 // which can hold arbitrary (non-heap using) classes.
15 // Since it stores basic types it can not derive from the Collection<ArrayElement>
16 // interface, or else the At() member functions would have to return pointers to
17 // basic elements. Something we did not want to do.
18 //
19 // Authors: J.Bendavid
20 //--------------------------------------------------------------------------------------------------
21
22 #ifndef MITANA_DATACONT_FASTARRAYBASIC_H
23 #define MITANA_DATACONT_FASTARRAYBASIC_H
24
25 #include <TObject.h>
26 #include <TClass.h>
27 #include <TStorage.h>
28 #include "MitAna/DataCont/interface/Collection.h"
29
30 namespace mithep
31 {
32 template<class ArrayElement, Bool_t IsDouble32 = kFALSE>
33 class FastArrayBasic : public BaseCollection
34 {
35 public:
36 FastArrayBasic(UShort_t icap=0);
37 FastArrayBasic(const FastArrayBasic &a);
38 ~FastArrayBasic() { Init(0); }
39
40 void Add(const ArrayElement &ae);
41 ArrayElement At(UInt_t idx);
42 const ArrayElement At(UInt_t idx) const;
43 void Clear(Option_t */*opt*/="") { fSize=0; Init(0); }
44 UInt_t Entries() const { return fSize; }
45 UInt_t GetEntries() const { return fSize; }
46 UInt_t GetSize() const { return fCapacity; }
47 Bool_t IsOwner() const { return kTRUE; }
48 TObject *ObjAt(UInt_t /*idx*/) { return 0; }
49 const TObject *ObjAt(UInt_t /*idx*/) const { return 0; }
50 void Reset();
51 void Trim() { Expand(fSize); }
52 ArrayElement UncheckedAt(UInt_t idx);
53 const ArrayElement UncheckedAt(UInt_t idx) const;
54 ArrayElement operator[](UInt_t idx);
55 const ArrayElement operator[](UInt_t idx) const;
56
57 protected:
58 void Init(UShort_t s);
59 void Expand(UShort_t s);
60
61 UShort_t fSize; //size of array
62 UShort_t fCapacity; //!size of heap allocated
63 ArrayElement *fArray; //!heap storage for objects
64
65 ClassDef(FastArrayBasic,1) // Fast array for basic types
66 };
67 }
68
69 //--------------------------------------------------------------------------------------------------
70 template<class ArrayElement, Bool_t IsDouble32>
71 inline mithep::FastArrayBasic<ArrayElement, IsDouble32>::FastArrayBasic(UShort_t icap) :
72 fSize(0),
73 fCapacity(0),
74 fArray(0)
75 {
76 // Default constructor.
77
78 if (icap)
79 Init(icap);
80 }
81
82 //--------------------------------------------------------------------------------------------------
83 template<class ArrayElement, Bool_t IsDouble32>
84 inline mithep::FastArrayBasic<ArrayElement, IsDouble32>::FastArrayBasic(const FastArrayBasic &a) :
85 fSize(0),
86 fCapacity(0),
87 fArray(0)
88 {
89 // Copy constructor. Copy only elements which are used.
90
91 Init(a.fSize);
92 for (UInt_t i=0; i<a.fSize; ++i)
93 Add(a.fArray[i]);
94 }
95
96 //--------------------------------------------------------------------------------------------------
97 template<class ArrayElement, Bool_t IsDouble32>
98 void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Add(const ArrayElement &ae)
99 {
100 // Add a copy of an existing object.
101
102 if (fSize >= fCapacity)
103 Expand(TMath::Max(16,2*fCapacity));
104
105 ++fSize;
106 fArray[fSize-1] = ae;
107 BaseCollection::Clear();
108 }
109
110 //--------------------------------------------------------------------------------------------------
111 template<class ArrayElement, Bool_t IsDouble32>
112 inline ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::At(UInt_t idx)
113 {
114 // Return entry at given index.
115
116 if (idx<fSize)
117 return fArray[idx];
118
119 ArrayElement tmp;
120 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
121 idx, fSize, this->GetName(), typeid(tmp).name());
122 return 0;
123 }
124
125 //--------------------------------------------------------------------------------------------------
126 template<class ArrayElement, Bool_t IsDouble32>
127 inline const ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::At(UInt_t idx) const
128 {
129 // Return entry at given index.
130
131 if (idx<fSize)
132 return fArray[idx];
133
134 ArrayElement tmp;
135 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
136 idx, fSize, this->GetName(), typeid(tmp).name());
137 return 0;
138 }
139
140 //--------------------------------------------------------------------------------------------------
141 template<class ArrayElement, Bool_t IsDouble32>
142 inline void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Expand(UShort_t s)
143 {
144
145 // Expand or shrink the array to given number of elements.
146
147 if (s < fSize) {
148 TObject::Fatal("Expand", "Cannot shrink FastArrayBasic to less than fSize");
149 return;
150 }
151
152 if (!fArray || s==0) {
153 Init(s);
154 return;
155 }
156
157 if (fCapacity == s)
158 return;
159
160 fArray = static_cast<ArrayElement*>(TStorage::ReAlloc(fArray, s * sizeof(ArrayElement),
161 fCapacity * sizeof(ArrayElement)));
162 fCapacity = s;
163 }
164
165 //--------------------------------------------------------------------------------------------------
166 template<class ArrayElement, Bool_t IsDouble32>
167 inline void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Init(UShort_t s)
168 {
169
170 // Initialize the array the heap.
171
172 if (fArray && fCapacity != s) {
173 TStorage::Dealloc(fArray);
174 fArray = 0;
175 }
176
177 fCapacity = s;
178
179 if (!fArray && fCapacity > 0)
180 fArray = static_cast<ArrayElement*>(TStorage::Alloc(fCapacity*sizeof(ArrayElement)));
181 }
182
183 //-------------------------------------------------------------------------------------------------
184 template<class ArrayElement, Bool_t IsDouble32>
185 void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Reset()
186 {
187 // Reset this array.
188
189 fSize = 0;
190 BaseCollection::Clear();
191 }
192
193 //-------------------------------------------------------------------------------------------------
194 template<class ArrayElement, Bool_t IsDouble32>
195 void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Streamer(TBuffer &b)
196 {
197 // Stream all objects in the array to or from the I/O buffer.
198 // Ugly special case handling for Double32
199
200 if (b.IsReading()) {
201 b >> fSize;
202 if (fSize) {
203 if (fSize > fCapacity)
204 Expand(TMath::Max(static_cast<Int_t>(fSize),2*fCapacity));
205
206 if (IsDouble32)
207 b.ReadFastArrayDouble32(reinterpret_cast<Double_t*>(fArray),fSize);
208 else
209 b.ReadFastArray(fArray,fSize);
210 BaseCollection::Clear();
211 }
212 } else { /*writing*/
213 b << fSize;
214 if (fSize) {
215 if (IsDouble32)
216 b.WriteFastArrayDouble32(reinterpret_cast<Double_t*>(fArray),fSize);
217 else
218 b.WriteFastArray(fArray,fSize);
219 }
220 }
221 }
222
223 //--------------------------------------------------------------------------------------------------
224 template<class ArrayElement, Bool_t IsDouble32>
225 inline ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::UncheckedAt(UInt_t idx)
226 {
227 // Return entry at given index.
228
229 return fArray[idx];
230 }
231
232 //--------------------------------------------------------------------------------------------------
233 template<class ArrayElement, Bool_t IsDouble32>
234 inline const ArrayElement
235 mithep::FastArrayBasic<ArrayElement, IsDouble32>::UncheckedAt(UInt_t idx) const
236 {
237 // Return entry at given index.
238
239 return fArray[idx];
240 }
241
242 //--------------------------------------------------------------------------------------------------
243 template<class ArrayElement, Bool_t IsDouble32>
244 inline const ArrayElement
245 mithep::FastArrayBasic<ArrayElement, IsDouble32>::operator[](UInt_t idx) const
246 {
247 // Return entry at given index.
248
249 return At(idx);
250 }
251
252 //--------------------------------------------------------------------------------------------------
253 template<class ArrayElement, Bool_t IsDouble32>
254 inline ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::operator[](UInt_t idx)
255 {
256 // Return entry at given index.
257
258 return At(idx);
259 }
260 #endif