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