ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/FastArrayBasic.h
Revision: 1.11
Committed: Fri Mar 11 04:02:38 2011 UTC (14 years, 1 month ago) by bendavid
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_032, Mit_031, Mit_025c_branch2, Mit_025c_branch1, Mit_030, Mit_029c, Mit_029b, Mit_030_pre1, Mit_029a, Mit_029, Mit_029_pre1, Mit_028a, Mit_025c_branch0, Mit_028, Mit_027a, Mit_027, Mit_026, Mit_025e, Mit_025d, Mit_025c, Mit_025b, Mit_025a, Mit_025, Mit_025pre2, Mit_024b, Mit_025pre1, Mit_024a, Mit_024, Mit_023, Mit_022a, Mit_022, Mit_020d, TMit_020d, Mit_020c, Mit_021, Mit_021pre2, Mit_021pre1, Mit_020b, Mit_020a, Mit_020, Mit_020pre1, HEAD
Branch point for: Mit_025c_branch
Changes since 1.10: +10 -3 lines
Log Message:
unspeakable hacks to workaround unspeakable cint bugs

File Contents

# User Rev Content
1 bendavid 1.1 //--------------------------------------------------------------------------------------------------
2 bendavid 1.11 // $Id: FastArrayBasic.h,v 1.10 2009/04/06 13:44:08 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 bendavid 1.11 typedef UInt_t unsignedint;
31     typedef UChar_t unsignedchar;
32     typedef UShort_t unsignedshort;
33     typedef ULong64_t unsignedlonglong;
34    
35 bendavid 1.1 namespace mithep
36     {
37 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32 = kFALSE>
38 bendavid 1.1 class FastArrayBasic : public BaseCollection
39     {
40     public:
41 loizides 1.4 FastArrayBasic(UShort_t icap=0);
42 bendavid 1.1 FastArrayBasic(const FastArrayBasic &a);
43     ~FastArrayBasic() { Init(0); }
44    
45 bendavid 1.11 Int_t Add(const ArrayElement &ae);
46 bendavid 1.1 ArrayElement At(UInt_t idx);
47     const ArrayElement At(UInt_t idx) const;
48 loizides 1.3 void Clear(Option_t */*opt*/="") { fSize=0; Init(0); }
49     UInt_t Entries() const { return fSize; }
50 loizides 1.6 UInt_t GetEntries() const { return fSize; }
51 bendavid 1.1 UInt_t GetSize() const { return fCapacity; }
52     Bool_t IsOwner() const { return kTRUE; }
53 bendavid 1.5 TObject *ObjAt(UInt_t /*idx*/) { return 0; }
54     const TObject *ObjAt(UInt_t /*idx*/) const { return 0; }
55 loizides 1.7 void Reset();
56 bendavid 1.1 void Trim() { Expand(fSize); }
57     ArrayElement UncheckedAt(UInt_t idx);
58     const ArrayElement UncheckedAt(UInt_t idx) const;
59     ArrayElement operator[](UInt_t idx);
60     const ArrayElement operator[](UInt_t idx) const;
61    
62     protected:
63     void Init(UShort_t s);
64     void Expand(UShort_t s);
65    
66     UShort_t fSize; //size of array
67     UShort_t fCapacity; //!size of heap allocated
68     ArrayElement *fArray; //!heap storage for objects
69    
70 loizides 1.3 ClassDef(FastArrayBasic,1) // Fast array for basic types
71 bendavid 1.1 };
72     }
73    
74     //--------------------------------------------------------------------------------------------------
75 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
76     inline mithep::FastArrayBasic<ArrayElement, IsDouble32>::FastArrayBasic(UShort_t icap) :
77 bendavid 1.1 fSize(0),
78     fCapacity(0),
79     fArray(0)
80     {
81     // Default constructor.
82 loizides 1.4
83     if (icap)
84     Init(icap);
85 bendavid 1.1 }
86    
87     //--------------------------------------------------------------------------------------------------
88 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
89     inline mithep::FastArrayBasic<ArrayElement, IsDouble32>::FastArrayBasic(const FastArrayBasic &a) :
90 bendavid 1.2 fSize(0),
91 bendavid 1.1 fCapacity(0),
92     fArray(0)
93     {
94     // Copy constructor. Copy only elements which are used.
95 loizides 1.3
96 bendavid 1.2 Init(a.fSize);
97 bendavid 1.1 for (UInt_t i=0; i<a.fSize; ++i)
98     Add(a.fArray[i]);
99     }
100    
101     //--------------------------------------------------------------------------------------------------
102 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
103 bendavid 1.11 Int_t mithep::FastArrayBasic<ArrayElement, IsDouble32>::Add(const ArrayElement &ae)
104 bendavid 1.1 {
105     // Add a copy of an existing object.
106    
107     if (fSize >= fCapacity)
108     Expand(TMath::Max(16,2*fCapacity));
109    
110     ++fSize;
111     fArray[fSize-1] = ae;
112 loizides 1.8 BaseCollection::Clear();
113 bendavid 1.11
114     return fSize;
115 bendavid 1.1 }
116    
117     //--------------------------------------------------------------------------------------------------
118 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
119     inline ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::At(UInt_t idx)
120 bendavid 1.1 {
121     // Return entry at given index.
122    
123     if (idx<fSize)
124     return fArray[idx];
125    
126     ArrayElement tmp;
127     TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
128     idx, fSize, this->GetName(), typeid(tmp).name());
129     return 0;
130     }
131    
132     //--------------------------------------------------------------------------------------------------
133 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
134     inline const ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::At(UInt_t idx) const
135 bendavid 1.1 {
136     // Return entry at given index.
137    
138     if (idx<fSize)
139     return fArray[idx];
140    
141     ArrayElement tmp;
142     TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
143     idx, fSize, this->GetName(), typeid(tmp).name());
144     return 0;
145     }
146    
147     //--------------------------------------------------------------------------------------------------
148 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
149     inline void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Expand(UShort_t s)
150 bendavid 1.1 {
151    
152 loizides 1.3 // Expand or shrink the array to given number of elements.
153 bendavid 1.1
154     if (s < fSize) {
155     TObject::Fatal("Expand", "Cannot shrink FastArrayBasic to less than fSize");
156     return;
157     }
158    
159     if (!fArray || s==0) {
160     Init(s);
161     return;
162     }
163    
164     if (fCapacity == s)
165     return;
166    
167     fArray = static_cast<ArrayElement*>(TStorage::ReAlloc(fArray, s * sizeof(ArrayElement),
168 loizides 1.10 fCapacity * sizeof(ArrayElement)));
169 bendavid 1.1 fCapacity = s;
170     }
171    
172     //--------------------------------------------------------------------------------------------------
173 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
174     inline void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Init(UShort_t s)
175 bendavid 1.1 {
176    
177 loizides 1.3 // Initialize the array the heap.
178 bendavid 1.1
179     if (fArray && fCapacity != s) {
180     TStorage::Dealloc(fArray);
181     fArray = 0;
182     }
183    
184     fCapacity = s;
185    
186 loizides 1.3 if (!fArray && fCapacity > 0)
187     fArray = static_cast<ArrayElement*>(TStorage::Alloc(fCapacity*sizeof(ArrayElement)));
188 bendavid 1.1 }
189    
190     //-------------------------------------------------------------------------------------------------
191 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
192 loizides 1.7 void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Reset()
193     {
194     // Reset this array.
195    
196     fSize = 0;
197     BaseCollection::Clear();
198     }
199    
200     //-------------------------------------------------------------------------------------------------
201     template<class ArrayElement, Bool_t IsDouble32>
202 bendavid 1.5 void mithep::FastArrayBasic<ArrayElement, IsDouble32>::Streamer(TBuffer &b)
203 bendavid 1.1 {
204     // Stream all objects in the array to or from the I/O buffer.
205 bendavid 1.5 // Ugly special case handling for Double32
206 bendavid 1.1
207     if (b.IsReading()) {
208     b >> fSize;
209     if (fSize) {
210     if (fSize > fCapacity)
211     Expand(TMath::Max(static_cast<Int_t>(fSize),2*fCapacity));
212    
213 bendavid 1.5 if (IsDouble32)
214     b.ReadFastArrayDouble32(reinterpret_cast<Double_t*>(fArray),fSize);
215     else
216     b.ReadFastArray(fArray,fSize);
217 loizides 1.8 BaseCollection::Clear();
218 bendavid 1.1 }
219     } else { /*writing*/
220     b << fSize;
221     if (fSize) {
222 bendavid 1.5 if (IsDouble32)
223     b.WriteFastArrayDouble32(reinterpret_cast<Double_t*>(fArray),fSize);
224     else
225     b.WriteFastArray(fArray,fSize);
226 bendavid 1.1 }
227     }
228     }
229    
230     //--------------------------------------------------------------------------------------------------
231 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
232     inline ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::UncheckedAt(UInt_t idx)
233 bendavid 1.1 {
234     // Return entry at given index.
235    
236     return fArray[idx];
237     }
238    
239     //--------------------------------------------------------------------------------------------------
240 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
241 loizides 1.7 inline const ArrayElement
242     mithep::FastArrayBasic<ArrayElement, IsDouble32>::UncheckedAt(UInt_t idx) const
243 bendavid 1.1 {
244     // Return entry at given index.
245    
246     return fArray[idx];
247     }
248    
249     //--------------------------------------------------------------------------------------------------
250 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
251 loizides 1.7 inline const ArrayElement
252     mithep::FastArrayBasic<ArrayElement, IsDouble32>::operator[](UInt_t idx) const
253 bendavid 1.1 {
254     // Return entry at given index.
255    
256     return At(idx);
257     }
258    
259     //--------------------------------------------------------------------------------------------------
260 bendavid 1.5 template<class ArrayElement, Bool_t IsDouble32>
261     inline ArrayElement mithep::FastArrayBasic<ArrayElement, IsDouble32>::operator[](UInt_t idx)
262 bendavid 1.1 {
263     // Return entry at given index.
264    
265     return At(idx);
266     }
267     #endif