ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/FastArrayBasic.h
Revision: 1.2
Committed: Fri Feb 27 09:16:30 2009 UTC (16 years, 2 months ago) by bendavid
Content type: text/plain
Branch: MAIN
Changes since 1.1: +3 -3 lines
Log Message:
Fixed FastArray copy ctor bugs

File Contents

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