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

# User Rev Content
1 bendavid 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.10 // $Id: FastArrayBasic.h,v 1.9 2009/03/23 22:15:09 loizides Exp $
3 bendavid 1.1 //
4     // FastArrayBasic
5     //
6 loizides 1.3 // Implementation of a "fast" array on the heap: Memory is dynamically allocated,
7 bendavid 1.1 // but there is an optimization in the read streamer similar to the TClonesArray
8 loizides 1.3 // 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 loizides 1.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 loizides 1.3 // 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 bendavid 1.1 //
19     // Authors: J.Bendavid
20     //--------------------------------------------------------------------------------------------------
21    
22 loizides 1.4 #ifndef MITANA_DATACONT_FASTARRAYBASIC_H
23     #define MITANA_DATACONT_FASTARRAYBASIC_H
24 bendavid 1.1
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 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32 = kFALSE>
33 bendavid 1.1 class FastArrayBasic : public BaseCollection
34     {
35     public:
36 loizides 1.4 FastArrayBasic(UShort_t icap=0);
37 bendavid 1.1 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 loizides 1.3 void Clear(Option_t */*opt*/="") { fSize=0; Init(0); }
44     UInt_t Entries() const { return fSize; }
45 loizides 1.6 UInt_t GetEntries() const { return fSize; }
46 bendavid 1.1 UInt_t GetSize() const { return fCapacity; }
47     Bool_t IsOwner() const { return kTRUE; }
48 bendavid 1.5 TObject *ObjAt(UInt_t /*idx*/) { return 0; }
49     const TObject *ObjAt(UInt_t /*idx*/) const { return 0; }
50 loizides 1.7 void Reset();
51 bendavid 1.1 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 loizides 1.3 ClassDef(FastArrayBasic,1) // Fast array for basic types
66 bendavid 1.1 };
67     }
68    
69     //--------------------------------------------------------------------------------------------------
70 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
71     inline mithep::FastArrayBasic<ArrayElement, IsDouble32>::FastArrayBasic(UShort_t icap) :
72 bendavid 1.1 fSize(0),
73     fCapacity(0),
74     fArray(0)
75     {
76     // Default constructor.
77 loizides 1.4
78     if (icap)
79     Init(icap);
80 bendavid 1.1 }
81    
82     //--------------------------------------------------------------------------------------------------
83 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
84     inline mithep::FastArrayBasic<ArrayElement, IsDouble32>::FastArrayBasic(const FastArrayBasic &a) :
85 bendavid 1.2 fSize(0),
86 bendavid 1.1 fCapacity(0),
87     fArray(0)
88     {
89     // Copy constructor. Copy only elements which are used.
90 loizides 1.3
91 bendavid 1.2 Init(a.fSize);
92 bendavid 1.1 for (UInt_t i=0; i<a.fSize; ++i)
93     Add(a.fArray[i]);
94     }
95    
96     //--------------------------------------------------------------------------------------------------
97 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
98     void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Add(const ArrayElement &ae)
99 bendavid 1.1 {
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 loizides 1.8 BaseCollection::Clear();
108 bendavid 1.1 }
109    
110     //--------------------------------------------------------------------------------------------------
111 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
112     inline ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::At(UInt_t idx)
113 bendavid 1.1 {
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 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
127     inline const ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::At(UInt_t idx) const
128 bendavid 1.1 {
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 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
142     inline void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Expand(UShort_t s)
143 bendavid 1.1 {
144    
145 loizides 1.3 // Expand or shrink the array to given number of elements.
146 bendavid 1.1
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 loizides 1.10 fCapacity * sizeof(ArrayElement)));
162 bendavid 1.1 fCapacity = s;
163     }
164    
165     //--------------------------------------------------------------------------------------------------
166 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
167     inline void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Init(UShort_t s)
168 bendavid 1.1 {
169    
170 loizides 1.3 // Initialize the array the heap.
171 bendavid 1.1
172     if (fArray && fCapacity != s) {
173     TStorage::Dealloc(fArray);
174     fArray = 0;
175     }
176    
177     fCapacity = s;
178    
179 loizides 1.3 if (!fArray && fCapacity > 0)
180     fArray = static_cast<ArrayElement*>(TStorage::Alloc(fCapacity*sizeof(ArrayElement)));
181 bendavid 1.1 }
182    
183     //-------------------------------------------------------------------------------------------------
184 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
185 loizides 1.7 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 bendavid 1.5 void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Streamer(TBuffer &b)
196 bendavid 1.1 {
197     // Stream all objects in the array to or from the I/O buffer.
198 bendavid 1.5 // Ugly special case handling for Double32
199 bendavid 1.1
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 bendavid 1.5 if (IsDouble32)
207     b.ReadFastArrayDouble32(reinterpret_cast<Double_t*>(fArray),fSize);
208     else
209     b.ReadFastArray(fArray,fSize);
210 loizides 1.8 BaseCollection::Clear();
211 bendavid 1.1 }
212     } else { /*writing*/
213     b << fSize;
214     if (fSize) {
215 bendavid 1.5 if (IsDouble32)
216     b.WriteFastArrayDouble32(reinterpret_cast<Double_t*>(fArray),fSize);
217     else
218     b.WriteFastArray(fArray,fSize);
219 bendavid 1.1 }
220     }
221     }
222    
223     //--------------------------------------------------------------------------------------------------
224 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
225     inline ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::UncheckedAt(UInt_t idx)
226 bendavid 1.1 {
227     // Return entry at given index.
228    
229     return fArray[idx];
230     }
231    
232     //--------------------------------------------------------------------------------------------------
233 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
234 loizides 1.7 inline const ArrayElement
235     mithep::FastArrayBasic<ArrayElement, IsDouble32>::UncheckedAt(UInt_t idx) const
236 bendavid 1.1 {
237     // Return entry at given index.
238    
239     return fArray[idx];
240     }
241    
242     //--------------------------------------------------------------------------------------------------
243 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
244 loizides 1.7 inline const ArrayElement
245     mithep::FastArrayBasic<ArrayElement, IsDouble32>::operator[](UInt_t idx) const
246 bendavid 1.1 {
247     // Return entry at given index.
248    
249     return At(idx);
250     }
251    
252     //--------------------------------------------------------------------------------------------------
253 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
254     inline ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::operator[](UInt_t idx)
255 bendavid 1.1 {
256     // Return entry at given index.
257    
258     return At(idx);
259     }
260     #endif