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

Last change on this file since 906 was 894, checked in by ansari, 25 years ago

documentation cmv 12/4/00

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