| [268] | 1 | // This may look like C code, but it is really -*- C++ -*- | 
|---|
|  | 2 | // Gestion de block de donnees avec partage de references | 
|---|
|  | 3 | //                         C.Magneville          04/99 | 
|---|
|  | 4 | // LAL (Orsay) / IN2P3-CNRS  DAPNIA/SPP (Saclay) / CEA | 
|---|
| [245] | 5 | #ifndef NDATABLOCK_H | 
|---|
|  | 6 | #define NDATABLOCK_H | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #include "machdefs.h" | 
|---|
| [948] | 9 | #include <stdlib.h>        /* pour que size_t soit defini */ | 
|---|
| [268] | 10 | #include "anydataobj.h" | 
|---|
| [3831] | 11 | #include <complex> | 
|---|
| [2322] | 12 | #include <iostream> | 
|---|
| [245] | 13 |  | 
|---|
| [552] | 14 | namespace SOPHYA { | 
|---|
| [268] | 15 |  | 
|---|
| [944] | 16 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 17 | //// ------------------- Class Bridge ----------------------- // | 
|---|
|  | 18 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 19 | /*! | 
|---|
|  | 20 | \class Bridge | 
|---|
| [1607] | 21 | \ingroup BaseTools | 
|---|
| [944] | 22 | This class is use by NDataBlock. It allows sharing of datas | 
|---|
|  | 23 | with external structures : by example, if you want to connect | 
|---|
|  | 24 | a Blitz data structure with a NDataBlock or a TMatrix or ... | 
|---|
|  | 25 | \sa NDataBlock | 
|---|
|  | 26 | */ | 
|---|
|  | 27 |  | 
|---|
| [245] | 28 | // Classe pour permettre de partager des donnees avec | 
|---|
|  | 29 | // un autre systeme de gestion de references (ex avec Blitz) | 
|---|
| [944] | 30 | //! Empty class which allows data sharing with external structures (for NDataBlock) | 
|---|
| [245] | 31 | class Bridge { | 
|---|
|  | 32 | public: | 
|---|
|  | 33 | Bridge() { } | 
|---|
|  | 34 | virtual ~Bridge() { } | 
|---|
|  | 35 | }; | 
|---|
|  | 36 |  | 
|---|
| [3212] | 37 |  | 
|---|
|  | 38 | class ThSafeOp; //forward class declaration for ThreadSafe operations (Ref.Count/Share) | 
|---|
|  | 39 |  | 
|---|
| [944] | 40 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 41 | //// ----------------- Class NDataBlock --------------------- // | 
|---|
|  | 42 | //////////////////////////////////////////////////////////////// | 
|---|
| [246] | 43 | // classe de container avec partage de reference | 
|---|
| [944] | 44 | //! Container of data with reference sharing | 
|---|
| [245] | 45 | template <class T> | 
|---|
| [268] | 46 | class NDataBlock : public AnyDataObj { | 
|---|
| [245] | 47 |  | 
|---|
|  | 48 | public: | 
|---|
|  | 49 |  | 
|---|
| [944] | 50 | // Methodes statiques pour debug. | 
|---|
|  | 51 | static void SetPrintDebug(int prtdbglevel=1); | 
|---|
|  | 52 | static void ResetDebug(size_t nallocdata=0, size_t nallocsref=0); | 
|---|
|  | 53 | static void PrintDebug(); | 
|---|
|  | 54 |  | 
|---|
| [246] | 55 | // Creation / destruction | 
|---|
| [2565] | 56 | NDataBlock(size_t n, bool fzero=true); | 
|---|
| [245] | 57 | NDataBlock(size_t n, T* data, Bridge* br=NULL); | 
|---|
|  | 58 | NDataBlock(); | 
|---|
| [268] | 59 | NDataBlock(const NDataBlock<T>& a); | 
|---|
|  | 60 | NDataBlock(const NDataBlock<T>& a,bool share); | 
|---|
| [245] | 61 | virtual ~NDataBlock(); | 
|---|
|  | 62 |  | 
|---|
|  | 63 | // Temporaire? | 
|---|
| [944] | 64 | //! Return true if data block is temporay | 
|---|
| [268] | 65 | inline bool IsTemp(void) const {return mIsTemp;} | 
|---|
| [944] | 66 | //! Set temporary caracter of data block | 
|---|
| [289] | 67 | inline void SetTemp(bool temp=false) const {mIsTemp = temp;} | 
|---|
| [1623] | 68 | // Depuis que le createur par copie partage les donnees | 
|---|
|  | 69 | // la seule utilisation de SetTemp et pour faire les operations du type: | 
|---|
|  | 70 | // NDataBlock = NDataBlock + NDataBlock + NDataBlock ... | 
|---|
| [245] | 71 |  | 
|---|
| [257] | 72 | // Gestion taille/Remplissage | 
|---|
| [268] | 73 | void Clone(const NDataBlock<T>& a); | 
|---|
| [289] | 74 | void CloneOrShare(const NDataBlock<T>& a); | 
|---|
|  | 75 | void Share(const NDataBlock<T>& a); | 
|---|
| [257] | 76 | void FillFrom(size_t n,T* data); | 
|---|
| [944] | 77 | //! Re-set all data values to \b v | 
|---|
| [289] | 78 | inline void Reset(T v=0) | 
|---|
|  | 79 | {if(mSz==0) return; T *p=Begin(),*pe=End(); while(p<pe) *p++=v;} | 
|---|
| [502] | 80 |  | 
|---|
|  | 81 | // ReSize redimmensionne une structure pour "n" donnees. | 
|---|
|  | 82 | // Les donnees precedentes sont perdues (pour cette classe) | 
|---|
| [2565] | 83 | // et le nouveau tableau mis a zero si fzero=true. La nouvelle structure de | 
|---|
| [502] | 84 | // donnees n'a qu'une reference (celle de cette classe). | 
|---|
| [944] | 85 | //! Re-size the data structure | 
|---|
| [2565] | 86 | /*! Old datas are lost (for this class). The new values are set | 
|---|
|  | 87 | to zero if \b fzero=true . | 
|---|
| [944] | 88 | The new data structure has only one reference (itself!). */ | 
|---|
| [2565] | 89 | inline void ReSize(size_t n, bool fzero=true) {Alloc(n,NULL,NULL,fzero);} | 
|---|
| [502] | 90 |  | 
|---|
|  | 91 | void Realloc(size_t nnew,bool force=false); | 
|---|
| [245] | 92 |  | 
|---|
| [3208] | 93 | //  Public, thread safe interface to Delete() (set the size to zero) | 
|---|
|  | 94 | void Dealloc(); | 
|---|
| [3172] | 95 |  | 
|---|
| [245] | 96 | // Informations pointeur/data | 
|---|
| [944] | 97 | //! Return pointer on data structure. | 
|---|
| [245] | 98 | inline T* Data() | 
|---|
|  | 99 | {if(mSRef) return mSRef->data; else return NULL;} | 
|---|
| [944] | 100 | //! Return pointer on data structure. | 
|---|
| [268] | 101 | inline T* Data() const | 
|---|
|  | 102 | {if(mSRef) return mSRef->data; else return NULL;} | 
|---|
| [944] | 103 | //! Return the size of the data structure | 
|---|
| [249] | 104 | inline size_t Size() const    {return mSz;} | 
|---|
| [944] | 105 | //! Return the \b i th element of  the data structure | 
|---|
| [268] | 106 | inline T& operator()(size_t i)       {return *(mSRef->data+i);} | 
|---|
| [944] | 107 | //! Return the \b i th element of  the data structure | 
|---|
| [268] | 108 | inline T  operator()(size_t i) const {return *(mSRef->data+i);} | 
|---|
| [944] | 109 | //! Return pointer to the beginning of the data structure. | 
|---|
| [245] | 110 | inline T*        Begin()        {return mSRef->data;} | 
|---|
| [944] | 111 | //! Return pointer to the beginning of the data structure. | 
|---|
| [268] | 112 | inline T const*  Begin() const  {return mSRef->data;} | 
|---|
| [944] | 113 | //! Return pointer to the end of the data structure. | 
|---|
| [245] | 114 | inline T*        End()          {return mSRef->data+mSz;} | 
|---|
| [944] | 115 | //! Return pointer to the end of the data structure. | 
|---|
| [245] | 116 | inline T const*  End() const    {return mSRef->data+mSz;} | 
|---|
| [944] | 117 | //! Return the number of references to the data structure | 
|---|
| [2657] | 118 | inline size_t NRef() const {if(mSRef) return mSRef->nref;  else return 0; } | 
|---|
| [245] | 119 |  | 
|---|
|  | 120 | // Impression | 
|---|
| [268] | 121 | void Print(ostream& os, size_t i1=0,size_t n=10) const; | 
|---|
| [944] | 122 | //! print infos and datas (from \b i1 to \b i2) on stdout. | 
|---|
| [268] | 123 | inline void Print(size_t i1=0,size_t n=0) const {Print(cout,i1,n);} | 
|---|
| [245] | 124 |  | 
|---|
| [268] | 125 | // | 
|---|
|  | 126 | T Sum(size_t i1=0,size_t n=0) const; | 
|---|
|  | 127 | T Product(size_t i1=0,size_t n=0) const; | 
|---|
|  | 128 |  | 
|---|
| [289] | 129 | // Surcharge d'operateurs INPLACE: A = x , A = B , A @= x , A @= B | 
|---|
| [268] | 130 | NDataBlock<T>& operator = (const NDataBlock<T>& a); | 
|---|
| [245] | 131 | NDataBlock<T>& operator = (T v); | 
|---|
|  | 132 |  | 
|---|
|  | 133 | NDataBlock<T>& operator += (T b); | 
|---|
|  | 134 | NDataBlock<T>& operator -= (T b); | 
|---|
|  | 135 | NDataBlock<T>& operator *= (T b); | 
|---|
|  | 136 | NDataBlock<T>& operator /= (T b); | 
|---|
|  | 137 |  | 
|---|
| [268] | 138 | NDataBlock<T>& operator += (const NDataBlock<T>& a); | 
|---|
|  | 139 | NDataBlock<T>& operator -= (const NDataBlock<T>& a); | 
|---|
|  | 140 | NDataBlock<T>& operator *= (const NDataBlock<T>& a); | 
|---|
|  | 141 | NDataBlock<T>& operator /= (const NDataBlock<T>& a); | 
|---|
| [245] | 142 |  | 
|---|
| [289] | 143 | // Surcharge d'operateurs: C = A @ x , C = A @ B | 
|---|
| [268] | 144 | NDataBlock<T> Add(T b) const; | 
|---|
| [976] | 145 | NDataBlock<T> Sub(T b,bool fginv=false) const; | 
|---|
| [268] | 146 | NDataBlock<T> Mul(T b) const; | 
|---|
| [976] | 147 | NDataBlock<T> Div(T b,bool fginv=false) const; | 
|---|
| [245] | 148 |  | 
|---|
| [268] | 149 | NDataBlock<T> Add(const NDataBlock<T>& b) const; | 
|---|
|  | 150 | NDataBlock<T> Sub(const NDataBlock<T>& b) const; | 
|---|
|  | 151 | NDataBlock<T> Mul(const NDataBlock<T>& b) const; | 
|---|
|  | 152 | NDataBlock<T> Div(const NDataBlock<T>& b) const; | 
|---|
| [257] | 153 |  | 
|---|
| [3172] | 154 | //! Return thye associated object Id (or DataRef Id) | 
|---|
| [3385] | 155 | inline uint_8 DRefId() { return ((mSRef)?mSRef->dsid:0); } | 
|---|
| [3172] | 156 | //! assign a new object Id (or DataRef Id) - useful for PPF write operations | 
|---|
| [3385] | 157 | inline void RenewDRefId() { if (mSRef) mSRef->dsid = AnyDataObj::getUniqueId(); } | 
|---|
| [3172] | 158 | //! assign a new object Id (or DataRef Id) - useful for PPF write operations | 
|---|
| [3385] | 159 | inline void RenewObjId() { if (mSRef) mSRef->dsid = AnyDataObj::getUniqueId(); } | 
|---|
| [3172] | 160 |  | 
|---|
| [1426] | 161 |  | 
|---|
| [245] | 162 | protected: | 
|---|
| [944] | 163 | //! NDREF structure for reference management | 
|---|
|  | 164 | typedef struct { | 
|---|
|  | 165 | size_t nref;      //!< Number of references to the data structure | 
|---|
| [1426] | 166 | uint_8 dsid;      //!< Data structure Id - Used by FIO_NDataBlock | 
|---|
| [944] | 167 | T* data;          //!< Pointer to data structure itself | 
|---|
|  | 168 | Bridge* bridge;   //!< Pointer to a bridge for the data structure | 
|---|
|  | 169 | } NDREF; | 
|---|
| [245] | 170 |  | 
|---|
| [773] | 171 | void Alloc(size_t n,T* data=NULL,Bridge* br=NULL, bool zero=true); | 
|---|
| [245] | 172 | void Delete(void); | 
|---|
|  | 173 |  | 
|---|
| [944] | 174 | static int Debug_NDataBlock; //!< DEBUG: set debug level (all type<T> classes) | 
|---|
|  | 175 | static size_t NallocData; //!< DEBUG: number of allocations (all type<T> classes) | 
|---|
|  | 176 | static size_t NallocSRef; //!< DEBUG: number of references (all type<T> classes) | 
|---|
| [3213] | 177 | static ThSafeOp* gThsop;  //!< Mutex For thread safe operation | 
|---|
| [944] | 178 |  | 
|---|
|  | 179 | size_t       mSz;      //!< size of data structure | 
|---|
|  | 180 | NDREF*       mSRef;    //!< NDREF structure for reference management | 
|---|
|  | 181 | mutable bool mIsTemp;  //!< true if class is temporary | 
|---|
| [245] | 182 | }; | 
|---|
|  | 183 |  | 
|---|
| [944] | 184 | //! Define operator \<\< for printing | 
|---|
| [259] | 185 | template<class T> | 
|---|
| [268] | 186 | inline ostream& operator << (ostream& os, const NDataBlock<T>& a) | 
|---|
| [269] | 187 | {a.Print(os); return(os);} | 
|---|
| [944] | 188 |  | 
|---|
|  | 189 | //! Add a constant to datas and return NDataBlock : ND = NDa + b | 
|---|
| [268] | 190 | template<class T> | 
|---|
|  | 191 | inline NDataBlock<T> operator + (const NDataBlock<T>& a,T b) | 
|---|
| [259] | 192 | {return a.Add(b);} | 
|---|
| [944] | 193 | //! Add a constant to datas and return NDataBlock : ND = b + NDa | 
|---|
| [259] | 194 | template<class T> | 
|---|
| [268] | 195 | inline NDataBlock<T> operator + (T b,const NDataBlock<T>& a) | 
|---|
| [259] | 196 | {return a.Add(b);} | 
|---|
| [944] | 197 | //! Substract a constant to datas and return NDataBlock : ND = NDa - b | 
|---|
| [259] | 198 | template<class T> | 
|---|
| [268] | 199 | inline NDataBlock<T> operator - (const NDataBlock<T>& a,T b) | 
|---|
| [259] | 200 | {return a.Sub(b);} | 
|---|
| [944] | 201 | //! Substract a constant to datas and return NDataBlock : ND = b - NDa | 
|---|
| [259] | 202 | template<class T> | 
|---|
| [268] | 203 | inline NDataBlock<T> operator - (T b,const NDataBlock<T>& a) | 
|---|
| [976] | 204 | {return a.Sub(b,true);} | 
|---|
| [944] | 205 | //! Multiply datas by a constant and return NDataBlock : ND = NDa * b | 
|---|
| [259] | 206 | template<class T> | 
|---|
| [268] | 207 | inline NDataBlock<T> operator * (const NDataBlock<T>& a,T b) | 
|---|
| [259] | 208 | {return a.Mul(b);} | 
|---|
| [944] | 209 | //! Multiply datas by a constant and return NDataBlock : ND = b * NDa | 
|---|
| [259] | 210 | template<class T> | 
|---|
| [268] | 211 | inline NDataBlock<T> operator * (T b,const NDataBlock<T>& a) | 
|---|
| [259] | 212 | {return a.Mul(b);} | 
|---|
| [944] | 213 | //! Divide datas by a constant and return NDataBlock : ND = NDa / b | 
|---|
| [259] | 214 | template<class T> | 
|---|
| [268] | 215 | inline NDataBlock<T> operator / (const NDataBlock<T>& a,T b) | 
|---|
| [259] | 216 | {return a.Div(b);} | 
|---|
| [944] | 217 | //! Divide a constant by datas and return NDataBlock : ND = b / NDa | 
|---|
| [259] | 218 | template<class T> | 
|---|
| [268] | 219 | inline NDataBlock<T> operator / (T b,const NDataBlock<T>& a) | 
|---|
| [976] | 220 | {return a.Div(b,true);} | 
|---|
| [245] | 221 |  | 
|---|
| [944] | 222 | //! Add datas of two data blocks and return NDataBlock : ND = NDa + NDb | 
|---|
| [259] | 223 | template<class T> | 
|---|
| [268] | 224 | inline NDataBlock<T> operator + (const NDataBlock<T>& a,const NDataBlock<T>& b) | 
|---|
| [265] | 225 | {return a.Add(b);} | 
|---|
| [944] | 226 | //! Substract datas of two data blocks and return NDataBlock : ND = NDa - NDb | 
|---|
| [259] | 227 | template<class T> | 
|---|
| [268] | 228 | inline NDataBlock<T> operator - (const NDataBlock<T>& a,const NDataBlock<T>& b) | 
|---|
| [259] | 229 | {return a.Sub(b);} | 
|---|
| [944] | 230 | //! Multiply datas of two data blocks and return NDataBlock : ND = NDa * NDb | 
|---|
| [259] | 231 | template<class T> | 
|---|
| [268] | 232 | inline NDataBlock<T> operator * (const NDataBlock<T>& a,const NDataBlock<T>& b) | 
|---|
| [259] | 233 | {return a.Mul(b);} | 
|---|
| [944] | 234 | //! Divide datas of two data blocks and return NDataBlock : ND = NDa / NDb | 
|---|
| [259] | 235 | template<class T> | 
|---|
| [268] | 236 | inline NDataBlock<T> operator / (const NDataBlock<T>& a,const NDataBlock<T>& b) | 
|---|
| [259] | 237 | {return a.Div(b);} | 
|---|
| [245] | 238 |  | 
|---|
| [3831] | 239 | //--------- extern template declarations (if needed) ----------- | 
|---|
|  | 240 | #if defined ( NEED_EXT_DECL_TEMP ) && !defined( NDATABLOCK_CC_BFILE ) | 
|---|
|  | 241 | extern template class NDataBlock<uint_1>; | 
|---|
|  | 242 | extern template class NDataBlock<uint_2>; | 
|---|
|  | 243 | extern template class NDataBlock<int_1>; | 
|---|
|  | 244 | extern template class NDataBlock<int_2>; | 
|---|
|  | 245 | extern template class NDataBlock<int_4>; | 
|---|
|  | 246 | extern template class NDataBlock<int_8>; | 
|---|
|  | 247 | extern template class NDataBlock<uint_4>; | 
|---|
|  | 248 | extern template class NDataBlock<uint_8>; | 
|---|
|  | 249 | extern template class NDataBlock<r_4>; | 
|---|
|  | 250 | extern template class NDataBlock<r_8>; | 
|---|
|  | 251 | extern template class NDataBlock< complex<r_4> >; | 
|---|
|  | 252 | extern template class NDataBlock< complex<r_8> >; | 
|---|
|  | 253 | #ifdef SO_LDBLE128 | 
|---|
|  | 254 | extern template class NDataBlock<r_16>; | 
|---|
|  | 255 | extern template class NDataBlock< complex<r_16> >; | 
|---|
|  | 256 | #endif | 
|---|
|  | 257 |  | 
|---|
|  | 258 | #endif  // Fin de if defined ( NEED_EXT_DECL_TEMP ) | 
|---|
|  | 259 |  | 
|---|
| [268] | 260 | } // Fin du namespace | 
|---|
| [257] | 261 |  | 
|---|
| [245] | 262 | #endif | 
|---|