ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/StackArrayBasic.h
Revision: 1.2
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.1: +45 -48 lines
Log Message:
Add Print plus Cleaned up.

File Contents

# User Rev Content
1 bendavid 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.2 // $Id: StackArrayBasic.h,v 1.1 2008/10/31 18:56:14 bendavid Exp $
3 bendavid 1.1 //
4     // StackArrayBasic
5     //
6 loizides 1.2 // 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 basic data types as opposed to StackArray which can hold
10     // classes.
11 bendavid 1.1 //
12     // Authors: C.Loizides, J.Bendavid
13     //--------------------------------------------------------------------------------------------------
14    
15     #ifndef MITANA_DATACONT_STACKARRAYBASIC
16     #define MITANA_DATACONT_STACKARRAYBASIC
17    
18     #include <TObject.h>
19     #include "MitAna/DataCont/interface/Collection.h"
20    
21     namespace mithep
22     {
23     template<class ArrayElement, UInt_t N>
24     class StackArrayBasic : public TObject
25     {
26     public:
27     StackArrayBasic();
28     StackArrayBasic(const StackArrayBasic &a);
29     ~StackArrayBasic() {}
30    
31     void Add(const ArrayElement &ae);
32     ArrayElement At(UInt_t idx);
33     const ArrayElement At(UInt_t idx) const;
34     void Clear(Option_t */*opt*/="") {}
35     UInt_t Entries() const { return GetEntries(); }
36     UInt_t GetEntries() const { return fSize; }
37 loizides 1.2 UInt_t GetSize() const { return N; }
38 bendavid 1.1 Bool_t IsOwner() const { return kTRUE; }
39     void Reset() { fSize = 0; }
40     void Trim() {}
41     ArrayElement UncheckedAt(UInt_t idx);
42     const ArrayElement UncheckedAt(UInt_t idx) const;
43     ArrayElement operator[](UInt_t idx);
44     const ArrayElement operator[](UInt_t idx) const;
45    
46     protected:
47 loizides 1.2 UShort_t fSize; //size of array
48     ArrayElement fArray[N]; //storage for basic types
49 bendavid 1.1
50 loizides 1.2 ClassDef(StackArrayBasic,1) // Array on stack for basic types
51 bendavid 1.1 };
52     }
53    
54     //--------------------------------------------------------------------------------------------------
55     template<class ArrayElement, UInt_t N>
56     inline mithep::StackArrayBasic<ArrayElement, N>::StackArrayBasic() :
57     fSize(0)
58     {
59 loizides 1.2 // Default constructor.
60 bendavid 1.1 }
61    
62     //--------------------------------------------------------------------------------------------------
63     template<class ArrayElement, UInt_t N>
64     inline mithep::StackArrayBasic<ArrayElement, N>::StackArrayBasic(const StackArrayBasic &a) :
65     fSize(a.fSize)
66     {
67 loizides 1.2 // Copy constructor. Copy only elements which are used.
68    
69 bendavid 1.1 for (UInt_t i=0; i<fSize; ++i)
70     fArray[i] = a.fArray[i];
71     }
72    
73     //--------------------------------------------------------------------------------------------------
74     template<class ArrayElement, UInt_t N>
75     void mithep::StackArrayBasic<ArrayElement, N>::Add(const ArrayElement &ae)
76     {
77 loizides 1.2 // Add element to array.
78 bendavid 1.1
79     if(fSize>=N) {
80 loizides 1.2 TObject::Fatal("Add", "Maximum number of slots reached (%d>=%d): "
81     "To support more requires a different template!", fSize, N);
82 bendavid 1.1 return;
83     }
84    
85     fArray[fSize] = ae;
86     ++fSize;
87     }
88    
89     //--------------------------------------------------------------------------------------------------
90     template<class ArrayElement, UInt_t N>
91 loizides 1.2 inline ArrayElement mithep::StackArrayBasic<ArrayElement, N>::At(UInt_t idx)
92 bendavid 1.1 {
93     // Return entry at given index.
94    
95     if(idx<fSize)
96     return fArray[idx];
97    
98 loizides 1.2 ArrayElement tmp;
99     TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
100     idx, fSize, GetName(), typeid(tmp).name());
101     return 0;
102 bendavid 1.1 }
103    
104     //--------------------------------------------------------------------------------------------------
105     template<class ArrayElement, UInt_t N>
106     inline const ArrayElement mithep::StackArrayBasic<ArrayElement, N>::At(UInt_t idx) const
107     {
108     // Return entry at given index.
109    
110     if(idx<fSize)
111     return fArray[idx];
112    
113 loizides 1.2 ArrayElement tmp;
114     TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
115     idx, fSize, GetName(), typeid(tmp).name());
116     return 0;
117 bendavid 1.1 }
118    
119     //-------------------------------------------------------------------------------------------------
120     template<class ArrayElement, UInt_t N>
121     void mithep::StackArrayBasic<ArrayElement, N>::Streamer(TBuffer &b)
122     {
123     // Stream all objects in the array to or from the I/O buffer.
124    
125     if (b.IsReading()) {
126     b >> fSize;
127     if (fSize) {
128     b.ReadFastArray(fArray,fSize);
129     }
130     } else { /*writing*/
131     b << fSize;
132     if (fSize) {
133     b.WriteFastArray(fArray,fSize);
134     }
135     }
136     }
137    
138     //--------------------------------------------------------------------------------------------------
139     template<class ArrayElement, UInt_t N>
140 loizides 1.2 inline ArrayElement mithep::StackArrayBasic<ArrayElement, N>::UncheckedAt(UInt_t idx)
141 bendavid 1.1 {
142     // Return entry at given index.
143    
144     return fArray[idx];
145     }
146    
147     //--------------------------------------------------------------------------------------------------
148     template<class ArrayElement, UInt_t N>
149 loizides 1.2 inline const ArrayElement mithep::StackArrayBasic<ArrayElement, N>::UncheckedAt(UInt_t idx) const
150 bendavid 1.1 {
151     // Return entry at given index.
152    
153     return fArray[idx];
154     }
155 loizides 1.2
156     //--------------------------------------------------------------------------------------------------
157     template<class ArrayElement, UInt_t N>
158     inline const ArrayElement mithep::StackArrayBasic<ArrayElement, N>::operator[](UInt_t idx) const
159     {
160     // Return entry at given index.
161    
162     return At(idx);
163     }
164    
165     //--------------------------------------------------------------------------------------------------
166     template<class ArrayElement, UInt_t N>
167     inline ArrayElement mithep::StackArrayBasic<ArrayElement, N>::operator[](UInt_t idx)
168     {
169     // Return entry at given index.
170    
171     return At(idx);
172     }
173 bendavid 1.1 #endif