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

Last change on this file since 944 was 944, checked in by ansari, 25 years ago

doc + nouvelle gestion debug cmv 16/04/00

File size: 8.7 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 "anydataobj.h"
10#include <iostream.h>
11
12namespace SOPHYA {
13
14////////////////////////////////////////////////////////////////
15//// ------------------- Class Bridge ----------------------- //
16////////////////////////////////////////////////////////////////
17/*!
18 \class Bridge
19 \ingroup SysTools
20 This class is use by NDataBlock. It allows sharing of datas
21 with external structures : by example, if you want to connect
22 a Blitz data structure with a NDataBlock or a TMatrix or ...
23 \sa NDataBlock
24*/
25
26// Classe pour permettre de partager des donnees avec
27// un autre systeme de gestion de references (ex avec Blitz)
28//! Empty class which allows data sharing with external structures (for NDataBlock)
29class Bridge {
30public:
31 Bridge() { }
32 virtual ~Bridge() { }
33};
34
35////////////////////////////////////////////////////////////////
36//// ----------------- Class NDataBlock --------------------- //
37////////////////////////////////////////////////////////////////
38
39// classe de container avec partage de reference
40//! Container of data with reference sharing
41template <class T>
42class NDataBlock : public AnyDataObj {
43
44public:
45
46 // Methodes statiques pour debug.
47 static void SetPrintDebug(int prtdbglevel=1);
48 static void ResetDebug(size_t nallocdata=0, size_t nallocsref=0);
49 static void PrintDebug();
50
51 // Creation / destruction
52 NDataBlock(size_t n);
53 NDataBlock(size_t n, T* data, Bridge* br=NULL);
54 NDataBlock();
55 NDataBlock(const NDataBlock<T>& a);
56 NDataBlock(const NDataBlock<T>& a,bool share);
57 virtual ~NDataBlock();
58
59 // Temporaire?
60 //! Return true if data block is temporay
61 inline bool IsTemp(void) const {return mIsTemp;}
62 //! Set temporary caracter of data block
63 inline void SetTemp(bool temp=false) const {mIsTemp = temp;}
64
65 // Gestion taille/Remplissage
66 void Clone(const NDataBlock<T>& a);
67 void CloneOrShare(const NDataBlock<T>& a);
68 void Share(const NDataBlock<T>& a);
69 void FillFrom(size_t n,T* data);
70 //! Re-set all data values to \b v
71 inline void Reset(T v=0)
72 {if(mSz==0) return; T *p=Begin(),*pe=End(); while(p<pe) *p++=v;}
73
74 // ReSize redimmensionne une structure pour "n" donnees.
75 // Les donnees precedentes sont perdues (pour cette classe)
76 // et le nouveau tableau mis a zero. La nouvelle structure de
77 // donnees n'a qu'une reference (celle de cette classe).
78 //! Re-size the data structure
79 /*! Old datas are lost (for this class). The new values are set to zero.
80 The new data structure has only one reference (itself!). */
81 inline void ReSize(size_t n) {Alloc(n);}
82
83 void Realloc(size_t nnew,bool force=false);
84
85 // Informations pointeur/data
86 //! Return pointer on data structure.
87 inline T* Data()
88 {if(mSRef) return mSRef->data; else return NULL;}
89 //! Return pointer on data structure.
90 inline T* Data() const
91 {if(mSRef) return mSRef->data; else return NULL;}
92 //! Return the size of the data structure
93 inline size_t Size() const {return mSz;}
94 //! Return the \b i th element of the data structure
95 inline T& operator()(size_t i) {return *(mSRef->data+i);}
96 //! Return the \b i th element of the data structure
97 inline T operator()(size_t i) const {return *(mSRef->data+i);}
98 //! Return pointer to the beginning of the data structure.
99 inline T* Begin() {return mSRef->data;}
100 //! Return pointer to the beginning of the data structure.
101 inline T const* Begin() const {return mSRef->data;}
102 //! Return pointer to the end of the data structure.
103 inline T* End() {return mSRef->data+mSz;}
104 //! Return pointer to the end of the data structure.
105 inline T const* End() const {return mSRef->data+mSz;}
106 //! Return the number of references to the data structure
107 inline size_t NRef() const {if(mSRef) return 0; else return mSRef->nref;}
108
109 // Impression
110 void Print(ostream& os, size_t i1=0,size_t n=10) const;
111 //! print infos and datas (from \b i1 to \b i2) on stdout.
112 inline void Print(size_t i1=0,size_t n=0) const {Print(cout,i1,n);}
113
114 //
115 T Sum(size_t i1=0,size_t n=0) const;
116 T Product(size_t i1=0,size_t n=0) const;
117
118 // Surcharge d'operateurs INPLACE: A = x , A = B , A @= x , A @= B
119 NDataBlock<T>& operator = (const NDataBlock<T>& a);
120 NDataBlock<T>& operator = (T v);
121
122 NDataBlock<T>& operator += (T b);
123 NDataBlock<T>& operator -= (T b);
124 NDataBlock<T>& operator *= (T b);
125 NDataBlock<T>& operator /= (T b);
126
127 NDataBlock<T>& operator += (const NDataBlock<T>& a);
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
132 // Surcharge d'operateurs: C = A @ x , C = A @ B
133 NDataBlock<T> Add(T b) const;
134 NDataBlock<T> Sub(T b) const;
135 NDataBlock<T> SubInv(T b) const;
136 NDataBlock<T> Mul(T b) const;
137 NDataBlock<T> Div(T b) const;
138 NDataBlock<T> DivInv(T b) const;
139
140 NDataBlock<T> Add(const NDataBlock<T>& b) const;
141 NDataBlock<T> Sub(const NDataBlock<T>& b) const;
142 NDataBlock<T> SubInv(const NDataBlock<T>& b) const;
143 NDataBlock<T> Mul(const NDataBlock<T>& b) const;
144 NDataBlock<T> Div(const NDataBlock<T>& b) const;
145 NDataBlock<T> DivInv(const NDataBlock<T>& b) const;
146
147protected:
148 //! NDREF structure for reference management
149 typedef struct {
150 size_t nref; //!< Number of references to the data structure
151 T* data; //!< Pointer to data structure itself
152 Bridge* bridge; //!< Pointer to a bridge for the data structure
153 } NDREF;
154
155 void Alloc(size_t n,T* data=NULL,Bridge* br=NULL, bool zero=true);
156 void Delete(void);
157
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
165};
166
167//! Define operator \<\< for printing
168template<class T>
169inline ostream& operator << (ostream& os, const NDataBlock<T>& a)
170 {a.Print(os); return(os);}
171
172//! Add a constant to datas and return NDataBlock : ND = NDa + b
173template<class T>
174inline NDataBlock<T> operator + (const NDataBlock<T>& a,T b)
175 {return a.Add(b);}
176//! Add a constant to datas and return NDataBlock : ND = b + NDa
177template<class T>
178inline NDataBlock<T> operator + (T b,const NDataBlock<T>& a)
179 {return a.Add(b);}
180//! Substract a constant to datas and return NDataBlock : ND = NDa - b
181template<class T>
182inline NDataBlock<T> operator - (const NDataBlock<T>& a,T b)
183 {return a.Sub(b);}
184//! Substract a constant to datas and return NDataBlock : ND = b - NDa
185template<class T>
186inline NDataBlock<T> operator - (T b,const NDataBlock<T>& a)
187 {return a.SubInv(b);}
188//! Multiply datas by a constant and return NDataBlock : ND = NDa * b
189template<class T>
190inline NDataBlock<T> operator * (const NDataBlock<T>& a,T b)
191 {return a.Mul(b);}
192//! Multiply datas by a constant and return NDataBlock : ND = b * NDa
193template<class T>
194inline NDataBlock<T> operator * (T b,const NDataBlock<T>& a)
195 {return a.Mul(b);}
196//! Divide datas by a constant and return NDataBlock : ND = NDa / b
197template<class T>
198inline NDataBlock<T> operator / (const NDataBlock<T>& a,T b)
199 {return a.Div(b);}
200//! Divide a constant by datas and return NDataBlock : ND = b / NDa
201template<class T>
202inline NDataBlock<T> operator / (T b,const NDataBlock<T>& a)
203 {return a.DivInv(b);}
204
205//! Add datas of two data blocks and return NDataBlock : ND = NDa + NDb
206template<class T>
207inline NDataBlock<T> operator + (const NDataBlock<T>& a,const NDataBlock<T>& b)
208 {return a.Add(b);}
209//! Substract datas of two data blocks and return NDataBlock : ND = NDa - NDb
210template<class T>
211inline NDataBlock<T> operator - (const NDataBlock<T>& a,const NDataBlock<T>& b)
212 {return a.Sub(b);}
213//! Multiply datas of two data blocks and return NDataBlock : ND = NDa * NDb
214template<class T>
215inline NDataBlock<T> operator * (const NDataBlock<T>& a,const NDataBlock<T>& b)
216 {return a.Mul(b);}
217//! Divide datas of two data blocks and return NDataBlock : ND = NDa / NDb
218template<class T>
219inline NDataBlock<T> operator / (const NDataBlock<T>& a,const NDataBlock<T>& b)
220 {return a.Div(b);}
221
222} // Fin du namespace
223
224#endif
Note: See TracBrowser for help on using the repository browser.