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