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

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

define module HiStats in Doc cmv 13/4/00

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