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

Last change on this file since 3846 was 3831, checked in by ansari, 15 years ago

Introduction et gestion du flag preprocesseur NEED_EXT_DECL_TEMP pour declaration extern des classes template avec instantiation explicite (pb dynamic_cast sur Mac OS 10.6), Reza 05/08/2010

File size: 20.1 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=false, 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=false, 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 //! To clear the array sizes - corresponding to an unallocated array.
70 virtual TArray<T>& ZeroSize();
71
72 // Compacts size=1 array dimensions
73 virtual TArray<T>& CompactAllDimensions();
74 virtual TArray<T>& CompactTrailingDimensions();
75
76 // Packing array elements in memory
77 virtual TArray<T> PackElements(bool force=false) const ;
78
79 // SubArrays - $CHECK$ Reza 03/2000 je ne sais pas s'il faut declarer ca const ??
80 TArray<T> SubArray(Range rx, Range ry, Range rz, Range rt, Range ru, bool compact=true) const ;
81
82 //! Extract the first 3D subarray specified by rx, ry, rz. (see SubArray() )
83 inline TArray<T> operator () (Range rx, Range ry, Range rz) const
84 { return SubArray(rx, ry, rz, Range::first(), Range::first()); }
85 //! Extract the first 4D subarray specified by rx, ry, rz. (see SubArray() )
86 inline TArray<T> operator () (Range rx, Range ry, Range rz, Range rt) const
87 { return SubArray(rx, ry, rz, rt, Range::first()); }
88 //! Extract the subarray specified by rx, ry, rz. (see SubArray() )
89 inline TArray<T> operator () (Range rx, Range ry, Range rz, Range rt, Range ru) const
90 { return SubArray(rx, ry, rz, rt, ru); }
91
92 // ---- Access to data
93 // Definition of virtual element acces method inherited from BaseArray class
94 virtual MuTyV & ValueAtPosition(sa_size_t ip) const;
95 virtual MuTyV & ValueAtPositionDB(sa_size_t ip) const;
96
97 // Data Access: operator overloaded inline acces methods
98 inline T const& operator()(sa_size_t ix, sa_size_t iy=0) const ;
99 inline T& operator()(sa_size_t ix, sa_size_t iy=0);
100 inline T const& operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz) const ;
101 inline T& operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz);
102 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 ;
103 inline T& operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu=0);
104 inline T const& operator[](sa_size_t ip) const ;
105 inline T& operator[](sa_size_t ip);
106
107 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 ;
108 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);
109 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 ;
110 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);
111
112 //! Return pointer to first element adress
113 inline T* Data() {return mNDBlock.Begin()+offset_;}
114 //! Return pointer to first element adress
115 inline const T* Data() const {return mNDBlock.Begin()+offset_;}
116 //! Return reference to datablock NDataBlock
117 inline NDataBlock<T>& DataBlock() {return mNDBlock;}
118 //! Return reference to datablock NDataBlock
119 inline const NDataBlock<T>& DataBlock() const {return mNDBlock;}
120
121 // Temporaire?
122 //! Are the array temporay ?
123 inline bool IsTemp(void) const {return mNDBlock.IsTemp();}
124 //! Set the array as temporay
125 inline void SetTemp(bool temp=false) const {mNDBlock.SetTemp(temp);}
126
127// Operations diverses = , +=, ...
128// Conversion en type T, if Size() == 1
129 inline T toScalar();
130// Met les elements a une suite de valeurs
131 virtual TArray<T>& SetSeq(Sequence const & seq);
132 //! Fill TArray with Sequence \b seq
133 inline TArray<T>& operator = (Sequence const & seq) { return SetSeq(seq); }
134
135// A = x (tous les elements a x)
136 virtual TArray<T>& SetCst(T x);
137 //! Fill an array with a constant value \b x ( alias for \b SetCst() method )
138 inline TArray<T>& SetT(T x) { return SetCst(x); }
139 //! Fill TArray with all elements equal to \b x
140 inline TArray<T>& operator = (T x) { return SetT(x); }
141
142// addition et soustraction de constante
143 virtual TArray<T>& AddCst(T x, TArray<T>& res) const ;
144 virtual TArray<T>& SubCst(T x, TArray<T>& res, bool fginv=false) const ;
145// Multiplication et division par une constante
146 virtual TArray<T>& MulCst(T x, TArray<T>& res) const ;
147 virtual TArray<T>& DivCst(T x, TArray<T>& res, bool fginv=false) const ;
148
149// A += -= *= /= x (ajoute, soustrait, ... x a tous les elements)
150 // Methodes Add/Sub/Mul/Div() sont la pour compatibilite avant V=2 (1.818)
151 // Faut-il les garder ? Reza, Juillet 2004
152 inline TArray<T>& Add(T x) { return AddCst(x, *this); }
153 inline TArray<T>& Sub(T x, bool fginv=false) { return SubCst(x, *this, fginv); }
154 inline TArray<T>& Mul(T x) { return MulCst(x, *this); }
155 inline TArray<T>& Div(T x, bool fginv=false) { return DivCst(x, *this, fginv); }
156
157 //! Add \b x to all elements
158 inline TArray<T>& operator += (T x) { return AddCst(x, *this); }
159 //! Substract \b x to all elements
160 inline TArray<T>& operator -= (T x) { return SubCst(x, *this); }
161 //! Multiply all elements by \b x
162 inline TArray<T>& operator *= (T x) { return MulCst(x, *this); }
163 //! Divide all elements by \b x
164 inline TArray<T>& operator /= (T x) { return DivCst(x, *this); }
165
166// applique le signe moins a tous les elements
167 virtual TArray<T>& NegateElt(TArray<T>& res) const ;
168//! Replace array elements values by their opposite ( (*this)(i) -> -(*this)(i) )
169 inline TArray<T>& NegateElt() { return NegateElt(*this); }
170
171// A += -= (ajoute, soustrait element par element les deux tableaux )
172 virtual TArray<T>& AddElt(const TArray<T>& a, TArray<T>& res) const ;
173 virtual TArray<T>& SubElt(const TArray<T>& a, TArray<T>& res, bool fginv=false) const ;
174// Multiplication, division element par element les deux tableaux
175 virtual TArray<T>& MulElt(const TArray<T>& a, TArray<T>& res) const ;
176 virtual TArray<T>& DivElt(const TArray<T>& a, TArray<T>& res, bool fginv=false, bool divzero=false) const ;
177
178 //! Operator TArray += TArray (element by element addition in place)
179 inline TArray<T>& operator += (const TArray<T>& a) { return AddElt(a, *this); }
180 //! Operator TArray -= TArray (element by element subtraction in place)
181 inline TArray<T>& operator -= (const TArray<T>& a) { return SubElt(a, *this); }
182
183 // Doit-on definir les operateur *= /= TArray ? Reza, Juillet 2004
184 //! Element by element multiplication in place TArray *= TArray (element by element)
185 inline TArray<T>& Mul(const TArray<T>& a) { return MulElt(a, *this); }
186 //! Element by element division in place TArray *= TArray (element by element)
187 inline TArray<T>& Div(const TArray<T>& a, bool divzero=false)
188 { return DivElt(a, *this, false, divzero); }
189
190// Recopie des valeurs, element par element
191 virtual TArray<T>& CopyElt(const TArray<T>& a);
192// Recopie des valeurs avec conversion prealable, element par element
193 virtual TArray<T>& ConvertAndCopyElt(const BaseArray& a);
194
195// Calcul du produit scalaire ( Somme_i (*this)(i)*a(i) )
196 virtual T ScalarProduct(const TArray<T>& a) const ;
197
198// Somme et produit des elements
199 virtual T Sum() const ;
200 virtual T Product() const ;
201// Somme du carre des elements
202 virtual T SumSq() const;
203// Norme^2 , identique a SumSq pour tableaux reels/integer , sum[el * conj(el)] pour complexe
204 virtual T Norm2() const;
205// Valeur min et max des elements (sauf tableaux complexes -> exception)
206 virtual void MinMax(T& min, T& max) const ;
207
208// Impression, I/O, ...
209 virtual string InfoString() const;
210 virtual void Print(ostream& os, sa_size_t maxprt=-1,
211 bool si=false, bool ascd=false) const ;
212
213// Lecture,Ecriture sur fichier ASCII
214 virtual sa_size_t ReadASCII(istream& is, sa_size_t & nr, sa_size_t & nc,
215 char clm='#', const char* sep=" \t");
216 virtual void WriteASCII(ostream& os) const;
217
218//! assign a new object Id (or DataRef Id) - useful for PPF write operations
219 inline void RenewObjId() { mNDBlock.RenewObjId(); }
220// Pour la gestion de persistance
221 friend class FIO_TArray<T>;
222
223protected:
224
225 NDataBlock<T> mNDBlock; //!< Block for datas
226 mutable MuTyV my_mtv; //!< for use by ValueAtPosition()
227};
228
229////////////////////////////////////////////////////////////////
230// Surcharge d'operateur <<
231//! Print TArray \b a on stream \b os
232template <class T>
233inline ostream& operator << (ostream& os, const TArray<T>& a)
234 { a.Print(os); return(os); }
235
236// Surcharge d'operateur >>
237//! Decodes the ASCII input stream \b is , filling TArray \b a elements
238template <class T>
239inline istream& operator >> (istream& is, TArray<T>& a)
240 { sa_size_t nr, nc;
241 a.ReadASCII(is, nr, nc); return(is); }
242
243
244////////////////////////////////////////////////////////////////
245// Surcharge d'operateurs A (+,-,*,/) (T) x
246
247/*! \ingroup TArray \fn operator+(const TArray<T>&,T)
248 \brief Operator TArray = TArray + constant */
249template <class T> inline TArray<T> operator + (const TArray<T>& a, T b)
250 {TArray<T> result; result.SetTemp(true);
251 a.AddCst(b, result); return result;}
252
253/*! \ingroup TArray \fn operator+(T,const TArray<T>&)
254 \brief Operator TArray = constant + TArray */
255template <class T> inline TArray<T> operator + (T b,const TArray<T>& a)
256 {TArray<T> result; result.SetTemp(true);
257 a.AddCst(b, result); return result;}
258
259/*! \ingroup TArray \fn operator-(const TArray<T>&,T)
260 \brief Operator TArray = TArray - constant */
261template <class T> inline TArray<T> operator - (const TArray<T>& a, T b)
262 {TArray<T> result; result.SetTemp(true);
263 a.SubCst(b,result); return result;}
264
265/*! \ingroup TArray \fn operator-(T,const TArray<T>&)
266 \brief Operator TArray = constant - TArray */
267template <class T> inline TArray<T> operator - (T b,const TArray<T>& a)
268 {TArray<T> result; result.SetTemp(true);
269 a.SubCst(b,result,true); return result;}
270
271/*! \ingroup TArray \fn operator*(const TArray<T>&,T)
272 \brief Operator TArray = TArray * constant */
273template <class T> inline TArray<T> operator * (const TArray<T>& a, T b)
274 {TArray<T> result; result.SetTemp(true);
275 a.MulCst(b, result); return result;}
276
277/*! \ingroup TArray \fn operator*(T,const TArray<T>&)
278 \brief Operator TArray = constant * TArray */
279template <class T> inline TArray<T> operator * (T b,const TArray<T>& a)
280 {TArray<T> result; result.SetTemp(true);
281 a.MulCst(b,result); return result;}
282
283/*! \ingroup TArray \fn operator/(const TArray<T>&,T)
284 \brief Operator TArray = TArray / constant */
285template <class T> inline TArray<T> operator / (const TArray<T>& a, T b)
286 {TArray<T> result; result.SetTemp(true);
287 a.DivCst(b,result); return result;}
288
289/*! \ingroup TArray \fn operator/(T,const TArray<T>&)
290 \brief Operator TArray = constant / TArray */
291template <class T> inline TArray<T> operator / (T b, const TArray<T>& a)
292 {TArray<T> result; result.SetTemp(true);
293 a.DivCst(b, result, true); return result;}
294
295////////////////////////////////////////////////////////////////
296// Surcharge d'operateurs B = -A
297
298/*! \ingroup TArray \fn operator - (const TArray<T>&)
299 \brief Operator - Returns an array with elements equal to the opposite of
300 the original array elements. */
301template <class T> inline TArray<T> operator - (const TArray<T>& a)
302 {TArray<T> result; result.SetTemp(true);
303 a.NegateElt(result); return result;}
304
305////////////////////////////////////////////////////////////////
306// Surcharge d'operateurs C = A (+,-,&&,/) B
307
308/*! \ingroup TArray \fn operator+(const TArray<T>&,const TArray<T>&)
309 \brief Operator TArray = TArray + TArray (element by element addition) */
310template <class T>
311inline TArray<T> operator + (const TArray<T>& a,const TArray<T>& b)
312 { TArray<T> result; result.SetTemp(true);
313 a.AddElt(b, result); return result; }
314
315/*! \ingroup TArray \fn operator-(const TArray<T>&,const TArray<T>&)
316 \brief Operator TArray = TArray - TArray (element by element subtraction) */
317template <class T>
318inline TArray<T> operator - (const TArray<T>& a,const TArray<T>& b)
319 { TArray<T> result; result.SetTemp(true);
320 a.SubElt(b, result); return result; }
321
322/*! \ingroup TArray \fn operator && (const TArray<T>&,const TArray<T>&)
323 \brief Element by element multiplication of two arrays TArray = TArray * TArray */
324
325template <class T>
326inline TArray<T> operator && (const TArray<T>& a,const TArray<T>& b)
327 { TArray<T> result; result.SetTemp(true);
328 a.MulElt(b, result); return result; }
329
330/*! \ingroup TArray \fn operator / (const TArray<T>&,const TArray<T>&)
331 \brief Element by element division of two arrays TArray = TArray / TArray */
332template <class T>
333inline TArray<T> operator / (const TArray<T>& a,const TArray<T>& b)
334 { TArray<T> result; result.SetTemp(true);
335 a.DivElt(b, result, false, false); return result; }
336
337// --------------------------------------------------
338// inline element acces methods
339// --------------------------------------------------
340
341//! Return element (ix,iy,iz,it,iu) value
342template <class T>
343inline 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
344{
345 return ( *( mNDBlock.Begin()+ offset_+
346 ix*step_[0] + iy*step_[1] + iz*step_[2] +
347 it*step_[3] + iu*step_[4]) );
348}
349
350//! Return element (ix,iy,iz,it,iu) value
351template <class T>
352inline T & TArray<T>::Elem(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu)
353{
354 return ( *( mNDBlock.Begin()+ offset_+
355 ix*step_[0] + iy*step_[1] + iz*step_[2] +
356 it*step_[3] + iu*step_[4]) );
357}
358
359//! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
360template <class T>
361inline 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
362{
363 CheckBound(ix, iy, iz, it, iu, 4);
364 return(Elem(ix, iy, iz, it, iu));
365}
366
367//! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
368template <class T>
369inline T & TArray<T>::ElemCheckBound(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu)
370{
371 CheckBound(ix, iy, iz, it, iu, 4);
372 return(Elem(ix, iy, iz, it, iu));
373}
374
375//=======> a(ix,iy) OR a(ix) accees operators
376//! Return the value of the element (ix,iy)
377template <class T>
378inline T const& TArray<T>::operator()(sa_size_t ix, sa_size_t iy) const
379{
380#ifdef SO_BOUNDCHECKING
381 CheckBound(ix, iy, 0, 0, 0, 4);
382#endif
383 return ( *( mNDBlock.Begin()+ offset_+
384 ix*step_[0] + iy*step_[1] ) );
385}
386
387//! Return a reference to the element (ix,iy)
388template <class T>
389inline T & TArray<T>::operator()(sa_size_t ix, sa_size_t iy)
390{
391#ifdef SO_BOUNDCHECKING
392 CheckBound(ix, iy, 0, 0, 0, 4);
393#endif
394 return ( *( mNDBlock.Begin()+ offset_+
395 ix*step_[0] + iy*step_[1] ) );
396}
397
398//=======> a(ix,iy,iz) accees operators
399//! Return the value of the element (ix,iy,iz)
400template <class T>
401inline T const& TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz) const
402{
403#ifdef SO_BOUNDCHECKING
404 CheckBound(ix, iy, iz, 0, 0, 4);
405#endif
406 return ( *( mNDBlock.Begin()+ offset_+
407 ix*step_[0] + iy*step_[1] + iz*step_[2]) );
408}
409
410//! Return a reference to the element (ix,iy,iz)
411template <class T>
412inline T & TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz)
413{
414#ifdef SO_BOUNDCHECKING
415 CheckBound(ix, iy, iz, 0, 0, 4);
416#endif
417 return ( *( mNDBlock.Begin()+ offset_+
418 ix*step_[0] + iy*step_[1] + iz*step_[2]) );
419}
420
421//=======> a(ix,iy,iz,it,iu) or a(ix,iy,iz,it) accees operators
422//! Return the value of the element (ix,iy,iz,it,iu)
423template <class T>
424inline 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
425{
426#ifdef SO_BOUNDCHECKING
427 CheckBound(ix, iy, iz, it, iu, 4);
428#endif
429 return ( *( mNDBlock.Begin()+ offset_+
430 ix*step_[0] + iy*step_[1] + iz*step_[2] +
431 it*step_[3] + iu*step_[4]) );
432}
433
434//! Return a reference to the element (ix,iy,iz,it,iu)
435template <class T>
436inline T & TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu)
437{
438#ifdef SO_BOUNDCHECKING
439 CheckBound(ix, iy, iz, it, iu, 4);
440#endif
441 return ( *( mNDBlock.Begin()+ offset_+
442 ix*step_[0] + iy*step_[1] + iz*step_[2] +
443 it*step_[3] + iu*step_[4]) );
444}
445
446
447//! Operator [] : return element at positon ip
448template <class T>
449inline T const& TArray<T>::operator[](sa_size_t ip) const
450{
451#ifdef SO_BOUNDCHECKING
452 if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
453#endif
454return *(mNDBlock.Begin()+Offset(ip));
455}
456
457//! Operator [] : return element at positon ip
458template <class T>
459inline T & TArray<T>::operator[](sa_size_t ip)
460{
461#ifdef SO_BOUNDCHECKING
462 if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
463#endif
464return *(mNDBlock.Begin()+Offset(ip));
465}
466
467
468//! Converts to a scalar (value of first element) if the array size is equal to 1
469template <class T>
470inline T TArray<T>::toScalar()
471{
472 if (Size() != 1) throw(SzMismatchError("TArray<T>::operator T() Size() != 1")) ;
473 return ( (*this)[0] );
474}
475
476// Typedef pour simplifier
477/*! \ingroup TArray
478 \typedef Array
479 \brief To simplified TArray<r_8> writing
480*/
481typedef TArray<r_8> Array;
482
483//--------- extern template declarations (if needed) -----------
484#if defined ( NEED_EXT_DECL_TEMP ) && !defined( TARRAY_CC_BFILE )
485extern template class TArray<uint_1>;
486extern template class TArray<uint_2>;
487extern template class TArray<uint_4>;
488extern template class TArray<uint_8>;
489extern template class TArray<int_1>;
490extern template class TArray<int_2>;
491extern template class TArray<int_4>;
492extern template class TArray<int_8>;
493extern template class TArray<r_4>;
494extern template class TArray<r_8>;
495extern template class TArray< complex<r_4> >;
496extern template class TArray< complex<r_8> >;
497#ifdef SO_LDBLE128
498extern template class TArray<r_16>;
499extern template class TArray< complex<r_16> >;
500#endif
501#endif // Fin de if defined ( NEED_EXT_DECL_TEMP )
502
503} // Fin du namespace
504
505#endif
Note: See TracBrowser for help on using the repository browser.