ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/StackArrayBasic.h
Revision: 1.8
Committed: Thu Mar 12 18:19:48 2009 UTC (16 years, 1 month ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_008pre2
Changes since 1.7: +12 -2 lines
Log Message:
Proper resetting of the cached entry in BaseCollection.

File Contents

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