ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/StackArray.h
Revision: 1.10
Committed: Mon Mar 2 14:56:41 2009 UTC (16 years, 2 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_008pre1
Changes since 1.9: +3 -3 lines
Log Message:
Introduced FastArrayBasic types.

File Contents

# User Rev Content
1 bendavid 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.10 // $Id: StackArray.h,v 1.9 2009/03/02 12:34:00 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     UInt_t Entries() const { return GetEntries(); }
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.7 Bool_t IsOwner() const { return kTRUE; }
42     TObject *ObjAt(UInt_t idx);
43     const TObject *ObjAt(UInt_t idx) const;
44     void Reset() { fSize = 0; }
45     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     }
96    
97     //--------------------------------------------------------------------------------------------------
98     template<class ArrayElement, UInt_t N>
99     ArrayElement* mithep::StackArray<ArrayElement, N>::AddNew()
100     {
101     // Add new object.
102    
103     return new(Allocate()) ArrayElement();
104     }
105    
106     //--------------------------------------------------------------------------------------------------
107     template<class ArrayElement, UInt_t N>
108     ArrayElement* mithep::StackArray<ArrayElement, N>::Allocate()
109     {
110 loizides 1.4 // Return next slot in the array, *only* to be used in placement new operator.
111 bendavid 1.1
112 loizides 1.7 if (fSize>=N) {
113 loizides 1.4 TObject::Fatal("Allocate", "Maximum number of slots reached (%d>=%d): "
114     "To support more requires a different template!", fSize, N);
115 bendavid 1.1 return 0;
116     }
117    
118     ++fSize;
119     return &fArray[fSize-1];
120     }
121    
122     //--------------------------------------------------------------------------------------------------
123     template<class ArrayElement, UInt_t N>
124     inline ArrayElement *mithep::StackArray<ArrayElement, N>::At(UInt_t idx)
125     {
126     // Return entry at given index.
127    
128 loizides 1.7 if (idx<fSize)
129 bendavid 1.1 return static_cast<ArrayElement*>(&fArray[idx]);
130    
131 loizides 1.4 ArrayElement tmp;
132 loizides 1.8 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
133 loizides 1.4 idx, fSize, this->GetName(), typeid(tmp).name());
134 bendavid 1.1 return 0;
135     }
136    
137     //--------------------------------------------------------------------------------------------------
138     template<class ArrayElement, UInt_t N>
139     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::At(UInt_t idx) const
140     {
141     // Return entry at given index.
142    
143 loizides 1.7 if (idx<fSize)
144 bendavid 1.1 return static_cast<const ArrayElement*>(&fArray[idx]);
145    
146 loizides 1.4 ArrayElement tmp;
147 loizides 1.8 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
148 loizides 1.4 idx, fSize, this->GetName(), typeid(tmp).name());
149 bendavid 1.1 return 0;
150     }
151    
152 bendavid 1.5 //--------------------------------------------------------------------------------------------------
153     template<class ArrayElement, UInt_t N>
154     inline Bool_t mithep::StackArray<ArrayElement, N>::HasObject(const ArrayElement *obj) const
155     {
156     // Check whether object is in array.
157    
158     for (UInt_t i=0; i<fSize; ++i) {
159 bendavid 1.6 if ( fArray[i].IsEqual(obj) )
160 bendavid 1.5 return true;
161     }
162    
163     return false;
164     }
165    
166 loizides 1.7 //--------------------------------------------------------------------------------------------------
167     template<class ArrayElement, UInt_t N>
168     inline TObject *mithep::StackArray<ArrayElement, N>::ObjAt(UInt_t idx)
169     {
170     // Return object at given index.
171    
172     return static_cast<TObject*>(At(idx));
173     }
174    
175     //--------------------------------------------------------------------------------------------------
176     template<class ArrayElement, UInt_t N>
177     inline const TObject *mithep::StackArray<ArrayElement, N>::ObjAt(UInt_t idx) const
178     {
179     // Return object at given index.
180    
181     return static_cast<const TObject*>(At(idx));
182     }
183    
184 bendavid 1.1 //-------------------------------------------------------------------------------------------------
185     template<class ArrayElement, UInt_t N>
186     void mithep::StackArray<ArrayElement, N>::Streamer(TBuffer &b)
187     {
188     // Stream all objects in the array to or from the I/O buffer.
189    
190     if (b.IsReading()) {
191     b >> fSize;
192     if (fSize) {
193     b.ReadFastArray(fArray,fClass,fSize);
194     }
195     } else { /*writing*/
196     b << fSize;
197     if (fSize) {
198     b.WriteFastArray(fArray,fClass,fSize);
199     }
200     }
201     }
202    
203     //--------------------------------------------------------------------------------------------------
204     template<class ArrayElement, UInt_t N>
205     inline ArrayElement *mithep::StackArray<ArrayElement, N>::UncheckedAt(UInt_t idx)
206     {
207     // Return entry at given index.
208    
209     return static_cast<ArrayElement*>(&fArray[idx]);
210     }
211    
212     //--------------------------------------------------------------------------------------------------
213     template<class ArrayElement, UInt_t N>
214     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::UncheckedAt(UInt_t idx) const
215     {
216     // Return entry at given index.
217    
218     return static_cast<const ArrayElement*>(&fArray[idx]);
219     }
220 loizides 1.4
221     //--------------------------------------------------------------------------------------------------
222     template<class ArrayElement, UInt_t N>
223     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::operator[](UInt_t idx) const
224     {
225     // Return entry at given index.
226    
227     return At(idx);
228     }
229    
230     //--------------------------------------------------------------------------------------------------
231     template<class ArrayElement, UInt_t N>
232     inline ArrayElement *mithep::StackArray<ArrayElement, N>::operator[](UInt_t idx)
233     {
234     // Return entry at given index.
235    
236     return At(idx);
237     }
238 bendavid 1.1 #endif