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

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

operator(Range) pour linux cmv 20/4/00

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