| [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> | 
|---|
| [2322] | 10 | #include <iostream> | 
|---|
| [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(); | 
|---|
| [2564] | 31 | TArray(int_4 ndim, const sa_size_t * siz, sa_size_t step =1, bool fzero=true); | 
|---|
|  | 32 | TArray(sa_size_t nx, sa_size_t ny=0, sa_size_t nz=0, sa_size_t nt=0, sa_size_t nu=0, bool fzero=true); | 
|---|
| [1156] | 33 | TArray(int_4 ndim, const sa_size_t * siz, NDataBlock<T> & db, bool share=false, sa_size_t step=1, sa_size_t offset=0); | 
|---|
|  | 34 | TArray(int_4 ndim, const sa_size_t * siz, T* values, sa_size_t step=1, sa_size_t offset=0, Bridge* br=NULL); | 
|---|
| [772] | 35 | TArray(const TArray<T>& a); | 
|---|
|  | 36 | TArray(const TArray<T>& a, bool share); | 
|---|
| [1081] | 37 | TArray(const BaseArray& a); | 
|---|
| [772] | 38 |  | 
|---|
|  | 39 | virtual ~TArray(); | 
|---|
|  | 40 |  | 
|---|
| [967] | 41 | // A = B | 
|---|
| [894] | 42 | //! = operator between TArray | 
|---|
| [976] | 43 | /*! \warning Datas are copied (cloned) from \b a. | 
|---|
|  | 44 | \sa Set \sa NDataBlock::operator=(const NDataBlock<T>&) */ | 
|---|
| [804] | 45 | inline  TArray<T>& operator = (const TArray<T>& a) { return Set(a); } | 
|---|
|  | 46 | virtual TArray<T>& Set(const TArray<T>& a); | 
|---|
| [772] | 47 |  | 
|---|
| [1081] | 48 | //! = operator between  TArray 's with different types - Elements are converted. | 
|---|
|  | 49 | inline  TArray<T>& operator = (const BaseArray& a) { return SetBA(a); } | 
|---|
|  | 50 | virtual TArray<T>& SetBA(const BaseArray& a); | 
|---|
|  | 51 |  | 
|---|
| [772] | 52 | // Gestion taille/Remplissage | 
|---|
|  | 53 | virtual void Clone(const TArray<T>& a); | 
|---|
| [970] | 54 | // partage les donnees si "a" temporaire, clone sinon. | 
|---|
|  | 55 | void CloneOrShare(const TArray<T>& a); | 
|---|
|  | 56 | // Share: partage les donnees de "a" | 
|---|
| [1393] | 57 | void Share(const TArray<T>& a); | 
|---|
| [970] | 58 |  | 
|---|
| [2564] | 59 | void ReSize(int_4 ndim, sa_size_t * siz, sa_size_t step=1, bool fzero=true); | 
|---|
| [2569] | 60 | void ReSize(const BaseArray& a, bool pack=false, bool fzero=true); | 
|---|
| [1393] | 61 | //! a synonym (alias) for method ReSize(int_4, ...) | 
|---|
| [2564] | 62 | inline void SetSize(int_4 ndim, sa_size_t * siz, sa_size_t step=1, bool fzero=true) | 
|---|
|  | 63 | { ReSize(ndim, siz, step, fzero); } | 
|---|
| [1517] | 64 | //! a synonym (alias) for method ReSize(const BaseArray&) | 
|---|
| [2569] | 65 | inline void SetSize(const BaseArray& a, bool pack=false, bool fzero=true) | 
|---|
| [2564] | 66 | { ReSize(a, pack, fzero); } | 
|---|
| [1156] | 67 | void Realloc(int_4 ndim, sa_size_t * siz, sa_size_t step=1, bool force=false); | 
|---|
| [787] | 68 |  | 
|---|
| [3173] | 69 | //! To clear the array sizes - corresponding to an unallocated array. | 
|---|
|  | 70 | virtual TArray<T>& ZeroSize(); | 
|---|
|  | 71 |  | 
|---|
| [785] | 72 | // Compacts size=1 array dimensions | 
|---|
| [787] | 73 | virtual TArray<T>& CompactAllDimensions(); | 
|---|
|  | 74 | virtual TArray<T>& CompactTrailingDimensions(); | 
|---|
| [785] | 75 |  | 
|---|
| [804] | 76 | // Packing array elements in memory | 
|---|
|  | 77 | virtual TArray<T> PackElements(bool force=false) const ; | 
|---|
| [772] | 78 |  | 
|---|
| [804] | 79 | // SubArrays - $CHECK$ Reza 03/2000 je ne sais pas s'il faut declarer ca const ?? | 
|---|
| [2917] | 80 | TArray<T> SubArray(Range rx, Range ry, Range rz, Range rt, Range ru, bool compact=true) const ; | 
|---|
| [1891] | 81 |  | 
|---|
| [2915] | 82 | //! Extract the first 3D subarray specified by rx, ry, rz. (see SubArray() ) | 
|---|
| [963] | 83 | inline TArray<T> operator () (Range rx, Range ry, Range rz) const | 
|---|
| [2915] | 84 | { return  SubArray(rx, ry, rz, Range::first(), Range::first()); } | 
|---|
|  | 85 | //! Extract the first 4D subarray specified by rx, ry, rz. (see SubArray() ) | 
|---|
| [963] | 86 | inline TArray<T> operator () (Range rx, Range ry, Range rz, Range rt) const | 
|---|
| [2915] | 87 | { return  SubArray(rx, ry, rz, rt, Range::first()); } | 
|---|
|  | 88 | //! Extract the subarray specified by rx, ry, rz. (see SubArray() ) | 
|---|
| [963] | 89 | inline TArray<T> operator () (Range rx, Range ry, Range rz, Range rt, Range ru) const | 
|---|
|  | 90 | { return  SubArray(rx, ry, rz, rt, ru); } | 
|---|
| [772] | 91 |  | 
|---|
| [787] | 92 | // ---- Access to data | 
|---|
|  | 93 | // Definition of virtual element acces method inherited from BaseArray class | 
|---|
| [1156] | 94 | virtual MuTyV & ValueAtPosition(sa_size_t ip) const; | 
|---|
| [2888] | 95 | virtual MuTyV & ValueAtPositionDB(sa_size_t ip) const; | 
|---|
| [785] | 96 |  | 
|---|
| [787] | 97 | // Data Access: operator overloaded inline acces methods | 
|---|
| [1156] | 98 | inline T const& operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz) const ; | 
|---|
|  | 99 | inline T&       operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz); | 
|---|
|  | 100 | inline T const& operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu=0) const ; | 
|---|
|  | 101 | inline T&       operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu=0); | 
|---|
|  | 102 | inline T const& operator[](sa_size_t ip) const ; | 
|---|
|  | 103 | inline T&       operator[](sa_size_t ip); | 
|---|
| [772] | 104 |  | 
|---|
| [1156] | 105 | inline T const& Elem(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it=0, sa_size_t iu=0) const ; | 
|---|
|  | 106 | inline T&       Elem(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it=0, sa_size_t iu=0); | 
|---|
|  | 107 | inline T const& ElemCheckBound(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it=0, sa_size_t iu=0) const ; | 
|---|
|  | 108 | inline T&       ElemCheckBound(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it=0, sa_size_t iu=0); | 
|---|
| [772] | 109 |  | 
|---|
| [894] | 110 | //! Return pointer to first element adress | 
|---|
| [772] | 111 | inline        T* Data()       {return mNDBlock.Begin()+offset_;} | 
|---|
| [894] | 112 | //! Return pointer to first element adress | 
|---|
| [772] | 113 | inline const  T* Data() const {return mNDBlock.Begin()+offset_;} | 
|---|
| [894] | 114 | //! Return reference to datablock NDataBlock | 
|---|
| [772] | 115 | inline        NDataBlock<T>& DataBlock()       {return mNDBlock;} | 
|---|
| [894] | 116 | //! Return reference to datablock NDataBlock | 
|---|
| [772] | 117 | inline const  NDataBlock<T>& DataBlock() const {return mNDBlock;} | 
|---|
|  | 118 |  | 
|---|
| [787] | 119 | // Temporaire? | 
|---|
| [894] | 120 | //! Are the array temporay ? | 
|---|
| [787] | 121 | inline bool   IsTemp(void) const {return mNDBlock.IsTemp();} | 
|---|
| [894] | 122 | //! Set the array as temporay | 
|---|
| [787] | 123 | inline void   SetTemp(bool temp=false) const {mNDBlock.SetTemp(temp);} | 
|---|
|  | 124 |  | 
|---|
| [772] | 125 | // Operations diverses  = , +=, ... | 
|---|
|  | 126 | // Conversion en type T, if Size() == 1 | 
|---|
| [787] | 127 | inline T toScalar(); | 
|---|
| [772] | 128 | // Met les elements a une suite de valeurs | 
|---|
| [1103] | 129 | virtual TArray<T>&  SetSeq(Sequence const & seq); | 
|---|
| [894] | 130 | //! Fill TArray with Sequence \b seq | 
|---|
| [1103] | 131 | inline  TArray<T>&  operator = (Sequence const & seq)    { return SetSeq(seq); } | 
|---|
| [2575] | 132 |  | 
|---|
| [772] | 133 | // A = x (tous les elements a x) | 
|---|
| [2575] | 134 | virtual TArray<T>&  SetCst(T x); | 
|---|
|  | 135 | //! Fill an array with a constant value \b x ( alias for \b SetCst() method ) | 
|---|
|  | 136 | inline  TArray<T>&  SetT(T x) { return SetCst(x); } | 
|---|
| [894] | 137 | //! Fill TArray with all elements equal to \b x | 
|---|
| [813] | 138 | inline  TArray<T>&  operator = (T x)             { return SetT(x); } | 
|---|
| [2564] | 139 |  | 
|---|
|  | 140 | // addition et soustraction de constante | 
|---|
|  | 141 | virtual TArray<T>&  AddCst(T x, TArray<T>& res) const ; | 
|---|
|  | 142 | virtual TArray<T>&  SubCst(T x, TArray<T>& res, bool fginv=false) const ; | 
|---|
|  | 143 | // Multiplication et division par une  constante | 
|---|
|  | 144 | virtual TArray<T>&  MulCst(T x, TArray<T>& res) const ; | 
|---|
|  | 145 | virtual TArray<T>&  DivCst(T x, TArray<T>& res, bool fginv=false) const ; | 
|---|
|  | 146 |  | 
|---|
| [772] | 147 | // A += -= *= /= x (ajoute, soustrait, ... x a tous les elements) | 
|---|
| [2575] | 148 | // Methodes Add/Sub/Mul/Div() sont la pour compatibilite avant V=2 (1.818) | 
|---|
|  | 149 | // Faut-il les garder ? Reza, Juillet 2004 | 
|---|
| [2564] | 150 | inline  TArray<T>&  Add(T x)                     { return AddCst(x, *this); } | 
|---|
|  | 151 | inline  TArray<T>&  Sub(T x, bool fginv=false)   { return SubCst(x, *this, fginv); } | 
|---|
|  | 152 | inline  TArray<T>&  Mul(T x)                     { return MulCst(x, *this); } | 
|---|
|  | 153 | inline  TArray<T>&  Div(T x, bool fginv=false)   { return DivCst(x, *this, fginv); } | 
|---|
|  | 154 |  | 
|---|
| [894] | 155 | //! Add \b x to all elements | 
|---|
| [2564] | 156 | inline  TArray<T>&  operator += (T x)            { return AddCst(x, *this); } | 
|---|
| [894] | 157 | //! Substract \b x to all elements | 
|---|
| [2564] | 158 | inline  TArray<T>&  operator -= (T x)            { return SubCst(x, *this); } | 
|---|
| [894] | 159 | //! Multiply all elements by \b x | 
|---|
| [2564] | 160 | inline  TArray<T>&  operator *= (T x)            { return MulCst(x, *this); } | 
|---|
| [894] | 161 | //! Divide all elements by \b x | 
|---|
| [2564] | 162 | inline  TArray<T>&  operator /= (T x)            { return DivCst(x, *this); } | 
|---|
| [804] | 163 |  | 
|---|
| [1156] | 164 | // applique le signe moins a tous les elements | 
|---|
| [2564] | 165 | virtual TArray<T>&  NegateElt(TArray<T>& res) const ; | 
|---|
|  | 166 | //! Replace array elements values by their opposite ( (*this)(i) -> -(*this)(i) ) | 
|---|
|  | 167 | inline  TArray<T>&  NegateElt()                  { return NegateElt(*this); } | 
|---|
|  | 168 |  | 
|---|
| [772] | 169 | // A += -=  (ajoute, soustrait element par element les deux tableaux ) | 
|---|
| [2575] | 170 | virtual TArray<T>&  AddElt(const TArray<T>& a, TArray<T>& res) const ; | 
|---|
|  | 171 | virtual TArray<T>&  SubElt(const TArray<T>& a, TArray<T>& res, bool fginv=false) const ; | 
|---|
| [787] | 172 | // Multiplication, division element par element les deux tableaux | 
|---|
| [2575] | 173 | virtual TArray<T>&  MulElt(const TArray<T>& a, TArray<T>& res) const ; | 
|---|
|  | 174 | virtual TArray<T>&  DivElt(const TArray<T>& a, TArray<T>& res, bool fginv=false, bool divzero=false) const ; | 
|---|
|  | 175 |  | 
|---|
|  | 176 | //! Operator TArray += TArray (element by element addition in place) | 
|---|
|  | 177 | inline  TArray<T>&  operator += (const TArray<T>& a)  { return AddElt(a, *this); } | 
|---|
|  | 178 | //! Operator TArray -= TArray (element by element subtraction in place) | 
|---|
|  | 179 | inline  TArray<T>&  operator -= (const TArray<T>& a)  { return SubElt(a, *this); } | 
|---|
|  | 180 |  | 
|---|
| [2588] | 181 | // Doit-on definir les operateur *= /= TArray ?  Reza, Juillet 2004 | 
|---|
|  | 182 | //! Element by element multiplication in place TArray *= TArray (element by element) | 
|---|
|  | 183 | inline  TArray<T>&  Mul(const TArray<T>& a)           { return MulElt(a, *this); } | 
|---|
|  | 184 | //! Element by element division in place TArray *= TArray (element by element) | 
|---|
| [2589] | 185 | inline  TArray<T>&  Div(const TArray<T>& a, bool divzero=false) | 
|---|
|  | 186 | { return DivElt(a, *this, false, divzero); } | 
|---|
| [2588] | 187 |  | 
|---|
| [804] | 188 | // Recopie des valeurs, element par element | 
|---|
|  | 189 | virtual TArray<T>&  CopyElt(const TArray<T>& a); | 
|---|
| [1081] | 190 | // Recopie des valeurs avec conversion prealable, element par element | 
|---|
|  | 191 | virtual TArray<T>&  ConvertAndCopyElt(const BaseArray& a); | 
|---|
| [772] | 192 |  | 
|---|
| [2575] | 193 | // Calcul du produit scalaire ( Somme_i (*this)(i)*a(i) ) | 
|---|
|  | 194 | virtual T ScalarProduct(const TArray<T>& a) const ; | 
|---|
|  | 195 |  | 
|---|
| [804] | 196 | // Somme et produit des elements | 
|---|
|  | 197 | virtual T Sum() const ; | 
|---|
|  | 198 | virtual T Product() const ; | 
|---|
| [1113] | 199 | // Somme du carre des elements | 
|---|
| [3332] | 200 | virtual T SumSq() const; | 
|---|
|  | 201 | //  Norme^2 , identique a SumSq pour tableaux reels/integer , sum[el * conj(el)] pour complexe | 
|---|
|  | 202 | virtual T Norm2() const; | 
|---|
|  | 203 | // Valeur min et max des elements (sauf tableaux complexes -> exception) | 
|---|
| [1113] | 204 | virtual void MinMax(T& min, T& max) const ; | 
|---|
| [804] | 205 |  | 
|---|
| [772] | 206 | // Impression, I/O, ... | 
|---|
| [813] | 207 | virtual string InfoString() const; | 
|---|
| [1550] | 208 | virtual void   Print(ostream& os, sa_size_t maxprt=-1, | 
|---|
|  | 209 | bool si=false, bool ascd=false) const ; | 
|---|
| [772] | 210 |  | 
|---|
| [1517] | 211 | // Lecture,Ecriture sur fichier ASCII | 
|---|
| [2286] | 212 | virtual sa_size_t ReadASCII(istream& is, sa_size_t & nr, sa_size_t & nc, | 
|---|
|  | 213 | char clm='#', const char* sep=" \t"); | 
|---|
| [1517] | 214 | virtual void   WriteASCII(ostream& os) const; | 
|---|
|  | 215 |  | 
|---|
| [3173] | 216 | //! assign a new object Id (or DataRef Id) - useful for PPF write operations | 
|---|
|  | 217 | inline void RenewObjId() { mNDBlock.RenewObjId(); } | 
|---|
| [772] | 218 | //  Pour la gestion de persistance | 
|---|
|  | 219 | friend class  FIO_TArray<T>; | 
|---|
|  | 220 |  | 
|---|
|  | 221 | protected: | 
|---|
|  | 222 |  | 
|---|
| [894] | 223 | NDataBlock<T> mNDBlock; //!< Block for datas | 
|---|
| [1081] | 224 | mutable MuTyV my_mtv;   //!< for use by ValueAtPosition() | 
|---|
| [772] | 225 | }; | 
|---|
|  | 226 |  | 
|---|
|  | 227 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 228 | //   Surcharge d'operateur << | 
|---|
| [894] | 229 | //! Print TArray \b a on stream \b os | 
|---|
| [772] | 230 | template <class T> | 
|---|
|  | 231 | inline ostream& operator << (ostream& os, const TArray<T>& a) | 
|---|
|  | 232 | { a.Print(os);    return(os);    } | 
|---|
|  | 233 |  | 
|---|
| [1517] | 234 | //   Surcharge d'operateur >> | 
|---|
|  | 235 | //! Decodes the ASCII input stream \b is , filling TArray \b a elements | 
|---|
|  | 236 | template <class T> | 
|---|
|  | 237 | inline istream& operator >> (istream& is, TArray<T>& a) | 
|---|
| [1576] | 238 | { sa_size_t nr, nc; | 
|---|
|  | 239 | a.ReadASCII(is, nr, nc);    return(is);    } | 
|---|
| [1517] | 240 |  | 
|---|
|  | 241 |  | 
|---|
| [772] | 242 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 243 | // Surcharge d'operateurs A (+,-,*,/) (T) x | 
|---|
|  | 244 |  | 
|---|
| [958] | 245 | /*! \ingroup TArray \fn operator+(const TArray<T>&,T) | 
|---|
|  | 246 | \brief Operator TArray = TArray + constant */ | 
|---|
| [772] | 247 | template <class T> inline TArray<T> operator + (const TArray<T>& a, T b) | 
|---|
| [2564] | 248 | {TArray<T> result; result.SetTemp(true); | 
|---|
| [2569] | 249 | a.AddCst(b, result); return result;} | 
|---|
| [772] | 250 |  | 
|---|
| [958] | 251 | /*! \ingroup TArray \fn operator+(T,const TArray<T>&) | 
|---|
|  | 252 | \brief Operator TArray = constant + TArray */ | 
|---|
| [772] | 253 | template <class T> inline TArray<T> operator + (T b,const TArray<T>& a) | 
|---|
| [2564] | 254 | {TArray<T> result; result.SetTemp(true); | 
|---|
| [2569] | 255 | a.AddCst(b, result); return result;} | 
|---|
| [772] | 256 |  | 
|---|
| [958] | 257 | /*! \ingroup TArray \fn operator-(const TArray<T>&,T) | 
|---|
|  | 258 | \brief Operator TArray = TArray - constant */ | 
|---|
| [772] | 259 | template <class T> inline TArray<T> operator - (const TArray<T>& a, T b) | 
|---|
| [2564] | 260 | {TArray<T> result; result.SetTemp(true); | 
|---|
| [2569] | 261 | a.SubCst(b,result); return result;} | 
|---|
| [772] | 262 |  | 
|---|
| [958] | 263 | /*! \ingroup TArray \fn operator-(T,const TArray<T>&) | 
|---|
|  | 264 | \brief Operator TArray = constant - TArray */ | 
|---|
| [772] | 265 | template <class T> inline TArray<T> operator - (T b,const TArray<T>& a) | 
|---|
| [2564] | 266 | {TArray<T> result; result.SetTemp(true); | 
|---|
| [2569] | 267 | a.SubCst(b,result,true); return result;} | 
|---|
| [772] | 268 |  | 
|---|
| [958] | 269 | /*! \ingroup TArray \fn operator*(const TArray<T>&,T) | 
|---|
|  | 270 | \brief Operator TArray = TArray * constant */ | 
|---|
| [772] | 271 | template <class T> inline TArray<T> operator * (const TArray<T>& a, T b) | 
|---|
| [2564] | 272 | {TArray<T> result; result.SetTemp(true); | 
|---|
|  | 273 | a.MulCst(b, result); return result;} | 
|---|
| [772] | 274 |  | 
|---|
| [958] | 275 | /*! \ingroup TArray \fn operator*(T,const TArray<T>&) | 
|---|
|  | 276 | \brief Operator TArray = constant * TArray */ | 
|---|
| [772] | 277 | template <class T> inline TArray<T> operator * (T b,const TArray<T>& a) | 
|---|
| [2564] | 278 | {TArray<T> result; result.SetTemp(true); | 
|---|
|  | 279 | a.MulCst(b,result); return result;} | 
|---|
| [772] | 280 |  | 
|---|
| [958] | 281 | /*! \ingroup TArray \fn operator/(const TArray<T>&,T) | 
|---|
|  | 282 | \brief Operator TArray = TArray / constant */ | 
|---|
| [772] | 283 | template <class T> inline TArray<T> operator / (const TArray<T>& a, T b) | 
|---|
| [2564] | 284 | {TArray<T> result; result.SetTemp(true); | 
|---|
| [2569] | 285 | a.DivCst(b,result); return result;} | 
|---|
| [772] | 286 |  | 
|---|
| [970] | 287 | /*! \ingroup TArray \fn operator/(T,const TArray<T>&) | 
|---|
|  | 288 | \brief Operator TArray = constant / TArray  */ | 
|---|
|  | 289 | template <class T> inline TArray<T> operator / (T b, const TArray<T>& a) | 
|---|
| [2564] | 290 | {TArray<T> result; result.SetTemp(true); | 
|---|
| [2569] | 291 | a.DivCst(b, result, true); return result;} | 
|---|
| [970] | 292 |  | 
|---|
| [772] | 293 | //////////////////////////////////////////////////////////////// | 
|---|
| [1156] | 294 | // Surcharge d'operateurs B = -A | 
|---|
|  | 295 |  | 
|---|
|  | 296 | /*! \ingroup TArray \fn operator - (const TArray<T>&) | 
|---|
|  | 297 | \brief Operator - Returns an array with elements equal to the opposite of | 
|---|
|  | 298 | the original array elements.  */ | 
|---|
|  | 299 | template <class T> inline TArray<T> operator - (const TArray<T>& a) | 
|---|
| [2569] | 300 | {TArray<T> result; result.SetTemp(true); | 
|---|
|  | 301 | a.NegateElt(result); return result;} | 
|---|
| [1156] | 302 |  | 
|---|
|  | 303 | //////////////////////////////////////////////////////////////// | 
|---|
| [2938] | 304 | // Surcharge d'operateurs C = A (+,-,&&,/) B | 
|---|
| [772] | 305 |  | 
|---|
| [958] | 306 | /*! \ingroup TArray \fn operator+(const TArray<T>&,const TArray<T>&) | 
|---|
| [2588] | 307 | \brief Operator TArray = TArray + TArray (element by element addition) */ | 
|---|
| [772] | 308 | template <class T> | 
|---|
|  | 309 | inline TArray<T> operator + (const TArray<T>& a,const TArray<T>& b) | 
|---|
| [970] | 310 | { TArray<T> result; result.SetTemp(true); | 
|---|
| [2575] | 311 | a.AddElt(b, result);    return result; } | 
|---|
| [772] | 312 |  | 
|---|
| [958] | 313 | /*! \ingroup TArray \fn operator-(const TArray<T>&,const TArray<T>&) | 
|---|
| [2588] | 314 | \brief Operator TArray = TArray - TArray (element by element subtraction) */ | 
|---|
| [772] | 315 | template <class T> | 
|---|
|  | 316 | inline TArray<T> operator - (const TArray<T>& a,const TArray<T>& b) | 
|---|
| [970] | 317 | { TArray<T> result; result.SetTemp(true); | 
|---|
| [2575] | 318 | a.SubElt(b, result);    return result; } | 
|---|
| [772] | 319 |  | 
|---|
| [2938] | 320 | /*! \ingroup TArray \fn operator && (const TArray<T>&,const TArray<T>&) | 
|---|
| [2588] | 321 | \brief Element by element multiplication of two arrays TArray = TArray * TArray */ | 
|---|
| [772] | 322 |  | 
|---|
| [2588] | 323 | template <class T> | 
|---|
| [2938] | 324 | inline TArray<T> operator && (const TArray<T>& a,const TArray<T>& b) | 
|---|
| [2588] | 325 | { TArray<T> result; result.SetTemp(true); | 
|---|
|  | 326 | a.MulElt(b, result);    return result; } | 
|---|
|  | 327 |  | 
|---|
| [2938] | 328 | /*! \ingroup TArray \fn operator / (const TArray<T>&,const TArray<T>&) | 
|---|
|  | 329 | \brief Element by element division of two arrays TArray = TArray / TArray */ | 
|---|
| [2588] | 330 | template <class T> | 
|---|
| [2938] | 331 | inline TArray<T> operator / (const TArray<T>& a,const TArray<T>& b) | 
|---|
| [2588] | 332 | { TArray<T> result; result.SetTemp(true); | 
|---|
| [2938] | 333 | a.DivElt(b, result, false, false);    return result; } | 
|---|
| [2588] | 334 |  | 
|---|
| [772] | 335 | // -------------------------------------------------- | 
|---|
|  | 336 | //        inline element acces methods | 
|---|
|  | 337 | // -------------------------------------------------- | 
|---|
| [894] | 338 |  | 
|---|
|  | 339 | //! Return element (ix,iy,iz,it,iu) value | 
|---|
| [772] | 340 | template <class T> | 
|---|
| [1156] | 341 | inline T const& TArray<T>::Elem(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu) const | 
|---|
| [772] | 342 | { | 
|---|
|  | 343 | return ( *( mNDBlock.Begin()+ offset_+ | 
|---|
|  | 344 | ix*step_[0] + iy*step_[1] + iz*step_[2] + | 
|---|
|  | 345 | it*step_[3] + iu*step_[4]) ); | 
|---|
|  | 346 | } | 
|---|
|  | 347 |  | 
|---|
| [894] | 348 | //! Return element (ix,iy,iz,it,iu) value | 
|---|
| [772] | 349 | template <class T> | 
|---|
| [1156] | 350 | inline T & TArray<T>::Elem(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu) | 
|---|
| [772] | 351 | { | 
|---|
|  | 352 | return ( *( mNDBlock.Begin()+ offset_+ | 
|---|
|  | 353 | ix*step_[0] + iy*step_[1] + iz*step_[2] + | 
|---|
|  | 354 | it*step_[3] + iu*step_[4]) ); | 
|---|
|  | 355 | } | 
|---|
|  | 356 |  | 
|---|
| [894] | 357 | //! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first | 
|---|
| [772] | 358 | template <class T> | 
|---|
| [1156] | 359 | inline T const& TArray<T>::ElemCheckBound(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu) const | 
|---|
| [772] | 360 | { | 
|---|
|  | 361 | CheckBound(ix, iy, iz, it, iu, 4); | 
|---|
| [804] | 362 | return(Elem(ix, iy, iz, it, iu)); | 
|---|
| [772] | 363 | } | 
|---|
|  | 364 |  | 
|---|
| [894] | 365 | //! Return element (ix,iy,iz,it,iu) value with Check of indexes bound first | 
|---|
| [772] | 366 | template <class T> | 
|---|
| [1156] | 367 | inline T & TArray<T>::ElemCheckBound(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu) | 
|---|
| [772] | 368 | { | 
|---|
|  | 369 | CheckBound(ix, iy, iz, it, iu, 4); | 
|---|
| [804] | 370 | return(Elem(ix, iy, iz, it, iu)); | 
|---|
| [772] | 371 | } | 
|---|
|  | 372 |  | 
|---|
| [894] | 373 | //! Return element (ix,iy,iz) value | 
|---|
| [772] | 374 | template <class T> | 
|---|
| [1156] | 375 | inline T const& TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz) const | 
|---|
| [772] | 376 | { | 
|---|
|  | 377 | #ifdef SO_BOUNDCHECKING | 
|---|
|  | 378 | CheckBound(ix, iy, iz, 0, 0, 4); | 
|---|
|  | 379 | #endif | 
|---|
|  | 380 | return ( *( mNDBlock.Begin()+ offset_+ | 
|---|
|  | 381 | ix*step_[0] + iy*step_[1] + iz*step_[2]) ); | 
|---|
|  | 382 | } | 
|---|
|  | 383 |  | 
|---|
| [894] | 384 | //! Return element (ix,iy,iz) value | 
|---|
| [772] | 385 | template <class T> | 
|---|
| [1156] | 386 | inline T & TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz) | 
|---|
| [772] | 387 | { | 
|---|
|  | 388 | #ifdef SO_BOUNDCHECKING | 
|---|
|  | 389 | CheckBound(ix, iy, iz, 0, 0, 4); | 
|---|
|  | 390 | #endif | 
|---|
|  | 391 | return ( *( mNDBlock.Begin()+ offset_+ | 
|---|
|  | 392 | ix*step_[0] + iy*step_[1] + iz*step_[2]) ); | 
|---|
|  | 393 | } | 
|---|
|  | 394 |  | 
|---|
| [894] | 395 | //! Operator () : return element (ix,iy,iz,it,iu) value | 
|---|
| [772] | 396 | template <class T> | 
|---|
| [1156] | 397 | inline T const& TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu) const | 
|---|
| [772] | 398 | { | 
|---|
|  | 399 | #ifdef SO_BOUNDCHECKING | 
|---|
|  | 400 | CheckBound(ix, iy, iz, it, iu, 4); | 
|---|
|  | 401 | #endif | 
|---|
|  | 402 | return ( *( mNDBlock.Begin()+ offset_+ | 
|---|
|  | 403 | ix*step_[0] + iy*step_[1] + iz*step_[2] + | 
|---|
|  | 404 | it*step_[3] + iu*step_[4]) ); | 
|---|
|  | 405 | } | 
|---|
|  | 406 |  | 
|---|
| [894] | 407 | //! Operator () : return element (ix,iy,iz,it,iu) value | 
|---|
| [772] | 408 | template <class T> | 
|---|
| [1156] | 409 | inline T & TArray<T>::operator()(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu) | 
|---|
| [772] | 410 | { | 
|---|
|  | 411 | #ifdef SO_BOUNDCHECKING | 
|---|
|  | 412 | CheckBound(ix, iy, iz, it, iu, 4); | 
|---|
|  | 413 | #endif | 
|---|
|  | 414 | return ( *( mNDBlock.Begin()+ offset_+ | 
|---|
|  | 415 | ix*step_[0] + iy*step_[1] + iz*step_[2] + | 
|---|
|  | 416 | it*step_[3] + iu*step_[4]) ); | 
|---|
|  | 417 | } | 
|---|
|  | 418 |  | 
|---|
| [785] | 419 |  | 
|---|
| [894] | 420 | //! Operator [] : return element at positon ip | 
|---|
| [772] | 421 | template <class T> | 
|---|
| [1156] | 422 | inline T const& TArray<T>::operator[](sa_size_t ip) const | 
|---|
| [772] | 423 | { | 
|---|
|  | 424 | #ifdef SO_BOUNDCHECKING | 
|---|
|  | 425 | if (ip >= totsize_)  throw( ParmError("TArray<T>::operator[] Out-of-bound Error") ); | 
|---|
|  | 426 | #endif | 
|---|
| [785] | 427 | return *(mNDBlock.Begin()+Offset(ip)); | 
|---|
| [772] | 428 | } | 
|---|
|  | 429 |  | 
|---|
| [894] | 430 | //! Operator [] : return element at positon ip | 
|---|
| [772] | 431 | template <class T> | 
|---|
| [1156] | 432 | inline T & TArray<T>::operator[](sa_size_t ip) | 
|---|
| [772] | 433 | { | 
|---|
|  | 434 | #ifdef SO_BOUNDCHECKING | 
|---|
|  | 435 | if (ip >= totsize_)  throw( ParmError("TArray<T>::operator[] Out-of-bound Error") ); | 
|---|
|  | 436 | #endif | 
|---|
| [785] | 437 | return *(mNDBlock.Begin()+Offset(ip)); | 
|---|
| [772] | 438 | } | 
|---|
|  | 439 |  | 
|---|
| [785] | 440 |  | 
|---|
| [1156] | 441 | //! Converts to a scalar (value of first element) if the array size is equal to 1 | 
|---|
| [772] | 442 | template <class T> | 
|---|
| [787] | 443 | inline T TArray<T>::toScalar() | 
|---|
| [772] | 444 | { | 
|---|
|  | 445 | if (Size() != 1) throw(SzMismatchError("TArray<T>::operator T() Size() != 1")) ; | 
|---|
|  | 446 | return ( (*this)[0] ); | 
|---|
|  | 447 | } | 
|---|
|  | 448 |  | 
|---|
| [804] | 449 | // Typedef pour simplifier | 
|---|
| [956] | 450 | /*! \ingroup TArray | 
|---|
|  | 451 | \typedef Array | 
|---|
|  | 452 | \brief To simplified TArray<r_8> writing | 
|---|
|  | 453 | */ | 
|---|
| [804] | 454 | typedef TArray<r_8> Array; | 
|---|
|  | 455 |  | 
|---|
| [772] | 456 | } // Fin du namespace | 
|---|
|  | 457 |  | 
|---|
|  | 458 | #endif | 
|---|