ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/StackArray.h
Revision: 1.7
Committed: Wed Dec 10 11:26:52 2008 UTC (16 years, 4 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.6: +35 -15 lines
Log Message:
Implemented ObjAt function.

File Contents

# User Rev Content
1 bendavid 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.7 // $Id: StackArray.h,v 1.6 2008/12/03 16:56:28 bendavid 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     #ifndef MITANA_DATACONT_STACKARRAY
16     #define MITANA_DATACONT_STACKARRAY
17    
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 ~StackArray() {}
31    
32     void AddCopy(const ArrayElement &ae);
33     ArrayElement *AddNew();
34     ArrayElement *Allocate();
35     ArrayElement *At(UInt_t idx);
36 loizides 1.7 const ArrayElement *At(UInt_t idx) const;
37     void Clear(Option_t */*opt*/="") {}
38     UInt_t Entries() const { return GetEntries(); }
39     UInt_t GetEntries() const { return fSize; }
40     UInt_t GetSize() const { return N; }
41 bendavid 1.5 Bool_t HasObject(const ArrayElement *obj) const;
42 loizides 1.7 Bool_t IsOwner() const { return kTRUE; }
43     TObject *ObjAt(UInt_t idx);
44     const TObject *ObjAt(UInt_t idx) const;
45     void Reset() { fSize = 0; }
46     void Trim() {}
47 bendavid 1.1 ArrayElement *UncheckedAt(UInt_t idx);
48 loizides 1.7 const ArrayElement *UncheckedAt(UInt_t idx) const;
49 bendavid 1.1 ArrayElement *operator[](UInt_t idx);
50 loizides 1.7 const ArrayElement *operator[](UInt_t idx) const;
51 bendavid 1.1
52     protected:
53 loizides 1.2 TClass *fClass; //!pointer to TClass object used by streamer
54     UShort_t fSize; //size of array
55 loizides 1.4 ArrayElement fArray[N]; //storage for objects
56 bendavid 1.1
57 loizides 1.4 ClassDef(StackArray,1) // Array on stack for arbitrary classes
58 bendavid 1.1 };
59     }
60    
61     //--------------------------------------------------------------------------------------------------
62     template<class ArrayElement, UInt_t N>
63     inline mithep::StackArray<ArrayElement, N>::StackArray() :
64 bendavid 1.3 fClass(TClass::GetClass(typeid(ArrayElement))),
65 bendavid 1.1 fSize(0)
66     {
67     // Default constructor.
68 bendavid 1.3 }
69    
70     //--------------------------------------------------------------------------------------------------
71     template<class ArrayElement, UInt_t N>
72     inline mithep::StackArray<ArrayElement, N>::StackArray(const StackArray &a) :
73     fClass(a.fClass),
74     fSize(a.fSize)
75     {
76 loizides 1.4 // Copy constructor. Copy only elements which are used.
77    
78 bendavid 1.3 for (UInt_t i=0; i<fSize; ++i)
79     fArray[i] = a.fArray[i];
80 bendavid 1.1 }
81    
82     //--------------------------------------------------------------------------------------------------
83     template<class ArrayElement, UInt_t N>
84     void mithep::StackArray<ArrayElement, N>::AddCopy(const ArrayElement &ae)
85     {
86     // Add a copy of an existing object.
87    
88 loizides 1.7 if (fSize>=N) {
89 loizides 1.4 TObject::Fatal("AddCopy", "Maximum number of slots reached (%d>=%d): "
90     "To support more requires a different template!", fSize, N);
91 bendavid 1.1 return;
92     }
93    
94     fArray[fSize] = ae;
95     ++fSize;
96     }
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     return &fArray[fSize-1];
121     }
122    
123     //--------------------------------------------------------------------------------------------------
124     template<class ArrayElement, UInt_t N>
125     inline ArrayElement *mithep::StackArray<ArrayElement, N>::At(UInt_t idx)
126     {
127     // Return entry at given index.
128    
129 loizides 1.7 if (idx<fSize)
130 bendavid 1.1 return static_cast<ArrayElement*>(&fArray[idx]);
131    
132 loizides 1.4 ArrayElement tmp;
133     TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
134     idx, fSize, this->GetName(), typeid(tmp).name());
135 bendavid 1.1 return 0;
136     }
137    
138     //--------------------------------------------------------------------------------------------------
139     template<class ArrayElement, UInt_t N>
140     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::At(UInt_t idx) const
141     {
142     // Return entry at given index.
143    
144 loizides 1.7 if (idx<fSize)
145 bendavid 1.1 return static_cast<const ArrayElement*>(&fArray[idx]);
146    
147 loizides 1.4 ArrayElement tmp;
148     TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
149     idx, fSize, this->GetName(), typeid(tmp).name());
150 bendavid 1.1 return 0;
151     }
152    
153 bendavid 1.5 //--------------------------------------------------------------------------------------------------
154     template<class ArrayElement, UInt_t N>
155     inline Bool_t mithep::StackArray<ArrayElement, N>::HasObject(const ArrayElement *obj) const
156     {
157     // Check whether object is in array.
158    
159     for (UInt_t i=0; i<fSize; ++i) {
160 bendavid 1.6 if ( fArray[i].IsEqual(obj) )
161 bendavid 1.5 return true;
162     }
163    
164     return false;
165     }
166    
167 loizides 1.7 //--------------------------------------------------------------------------------------------------
168     template<class ArrayElement, UInt_t N>
169     inline TObject *mithep::StackArray<ArrayElement, N>::ObjAt(UInt_t idx)
170     {
171     // Return object at given index.
172    
173     return static_cast<TObject*>(At(idx));
174     }
175    
176     //--------------------------------------------------------------------------------------------------
177     template<class ArrayElement, UInt_t N>
178     inline const TObject *mithep::StackArray<ArrayElement, N>::ObjAt(UInt_t idx) const
179     {
180     // Return object at given index.
181    
182     return static_cast<const TObject*>(At(idx));
183     }
184    
185 bendavid 1.1 //-------------------------------------------------------------------------------------------------
186     template<class ArrayElement, UInt_t N>
187     void mithep::StackArray<ArrayElement, N>::Streamer(TBuffer &b)
188     {
189     // Stream all objects in the array to or from the I/O buffer.
190    
191     if (b.IsReading()) {
192     b >> fSize;
193     if (fSize) {
194     b.ReadFastArray(fArray,fClass,fSize);
195     }
196     } else { /*writing*/
197     b << fSize;
198     if (fSize) {
199     b.WriteFastArray(fArray,fClass,fSize);
200     }
201     }
202     }
203    
204     //--------------------------------------------------------------------------------------------------
205     template<class ArrayElement, UInt_t N>
206     inline ArrayElement *mithep::StackArray<ArrayElement, N>::UncheckedAt(UInt_t idx)
207     {
208     // Return entry at given index.
209    
210     return static_cast<ArrayElement*>(&fArray[idx]);
211     }
212    
213     //--------------------------------------------------------------------------------------------------
214     template<class ArrayElement, UInt_t N>
215     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::UncheckedAt(UInt_t idx) const
216     {
217     // Return entry at given index.
218    
219     return static_cast<const ArrayElement*>(&fArray[idx]);
220     }
221 loizides 1.4
222     //--------------------------------------------------------------------------------------------------
223     template<class ArrayElement, UInt_t N>
224     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::operator[](UInt_t idx) const
225     {
226     // Return entry at given index.
227    
228     return At(idx);
229     }
230    
231     //--------------------------------------------------------------------------------------------------
232     template<class ArrayElement, UInt_t N>
233     inline ArrayElement *mithep::StackArray<ArrayElement, N>::operator[](UInt_t idx)
234     {
235     // Return entry at given index.
236    
237     return At(idx);
238     }
239 bendavid 1.1 #endif