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

Last change on this file since 2564 was 2564, checked in by ansari, 21 years ago

Remplacement methodes Add/Mul/Sub/Div(T x) par AddCst/MulCst/SubCst/DivCst(T x, TArray<T> res) ds TArray - Reza 26 Juillet 2004

File size: 16.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>
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, bool fzero=true);
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, bool fzero=true);
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, bool fzero=true);
60 void ReSize(const BaseArray& a, bool pack=true, bool fzero=true);
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, bool fzero=true)
63 { ReSize(ndim, siz, step, fzero); }
64 //! a synonym (alias) for method ReSize(const BaseArray&)
65 inline void SetSize(const BaseArray& a, bool pack=true, bool fzero=true)
66 { ReSize(a, pack, fzero); }
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
132// addition et soustraction de constante
133 virtual TArray<T>& AddCst(T x, TArray<T>& res) const ;
134 virtual TArray<T>& SubCst(T x, TArray<T>& res, bool fginv=false) const ;
135// Multiplication et division par une constante
136 virtual TArray<T>& MulCst(T x, TArray<T>& res) const ;
137 virtual TArray<T>& DivCst(T x, TArray<T>& res, bool fginv=false) const ;
138
139// A += -= *= /= x (ajoute, soustrait, ... x a tous les elements)
140 inline TArray<T>& Add(T x) { return AddCst(x, *this); }
141 inline TArray<T>& Sub(T x, bool fginv=false) { return SubCst(x, *this, fginv); }
142 inline TArray<T>& Mul(T x) { return MulCst(x, *this); }
143 inline TArray<T>& Div(T x, bool fginv=false) { return DivCst(x, *this, fginv); }
144
145 //! Add \b x to all elements
146 inline TArray<T>& operator += (T x) { return AddCst(x, *this); }
147 //! Substract \b x to all elements
148 inline TArray<T>& operator -= (T x) { return SubCst(x, *this); }
149 //! Multiply all elements by \b x
150 inline TArray<T>& operator *= (T x) { return MulCst(x, *this); }
151 //! Divide all elements by \b x
152 inline TArray<T>& operator /= (T x) { return DivCst(x, *this); }
153
154// applique le signe moins a tous les elements
155 virtual TArray<T>& NegateElt(TArray<T>& res) const ;
156//! Replace array elements values by their opposite ( (*this)(i) -> -(*this)(i) )
157 inline TArray<T>& NegateElt() { return NegateElt(*this); }
158
159// A += -= (ajoute, soustrait element par element les deux tableaux )
160 virtual TArray<T>& AddElt(const TArray<T>& a);
161 //! Operator TArray += TArray
162 inline TArray<T>& operator += (const TArray<T>& a) { return AddElt(a); }
163 virtual TArray<T>& SubElt(const TArray<T>& a, bool fginv=false);
164 //! Operator TArray -= TArray
165 inline TArray<T>& operator -= (const TArray<T>& a) { return SubElt(a); }
166// Multiplication, division element par element les deux tableaux
167 virtual TArray<T>& MulElt(const TArray<T>& a);
168 virtual TArray<T>& DivElt(const TArray<T>& a, bool fginv=false, bool divzero=false);
169// Recopie des valeurs, element par element
170 virtual TArray<T>& CopyElt(const TArray<T>& a);
171// Recopie des valeurs avec conversion prealable, element par element
172 virtual TArray<T>& ConvertAndCopyElt(const BaseArray& a);
173
174// Somme et produit des elements
175 virtual T Sum() const ;
176 virtual T Product() const ;
177// Somme du carre des elements
178 virtual T SumX2() const;
179// Valeur min et max des elements
180 virtual void MinMax(T& min, T& max) const ;
181
182// Impression, I/O, ...
183 virtual string InfoString() const;
184 virtual void Print(ostream& os, sa_size_t maxprt=-1,
185 bool si=false, bool ascd=false) const ;
186
187// Lecture,Ecriture sur fichier ASCII
188 virtual sa_size_t ReadASCII(istream& is, sa_size_t & nr, sa_size_t & nc,
189 char clm='#', const char* sep=" \t");
190 virtual void WriteASCII(ostream& os) const;
191
192// Pour la gestion de persistance
193 friend class FIO_TArray<T>;
194
195protected:
196
197 NDataBlock<T> mNDBlock; //!< Block for datas
198 mutable MuTyV my_mtv; //!< for use by ValueAtPosition()
199};
200
201////////////////////////////////////////////////////////////////
202// Surcharge d'operateur <<
203//! Print TArray \b a on stream \b os
204template <class T>
205inline ostream& operator << (ostream& os, const TArray<T>& a)
206 { a.Print(os); return(os); }
207
208// Surcharge d'operateur >>
209//! Decodes the ASCII input stream \b is , filling TArray \b a elements
210template <class T>
211inline istream& operator >> (istream& is, TArray<T>& a)
212 { sa_size_t nr, nc;
213 a.ReadASCII(is, nr, nc); return(is); }
214
215
216////////////////////////////////////////////////////////////////
217// Surcharge d'operateurs A (+,-,*,/) (T) x
218
219/*! \ingroup TArray \fn operator+(const TArray<T>&,T)
220 \brief Operator TArray = TArray + constant */
221template <class T> inline TArray<T> operator + (const TArray<T>& a, T b)
222 {TArray<T> result; result.SetTemp(true);
223 a.Add(b, result); return result;}
224
225/*! \ingroup TArray \fn operator+(T,const TArray<T>&)
226 \brief Operator TArray = constant + TArray */
227template <class T> inline TArray<T> operator + (T b,const TArray<T>& a)
228 {TArray<T> result; result.SetTemp(true);
229 a.Add(b, result); return result;}
230
231/*! \ingroup TArray \fn operator-(const TArray<T>&,T)
232 \brief Operator TArray = TArray - constant */
233template <class T> inline TArray<T> operator - (const TArray<T>& a, T b)
234 {TArray<T> result; result.SetTemp(true);
235 a.Sub(b,result); return result;}
236
237/*! \ingroup TArray \fn operator-(T,const TArray<T>&)
238 \brief Operator TArray = constant - TArray */
239template <class T> inline TArray<T> operator - (T b,const TArray<T>& a)
240 {TArray<T> result; result.SetTemp(true);
241 a.Sub(b,result,true); return result;}
242
243/*! \ingroup TArray \fn operator*(const TArray<T>&,T)
244 \brief Operator TArray = TArray * constant */
245template <class T> inline TArray<T> operator * (const TArray<T>& a, T b)
246 {TArray<T> result; result.SetTemp(true);
247 a.MulCst(b, result); return result;}
248
249/*! \ingroup TArray \fn operator*(T,const TArray<T>&)
250 \brief Operator TArray = constant * TArray */
251template <class T> inline TArray<T> operator * (T b,const TArray<T>& a)
252 {TArray<T> result; result.SetTemp(true);
253 a.MulCst(b,result); return result;}
254
255/*! \ingroup TArray \fn operator/(const TArray<T>&,T)
256 \brief Operator TArray = TArray / constant */
257template <class T> inline TArray<T> operator / (const TArray<T>& a, T b)
258 {TArray<T> result; result.SetTemp(true);
259 a.Div(b,result); return result;}
260
261/*! \ingroup TArray \fn operator/(T,const TArray<T>&)
262 \brief Operator TArray = constant / TArray */
263template <class T> inline TArray<T> operator / (T b, const TArray<T>& a)
264 {TArray<T> result; result.SetTemp(true);
265 a.Div(b, result, true); return result;}
266
267////////////////////////////////////////////////////////////////
268// Surcharge d'operateurs B = -A
269
270/*! \ingroup TArray \fn operator - (const TArray<T>&)
271 \brief Operator - Returns an array with elements equal to the opposite of
272 the original array elements. */
273template <class T> inline TArray<T> operator - (const TArray<T>& a)
274 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
275 result.NegateElt(); return result;}
276
277////////////////////////////////////////////////////////////////
278// Surcharge d'operateurs C = A (+,-) B
279
280/*! \ingroup TArray \fn operator+(const TArray<T>&,const TArray<T>&)
281 \brief Operator TArray = TArray + TArray */
282template <class T>
283inline TArray<T> operator + (const TArray<T>& a,const TArray<T>& b)
284 { TArray<T> result; result.SetTemp(true);
285 if (b.IsTemp()) { result.Share(b); result.AddElt(a); }
286 else { result.CloneOrShare(a); result.AddElt(b); }
287 return result; }
288
289/*! \ingroup TArray \fn operator-(const TArray<T>&,const TArray<T>&)
290 \brief Operator TArray = TArray - TArray */
291template <class T>
292inline TArray<T> operator - (const TArray<T>& a,const TArray<T>& b)
293 { TArray<T> result; result.SetTemp(true);
294 if (b.IsTemp()) { result.Share(b); result.SubElt(a, true); }
295 else { result.CloneOrShare(a); result.SubElt(b); }
296 return result; }
297
298
299// --------------------------------------------------
300// inline element acces methods
301// --------------------------------------------------
302
303//! Return element (ix,iy,iz,it,iu) value
304template <class T>
305inline 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
306{
307 return ( *( mNDBlock.Begin()+ offset_+
308 ix*step_[0] + iy*step_[1] + iz*step_[2] +
309 it*step_[3] + iu*step_[4]) );
310}
311
312//! Return element (ix,iy,iz,it,iu) value
313template <class T>
314inline T & TArray<T>::Elem(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu)
315{
316 return ( *( mNDBlock.Begin()+ offset_+
317 ix*step_[0] + iy*step_[1] + iz*step_[2] +
318 it*step_[3] + iu*step_[4]) );
319}
320
321//! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
322template <class T>
323inline 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
324{
325 CheckBound(ix, iy, iz, it, iu, 4);
326 return(Elem(ix, iy, iz, it, iu));
327}
328
329//! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
330template <class T>
331inline T & TArray<T>::ElemCheckBound(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu)
332{
333 CheckBound(ix, iy, iz, it, iu, 4);
334 return(Elem(ix, iy, iz, it, iu));
335}
336
337//! Return element (ix,iy,iz) value
338template <class T>
339inline T const& TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz) const
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//! Return element (ix,iy,iz) value
349template <class T>
350inline T & TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz)
351{
352#ifdef SO_BOUNDCHECKING
353 CheckBound(ix, iy, iz, 0, 0, 4);
354#endif
355 return ( *( mNDBlock.Begin()+ offset_+
356 ix*step_[0] + iy*step_[1] + iz*step_[2]) );
357}
358
359//! Operator () : return element (ix,iy,iz,it,iu) value
360template <class T>
361inline 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
362{
363#ifdef SO_BOUNDCHECKING
364 CheckBound(ix, iy, iz, it, iu, 4);
365#endif
366 return ( *( mNDBlock.Begin()+ offset_+
367 ix*step_[0] + iy*step_[1] + iz*step_[2] +
368 it*step_[3] + iu*step_[4]) );
369}
370
371//! Operator () : return element (ix,iy,iz,it,iu) value
372template <class T>
373inline T & TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu)
374{
375#ifdef SO_BOUNDCHECKING
376 CheckBound(ix, iy, iz, it, iu, 4);
377#endif
378 return ( *( mNDBlock.Begin()+ offset_+
379 ix*step_[0] + iy*step_[1] + iz*step_[2] +
380 it*step_[3] + iu*step_[4]) );
381}
382
383
384//! Operator [] : return element at positon ip
385template <class T>
386inline T const& TArray<T>::operator[](sa_size_t ip) const
387{
388#ifdef SO_BOUNDCHECKING
389 if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
390#endif
391return *(mNDBlock.Begin()+Offset(ip));
392}
393
394//! Operator [] : return element at positon ip
395template <class T>
396inline T & TArray<T>::operator[](sa_size_t ip)
397{
398#ifdef SO_BOUNDCHECKING
399 if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
400#endif
401return *(mNDBlock.Begin()+Offset(ip));
402}
403
404
405//! Converts to a scalar (value of first element) if the array size is equal to 1
406template <class T>
407inline T TArray<T>::toScalar()
408{
409 if (Size() != 1) throw(SzMismatchError("TArray<T>::operator T() Size() != 1")) ;
410 return ( (*this)[0] );
411}
412
413// Typedef pour simplifier
414/*! \ingroup TArray
415 \typedef Array
416 \brief To simplified TArray<r_8> writing
417*/
418typedef TArray<r_8> Array;
419
420} // Fin du namespace
421
422#endif
Note: See TracBrowser for help on using the repository browser.