ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/StackArray.h
Revision: 1.13
Committed: Mon Mar 23 13:07:18 2009 UTC (16 years, 1 month ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.12: +3 -1 lines
Log Message:
Reset entries cache when objects are added

File Contents

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