ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/StackArray.h
Revision: 1.3
Committed: Fri Oct 31 18:56:14 2008 UTC (16 years, 6 months ago) by bendavid
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_006
Changes since 1.2: +16 -5 lines
Log Message:
Switched to new optimized RefArray now supporting multiple PIDs

File Contents

# User Rev Content
1 bendavid 1.1 //--------------------------------------------------------------------------------------------------
2 bendavid 1.3 // $Id: StackArray.h,v 1.2 2008/10/23 18:22:27 loizides Exp $
3 bendavid 1.1 //
4     // StackArray
5     //
6     // Implementation of a TStackArray using stack (and not heap) memory.
7     // For various reasons, the array can not be written in split mode.
8     // Maximum size of references is set to 1024 (but this could be
9     // changed if there is need for it).
10     //
11     // Authors: C.Loizides, J.Bendavid
12     //--------------------------------------------------------------------------------------------------
13    
14     #ifndef MITANA_DATACONT_STACKARRAY
15     #define MITANA_DATACONT_STACKARRAY
16    
17     #include <TObject.h>
18     #include <TError.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     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     Bool_t IsOwner() const { return kTRUE; }
41     void Reset() { fSize = 0; }
42     void Trim() {}
43     ArrayElement *UncheckedAt(UInt_t idx);
44     const ArrayElement *UncheckedAt(UInt_t idx) const;
45    
46     ArrayElement *operator[](UInt_t idx);
47     const ArrayElement *operator[](UInt_t idx) const;
48    
49     protected:
50 loizides 1.2 TClass *fClass; //!pointer to TClass object used by streamer
51     UShort_t fSize; //size of array
52     ArrayElement fArray[N]; //storage of uids of referenced objects
53 bendavid 1.1
54     ClassDef(StackArray,1) // Implementation of our own TStackArray
55     };
56     }
57    
58     //--------------------------------------------------------------------------------------------------
59     template<class ArrayElement, UInt_t N>
60     inline mithep::StackArray<ArrayElement, N>::StackArray() :
61 bendavid 1.3 fClass(TClass::GetClass(typeid(ArrayElement))),
62 bendavid 1.1 fSize(0)
63     {
64     // Default constructor.
65 bendavid 1.3
66     }
67    
68     //--------------------------------------------------------------------------------------------------
69     template<class ArrayElement, UInt_t N>
70     inline mithep::StackArray<ArrayElement, N>::StackArray(const StackArray &a) :
71     fClass(a.fClass),
72     fSize(a.fSize)
73     {
74     // Copy constructor. Copy only elements which are used.
75     for (UInt_t i=0; i<fSize; ++i)
76     fArray[i] = a.fArray[i];
77    
78 bendavid 1.1 }
79    
80     //--------------------------------------------------------------------------------------------------
81     template<class ArrayElement, UInt_t N>
82     void mithep::StackArray<ArrayElement, N>::AddCopy(const ArrayElement &ae)
83     {
84     // Add a copy of an existing object.
85    
86     if(fSize>=N) {
87 loizides 1.2 TObject::Fatal("Add",
88     "Maximum number of references reached: To support more requires change code!");
89 bendavid 1.1 return;
90     }
91    
92     fArray[fSize] = ae;
93     ++fSize;
94     }
95    
96     //--------------------------------------------------------------------------------------------------
97     template<class ArrayElement, UInt_t N>
98     ArrayElement* mithep::StackArray<ArrayElement, N>::AddNew()
99     {
100     // Add new object.
101    
102     return new(Allocate()) ArrayElement();
103     }
104    
105     //--------------------------------------------------------------------------------------------------
106     template<class ArrayElement, UInt_t N>
107     ArrayElement* mithep::StackArray<ArrayElement, N>::Allocate()
108     {
109     // Allocate a slot in the array, *only* to be used in placement new operator.
110    
111     if(fSize>=N) {
112 loizides 1.2 TObject::Fatal("Add",
113     "Maximum number of references reached: To support more requires change code!");
114 bendavid 1.1 return 0;
115     }
116    
117     ++fSize;
118     return &fArray[fSize-1];
119     }
120    
121     //--------------------------------------------------------------------------------------------------
122     template<class ArrayElement, UInt_t N>
123     inline ArrayElement *mithep::StackArray<ArrayElement, N>::At(UInt_t idx)
124     {
125     // Return entry at given index.
126    
127     if(idx<fSize)
128     return static_cast<ArrayElement*>(&fArray[idx]);
129    
130     Error("At", "Given index (%ud) is larger than array size (%ud)", idx, fSize);
131     return 0;
132     }
133    
134     //--------------------------------------------------------------------------------------------------
135     template<class ArrayElement, UInt_t N>
136     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::At(UInt_t idx) const
137     {
138     // Return entry at given index.
139    
140     if(idx<fSize)
141     return static_cast<const ArrayElement*>(&fArray[idx]);
142    
143     Error("At", "Given index (%ud) is larger than array size (%ud)", idx, fSize);
144     return 0;
145     }
146    
147     //--------------------------------------------------------------------------------------------------
148     template<class ArrayElement, UInt_t N>
149     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::operator[](UInt_t idx) const
150     {
151     // Return entry at given index.
152    
153     return At(idx);
154     }
155    
156     //--------------------------------------------------------------------------------------------------
157     template<class ArrayElement, UInt_t N>
158     inline ArrayElement *mithep::StackArray<ArrayElement, N>::operator[](UInt_t idx)
159     {
160     // Return entry at given index.
161    
162     return At(idx);
163     }
164    
165     //-------------------------------------------------------------------------------------------------
166     template<class ArrayElement, UInt_t N>
167     void mithep::StackArray<ArrayElement, N>::Streamer(TBuffer &b)
168     {
169     // Stream all objects in the array to or from the I/O buffer.
170    
171     if (b.IsReading()) {
172     b >> fSize;
173     if (fSize) {
174     b.ReadFastArray(fArray,fClass,fSize);
175     }
176     } else { /*writing*/
177     b << fSize;
178     if (fSize) {
179     b.WriteFastArray(fArray,fClass,fSize);
180     }
181     }
182     }
183    
184     //--------------------------------------------------------------------------------------------------
185     template<class ArrayElement, UInt_t N>
186     inline ArrayElement *mithep::StackArray<ArrayElement, N>::UncheckedAt(UInt_t idx)
187     {
188     // Return entry at given index.
189    
190     return static_cast<ArrayElement*>(&fArray[idx]);
191     }
192    
193     //--------------------------------------------------------------------------------------------------
194     template<class ArrayElement, UInt_t N>
195     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::UncheckedAt(UInt_t idx) const
196     {
197     // Return entry at given index.
198    
199     return static_cast<const ArrayElement*>(&fArray[idx]);
200     }
201     #endif