[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 ;
|
---|
| 60 | //! () operator for Sub arrays extraction
|
---|
| 61 | /*! \sa SubArray */
|
---|
[804] | 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); }
|
---|
[772] | 64 |
|
---|
[787] | 65 | // ---- Access to data
|
---|
| 66 | // Definition of virtual element acces method inherited from BaseArray class
|
---|
| 67 | virtual double ValueAtPosition(uint_8 ip) const;
|
---|
[785] | 68 |
|
---|
[787] | 69 | // Data Access: operator overloaded inline acces methods
|
---|
[772] | 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);
|
---|
[785] | 74 | inline T const& operator[](uint_8 ip) const ;
|
---|
| 75 | inline T& operator[](uint_8 ip);
|
---|
[772] | 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 |
|
---|
[894] | 82 | //! Return pointer to first element adress
|
---|
[772] | 83 | inline T* Data() {return mNDBlock.Begin()+offset_;}
|
---|
[894] | 84 | //! Return pointer to first element adress
|
---|
[772] | 85 | inline const T* Data() const {return mNDBlock.Begin()+offset_;}
|
---|
[894] | 86 | //! Return reference to datablock NDataBlock
|
---|
[772] | 87 | inline NDataBlock<T>& DataBlock() {return mNDBlock;}
|
---|
[894] | 88 | //! Return reference to datablock NDataBlock
|
---|
[772] | 89 | inline const NDataBlock<T>& DataBlock() const {return mNDBlock;}
|
---|
| 90 |
|
---|
[787] | 91 | // Temporaire?
|
---|
[894] | 92 | //! Are the array temporay ?
|
---|
[787] | 93 | inline bool IsTemp(void) const {return mNDBlock.IsTemp();}
|
---|
[894] | 94 | //! Set the array as temporay
|
---|
[787] | 95 | inline void SetTemp(bool temp=false) const {mNDBlock.SetTemp(temp);}
|
---|
| 96 |
|
---|
[772] | 97 | // Operations diverses = , +=, ...
|
---|
| 98 | // Conversion en type T, if Size() == 1
|
---|
[787] | 99 | inline T toScalar();
|
---|
[772] | 100 | // Met les elements a une suite de valeurs
|
---|
[813] | 101 | virtual TArray<T>& SetSeq(Sequence seq);
|
---|
[894] | 102 | //! Fill TArray with Sequence \b seq
|
---|
[813] | 103 | inline TArray<T>& operator = (Sequence seq) { return SetSeq(seq); }
|
---|
[772] | 104 | // A = x (tous les elements a x)
|
---|
[813] | 105 | virtual TArray<T>& SetT(T x);
|
---|
[894] | 106 | //! Fill TArray with all elements equal to \b x
|
---|
[813] | 107 | inline TArray<T>& operator = (T x) { return SetT(x); }
|
---|
[772] | 108 | // A += -= *= /= x (ajoute, soustrait, ... x a tous les elements)
|
---|
[804] | 109 | virtual TArray<T>& Add(T x);
|
---|
[894] | 110 | //! Add \b x to all elements
|
---|
[804] | 111 | inline TArray<T>& operator += (T x) { return Add(x); }
|
---|
| 112 | virtual TArray<T>& Sub(T x);
|
---|
[894] | 113 | //! Substract \b x to all elements
|
---|
[804] | 114 | inline TArray<T>& operator -= (T x) { return Sub(x); }
|
---|
| 115 | virtual TArray<T>& Mul(T x);
|
---|
[894] | 116 | //! Multiply all elements by \b x
|
---|
[804] | 117 | inline TArray<T>& operator *= (T x) { return Mul(x); }
|
---|
| 118 | virtual TArray<T>& Div(T x);
|
---|
[894] | 119 | //! Divide all elements by \b x
|
---|
[804] | 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 |
|
---|
[772] | 124 | // A += -= (ajoute, soustrait element par element les deux tableaux )
|
---|
[804] | 125 | virtual TArray<T>& AddElt(const TArray<T>& a);
|
---|
[894] | 126 | //! Operator TArray += TArray
|
---|
[804] | 127 | inline TArray<T>& operator += (const TArray<T>& a) { return AddElt(a); }
|
---|
| 128 | virtual TArray<T>& SubElt(const TArray<T>& a);
|
---|
[894] | 129 | //! Operator TArray -= TArray
|
---|
[804] | 130 | inline TArray<T>& operator -= (const TArray<T>& a) { return SubElt(a); }
|
---|
[787] | 131 | // Multiplication, division element par element les deux tableaux
|
---|
[804] | 132 | virtual TArray<T>& MulElt(const TArray<T>& a);
|
---|
[772] | 133 | virtual TArray<T>& DivElt(const TArray<T>& a);
|
---|
[804] | 134 | // Recopie des valeurs, element par element
|
---|
| 135 | virtual TArray<T>& CopyElt(const TArray<T>& a);
|
---|
[772] | 136 |
|
---|
[804] | 137 | // Somme et produit des elements
|
---|
| 138 | virtual T Sum() const ;
|
---|
| 139 | virtual T Product() const ;
|
---|
| 140 |
|
---|
[772] | 141 | // Impression, I/O, ...
|
---|
[813] | 142 | virtual string InfoString() const;
|
---|
| 143 | virtual void Print(ostream& os, int_4 maxprt=-1, bool si=false) const ;
|
---|
[772] | 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 |
|
---|
[894] | 154 | NDataBlock<T> mNDBlock; //!< Block for datas
|
---|
[772] | 155 | };
|
---|
| 156 |
|
---|
| 157 | ////////////////////////////////////////////////////////////////
|
---|
| 158 | // Surcharge d'operateur <<
|
---|
[894] | 159 | //! Print TArray \b a on stream \b os
|
---|
[772] | 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 |
|
---|
[894] | 167 | //! Operator TArray = TArray + constant
|
---|
[772] | 168 | template <class T> inline TArray<T> operator + (const TArray<T>& a, T b)
|
---|
[804] | 169 | {TArray<T> result(a); result.SetTemp(true); result.Add(b); return result;}
|
---|
[772] | 170 |
|
---|
[894] | 171 | //! Operator TArray = constant + TArray
|
---|
[772] | 172 | template <class T> inline TArray<T> operator + (T b,const TArray<T>& a)
|
---|
[804] | 173 | {TArray<T> result(a); result.SetTemp(true); result.Add(b); return result;}
|
---|
[772] | 174 |
|
---|
[894] | 175 | //! Operator TArray = TArray - constant
|
---|
[772] | 176 | template <class T> inline TArray<T> operator - (const TArray<T>& a, T b)
|
---|
[804] | 177 | {TArray<T> result(a); result.SetTemp(true); result.Sub(b); return result;}
|
---|
[772] | 178 |
|
---|
[894] | 179 | //! Operator TArray = constant - TArray
|
---|
[772] | 180 | template <class T> inline TArray<T> operator - (T b,const TArray<T>& a)
|
---|
[804] | 181 | {TArray<T> result(a); result.SetTemp(true); result.SubInv(b); return result;}
|
---|
[772] | 182 |
|
---|
[894] | 183 | //! Operator TArray = TArray * constant
|
---|
[772] | 184 | template <class T> inline TArray<T> operator * (const TArray<T>& a, T b)
|
---|
[804] | 185 | {TArray<T> result(a); result.SetTemp(true); result.Mul(b); return result;}
|
---|
[772] | 186 |
|
---|
[894] | 187 | //! Operator TArray = constant * TArray
|
---|
[772] | 188 | template <class T> inline TArray<T> operator * (T b,const TArray<T>& a)
|
---|
[804] | 189 | {TArray<T> result(a); result.SetTemp(true); result.Mul(b); return result;}
|
---|
[772] | 190 |
|
---|
[894] | 191 | //! Operator TArray = TArray / constant
|
---|
[772] | 192 | template <class T> inline TArray<T> operator / (const TArray<T>& a, T b)
|
---|
[804] | 193 | {TArray<T> result(a); result.SetTemp(true); result.DivInv(b); return result;}
|
---|
[772] | 194 |
|
---|
| 195 | ////////////////////////////////////////////////////////////////
|
---|
| 196 | // Surcharge d'operateurs C = A (+,-) B
|
---|
| 197 |
|
---|
[894] | 198 | //! Operator TArray = TArray + TArray
|
---|
[772] | 199 | template <class T>
|
---|
| 200 | inline TArray<T> operator + (const TArray<T>& a,const TArray<T>& b)
|
---|
[804] | 201 | {TArray<T> result(a); result.SetTemp(true); result.AddElt(b); return result;}
|
---|
[772] | 202 |
|
---|
[894] | 203 | //! Operator TArray = TArray - TArray
|
---|
[772] | 204 | template <class T>
|
---|
| 205 | inline TArray<T> operator - (const TArray<T>& a,const TArray<T>& b)
|
---|
[804] | 206 | {TArray<T> result(a); result.SetTemp(true); result.SubElt(b); return result;}
|
---|
[772] | 207 |
|
---|
| 208 |
|
---|
| 209 | // --------------------------------------------------
|
---|
| 210 | // inline element acces methods
|
---|
| 211 | // --------------------------------------------------
|
---|
[894] | 212 |
|
---|
| 213 | //! Return element (ix,iy,iz,it,iu) value
|
---|
[772] | 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 |
|
---|
[894] | 222 | //! Return element (ix,iy,iz,it,iu) value
|
---|
[772] | 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 |
|
---|
[894] | 231 | //! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
|
---|
[772] | 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);
|
---|
[804] | 236 | return(Elem(ix, iy, iz, it, iu));
|
---|
[772] | 237 | }
|
---|
| 238 |
|
---|
[894] | 239 | //! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
|
---|
[772] | 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);
|
---|
[804] | 244 | return(Elem(ix, iy, iz, it, iu));
|
---|
[772] | 245 | }
|
---|
| 246 |
|
---|
[894] | 247 | //! Return element (ix,iy,iz) value
|
---|
[772] | 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 |
|
---|
[894] | 258 | //! Return element (ix,iy,iz) value
|
---|
[772] | 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 |
|
---|
[894] | 269 | //! Operator () : return element (ix,iy,iz,it,iu) value
|
---|
[772] | 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 |
|
---|
[894] | 281 | //! Operator () : return element (ix,iy,iz,it,iu) value
|
---|
[772] | 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 |
|
---|
[785] | 293 |
|
---|
[894] | 294 | //! Operator [] : return element at positon ip
|
---|
[772] | 295 | template <class T>
|
---|
[785] | 296 | inline T const& TArray<T>::operator[](uint_8 ip) const
|
---|
[772] | 297 | {
|
---|
| 298 | #ifdef SO_BOUNDCHECKING
|
---|
| 299 | if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
|
---|
| 300 | #endif
|
---|
[785] | 301 | return *(mNDBlock.Begin()+Offset(ip));
|
---|
[772] | 302 | }
|
---|
| 303 |
|
---|
[894] | 304 | //! Operator [] : return element at positon ip
|
---|
[772] | 305 | template <class T>
|
---|
[785] | 306 | inline T & TArray<T>::operator[](uint_8 ip)
|
---|
[772] | 307 | {
|
---|
| 308 | #ifdef SO_BOUNDCHECKING
|
---|
| 309 | if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
|
---|
| 310 | #endif
|
---|
[785] | 311 | return *(mNDBlock.Begin()+Offset(ip));
|
---|
[772] | 312 | }
|
---|
| 313 |
|
---|
[785] | 314 |
|
---|
[894] | 315 | //! Return the value of first element
|
---|
[772] | 316 | template <class T>
|
---|
[787] | 317 | inline T TArray<T>::toScalar()
|
---|
[772] | 318 | {
|
---|
| 319 | if (Size() != 1) throw(SzMismatchError("TArray<T>::operator T() Size() != 1")) ;
|
---|
| 320 | return ( (*this)[0] );
|
---|
| 321 | }
|
---|
| 322 |
|
---|
[804] | 323 | // Typedef pour simplifier
|
---|
[894] | 324 | //! To simplify, Array \<==\> TArray<r_8>
|
---|
[804] | 325 | typedef TArray<r_8> Array;
|
---|
| 326 |
|
---|
[772] | 327 | } // Fin du namespace
|
---|
| 328 |
|
---|
| 329 | #endif
|
---|