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
RevLine 
[772]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>
[2322]10#include <iostream>
[787]11#include "basarr.h"
[772]12#include "ndatablock.h"
13#include <complex>
[785]14#include "utilarr.h"
[772]15
16
17namespace SOPHYA {
18
19// Forward declaration
20template <class T> class FIO_TArray;
21
[787]22// --------------------------- classe template Array -----------------------
23// ( See BaseArray class for data organisation in memory and related methods )
24
[894]25//! Class for template arrays
[772]26template <class T>
[787]27class TArray : public BaseArray {
[772]28public:
29 // Creation / destruction
30 TArray();
[2564]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);
[1156]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);
[772]35 TArray(const TArray<T>& a);
36 TArray(const TArray<T>& a, bool share);
[1081]37 TArray(const BaseArray& a);
[772]38
39 virtual ~TArray();
40
[967]41 // A = B
[894]42 //! = operator between TArray
[976]43 /*! \warning Datas are copied (cloned) from \b a.
44 \sa Set \sa NDataBlock::operator=(const NDataBlock<T>&) */
[804]45 inline TArray<T>& operator = (const TArray<T>& a) { return Set(a); }
46 virtual TArray<T>& Set(const TArray<T>& a);
[772]47
[1081]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
[772]52 // Gestion taille/Remplissage
53 virtual void Clone(const TArray<T>& a);
[970]54 // partage les donnees si "a" temporaire, clone sinon.
55 void CloneOrShare(const TArray<T>& a);
56 // Share: partage les donnees de "a"
[1393]57 void Share(const TArray<T>& a);
[970]58
[2564]59 void ReSize(int_4 ndim, sa_size_t * siz, sa_size_t step=1, bool fzero=true);
[2569]60 void ReSize(const BaseArray& a, bool pack=false, bool fzero=true);
[1393]61 //! a synonym (alias) for method ReSize(int_4, ...)
[2564]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); }
[1517]64 //! a synonym (alias) for method ReSize(const BaseArray&)
[2569]65 inline void SetSize(const BaseArray& a, bool pack=false, bool fzero=true)
[2564]66 { ReSize(a, pack, fzero); }
[1156]67 void Realloc(int_4 ndim, sa_size_t * siz, sa_size_t step=1, bool force=false);
[787]68
[785]69 // Compacts size=1 array dimensions
[787]70 virtual TArray<T>& CompactAllDimensions();
71 virtual TArray<T>& CompactTrailingDimensions();
[785]72
[804]73 // Packing array elements in memory
74 virtual TArray<T> PackElements(bool force=false) const ;
[772]75
[804]76 // SubArrays - $CHECK$ Reza 03/2000 je ne sais pas s'il faut declarer ca const ??
[894]77 TArray<T> SubArray(Range rx, Range ry, Range rz, Range rt, Range ru) const ;
[1891]78
[894]79 //! () operator for Sub arrays extraction
80 /*! \sa SubArray */
[963]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); }
[772]87
[787]88 // ---- Access to data
89 // Definition of virtual element acces method inherited from BaseArray class
[1156]90 virtual MuTyV & ValueAtPosition(sa_size_t ip) const;
[2888]91 virtual MuTyV & ValueAtPositionDB(sa_size_t ip) const;
[785]92
[787]93 // Data Access: operator overloaded inline acces methods
[1156]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);
[772]100
[1156]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);
[772]105
[894]106 //! Return pointer to first element adress
[772]107 inline T* Data() {return mNDBlock.Begin()+offset_;}
[894]108 //! Return pointer to first element adress
[772]109 inline const T* Data() const {return mNDBlock.Begin()+offset_;}
[894]110 //! Return reference to datablock NDataBlock
[772]111 inline NDataBlock<T>& DataBlock() {return mNDBlock;}
[894]112 //! Return reference to datablock NDataBlock
[772]113 inline const NDataBlock<T>& DataBlock() const {return mNDBlock;}
114
[787]115 // Temporaire?
[894]116 //! Are the array temporay ?
[787]117 inline bool IsTemp(void) const {return mNDBlock.IsTemp();}
[894]118 //! Set the array as temporay
[787]119 inline void SetTemp(bool temp=false) const {mNDBlock.SetTemp(temp);}
120
[772]121// Operations diverses = , +=, ...
122// Conversion en type T, if Size() == 1
[787]123 inline T toScalar();
[772]124// Met les elements a une suite de valeurs
[1103]125 virtual TArray<T>& SetSeq(Sequence const & seq);
[894]126 //! Fill TArray with Sequence \b seq
[1103]127 inline TArray<T>& operator = (Sequence const & seq) { return SetSeq(seq); }
[2575]128
[772]129// A = x (tous les elements a x)
[2575]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); }
[894]133 //! Fill TArray with all elements equal to \b x
[813]134 inline TArray<T>& operator = (T x) { return SetT(x); }
[2564]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
[772]143// A += -= *= /= x (ajoute, soustrait, ... x a tous les elements)
[2575]144 // Methodes Add/Sub/Mul/Div() sont la pour compatibilite avant V=2 (1.818)
145 // Faut-il les garder ? Reza, Juillet 2004
[2564]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
[894]151 //! Add \b x to all elements
[2564]152 inline TArray<T>& operator += (T x) { return AddCst(x, *this); }
[894]153 //! Substract \b x to all elements
[2564]154 inline TArray<T>& operator -= (T x) { return SubCst(x, *this); }
[894]155 //! Multiply all elements by \b x
[2564]156 inline TArray<T>& operator *= (T x) { return MulCst(x, *this); }
[894]157 //! Divide all elements by \b x
[2564]158 inline TArray<T>& operator /= (T x) { return DivCst(x, *this); }
[804]159
[1156]160// applique le signe moins a tous les elements
[2564]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
[772]165// A += -= (ajoute, soustrait element par element les deux tableaux )
[2575]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 ;
[787]168// Multiplication, division element par element les deux tableaux
[2575]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
[2588]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)
[2589]181 inline TArray<T>& Div(const TArray<T>& a, bool divzero=false)
182 { return DivElt(a, *this, false, divzero); }
[2588]183
[804]184// Recopie des valeurs, element par element
185 virtual TArray<T>& CopyElt(const TArray<T>& a);
[1081]186// Recopie des valeurs avec conversion prealable, element par element
187 virtual TArray<T>& ConvertAndCopyElt(const BaseArray& a);
[772]188
[2575]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
[804]195// Somme et produit des elements
196 virtual T Sum() const ;
197 virtual T Product() const ;
[1113]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 ;
[804]202
[772]203// Impression, I/O, ...
[813]204 virtual string InfoString() const;
[1550]205 virtual void Print(ostream& os, sa_size_t maxprt=-1,
206 bool si=false, bool ascd=false) const ;
[772]207
[1517]208// Lecture,Ecriture sur fichier ASCII
[2286]209 virtual sa_size_t ReadASCII(istream& is, sa_size_t & nr, sa_size_t & nc,
210 char clm='#', const char* sep=" \t");
[1517]211 virtual void WriteASCII(ostream& os) const;
212
[772]213// Pour la gestion de persistance
214 friend class FIO_TArray<T>;
215
216protected:
217
[894]218 NDataBlock<T> mNDBlock; //!< Block for datas
[1081]219 mutable MuTyV my_mtv; //!< for use by ValueAtPosition()
[772]220};
221
222////////////////////////////////////////////////////////////////
223// Surcharge d'operateur <<
[894]224//! Print TArray \b a on stream \b os
[772]225template <class T>
226inline ostream& operator << (ostream& os, const TArray<T>& a)
227 { a.Print(os); return(os); }
228
[1517]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)
[1576]233 { sa_size_t nr, nc;
234 a.ReadASCII(is, nr, nc); return(is); }
[1517]235
236
[772]237////////////////////////////////////////////////////////////////
238// Surcharge d'operateurs A (+,-,*,/) (T) x
239
[958]240/*! \ingroup TArray \fn operator+(const TArray<T>&,T)
241 \brief Operator TArray = TArray + constant */
[772]242template <class T> inline TArray<T> operator + (const TArray<T>& a, T b)
[2564]243 {TArray<T> result; result.SetTemp(true);
[2569]244 a.AddCst(b, result); return result;}
[772]245
[958]246/*! \ingroup TArray \fn operator+(T,const TArray<T>&)
247 \brief Operator TArray = constant + TArray */
[772]248template <class T> inline TArray<T> operator + (T b,const TArray<T>& a)
[2564]249 {TArray<T> result; result.SetTemp(true);
[2569]250 a.AddCst(b, result); return result;}
[772]251
[958]252/*! \ingroup TArray \fn operator-(const TArray<T>&,T)
253 \brief Operator TArray = TArray - constant */
[772]254template <class T> inline TArray<T> operator - (const TArray<T>& a, T b)
[2564]255 {TArray<T> result; result.SetTemp(true);
[2569]256 a.SubCst(b,result); return result;}
[772]257
[958]258/*! \ingroup TArray \fn operator-(T,const TArray<T>&)
259 \brief Operator TArray = constant - TArray */
[772]260template <class T> inline TArray<T> operator - (T b,const TArray<T>& a)
[2564]261 {TArray<T> result; result.SetTemp(true);
[2569]262 a.SubCst(b,result,true); return result;}
[772]263
[958]264/*! \ingroup TArray \fn operator*(const TArray<T>&,T)
265 \brief Operator TArray = TArray * constant */
[772]266template <class T> inline TArray<T> operator * (const TArray<T>& a, T b)
[2564]267 {TArray<T> result; result.SetTemp(true);
268 a.MulCst(b, result); return result;}
[772]269
[958]270/*! \ingroup TArray \fn operator*(T,const TArray<T>&)
271 \brief Operator TArray = constant * TArray */
[772]272template <class T> inline TArray<T> operator * (T b,const TArray<T>& a)
[2564]273 {TArray<T> result; result.SetTemp(true);
274 a.MulCst(b,result); return result;}
[772]275
[958]276/*! \ingroup TArray \fn operator/(const TArray<T>&,T)
277 \brief Operator TArray = TArray / constant */
[772]278template <class T> inline TArray<T> operator / (const TArray<T>& a, T b)
[2564]279 {TArray<T> result; result.SetTemp(true);
[2569]280 a.DivCst(b,result); return result;}
[772]281
[970]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)
[2564]285 {TArray<T> result; result.SetTemp(true);
[2569]286 a.DivCst(b, result, true); return result;}
[970]287
[772]288////////////////////////////////////////////////////////////////
[1156]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)
[2569]295 {TArray<T> result; result.SetTemp(true);
296 a.NegateElt(result); return result;}
[1156]297
298////////////////////////////////////////////////////////////////
[772]299// Surcharge d'operateurs C = A (+,-) B
300
[958]301/*! \ingroup TArray \fn operator+(const TArray<T>&,const TArray<T>&)
[2588]302 \brief Operator TArray = TArray + TArray (element by element addition) */
[772]303template <class T>
304inline TArray<T> operator + (const TArray<T>& a,const TArray<T>& b)
[970]305 { TArray<T> result; result.SetTemp(true);
[2575]306 a.AddElt(b, result); return result; }
[772]307
[958]308/*! \ingroup TArray \fn operator-(const TArray<T>&,const TArray<T>&)
[2588]309 \brief Operator TArray = TArray - TArray (element by element subtraction) */
[772]310template <class T>
311inline TArray<T> operator - (const TArray<T>& a,const TArray<T>& b)
[970]312 { TArray<T> result; result.SetTemp(true);
[2575]313 a.SubElt(b, result); return result; }
[772]314
[2588]315/*! \ingroup TArray \fn MultiplyElt((const TArray<T>&,const TArray<T>&)
316 \brief Element by element multiplication of two arrays TArray = TArray * TArray */
[772]317
[2588]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>
[2589]326inline TArray<T> DivideElt(const TArray<T>& a,const TArray<T>& b, bool divzero=false)
[2588]327 { TArray<T> result; result.SetTemp(true);
[2589]328 a.DivElt(b, result, false, divzero); return result; }
[2588]329
[772]330// --------------------------------------------------
331// inline element acces methods
332// --------------------------------------------------
[894]333
334//! Return element (ix,iy,iz,it,iu) value
[772]335template <class T>
[1156]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
[772]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
[894]343//! Return element (ix,iy,iz,it,iu) value
[772]344template <class T>
[1156]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)
[772]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
[894]352//! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
[772]353template <class T>
[1156]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
[772]355{
356 CheckBound(ix, iy, iz, it, iu, 4);
[804]357 return(Elem(ix, iy, iz, it, iu));
[772]358}
359
[894]360//! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
[772]361template <class T>
[1156]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)
[772]363{
364 CheckBound(ix, iy, iz, it, iu, 4);
[804]365 return(Elem(ix, iy, iz, it, iu));
[772]366}
367
[894]368//! Return element (ix,iy,iz) value
[772]369template <class T>
[1156]370inline T const& TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz) const
[772]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
[894]379//! Return element (ix,iy,iz) value
[772]380template <class T>
[1156]381inline T & TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz)
[772]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
[894]390//! Operator () : return element (ix,iy,iz,it,iu) value
[772]391template <class T>
[1156]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
[772]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
[894]402//! Operator () : return element (ix,iy,iz,it,iu) value
[772]403template <class T>
[1156]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)
[772]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
[785]414
[894]415//! Operator [] : return element at positon ip
[772]416template <class T>
[1156]417inline T const& TArray<T>::operator[](sa_size_t ip) const
[772]418{
419#ifdef SO_BOUNDCHECKING
420 if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
421#endif
[785]422return *(mNDBlock.Begin()+Offset(ip));
[772]423}
424
[894]425//! Operator [] : return element at positon ip
[772]426template <class T>
[1156]427inline T & TArray<T>::operator[](sa_size_t ip)
[772]428{
429#ifdef SO_BOUNDCHECKING
430 if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
431#endif
[785]432return *(mNDBlock.Begin()+Offset(ip));
[772]433}
434
[785]435
[1156]436//! Converts to a scalar (value of first element) if the array size is equal to 1
[772]437template <class T>
[787]438inline T TArray<T>::toScalar()
[772]439{
440 if (Size() != 1) throw(SzMismatchError("TArray<T>::operator T() Size() != 1")) ;
441 return ( (*this)[0] );
442}
443
[804]444// Typedef pour simplifier
[956]445/*! \ingroup TArray
446 \typedef Array
447 \brief To simplified TArray<r_8> writing
448*/
[804]449typedef TArray<r_8> Array;
450
[772]451} // Fin du namespace
452
453#endif
Note: See TracBrowser for help on using the repository browser.