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

Last change on this file since 919 was 894, checked in by ansari, 25 years ago

documentation cmv 12/4/00

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