[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 |
|
---|
[772] | 25 | template <class T>
|
---|
[787] | 26 | class TArray : public BaseArray {
|
---|
[772] | 27 | public:
|
---|
| 28 | // Creation / destruction
|
---|
| 29 | TArray();
|
---|
| 30 | TArray(uint_4 ndim, uint_4 * siz, uint_4 step =1);
|
---|
| 31 | TArray(uint_4 nx, uint_4 ny=0, uint_4 nz=0);
|
---|
| 32 | TArray(uint_4 ndim, uint_4 * siz, NDataBlock<T> & db, bool share=false, uint_4 step=1, uint_8 offset=0);
|
---|
| 33 | TArray(uint_4 ndim, uint_4 * siz, T* values, uint_4 step=1, uint_8 offset=0, Bridge* br=NULL);
|
---|
| 34 | TArray(const TArray<T>& a);
|
---|
| 35 | TArray(const TArray<T>& a, bool share);
|
---|
| 36 |
|
---|
| 37 | virtual ~TArray();
|
---|
| 38 |
|
---|
| 39 | // A = B : partage les donnees si "a" est temporaire, clone sinon.
|
---|
| 40 | virtual TArray<T>& operator = (const TArray<T>& a);
|
---|
| 41 |
|
---|
| 42 | // Gestion taille/Remplissage
|
---|
| 43 | virtual void Clone(const TArray<T>& a);
|
---|
| 44 | virtual void ReSize(uint_4 ndim, uint_4 * siz, uint_4 step=1);
|
---|
| 45 | virtual void Realloc(uint_4 ndim, uint_4 * siz, uint_4 step=1, bool force=false);
|
---|
[787] | 46 |
|
---|
[785] | 47 | // Compacts size=1 array dimensions
|
---|
[787] | 48 | virtual TArray<T>& CompactAllDimensions();
|
---|
| 49 | virtual TArray<T>& CompactTrailingDimensions();
|
---|
[785] | 50 |
|
---|
[787] | 51 | // SubArrays
|
---|
| 52 | virtual TArray<T> operator () (Range rx, Range ry, Range rz=0, Range rt=0, Range ru=0);
|
---|
[772] | 53 |
|
---|
| 54 |
|
---|
[787] | 55 | // ---- Access to data
|
---|
| 56 | // Definition of virtual element acces method inherited from BaseArray class
|
---|
| 57 | virtual double ValueAtPosition(uint_8 ip) const;
|
---|
[785] | 58 |
|
---|
[787] | 59 | // Data Access: operator overloaded inline acces methods
|
---|
[772] | 60 | inline T const& operator()(uint_4 ix, uint_4 iy, uint_4 iz) const ;
|
---|
| 61 | inline T& operator()(uint_4 ix, uint_4 iy, uint_4 iz);
|
---|
| 62 | inline T const& operator()(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu=0) const ;
|
---|
| 63 | inline T& operator()(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu=0);
|
---|
[785] | 64 | inline T const& operator[](uint_8 ip) const ;
|
---|
| 65 | inline T& operator[](uint_8 ip);
|
---|
[772] | 66 |
|
---|
| 67 | inline T const& Elem(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it=0, uint_4 iu=0) const ;
|
---|
| 68 | inline T& Elem(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it=0, uint_4 iu=0);
|
---|
| 69 | inline T const& ElemCheckBound(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it=0, uint_4 iu=0) const ;
|
---|
| 70 | inline T& ElemCheckBound(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it=0, uint_4 iu=0);
|
---|
| 71 |
|
---|
| 72 | inline T* Data() {return mNDBlock.Begin()+offset_;}
|
---|
| 73 | inline const T* Data() const {return mNDBlock.Begin()+offset_;}
|
---|
| 74 | inline NDataBlock<T>& DataBlock() {return mNDBlock;}
|
---|
| 75 | inline const NDataBlock<T>& DataBlock() const {return mNDBlock;}
|
---|
| 76 |
|
---|
[787] | 77 | // Temporaire?
|
---|
| 78 | inline bool IsTemp(void) const {return mNDBlock.IsTemp();}
|
---|
| 79 | inline void SetTemp(bool temp=false) const {mNDBlock.SetTemp(temp);}
|
---|
| 80 |
|
---|
[772] | 81 | // Operations diverses = , +=, ...
|
---|
| 82 | // Conversion en type T, if Size() == 1
|
---|
[787] | 83 | inline T toScalar();
|
---|
[772] | 84 | // Met les elements a une suite de valeurs
|
---|
[785] | 85 | virtual TArray<T>& operator = (Sequence seq);
|
---|
[772] | 86 | // A = x (tous les elements a x)
|
---|
| 87 | virtual TArray<T>& operator = (T x);
|
---|
| 88 | // A += -= *= /= x (ajoute, soustrait, ... x a tous les elements)
|
---|
| 89 | virtual TArray<T>& operator += (T x);
|
---|
| 90 | virtual TArray<T>& operator -= (T x);
|
---|
| 91 | virtual TArray<T>& operator *= (T x);
|
---|
| 92 | virtual TArray<T>& operator /= (T x);
|
---|
| 93 | // A += -= (ajoute, soustrait element par element les deux tableaux )
|
---|
| 94 | virtual TArray<T>& operator += (const TArray<T>& a);
|
---|
| 95 | virtual TArray<T>& operator -= (const TArray<T>& a);
|
---|
[787] | 96 | // Multiplication, division element par element les deux tableaux
|
---|
[772] | 97 | virtual TArray<T>& MultElt(const TArray<T>& a);
|
---|
| 98 | virtual TArray<T>& DivElt(const TArray<T>& a);
|
---|
| 99 |
|
---|
| 100 | // Impression, I/O, ...
|
---|
[787] | 101 | virtual string DataType() const; // definition of abstract method inherited from BaseArray
|
---|
| 102 | virtual void Print(ostream& os, int_4 maxprt=-1, bool si=false) const ;
|
---|
[772] | 103 |
|
---|
| 104 | // Pour la gestion de persistance
|
---|
| 105 | friend class FIO_TArray<T>;
|
---|
| 106 |
|
---|
| 107 | protected:
|
---|
| 108 | // partage les donnees si "a" temporaire, clone sinon.
|
---|
| 109 | void CloneOrShare(const TArray<T>& a);
|
---|
| 110 | // Share: partage les donnees de "a"
|
---|
| 111 | void Share(const TArray<T>& a);
|
---|
| 112 |
|
---|
| 113 | NDataBlock<T> mNDBlock; // Le bloc des donnees
|
---|
| 114 | };
|
---|
| 115 |
|
---|
| 116 | ////////////////////////////////////////////////////////////////
|
---|
| 117 | // Surcharge d'operateur <<
|
---|
| 118 | template <class T>
|
---|
| 119 | inline ostream& operator << (ostream& os, const TArray<T>& a)
|
---|
| 120 | { a.Print(os); return(os); }
|
---|
| 121 |
|
---|
| 122 | ////////////////////////////////////////////////////////////////
|
---|
| 123 | // Surcharge d'operateurs A (+,-,*,/) (T) x
|
---|
| 124 |
|
---|
| 125 | template <class T> inline TArray<T> operator + (const TArray<T>& a, T b)
|
---|
| 126 | {TArray<T> result(a); result.SetTemp(true); result += b; return result;}
|
---|
| 127 |
|
---|
| 128 | template <class T> inline TArray<T> operator + (T b,const TArray<T>& a)
|
---|
| 129 | {TArray<T> result(a); result.SetTemp(true); result += b; return result;}
|
---|
| 130 |
|
---|
| 131 | template <class T> inline TArray<T> operator - (const TArray<T>& a, T b)
|
---|
| 132 | {TArray<T> result(a); result.SetTemp(true); result -= b; return result;}
|
---|
| 133 |
|
---|
| 134 | template <class T> inline TArray<T> operator - (T b,const TArray<T>& a)
|
---|
| 135 | {TArray<T> result(a); result.SetTemp(true);
|
---|
| 136 | result.DataBlock() = b-result.DataBlock(); return result;}
|
---|
| 137 |
|
---|
| 138 | template <class T> inline TArray<T> operator * (const TArray<T>& a, T b)
|
---|
| 139 | {TArray<T> result(a); result.SetTemp(true); result *= b; return result;}
|
---|
| 140 |
|
---|
| 141 | template <class T> inline TArray<T> operator * (T b,const TArray<T>& a)
|
---|
| 142 | {TArray<T> result(a); result.SetTemp(true); result *= b; return result;}
|
---|
| 143 |
|
---|
| 144 | template <class T> inline TArray<T> operator / (const TArray<T>& a, T b)
|
---|
| 145 | {TArray<T> result(a); result.SetTemp(true); result /= b; return result;}
|
---|
| 146 |
|
---|
| 147 | ////////////////////////////////////////////////////////////////
|
---|
| 148 | // Surcharge d'operateurs C = A (+,-) B
|
---|
| 149 |
|
---|
| 150 | template <class T>
|
---|
| 151 | inline TArray<T> operator + (const TArray<T>& a,const TArray<T>& b)
|
---|
| 152 | {TArray<T> result(a); result.SetTemp(true); result += b; return result;}
|
---|
| 153 |
|
---|
| 154 | template <class T>
|
---|
| 155 | inline TArray<T> operator - (const TArray<T>& a,const TArray<T>& b)
|
---|
| 156 | {TArray<T> result(a); result.SetTemp(true); result += b; return result;}
|
---|
| 157 |
|
---|
| 158 |
|
---|
| 159 | // --------------------------------------------------
|
---|
| 160 | // inline element acces methods
|
---|
| 161 | // --------------------------------------------------
|
---|
| 162 | template <class T>
|
---|
| 163 | inline T const& TArray<T>::Elem(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu) const
|
---|
| 164 | {
|
---|
| 165 | return ( *( mNDBlock.Begin()+ offset_+
|
---|
| 166 | ix*step_[0] + iy*step_[1] + iz*step_[2] +
|
---|
| 167 | it*step_[3] + iu*step_[4]) );
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | template <class T>
|
---|
| 171 | inline T & TArray<T>::Elem(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu)
|
---|
| 172 | {
|
---|
| 173 | return ( *( mNDBlock.Begin()+ offset_+
|
---|
| 174 | ix*step_[0] + iy*step_[1] + iz*step_[2] +
|
---|
| 175 | it*step_[3] + iu*step_[4]) );
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | template <class T>
|
---|
| 179 | inline T const& TArray<T>::ElemCheckBound(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu) const
|
---|
| 180 | {
|
---|
| 181 | CheckBound(ix, iy, iz, it, iu, 4);
|
---|
| 182 | Elem(ix, iy, iz, it, iu);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | template <class T>
|
---|
| 186 | inline T & TArray<T>::ElemCheckBound(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu)
|
---|
| 187 | {
|
---|
| 188 | CheckBound(ix, iy, iz, it, iu, 4);
|
---|
| 189 | Elem(ix, iy, iz, it, iu);
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | template <class T>
|
---|
| 193 | inline T const& TArray<T>::operator()(uint_4 ix, uint_4 iy, uint_4 iz) const
|
---|
| 194 | {
|
---|
| 195 | #ifdef SO_BOUNDCHECKING
|
---|
| 196 | CheckBound(ix, iy, iz, 0, 0, 4);
|
---|
| 197 | #endif
|
---|
| 198 | return ( *( mNDBlock.Begin()+ offset_+
|
---|
| 199 | ix*step_[0] + iy*step_[1] + iz*step_[2]) );
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | template <class T>
|
---|
| 203 | inline T & TArray<T>::operator()(uint_4 ix, uint_4 iy, uint_4 iz)
|
---|
| 204 | {
|
---|
| 205 | #ifdef SO_BOUNDCHECKING
|
---|
| 206 | CheckBound(ix, iy, iz, 0, 0, 4);
|
---|
| 207 | #endif
|
---|
| 208 | return ( *( mNDBlock.Begin()+ offset_+
|
---|
| 209 | ix*step_[0] + iy*step_[1] + iz*step_[2]) );
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | template <class T>
|
---|
| 213 | inline T const& TArray<T>::operator()(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu) const
|
---|
| 214 | {
|
---|
| 215 | #ifdef SO_BOUNDCHECKING
|
---|
| 216 | CheckBound(ix, iy, iz, it, iu, 4);
|
---|
| 217 | #endif
|
---|
| 218 | return ( *( mNDBlock.Begin()+ offset_+
|
---|
| 219 | ix*step_[0] + iy*step_[1] + iz*step_[2] +
|
---|
| 220 | it*step_[3] + iu*step_[4]) );
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | template <class T>
|
---|
| 224 | inline T & TArray<T>::operator()(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu)
|
---|
| 225 | {
|
---|
| 226 | #ifdef SO_BOUNDCHECKING
|
---|
| 227 | CheckBound(ix, iy, iz, it, iu, 4);
|
---|
| 228 | #endif
|
---|
| 229 | return ( *( mNDBlock.Begin()+ offset_+
|
---|
| 230 | ix*step_[0] + iy*step_[1] + iz*step_[2] +
|
---|
| 231 | it*step_[3] + iu*step_[4]) );
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[785] | 234 |
|
---|
[772] | 235 | template <class T>
|
---|
[785] | 236 | inline T const& TArray<T>::operator[](uint_8 ip) const
|
---|
[772] | 237 | {
|
---|
| 238 | #ifdef SO_BOUNDCHECKING
|
---|
| 239 | if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
|
---|
| 240 | #endif
|
---|
[785] | 241 | return *(mNDBlock.Begin()+Offset(ip));
|
---|
[772] | 242 | }
|
---|
| 243 |
|
---|
| 244 | template <class T>
|
---|
[785] | 245 | inline T & TArray<T>::operator[](uint_8 ip)
|
---|
[772] | 246 | {
|
---|
| 247 | #ifdef SO_BOUNDCHECKING
|
---|
| 248 | if (ip >= totsize_) throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
|
---|
| 249 | #endif
|
---|
[785] | 250 | return *(mNDBlock.Begin()+Offset(ip));
|
---|
[772] | 251 | }
|
---|
| 252 |
|
---|
[785] | 253 |
|
---|
[772] | 254 | template <class T>
|
---|
[787] | 255 | inline T TArray<T>::toScalar()
|
---|
[772] | 256 | {
|
---|
| 257 | if (Size() != 1) throw(SzMismatchError("TArray<T>::operator T() Size() != 1")) ;
|
---|
| 258 | return ( (*this)[0] );
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | } // Fin du namespace
|
---|
| 262 |
|
---|
| 263 | #endif
|
---|