source: trunk/Mars/mbase/MArrayF.h@ 13078

Last change on this file since 13078 was 9211, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 3.8 KB
Line 
1#ifndef MARS_MArrayF
2#define MARS_MArrayF
3
4#ifndef MARS_MArray
5#include "MArray.h"
6#endif
7
8#include <string.h>
9
10class TArrayF;
11class MArrayF : public MArray
12{
13private:
14 Float_t *fArray; //[fN] Array of fN chars
15
16public:
17
18 MArrayF()
19 {
20 fN = 0;
21 fArray = NULL;
22 }
23
24 MArrayF(UInt_t n)
25 {
26 fN = 0;
27 fArray = NULL;
28 Set(n);
29 }
30
31 MArrayF(UInt_t n, Float_t *array)
32 {
33 // Create TArrayC object and initialize it with values of array.
34 fN = 0;
35 fArray = NULL;
36 Set(n, array);
37 }
38
39 MArrayF(const MArrayF &array) : MArray()
40 {
41 // Copy constructor.
42 fArray = NULL;
43 Set(array.fN, array.fArray);
44 }
45
46 MArrayF &operator=(const MArrayF &rhs)
47 {
48 // TArrayC assignment operator.
49 if (this != &rhs)
50 Set(rhs.fN, rhs.fArray);
51 return *this;
52 }
53
54 virtual ~MArrayF()
55 {
56 // Delete TArrayC object.
57 delete [] fArray;
58 fArray = NULL;
59 }
60
61 void Adopt(UInt_t n, Float_t *array)
62 {
63 // Adopt array arr into TArrayC, i.e. don't copy arr but use it directly
64 // in TArrayC. User may not delete arr, TArrayC dtor will do it.
65 if (fArray)
66 delete [] fArray;
67
68 fN = n;
69 fArray = array;
70 }
71
72 void AddAt(Float_t c, UInt_t i)
73 {
74 // Add char c at position i. Check for out of bounds.
75 fArray[i] = c;
76 }
77
78 Float_t At(UInt_t i) const
79 {
80 return fArray[i];
81 }
82
83 Float_t *GetArray() const
84 {
85 return fArray;
86 }
87
88 void Reset()
89 {
90 memset(fArray, 0, fN*sizeof(Float_t));
91 }
92
93 void Reset(Float_t v)
94 {
95 for (Float_t *f=fArray; f<fArray+fN; f++)
96 *f = v;
97 }
98
99 void Set(UInt_t n)
100 {
101 // Set size of this array to n chars.
102 // A new array is created, the old contents copied to the new array,
103 // then the old array is deleted.
104
105 if (n==fN)
106 return;
107
108 Float_t *temp = fArray;
109 if (n == 0)
110 fArray = NULL;
111 else
112 {
113 fArray = new Float_t[n];
114 if (n < fN)
115 memcpy(fArray, temp, n*sizeof(Float_t));
116 else
117 {
118 memcpy(fArray, temp, fN*sizeof(Float_t));
119 memset(&fArray[fN], 0, (n-fN)*sizeof(Float_t));
120 }
121 }
122
123 if (fN)
124 delete [] temp;
125
126 fN = n;
127 }
128
129 void Set(UInt_t n, Float_t *array)
130 {
131 // Set size of this array to n chars and set the contents.
132 if (!array)
133 return;
134
135 if (fArray && fN != n)
136 {
137 delete [] fArray;
138 fArray = 0;
139 }
140 fN = n;
141
142 if (fN == 0)
143 return;
144
145 if (!fArray)
146 fArray = new Float_t[fN];
147
148 memcpy(fArray, array, n*sizeof(Float_t));
149 }
150
151 Float_t &operator[](UInt_t i)
152 {
153 return fArray[i];
154 }
155 const Float_t &operator[](UInt_t i) const
156 {
157 return fArray[i];
158 }
159
160 Double_t GetSum() const
161 {
162 const Float_t *end = fArray+fN;
163
164 Double_t sum = 0;
165 for (Float_t *ptr=fArray; ptr<end; ptr++)
166 sum += *ptr;
167
168 return sum;
169 }
170
171 template<class T>
172 void Add(T *src, Int_t n, Int_t p=0)
173 {
174 Float_t *dest = fArray + p;
175 Float_t *end = dest + n;
176
177 while (dest<end)
178 *dest++ += *src++;
179 }
180
181 template<class T>
182 void AddClipped(Double_t th, T src, Int_t n, Int_t p=0)
183 {
184 Float_t *dest = fArray + p;
185 Float_t *end = dest + n;
186
187 while (dest<end)
188 *dest++ += TMath::Min(*src++, th);
189 }
190
191 static void StripZeros(TArrayF &arr);
192 void StripZeros();
193
194 ClassDef(MArrayF, 1) //Array of Float_t
195};
196
197#endif
Note: See TracBrowser for help on using the repository browser.