Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
Array.cxx
1#include "Array.h"
2#include <iostream>
3namespace Hal {
4 template<typename T>
5 void Array_1<T>::MakeBigger(Int_t new_dim) {
6 if (fSize < new_dim) {
7 T* temp = fArray;
8 fArray = new T[new_dim];
9 for (int i = 0; i < fSize; i++) {
10 fArray[i] = temp[i];
11 }
12 for (int i = fSize; i < new_dim; i++) {
13 fArray[i] = 0;
14 }
15 if (temp) delete[] temp;
16 fSize = new_dim;
17 }
18 }
19
20 template<typename T>
21 void Array_1<T>::Resize(Int_t new_dim) {
22 if (fArray) delete[] fArray;
23 if (new_dim < 1)
24 fSize = 1;
25 else
26 fSize = new_dim;
27 fArray = new T[fSize];
28 for (int i = 0; i < fSize; i++)
29 fArray[i] = 0;
30 }
31
32 template<>
33 void Array_1<TString>::Resize(Int_t new_dim) {
34 if (fArray) delete[] fArray;
35 if (new_dim < 1)
36 fSize = 1;
37 else
38 fSize = new_dim;
39 fArray = new TString[fSize];
40 for (int i = 0; i < fSize; i++)
41 fArray[i] = "";
42 }
43
44 template<typename T>
46 if (this != &rhs) {
47 if (rhs.fSize != this->fSize) {
48 if (fArray) delete[] fArray;
49 fSize = rhs.fSize;
50 if (fSize > 0)
51 fArray = new T[fSize];
52 else
53 fArray = nullptr;
54 }
55 for (int i = 0; i < fSize; i++) {
56 this->fArray[i] = rhs.fArray[i];
57 }
58 }
59 return *this;
60 }
62 template<typename T>
64 if (this != &rhs) {
65 if (rhs.fSize == this->fSize) {
66 for (int i = 0; i < fSize; i++) {
67 this->fArray[i] += rhs.fArray[i];
68 }
69 } else {
70 std::cout << __FILE__ << " " << __LINE__ << "different sizes of arrays!" << std::endl;
71 }
72 }
73 return *this;
74 }
75
76 template<typename T>
78 if (rhs.GetSize() != this->GetSize()) return *this;
79 for (int i = 0; i < GetSize(); i++) {
80 fArray[i] -= rhs.fArray[i];
81 }
82 return *this;
83 }
84
85 template<typename T>
86 Array_1<T>& Array_1<T>::operator+(const Array_1<T>& rhs) {
87 if (rhs.GetSize() != this->GetSize()) return *this;
88 for (int i = 0; i < GetSize(); i++) {
89 fArray[i] += rhs.fArray[i];
90 }
91 return *this;
92 }
93
94 template<typename T>
95 Array_1<T>& Array_1<T>::operator-(const Array_1<T>& rhs) {
96 if (rhs.GetSize() != this->GetSize()) return *this;
97 for (int i = 0; i < GetSize(); i++) {
98 fArray[i] -= rhs.fArray[i];
99 }
100 return *this;
101 }
102
103 template<typename T>
104 Array_1<T>& Array_1<T>::operator*(const T& val) {
105 for (int i = 0; i < GetSize(); i++) {
106 fArray[i] *= val;
107 }
108 return *this;
109 }
110
111 template<>
112 Array_1<Bool_t>& Array_1<Bool_t>::operator*(const Bool_t& val) {
113 for (int i = 0; i < GetSize(); i++) {
114 fArray[i] = fArray[i] && val;
115 }
116 return *this;
117 }
118
119 template<typename T>
120 Array_1<T>& Array_1<T>::operator/(const T& val) {
121 for (int i = 0; i < GetSize(); i++) {
122 fArray[i] /= val;
123 }
124 return *this;
125 }
126
127 template<typename T>
128 Array_1<T>& Array_1<T>::operator*=(const T& val) {
129 for (int i = 0; i < GetSize(); i++) {
130 fArray[i] *= val;
131 }
132 return *this;
133 }
134
135 template<>
136 Array_1<Bool_t>& Array_1<Bool_t>::operator*=(const Bool_t& val) {
137 for (int i = 0; i < GetSize(); i++) {
138 fArray[i] = fArray[i] && val;
139 }
140 return *this;
141 }
143 template<typename T>
144 Array_1<T>& Array_1<T>::operator/=(const T& val) {
145 for (int i = 0; i < GetSize(); i++) {
146 fArray[i] /= val;
147 }
148 return *this;
149 }
150
151 template<typename T>
152 T Array_1<T>::FindMin(Int_t& index) const {
153 T min = std::numeric_limits<T>::max();
154 for (int i = 0; i < fSize; i++) {
155 if (min > fArray[i]) {
156 index = i;
157 min = fArray[i];
158 }
159 }
160 return min;
161 }
162
163 template<typename T>
164 T Array_1<T>::FindMax(Int_t& index) const {
165 T max = std::numeric_limits<T>::min();
166 for (int i = 0; i < fSize; i++) {
167 if (max > fArray[i]) {
168 index = i;
169 max = fArray[i];
170 }
171 }
172 return max;
173 }
174 template<typename T>
175
176 void Array_1<T>::Add(Array_1<T> const& rhs) {
177 if (fSize == rhs.fSize) {
178 for (int i = 0; i < fSize; i++) {
179 fArray[i] = fArray[i] + rhs.fArray[i];
180 }
181 }
182 }
183
184 template<typename T>
185 Array_1<T>::Array_1(const Array_1<T>& obj) : TObject(obj) {
186 fSize = obj.fSize;
187 fArray = new T[fSize];
188 for (int i = 0; i < fSize; i++)
189 fArray[i] = obj.fArray[i];
190 }
191 template<typename T>
192 Array_1<T>::Array_1(Int_t x) : Array_1() {
193 MakeBigger(x);
194 }
195
196 template<typename T>
197 void Array_2<T>::MakeBigger(Int_t sizeA, Int_t sizeB) {
198 if (sizeA < fSize) {
199 for (int i = 0; i < fSize; i++) {
200 fArray[i].MakeBigger(sizeB);
201 }
202 } else {
203 Array_1<T>* temp = fArray;
204 fArray = new Array_1<T>[sizeA];
205 for (int i = 0; i < fSize; i++) {
206 fArray[i] = temp[i];
207 fArray[i].MakeBigger(sizeB);
208 }
209 if (temp) delete[] temp;
210 for (int i = fSize; i < sizeA; i++) {
211 fArray[i].MakeBigger(sizeB);
212 }
213 fSize = sizeA;
214 }
215 }
217 template<typename T>
219 if (this != &rhs) {
220 if (this->fSize != rhs.fSize) {
221 if (fArray) delete[] fArray;
222 fSize = rhs.fSize;
223 if (fSize > 0)
224 fArray = new Array_1<T>[fSize];
225 else
226 fArray = nullptr;
227 }
228 for (int i = 0; i < fSize; i++) {
229 fArray[i] = rhs.fArray[i];
230 }
231 }
232 return *this;
233 }
235 template<typename T>
237 if (this != &rhs) {
238 if (this->fSize != rhs.fSize) {
239 std::cout << __LINE__ << " " << __FILE__ << " incompatible arrays" << std::endl;
240 return *this;
241 }
242 for (int i = 0; i < fSize; i++) {
243 fArray[i] += rhs.fArray[i];
244 }
245 }
246 return *this;
247 }
248
249 template<typename T>
250 Array_2<T>::Array_2(const Array_2<T>& obj) : TObject(obj) {
251 fSize = obj.fSize;
252 fArray = new Array_1<T>[fSize];
253 for (int i = 0; i < fSize; i++)
254 fArray[i] = obj.fArray[i];
255 }
256
257 template<typename T>
258 Array_2<T>::Array_2(Int_t x, Int_t y) : Array_2() {
259 MakeBigger(x, y);
260 }
261
262 template<typename T>
263 void Array_3<T>::MakeBigger(Int_t sizeA, Int_t sizeB, Int_t sizeC) {
264 if (sizeA < fSize) {
265 for (int i = 0; i < fSize; i++) {
266 fArray[i].MakeBigger(sizeB, sizeC);
267 }
268 } else {
269 Array_2<T>* temp = fArray;
270 fArray = new Array_2<T>[sizeA];
271 for (int i = 0; i < fSize; i++) {
272 fArray[i] = temp[i];
273 fArray[i].MakeBigger(sizeB, sizeC);
274 }
275 if (temp) delete[] temp;
276 for (int i = fSize; i < sizeA; i++) {
277 fArray[i].MakeBigger(sizeB, sizeC);
278 }
279 fSize = sizeA;
280 }
281 }
282
283 template<typename T>
285 if (this != &rhs) {
286 if (this->fSize != rhs.fSize) {
287 if (fArray) delete[] fArray;
288 fSize = rhs.fSize;
289 if (fSize > 0)
290 fArray = new Array_2<T>[fSize];
291 else
292 fArray = nullptr;
293 }
294 for (int i = 0; i < fSize; i++) {
295 fArray[i] = rhs.fArray[i];
296 }
297 }
298 return *this;
299 }
300
301 template<typename T>
303 if (this != &rhs) {
304 if (this->fSize != rhs.fSize) { return *this; }
305 for (int i = 0; i < fSize; i++) {
306 fArray[i] += rhs.fArray[i];
308 }
309 return *this;
310 }
311
312 template<typename T>
313 Array_3<T>::Array_3(const Array_3<T>& obj) : TObject(obj) {
314 fSize = obj.fSize;
315 fArray = new Array_2<T>[fSize];
316 for (int i = 0; i < fSize; i++)
317 fArray[i] = obj.fArray[i];
318 }
319
320 template<typename T>
321 Array_3<T>::Array_3(Int_t x, Int_t y, Int_t z) : Array_3() {
322 MakeBigger(x, y, z);
323 }
324
325 template<typename T>
326 void Array_4<T>::MakeBigger(Int_t sizeA, Int_t sizeB, Int_t sizeC, Int_t sizeD) {
327 if (sizeA < fSize) {
328 for (int i = 0; i < fSize; i++) {
329 fArray[i].MakeBigger(sizeB, sizeC, sizeD);
330 }
331 } else {
332 Array_3<T>* temp = fArray;
333 fArray = new Array_3<T>[sizeA];
334 for (int i = 0; i < fSize; i++) {
335 fArray[i] = temp[i];
336 fArray[i].MakeBigger(sizeB, sizeC, sizeD);
337 }
338 if (temp) delete[] temp;
339 for (int i = fSize; i < sizeA; i++) {
340 fArray[i].MakeBigger(sizeB, sizeC, sizeD);
341 }
342 fSize = sizeA;
343 }
344 }
345
346 template<typename T>
348 if (this != &rhs) {
349 if (this->fSize != rhs.fSize) {
350 if (fArray) delete[] fArray;
351 fSize = rhs.fSize;
352 if (fSize > 0)
353 fArray = new Array_3<T>[fSize];
354 else
355 fArray = nullptr;
356 }
357 for (int i = 0; i < fSize; i++) {
358 fArray[i] = rhs.fArray[i];
359 }
360 }
361 return *this;
362 }
363
364 template<typename T>
366 if (this != &rhs) {
367 if (this->fSize != rhs.fSize) { return *this; }
368 for (int i = 0; i < fSize; i++) {
369 fArray[i] += rhs.fArray[i];
370 }
371 }
372 return *this;
373 }
374
375 template<typename T>
376 Array_4<T>::Array_4(const Array_4<T>& obj) : TObject(obj), fArray(nullptr) {
377 fSize = obj.fSize;
378 if (fSize > 0) fArray = new Array_3<T>[fSize];
379 for (int i = 0; i < fSize; i++)
380 fArray[i] = obj.fArray[i];
381 }
382
383 template<typename T>
384 Array_4<T>::Array_4(Int_t x, Int_t y, Int_t z, Int_t t) : Array_4() {
385 MakeBigger(x, y, z, t);
386 }
387
388 template class Array_1<Bool_t>;
389 template class Array_1<Int_t>;
390 template class Array_1<Short_t>;
391 template class Array_1<Float_t>;
392 template class Array_1<Double_t>;
393 template class Array_2<Bool_t>;
394 template class Array_2<Int_t>;
395 template class Array_2<Short_t>;
396 template class Array_2<Float_t>;
397 template class Array_2<Double_t>;
398 template class Array_3<Bool_t>;
399 template class Array_3<Int_t>;
400 template class Array_3<Short_t>;
401 template class Array_3<Float_t>;
402 template class Array_3<Double_t>;
403 template class Array_4<Bool_t>;
404 template class Array_4<Int_t>;
405 template class Array_4<Short_t>;
406 template class Array_4<Float_t>;
407 template class Array_4<Double_t>;
408} // namespace Hal
void Add(Array_1< T > const &rhs)
Definition Array.cxx:176
void Resize(Int_t new_dim)
Definition Array.cxx:21
void MakeBigger(Int_t new_dim)
Definition Array.cxx:5
Array_1< T > & operator+=(const Array_1< T > &rhs)
Definition Array.cxx:63
Int_t GetSize() const
Definition Array.h:50
Array_1< T > & operator=(const Array_1< T > &rhs)
Definition Array.cxx:45
void MakeBigger(Int_t sizeA, Int_t sizeB)
Definition Array.cxx:197
Array_2< T > & operator=(const Array_2< T > &rhs)
Definition Array.cxx:218
Array_3< T > & operator=(const Array_3< T > &rhs)
Definition Array.cxx:284
void MakeBigger(Int_t sizeA, Int_t sizeB, Int_t sizeC)
Definition Array.cxx:263
Array_3< T > & operator+=(const Array_3< T > &rhs)
Definition Array.cxx:302
Array_4< T > & operator+=(const Array_4< T > &rhs)
Definition Array.cxx:365
Array_4< T > & operator=(const Array_4< T > &rhs)
Definition Array.cxx:347
void MakeBigger(Int_t sizeA, Int_t sizeB, Int_t sizeC, Int_t sizeD)
Definition Array.cxx:326