1 | // template array class for numerical types
|
---|
2 | // R. Ansari, C.Magneville 03/2000
|
---|
3 |
|
---|
4 | #include "machdefs.h"
|
---|
5 | #include <stdio.h>
|
---|
6 | #include <stdlib.h>
|
---|
7 | #include "pexceptions.h"
|
---|
8 | #include "tarray.h"
|
---|
9 |
|
---|
10 |
|
---|
11 |
|
---|
12 |
|
---|
13 | // -------------------------------------------------------
|
---|
14 | // Methodes de la classe
|
---|
15 | // -------------------------------------------------------
|
---|
16 |
|
---|
17 | // Les constructeurs
|
---|
18 | template <class T>
|
---|
19 | TArray<T>::TArray()
|
---|
20 | : BaseArray() , mNDBlock()
|
---|
21 | // Default constructor
|
---|
22 | {
|
---|
23 | }
|
---|
24 |
|
---|
25 | template <class T>
|
---|
26 | TArray<T>::TArray(uint_4 ndim, const uint_4 * siz, uint_4 step)
|
---|
27 | : BaseArray() , mNDBlock(ComputeTotalSize(ndim, siz, step, 1))
|
---|
28 | {
|
---|
29 | string exmsg = "TArray<T>::TArray(uint_4, uint_4 *, uint_4)";
|
---|
30 | if (!UpdateSizes(ndim, siz, step, 0, exmsg)) throw( ParmError(exmsg) );
|
---|
31 | }
|
---|
32 |
|
---|
33 | template <class T>
|
---|
34 | TArray<T>::TArray(uint_4 nx, uint_4 ny, uint_4 nz, uint_4 nt, uint_4 nu)
|
---|
35 | : BaseArray() , mNDBlock(nx*((ny>0)?ny:1)*((nz>0)?nz:1)*((nt>0)?nt:1)*((nu>0)?nu:1))
|
---|
36 | {
|
---|
37 | uint_4 size[BASEARRAY_MAXNDIMS];
|
---|
38 | size[0] = nx; size[1] = ny; size[2] = nz;
|
---|
39 | size[3] = nt; size[4] = nu;
|
---|
40 | int ndim = 1;
|
---|
41 | if ((size[1] > 0) && (size[2] > 0) && (size[3] > 0) && (size[4] > 0) ) ndim = 5;
|
---|
42 | else if ((size[1] > 0) && (size[2] > 0) && (size[3] > 0) ) ndim = 4;
|
---|
43 | else if ((size[1] > 0) && (size[2] > 0)) ndim = 3;
|
---|
44 | else if (size[1] > 0) ndim = 2;
|
---|
45 | else ndim = 1;
|
---|
46 | string exmsg = "TArray<T>::TArray(uint_4, uint_4, uint_4)";
|
---|
47 | if (!UpdateSizes(ndim, size, 1, 0, exmsg)) throw( ParmError(exmsg) );
|
---|
48 | }
|
---|
49 |
|
---|
50 | template <class T>
|
---|
51 | TArray<T>::TArray(uint_4 ndim, const uint_4 * siz, NDataBlock<T> & db, bool share, uint_4 step, uint_8 offset)
|
---|
52 | : BaseArray() , mNDBlock(db, share)
|
---|
53 | {
|
---|
54 | string exmsg = "TArray<T>::TArray(uint_4, uint_4 *, NDataBlock<T> & ... )";
|
---|
55 | if (!UpdateSizes(ndim, siz, step, offset, exmsg)) throw( ParmError(exmsg) );
|
---|
56 |
|
---|
57 | }
|
---|
58 |
|
---|
59 | template <class T>
|
---|
60 | TArray<T>::TArray(uint_4 ndim, const uint_4 * siz, T* values, uint_4 step, uint_8 offset, Bridge* br)
|
---|
61 | : BaseArray() , mNDBlock(ComputeTotalSize(ndim, siz, step, 1), values, br)
|
---|
62 | {
|
---|
63 | string exmsg = "TArray<T>::TArray(uint_4, uint_4 *, T* ... )";
|
---|
64 | if (!UpdateSizes(ndim, siz, step, offset, exmsg)) throw( ParmError(exmsg) );
|
---|
65 | }
|
---|
66 |
|
---|
67 | template <class T>
|
---|
68 | TArray<T>::TArray(const TArray<T>& a)
|
---|
69 | : BaseArray() , mNDBlock(a.mNDBlock)
|
---|
70 | {
|
---|
71 | string exmsg = "TArray<T>::TArray(const TArray<T>&)";
|
---|
72 | if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
|
---|
73 | if (a.mInfo) mInfo = new DVList(*(a.mInfo));
|
---|
74 | }
|
---|
75 |
|
---|
76 | template <class T>
|
---|
77 | TArray<T>::TArray(const TArray<T>& a, bool share)
|
---|
78 | : BaseArray() , mNDBlock(a.mNDBlock, share)
|
---|
79 | {
|
---|
80 | string exmsg = "TArray<T>::TArray(const TArray<T>&, bool)";
|
---|
81 | if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
|
---|
82 | if (a.mInfo) mInfo = new DVList(*(a.mInfo));
|
---|
83 | }
|
---|
84 |
|
---|
85 | // Destructeur
|
---|
86 | template <class T>
|
---|
87 | TArray<T>::~TArray()
|
---|
88 | {
|
---|
89 | }
|
---|
90 |
|
---|
91 | template <class T>
|
---|
92 | TArray<T>& TArray<T>::Set(const TArray<T>& a)
|
---|
93 | {
|
---|
94 | if (this != &a) {
|
---|
95 | CloneOrShare(a);
|
---|
96 | if (mInfo) delete mInfo;
|
---|
97 | if (a.mInfo) mInfo = new DVList(*(a.mInfo));
|
---|
98 | }
|
---|
99 | return(*this);
|
---|
100 | }
|
---|
101 |
|
---|
102 | template <class T>
|
---|
103 | void TArray<T>::Clone(const TArray<T>& a)
|
---|
104 | {
|
---|
105 | string exmsg = "TArray<T>::Clone()";
|
---|
106 | if (!UpdateSizes(a, exmsg)) throw( ParmError(exmsg) );
|
---|
107 | mNDBlock.Clone(a.mNDBlock);
|
---|
108 | if (mInfo) delete mInfo;
|
---|
109 | if (a.mInfo) mInfo = new DVList(*(a.mInfo));
|
---|
110 | }
|
---|
111 |
|
---|
112 | template <class T>
|
---|
113 | void TArray<T>::ReSize(uint_4 ndim, uint_4 * siz, uint_4 step)
|
---|
114 | {
|
---|
115 | string exmsg = "TArray<T>::ReSize()";
|
---|
116 | if (!UpdateSizes(ndim, siz, step, 0, exmsg)) throw( ParmError(exmsg) );
|
---|
117 | mNDBlock.ReSize(totsize_);
|
---|
118 | }
|
---|
119 |
|
---|
120 | template <class T>
|
---|
121 | void TArray<T>::Realloc(uint_4 ndim, uint_4 * siz, uint_4 step, bool force)
|
---|
122 | {
|
---|
123 | string exmsg = "TArray<T>::Realloc()";
|
---|
124 | if (!UpdateSizes(ndim, siz, step, 0, exmsg)) throw( ParmError(exmsg) );
|
---|
125 | mNDBlock.ReSize(totsize_);
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | template <class T>
|
---|
130 | TArray<T>& TArray<T>::CompactAllDimensions()
|
---|
131 | {
|
---|
132 | CompactAllDim();
|
---|
133 | return(*this);
|
---|
134 | }
|
---|
135 |
|
---|
136 | template <class T>
|
---|
137 | TArray<T>& TArray<T>::CompactTrailingDimensions()
|
---|
138 | {
|
---|
139 | CompactTrailingDim();
|
---|
140 | return(*this);
|
---|
141 | }
|
---|
142 |
|
---|
143 | template <class T>
|
---|
144 | double TArray<T>::ValueAtPosition(uint_8 ip) const
|
---|
145 | {
|
---|
146 | #ifdef SO_BOUNDCHECKING
|
---|
147 | if (ip >= totsize_) throw( ParmError("TArray<T>::ValueAtPosition(uint_8 ip) Out-of-bound Error") );
|
---|
148 | #endif
|
---|
149 | return( (double)(*(mNDBlock.Begin()+Offset(ip))) );
|
---|
150 | }
|
---|
151 |
|
---|
152 | // For complex values, we return the module of the complex number
|
---|
153 | double TArray< complex<r_4> >::ValueAtPosition(uint_8 ip) const
|
---|
154 | {
|
---|
155 | #ifdef SO_BOUNDCHECKING
|
---|
156 | if (ip >= totsize_) throw( ParmError("TArray<T>::ValueAtPosition(uint_8 ip) Out-of-bound Error") );
|
---|
157 | #endif
|
---|
158 | complex<r_4> c = *(mNDBlock.Begin()+Offset(ip));
|
---|
159 | double cr = (double)(c.real());
|
---|
160 | double ci = (double)(c.imag());
|
---|
161 | return( sqrt(cr*cr+ci*ci) );
|
---|
162 | }
|
---|
163 |
|
---|
164 | double TArray< complex<r_8> >::ValueAtPosition(uint_8 ip) const
|
---|
165 | {
|
---|
166 | #ifdef SO_BOUNDCHECKING
|
---|
167 | if (ip >= totsize_) throw( ParmError("TArray<T>::ValueAtPosition(uint_8 ip) Out-of-bound Error") );
|
---|
168 | #endif
|
---|
169 | complex<r_8> c = *(mNDBlock.Begin()+Offset(ip));
|
---|
170 | double cr = (double)(c.real());
|
---|
171 | double ci = (double)(c.imag());
|
---|
172 | return( sqrt(cr*cr+ci*ci) );
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | template <class T>
|
---|
177 | TArray<T> TArray<T>::PackElements(bool force) const
|
---|
178 | {
|
---|
179 | if (NbDimensions() < 1)
|
---|
180 | throw RangeCheckError("TArray<T>::PackElements() - Not Allocated Array ! ");
|
---|
181 | if ( !force && (AvgStep() == 1) ) {
|
---|
182 | TArray<T> ra(*this, true);
|
---|
183 | ra.SetTemp(true);
|
---|
184 | return(ra);
|
---|
185 | }
|
---|
186 | else {
|
---|
187 | TArray<T> ra(ndim_, size_, 1);
|
---|
188 | ra.CopyElt(*this);
|
---|
189 | ra.SetTemp(true);
|
---|
190 | return(ra);
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | // SubArrays
|
---|
195 | // $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
|
---|
196 | template <class T>
|
---|
197 | TArray<T> TArray<T>::SubArray(Range rx, Range ry, Range rz, Range rt, Range ru) const
|
---|
198 | {
|
---|
199 | if (NbDimensions() < 1)
|
---|
200 | throw RangeCheckError("TArray<T>::operator () (Range, ...) - Not Allocated Array ! ");
|
---|
201 | uint_4 ndim = 0;
|
---|
202 | uint_4 size[BASEARRAY_MAXNDIMS];
|
---|
203 | uint_4 step[BASEARRAY_MAXNDIMS];
|
---|
204 | uint_4 pos[BASEARRAY_MAXNDIMS];
|
---|
205 | size[0] = rx.Size();
|
---|
206 | size[1] = ry.Size();
|
---|
207 | size[2] = rz.Size();
|
---|
208 | size[3] = rt.Size();
|
---|
209 | size[4] = ru.Size();
|
---|
210 |
|
---|
211 | step[0] = rx.Step();
|
---|
212 | step[1] = ry.Step();
|
---|
213 | step[2] = rz.Step();
|
---|
214 | step[3] = rt.Step();
|
---|
215 | step[4] = ru.Step();
|
---|
216 |
|
---|
217 | pos[0] = rx.Start();
|
---|
218 | pos[1] = ry.Start();
|
---|
219 | pos[2] = rz.Start();
|
---|
220 | pos[3] = rt.Start();
|
---|
221 | pos[4] = ru.Start();
|
---|
222 |
|
---|
223 | ndim = ndim_;
|
---|
224 | TArray<T> ra;
|
---|
225 | UpdateSubArraySizes(ra, ndim, size, pos, step);
|
---|
226 | ra.DataBlock().Share(this->DataBlock());
|
---|
227 | ra.SetTemp(true);
|
---|
228 | return(ra);
|
---|
229 | }
|
---|
230 |
|
---|
231 | // ...... Operation de calcul sur les tableaux ......
|
---|
232 | // ------- Attention --------
|
---|
233 | // Boucles normales prenant en compte les steps ....
|
---|
234 | // Possibilite de // , vectorisation
|
---|
235 | template <class T>
|
---|
236 | TArray<T>& TArray<T>::Set(Sequence seq)
|
---|
237 | {
|
---|
238 | if (NbDimensions() < 1)
|
---|
239 | throw RangeCheckError("TArray<T>::Set(Sequence ) - Not Allocated Array ! ");
|
---|
240 | T * pe;
|
---|
241 | uint_8 j,k;
|
---|
242 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
243 | uint_8 step = AvgStep();
|
---|
244 | pe = Data();
|
---|
245 | for(k=0; k<totsize_; k++ ) pe[k*step] = seq(k);
|
---|
246 | }
|
---|
247 | else { // Non regular data spacing ...
|
---|
248 | uint_4 ka = MaxSizeKA();
|
---|
249 | uint_8 step = Step(ka);
|
---|
250 | uint_8 gpas = Size(ka);
|
---|
251 | for(j=0; j<totsize_; j += Size(ka)) {
|
---|
252 | pe = mNDBlock.Begin()+Offset(j);
|
---|
253 | for(k=0; k<gpas; k++) pe[k*step] = seq(j+k);
|
---|
254 | }
|
---|
255 | }
|
---|
256 | return(*this);
|
---|
257 | }
|
---|
258 |
|
---|
259 | // >>>> Operations avec 2nd membre de type scalaire
|
---|
260 |
|
---|
261 | template <class T>
|
---|
262 | TArray<T>& TArray<T>::Set(T x)
|
---|
263 | {
|
---|
264 | if (NbDimensions() < 1)
|
---|
265 | throw RangeCheckError("TArray<T>::Set(T ) - Not Allocated Array ! ");
|
---|
266 | T * pe;
|
---|
267 | uint_8 j,k;
|
---|
268 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
269 | uint_8 step = AvgStep();
|
---|
270 | uint_8 maxx = totsize_*step;
|
---|
271 | pe = Data();
|
---|
272 | for(k=0; k<maxx; k+=step ) pe[k] = x;
|
---|
273 | }
|
---|
274 | else { // Non regular data spacing ...
|
---|
275 | uint_4 ka = MaxSizeKA();
|
---|
276 | uint_8 step = Step(ka);
|
---|
277 | uint_8 gpas = Size(ka)*step;
|
---|
278 | for(j=0; j<totsize_; j += Size(ka)) {
|
---|
279 | pe = mNDBlock.Begin()+Offset(j);
|
---|
280 | for(k=0; k<gpas; k+=step) pe[k] = x;
|
---|
281 | }
|
---|
282 | }
|
---|
283 | return(*this);
|
---|
284 | }
|
---|
285 |
|
---|
286 | template <class T>
|
---|
287 | TArray<T>& TArray<T>::Add(T x)
|
---|
288 | {
|
---|
289 | if (NbDimensions() < 1)
|
---|
290 | throw RangeCheckError("TArray<T>::Add(T ) - Not Allocated Array ! ");
|
---|
291 | T * pe;
|
---|
292 | uint_8 j,k;
|
---|
293 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
294 | uint_8 step = AvgStep();
|
---|
295 | uint_8 maxx = totsize_*step;
|
---|
296 | pe = Data();
|
---|
297 | for(k=0; k<maxx; k+=step ) pe[k] += x;
|
---|
298 | }
|
---|
299 | else { // Non regular data spacing ...
|
---|
300 | uint_4 ka = MaxSizeKA();
|
---|
301 | uint_8 step = Step(ka);
|
---|
302 | uint_8 gpas = Size(ka)*step;
|
---|
303 | for(j=0; j<totsize_; j += Size(ka)) {
|
---|
304 | pe = mNDBlock.Begin()+Offset(j);
|
---|
305 | for(k=0; k<gpas; k+=step) pe[k] += x;
|
---|
306 | }
|
---|
307 | }
|
---|
308 | return(*this);
|
---|
309 | }
|
---|
310 |
|
---|
311 | template <class T>
|
---|
312 | TArray<T>& TArray<T>::Sub(T x)
|
---|
313 | {
|
---|
314 | if (NbDimensions() < 1)
|
---|
315 | throw RangeCheckError("TArray<T>::Sub(T ) - Not Allocated Array ! ");
|
---|
316 | T * pe;
|
---|
317 | uint_8 j,k;
|
---|
318 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
319 | uint_8 step = AvgStep();
|
---|
320 | uint_8 maxx = totsize_*step;
|
---|
321 | pe = Data();
|
---|
322 | for(k=0; k<maxx; k+=step ) pe[k] -= x;
|
---|
323 | }
|
---|
324 | else { // Non regular data spacing ...
|
---|
325 | uint_4 ka = MaxSizeKA();
|
---|
326 | uint_8 step = Step(ka);
|
---|
327 | uint_8 gpas = Size(ka)*step;
|
---|
328 | for(j=0; j<totsize_; j += Size(ka)) {
|
---|
329 | pe = mNDBlock.Begin()+Offset(j);
|
---|
330 | for(k=0; k<gpas; k+=step) pe[k] -= x;
|
---|
331 | }
|
---|
332 | }
|
---|
333 | return(*this);
|
---|
334 | }
|
---|
335 |
|
---|
336 | template <class T>
|
---|
337 | TArray<T>& TArray<T>::Mul(T x)
|
---|
338 | {
|
---|
339 | if (NbDimensions() < 1)
|
---|
340 | throw RangeCheckError("TArray<T>::Mul(T ) - Not Allocated Array ! ");
|
---|
341 | T * pe;
|
---|
342 | uint_8 j,k;
|
---|
343 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
344 | uint_8 step = AvgStep();
|
---|
345 | uint_8 maxx = totsize_*step;
|
---|
346 | pe = Data();
|
---|
347 | for(k=0; k<maxx; k+=step ) pe[k] *= x;
|
---|
348 | }
|
---|
349 | else { // Non regular data spacing ...
|
---|
350 | uint_4 ka = MaxSizeKA();
|
---|
351 | uint_8 step = Step(ka);
|
---|
352 | uint_8 gpas = Size(ka)*step;
|
---|
353 | for(j=0; j<totsize_; j += Size(ka)) {
|
---|
354 | pe = mNDBlock.Begin()+Offset(j);
|
---|
355 | for(k=0; k<gpas; k+=step) pe[k] *= x;
|
---|
356 | }
|
---|
357 | }
|
---|
358 | return(*this);
|
---|
359 | }
|
---|
360 |
|
---|
361 | template <class T>
|
---|
362 | TArray<T>& TArray<T>::Div(T x)
|
---|
363 | {
|
---|
364 | if (NbDimensions() < 1)
|
---|
365 | throw RangeCheckError("TArray<T>::Div(T ) - Not Allocated Array ! ");
|
---|
366 | T * pe;
|
---|
367 | uint_8 j,k;
|
---|
368 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
369 | uint_8 step = AvgStep();
|
---|
370 | uint_8 maxx = totsize_*step;
|
---|
371 | pe = Data();
|
---|
372 | for(k=0; k<maxx; k+=step ) pe[k] /= x;
|
---|
373 | }
|
---|
374 | else { // Non regular data spacing ...
|
---|
375 | uint_4 ka = MaxSizeKA();
|
---|
376 | uint_8 step = Step(ka);
|
---|
377 | uint_8 gpas = Size(ka)*step;
|
---|
378 | for(j=0; j<totsize_; j += Size(ka)) {
|
---|
379 | pe = mNDBlock.Begin()+Offset(j);
|
---|
380 | for(k=0; k<gpas; k+=step) pe[k] /= x;
|
---|
381 | }
|
---|
382 | }
|
---|
383 | return(*this);
|
---|
384 | }
|
---|
385 |
|
---|
386 |
|
---|
387 | template <class T>
|
---|
388 | TArray<T>& TArray<T>::SubInv(T x)
|
---|
389 | {
|
---|
390 | if (NbDimensions() < 1)
|
---|
391 | throw RangeCheckError("TArray<T>::SubInv(T ) - Not Allocated Array ! ");
|
---|
392 | T * pe;
|
---|
393 | uint_8 j,k;
|
---|
394 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
395 | uint_8 step = AvgStep();
|
---|
396 | uint_8 maxx = totsize_*step;
|
---|
397 | pe = Data();
|
---|
398 | for(k=0; k<maxx; k+=step ) pe[k] = x-pe[k];
|
---|
399 | }
|
---|
400 | else { // Non regular data spacing ...
|
---|
401 | uint_4 ka = MaxSizeKA();
|
---|
402 | uint_8 step = Step(ka);
|
---|
403 | uint_8 gpas = Size(ka)*step;
|
---|
404 | for(j=0; j<totsize_; j += Size(ka)) {
|
---|
405 | pe = mNDBlock.Begin()+Offset(j);
|
---|
406 | for(k=0; k<gpas; k+=step) pe[k] = x-pe[k];
|
---|
407 | }
|
---|
408 | }
|
---|
409 | return(*this);
|
---|
410 | }
|
---|
411 |
|
---|
412 | template <class T>
|
---|
413 | TArray<T>& TArray<T>::DivInv(T x)
|
---|
414 | {
|
---|
415 | if (NbDimensions() < 1)
|
---|
416 | throw RangeCheckError("TArray<T>::DivInv(T ) - Not Allocated Array ! ");
|
---|
417 | T * pe;
|
---|
418 | uint_8 j,k;
|
---|
419 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
420 | uint_8 step = AvgStep();
|
---|
421 | uint_8 maxx = totsize_*step;
|
---|
422 | pe = Data();
|
---|
423 | for(k=0; k<maxx; k+=step ) pe[k] = x/pe[k];
|
---|
424 | }
|
---|
425 | else { // Non regular data spacing ...
|
---|
426 | uint_4 ka = MaxSizeKA();
|
---|
427 | uint_8 step = Step(ka);
|
---|
428 | uint_8 gpas = Size(ka)*step;
|
---|
429 | for(j=0; j<totsize_; j += Size(ka)) {
|
---|
430 | pe = mNDBlock.Begin()+Offset(j);
|
---|
431 | for(k=0; k<gpas; k+=step) pe[k] = x/pe[k];
|
---|
432 | }
|
---|
433 | }
|
---|
434 | return(*this);
|
---|
435 | }
|
---|
436 |
|
---|
437 |
|
---|
438 | // >>>> Operations avec 2nd membre de type tableau
|
---|
439 | template <class T>
|
---|
440 | TArray<T>& TArray<T>::AddElt(const TArray<T>& a)
|
---|
441 | {
|
---|
442 | if (NbDimensions() < 1)
|
---|
443 | throw RangeCheckError("TArray<T>::AddElt(const TArray<T>& ) - Not Allocated Array ! ");
|
---|
444 | if (!CompareSizes(a)) throw(SzMismatchError("TArray<T>::AddElt(const TArray<T>&)")) ;
|
---|
445 |
|
---|
446 | T * pe;
|
---|
447 | const T * pea;
|
---|
448 | uint_8 j,k,ka;
|
---|
449 | if ((AvgStep() > 0) && (a.AvgStep() > 0) ) { // regularly spaced elements
|
---|
450 | uint_8 step = AvgStep();
|
---|
451 | uint_8 stepa = a.AvgStep();
|
---|
452 | uint_8 maxx = totsize_*step;
|
---|
453 | pe = Data();
|
---|
454 | pea = a.Data();
|
---|
455 | for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] += pea[ka] ;
|
---|
456 | }
|
---|
457 | else { // Non regular data spacing ...
|
---|
458 | uint_4 ax = MaxSizeKA();
|
---|
459 | uint_8 step = Step(ax);
|
---|
460 | uint_8 stepa = a.Step(ax);
|
---|
461 | uint_8 gpas = Size(ax)*step;
|
---|
462 | for(j=0; j<totsize_; j += Size(ax)) {
|
---|
463 | pe = mNDBlock.Begin()+Offset(j);
|
---|
464 | pea = a.DataBlock().Begin()+a.Offset(j);
|
---|
465 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] += pea[ka];
|
---|
466 | }
|
---|
467 | }
|
---|
468 | return(*this);
|
---|
469 | }
|
---|
470 |
|
---|
471 | template <class T>
|
---|
472 | TArray<T>& TArray<T>::SubElt(const TArray<T>& a)
|
---|
473 | {
|
---|
474 | if (NbDimensions() < 1)
|
---|
475 | throw RangeCheckError("TArray<T>::SubElt(const TArray<T>& ) - Not Allocated Array ! ");
|
---|
476 | if (!CompareSizes(a)) throw(SzMismatchError("TArray<T>::SubElt(const TArray<T>&)")) ;
|
---|
477 |
|
---|
478 | T * pe;
|
---|
479 | const T * pea;
|
---|
480 | uint_8 j,k,ka;
|
---|
481 | if ((AvgStep() > 0) && (a.AvgStep() > 0) ) { // regularly spaced elements
|
---|
482 | uint_8 step = AvgStep();
|
---|
483 | uint_8 stepa = a.AvgStep();
|
---|
484 | uint_8 maxx = totsize_*step;
|
---|
485 | pe = Data();
|
---|
486 | pea = a.Data();
|
---|
487 | for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] -= pea[ka] ;
|
---|
488 | }
|
---|
489 | else { // Non regular data spacing ...
|
---|
490 | uint_4 ax = MaxSizeKA();
|
---|
491 | uint_8 step = Step(ax);
|
---|
492 | uint_8 stepa = a.Step(ax);
|
---|
493 | uint_8 gpas = Size(ax)*step;
|
---|
494 | for(j=0; j<totsize_; j += Size(ax)) {
|
---|
495 | pe = mNDBlock.Begin()+Offset(j);
|
---|
496 | pea = a.DataBlock().Begin()+a.Offset(j);
|
---|
497 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] -= pea[ka];
|
---|
498 | }
|
---|
499 | }
|
---|
500 | return(*this);
|
---|
501 | }
|
---|
502 |
|
---|
503 |
|
---|
504 | template <class T>
|
---|
505 | TArray<T>& TArray<T>::MulElt(const TArray<T>& a)
|
---|
506 | {
|
---|
507 | if (NbDimensions() < 1)
|
---|
508 | throw RangeCheckError("TArray<T>::MulElt(const TArray<T>& ) - Not Allocated Array ! ");
|
---|
509 | if (!CompareSizes(a)) throw(SzMismatchError("TArray<T>::MulElt(const TArray<T>&)")) ;
|
---|
510 |
|
---|
511 | T * pe;
|
---|
512 | const T * pea;
|
---|
513 | uint_8 j,k,ka;
|
---|
514 | if ((AvgStep() > 0) && (a.AvgStep() > 0) ) { // regularly spaced elements
|
---|
515 | uint_8 step = AvgStep();
|
---|
516 | uint_8 stepa = a.AvgStep();
|
---|
517 | uint_8 maxx = totsize_*step;
|
---|
518 | pe = Data();
|
---|
519 | pea = a.Data();
|
---|
520 | for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] *= pea[ka] ;
|
---|
521 | }
|
---|
522 | else { // Non regular data spacing ...
|
---|
523 | uint_4 ax = MaxSizeKA();
|
---|
524 | uint_8 step = Step(ax);
|
---|
525 | uint_8 stepa = a.Step(ax);
|
---|
526 | uint_8 gpas = Size(ax)*step;
|
---|
527 | for(j=0; j<totsize_; j += Size(ax)) {
|
---|
528 | pe = mNDBlock.Begin()+Offset(j);
|
---|
529 | pea = a.DataBlock().Begin()+a.Offset(j);
|
---|
530 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] *= pea[ka];
|
---|
531 | }
|
---|
532 | }
|
---|
533 | return(*this);
|
---|
534 | }
|
---|
535 |
|
---|
536 |
|
---|
537 | template <class T>
|
---|
538 | TArray<T>& TArray<T>::DivElt(const TArray<T>& a)
|
---|
539 | {
|
---|
540 | if (NbDimensions() < 1)
|
---|
541 | throw RangeCheckError("TArray<T>::DivElt(const TArray<T>& ) - Not Allocated Array ! ");
|
---|
542 | if (!CompareSizes(a)) throw(SzMismatchError("TArray<T>::DivElt(const TArray<T>&)")) ;
|
---|
543 |
|
---|
544 | T * pe;
|
---|
545 | const T * pea;
|
---|
546 | uint_8 j,k,ka;
|
---|
547 | if ((AvgStep() > 0) && (a.AvgStep() > 0) ) { // regularly spaced elements
|
---|
548 | uint_8 step = AvgStep();
|
---|
549 | uint_8 stepa = a.AvgStep();
|
---|
550 | uint_8 maxx = totsize_*step;
|
---|
551 | pe = Data();
|
---|
552 | pea = a.Data();
|
---|
553 | for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] /= pea[ka] ;
|
---|
554 | }
|
---|
555 | else { // Non regular data spacing ...
|
---|
556 | uint_4 ax = MaxSizeKA();
|
---|
557 | uint_8 step = Step(ax);
|
---|
558 | uint_8 stepa = a.Step(ax);
|
---|
559 | uint_8 gpas = Size(ax)*step;
|
---|
560 | for(j=0; j<totsize_; j += Size(ax)) {
|
---|
561 | pe = mNDBlock.Begin()+Offset(j);
|
---|
562 | pea = a.DataBlock().Begin()+a.Offset(j);
|
---|
563 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] /= pea[ka];
|
---|
564 | }
|
---|
565 | }
|
---|
566 | return(*this);
|
---|
567 | }
|
---|
568 |
|
---|
569 | template <class T>
|
---|
570 | TArray<T>& TArray<T>::CopyElt(const TArray<T>& a)
|
---|
571 | {
|
---|
572 | if (NbDimensions() < 1)
|
---|
573 | throw RangeCheckError("TArray<T>::CopyElt(const TArray<T>& ) - Not Allocated Array ! ");
|
---|
574 | if (!CompareSizes(a)) throw(SzMismatchError("TArray<T>::MultElt(const TArray<T>&)")) ;
|
---|
575 |
|
---|
576 | T * pe;
|
---|
577 | const T * pea;
|
---|
578 | uint_8 j,k,ka;
|
---|
579 | if ((AvgStep() > 0) && (a.AvgStep() > 0) ) { // regularly spaced elements
|
---|
580 | uint_8 step = AvgStep();
|
---|
581 | uint_8 stepa = a.AvgStep();
|
---|
582 | uint_8 maxx = totsize_*step;
|
---|
583 | pe = Data();
|
---|
584 | pea = a.Data();
|
---|
585 | for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] = pea[ka] ;
|
---|
586 | }
|
---|
587 | else { // Non regular data spacing ...
|
---|
588 | uint_4 ax = MaxSizeKA();
|
---|
589 | uint_8 step = Step(ax);
|
---|
590 | uint_8 stepa = a.Step(ax);
|
---|
591 | uint_8 gpas = Size(ax)*step;
|
---|
592 | for(j=0; j<totsize_; j += Size(ax)) {
|
---|
593 | pe = mNDBlock.Begin()+Offset(j);
|
---|
594 | pea = a.DataBlock().Begin()+a.Offset(j);
|
---|
595 | for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] = pea[ka];
|
---|
596 | }
|
---|
597 | }
|
---|
598 | return(*this);
|
---|
599 | }
|
---|
600 |
|
---|
601 |
|
---|
602 | // Somme et produit des elements
|
---|
603 | template <class T>
|
---|
604 | T TArray<T>::Sum() const
|
---|
605 | {
|
---|
606 | if (NbDimensions() < 1)
|
---|
607 | throw RangeCheckError("TArray<T>::Sum() - Not Allocated Array ! ");
|
---|
608 | T ret=0;
|
---|
609 | const T * pe;
|
---|
610 | uint_8 j,k;
|
---|
611 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
612 | uint_8 step = AvgStep();
|
---|
613 | uint_8 maxx = totsize_*step;
|
---|
614 | pe = Data();
|
---|
615 | for(k=0; k<maxx; k+=step ) ret += pe[k];
|
---|
616 | }
|
---|
617 | else { // Non regular data spacing ...
|
---|
618 | uint_4 ka = MaxSizeKA();
|
---|
619 | uint_8 step = Step(ka);
|
---|
620 | uint_8 gpas = Size(ka)*step;
|
---|
621 | for(j=0; j<totsize_; j += Size(ka)) {
|
---|
622 | pe = mNDBlock.Begin()+Offset(j);
|
---|
623 | for(k=0; k<gpas; k+=step) ret += pe[k] ;
|
---|
624 | }
|
---|
625 | }
|
---|
626 | return ret;
|
---|
627 | }
|
---|
628 |
|
---|
629 | template <class T>
|
---|
630 | T TArray<T>::Product() const
|
---|
631 | {
|
---|
632 | if (NbDimensions() < 1)
|
---|
633 | throw RangeCheckError("TArray<T>::Product() - Not Allocated Array ! ");
|
---|
634 | T ret=0;
|
---|
635 | const T * pe;
|
---|
636 | uint_8 j,k;
|
---|
637 | if (AvgStep() > 0) { // regularly spaced elements
|
---|
638 | uint_8 step = AvgStep();
|
---|
639 | uint_8 maxx = totsize_*step;
|
---|
640 | pe = Data();
|
---|
641 | for(k=0; k<maxx; k+=step ) ret *= pe[k];
|
---|
642 | }
|
---|
643 | else { // Non regular data spacing ...
|
---|
644 | uint_4 ka = MaxSizeKA();
|
---|
645 | uint_8 step = Step(ka);
|
---|
646 | uint_8 gpas = Size(ka)*step;
|
---|
647 | for(j=0; j<totsize_; j += Size(ka)) {
|
---|
648 | pe = mNDBlock.Begin()+Offset(j);
|
---|
649 | for(k=0; k<gpas; k+=step) ret *= pe[k] ;
|
---|
650 | }
|
---|
651 | }
|
---|
652 | return ret;
|
---|
653 | }
|
---|
654 |
|
---|
655 |
|
---|
656 |
|
---|
657 | // ----------------------------------------------------
|
---|
658 | // Impression, etc ...
|
---|
659 | // ----------------------------------------------------
|
---|
660 |
|
---|
661 | template <class T>
|
---|
662 | string TArray<T>::DataType() const
|
---|
663 | {
|
---|
664 | string rs = typeid(T).name();
|
---|
665 | return(rs);
|
---|
666 | }
|
---|
667 |
|
---|
668 | template <class T>
|
---|
669 | void TArray<T>::Print(ostream& os, int_4 maxprt, bool si) const
|
---|
670 | {
|
---|
671 | if (maxprt < 0) maxprt = max_nprt_;
|
---|
672 | int_4 npr = 0;
|
---|
673 | Show(os, si);
|
---|
674 | uint_4 k0,k1,k2,k3,k4;
|
---|
675 | for(k4=0; k4<size_[4]; k4++) {
|
---|
676 | if (size_[4] > 1) cout << "\n ----- Dimension 5 (U) K4= " << k4 << endl;
|
---|
677 | for(k3=0; k3<size_[3]; k3++) {
|
---|
678 | if (size_[3] > 1) cout << "\n ----- Dimension 4 (T) K3= " << k3 << endl;
|
---|
679 | for(k2=0; k2<size_[2]; k2++) {
|
---|
680 | if (size_[2] > 1) cout << "\n ----- Dimension 3 (Z) K2= " << k2 << endl;
|
---|
681 | for(k1=0; k1<size_[1]; k1++) {
|
---|
682 | if ( (size_[1] > 1) && (size_[0] > 10) ) cout << "----- Dimension 2 (Y) K1= " << k1 << endl;
|
---|
683 | for(k0=0; k0<size_[0]; k0++) {
|
---|
684 | if(k0 > 0) os << ", ";
|
---|
685 | os << Elem(k0, k1, k2, k3, k4); npr++;
|
---|
686 | if (npr >= maxprt) {
|
---|
687 | if (npr < totsize_) os << "\n .... " << endl; return;
|
---|
688 | }
|
---|
689 | }
|
---|
690 | os << endl;
|
---|
691 | }
|
---|
692 | }
|
---|
693 | }
|
---|
694 | }
|
---|
695 | os << "\n" << endl;
|
---|
696 | }
|
---|
697 |
|
---|
698 |
|
---|
699 | template <class T>
|
---|
700 | void TArray<T>::CloneOrShare(const TArray<T>& a)
|
---|
701 | {
|
---|
702 | string exmsg = "TArray<T>::CloneOrShare()";
|
---|
703 | if (!UpdateSizes(a.ndim_, a.size_, a.step_, a.offset_, exmsg)) throw( ParmError(exmsg) );
|
---|
704 | mNDBlock.CloneOrShare(a.mNDBlock);
|
---|
705 | }
|
---|
706 |
|
---|
707 | template <class T>
|
---|
708 | void TArray<T>::Share(const TArray<T>& a)
|
---|
709 | {
|
---|
710 | string exmsg = "TArray<T>::Share()";
|
---|
711 | if (!UpdateSizes(a.ndim_, a.size_, a.step_, a.offset_, exmsg)) throw( ParmError(exmsg) );
|
---|
712 | mNDBlock.Share(a.mNDBlock);
|
---|
713 | }
|
---|
714 |
|
---|
715 |
|
---|
716 |
|
---|
717 | ///////////////////////////////////////////////////////////////
|
---|
718 | ///////////////////////////////////////////////////////////////
|
---|
719 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
720 | /*
|
---|
721 | #pragma define_template TArray<uint_1>
|
---|
722 | #pragma define_template TArray<int_2>
|
---|
723 | #pragma define_template TArray<uint_4>
|
---|
724 | #pragma define_template TArray<uint_8>
|
---|
725 | */
|
---|
726 | #pragma define_template TArray<uint_2>
|
---|
727 | #pragma define_template TArray<int_4>
|
---|
728 | #pragma define_template TArray<int_8>
|
---|
729 | #pragma define_template TArray<r_4>
|
---|
730 | #pragma define_template TArray<r_8>
|
---|
731 | #pragma define_template TArray< complex<r_4> >
|
---|
732 | #pragma define_template TArray< complex<r_8> >
|
---|
733 | #endif
|
---|
734 |
|
---|
735 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
736 | /*
|
---|
737 | template class TArray<uint_1>;
|
---|
738 | template class TArray<int_2>;
|
---|
739 | template class TArray<uint_4>;
|
---|
740 | template class TArray<uint_8>;
|
---|
741 | */
|
---|
742 | template class TArray<uint_2>;
|
---|
743 | template class TArray<int_4>;
|
---|
744 | template class TArray<int_8>;
|
---|
745 | template class TArray<r_4>;
|
---|
746 | template class TArray<r_8>;
|
---|
747 | template class TArray< complex<r_4> >;
|
---|
748 | template class TArray< complex<r_8> >;
|
---|
749 | #endif
|
---|
750 |
|
---|
751 |
|
---|