| 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>
 | 
|---|
| 11 | #include "basarr.h"
 | 
|---|
| 12 | #include "ndatablock.h"
 | 
|---|
| 13 | #include <complex>
 | 
|---|
| 14 | #include "utilarr.h"
 | 
|---|
| 15 | 
 | 
|---|
| 16 | 
 | 
|---|
| 17 | namespace SOPHYA {
 | 
|---|
| 18 | 
 | 
|---|
| 19 | // Forward declaration
 | 
|---|
| 20 | template <class T> class FIO_TArray;
 | 
|---|
| 21 | 
 | 
|---|
| 22 | //   --------------------------- classe template Array -----------------------
 | 
|---|
| 23 | //  ( See BaseArray class for data organisation in  memory and related methods )
 | 
|---|
| 24 | 
 | 
|---|
| 25 | template <class T>
 | 
|---|
| 26 | class TArray : public BaseArray {
 | 
|---|
| 27 | public:
 | 
|---|
| 28 |   // Creation / destruction 
 | 
|---|
| 29 |   TArray();
 | 
|---|
| 30 |   TArray(uint_4 ndim, const uint_4 * siz, uint_4 step =1);
 | 
|---|
| 31 |   TArray(uint_4 nx, uint_4 ny=0, uint_4 nz=0, uint_4 nt=0, uint_4 nu=0);
 | 
|---|
| 32 |   TArray(uint_4 ndim, const uint_4 * siz, NDataBlock<T> & db, bool share=false, uint_4 step=1, uint_8 offset=0);
 | 
|---|
| 33 |   TArray(uint_4 ndim, const 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 |   inline  TArray<T>& operator = (const TArray<T>& a) { return Set(a); }
 | 
|---|
| 41 |   virtual TArray<T>& Set(const TArray<T>& a);
 | 
|---|
| 42 | 
 | 
|---|
| 43 |   // Gestion taille/Remplissage
 | 
|---|
| 44 |   virtual void Clone(const TArray<T>& a); 
 | 
|---|
| 45 |   void ReSize(uint_4 ndim, uint_4 * siz, uint_4 step=1);
 | 
|---|
| 46 |   void Realloc(uint_4 ndim, uint_4 * siz, uint_4 step=1, bool force=false);
 | 
|---|
| 47 | 
 | 
|---|
| 48 |   // Compacts size=1 array dimensions
 | 
|---|
| 49 |   virtual TArray<T>& CompactAllDimensions();
 | 
|---|
| 50 |   virtual TArray<T>& CompactTrailingDimensions();
 | 
|---|
| 51 | 
 | 
|---|
| 52 |   // Packing array elements in memory
 | 
|---|
| 53 |   virtual TArray<T> PackElements(bool force=false) const ;
 | 
|---|
| 54 | 
 | 
|---|
| 55 |   // SubArrays - $CHECK$ Reza 03/2000 je ne sais pas s'il faut declarer ca const ??
 | 
|---|
| 56 |   TArray<T> SubArray(Range rx, Range ry, Range rz, Range rt, Range ru) const ; 
 | 
|---|
| 57 |   inline TArray<T> operator () (Range rx, Range ry, Range rz, Range rt=0, Range ru=0) const
 | 
|---|
| 58 |                    { return  SubArray(rx, ry, rz, rt, ru); }
 | 
|---|
| 59 | 
 | 
|---|
| 60 |   // ---- Access to data
 | 
|---|
| 61 |   // Definition of virtual element acces method inherited from BaseArray class 
 | 
|---|
| 62 |   virtual double ValueAtPosition(uint_8 ip) const;
 | 
|---|
| 63 | 
 | 
|---|
| 64 |   // Data Access: operator overloaded inline acces methods 
 | 
|---|
| 65 |   inline T const& operator()(uint_4 ix, uint_4 iy, uint_4 iz) const ;
 | 
|---|
| 66 |   inline T&       operator()(uint_4 ix, uint_4 iy, uint_4 iz);
 | 
|---|
| 67 |   inline T const& operator()(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu=0) const ;
 | 
|---|
| 68 |   inline T&       operator()(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu=0);
 | 
|---|
| 69 |   inline T const& operator[](uint_8 ip) const ;
 | 
|---|
| 70 |   inline T&       operator[](uint_8 ip);
 | 
|---|
| 71 | 
 | 
|---|
| 72 |   inline T const& Elem(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it=0, uint_4 iu=0) const ;
 | 
|---|
| 73 |   inline T&       Elem(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it=0, uint_4 iu=0);
 | 
|---|
| 74 |   inline T const& ElemCheckBound(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it=0, uint_4 iu=0) const ;
 | 
|---|
| 75 |   inline T&       ElemCheckBound(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it=0, uint_4 iu=0);
 | 
|---|
| 76 | 
 | 
|---|
| 77 |   inline        T* Data()       {return mNDBlock.Begin()+offset_;}
 | 
|---|
| 78 |   inline const  T* Data() const {return mNDBlock.Begin()+offset_;}
 | 
|---|
| 79 |   inline        NDataBlock<T>& DataBlock()       {return mNDBlock;}
 | 
|---|
| 80 |   inline const  NDataBlock<T>& DataBlock() const {return mNDBlock;}
 | 
|---|
| 81 | 
 | 
|---|
| 82 |   // Temporaire?
 | 
|---|
| 83 |   inline bool   IsTemp(void) const {return mNDBlock.IsTemp();}
 | 
|---|
| 84 |   inline void   SetTemp(bool temp=false) const {mNDBlock.SetTemp(temp);}
 | 
|---|
| 85 | 
 | 
|---|
| 86 | // Operations diverses  = , +=, ...
 | 
|---|
| 87 | // Conversion en type T, if Size() == 1
 | 
|---|
| 88 |   inline T toScalar();
 | 
|---|
| 89 | // Met les elements a une suite de valeurs
 | 
|---|
| 90 |   virtual TArray<T>&  Set(Sequence seq);
 | 
|---|
| 91 |   inline  TArray<T>&  operator = (Sequence seq)    { return Set(seq); }
 | 
|---|
| 92 | // A = x (tous les elements a x)
 | 
|---|
| 93 |   virtual TArray<T>&  Set(T x);
 | 
|---|
| 94 |   inline  TArray<T>&  operator = (T x)             { return Set(x); }
 | 
|---|
| 95 | // A += -= *= /= x (ajoute, soustrait, ... x a tous les elements)
 | 
|---|
| 96 |   virtual TArray<T>&  Add(T x);
 | 
|---|
| 97 |   inline  TArray<T>&  operator += (T x)            { return Add(x); }
 | 
|---|
| 98 |   virtual TArray<T>&  Sub(T x);
 | 
|---|
| 99 |   inline  TArray<T>&  operator -= (T x)            { return Sub(x); }
 | 
|---|
| 100 |   virtual TArray<T>&  Mul(T x);
 | 
|---|
| 101 |   inline  TArray<T>&  operator *= (T x)            { return Mul(x); }
 | 
|---|
| 102 |   virtual TArray<T>&  Div(T x);
 | 
|---|
| 103 |   inline  TArray<T>&  operator /= (T x)            { return Div(x); }
 | 
|---|
| 104 |   virtual TArray<T>&  SubInv(T x);  //   A ---> x-A
 | 
|---|
| 105 |   virtual TArray<T>&  DivInv(T x);  //   A ---> x/A
 | 
|---|
| 106 | 
 | 
|---|
| 107 | // A += -=  (ajoute, soustrait element par element les deux tableaux )
 | 
|---|
| 108 |   virtual TArray<T>&  AddElt(const TArray<T>& a);
 | 
|---|
| 109 |   inline  TArray<T>&  operator += (const TArray<T>& a)  { return AddElt(a); }
 | 
|---|
| 110 |   virtual TArray<T>&  SubElt(const TArray<T>& a);
 | 
|---|
| 111 |   inline  TArray<T>&  operator -= (const TArray<T>& a)  { return SubElt(a); }
 | 
|---|
| 112 | // Multiplication, division element par element les deux tableaux 
 | 
|---|
| 113 |   virtual TArray<T>&  MulElt(const TArray<T>& a);
 | 
|---|
| 114 |   virtual TArray<T>&  DivElt(const TArray<T>& a);
 | 
|---|
| 115 | // Recopie des valeurs, element par element
 | 
|---|
| 116 |   virtual TArray<T>&  CopyElt(const TArray<T>& a);
 | 
|---|
| 117 | 
 | 
|---|
| 118 | // Somme et produit des elements
 | 
|---|
| 119 |   virtual T Sum() const ;
 | 
|---|
| 120 |   virtual T Product() const ;
 | 
|---|
| 121 | 
 | 
|---|
| 122 | // Impression, I/O, ...
 | 
|---|
| 123 |   virtual string DataType() const;   // definition of abstract method inherited from BaseArray
 | 
|---|
| 124 |   virtual void  Print(ostream& os, int_4 maxprt=-1, bool si=false) const ;
 | 
|---|
| 125 | 
 | 
|---|
| 126 | //  Pour la gestion de persistance
 | 
|---|
| 127 |   friend class  FIO_TArray<T>;
 | 
|---|
| 128 | 
 | 
|---|
| 129 | protected:
 | 
|---|
| 130 |   // partage les donnees si "a" temporaire, clone sinon.
 | 
|---|
| 131 |   void CloneOrShare(const TArray<T>& a);
 | 
|---|
| 132 |   // Share: partage les donnees de "a"
 | 
|---|
| 133 |   void Share(const TArray<T>& a);
 | 
|---|
| 134 | 
 | 
|---|
| 135 |   NDataBlock<T> mNDBlock;      // Le bloc des donnees
 | 
|---|
| 136 | };
 | 
|---|
| 137 | 
 | 
|---|
| 138 | ////////////////////////////////////////////////////////////////
 | 
|---|
| 139 | //   Surcharge d'operateur << 
 | 
|---|
| 140 | template <class T>
 | 
|---|
| 141 | inline ostream& operator << (ostream& os, const TArray<T>& a)
 | 
|---|
| 142 |                             { a.Print(os);    return(os);    }
 | 
|---|
| 143 | 
 | 
|---|
| 144 | ////////////////////////////////////////////////////////////////
 | 
|---|
| 145 | // Surcharge d'operateurs A (+,-,*,/) (T) x
 | 
|---|
| 146 | 
 | 
|---|
| 147 | template <class T> inline TArray<T> operator + (const TArray<T>& a, T b)
 | 
|---|
| 148 |     {TArray<T> result(a); result.SetTemp(true); result.Add(b); return result;}
 | 
|---|
| 149 | 
 | 
|---|
| 150 | template <class T> inline TArray<T> operator + (T b,const TArray<T>& a)
 | 
|---|
| 151 |     {TArray<T> result(a); result.SetTemp(true); result.Add(b); return result;}
 | 
|---|
| 152 | 
 | 
|---|
| 153 | template <class T> inline TArray<T> operator - (const TArray<T>& a, T b)
 | 
|---|
| 154 |     {TArray<T> result(a); result.SetTemp(true); result.Sub(b); return result;}
 | 
|---|
| 155 | 
 | 
|---|
| 156 | template <class T> inline TArray<T> operator - (T b,const TArray<T>& a)
 | 
|---|
| 157 |     {TArray<T> result(a); result.SetTemp(true); result.SubInv(b); return result;}
 | 
|---|
| 158 | 
 | 
|---|
| 159 | template <class T> inline TArray<T> operator * (const TArray<T>& a, T b)
 | 
|---|
| 160 |     {TArray<T> result(a); result.SetTemp(true); result.Mul(b); return result;}
 | 
|---|
| 161 | 
 | 
|---|
| 162 | template <class T> inline TArray<T> operator * (T b,const TArray<T>& a)
 | 
|---|
| 163 |     {TArray<T> result(a); result.SetTemp(true); result.Mul(b); return result;}
 | 
|---|
| 164 | 
 | 
|---|
| 165 | template <class T> inline TArray<T> operator / (const TArray<T>& a, T b)
 | 
|---|
| 166 |     {TArray<T> result(a); result.SetTemp(true); result.DivInv(b); return result;}
 | 
|---|
| 167 | 
 | 
|---|
| 168 | ////////////////////////////////////////////////////////////////
 | 
|---|
| 169 | // Surcharge d'operateurs C = A (+,-) B
 | 
|---|
| 170 | 
 | 
|---|
| 171 | template <class T>
 | 
|---|
| 172 | inline TArray<T> operator + (const TArray<T>& a,const TArray<T>& b)
 | 
|---|
| 173 |     {TArray<T> result(a); result.SetTemp(true); result.AddElt(b); return result;}
 | 
|---|
| 174 | 
 | 
|---|
| 175 | template <class T>
 | 
|---|
| 176 | inline TArray<T> operator - (const TArray<T>& a,const TArray<T>& b)
 | 
|---|
| 177 |     {TArray<T> result(a); result.SetTemp(true); result.SubElt(b); return result;}
 | 
|---|
| 178 | 
 | 
|---|
| 179 | 
 | 
|---|
| 180 | // --------------------------------------------------
 | 
|---|
| 181 | //        inline element acces methods
 | 
|---|
| 182 | // --------------------------------------------------
 | 
|---|
| 183 | template <class T>
 | 
|---|
| 184 | inline T const& TArray<T>::Elem(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu) const
 | 
|---|
| 185 | {
 | 
|---|
| 186 |   return ( *( mNDBlock.Begin()+ offset_+ 
 | 
|---|
| 187 |               ix*step_[0] + iy*step_[1] + iz*step_[2] +
 | 
|---|
| 188 |               it*step_[3] + iu*step_[4]) );
 | 
|---|
| 189 | }
 | 
|---|
| 190 | 
 | 
|---|
| 191 | template <class T>
 | 
|---|
| 192 | inline T & TArray<T>::Elem(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu)
 | 
|---|
| 193 | {
 | 
|---|
| 194 |   return ( *( mNDBlock.Begin()+ offset_+ 
 | 
|---|
| 195 |               ix*step_[0] + iy*step_[1] + iz*step_[2] +
 | 
|---|
| 196 |               it*step_[3] + iu*step_[4]) );
 | 
|---|
| 197 | }
 | 
|---|
| 198 | 
 | 
|---|
| 199 | template <class T>
 | 
|---|
| 200 | inline T const& TArray<T>::ElemCheckBound(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu) const
 | 
|---|
| 201 | {
 | 
|---|
| 202 |   CheckBound(ix, iy, iz, it, iu, 4);
 | 
|---|
| 203 |   return(Elem(ix, iy, iz, it, iu));
 | 
|---|
| 204 | }
 | 
|---|
| 205 | 
 | 
|---|
| 206 | template <class T>
 | 
|---|
| 207 | inline T & TArray<T>::ElemCheckBound(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu)
 | 
|---|
| 208 | {
 | 
|---|
| 209 |   CheckBound(ix, iy, iz, it, iu, 4);
 | 
|---|
| 210 |   return(Elem(ix, iy, iz, it, iu));
 | 
|---|
| 211 | }
 | 
|---|
| 212 | 
 | 
|---|
| 213 | template <class T>
 | 
|---|
| 214 | inline T const& TArray<T>::operator()(uint_4 ix, uint_4 iy, uint_4 iz) const
 | 
|---|
| 215 | {
 | 
|---|
| 216 | #ifdef SO_BOUNDCHECKING
 | 
|---|
| 217 |   CheckBound(ix, iy, iz, 0, 0, 4);
 | 
|---|
| 218 | #endif
 | 
|---|
| 219 |   return ( *( mNDBlock.Begin()+ offset_+ 
 | 
|---|
| 220 |               ix*step_[0] + iy*step_[1] + iz*step_[2]) );
 | 
|---|
| 221 | }
 | 
|---|
| 222 | 
 | 
|---|
| 223 | template <class T>
 | 
|---|
| 224 | inline T & TArray<T>::operator()(uint_4 ix, uint_4 iy, uint_4 iz) 
 | 
|---|
| 225 | {
 | 
|---|
| 226 | #ifdef SO_BOUNDCHECKING
 | 
|---|
| 227 |   CheckBound(ix, iy, iz, 0, 0, 4);
 | 
|---|
| 228 | #endif
 | 
|---|
| 229 |   return ( *( mNDBlock.Begin()+ offset_+ 
 | 
|---|
| 230 |               ix*step_[0] + iy*step_[1] + iz*step_[2]) );
 | 
|---|
| 231 | }
 | 
|---|
| 232 | 
 | 
|---|
| 233 | template <class T>
 | 
|---|
| 234 | inline T const& TArray<T>::operator()(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu) const
 | 
|---|
| 235 | {
 | 
|---|
| 236 | #ifdef SO_BOUNDCHECKING
 | 
|---|
| 237 |   CheckBound(ix, iy, iz, it, iu, 4);
 | 
|---|
| 238 | #endif
 | 
|---|
| 239 |   return ( *( mNDBlock.Begin()+ offset_+ 
 | 
|---|
| 240 |               ix*step_[0] + iy*step_[1] + iz*step_[2] +
 | 
|---|
| 241 |               it*step_[3] + iu*step_[4]) );
 | 
|---|
| 242 | }
 | 
|---|
| 243 | 
 | 
|---|
| 244 | template <class T>
 | 
|---|
| 245 | inline T & TArray<T>::operator()(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu)
 | 
|---|
| 246 | {
 | 
|---|
| 247 | #ifdef SO_BOUNDCHECKING
 | 
|---|
| 248 |   CheckBound(ix, iy, iz, it, iu, 4);
 | 
|---|
| 249 | #endif
 | 
|---|
| 250 |   return ( *( mNDBlock.Begin()+ offset_+ 
 | 
|---|
| 251 |               ix*step_[0] + iy*step_[1] + iz*step_[2] +
 | 
|---|
| 252 |               it*step_[3] + iu*step_[4]) );
 | 
|---|
| 253 | }
 | 
|---|
| 254 | 
 | 
|---|
| 255 | 
 | 
|---|
| 256 | template <class T>
 | 
|---|
| 257 | inline T const& TArray<T>::operator[](uint_8 ip) const
 | 
|---|
| 258 | {
 | 
|---|
| 259 | #ifdef SO_BOUNDCHECKING
 | 
|---|
| 260 |   if (ip >= totsize_)  throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
 | 
|---|
| 261 | #endif
 | 
|---|
| 262 | return *(mNDBlock.Begin()+Offset(ip));
 | 
|---|
| 263 | }
 | 
|---|
| 264 | 
 | 
|---|
| 265 | template <class T>
 | 
|---|
| 266 | inline T & TArray<T>::operator[](uint_8 ip) 
 | 
|---|
| 267 | {
 | 
|---|
| 268 | #ifdef SO_BOUNDCHECKING
 | 
|---|
| 269 |   if (ip >= totsize_)  throw( ParmError("TArray<T>::operator[] Out-of-bound Error") );
 | 
|---|
| 270 | #endif
 | 
|---|
| 271 | return *(mNDBlock.Begin()+Offset(ip));
 | 
|---|
| 272 | }
 | 
|---|
| 273 | 
 | 
|---|
| 274 | 
 | 
|---|
| 275 | template <class T>
 | 
|---|
| 276 | inline T TArray<T>::toScalar()
 | 
|---|
| 277 | { 
 | 
|---|
| 278 |   if (Size() != 1) throw(SzMismatchError("TArray<T>::operator T() Size() != 1")) ;
 | 
|---|
| 279 |   return ( (*this)[0] );
 | 
|---|
| 280 | }
 | 
|---|
| 281 | 
 | 
|---|
| 282 | // Typedef pour simplifier
 | 
|---|
| 283 | typedef TArray<r_8> Array;
 | 
|---|
| 284 | 
 | 
|---|
| 285 | } // Fin du namespace
 | 
|---|
| 286 | 
 | 
|---|
| 287 | #endif
 | 
|---|