source: Sophya/trunk/SophyaLib/TArray/tarray.h@ 2909

Last change on this file since 2909 was 2888, checked in by ansari, 20 years ago

Ajout de la methode BaseArray::ValueAtPositionDB() pour corriger un gros bug au niveau de la conversion de type (r_4 r_8 ...) des tableaux - Reza 4 Jan 2006

File size: 18.0 KB
Line 
1// This may look like C code, but it is really -*- C++ -*-
2// template array class for numerical types
3// R. Ansari, C.Magneville 03/2000
4
5#ifndef TArray_SEEN
6#define TArray_SEEN
7
8#include "machdefs.h"
9#include <math.h>
10#include <iostream>
11#include "basarr.h"
12#include "ndatablock.h"
13#include <complex>
14#include "utilarr.h"
15
16
17namespace SOPHYA {
18
19// Forward declaration
20template <class T> class FIO_TArray;
21
22// --------------------------- classe template Array -----------------------
23// ( See BaseArray class for data organisation in memory and related methods )
24
25//! Class for template arrays
26template <class T>
27class TArray : public BaseArray {
28public:
29 // Creation / destruction
30 TArray();
31 TArray(int_4 ndim, const sa_size_t * siz, sa_size_t step =1, bool fzero=true);
32 TArray(sa_size_t nx, sa_size_t ny=0, sa_size_t nz=0, sa_size_t nt=0, sa_size_t nu=0, bool fzero=true);
33 TArray(int_4 ndim, const sa_size_t * siz, NDataBlock<T> & db, bool share=false, sa_size_t step=1, sa_size_t offset=0);
34 TArray(int_4 ndim, const sa_size_t * siz, T* values, sa_size_t step=1, sa_size_t offset=0, Bridge* br=NULL);
35 TArray(const TArray<T>& a);
36 TArray(const TArray<T>& a, bool share);
37 TArray(const BaseArray& a);
38
39 virtual ~TArray();
40
41 // A = B
42 //! = operator between TArray
43 /*! \warning Datas are copied (cloned) from \b a.
44 \sa Set \sa NDataBlock::operator=(const NDataBlock<T>&) */
45 inline TArray<T>& operator = (const TArray<T>& a) { return Set(a); }
46 virtual TArray<T>& Set(const TArray<T>& a);
47
48 //! = operator between TArray 's with different types - Elements are converted.
49 inline TArray<T>& operator = (const BaseArray& a) { return SetBA(a); }
50 virtual TArray<T>& SetBA(const BaseArray& a);
51
52 // Gestion taille/Remplissage
53 virtual void Clone(const TArray<T>& a);
54 // partage les donnees si "a" temporaire, clone sinon.
55 void CloneOrShare(const TArray<T>& a);
56 // Share: partage les donnees de "a"
57 void Share(const TArray<T>& a);
58
59 void ReSize(int_4 ndim, sa_size_t * siz, sa_size_t step=1, bool fzero=true);
60 void ReSize(const BaseArray& a, bool pack=false, bool fzero=true);
61 //! a synonym (alias) for method ReSize(int_4, ...)
62 inline void SetSize(int_4 ndim, sa_size_t * siz, sa_size_t step=1, bool fzero=true)
63 { ReSize(ndim, siz, step, fzero); }
64 //! a synonym (alias) for method ReSize(const BaseArray&)
65 inline void SetSize(const BaseArray& a, bool pack=false, bool fzero=true)
66 { ReSize(a, pack, fzero); }
67 void Realloc(int_4 ndim, sa_size_t * siz, sa_size_t step=1, bool force=false);
68
69 // Compacts size=1 array dimensions
70 virtual TArray<T>& CompactAllDimensions();
71 virtual TArray<T>& CompactTrailingDimensions();
72
73 // Packing array elements in memory
74 virtual TArray<T> PackElements(bool force=false) const ;
75
76 // SubArrays - $CHECK$ Reza 03/2000 je ne sais pas s'il faut declarer ca const ??
77 TArray<T> SubArray(Range rx, Range ry, Range rz, Range rt, Range ru) const ;
78
79 //! () operator for Sub arrays extraction
80 /*! \sa SubArray */
81 inline TArray<T> operator () (Range rx, Range ry, Range rz) const
82 { return SubArray(rx, ry, rz, Range(0), Range(0)); }
83 inline TArray<T> operator () (Range rx, Range ry, Range rz, Range rt) const
84 { return SubArray(rx, ry, rz, rt, Range(0)); }
85 inline TArray<T> operator () (Range rx, Range ry, Range rz, Range rt, Range ru) const
86 { return SubArray(rx, ry, rz, rt, ru); }
87
88 // ---- Access to data
89 // Definition of virtual element acces method inherited from BaseArray class
90 virtual MuTyV & ValueAtPosition(sa_size_t ip) const;
91 virtual MuTyV & ValueAtPositionDB(sa_size_t ip) const;
92
93 // Data Access: operator overloaded inline acces methods
94 inline T const& operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz) const ;
95 inline T& operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz);
96 inline T const& operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu=0) const ;
97 inline T& operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu=0);
98 inline T const& operator[](sa_size_t ip) const ;
99 inline T& operator[](sa_size_t ip);
100
101 inline T const& Elem(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it=0, sa_size_t iu=0) const ;
102 inline T& Elem(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it=0, sa_size_t iu=0);
103 inline T const& ElemCheckBound(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it=0, sa_size_t iu=0) const ;
104 inline T& ElemCheckBound(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it=0, sa_size_t iu=0);
105
106 //! Return pointer to first element adress
107 inline T* Data() {return mNDBlock.Begin()+offset_;}
108 //! Return pointer to first element adress
109 inline const T* Data() const {return mNDBlock.Begin()+offset_;}
110 //! Return reference to datablock NDataBlock
111 inline NDataBlock<T>& DataBlock() {return mNDBlock;}
112 //! Return reference to datablock NDataBlock
113 inline const NDataBlock<T>& DataBlock() const {return mNDBlock;}
114
115 // Temporaire?
116 //! Are the array temporay ?
117 inline bool IsTemp(void) const {return mNDBlock.IsTemp();}
118 //! Set the array as temporay
119 inline void SetTemp(bool temp=false) const {mNDBlock.SetTemp(temp);}
120
121// Operations diverses = , +=, ...
122// Conversion en type T, if Size() == 1
123 inline T toScalar();
124// Met les elements a une suite de valeurs
125 virtual TArray<T>& SetSeq(Sequence const & seq);
126 //! Fill TArray with Sequence \b seq
127 inline TArray<T>& operator = (Sequence const & seq) { return SetSeq(seq); }
128
129// A = x (tous les elements a x)
130 virtual TArray<T>& SetCst(T x);
131 //! Fill an array with a constant value \b x ( alias for \b SetCst() method )
132 inline TArray<T>& SetT(T x) { return SetCst(x); }
133 //! Fill TArray with all elements equal to \b x
134 inline TArray<T>& operator = (T x) { return SetT(x); }
135
136// addition et soustraction de constante
137 virtual TArray<T>& AddCst(T x, TArray<T>& res) const ;
138 virtual TArray<T>& SubCst(T x, TArray<T>& res, bool fginv=false) const ;
139// Multiplication et division par une constante
140 virtual TArray<T>& MulCst(T x, TArray<T>& res) const ;
141 virtual TArray<T>& DivCst(T x, TArray<T>& res, bool fginv=false) const ;
142
143// A += -= *= /= x (ajoute, soustrait, ... x a tous les elements)
144 // Methodes Add/Sub/Mul/Div() sont la pour compatibilite avant V=2 (1.818)
145 // Faut-il les garder ? Reza, Juillet 2004
146 inline TArray<T>& Add(T x) { return AddCst(x, *this); }
147 inline TArray<T>& Sub(T x, bool fginv=false) { return SubCst(x, *this, fginv); }
148 inline TArray<T>& Mul(T x) { return MulCst(x, *this); }
149 inline TArray<T>& Div(T x, bool fginv=false) { return DivCst(x, *this, fginv); }
150
151 //! Add \b x to all elements
152 inline TArray<T>& operator += (T x) { return AddCst(x, *this); }
153 //! Substract \b x to all elements
154 inline TArray<T>& operator -= (T x) { return SubCst(x, *this); }
155 //! Multiply all elements by \b x
156 inline TArray<T>& operator *= (T x) { return MulCst(x, *this); }
157 //! Divide all elements by \b x
158 inline TArray<T>& operator /= (T x) { return DivCst(x, *this); }
159
160// applique le signe moins a tous les elements
161 virtual TArray<T>& NegateElt(TArray<T>& res) const ;
162//! Replace array elements values by their opposite ( (*this)(i) -> -(*this)(i) )
163 inline TArray<T>& NegateElt() { return NegateElt(*this); }
164
165// A += -= (ajoute, soustrait element par element les deux tableaux )
166 virtual TArray<T>& AddElt(const TArray<T>& a, TArray<T>& res) const ;
167 virtual TArray<T>& SubElt(const TArray<T>& a, TArray<T>& res, bool fginv=false) const ;
168// Multiplication, division element par element les deux tableaux
169 virtual TArray<T>& MulElt(const TArray<T>& a, TArray<T>& res) const ;
170 virtual TArray<T>& DivElt(const TArray<T>& a, TArray<T>& res, bool fginv=false, bool divzero=false) const ;
171
172 //! Operator TArray += TArray (element by element addition in place)
173 inline TArray<T>& operator += (const TArray<T>& a) { return AddElt(a, *this); }
174 //! Operator TArray -= TArray (element by element subtraction in place)
175 inline TArray<T>& operator -= (const TArray<T>& a) { return SubElt(a, *this); }
176
177 // Doit-on definir les operateur *= /= TArray ? Reza, Juillet 2004
178 //! Element by element multiplication in place TArray *= TArray (element by element)
179 inline TArray<T>& Mul(const TArray<T>& a) { return MulElt(a, *this); }
180 //! Element by element division in place TArray *= TArray (element by element)
181 inline TArray<T>& Div(const TArray<T>& a, bool divzero=false)
182 { return DivElt(a, *this, false, divzero); }
183
184// Recopie des valeurs, element par element
185 virtual TArray<T>& CopyElt(const TArray<T>& a);
186// Recopie des valeurs avec conversion prealable, element par element
187 virtual TArray<T>& ConvertAndCopyElt(const BaseArray& a);
188
189// Calcul du produit scalaire ( Somme_i (*this)(i)*a(i) )
190 virtual T ScalarProduct(const TArray<T>& a) const ;
191// Norme(^2)
192 //! Returns the squarred of the array norm, defined as Sum_k (*this)(k)*(*this)(k)
193 inline T Norm2() const { return ScalarProduct(*this); }
194
195// Somme et produit des elements
196 virtual T Sum() const ;
197 virtual T Product() const ;
198// Somme du carre des elements
199 virtual T SumX2() const;
200// Valeur min et max des elements
201 virtual void MinMax(T& min, T& max) const ;
202
203// Impression, I/O, ...
204 virtual string InfoString() const;
205 virtual void Print(ostream& os, sa_size_t maxprt=-1,
206 bool si=false, bool ascd=false) const ;
207
208// Lecture,Ecriture sur fichier ASCII
209 virtual sa_size_t ReadASCII(istream& is, sa_size_t & nr, sa_size_t & nc,
210 char clm='#', const char* sep=" \t");
211 virtual void WriteASCII(ostream& os) const;
212
213// Pour la gestion de persistance
214 friend class FIO_TArray<T>;
215
216protected:
217
218 NDataBlock<T> mNDBlock; //!< Block for datas
219 mutable MuTyV my_mtv; //!< for use by ValueAtPosition()
220};
221
222////////////////////////////////////////////////////////////////
223// Surcharge d'operateur <<
224//! Print TArray \b a on stream \b os
225template <class T>
226inline ostream& operator << (ostream& os, const TArray<T>& a)
227 { a.Print(os); return(os); }
228
229// Surcharge d'operateur >>
230//! Decodes the ASCII input stream \b is , filling TArray \b a elements
231template <class T>
232inline istream& operator >> (istream& is, TArray<T>& a)
233 { sa_size_t nr, nc;
234 a.ReadASCII(is, nr, nc); return(is); }
235
236
237////////////////////////////////////////////////////////////////
238// Surcharge d'operateurs A (+,-,*,/) (T) x
239
240/*! \ingroup TArray \fn operator+(const TArray<T>&,T)
241 \brief Operator TArray = TArray + constant */
242template <class T> inline TArray<T> operator + (const TArray<T>& a, T b)
243 {TArray<T> result; result.SetTemp(true);
244 a.AddCst(b, result); return result;}
245
246/*! \ingroup TArray \fn operator+(T,const TArray<T>&)
247 \brief Operator TArray = constant + TArray */
248template <class T> inline TArray<T> operator + (T b,const TArray<T>& a)
249 {TArray<T> result; result.SetTemp(true);
250 a.AddCst(b, result); return result;}
251
252/*! \ingroup TArray \fn operator-(const TArray<T>&,T)
253 \brief Operator TArray = TArray - constant */
254template <class T> inline TArray<T> operator - (const TArray<T>& a, T b)
255 {TArray<T> result; result.SetTemp(true);
256 a.SubCst(b,result); return result;}
257
258/*! \ingroup TArray \fn operator-(T,const TArray<T>&)
259 \brief Operator TArray = constant - TArray */
260template <class T> inline TArray<T> operator - (T b,const TArray<T>& a)
261 {TArray<T> result; result.SetTemp(true);
262 a.SubCst(b,result,true); return result;}
263
264/*! \ingroup TArray \fn operator*(const TArray<T>&,T)
265 \brief Operator TArray = TArray * constant */
266template <class T> inline TArray<T> operator * (const TArray<T>& a, T b)
267 {TArray<T> result; result.SetTemp(true);
268 a.MulCst(b, result); return result;}
269
270/*! \ingroup TArray \fn operator*(T,const TArray<T>&)
271 \brief Operator TArray = constant * TArray */
272template <class T> inline TArray<T> operator * (T b,const TArray<T>& a)
273 {TArray<T> result; result.SetTemp(true);
274 a.MulCst(b,result); return result;}
275
276/*! \ingroup TArray \fn operator/(const TArray<T>&,T)
277 \brief Operator TArray = TArray / constant */
278template <class T> inline TArray<T> operator / (const TArray<T>& a, T b)
279 {TArray<T> result; result.SetTemp(true);
280 a.DivCst(b,result); return result;}
281
282/*! \ingroup TArray \fn operator/(T,const TArray<T>&)
283 \brief Operator TArray = constant / TArray */
284template <class T> inline TArray<T> operator / (T b, const TArray<T>& a)
285 {TArray<T> result; result.SetTemp(true);
286 a.DivCst(b, result, true); return result;}
287
288////////////////////////////////////////////////////////////////
289// Surcharge d'operateurs B = -A
290
291/*! \ingroup TArray \fn operator - (const TArray<T>&)
292 \brief Operator - Returns an array with elements equal to the opposite of
293 the original array elements. */
294template <class T> inline TArray<T> operator - (const TArray<T>& a)
295 {TArray<T> result; result.SetTemp(true);
296 a.NegateElt(result); return result;}
297
298////////////////////////////////////////////////////////////////
299// Surcharge d'operateurs C = A (+,-) B
300
301/*! \ingroup TArray \fn operator+(const TArray<T>&,const TArray<T>&)
302 \brief Operator TArray = TArray + TArray (element by element addition) */
303template <class T>
304inline TArray<T> operator + (const TArray<T>& a,const TArray<T>& b)
305 { TArray<T> result; result.SetTemp(true);
306 a.AddElt(b, result); return result; }
307
308/*! \ingroup TArray \fn operator-(const TArray<T>&,const TArray<T>&)
309 \brief Operator TArray = TArray - TArray (element by element subtraction) */
310template <class T>
311inline TArray<T> operator - (const TArray<T>& a,const TArray<T>& b)
312 { TArray<T> result; result.SetTemp(true);
313 a.SubElt(b, result); return result; }
314
315/*! \ingroup TArray \fn MultiplyElt((const TArray<T>&,const TArray<T>&)
316 \brief Element by element multiplication of two arrays TArray = TArray * TArray */
317
318template <class T>
319inline TArray<T> MultiplyElt(const TArray<T>& a,const TArray<T>& b)
320 { TArray<T> result; result.SetTemp(true);
321 a.MulElt(b, result); return result; }
322
323/*! \ingroup TArray \fn DivideElt((const TArray<T>&,const TArray<T>&)
324 \brief Element by element division of two arrays TArray = TArray * TArray */
325template <class T>
326inline TArray<T> DivideElt(const TArray<T>& a,const TArray<T>& b, bool divzero=false)
327 { TArray<T> result; result.SetTemp(true);
328 a.DivElt(b, result, false, divzero); return result; }
329
330// --------------------------------------------------
331// inline element acces methods
332// --------------------------------------------------
333
334//! Return element (ix,iy,iz,it,iu) value
335template <class T>
336inline T const& TArray<T>::Elem(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu) const
337{
338 return ( *( mNDBlock.Begin()+ offset_+
339 ix*step_[0] + iy*step_[1] + iz*step_[2] +
340 it*step_[3] + iu*step_[4]) );
341}
342
343//! Return element (ix,iy,iz,it,iu) value
344template <class T>
345inline T & TArray<T>::Elem(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu)
346{
347 return ( *( mNDBlock.Begin()+ offset_+
348 ix*step_[0] + iy*step_[1] + iz*step_[2] +
349 it*step_[3] + iu*step_[4]) );
350}
351
352//! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
353template <class T>
354inline T const& TArray<T>::ElemCheckBound(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu) const
355{
356 CheckBound(ix, iy, iz, it, iu, 4);
357 return(Elem(ix, iy, iz, it, iu));
358}
359
360//! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
361template <class T>
362inline T & TArray<T>::ElemCheckBound(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu)
363{
364 CheckBound(ix, iy, iz, it, iu, 4);
365 return(Elem(ix, iy, iz, it, iu));
366}
367
368//! Return element (ix,iy,iz) value
369template <class T>
370inline T const& TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz) const
371{
372#ifdef SO_BOUNDCHECKING
373 CheckBound(ix, iy, iz, 0, 0, 4);
374#endif
375 return ( *( mNDBlock.Begin()+ offset_+
376 ix*step_[0] + iy*step_[1] + iz*step_[2]) );
377}
378
379//! Return element (ix,iy,iz) value
380template <class T>
381inline T & TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz)
382{
383#ifdef SO_BOUNDCHECKING
384 CheckBound(ix, iy, iz, 0, 0, 4);
385#endif
386 return ( *( mNDBlock.Begin()+ offset_+
387 ix*step_[0] + iy*step_[1] + iz*step_[2]) );
388}
389
390//! Operator () : return element (ix,iy,iz,it,iu) value
391template <class T>
392inline T const& TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu) const
393{
394#ifdef SO_BOUNDCHECKING
395 CheckBound(ix, iy, iz, it, iu, 4);
396#endif
397 return ( *( mNDBlock.Begin()+ offset_+
398 ix*step_[0] + iy*step_[1] + iz*step_[2] +
399 it*step_[3] + iu*step_[4]) );
400}
401
402//! Operator () : return element (ix,iy,iz,it,iu) value
403template <class T>
404inline T & TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu)
405{
406#ifdef SO_BOUNDCHECKING
407 CheckBound(ix, iy, iz, it, iu, 4);
408#endif
409 return ( *( mNDBlock.Begin()+ offset_+
410 ix*step_[0] + iy*step_[1] + iz*step_[2] +
411 it*step_[3] + iu*step_[4]) );
412}
413
414
415//! Operator [] : return element at positon ip
416template <class T>
417inline T const& TArray<T>::operator[](sa_size_t ip) const
418{
419#ifdef SO_BOUNDCHECKING
420 if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
421#endif
422return *(mNDBlock.Begin()+Offset(ip));
423}
424
425//! Operator [] : return element at positon ip
426template <class T>
427inline T & TArray<T>::operator[](sa_size_t ip)
428{
429#ifdef SO_BOUNDCHECKING
430 if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
431#endif
432return *(mNDBlock.Begin()+Offset(ip));
433}
434
435
436//! Converts to a scalar (value of first element) if the array size is equal to 1
437template <class T>
438inline T TArray<T>::toScalar()
439{
440 if (Size() != 1) throw(SzMismatchError("TArray<T>::operator T() Size() != 1")) ;
441 return ( (*this)[0] );
442}
443
444// Typedef pour simplifier
445/*! \ingroup TArray
446 \typedef Array
447 \brief To simplified TArray<r_8> writing
448*/
449typedef TArray<r_8> Array;
450
451} // Fin du namespace
452
453#endif
Note: See TracBrowser for help on using the repository browser.