ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/StackArray.h
Revision: 1.1
Committed: Fri Sep 19 11:56:08 2008 UTC (16 years, 7 months ago) by bendavid
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_005, Mit_004
Log Message:
Added StackArray

File Contents

# User Rev Content
1 bendavid 1.1 //--------------------------------------------------------------------------------------------------
2     // $Id: StackArray.h,v 1.2 2008/09/10 03:33:26 loizides Exp $
3     //
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     ~StackArray() {}
30    
31     void AddCopy(const ArrayElement &ae);
32     ArrayElement *AddNew();
33     ArrayElement *Allocate();
34     ArrayElement *At(UInt_t idx);
35     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     Bool_t IsOwner() const { return kTRUE; }
40     void Reset() { fSize = 0; }
41     void Trim() {}
42     ArrayElement *UncheckedAt(UInt_t idx);
43     const ArrayElement *UncheckedAt(UInt_t idx) const;
44    
45     ArrayElement *operator[](UInt_t idx);
46     const ArrayElement *operator[](UInt_t idx) const;
47    
48     protected:
49     TClass *fClass;//! pointer to TClass object used by streamer
50     UShort_t fSize; //size of array
51     ArrayElement fArray[N]; //storage of uids of referenced objects
52    
53     private:
54     StackArray(const StackArray &a);
55    
56     ClassDef(StackArray,1) // Implementation of our own TStackArray
57     };
58     }
59    
60     //--------------------------------------------------------------------------------------------------
61     template<class ArrayElement, UInt_t N>
62     inline mithep::StackArray<ArrayElement, N>::StackArray() :
63     fSize(0)
64     {
65     // Default constructor.
66     fClass = TClass::GetClass(typeid(ArrayElement));
67     }
68    
69     //--------------------------------------------------------------------------------------------------
70     template<class ArrayElement, UInt_t N>
71     void mithep::StackArray<ArrayElement, N>::AddCopy(const ArrayElement &ae)
72     {
73     // Add a copy of an existing object.
74    
75     if(fSize>=N) {
76     Fatal("Add", "Maximum number of references reached: To support more requires change in code!");
77     return;
78     }
79    
80     fArray[fSize] = ae;
81     ++fSize;
82     }
83    
84     //--------------------------------------------------------------------------------------------------
85     template<class ArrayElement, UInt_t N>
86     ArrayElement* mithep::StackArray<ArrayElement, N>::AddNew()
87     {
88     // Add new object.
89    
90     return new(Allocate()) ArrayElement();
91     }
92    
93     //--------------------------------------------------------------------------------------------------
94     template<class ArrayElement, UInt_t N>
95     ArrayElement* mithep::StackArray<ArrayElement, N>::Allocate()
96     {
97     // Allocate a slot in the array, *only* to be used in placement new operator.
98    
99     if(fSize>=N) {
100     Fatal("Add", "Maximum number of references reached: To support more requires change in code!");
101     return 0;
102     }
103    
104     ++fSize;
105     return &fArray[fSize-1];
106     }
107    
108     //--------------------------------------------------------------------------------------------------
109     template<class ArrayElement, UInt_t N>
110     inline ArrayElement *mithep::StackArray<ArrayElement, N>::At(UInt_t idx)
111     {
112     // Return entry at given index.
113    
114     if(idx<fSize)
115     return static_cast<ArrayElement*>(&fArray[idx]);
116    
117     Error("At", "Given index (%ud) is larger than array size (%ud)", idx, fSize);
118     return 0;
119     }
120    
121     //--------------------------------------------------------------------------------------------------
122     template<class ArrayElement, UInt_t N>
123     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::At(UInt_t idx) const
124     {
125     // Return entry at given index.
126    
127     if(idx<fSize)
128     return static_cast<const 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>::operator[](UInt_t idx) const
137     {
138     // Return entry at given index.
139    
140     return At(idx);
141     }
142    
143     //--------------------------------------------------------------------------------------------------
144     template<class ArrayElement, UInt_t N>
145     inline ArrayElement *mithep::StackArray<ArrayElement, N>::operator[](UInt_t idx)
146     {
147     // Return entry at given index.
148    
149     return At(idx);
150     }
151    
152     //-------------------------------------------------------------------------------------------------
153     template<class ArrayElement, UInt_t N>
154     void mithep::StackArray<ArrayElement, N>::Streamer(TBuffer &b)
155     {
156     // Stream all objects in the array to or from the I/O buffer.
157    
158     if (b.IsReading()) {
159     //UInt_t sv, cv;
160     //b.ReadVersion(&sv, &cv);
161     //TObject::Streamer(b);
162     b >> fSize;
163     if (fSize) {
164     b.ReadFastArray(fArray,fClass,fSize);
165     }
166     //b.CheckByteCount(sv, cv, StackArray::IsA());
167     } else { /*writing*/
168     //UInt_t cv;
169     //cv = b.WriteVersion(StackArray::IsA(), kTRUE);
170     //TObject::Streamer(b);
171     b << fSize;
172     if (fSize) {
173     b.WriteFastArray(fArray,fClass,fSize);
174     }
175     //b.SetByteCount(cv, kTRUE);
176     }
177     }
178    
179     //--------------------------------------------------------------------------------------------------
180     template<class ArrayElement, UInt_t N>
181     inline ArrayElement *mithep::StackArray<ArrayElement, N>::UncheckedAt(UInt_t idx)
182     {
183     // Return entry at given index.
184    
185     return static_cast<ArrayElement*>(&fArray[idx]);
186     }
187    
188     //--------------------------------------------------------------------------------------------------
189     template<class ArrayElement, UInt_t N>
190     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::UncheckedAt(UInt_t idx) const
191     {
192     // Return entry at given index.
193    
194     return static_cast<const ArrayElement*>(&fArray[idx]);
195     }
196     #endif