[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"
|
---|
| 11 | #include <iostream.h>
|
---|
[245] | 12 |
|
---|
[552] | 13 | namespace SOPHYA {
|
---|
[268] | 14 |
|
---|
[944] | 15 | ////////////////////////////////////////////////////////////////
|
---|
| 16 | //// ------------------- Class Bridge ----------------------- //
|
---|
| 17 | ////////////////////////////////////////////////////////////////
|
---|
| 18 | /*!
|
---|
| 19 | \class Bridge
|
---|
| 20 | \ingroup SysTools
|
---|
| 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 |
|
---|
[944] | 36 | ////////////////////////////////////////////////////////////////
|
---|
| 37 | //// ----------------- Class NDataBlock --------------------- //
|
---|
| 38 | ////////////////////////////////////////////////////////////////
|
---|
| 39 |
|
---|
[246] | 40 | // classe de container avec partage de reference
|
---|
[944] | 41 | //! Container of data with reference sharing
|
---|
[245] | 42 | template <class T>
|
---|
[268] | 43 | class NDataBlock : public AnyDataObj {
|
---|
[245] | 44 |
|
---|
| 45 | public:
|
---|
| 46 |
|
---|
[944] | 47 | // Methodes statiques pour debug.
|
---|
| 48 | static void SetPrintDebug(int prtdbglevel=1);
|
---|
| 49 | static void ResetDebug(size_t nallocdata=0, size_t nallocsref=0);
|
---|
| 50 | static void PrintDebug();
|
---|
| 51 |
|
---|
[246] | 52 | // Creation / destruction
|
---|
[245] | 53 | NDataBlock(size_t n);
|
---|
| 54 | NDataBlock(size_t n, T* data, Bridge* br=NULL);
|
---|
| 55 | NDataBlock();
|
---|
[268] | 56 | NDataBlock(const NDataBlock<T>& a);
|
---|
| 57 | NDataBlock(const NDataBlock<T>& a,bool share);
|
---|
[245] | 58 | virtual ~NDataBlock();
|
---|
| 59 |
|
---|
| 60 | // Temporaire?
|
---|
[944] | 61 | //! Return true if data block is temporay
|
---|
[268] | 62 | inline bool IsTemp(void) const {return mIsTemp;}
|
---|
[944] | 63 | //! Set temporary caracter of data block
|
---|
[289] | 64 | inline void SetTemp(bool temp=false) const {mIsTemp = temp;}
|
---|
[245] | 65 |
|
---|
[257] | 66 | // Gestion taille/Remplissage
|
---|
[268] | 67 | void Clone(const NDataBlock<T>& a);
|
---|
[289] | 68 | void CloneOrShare(const NDataBlock<T>& a);
|
---|
| 69 | void Share(const NDataBlock<T>& a);
|
---|
[257] | 70 | void FillFrom(size_t n,T* data);
|
---|
[944] | 71 | //! Re-set all data values to \b v
|
---|
[289] | 72 | inline void Reset(T v=0)
|
---|
| 73 | {if(mSz==0) return; T *p=Begin(),*pe=End(); while(p<pe) *p++=v;}
|
---|
[502] | 74 |
|
---|
| 75 | // ReSize redimmensionne une structure pour "n" donnees.
|
---|
| 76 | // Les donnees precedentes sont perdues (pour cette classe)
|
---|
| 77 | // et le nouveau tableau mis a zero. La nouvelle structure de
|
---|
| 78 | // donnees n'a qu'une reference (celle de cette classe).
|
---|
[944] | 79 | //! Re-size the data structure
|
---|
| 80 | /*! Old datas are lost (for this class). The new values are set to zero.
|
---|
| 81 | The new data structure has only one reference (itself!). */
|
---|
[289] | 82 | inline void ReSize(size_t n) {Alloc(n);}
|
---|
[502] | 83 |
|
---|
| 84 | void Realloc(size_t nnew,bool force=false);
|
---|
[245] | 85 |
|
---|
| 86 | // Informations pointeur/data
|
---|
[944] | 87 | //! Return pointer on data structure.
|
---|
[245] | 88 | inline T* Data()
|
---|
| 89 | {if(mSRef) return mSRef->data; else return NULL;}
|
---|
[944] | 90 | //! Return pointer on data structure.
|
---|
[268] | 91 | inline T* Data() const
|
---|
| 92 | {if(mSRef) return mSRef->data; else return NULL;}
|
---|
[944] | 93 | //! Return the size of the data structure
|
---|
[249] | 94 | inline size_t Size() const {return mSz;}
|
---|
[944] | 95 | //! Return the \b i th element of the data structure
|
---|
[268] | 96 | inline T& operator()(size_t i) {return *(mSRef->data+i);}
|
---|
[944] | 97 | //! Return the \b i th element of the data structure
|
---|
[268] | 98 | inline T operator()(size_t i) const {return *(mSRef->data+i);}
|
---|
[944] | 99 | //! Return pointer to the beginning of the data structure.
|
---|
[245] | 100 | inline T* Begin() {return mSRef->data;}
|
---|
[944] | 101 | //! Return pointer to the beginning of the data structure.
|
---|
[268] | 102 | inline T const* Begin() const {return mSRef->data;}
|
---|
[944] | 103 | //! Return pointer to the end of the data structure.
|
---|
[245] | 104 | inline T* End() {return mSRef->data+mSz;}
|
---|
[944] | 105 | //! Return pointer to the end of the data structure.
|
---|
[245] | 106 | inline T const* End() const {return mSRef->data+mSz;}
|
---|
[944] | 107 | //! Return the number of references to the data structure
|
---|
[502] | 108 | inline size_t NRef() const {if(mSRef) return 0; else return mSRef->nref;}
|
---|
[245] | 109 |
|
---|
| 110 | // Impression
|
---|
[268] | 111 | void Print(ostream& os, size_t i1=0,size_t n=10) const;
|
---|
[944] | 112 | //! print infos and datas (from \b i1 to \b i2) on stdout.
|
---|
[268] | 113 | inline void Print(size_t i1=0,size_t n=0) const {Print(cout,i1,n);}
|
---|
[245] | 114 |
|
---|
[268] | 115 | //
|
---|
| 116 | T Sum(size_t i1=0,size_t n=0) const;
|
---|
| 117 | T Product(size_t i1=0,size_t n=0) const;
|
---|
| 118 |
|
---|
[289] | 119 | // Surcharge d'operateurs INPLACE: A = x , A = B , A @= x , A @= B
|
---|
[268] | 120 | NDataBlock<T>& operator = (const NDataBlock<T>& a);
|
---|
[245] | 121 | NDataBlock<T>& operator = (T v);
|
---|
| 122 |
|
---|
| 123 | NDataBlock<T>& operator += (T b);
|
---|
| 124 | NDataBlock<T>& operator -= (T b);
|
---|
| 125 | NDataBlock<T>& operator *= (T b);
|
---|
| 126 | NDataBlock<T>& operator /= (T b);
|
---|
| 127 |
|
---|
[268] | 128 | NDataBlock<T>& operator += (const NDataBlock<T>& a);
|
---|
| 129 | NDataBlock<T>& operator -= (const NDataBlock<T>& a);
|
---|
| 130 | NDataBlock<T>& operator *= (const NDataBlock<T>& a);
|
---|
| 131 | NDataBlock<T>& operator /= (const NDataBlock<T>& a);
|
---|
[245] | 132 |
|
---|
[289] | 133 | // Surcharge d'operateurs: C = A @ x , C = A @ B
|
---|
[268] | 134 | NDataBlock<T> Add(T b) const;
|
---|
[976] | 135 | NDataBlock<T> Sub(T b,bool fginv=false) const;
|
---|
[268] | 136 | NDataBlock<T> Mul(T b) const;
|
---|
[976] | 137 | NDataBlock<T> Div(T b,bool fginv=false) const;
|
---|
[245] | 138 |
|
---|
[268] | 139 | NDataBlock<T> Add(const NDataBlock<T>& b) const;
|
---|
| 140 | NDataBlock<T> Sub(const NDataBlock<T>& b) const;
|
---|
| 141 | NDataBlock<T> Mul(const NDataBlock<T>& b) const;
|
---|
| 142 | NDataBlock<T> Div(const NDataBlock<T>& b) const;
|
---|
[257] | 143 |
|
---|
[1426] | 144 | inline uint_8 DRefId() { return mSRef->dsid; }
|
---|
| 145 |
|
---|
[245] | 146 | protected:
|
---|
[944] | 147 | //! NDREF structure for reference management
|
---|
| 148 | typedef struct {
|
---|
| 149 | size_t nref; //!< Number of references to the data structure
|
---|
[1426] | 150 | uint_8 dsid; //!< Data structure Id - Used by FIO_NDataBlock
|
---|
[944] | 151 | T* data; //!< Pointer to data structure itself
|
---|
| 152 | Bridge* bridge; //!< Pointer to a bridge for the data structure
|
---|
| 153 | } NDREF;
|
---|
[245] | 154 |
|
---|
[773] | 155 | void Alloc(size_t n,T* data=NULL,Bridge* br=NULL, bool zero=true);
|
---|
[245] | 156 | void Delete(void);
|
---|
| 157 |
|
---|
[944] | 158 | static int Debug_NDataBlock; //!< DEBUG: set debug level (all type<T> classes)
|
---|
| 159 | static size_t NallocData; //!< DEBUG: number of allocations (all type<T> classes)
|
---|
| 160 | static size_t NallocSRef; //!< DEBUG: number of references (all type<T> classes)
|
---|
| 161 |
|
---|
| 162 | size_t mSz; //!< size of data structure
|
---|
| 163 | NDREF* mSRef; //!< NDREF structure for reference management
|
---|
| 164 | mutable bool mIsTemp; //!< true if class is temporary
|
---|
[245] | 165 | };
|
---|
| 166 |
|
---|
[944] | 167 | //! Define operator \<\< for printing
|
---|
[259] | 168 | template<class T>
|
---|
[268] | 169 | inline ostream& operator << (ostream& os, const NDataBlock<T>& a)
|
---|
[269] | 170 | {a.Print(os); return(os);}
|
---|
[944] | 171 |
|
---|
| 172 | //! Add a constant to datas and return NDataBlock : ND = NDa + b
|
---|
[268] | 173 | template<class T>
|
---|
| 174 | inline NDataBlock<T> operator + (const NDataBlock<T>& a,T b)
|
---|
[259] | 175 | {return a.Add(b);}
|
---|
[944] | 176 | //! Add a constant to datas and return NDataBlock : ND = b + NDa
|
---|
[259] | 177 | template<class T>
|
---|
[268] | 178 | inline NDataBlock<T> operator + (T b,const NDataBlock<T>& a)
|
---|
[259] | 179 | {return a.Add(b);}
|
---|
[944] | 180 | //! Substract a constant to datas and return NDataBlock : ND = NDa - b
|
---|
[259] | 181 | template<class T>
|
---|
[268] | 182 | inline NDataBlock<T> operator - (const NDataBlock<T>& a,T b)
|
---|
[259] | 183 | {return a.Sub(b);}
|
---|
[944] | 184 | //! Substract a constant to datas and return NDataBlock : ND = b - NDa
|
---|
[259] | 185 | template<class T>
|
---|
[268] | 186 | inline NDataBlock<T> operator - (T b,const NDataBlock<T>& a)
|
---|
[976] | 187 | {return a.Sub(b,true);}
|
---|
[944] | 188 | //! Multiply datas by a constant and return NDataBlock : ND = NDa * b
|
---|
[259] | 189 | template<class T>
|
---|
[268] | 190 | inline NDataBlock<T> operator * (const NDataBlock<T>& a,T b)
|
---|
[259] | 191 | {return a.Mul(b);}
|
---|
[944] | 192 | //! Multiply datas by a constant 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.Mul(b);}
|
---|
[944] | 196 | //! Divide datas by a constant 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.Div(b);}
|
---|
[944] | 200 | //! Divide a constant by 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.Div(b,true);}
|
---|
[245] | 204 |
|
---|
[944] | 205 | //! Add datas of two data blocks and return NDataBlock : ND = NDa + NDb
|
---|
[259] | 206 | template<class T>
|
---|
[268] | 207 | inline NDataBlock<T> operator + (const NDataBlock<T>& a,const NDataBlock<T>& b)
|
---|
[265] | 208 | {return a.Add(b);}
|
---|
[944] | 209 | //! Substract datas of two data blocks and return NDataBlock : ND = NDa - NDb
|
---|
[259] | 210 | template<class T>
|
---|
[268] | 211 | inline NDataBlock<T> operator - (const NDataBlock<T>& a,const NDataBlock<T>& b)
|
---|
[259] | 212 | {return a.Sub(b);}
|
---|
[944] | 213 | //! Multiply datas of two data blocks and return NDataBlock : ND = NDa * NDb
|
---|
[259] | 214 | template<class T>
|
---|
[268] | 215 | inline NDataBlock<T> operator * (const NDataBlock<T>& a,const NDataBlock<T>& b)
|
---|
[259] | 216 | {return a.Mul(b);}
|
---|
[944] | 217 | //! Divide datas of two data blocks and return NDataBlock : ND = NDa / NDb
|
---|
[259] | 218 | template<class T>
|
---|
[268] | 219 | inline NDataBlock<T> operator / (const NDataBlock<T>& a,const NDataBlock<T>& b)
|
---|
[259] | 220 | {return a.Div(b);}
|
---|
[245] | 221 |
|
---|
[268] | 222 | } // Fin du namespace
|
---|
[257] | 223 |
|
---|
[245] | 224 | #endif
|
---|