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

Last change on this file since 2286 was 2286, checked in by ansari, 23 years ago

Correction lecture tableaux en ASCII - Reza 5/12/02

File size: 15.5 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(int_4 ndim, const sa_size_t * siz, sa_size_t step =1);
32 TArray(sa_size_t nx, sa_size_t ny=0, sa_size_t nz=0, sa_size_t nt=0, sa_size_t nu=0);
33 TArray(int_4 ndim, const sa_size_t * siz, NDataBlock<T> & db, bool share=false, sa_size_t step=1, sa_size_t offset=0);
34 TArray(int_4 ndim, const sa_size_t * siz, T* values, sa_size_t step=1, sa_size_t 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 void Share(const TArray<T>& a);
58
59 void ReSize(int_4 ndim, sa_size_t * siz, sa_size_t step=1);
60 void ReSize(const BaseArray& a);
61 //! a synonym (alias) for method ReSize(int_4, ...)
62 inline void SetSize(int_4 ndim, sa_size_t * siz, sa_size_t step=1)
63 { ReSize(ndim, siz, step); }
64 //! a synonym (alias) for method ReSize(const BaseArray&)
65 inline void SetSize(const BaseArray& a)
66 { ReSize(a); }
67 void Realloc(int_4 ndim, sa_size_t * siz, sa_size_t step=1, bool force=false);
68
69 // Compacts size=1 array dimensions
70 virtual TArray<T>& CompactAllDimensions();
71 virtual TArray<T>& CompactTrailingDimensions();
72
73 // Packing array elements in memory
74 virtual TArray<T> PackElements(bool force=false) const ;
75
76 // SubArrays - $CHECK$ Reza 03/2000 je ne sais pas s'il faut declarer ca const ??
77 TArray<T> SubArray(Range rx, Range ry, Range rz, Range rt, Range ru) const ;
78
79 //! () operator for Sub arrays extraction
80 /*! \sa SubArray */
81 inline TArray<T> operator () (Range rx, Range ry, Range rz) const
82 { return SubArray(rx, ry, rz, Range(0), Range(0)); }
83 inline TArray<T> operator () (Range rx, Range ry, Range rz, Range rt) const
84 { return SubArray(rx, ry, rz, rt, Range(0)); }
85 inline TArray<T> operator () (Range rx, Range ry, Range rz, Range rt, Range ru) const
86 { return SubArray(rx, ry, rz, rt, ru); }
87
88 // ---- Access to data
89 // Definition of virtual element acces method inherited from BaseArray class
90 virtual MuTyV & ValueAtPosition(sa_size_t ip) const;
91
92 // Data Access: operator overloaded inline acces methods
93 inline T const& operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz) const ;
94 inline T& operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz);
95 inline T const& operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu=0) const ;
96 inline T& operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu=0);
97 inline T const& operator[](sa_size_t ip) const ;
98 inline T& operator[](sa_size_t ip);
99
100 inline T const& Elem(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it=0, sa_size_t iu=0) const ;
101 inline T& Elem(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it=0, sa_size_t iu=0);
102 inline T const& ElemCheckBound(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it=0, sa_size_t iu=0) const ;
103 inline T& ElemCheckBound(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it=0, sa_size_t 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// applique le signe moins a tous les elements
146 virtual TArray<T>& NegateElt();
147
148// A += -= (ajoute, soustrait element par element les deux tableaux )
149 virtual TArray<T>& AddElt(const TArray<T>& a);
150 //! Operator TArray += TArray
151 inline TArray<T>& operator += (const TArray<T>& a) { return AddElt(a); }
152 virtual TArray<T>& SubElt(const TArray<T>& a, bool fginv=false);
153 //! Operator TArray -= TArray
154 inline TArray<T>& operator -= (const TArray<T>& a) { return SubElt(a); }
155// Multiplication, division element par element les deux tableaux
156 virtual TArray<T>& MulElt(const TArray<T>& a);
157 virtual TArray<T>& DivElt(const TArray<T>& a, bool fginv=false, bool divzero=false);
158// Recopie des valeurs, element par element
159 virtual TArray<T>& CopyElt(const TArray<T>& a);
160// Recopie des valeurs avec conversion prealable, element par element
161 virtual TArray<T>& ConvertAndCopyElt(const BaseArray& a);
162
163// Somme et produit des elements
164 virtual T Sum() const ;
165 virtual T Product() const ;
166// Somme du carre des elements
167 virtual T SumX2() const;
168// Valeur min et max des elements
169 virtual void MinMax(T& min, T& max) const ;
170
171// Impression, I/O, ...
172 virtual string InfoString() const;
173 virtual void Print(ostream& os, sa_size_t maxprt=-1,
174 bool si=false, bool ascd=false) const ;
175
176// Lecture,Ecriture sur fichier ASCII
177 virtual sa_size_t ReadASCII(istream& is, sa_size_t & nr, sa_size_t & nc,
178 char clm='#', const char* sep=" \t");
179 virtual void WriteASCII(ostream& os) const;
180
181// Pour la gestion de persistance
182 friend class FIO_TArray<T>;
183
184protected:
185
186 NDataBlock<T> mNDBlock; //!< Block for datas
187 mutable MuTyV my_mtv; //!< for use by ValueAtPosition()
188};
189
190////////////////////////////////////////////////////////////////
191// Surcharge d'operateur <<
192//! Print TArray \b a on stream \b os
193template <class T>
194inline ostream& operator << (ostream& os, const TArray<T>& a)
195 { a.Print(os); return(os); }
196
197// Surcharge d'operateur >>
198//! Decodes the ASCII input stream \b is , filling TArray \b a elements
199template <class T>
200inline istream& operator >> (istream& is, TArray<T>& a)
201 { sa_size_t nr, nc;
202 a.ReadASCII(is, nr, nc); return(is); }
203
204
205////////////////////////////////////////////////////////////////
206// Surcharge d'operateurs A (+,-,*,/) (T) x
207
208/*! \ingroup TArray \fn operator+(const TArray<T>&,T)
209 \brief Operator TArray = TArray + constant */
210template <class T> inline TArray<T> operator + (const TArray<T>& a, T b)
211 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
212 result.Add(b); return result;}
213
214/*! \ingroup TArray \fn operator+(T,const TArray<T>&)
215 \brief Operator TArray = constant + TArray */
216template <class T> inline TArray<T> operator + (T b,const TArray<T>& a)
217 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
218 result.Add(b); return result;}
219
220/*! \ingroup TArray \fn operator-(const TArray<T>&,T)
221 \brief Operator TArray = TArray - constant */
222template <class T> inline TArray<T> operator - (const TArray<T>& a, T b)
223 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
224 result.Sub(b); return result;}
225
226/*! \ingroup TArray \fn operator-(T,const TArray<T>&)
227 \brief Operator TArray = constant - TArray */
228template <class T> inline TArray<T> operator - (T b,const TArray<T>& a)
229 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
230 result.Sub(b,true); return result;}
231
232/*! \ingroup TArray \fn operator*(const TArray<T>&,T)
233 \brief Operator TArray = TArray * constant */
234template <class T> inline TArray<T> operator * (const TArray<T>& a, T b)
235 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
236 result.Mul(b); return result;}
237
238/*! \ingroup TArray \fn operator*(T,const TArray<T>&)
239 \brief Operator TArray = constant * TArray */
240template <class T> inline TArray<T> operator * (T b,const TArray<T>& a)
241 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
242 result.Mul(b); return result;}
243
244/*! \ingroup TArray \fn operator/(const TArray<T>&,T)
245 \brief Operator TArray = TArray / constant */
246template <class T> inline TArray<T> operator / (const TArray<T>& a, T b)
247 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
248 result.Div(b); return result;}
249
250/*! \ingroup TArray \fn operator/(T,const TArray<T>&)
251 \brief Operator TArray = constant / TArray */
252template <class T> inline TArray<T> operator / (T b, const TArray<T>& a)
253 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
254 result.Div(b, true); return result;}
255
256////////////////////////////////////////////////////////////////
257// Surcharge d'operateurs B = -A
258
259/*! \ingroup TArray \fn operator - (const TArray<T>&)
260 \brief Operator - Returns an array with elements equal to the opposite of
261 the original array elements. */
262template <class T> inline TArray<T> operator - (const TArray<T>& a)
263 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
264 result.NegateElt(); return result;}
265
266////////////////////////////////////////////////////////////////
267// Surcharge d'operateurs C = A (+,-) B
268
269/*! \ingroup TArray \fn operator+(const TArray<T>&,const TArray<T>&)
270 \brief Operator TArray = TArray + TArray */
271template <class T>
272inline TArray<T> operator + (const TArray<T>& a,const TArray<T>& b)
273 { TArray<T> result; result.SetTemp(true);
274 if (b.IsTemp()) { result.Share(b); result.AddElt(a); }
275 else { result.CloneOrShare(a); result.AddElt(b); }
276 return result; }
277
278/*! \ingroup TArray \fn operator-(const TArray<T>&,const TArray<T>&)
279 \brief Operator TArray = TArray - TArray */
280template <class T>
281inline TArray<T> operator - (const TArray<T>& a,const TArray<T>& b)
282 { TArray<T> result; result.SetTemp(true);
283 if (b.IsTemp()) { result.Share(b); result.SubElt(a, true); }
284 else { result.CloneOrShare(a); result.SubElt(b); }
285 return result; }
286
287
288// --------------------------------------------------
289// inline element acces methods
290// --------------------------------------------------
291
292//! Return element (ix,iy,iz,it,iu) value
293template <class T>
294inline T const& TArray<T>::Elem(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu) const
295{
296 return ( *( mNDBlock.Begin()+ offset_+
297 ix*step_[0] + iy*step_[1] + iz*step_[2] +
298 it*step_[3] + iu*step_[4]) );
299}
300
301//! Return element (ix,iy,iz,it,iu) value
302template <class T>
303inline T & TArray<T>::Elem(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu)
304{
305 return ( *( mNDBlock.Begin()+ offset_+
306 ix*step_[0] + iy*step_[1] + iz*step_[2] +
307 it*step_[3] + iu*step_[4]) );
308}
309
310//! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
311template <class T>
312inline T const& TArray<T>::ElemCheckBound(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu) const
313{
314 CheckBound(ix, iy, iz, it, iu, 4);
315 return(Elem(ix, iy, iz, it, iu));
316}
317
318//! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
319template <class T>
320inline T & TArray<T>::ElemCheckBound(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu)
321{
322 CheckBound(ix, iy, iz, it, iu, 4);
323 return(Elem(ix, iy, iz, it, iu));
324}
325
326//! Return element (ix,iy,iz) value
327template <class T>
328inline T const& TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz) const
329{
330#ifdef SO_BOUNDCHECKING
331 CheckBound(ix, iy, iz, 0, 0, 4);
332#endif
333 return ( *( mNDBlock.Begin()+ offset_+
334 ix*step_[0] + iy*step_[1] + iz*step_[2]) );
335}
336
337//! Return element (ix,iy,iz) value
338template <class T>
339inline T & TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz)
340{
341#ifdef SO_BOUNDCHECKING
342 CheckBound(ix, iy, iz, 0, 0, 4);
343#endif
344 return ( *( mNDBlock.Begin()+ offset_+
345 ix*step_[0] + iy*step_[1] + iz*step_[2]) );
346}
347
348//! Operator () : return element (ix,iy,iz,it,iu) value
349template <class T>
350inline T const& TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu) const
351{
352#ifdef SO_BOUNDCHECKING
353 CheckBound(ix, iy, iz, it, iu, 4);
354#endif
355 return ( *( mNDBlock.Begin()+ offset_+
356 ix*step_[0] + iy*step_[1] + iz*step_[2] +
357 it*step_[3] + iu*step_[4]) );
358}
359
360//! Operator () : return element (ix,iy,iz,it,iu) value
361template <class T>
362inline T & TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu)
363{
364#ifdef SO_BOUNDCHECKING
365 CheckBound(ix, iy, iz, it, iu, 4);
366#endif
367 return ( *( mNDBlock.Begin()+ offset_+
368 ix*step_[0] + iy*step_[1] + iz*step_[2] +
369 it*step_[3] + iu*step_[4]) );
370}
371
372
373//! Operator [] : return element at positon ip
374template <class T>
375inline T const& TArray<T>::operator[](sa_size_t ip) const
376{
377#ifdef SO_BOUNDCHECKING
378 if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
379#endif
380return *(mNDBlock.Begin()+Offset(ip));
381}
382
383//! Operator [] : return element at positon ip
384template <class T>
385inline T & TArray<T>::operator[](sa_size_t ip)
386{
387#ifdef SO_BOUNDCHECKING
388 if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
389#endif
390return *(mNDBlock.Begin()+Offset(ip));
391}
392
393
394//! Converts to a scalar (value of first element) if the array size is equal to 1
395template <class T>
396inline T TArray<T>::toScalar()
397{
398 if (Size() != 1) throw(SzMismatchError("TArray<T>::operator T() Size() != 1")) ;
399 return ( (*this)[0] );
400}
401
402// Typedef pour simplifier
403/*! \ingroup TArray
404 \typedef Array
405 \brief To simplified TArray<r_8> writing
406*/
407typedef TArray<r_8> Array;
408
409} // Fin du namespace
410
411#endif
Note: See TracBrowser for help on using the repository browser.