ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/FastArrayBasic.h
Revision: 1.3
Committed: Mon Mar 2 12:34:00 2009 UTC (16 years, 2 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.2: +20 -21 lines
Log Message:
Documentation, cleanup and object id removal.

File Contents

# User Rev Content
1 bendavid 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.3 // $Id: FastArrayBasic.h,v 1.2 2009/02/27 09:16:30 bendavid 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     // inside a TClonesArray.
11     // For various reasons, the array can not 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 bendavid 1.1 //
18     // Authors: J.Bendavid
19     //--------------------------------------------------------------------------------------------------
20    
21     #ifndef MITANA_DATACONT_FASTARRAYBASIC
22     #define MITANA_DATACONT_FASTARRAYBASIC
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>
32     class FastArrayBasic : public BaseCollection
33     {
34     public:
35     FastArrayBasic();
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 loizides 1.3 void Clear(Option_t */*opt*/="") { fSize=0; Init(0); }
43     UInt_t Entries() const { return fSize; }
44     UInt_t GetEntries() const { return Entries(); }
45 bendavid 1.1 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() { fSize = 0; }
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 loizides 1.3 ClassDef(FastArrayBasic,1) // Fast array for basic types
65 bendavid 1.1 };
66     }
67    
68     //--------------------------------------------------------------------------------------------------
69     template<class ArrayElement>
70     inline mithep::FastArrayBasic<ArrayElement>::FastArrayBasic() :
71     fSize(0),
72     fCapacity(0),
73     fArray(0)
74     {
75     // Default constructor.
76     }
77    
78     //--------------------------------------------------------------------------------------------------
79     template<class ArrayElement>
80     inline mithep::FastArrayBasic<ArrayElement>::FastArrayBasic(const FastArrayBasic &a) :
81 bendavid 1.2 fSize(0),
82 bendavid 1.1 fCapacity(0),
83     fArray(0)
84     {
85     // Copy constructor. Copy only elements which are used.
86 loizides 1.3
87 bendavid 1.2 Init(a.fSize);
88 bendavid 1.1 for (UInt_t i=0; i<a.fSize; ++i)
89     Add(a.fArray[i]);
90     }
91    
92     //--------------------------------------------------------------------------------------------------
93     template<class ArrayElement>
94     void mithep::FastArrayBasic<ArrayElement>::Add(const ArrayElement &ae)
95     {
96     // Add a copy of an existing object.
97    
98     if (fSize >= fCapacity)
99     Expand(TMath::Max(16,2*fCapacity));
100    
101     ++fSize;
102     fArray[fSize-1] = ae;
103     }
104    
105     //--------------------------------------------------------------------------------------------------
106     template<class ArrayElement>
107     inline ArrayElement mithep::FastArrayBasic<ArrayElement>::At(UInt_t idx)
108     {
109     // Return entry at given index.
110    
111     if (idx<fSize)
112     return fArray[idx];
113    
114     ArrayElement tmp;
115     TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
116     idx, fSize, this->GetName(), typeid(tmp).name());
117     return 0;
118     }
119    
120     //--------------------------------------------------------------------------------------------------
121     template<class ArrayElement>
122     inline const ArrayElement mithep::FastArrayBasic<ArrayElement>::At(UInt_t idx) const
123     {
124     // Return entry at given index.
125    
126     if (idx<fSize)
127     return fArray[idx];
128    
129     ArrayElement tmp;
130     TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
131     idx, fSize, this->GetName(), typeid(tmp).name());
132     return 0;
133     }
134    
135     //--------------------------------------------------------------------------------------------------
136     template<class ArrayElement>
137     inline void mithep::FastArrayBasic<ArrayElement>::Expand(UShort_t s)
138     {
139    
140 loizides 1.3 // Expand or shrink the array to given number of elements.
141 bendavid 1.1
142     if (s < fSize) {
143     TObject::Fatal("Expand", "Cannot shrink FastArrayBasic to less than fSize");
144     return;
145     }
146    
147     if (!fArray || s==0) {
148     Init(s);
149     return;
150     }
151    
152     if (fCapacity == s)
153     return;
154    
155     fArray = static_cast<ArrayElement*>(TStorage::ReAlloc(fArray, s * sizeof(ArrayElement),
156     fCapacity * sizeof(ArrayElement)));
157     fCapacity = s;
158     }
159    
160     //--------------------------------------------------------------------------------------------------
161     template<class ArrayElement>
162     inline void mithep::FastArrayBasic<ArrayElement>::Init(UShort_t s)
163     {
164    
165 loizides 1.3 // Initialize the array the heap.
166 bendavid 1.1
167     if (fArray && fCapacity != s) {
168     TStorage::Dealloc(fArray);
169     fArray = 0;
170     }
171    
172     fCapacity = s;
173    
174 loizides 1.3 if (!fArray && fCapacity > 0)
175     fArray = static_cast<ArrayElement*>(TStorage::Alloc(fCapacity*sizeof(ArrayElement)));
176 bendavid 1.1 }
177    
178     //-------------------------------------------------------------------------------------------------
179     template<class ArrayElement>
180     void mithep::FastArrayBasic<ArrayElement>::Streamer(TBuffer &b)
181     {
182     // Stream all objects in the array to or from the I/O buffer.
183    
184     if (b.IsReading()) {
185     b >> fSize;
186     if (fSize) {
187     if (fSize > fCapacity)
188     Expand(TMath::Max(static_cast<Int_t>(fSize),2*fCapacity));
189    
190     b.ReadFastArray(fArray,fSize);
191     }
192     } else { /*writing*/
193     b << fSize;
194     if (fSize) {
195     b.WriteFastArray(fArray,fSize);
196     }
197     }
198     }
199    
200     //--------------------------------------------------------------------------------------------------
201     template<class ArrayElement>
202     inline ArrayElement mithep::FastArrayBasic<ArrayElement>::UncheckedAt(UInt_t idx)
203     {
204     // Return entry at given index.
205    
206     return fArray[idx];
207     }
208    
209     //--------------------------------------------------------------------------------------------------
210     template<class ArrayElement>
211     inline const ArrayElement mithep::FastArrayBasic<ArrayElement>::UncheckedAt(UInt_t idx) const
212     {
213     // Return entry at given index.
214    
215     return fArray[idx];
216     }
217    
218     //--------------------------------------------------------------------------------------------------
219     template<class ArrayElement>
220     inline const ArrayElement mithep::FastArrayBasic<ArrayElement>::operator[](UInt_t idx) const
221     {
222     // Return entry at given index.
223    
224     return At(idx);
225     }
226    
227     //--------------------------------------------------------------------------------------------------
228     template<class ArrayElement>
229     inline ArrayElement mithep::FastArrayBasic<ArrayElement>::operator[](UInt_t idx)
230     {
231     // Return entry at given index.
232    
233     return At(idx);
234     }
235     #endif