[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
|
---|
| 26 | /*!
|
---|
[920] | 27 | \class SOPHYA::TArray
|
---|
| 28 | \ingroup TArray
|
---|
[894] | 29 | This class implements arrays of dimensions up to
|
---|
| 30 | \ref BASEARRAY_MAXNDIMS "BASEARRAY_MAXNDIMS"
|
---|
| 31 | */
|
---|
[772] | 32 | template <class T>
|
---|
[787] | 33 | class TArray : public BaseArray {
|
---|
[772] | 34 | public:
|
---|
| 35 | // Creation / destruction
|
---|
| 36 | TArray();
|
---|
[804] | 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);
|
---|
[772] | 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.
|
---|
[894] | 47 | //! = operator between TArray
|
---|
| 48 | /*! \sa Set */
|
---|
[804] | 49 | inline TArray<T>& operator = (const TArray<T>& a) { return Set(a); }
|
---|
| 50 | virtual TArray<T>& Set(const TArray<T>& a);
|
---|
[772] | 51 |
|
---|
| 52 | // Gestion taille/Remplissage
|
---|
| 53 | virtual void Clone(const TArray<T>& a);
|
---|
[804] | 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);
|
---|
[787] | 56 |
|
---|
[785] | 57 | // Compacts size=1 array dimensions
|
---|
[787] | 58 | virtual TArray<T>& CompactAllDimensions();
|
---|
| 59 | virtual TArray<T>& CompactTrailingDimensions();
|
---|
[785] | 60 |
|
---|
[804] | 61 | // Packing array elements in memory
|
---|
| 62 | virtual TArray<T> PackElements(bool force=false) const ;
|
---|
[772] | 63 |
|
---|
[804] | 64 | // SubArrays - $CHECK$ Reza 03/2000 je ne sais pas s'il faut declarer ca const ??
|
---|
[894] | 65 | TArray<T> SubArray(Range rx, Range ry, Range rz, Range rt, Range ru) const ;
|
---|
| 66 | //! () operator for Sub arrays extraction
|
---|
| 67 | /*! \sa SubArray */
|
---|
[804] | 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); }
|
---|
[772] | 70 |
|
---|
[787] | 71 | // ---- Access to data
|
---|
| 72 | // Definition of virtual element acces method inherited from BaseArray class
|
---|
| 73 | virtual double ValueAtPosition(uint_8 ip) const;
|
---|
[785] | 74 |
|
---|
[787] | 75 | // Data Access: operator overloaded inline acces methods
|
---|
[772] | 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);
|
---|
[785] | 80 | inline T const& operator[](uint_8 ip) const ;
|
---|
| 81 | inline T& operator[](uint_8 ip);
|
---|
[772] | 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 |
|
---|
[894] | 88 | //! Return pointer to first element adress
|
---|
[772] | 89 | inline T* Data() {return mNDBlock.Begin()+offset_;}
|
---|
[894] | 90 | //! Return pointer to first element adress
|
---|
[772] | 91 | inline const T* Data() const {return mNDBlock.Begin()+offset_;}
|
---|
[894] | 92 | //! Return reference to datablock NDataBlock
|
---|
[772] | 93 | inline NDataBlock<T>& DataBlock() {return mNDBlock;}
|
---|
[894] | 94 | //! Return reference to datablock NDataBlock
|
---|
[772] | 95 | inline const NDataBlock<T>& DataBlock() const {return mNDBlock;}
|
---|
| 96 |
|
---|
[787] | 97 | // Temporaire?
|
---|
[894] | 98 | //! Are the array temporay ?
|
---|
[787] | 99 | inline bool IsTemp(void) const {return mNDBlock.IsTemp();}
|
---|
[894] | 100 | //! Set the array as temporay
|
---|
[787] | 101 | inline void SetTemp(bool temp=false) const {mNDBlock.SetTemp(temp);}
|
---|
| 102 |
|
---|
[772] | 103 | // Operations diverses = , +=, ...
|
---|
| 104 | // Conversion en type T, if Size() == 1
|
---|
[787] | 105 | inline T toScalar();
|
---|
[772] | 106 | // Met les elements a une suite de valeurs
|
---|
[813] | 107 | virtual TArray<T>& SetSeq(Sequence seq);
|
---|
[894] | 108 | //! Fill TArray with Sequence \b seq
|
---|
[813] | 109 | inline TArray<T>& operator = (Sequence seq) { return SetSeq(seq); }
|
---|
[772] | 110 | // A = x (tous les elements a x)
|
---|
[813] | 111 | virtual TArray<T>& SetT(T x);
|
---|
[894] | 112 | //! Fill TArray with all elements equal to \b x
|
---|
[813] | 113 | inline TArray<T>& operator = (T x) { return SetT(x); }
|
---|
[772] | 114 | // A += -= *= /= x (ajoute, soustrait, ... x a tous les elements)
|
---|
[804] | 115 | virtual TArray<T>& Add(T x);
|
---|
[894] | 116 | //! Add \b x to all elements
|
---|
[804] | 117 | inline TArray<T>& operator += (T x) { return Add(x); }
|
---|
| 118 | virtual TArray<T>& Sub(T x);
|
---|
[894] | 119 | //! Substract \b x to all elements
|
---|
[804] | 120 | inline TArray<T>& operator -= (T x) { return Sub(x); }
|
---|
| 121 | virtual TArray<T>& Mul(T x);
|
---|
[894] | 122 | //! Multiply all elements by \b x
|
---|
[804] | 123 | inline TArray<T>& operator *= (T x) { return Mul(x); }
|
---|
| 124 | virtual TArray<T>& Div(T x);
|
---|
[894] | 125 | //! Divide all elements by \b x
|
---|
[804] | 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 |
|
---|
[772] | 130 | // A += -= (ajoute, soustrait element par element les deux tableaux )
|
---|
[804] | 131 | virtual TArray<T>& AddElt(const TArray<T>& a);
|
---|
[894] | 132 | //! Operator TArray += TArray
|
---|
[804] | 133 | inline TArray<T>& operator += (const TArray<T>& a) { return AddElt(a); }
|
---|
| 134 | virtual TArray<T>& SubElt(const TArray<T>& a);
|
---|
[894] | 135 | //! Operator TArray -= TArray
|
---|
[804] | 136 | inline TArray<T>& operator -= (const TArray<T>& a) { return SubElt(a); }
|
---|
[787] | 137 | // Multiplication, division element par element les deux tableaux
|
---|
[804] | 138 | virtual TArray<T>& MulElt(const TArray<T>& a);
|
---|
[772] | 139 | virtual TArray<T>& DivElt(const TArray<T>& a);
|
---|
[804] | 140 | // Recopie des valeurs, element par element
|
---|
| 141 | virtual TArray<T>& CopyElt(const TArray<T>& a);
|
---|
[772] | 142 |
|
---|
[804] | 143 | // Somme et produit des elements
|
---|
| 144 | virtual T Sum() const ;
|
---|
| 145 | virtual T Product() const ;
|
---|
| 146 |
|
---|
[772] | 147 | // Impression, I/O, ...
|
---|
[813] | 148 | virtual string InfoString() const;
|
---|
| 149 | virtual void Print(ostream& os, int_4 maxprt=-1, bool si=false) const ;
|
---|
[772] | 150 |
|
---|
| 151 | // Pour la gestion de persistance
|
---|
| 152 | friend class FIO_TArray<T>;
|
---|
| 153 |
|
---|
| 154 | protected:
|
---|
| 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 |
|
---|
[894] | 160 | NDataBlock<T> mNDBlock; //!< Block for datas
|
---|
[772] | 161 | };
|
---|
| 162 |
|
---|
| 163 | ////////////////////////////////////////////////////////////////
|
---|
| 164 | // Surcharge d'operateur <<
|
---|
[894] | 165 | //! Print TArray \b a on stream \b os
|
---|
[772] | 166 | template <class T>
|
---|
| 167 | inline ostream& operator << (ostream& os, const TArray<T>& a)
|
---|
| 168 | { a.Print(os); return(os); }
|
---|
| 169 |
|
---|
| 170 | ////////////////////////////////////////////////////////////////
|
---|
| 171 | // Surcharge d'operateurs A (+,-,*,/) (T) x
|
---|
| 172 |
|
---|
[894] | 173 | //! Operator TArray = TArray + constant
|
---|
[772] | 174 | template <class T> inline TArray<T> operator + (const TArray<T>& a, T b)
|
---|
[804] | 175 | {TArray<T> result(a); result.SetTemp(true); result.Add(b); return result;}
|
---|
[772] | 176 |
|
---|
[894] | 177 | //! Operator TArray = constant + TArray
|
---|
[772] | 178 | template <class T> inline TArray<T> operator + (T b,const TArray<T>& a)
|
---|
[804] | 179 | {TArray<T> result(a); result.SetTemp(true); result.Add(b); return result;}
|
---|
[772] | 180 |
|
---|
[894] | 181 | //! Operator TArray = TArray - constant
|
---|
[772] | 182 | template <class T> inline TArray<T> operator - (const TArray<T>& a, T b)
|
---|
[804] | 183 | {TArray<T> result(a); result.SetTemp(true); result.Sub(b); return result;}
|
---|
[772] | 184 |
|
---|
[894] | 185 | //! 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.SubInv(b); return result;}
|
---|
[772] | 188 |
|
---|
[894] | 189 | //! Operator TArray = TArray * constant
|
---|
[772] | 190 | template <class T> inline TArray<T> operator * (const TArray<T>& a, T b)
|
---|
[804] | 191 | {TArray<T> result(a); result.SetTemp(true); result.Mul(b); return result;}
|
---|
[772] | 192 |
|
---|
[894] | 193 | //! Operator TArray = constant * TArray
|
---|
[772] | 194 | template <class T> inline TArray<T> operator * (T b,const TArray<T>& a)
|
---|
[804] | 195 | {TArray<T> result(a); result.SetTemp(true); result.Mul(b); return result;}
|
---|
[772] | 196 |
|
---|
[894] | 197 | //! Operator TArray = TArray / constant
|
---|
[772] | 198 | template <class T> inline TArray<T> operator / (const TArray<T>& a, T b)
|
---|
[804] | 199 | {TArray<T> result(a); result.SetTemp(true); result.DivInv(b); return result;}
|
---|
[772] | 200 |
|
---|
| 201 | ////////////////////////////////////////////////////////////////
|
---|
| 202 | // Surcharge d'operateurs C = A (+,-) B
|
---|
| 203 |
|
---|
[894] | 204 | //! Operator TArray = TArray + TArray
|
---|
[772] | 205 | template <class T>
|
---|
| 206 | inline TArray<T> operator + (const TArray<T>& a,const TArray<T>& b)
|
---|
[804] | 207 | {TArray<T> result(a); result.SetTemp(true); result.AddElt(b); return result;}
|
---|
[772] | 208 |
|
---|
[894] | 209 | //! Operator TArray = TArray - TArray
|
---|
[772] | 210 | template <class T>
|
---|
| 211 | inline TArray<T> operator - (const TArray<T>& a,const TArray<T>& b)
|
---|
[804] | 212 | {TArray<T> result(a); result.SetTemp(true); result.SubElt(b); return result;}
|
---|
[772] | 213 |
|
---|
| 214 |
|
---|
| 215 | // --------------------------------------------------
|
---|
| 216 | // inline element acces methods
|
---|
| 217 | // --------------------------------------------------
|
---|
[894] | 218 |
|
---|
| 219 | //! Return element (ix,iy,iz,it,iu) value
|
---|
[772] | 220 | template <class T>
|
---|
| 221 | inline 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 |
|
---|
[894] | 228 | //! Return element (ix,iy,iz,it,iu) value
|
---|
[772] | 229 | template <class T>
|
---|
| 230 | inline 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 |
|
---|
[894] | 237 | //! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
|
---|
[772] | 238 | template <class T>
|
---|
| 239 | inline 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);
|
---|
[804] | 242 | return(Elem(ix, iy, iz, it, iu));
|
---|
[772] | 243 | }
|
---|
| 244 |
|
---|
[894] | 245 | //! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first
|
---|
[772] | 246 | template <class T>
|
---|
| 247 | inline 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);
|
---|
[804] | 250 | return(Elem(ix, iy, iz, it, iu));
|
---|
[772] | 251 | }
|
---|
| 252 |
|
---|
[894] | 253 | //! Return element (ix,iy,iz) value
|
---|
[772] | 254 | template <class T>
|
---|
| 255 | inline 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 |
|
---|
[894] | 264 | //! Return element (ix,iy,iz) value
|
---|
[772] | 265 | template <class T>
|
---|
| 266 | inline 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 |
|
---|
[894] | 275 | //! Operator () : return element (ix,iy,iz,it,iu) value
|
---|
[772] | 276 | template <class T>
|
---|
| 277 | inline 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 |
|
---|
[894] | 287 | //! Operator () : return element (ix,iy,iz,it,iu) value
|
---|
[772] | 288 | template <class T>
|
---|
| 289 | inline 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 |
|
---|
[785] | 299 |
|
---|
[894] | 300 | //! Operator [] : return element at positon ip
|
---|
[772] | 301 | template <class T>
|
---|
[785] | 302 | inline T const& TArray<T>::operator[](uint_8 ip) const
|
---|
[772] | 303 | {
|
---|
| 304 | #ifdef SO_BOUNDCHECKING
|
---|
| 305 | if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
|
---|
| 306 | #endif
|
---|
[785] | 307 | return *(mNDBlock.Begin()+Offset(ip));
|
---|
[772] | 308 | }
|
---|
| 309 |
|
---|
[894] | 310 | //! Operator [] : return element at positon ip
|
---|
[772] | 311 | template <class T>
|
---|
[785] | 312 | inline T & TArray<T>::operator[](uint_8 ip)
|
---|
[772] | 313 | {
|
---|
| 314 | #ifdef SO_BOUNDCHECKING
|
---|
| 315 | if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
|
---|
| 316 | #endif
|
---|
[785] | 317 | return *(mNDBlock.Begin()+Offset(ip));
|
---|
[772] | 318 | }
|
---|
| 319 |
|
---|
[785] | 320 |
|
---|
[894] | 321 | //! Return the value of first element
|
---|
[772] | 322 | template <class T>
|
---|
[787] | 323 | inline T TArray<T>::toScalar()
|
---|
[772] | 324 | {
|
---|
| 325 | if (Size() != 1) throw(SzMismatchError("TArray<T>::operator T() Size() != 1")) ;
|
---|
| 326 | return ( (*this)[0] );
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[804] | 329 | // Typedef pour simplifier
|
---|
[894] | 330 | //! To simplify, Array \<==\> TArray<r_8>
|
---|
[804] | 331 | typedef TArray<r_8> Array;
|
---|
| 332 |
|
---|
[772] | 333 | } // Fin du namespace
|
---|
| 334 |
|
---|
| 335 | #endif
|
---|