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

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

doc + vire warning cmv 18/04/00

File size: 12.3 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.h>
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(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);
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.
41 //! = operator between TArray
42 /*! \sa Set */
43 inline TArray<T>& operator = (const TArray<T>& a) { return Set(a); }
44 virtual TArray<T>& Set(const TArray<T>& a);
45
46 // Gestion taille/Remplissage
47 virtual void Clone(const TArray<T>& a);
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);
50
51 // Compacts size=1 array dimensions
52 virtual TArray<T>& CompactAllDimensions();
53 virtual TArray<T>& CompactTrailingDimensions();
54
55 // Packing array elements in memory
56 virtual TArray<T> PackElements(bool force=false) const ;
57
58 // SubArrays - $CHECK$ Reza 03/2000 je ne sais pas s'il faut declarer ca const ??
59 TArray<T> SubArray(Range rx, Range ry, Range rz, Range rt, Range ru) const ;
60 //! () operator for Sub arrays extraction
61 /*! \sa SubArray */
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); }
64
65 // ---- Access to data
66 // Definition of virtual element acces method inherited from BaseArray class
67 virtual double ValueAtPosition(uint_8 ip) const;
68
69 // Data Access: operator overloaded inline acces methods
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);
74 inline T const& operator[](uint_8 ip) const ;
75 inline T& operator[](uint_8 ip);
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
82 //! Return pointer to first element adress
83 inline T* Data() {return mNDBlock.Begin()+offset_;}
84 //! Return pointer to first element adress
85 inline const T* Data() const {return mNDBlock.Begin()+offset_;}
86 //! Return reference to datablock NDataBlock
87 inline NDataBlock<T>& DataBlock() {return mNDBlock;}
88 //! Return reference to datablock NDataBlock
89 inline const NDataBlock<T>& DataBlock() const {return mNDBlock;}
90
91 // Temporaire?
92 //! Are the array temporay ?
93 inline bool IsTemp(void) const {return mNDBlock.IsTemp();}
94 //! Set the array as temporay
95 inline void SetTemp(bool temp=false) const {mNDBlock.SetTemp(temp);}
96
97// Operations diverses = , +=, ...
98// Conversion en type T, if Size() == 1
99 inline T toScalar();
100// Met les elements a une suite de valeurs
101 virtual TArray<T>& SetSeq(Sequence seq);
102 //! Fill TArray with Sequence \b seq
103 inline TArray<T>& operator = (Sequence seq) { return SetSeq(seq); }
104// A = x (tous les elements a x)
105 virtual TArray<T>& SetT(T x);
106 //! Fill TArray with all elements equal to \b x
107 inline TArray<T>& operator = (T x) { return SetT(x); }
108// A += -= *= /= x (ajoute, soustrait, ... x a tous les elements)
109 virtual TArray<T>& Add(T x);
110 //! Add \b x to all elements
111 inline TArray<T>& operator += (T x) { return Add(x); }
112 virtual TArray<T>& Sub(T x);
113 //! Substract \b x to all elements
114 inline TArray<T>& operator -= (T x) { return Sub(x); }
115 virtual TArray<T>& Mul(T x);
116 //! Multiply all elements by \b x
117 inline TArray<T>& operator *= (T x) { return Mul(x); }
118 virtual TArray<T>& Div(T x);
119 //! Divide all elements by \b x
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
124// A += -= (ajoute, soustrait element par element les deux tableaux )
125 virtual TArray<T>& AddElt(const TArray<T>& a);
126 //! Operator TArray += TArray
127 inline TArray<T>& operator += (const TArray<T>& a) { return AddElt(a); }
128 virtual TArray<T>& SubElt(const TArray<T>& a);
129 //! Operator TArray -= TArray
130 inline TArray<T>& operator -= (const TArray<T>& a) { return SubElt(a); }
131// Multiplication, division element par element les deux tableaux
132 virtual TArray<T>& MulElt(const TArray<T>& a);
133 virtual TArray<T>& DivElt(const TArray<T>& a);
134// Recopie des valeurs, element par element
135 virtual TArray<T>& CopyElt(const TArray<T>& a);
136
137// Somme et produit des elements
138 virtual T Sum() const ;
139 virtual T Product() const ;
140
141// Impression, I/O, ...
142 virtual string InfoString() const;
143 virtual void Print(ostream& os, int_4 maxprt=-1, bool si=false) const ;
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
154 NDataBlock<T> mNDBlock; //!< Block for datas
155};
156
157////////////////////////////////////////////////////////////////
158// Surcharge d'operateur <<
159//! Print TArray \b a on stream \b os
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
167/*! \ingroup TArray \fn operator+(const TArray<T>&,T)
168 \brief Operator TArray = TArray + constant */
169template <class T> inline TArray<T> operator + (const TArray<T>& a, T b)
170 {TArray<T> result(a); result.SetTemp(true); result.Add(b); return result;}
171
172/*! \ingroup TArray \fn operator+(T,const TArray<T>&)
173 \brief Operator TArray = constant + TArray */
174template <class T> inline TArray<T> operator + (T b,const TArray<T>& a)
175 {TArray<T> result(a); result.SetTemp(true); result.Add(b); return result;}
176
177/*! \ingroup TArray \fn operator-(const TArray<T>&,T)
178 \brief Operator TArray = TArray - constant */
179template <class T> inline TArray<T> operator - (const TArray<T>& a, T b)
180 {TArray<T> result(a); result.SetTemp(true); result.Sub(b); return result;}
181
182/*! \ingroup TArray \fn operator-(T,const TArray<T>&)
183 \brief Operator TArray = constant - TArray */
184template <class T> inline TArray<T> operator - (T b,const TArray<T>& a)
185 {TArray<T> result(a); result.SetTemp(true); result.SubInv(b); return result;}
186
187/*! \ingroup TArray \fn operator*(const TArray<T>&,T)
188 \brief Operator TArray = TArray * constant */
189template <class T> inline TArray<T> operator * (const TArray<T>& a, T b)
190 {TArray<T> result(a); result.SetTemp(true); result.Mul(b); return result;}
191
192/*! \ingroup TArray \fn operator*(T,const TArray<T>&)
193 \brief Operator TArray = constant * TArray */
194template <class T> inline TArray<T> operator * (T b,const TArray<T>& a)
195 {TArray<T> result(a); result.SetTemp(true); result.Mul(b); return result;}
196
197/*! \ingroup TArray \fn operator/(const TArray<T>&,T)
198 \brief Operator TArray = TArray / constant */
199template <class T> inline TArray<T> operator / (const TArray<T>& a, T b)
200 {TArray<T> result(a); result.SetTemp(true); result.DivInv(b); return result;}
201
202////////////////////////////////////////////////////////////////
203// Surcharge d'operateurs C = A (+,-) B
204
205/*! \ingroup TArray \fn operator+(const TArray<T>&,const TArray<T>&)
206 \brief Operator TArray = TArray + TArray */
207template <class T>
208inline TArray<T> operator + (const TArray<T>& a,const TArray<T>& b)
209 {TArray<T> result(a); result.SetTemp(true); result.AddElt(b); return result;}
210
211/*! \ingroup TArray \fn operator-(const TArray<T>&,const TArray<T>&)
212 \brief Operator TArray = TArray - TArray */
213template <class T>
214inline TArray<T> operator - (const TArray<T>& a,const TArray<T>& b)
215 {TArray<T> result(a); result.SetTemp(true); result.SubElt(b); return result;}
216
217
218// --------------------------------------------------
219// inline element acces methods
220// --------------------------------------------------
221
222//! Return element (ix,iy,iz,it,iu) value
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
231//! Return element (ix,iy,iz,it,iu) value
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
240//! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
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);
245 return(Elem(ix, iy, iz, it, iu));
246}
247
248//! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
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);
253 return(Elem(ix, iy, iz, it, iu));
254}
255
256//! Return element (ix,iy,iz) value
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
267//! Return element (ix,iy,iz) value
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
278//! Operator () : return element (ix,iy,iz,it,iu) value
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
290//! Operator () : return element (ix,iy,iz,it,iu) value
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
302
303//! Operator [] : return element at positon ip
304template <class T>
305inline T const& TArray<T>::operator[](uint_8 ip) const
306{
307#ifdef SO_BOUNDCHECKING
308 if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
309#endif
310return *(mNDBlock.Begin()+Offset(ip));
311}
312
313//! Operator [] : return element at positon ip
314template <class T>
315inline T & TArray<T>::operator[](uint_8 ip)
316{
317#ifdef SO_BOUNDCHECKING
318 if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
319#endif
320return *(mNDBlock.Begin()+Offset(ip));
321}
322
323
324//! Return the value of first element
325template <class T>
326inline T TArray<T>::toScalar()
327{
328 if (Size() != 1) throw(SzMismatchError("TArray<T>::operator T() Size() != 1")) ;
329 return ( (*this)[0] );
330}
331
332// Typedef pour simplifier
333/*! \ingroup TArray
334 \typedef Array
335 \brief To simplified TArray<r_8> writing
336*/
337typedef TArray<r_8> Array;
338
339} // Fin du namespace
340
341#endif
Note: See TracBrowser for help on using the repository browser.