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
|
---|
5 | #ifndef NDATABLOCK_H
|
---|
6 | #define NDATABLOCK_H
|
---|
7 |
|
---|
8 | #include "machdefs.h"
|
---|
9 | #include <stdlib.h> /* pour que size_t soit defini */
|
---|
10 | #include "anydataobj.h"
|
---|
11 | #include <complex>
|
---|
12 | #include <iostream>
|
---|
13 |
|
---|
14 | namespace SOPHYA {
|
---|
15 |
|
---|
16 | ////////////////////////////////////////////////////////////////
|
---|
17 | //// ------------------- Class Bridge ----------------------- //
|
---|
18 | ////////////////////////////////////////////////////////////////
|
---|
19 | /*!
|
---|
20 | \class Bridge
|
---|
21 | \ingroup BaseTools
|
---|
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 |
|
---|
28 | // Classe pour permettre de partager des donnees avec
|
---|
29 | // un autre systeme de gestion de references (ex avec Blitz)
|
---|
30 | //! Empty class which allows data sharing with external structures (for NDataBlock)
|
---|
31 | class Bridge {
|
---|
32 | public:
|
---|
33 | Bridge() { }
|
---|
34 | virtual ~Bridge() { }
|
---|
35 | };
|
---|
36 |
|
---|
37 |
|
---|
38 | class ThSafeOp; //forward class declaration for ThreadSafe operations (Ref.Count/Share)
|
---|
39 |
|
---|
40 | ////////////////////////////////////////////////////////////////
|
---|
41 | //// ----------------- Class NDataBlock --------------------- //
|
---|
42 | ////////////////////////////////////////////////////////////////
|
---|
43 | // classe de container avec partage de reference
|
---|
44 | //! Container of data with reference sharing
|
---|
45 | template <class T>
|
---|
46 | class NDataBlock : public AnyDataObj {
|
---|
47 |
|
---|
48 | public:
|
---|
49 |
|
---|
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 |
|
---|
55 | // Creation / destruction
|
---|
56 | NDataBlock(size_t n, bool fzero=true);
|
---|
57 | NDataBlock(size_t n, T* data, Bridge* br=NULL);
|
---|
58 | NDataBlock();
|
---|
59 | NDataBlock(const NDataBlock<T>& a);
|
---|
60 | NDataBlock(const NDataBlock<T>& a,bool share);
|
---|
61 | virtual ~NDataBlock();
|
---|
62 |
|
---|
63 | // Temporaire?
|
---|
64 | //! Return true if data block is temporay
|
---|
65 | inline bool IsTemp(void) const {return mIsTemp;}
|
---|
66 | //! Set temporary caracter of data block
|
---|
67 | inline void SetTemp(bool temp=false) const {mIsTemp = temp;}
|
---|
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 ...
|
---|
71 |
|
---|
72 | // Gestion taille/Remplissage
|
---|
73 | void Clone(const NDataBlock<T>& a);
|
---|
74 | void CloneOrShare(const NDataBlock<T>& a);
|
---|
75 | void Share(const NDataBlock<T>& a);
|
---|
76 | void FillFrom(size_t n,T* data);
|
---|
77 | //! Re-set all data values to \b v
|
---|
78 | inline void Reset(T v=0)
|
---|
79 | {if(mSz==0) return; T *p=Begin(),*pe=End(); while(p<pe) *p++=v;}
|
---|
80 |
|
---|
81 | // ReSize redimmensionne une structure pour "n" donnees.
|
---|
82 | // Les donnees precedentes sont perdues (pour cette classe)
|
---|
83 | // et le nouveau tableau mis a zero si fzero=true. La nouvelle structure de
|
---|
84 | // donnees n'a qu'une reference (celle de cette classe).
|
---|
85 | //! Re-size the data structure
|
---|
86 | /*! Old datas are lost (for this class). The new values are set
|
---|
87 | to zero if \b fzero=true .
|
---|
88 | The new data structure has only one reference (itself!). */
|
---|
89 | inline void ReSize(size_t n, bool fzero=true) {Alloc(n,NULL,NULL,fzero);}
|
---|
90 |
|
---|
91 | void Realloc(size_t nnew,bool force=false);
|
---|
92 |
|
---|
93 | // Public, thread safe interface to Delete() (set the size to zero)
|
---|
94 | void Dealloc();
|
---|
95 |
|
---|
96 | // Informations pointeur/data
|
---|
97 | //! Return pointer on data structure.
|
---|
98 | inline T* Data()
|
---|
99 | {if(mSRef) return mSRef->data; else return NULL;}
|
---|
100 | //! Return pointer on data structure.
|
---|
101 | inline T* Data() const
|
---|
102 | {if(mSRef) return mSRef->data; else return NULL;}
|
---|
103 | //! Return the size of the data structure
|
---|
104 | inline size_t Size() const {return mSz;}
|
---|
105 | //! Return the \b i th element of the data structure
|
---|
106 | inline T& operator()(size_t i) {return *(mSRef->data+i);}
|
---|
107 | //! Return the \b i th element of the data structure
|
---|
108 | inline T operator()(size_t i) const {return *(mSRef->data+i);}
|
---|
109 | //! Return pointer to the beginning of the data structure.
|
---|
110 | inline T* Begin() {return mSRef->data;}
|
---|
111 | //! Return pointer to the beginning of the data structure.
|
---|
112 | inline T const* Begin() const {return mSRef->data;}
|
---|
113 | //! Return pointer to the end of the data structure.
|
---|
114 | inline T* End() {return mSRef->data+mSz;}
|
---|
115 | //! Return pointer to the end of the data structure.
|
---|
116 | inline T const* End() const {return mSRef->data+mSz;}
|
---|
117 | //! Return the number of references to the data structure
|
---|
118 | inline size_t NRef() const {if(mSRef) return mSRef->nref; else return 0; }
|
---|
119 |
|
---|
120 | // Impression
|
---|
121 | void Print(ostream& os, size_t i1=0,size_t n=10) const;
|
---|
122 | //! print infos and datas (from \b i1 to \b i2) on stdout.
|
---|
123 | inline void Print(size_t i1=0,size_t n=0) const {Print(cout,i1,n);}
|
---|
124 |
|
---|
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 |
|
---|
129 | // Surcharge d'operateurs INPLACE: A = x , A = B , A @= x , A @= B
|
---|
130 | NDataBlock<T>& operator = (const NDataBlock<T>& a);
|
---|
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 |
|
---|
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);
|
---|
142 |
|
---|
143 | // Surcharge d'operateurs: C = A @ x , C = A @ B
|
---|
144 | NDataBlock<T> Add(T b) const;
|
---|
145 | NDataBlock<T> Sub(T b,bool fginv=false) const;
|
---|
146 | NDataBlock<T> Mul(T b) const;
|
---|
147 | NDataBlock<T> Div(T b,bool fginv=false) const;
|
---|
148 |
|
---|
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;
|
---|
153 |
|
---|
154 | //! Return thye associated object Id (or DataRef Id)
|
---|
155 | inline uint_8 DRefId() { return ((mSRef)?mSRef->dsid:0); }
|
---|
156 | //! assign a new object Id (or DataRef Id) - useful for PPF write operations
|
---|
157 | inline void RenewDRefId() { if (mSRef) mSRef->dsid = AnyDataObj::getUniqueId(); }
|
---|
158 | //! assign a new object Id (or DataRef Id) - useful for PPF write operations
|
---|
159 | inline void RenewObjId() { if (mSRef) mSRef->dsid = AnyDataObj::getUniqueId(); }
|
---|
160 |
|
---|
161 |
|
---|
162 | protected:
|
---|
163 | //! NDREF structure for reference management
|
---|
164 | typedef struct {
|
---|
165 | size_t nref; //!< Number of references to the data structure
|
---|
166 | uint_8 dsid; //!< Data structure Id - Used by FIO_NDataBlock
|
---|
167 | T* data; //!< Pointer to data structure itself
|
---|
168 | Bridge* bridge; //!< Pointer to a bridge for the data structure
|
---|
169 | } NDREF;
|
---|
170 |
|
---|
171 | void Alloc(size_t n,T* data=NULL,Bridge* br=NULL, bool zero=true);
|
---|
172 | void Delete(void);
|
---|
173 |
|
---|
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)
|
---|
177 | static ThSafeOp* gThsop; //!< Mutex For thread safe operation
|
---|
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
|
---|
182 | };
|
---|
183 |
|
---|
184 | //! Define operator \<\< for printing
|
---|
185 | template<class T>
|
---|
186 | inline ostream& operator << (ostream& os, const NDataBlock<T>& a)
|
---|
187 | {a.Print(os); return(os);}
|
---|
188 |
|
---|
189 | //! Add a constant to datas and return NDataBlock : ND = NDa + b
|
---|
190 | template<class T>
|
---|
191 | inline NDataBlock<T> operator + (const NDataBlock<T>& a,T b)
|
---|
192 | {return a.Add(b);}
|
---|
193 | //! Add a constant to datas and return NDataBlock : ND = b + NDa
|
---|
194 | template<class T>
|
---|
195 | inline NDataBlock<T> operator + (T b,const NDataBlock<T>& a)
|
---|
196 | {return a.Add(b);}
|
---|
197 | //! Substract a constant to datas and return NDataBlock : ND = NDa - b
|
---|
198 | template<class T>
|
---|
199 | inline NDataBlock<T> operator - (const NDataBlock<T>& a,T b)
|
---|
200 | {return a.Sub(b);}
|
---|
201 | //! Substract a constant to datas and return NDataBlock : ND = b - NDa
|
---|
202 | template<class T>
|
---|
203 | inline NDataBlock<T> operator - (T b,const NDataBlock<T>& a)
|
---|
204 | {return a.Sub(b,true);}
|
---|
205 | //! Multiply datas by a constant and return NDataBlock : ND = NDa * b
|
---|
206 | template<class T>
|
---|
207 | inline NDataBlock<T> operator * (const NDataBlock<T>& a,T b)
|
---|
208 | {return a.Mul(b);}
|
---|
209 | //! Multiply datas by a constant and return NDataBlock : ND = b * NDa
|
---|
210 | template<class T>
|
---|
211 | inline NDataBlock<T> operator * (T b,const NDataBlock<T>& a)
|
---|
212 | {return a.Mul(b);}
|
---|
213 | //! Divide datas by a constant and return NDataBlock : ND = NDa / b
|
---|
214 | template<class T>
|
---|
215 | inline NDataBlock<T> operator / (const NDataBlock<T>& a,T b)
|
---|
216 | {return a.Div(b);}
|
---|
217 | //! Divide a constant by datas and return NDataBlock : ND = b / NDa
|
---|
218 | template<class T>
|
---|
219 | inline NDataBlock<T> operator / (T b,const NDataBlock<T>& a)
|
---|
220 | {return a.Div(b,true);}
|
---|
221 |
|
---|
222 | //! Add datas of two data blocks and return NDataBlock : ND = NDa + NDb
|
---|
223 | template<class T>
|
---|
224 | inline NDataBlock<T> operator + (const NDataBlock<T>& a,const NDataBlock<T>& b)
|
---|
225 | {return a.Add(b);}
|
---|
226 | //! Substract datas of two data blocks and return NDataBlock : ND = NDa - NDb
|
---|
227 | template<class T>
|
---|
228 | inline NDataBlock<T> operator - (const NDataBlock<T>& a,const NDataBlock<T>& b)
|
---|
229 | {return a.Sub(b);}
|
---|
230 | //! Multiply datas of two data blocks and return NDataBlock : ND = NDa * NDb
|
---|
231 | template<class T>
|
---|
232 | inline NDataBlock<T> operator * (const NDataBlock<T>& a,const NDataBlock<T>& b)
|
---|
233 | {return a.Mul(b);}
|
---|
234 | //! Divide datas of two data blocks and return NDataBlock : ND = NDa / NDb
|
---|
235 | template<class T>
|
---|
236 | inline NDataBlock<T> operator / (const NDataBlock<T>& a,const NDataBlock<T>& b)
|
---|
237 | {return a.Div(b);}
|
---|
238 |
|
---|
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 |
|
---|
260 | } // Fin du namespace
|
---|
261 |
|
---|
262 | #endif
|
---|