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

Last change on this file since 1757 was 1576, checked in by ansari, 24 years ago

Petite correction de definition istream >> , Reza 18/7/2001

File size: 15.8 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>
10#include <iostream.h>
[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();
[1156]31 TArray(int_4 ndim, const sa_size_t * siz, sa_size_t step =1);
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);
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
[1156]59 void ReSize(int_4 ndim, sa_size_t * siz, sa_size_t step=1);
[1517]60 void ReSize(const BaseArray& a);
[1393]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)
63 { ReSize(ndim, siz, step); }
[1517]64 //! a synonym (alias) for method ReSize(const BaseArray&)
65 inline void SetSize(const BaseArray& a)
[1393]66 { ReSize(a); }
[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 ;
[963]78#if !defined(__GNUG__)
[894]79 //! () operator for Sub arrays extraction
80 /*! \sa SubArray */
[804]81 inline TArray<T> operator () (Range rx, Range ry, Range rz, Range rt=0, Range ru=0) const
82 { return SubArray(rx, ry, rz, rt, ru); }
[963]83#else
84 // g++ (2.95) se melange les pinceaux avec les arguments par defaut !
85 //! () operator for Sub arrays extraction
86 /*! \sa SubArray */
87 inline TArray<T> operator () (Range rx, Range ry, Range rz) const
88 { return SubArray(rx, ry, rz, Range(0), Range(0)); }
89 inline TArray<T> operator () (Range rx, Range ry, Range rz, Range rt) const
90 { return SubArray(rx, ry, rz, rt, Range(0)); }
91 inline TArray<T> operator () (Range rx, Range ry, Range rz, Range rt, Range ru) const
92 { return SubArray(rx, ry, rz, rt, ru); }
93#endif
[772]94
[787]95 // ---- Access to data
96 // Definition of virtual element acces method inherited from BaseArray class
[1156]97 virtual MuTyV & ValueAtPosition(sa_size_t ip) const;
[785]98
[787]99 // Data Access: operator overloaded inline acces methods
[1156]100 inline T const& operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz) const ;
101 inline T& operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz);
102 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 ;
103 inline T& operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu=0);
104 inline T const& operator[](sa_size_t ip) const ;
105 inline T& operator[](sa_size_t ip);
[772]106
[1156]107 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 ;
108 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);
109 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 ;
110 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]111
[894]112 //! Return pointer to first element adress
[772]113 inline T* Data() {return mNDBlock.Begin()+offset_;}
[894]114 //! Return pointer to first element adress
[772]115 inline const T* Data() const {return mNDBlock.Begin()+offset_;}
[894]116 //! Return reference to datablock NDataBlock
[772]117 inline NDataBlock<T>& DataBlock() {return mNDBlock;}
[894]118 //! Return reference to datablock NDataBlock
[772]119 inline const NDataBlock<T>& DataBlock() const {return mNDBlock;}
120
[787]121 // Temporaire?
[894]122 //! Are the array temporay ?
[787]123 inline bool IsTemp(void) const {return mNDBlock.IsTemp();}
[894]124 //! Set the array as temporay
[787]125 inline void SetTemp(bool temp=false) const {mNDBlock.SetTemp(temp);}
126
[772]127// Operations diverses = , +=, ...
128// Conversion en type T, if Size() == 1
[787]129 inline T toScalar();
[772]130// Met les elements a une suite de valeurs
[1103]131 virtual TArray<T>& SetSeq(Sequence const & seq);
[894]132 //! Fill TArray with Sequence \b seq
[1103]133 inline TArray<T>& operator = (Sequence const & seq) { return SetSeq(seq); }
[772]134// A = x (tous les elements a x)
[813]135 virtual TArray<T>& SetT(T x);
[894]136 //! Fill TArray with all elements equal to \b x
[813]137 inline TArray<T>& operator = (T x) { return SetT(x); }
[772]138// A += -= *= /= x (ajoute, soustrait, ... x a tous les elements)
[804]139 virtual TArray<T>& Add(T x);
[894]140 //! Add \b x to all elements
[804]141 inline TArray<T>& operator += (T x) { return Add(x); }
[970]142 virtual TArray<T>& Sub(T x, bool fginv=false);
[894]143 //! Substract \b x to all elements
[804]144 inline TArray<T>& operator -= (T x) { return Sub(x); }
145 virtual TArray<T>& Mul(T x);
[894]146 //! Multiply all elements by \b x
[804]147 inline TArray<T>& operator *= (T x) { return Mul(x); }
[970]148 virtual TArray<T>& Div(T x, bool fginv=false);
[894]149 //! Divide all elements by \b x
[804]150 inline TArray<T>& operator /= (T x) { return Div(x); }
151
[1156]152// applique le signe moins a tous les elements
153 virtual TArray<T>& NegateElt();
154
[772]155// A += -= (ajoute, soustrait element par element les deux tableaux )
[804]156 virtual TArray<T>& AddElt(const TArray<T>& a);
[894]157 //! Operator TArray += TArray
[804]158 inline TArray<T>& operator += (const TArray<T>& a) { return AddElt(a); }
[970]159 virtual TArray<T>& SubElt(const TArray<T>& a, bool fginv=false);
[894]160 //! Operator TArray -= TArray
[804]161 inline TArray<T>& operator -= (const TArray<T>& a) { return SubElt(a); }
[787]162// Multiplication, division element par element les deux tableaux
[804]163 virtual TArray<T>& MulElt(const TArray<T>& a);
[1072]164 virtual TArray<T>& DivElt(const TArray<T>& a, bool fginv=false, bool divzero=false);
[804]165// Recopie des valeurs, element par element
166 virtual TArray<T>& CopyElt(const TArray<T>& a);
[1081]167// Recopie des valeurs avec conversion prealable, element par element
168 virtual TArray<T>& ConvertAndCopyElt(const BaseArray& a);
[772]169
[804]170// Somme et produit des elements
171 virtual T Sum() const ;
172 virtual T Product() const ;
[1113]173// Somme du carre des elements
174 virtual T SumX2() const;
175// Valeur min et max des elements
176 virtual void MinMax(T& min, T& max) const ;
[804]177
[772]178// Impression, I/O, ...
[813]179 virtual string InfoString() const;
[1550]180 virtual void Print(ostream& os, sa_size_t maxprt=-1,
181 bool si=false, bool ascd=false) const ;
[772]182
[1517]183// Lecture,Ecriture sur fichier ASCII
[1558]184 virtual sa_size_t ReadASCII(istream& is, sa_size_t & nr, sa_size_t & nc);
[1517]185 virtual void WriteASCII(ostream& os) const;
186
[772]187// Pour la gestion de persistance
188 friend class FIO_TArray<T>;
189
190protected:
191
[894]192 NDataBlock<T> mNDBlock; //!< Block for datas
[1081]193 mutable MuTyV my_mtv; //!< for use by ValueAtPosition()
[772]194};
195
196////////////////////////////////////////////////////////////////
197// Surcharge d'operateur <<
[894]198//! Print TArray \b a on stream \b os
[772]199template <class T>
200inline ostream& operator << (ostream& os, const TArray<T>& a)
201 { a.Print(os); return(os); }
202
[1517]203// Surcharge d'operateur >>
204//! Decodes the ASCII input stream \b is , filling TArray \b a elements
205template <class T>
206inline istream& operator >> (istream& is, TArray<T>& a)
[1576]207 { sa_size_t nr, nc;
208 a.ReadASCII(is, nr, nc); return(is); }
[1517]209
210
[772]211////////////////////////////////////////////////////////////////
212// Surcharge d'operateurs A (+,-,*,/) (T) x
213
[958]214/*! \ingroup TArray \fn operator+(const TArray<T>&,T)
215 \brief Operator TArray = TArray + constant */
[772]216template <class T> inline TArray<T> operator + (const TArray<T>& a, T b)
[970]217 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
218 result.Add(b); return result;}
[772]219
[958]220/*! \ingroup TArray \fn operator+(T,const TArray<T>&)
221 \brief Operator TArray = constant + TArray */
[772]222template <class T> inline TArray<T> operator + (T b,const TArray<T>& a)
[970]223 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
224 result.Add(b); return result;}
[772]225
[958]226/*! \ingroup TArray \fn operator-(const TArray<T>&,T)
227 \brief Operator TArray = TArray - constant */
[772]228template <class T> inline TArray<T> operator - (const TArray<T>& a, T b)
[970]229 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
230 result.Sub(b); return result;}
[772]231
[958]232/*! \ingroup TArray \fn operator-(T,const TArray<T>&)
233 \brief Operator TArray = constant - TArray */
[772]234template <class T> inline TArray<T> operator - (T b,const TArray<T>& a)
[970]235 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
236 result.Sub(b,true); return result;}
[772]237
[958]238/*! \ingroup TArray \fn operator*(const TArray<T>&,T)
239 \brief Operator TArray = TArray * constant */
[772]240template <class T> inline TArray<T> operator * (const TArray<T>& a, T b)
[970]241 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
242 result.Mul(b); return result;}
[772]243
[958]244/*! \ingroup TArray \fn operator*(T,const TArray<T>&)
245 \brief Operator TArray = constant * TArray */
[772]246template <class T> inline TArray<T> operator * (T b,const TArray<T>& a)
[970]247 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
248 result.Mul(b); return result;}
[772]249
[958]250/*! \ingroup TArray \fn operator/(const TArray<T>&,T)
251 \brief Operator TArray = TArray / constant */
[772]252template <class T> inline TArray<T> operator / (const TArray<T>& a, T b)
[970]253 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
254 result.Div(b); return result;}
[772]255
[970]256/*! \ingroup TArray \fn operator/(T,const TArray<T>&)
257 \brief Operator TArray = constant / TArray */
258template <class T> inline TArray<T> operator / (T b, const TArray<T>& a)
259 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
260 result.Div(b, true); return result;}
261
[772]262////////////////////////////////////////////////////////////////
[1156]263// Surcharge d'operateurs B = -A
264
265/*! \ingroup TArray \fn operator - (const TArray<T>&)
266 \brief Operator - Returns an array with elements equal to the opposite of
267 the original array elements. */
268template <class T> inline TArray<T> operator - (const TArray<T>& a)
269 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
270 result.NegateElt(); return result;}
271
272////////////////////////////////////////////////////////////////
[772]273// Surcharge d'operateurs C = A (+,-) B
274
[958]275/*! \ingroup TArray \fn operator+(const TArray<T>&,const TArray<T>&)
276 \brief Operator TArray = TArray + TArray */
[772]277template <class T>
278inline TArray<T> operator + (const TArray<T>& a,const TArray<T>& b)
[970]279 { TArray<T> result; result.SetTemp(true);
280 if (b.IsTemp()) { result.Share(b); result.AddElt(a); }
281 else { result.CloneOrShare(a); result.AddElt(b); }
282 return result; }
[772]283
[958]284/*! \ingroup TArray \fn operator-(const TArray<T>&,const TArray<T>&)
285 \brief Operator TArray = TArray - TArray */
[772]286template <class T>
287inline TArray<T> operator - (const TArray<T>& a,const TArray<T>& b)
[970]288 { TArray<T> result; result.SetTemp(true);
289 if (b.IsTemp()) { result.Share(b); result.SubElt(a, true); }
290 else { result.CloneOrShare(a); result.SubElt(b); }
291 return result; }
[772]292
293
294// --------------------------------------------------
295// inline element acces methods
296// --------------------------------------------------
[894]297
298//! Return element (ix,iy,iz,it,iu) value
[772]299template <class T>
[1156]300inline 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]301{
302 return ( *( mNDBlock.Begin()+ offset_+
303 ix*step_[0] + iy*step_[1] + iz*step_[2] +
304 it*step_[3] + iu*step_[4]) );
305}
306
[894]307//! Return element (ix,iy,iz,it,iu) value
[772]308template <class T>
[1156]309inline 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]310{
311 return ( *( mNDBlock.Begin()+ offset_+
312 ix*step_[0] + iy*step_[1] + iz*step_[2] +
313 it*step_[3] + iu*step_[4]) );
314}
315
[894]316//! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
[772]317template <class T>
[1156]318inline 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]319{
320 CheckBound(ix, iy, iz, it, iu, 4);
[804]321 return(Elem(ix, iy, iz, it, iu));
[772]322}
323
[894]324//! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
[772]325template <class T>
[1156]326inline 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]327{
328 CheckBound(ix, iy, iz, it, iu, 4);
[804]329 return(Elem(ix, iy, iz, it, iu));
[772]330}
331
[894]332//! Return element (ix,iy,iz) value
[772]333template <class T>
[1156]334inline T const& TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz) const
[772]335{
336#ifdef SO_BOUNDCHECKING
337 CheckBound(ix, iy, iz, 0, 0, 4);
338#endif
339 return ( *( mNDBlock.Begin()+ offset_+
340 ix*step_[0] + iy*step_[1] + iz*step_[2]) );
341}
342
[894]343//! Return element (ix,iy,iz) value
[772]344template <class T>
[1156]345inline T & TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz)
[772]346{
347#ifdef SO_BOUNDCHECKING
348 CheckBound(ix, iy, iz, 0, 0, 4);
349#endif
350 return ( *( mNDBlock.Begin()+ offset_+
351 ix*step_[0] + iy*step_[1] + iz*step_[2]) );
352}
353
[894]354//! Operator () : return element (ix,iy,iz,it,iu) value
[772]355template <class T>
[1156]356inline 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]357{
358#ifdef SO_BOUNDCHECKING
359 CheckBound(ix, iy, iz, it, iu, 4);
360#endif
361 return ( *( mNDBlock.Begin()+ offset_+
362 ix*step_[0] + iy*step_[1] + iz*step_[2] +
363 it*step_[3] + iu*step_[4]) );
364}
365
[894]366//! Operator () : return element (ix,iy,iz,it,iu) value
[772]367template <class T>
[1156]368inline 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]369{
370#ifdef SO_BOUNDCHECKING
371 CheckBound(ix, iy, iz, it, iu, 4);
372#endif
373 return ( *( mNDBlock.Begin()+ offset_+
374 ix*step_[0] + iy*step_[1] + iz*step_[2] +
375 it*step_[3] + iu*step_[4]) );
376}
377
[785]378
[894]379//! Operator [] : return element at positon ip
[772]380template <class T>
[1156]381inline T const& TArray<T>::operator[](sa_size_t ip) const
[772]382{
383#ifdef SO_BOUNDCHECKING
384 if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
385#endif
[785]386return *(mNDBlock.Begin()+Offset(ip));
[772]387}
388
[894]389//! Operator [] : return element at positon ip
[772]390template <class T>
[1156]391inline T & TArray<T>::operator[](sa_size_t ip)
[772]392{
393#ifdef SO_BOUNDCHECKING
394 if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
395#endif
[785]396return *(mNDBlock.Begin()+Offset(ip));
[772]397}
398
[785]399
[1156]400//! Converts to a scalar (value of first element) if the array size is equal to 1
[772]401template <class T>
[787]402inline T TArray<T>::toScalar()
[772]403{
404 if (Size() != 1) throw(SzMismatchError("TArray<T>::operator T() Size() != 1")) ;
405 return ( (*this)[0] );
406}
407
[804]408// Typedef pour simplifier
[956]409/*! \ingroup TArray
410 \typedef Array
411 \brief To simplified TArray<r_8> writing
412*/
[804]413typedef TArray<r_8> Array;
414
[772]415} // Fin du namespace
416
417#endif
Note: See TracBrowser for help on using the repository browser.