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