| 1 | #ifndef ARRCTCAST_SEEN | 
|---|
| 2 | #define ARRCTCAST_SEEN | 
|---|
| 3 | //---------------------------------------------------- | 
|---|
| 4 | //    SOPHYA class library | 
|---|
| 5 | // (C) LAL-IN2P3/CNRS         (C) DAPNIA-CEA | 
|---|
| 6 | //             R. Ansari - Oct 2006 | 
|---|
| 7 | //---------------------------------------------------- | 
|---|
| 8 |  | 
|---|
| 9 | #include "tarray.h" | 
|---|
| 10 |  | 
|---|
| 11 | namespace SOPHYA { | 
|---|
| 12 |  | 
|---|
| 13 | //----------------------------------------------------------------------- | 
|---|
| 14 | // Classe de Bridge et fonctions utilitaires pour permettre des | 
|---|
| 15 | // cast de type de donnees - sans conversion - pour des tableaux (TArray) | 
|---|
| 16 | //----------------------------------------------------------------------- | 
|---|
| 17 |  | 
|---|
| 18 | //****************************************** | 
|---|
| 19 | // Classe NDCBridge : Bridge sur NDataBlock, utile pour cast - sans conversion - de type | 
|---|
| 20 | /*! | 
|---|
| 21 | \class NDCBridge | 
|---|
| 22 | \brief Special Bridge class to be used the ArrayCast class | 
|---|
| 23 | \ingroup TArray | 
|---|
| 24 | */ | 
|---|
| 25 | template <class T> | 
|---|
| 26 | class NDCBridge : public Bridge { | 
|---|
| 27 | public: | 
|---|
| 28 | NDCBridge(NDataBlock< T > & db) : dbcs_(db) { | 
|---|
| 29 | //DBG cout << "+++ NDCBridge() this= " << hex << (long int)this << dec << endl; | 
|---|
| 30 | } | 
|---|
| 31 | virtual ~NDCBridge() { | 
|---|
| 32 | //DBG cout << "--- ~NDCBridge() this= " << hex << (long int)this << dec << endl; | 
|---|
| 33 | } | 
|---|
| 34 | protected: | 
|---|
| 35 | NDataBlock< T > dbcs_; | 
|---|
| 36 | }; | 
|---|
| 37 |  | 
|---|
| 38 | //****************************************** | 
|---|
| 39 | // Fonction de cast de type de contenu - sans conversion - de tableaux | 
|---|
| 40 | // pour tableau packe en memoire | 
|---|
| 41 | /*! | 
|---|
| 42 | \ingroup TArray | 
|---|
| 43 | \brief Function to handle Array content cast without conversion (shared data) | 
|---|
| 44 | */ | 
|---|
| 45 | template <class T1, class T2> | 
|---|
| 46 | TArray< T2 > ArrayCast( TArray< T1 > & a, T2 ct, sa_size_t doff=0, sa_size_t dstep=1) | 
|---|
| 47 | { | 
|---|
| 48 | NDCBridge< T1 > * db = new NDCBridge< T1 >(a.DataBlock()); | 
|---|
| 49 | //      if (a.AvgStep() != 1) | 
|---|
| 50 | if (a.IsPacked() == false) | 
|---|
| 51 | throw ParmError("ArrayCast - Pb: Not a packed array"); | 
|---|
| 52 | float rs = (float)sizeof(T1)/(float)sizeof(T2); | 
|---|
| 53 | //DBG cout << "--DBG-ArrayCast SizeOf T1= " << sizeof(T1) | 
|---|
| 54 | //DBG     << " T2= " << sizeof(T2) << " rs=T1/T2= " << rs << endl; | 
|---|
| 55 |  | 
|---|
| 56 | int_4 ndim = a.Rank(); | 
|---|
| 57 | sa_size_t sz[BASEARRAY_MAXNDIMS]; | 
|---|
| 58 | for(int_4 k=0; k<BASEARRAY_MAXNDIMS; k++)   sz[k] = 0; | 
|---|
| 59 | for(int_4 k=0; k<ndim; k++)  sz[k] = a.Size(k); | 
|---|
| 60 | // Pour gerer correctement les vecteurs colonnes | 
|---|
| 61 | int kszsc = 0; | 
|---|
| 62 | if ((sz[0] == 1) && (sz[1] > 1))   kszsc = 1; | 
|---|
| 63 | if (dstep > 1) | 
|---|
| 64 | sz[kszsc] = sz[kszsc]*sizeof(T1)/sizeof(T2)/dstep; | 
|---|
| 65 | else | 
|---|
| 66 | sz[kszsc] = sz[kszsc]*sizeof(T1)/sizeof(T2); | 
|---|
| 67 | //DBG  cout << " --DBG-ArrayCast SzX=" << a.SizeX() | 
|---|
| 68 | //     << " rs=" << rs << " dstep=" << dstep << " --> SzX=" << sz[0] << endl; | 
|---|
| 69 |  | 
|---|
| 70 | sa_size_t step = dstep; | 
|---|
| 71 | sa_size_t off = doff; | 
|---|
| 72 | TArray< T2 > ac(ndim, sz, (T2 *)(a.Data()), step, off, db); | 
|---|
| 73 | ac.SetMemoryMapping(a.GetMemoryMapping()); | 
|---|
| 74 | ac.SetTemp(true); | 
|---|
| 75 | return ac; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 |  | 
|---|
| 79 | //****************************************** | 
|---|
| 80 | // cast de contenu - sans conversion - tableau complex vers float | 
|---|
| 81 | /*! | 
|---|
| 82 | \ingroup TArray | 
|---|
| 83 | \brief Complex to real array cast - without conversion (shared data) | 
|---|
| 84 | */ | 
|---|
| 85 | template <class T> | 
|---|
| 86 | TArray<T> ArrCastC2R(TArray< complex<T> > & a) | 
|---|
| 87 | { | 
|---|
| 88 | T x = 0; | 
|---|
| 89 | return ArrayCast(a, x); | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | //****************************************** | 
|---|
| 93 | // cast de contenu - sans conversion - tableau float vers complex | 
|---|
| 94 | /*! | 
|---|
| 95 | \ingroup TArray | 
|---|
| 96 | \brief Real to complex array cast - without conversion (shared data) | 
|---|
| 97 | */ | 
|---|
| 98 | template <class T> | 
|---|
| 99 | TArray< complex<T> > ArrCastR2C(TArray< T > & a) | 
|---|
| 100 | { | 
|---|
| 101 | complex<T> x = 0; | 
|---|
| 102 | //  return ArrayCast< TArray< T > , complex<T> > (a, x); | 
|---|
| 103 | return ArrayCast(a, x); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 |  | 
|---|
| 107 | //****************************************** | 
|---|
| 108 | // Acces a la partie reelle d'un tableau complex - en shared data | 
|---|
| 109 | // To access real part of a complex array (shared data) | 
|---|
| 110 | /*! | 
|---|
| 111 | \ingroup TArray | 
|---|
| 112 | \brief Return the real part of a complex array (shared data) | 
|---|
| 113 | */ | 
|---|
| 114 | template <class T> | 
|---|
| 115 | TArray<T> SDRealPart(TArray< complex<T> > & a) | 
|---|
| 116 | { | 
|---|
| 117 | T x = 0; | 
|---|
| 118 | return ArrayCast(a, x, 0, 2); | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | //****************************************** | 
|---|
| 122 | // Acces a la partie imaginaire d'un tableau complex - en shared data | 
|---|
| 123 | // To access imaginary part of a complex array (shared data) | 
|---|
| 124 | /*! | 
|---|
| 125 | \ingroup TArray | 
|---|
| 126 | \brief Return the imaginary part of a complex array (shared data) | 
|---|
| 127 | */ | 
|---|
| 128 | template <class T> | 
|---|
| 129 | TArray<T> SDImagPart(TArray< complex<T> > & a) | 
|---|
| 130 | { | 
|---|
| 131 | T x = 0; | 
|---|
| 132 | return ArrayCast(a, x, 1, 2); | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | } // Fin du namespace | 
|---|
| 136 |  | 
|---|
| 137 | #endif | 
|---|