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

Last change on this file since 960 was 958, checked in by ansari, 25 years ago

doc + vire warning cmv 18/04/00

File size: 12.3 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();
[804]31 TArray(uint_4 ndim, const uint_4 * siz, uint_4 step =1);
32 TArray(uint_4 nx, uint_4 ny=0, uint_4 nz=0, uint_4 nt=0, uint_4 nu=0);
33 TArray(uint_4 ndim, const uint_4 * siz, NDataBlock<T> & db, bool share=false, uint_4 step=1, uint_8 offset=0);
34 TArray(uint_4 ndim, const uint_4 * siz, T* values, uint_4 step=1, uint_8 offset=0, Bridge* br=NULL);
[772]35 TArray(const TArray<T>& a);
36 TArray(const TArray<T>& a, bool share);
37
38 virtual ~TArray();
39
40 // A = B : partage les donnees si "a" est temporaire, clone sinon.
[894]41 //! = operator between TArray
42 /*! \sa Set */
[804]43 inline TArray<T>& operator = (const TArray<T>& a) { return Set(a); }
44 virtual TArray<T>& Set(const TArray<T>& a);
[772]45
46 // Gestion taille/Remplissage
47 virtual void Clone(const TArray<T>& a);
[804]48 void ReSize(uint_4 ndim, uint_4 * siz, uint_4 step=1);
49 void Realloc(uint_4 ndim, uint_4 * siz, uint_4 step=1, bool force=false);
[787]50
[785]51 // Compacts size=1 array dimensions
[787]52 virtual TArray<T>& CompactAllDimensions();
53 virtual TArray<T>& CompactTrailingDimensions();
[785]54
[804]55 // Packing array elements in memory
56 virtual TArray<T> PackElements(bool force=false) const ;
[772]57
[804]58 // SubArrays - $CHECK$ Reza 03/2000 je ne sais pas s'il faut declarer ca const ??
[894]59 TArray<T> SubArray(Range rx, Range ry, Range rz, Range rt, Range ru) const ;
60 //! () operator for Sub arrays extraction
61 /*! \sa SubArray */
[804]62 inline TArray<T> operator () (Range rx, Range ry, Range rz, Range rt=0, Range ru=0) const
63 { return SubArray(rx, ry, rz, rt, ru); }
[772]64
[787]65 // ---- Access to data
66 // Definition of virtual element acces method inherited from BaseArray class
67 virtual double ValueAtPosition(uint_8 ip) const;
[785]68
[787]69 // Data Access: operator overloaded inline acces methods
[772]70 inline T const& operator()(uint_4 ix, uint_4 iy, uint_4 iz) const ;
71 inline T& operator()(uint_4 ix, uint_4 iy, uint_4 iz);
72 inline T const& operator()(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu=0) const ;
73 inline T& operator()(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu=0);
[785]74 inline T const& operator[](uint_8 ip) const ;
75 inline T& operator[](uint_8 ip);
[772]76
77 inline T const& Elem(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it=0, uint_4 iu=0) const ;
78 inline T& Elem(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it=0, uint_4 iu=0);
79 inline T const& ElemCheckBound(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it=0, uint_4 iu=0) const ;
80 inline T& ElemCheckBound(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it=0, uint_4 iu=0);
81
[894]82 //! Return pointer to first element adress
[772]83 inline T* Data() {return mNDBlock.Begin()+offset_;}
[894]84 //! Return pointer to first element adress
[772]85 inline const T* Data() const {return mNDBlock.Begin()+offset_;}
[894]86 //! Return reference to datablock NDataBlock
[772]87 inline NDataBlock<T>& DataBlock() {return mNDBlock;}
[894]88 //! Return reference to datablock NDataBlock
[772]89 inline const NDataBlock<T>& DataBlock() const {return mNDBlock;}
90
[787]91 // Temporaire?
[894]92 //! Are the array temporay ?
[787]93 inline bool IsTemp(void) const {return mNDBlock.IsTemp();}
[894]94 //! Set the array as temporay
[787]95 inline void SetTemp(bool temp=false) const {mNDBlock.SetTemp(temp);}
96
[772]97// Operations diverses = , +=, ...
98// Conversion en type T, if Size() == 1
[787]99 inline T toScalar();
[772]100// Met les elements a une suite de valeurs
[813]101 virtual TArray<T>& SetSeq(Sequence seq);
[894]102 //! Fill TArray with Sequence \b seq
[813]103 inline TArray<T>& operator = (Sequence seq) { return SetSeq(seq); }
[772]104// A = x (tous les elements a x)
[813]105 virtual TArray<T>& SetT(T x);
[894]106 //! Fill TArray with all elements equal to \b x
[813]107 inline TArray<T>& operator = (T x) { return SetT(x); }
[772]108// A += -= *= /= x (ajoute, soustrait, ... x a tous les elements)
[804]109 virtual TArray<T>& Add(T x);
[894]110 //! Add \b x to all elements
[804]111 inline TArray<T>& operator += (T x) { return Add(x); }
112 virtual TArray<T>& Sub(T x);
[894]113 //! Substract \b x to all elements
[804]114 inline TArray<T>& operator -= (T x) { return Sub(x); }
115 virtual TArray<T>& Mul(T x);
[894]116 //! Multiply all elements by \b x
[804]117 inline TArray<T>& operator *= (T x) { return Mul(x); }
118 virtual TArray<T>& Div(T x);
[894]119 //! Divide all elements by \b x
[804]120 inline TArray<T>& operator /= (T x) { return Div(x); }
121 virtual TArray<T>& SubInv(T x); // A ---> x-A
122 virtual TArray<T>& DivInv(T x); // A ---> x/A
123
[772]124// A += -= (ajoute, soustrait element par element les deux tableaux )
[804]125 virtual TArray<T>& AddElt(const TArray<T>& a);
[894]126 //! Operator TArray += TArray
[804]127 inline TArray<T>& operator += (const TArray<T>& a) { return AddElt(a); }
128 virtual TArray<T>& SubElt(const TArray<T>& a);
[894]129 //! Operator TArray -= TArray
[804]130 inline TArray<T>& operator -= (const TArray<T>& a) { return SubElt(a); }
[787]131// Multiplication, division element par element les deux tableaux
[804]132 virtual TArray<T>& MulElt(const TArray<T>& a);
[772]133 virtual TArray<T>& DivElt(const TArray<T>& a);
[804]134// Recopie des valeurs, element par element
135 virtual TArray<T>& CopyElt(const TArray<T>& a);
[772]136
[804]137// Somme et produit des elements
138 virtual T Sum() const ;
139 virtual T Product() const ;
140
[772]141// Impression, I/O, ...
[813]142 virtual string InfoString() const;
143 virtual void Print(ostream& os, int_4 maxprt=-1, bool si=false) const ;
[772]144
145// Pour la gestion de persistance
146 friend class FIO_TArray<T>;
147
148protected:
149 // partage les donnees si "a" temporaire, clone sinon.
150 void CloneOrShare(const TArray<T>& a);
151 // Share: partage les donnees de "a"
152 void Share(const TArray<T>& a);
153
[894]154 NDataBlock<T> mNDBlock; //!< Block for datas
[772]155};
156
157////////////////////////////////////////////////////////////////
158// Surcharge d'operateur <<
[894]159//! Print TArray \b a on stream \b os
[772]160template <class T>
161inline ostream& operator << (ostream& os, const TArray<T>& a)
162 { a.Print(os); return(os); }
163
164////////////////////////////////////////////////////////////////
165// Surcharge d'operateurs A (+,-,*,/) (T) x
166
[958]167/*! \ingroup TArray \fn operator+(const TArray<T>&,T)
168 \brief Operator TArray = TArray + constant */
[772]169template <class T> inline TArray<T> operator + (const TArray<T>& a, T b)
[804]170 {TArray<T> result(a); result.SetTemp(true); result.Add(b); return result;}
[772]171
[958]172/*! \ingroup TArray \fn operator+(T,const TArray<T>&)
173 \brief Operator TArray = constant + TArray */
[772]174template <class T> inline TArray<T> operator + (T b,const TArray<T>& a)
[804]175 {TArray<T> result(a); result.SetTemp(true); result.Add(b); return result;}
[772]176
[958]177/*! \ingroup TArray \fn operator-(const TArray<T>&,T)
178 \brief Operator TArray = TArray - constant */
[772]179template <class T> inline TArray<T> operator - (const TArray<T>& a, T b)
[804]180 {TArray<T> result(a); result.SetTemp(true); result.Sub(b); return result;}
[772]181
[958]182/*! \ingroup TArray \fn operator-(T,const TArray<T>&)
183 \brief Operator TArray = constant - TArray */
[772]184template <class T> inline TArray<T> operator - (T b,const TArray<T>& a)
[804]185 {TArray<T> result(a); result.SetTemp(true); result.SubInv(b); return result;}
[772]186
[958]187/*! \ingroup TArray \fn operator*(const TArray<T>&,T)
188 \brief Operator TArray = TArray * constant */
[772]189template <class T> inline TArray<T> operator * (const TArray<T>& a, T b)
[804]190 {TArray<T> result(a); result.SetTemp(true); result.Mul(b); return result;}
[772]191
[958]192/*! \ingroup TArray \fn operator*(T,const TArray<T>&)
193 \brief Operator TArray = constant * TArray */
[772]194template <class T> inline TArray<T> operator * (T b,const TArray<T>& a)
[804]195 {TArray<T> result(a); result.SetTemp(true); result.Mul(b); return result;}
[772]196
[958]197/*! \ingroup TArray \fn operator/(const TArray<T>&,T)
198 \brief Operator TArray = TArray / constant */
[772]199template <class T> inline TArray<T> operator / (const TArray<T>& a, T b)
[804]200 {TArray<T> result(a); result.SetTemp(true); result.DivInv(b); return result;}
[772]201
202////////////////////////////////////////////////////////////////
203// Surcharge d'operateurs C = A (+,-) B
204
[958]205/*! \ingroup TArray \fn operator+(const TArray<T>&,const TArray<T>&)
206 \brief Operator TArray = TArray + TArray */
[772]207template <class T>
208inline TArray<T> operator + (const TArray<T>& a,const TArray<T>& b)
[804]209 {TArray<T> result(a); result.SetTemp(true); result.AddElt(b); return result;}
[772]210
[958]211/*! \ingroup TArray \fn operator-(const TArray<T>&,const TArray<T>&)
212 \brief Operator TArray = TArray - TArray */
[772]213template <class T>
214inline TArray<T> operator - (const TArray<T>& a,const TArray<T>& b)
[804]215 {TArray<T> result(a); result.SetTemp(true); result.SubElt(b); return result;}
[772]216
217
218// --------------------------------------------------
219// inline element acces methods
220// --------------------------------------------------
[894]221
222//! Return element (ix,iy,iz,it,iu) value
[772]223template <class T>
224inline T const& TArray<T>::Elem(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu) const
225{
226 return ( *( mNDBlock.Begin()+ offset_+
227 ix*step_[0] + iy*step_[1] + iz*step_[2] +
228 it*step_[3] + iu*step_[4]) );
229}
230
[894]231//! Return element (ix,iy,iz,it,iu) value
[772]232template <class T>
233inline T & TArray<T>::Elem(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu)
234{
235 return ( *( mNDBlock.Begin()+ offset_+
236 ix*step_[0] + iy*step_[1] + iz*step_[2] +
237 it*step_[3] + iu*step_[4]) );
238}
239
[894]240//! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
[772]241template <class T>
242inline T const& TArray<T>::ElemCheckBound(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu) const
243{
244 CheckBound(ix, iy, iz, it, iu, 4);
[804]245 return(Elem(ix, iy, iz, it, iu));
[772]246}
247
[894]248//! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
[772]249template <class T>
250inline T & TArray<T>::ElemCheckBound(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu)
251{
252 CheckBound(ix, iy, iz, it, iu, 4);
[804]253 return(Elem(ix, iy, iz, it, iu));
[772]254}
255
[894]256//! Return element (ix,iy,iz) value
[772]257template <class T>
258inline T const& TArray<T>::operator()(uint_4 ix, uint_4 iy, uint_4 iz) const
259{
260#ifdef SO_BOUNDCHECKING
261 CheckBound(ix, iy, iz, 0, 0, 4);
262#endif
263 return ( *( mNDBlock.Begin()+ offset_+
264 ix*step_[0] + iy*step_[1] + iz*step_[2]) );
265}
266
[894]267//! Return element (ix,iy,iz) value
[772]268template <class T>
269inline T & TArray<T>::operator()(uint_4 ix, uint_4 iy, uint_4 iz)
270{
271#ifdef SO_BOUNDCHECKING
272 CheckBound(ix, iy, iz, 0, 0, 4);
273#endif
274 return ( *( mNDBlock.Begin()+ offset_+
275 ix*step_[0] + iy*step_[1] + iz*step_[2]) );
276}
277
[894]278//! Operator () : return element (ix,iy,iz,it,iu) value
[772]279template <class T>
280inline T const& TArray<T>::operator()(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu) const
281{
282#ifdef SO_BOUNDCHECKING
283 CheckBound(ix, iy, iz, it, iu, 4);
284#endif
285 return ( *( mNDBlock.Begin()+ offset_+
286 ix*step_[0] + iy*step_[1] + iz*step_[2] +
287 it*step_[3] + iu*step_[4]) );
288}
289
[894]290//! Operator () : return element (ix,iy,iz,it,iu) value
[772]291template <class T>
292inline T & TArray<T>::operator()(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu)
293{
294#ifdef SO_BOUNDCHECKING
295 CheckBound(ix, iy, iz, it, iu, 4);
296#endif
297 return ( *( mNDBlock.Begin()+ offset_+
298 ix*step_[0] + iy*step_[1] + iz*step_[2] +
299 it*step_[3] + iu*step_[4]) );
300}
301
[785]302
[894]303//! Operator [] : return element at positon ip
[772]304template <class T>
[785]305inline T const& TArray<T>::operator[](uint_8 ip) const
[772]306{
307#ifdef SO_BOUNDCHECKING
308 if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
309#endif
[785]310return *(mNDBlock.Begin()+Offset(ip));
[772]311}
312
[894]313//! Operator [] : return element at positon ip
[772]314template <class T>
[785]315inline T & TArray<T>::operator[](uint_8 ip)
[772]316{
317#ifdef SO_BOUNDCHECKING
318 if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
319#endif
[785]320return *(mNDBlock.Begin()+Offset(ip));
[772]321}
322
[785]323
[894]324//! Return the value of first element
[772]325template <class T>
[787]326inline T TArray<T>::toScalar()
[772]327{
328 if (Size() != 1) throw(SzMismatchError("TArray<T>::operator T() Size() != 1")) ;
329 return ( (*this)[0] );
330}
331
[804]332// Typedef pour simplifier
[956]333/*! \ingroup TArray
334 \typedef Array
335 \brief To simplified TArray<r_8> writing
336*/
[804]337typedef TArray<r_8> Array;
338
[772]339} // Fin du namespace
340
341#endif
Note: See TracBrowser for help on using the repository browser.