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