source: Sophya/trunk/SophyaLib/BaseTools/ndatablock.h@ 3820

Last change on this file since 3820 was 3385, checked in by ansari, 18 years ago

protection pour I/O PPF de NDataBlock, lorsque taille (Size()) == 0, Reza 21/11/2007

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