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

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

Ajout Methodes TArray::MinMax() et TArray::SumX2() - Reza 28/7/2000

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