ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/StackArray.h
Revision: 1.4
Committed: Thu Nov 20 17:49:15 2008 UTC (16 years, 5 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.3: +40 -37 lines
Log Message:
Add Print plus Cleaned up.

File Contents

# User Rev Content
1 bendavid 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.4 // $Id: StackArray.h,v 1.3 2008/10/31 18:56:14 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     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 loizides 1.4 UInt_t GetSize() const { return N; }
41 bendavid 1.1 Bool_t IsOwner() const { return kTRUE; }
42     void Reset() { fSize = 0; }
43     void Trim() {}
44     ArrayElement *UncheckedAt(UInt_t idx);
45     const ArrayElement *UncheckedAt(UInt_t idx) const;
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 loizides 1.4 ArrayElement fArray[N]; //storage for objects
53 bendavid 1.1
54 loizides 1.4 ClassDef(StackArray,1) // Array on stack for arbitrary classes
55 bendavid 1.1 };
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     template<class ArrayElement, UInt_t N>
69     inline mithep::StackArray<ArrayElement, N>::StackArray(const StackArray &a) :
70     fClass(a.fClass),
71     fSize(a.fSize)
72     {
73 loizides 1.4 // Copy constructor. Copy only elements which are used.
74    
75 bendavid 1.3 for (UInt_t i=0; i<fSize; ++i)
76     fArray[i] = a.fArray[i];
77 bendavid 1.1 }
78    
79     //--------------------------------------------------------------------------------------------------
80     template<class ArrayElement, UInt_t N>
81     void mithep::StackArray<ArrayElement, N>::AddCopy(const ArrayElement &ae)
82     {
83     // Add a copy of an existing object.
84    
85     if(fSize>=N) {
86 loizides 1.4 TObject::Fatal("AddCopy", "Maximum number of slots reached (%d>=%d): "
87     "To support more requires a different template!", fSize, N);
88 bendavid 1.1 return;
89     }
90    
91     fArray[fSize] = ae;
92     ++fSize;
93     }
94    
95     //--------------------------------------------------------------------------------------------------
96     template<class ArrayElement, UInt_t N>
97     ArrayElement* mithep::StackArray<ArrayElement, N>::AddNew()
98     {
99     // Add new object.
100    
101     return new(Allocate()) ArrayElement();
102     }
103    
104     //--------------------------------------------------------------------------------------------------
105     template<class ArrayElement, UInt_t N>
106     ArrayElement* mithep::StackArray<ArrayElement, N>::Allocate()
107     {
108 loizides 1.4 // Return next slot in the array, *only* to be used in placement new operator.
109 bendavid 1.1
110     if(fSize>=N) {
111 loizides 1.4 TObject::Fatal("Allocate", "Maximum number of slots reached (%d>=%d): "
112     "To support more requires a different template!", fSize, N);
113 bendavid 1.1 return 0;
114     }
115    
116     ++fSize;
117     return &fArray[fSize-1];
118     }
119    
120     //--------------------------------------------------------------------------------------------------
121     template<class ArrayElement, UInt_t N>
122     inline ArrayElement *mithep::StackArray<ArrayElement, N>::At(UInt_t idx)
123     {
124     // Return entry at given index.
125    
126     if(idx<fSize)
127     return static_cast<ArrayElement*>(&fArray[idx]);
128    
129 loizides 1.4 ArrayElement tmp;
130     TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
131     idx, fSize, this->GetName(), typeid(tmp).name());
132 bendavid 1.1 return 0;
133     }
134    
135     //--------------------------------------------------------------------------------------------------
136     template<class ArrayElement, UInt_t N>
137     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::At(UInt_t idx) const
138     {
139     // Return entry at given index.
140    
141     if(idx<fSize)
142     return static_cast<const ArrayElement*>(&fArray[idx]);
143    
144 loizides 1.4 ArrayElement tmp;
145     TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
146     idx, fSize, this->GetName(), typeid(tmp).name());
147 bendavid 1.1 return 0;
148     }
149    
150     //-------------------------------------------------------------------------------------------------
151     template<class ArrayElement, UInt_t N>
152     void mithep::StackArray<ArrayElement, N>::Streamer(TBuffer &b)
153     {
154     // Stream all objects in the array to or from the I/O buffer.
155    
156     if (b.IsReading()) {
157     b >> fSize;
158     if (fSize) {
159     b.ReadFastArray(fArray,fClass,fSize);
160     }
161     } else { /*writing*/
162     b << fSize;
163     if (fSize) {
164     b.WriteFastArray(fArray,fClass,fSize);
165     }
166     }
167     }
168    
169     //--------------------------------------------------------------------------------------------------
170     template<class ArrayElement, UInt_t N>
171     inline ArrayElement *mithep::StackArray<ArrayElement, N>::UncheckedAt(UInt_t idx)
172     {
173     // Return entry at given index.
174    
175     return static_cast<ArrayElement*>(&fArray[idx]);
176     }
177    
178     //--------------------------------------------------------------------------------------------------
179     template<class ArrayElement, UInt_t N>
180     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::UncheckedAt(UInt_t idx) const
181     {
182     // Return entry at given index.
183    
184     return static_cast<const ArrayElement*>(&fArray[idx]);
185     }
186 loizides 1.4
187     //--------------------------------------------------------------------------------------------------
188     template<class ArrayElement, UInt_t N>
189     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::operator[](UInt_t idx) const
190     {
191     // Return entry at given index.
192    
193     return At(idx);
194     }
195    
196     //--------------------------------------------------------------------------------------------------
197     template<class ArrayElement, UInt_t N>
198     inline ArrayElement *mithep::StackArray<ArrayElement, N>::operator[](UInt_t idx)
199     {
200     // Return entry at given index.
201    
202     return At(idx);
203     }
204 bendavid 1.1 #endif