ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/StackArrayBasic.h
Revision: 1.3
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.2: +15 -13 lines
Log Message:
Implemented ObjAt function.

File Contents

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