source: tags/Mars-V2.1.1/mbase/MArrayD.h

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