1 | // Gestion de block de donnees avec partage de references
|
---|
2 | // malheureusement tres mal concu... C.Magneville 04/99
|
---|
3 | // LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
|
---|
4 | #include "sopnamsp.h"
|
---|
5 | #include "machdefs.h"
|
---|
6 | #include <stdio.h>
|
---|
7 | #include <stdlib.h>
|
---|
8 | #include <iostream>
|
---|
9 | #include <complex>
|
---|
10 | #include "pexceptions.h"
|
---|
11 | #include "ndatablock.h"
|
---|
12 |
|
---|
13 | /* ---- Pour renvoyer un identificateur unique ---- */
|
---|
14 | static uint_8 _ndrefid_ = 0; // Identificateur de NDREF cree
|
---|
15 | uint_8 AnyDataObj::getUniqueId()
|
---|
16 | {
|
---|
17 | _ndrefid_++;
|
---|
18 | return ( _ndrefid_ );
|
---|
19 | }
|
---|
20 |
|
---|
21 | /*!
|
---|
22 | \class SOPHYA::NDataBlock
|
---|
23 | \ingroup BaseTools
|
---|
24 | Management of data blocks
|
---|
25 | */
|
---|
26 |
|
---|
27 | //////////////////////////////////
|
---|
28 | // Fonctionnement en mode debug //
|
---|
29 | //////////////////////////////////
|
---|
30 |
|
---|
31 | template <class T> int NDataBlock<T>::Debug_NDataBlock = 0;
|
---|
32 | template <class T> size_t NDataBlock<T>::NallocData = 0;
|
---|
33 | template <class T> size_t NDataBlock<T>::NallocSRef = 0;
|
---|
34 |
|
---|
35 | //! Set debug (and level print) for allocation and references debug.
|
---|
36 | /*!
|
---|
37 | \param prtlevel : activate/des-activate debug mode
|
---|
38 | and select print level
|
---|
39 |
|
---|
40 | \arg prtlevel <= 0 : no debug
|
---|
41 | \arg prtlevel == 1 : debug activated, no print
|
---|
42 | \arg prtlevel >=2 : debug activated,
|
---|
43 | print infos in all routines that have something to do with
|
---|
44 | allocations or des-allocation of datas or references.
|
---|
45 | */
|
---|
46 | template <class T>
|
---|
47 | void NDataBlock<T>::SetPrintDebug(int prtdbglevel)
|
---|
48 | {
|
---|
49 | Debug_NDataBlock = prtdbglevel;
|
---|
50 | }
|
---|
51 |
|
---|
52 | //! Reset debug counter values.
|
---|
53 | /*!
|
---|
54 | \param nallocdata : reset number of allocated data structures to \b nallocdata
|
---|
55 | \param nallocsref : reset number of allocated references to \b nallocsref
|
---|
56 | \warning In principle this routine should not be use (only experts)
|
---|
57 | */
|
---|
58 | template <class T>
|
---|
59 | void NDataBlock<T>::ResetDebug(size_t nallocdata, size_t nallocsref)
|
---|
60 | {
|
---|
61 | NallocData = nallocdata;
|
---|
62 | NallocSRef = nallocsref;
|
---|
63 | }
|
---|
64 |
|
---|
65 | //! Print debug current status.
|
---|
66 | /*!
|
---|
67 | Print debug current status for number of allocated
|
---|
68 | data structures and number of allocated references.
|
---|
69 | */
|
---|
70 | template <class T>
|
---|
71 | void NDataBlock<T>::PrintDebug()
|
---|
72 | {
|
---|
73 | cout<<"... ... ... NallocData = "<<NallocData
|
---|
74 | <<" , NallocSRef = "<<NallocSRef
|
---|
75 | <<" ... ... ..."<<endl;
|
---|
76 | }
|
---|
77 |
|
---|
78 | ///////////////////////////
|
---|
79 | // Createur, Destructeur //
|
---|
80 | ///////////////////////////
|
---|
81 |
|
---|
82 | //! Constructor for \b n datas. if \b zero=true, filled with zeros
|
---|
83 | template <class T>
|
---|
84 | NDataBlock<T>::NDataBlock(size_t n, bool fzero)
|
---|
85 | // Createur d'une structure de "n" donnees
|
---|
86 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
87 | {
|
---|
88 | if(Debug_NDataBlock>1)
|
---|
89 | cout<<"?_NDataBlock::NDataBlock("<<this<<",n="<<n<<")"<<endl;
|
---|
90 |
|
---|
91 | Alloc(n, NULL, NULL, fzero); // allocation et mise a zero
|
---|
92 | }
|
---|
93 |
|
---|
94 | //! Constructor for \b n datas shared with external
|
---|
95 | /*!
|
---|
96 | Datas are previously allocated by an other external source.
|
---|
97 | \warning This require particular care (see Alloc)
|
---|
98 | \sa Alloc
|
---|
99 | */
|
---|
100 | template <class T>
|
---|
101 | NDataBlock<T>::NDataBlock(size_t n, T* data, Bridge* br)
|
---|
102 | // Createur d'une structure de "n" donnees, avec donnees preallouees.
|
---|
103 | // Attention createur TRES DANGEREUX (Voir explications dans Alloc()).
|
---|
104 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
105 | {
|
---|
106 | if(Debug_NDataBlock>1)
|
---|
107 | cout<<"?_NDataBlock::NDataBlock("<<this
|
---|
108 | <<",data="<<data<<",br="<<br<<")"<<endl;
|
---|
109 |
|
---|
110 | Alloc(n,data,br);
|
---|
111 | }
|
---|
112 |
|
---|
113 | //! Default constructor
|
---|
114 | template <class T>
|
---|
115 | NDataBlock<T>::NDataBlock()
|
---|
116 | // Createur par default
|
---|
117 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
118 | {
|
---|
119 | if(Debug_NDataBlock>1)
|
---|
120 | cout<<"?_NDataBlock::NDataBlock("<<this<<") default"<<endl;
|
---|
121 | }
|
---|
122 |
|
---|
123 | //! Copy constructor
|
---|
124 | /*!
|
---|
125 | \warning datas are \b SHARED with \b a.
|
---|
126 | */
|
---|
127 | template <class T>
|
---|
128 | NDataBlock<T>::NDataBlock(const NDataBlock<T>& a)
|
---|
129 | // Createur par copie: partage les donnees dans tous les cas
|
---|
130 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
131 | {
|
---|
132 | if(Debug_NDataBlock>1)
|
---|
133 | cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<" a.mSz="<<a.mSz<<")"<<endl;
|
---|
134 |
|
---|
135 | if(a.mSRef && a.mSz>0) Share(a);
|
---|
136 | }
|
---|
137 |
|
---|
138 | //! Copy constructor with \b share option
|
---|
139 | /*!
|
---|
140 | \warning datas are shared if \b share is \b true, cloned if not.
|
---|
141 | */
|
---|
142 | template <class T>
|
---|
143 | NDataBlock<T>::NDataBlock(const NDataBlock<T>& a,bool share)
|
---|
144 | // Createur avec choix de partager ou non selon "share"
|
---|
145 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
146 | {
|
---|
147 | if(Debug_NDataBlock>1)
|
---|
148 | cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a
|
---|
149 | <<",sh=<<"<<share<<")"<<endl;
|
---|
150 |
|
---|
151 | if(a.mSRef && a.mSz>0) {if(share) Share(a); else Clone(a);}
|
---|
152 | }
|
---|
153 |
|
---|
154 | //! Destructor
|
---|
155 | template <class T>
|
---|
156 | NDataBlock<T>::~NDataBlock()
|
---|
157 | // Destructeur
|
---|
158 | {
|
---|
159 | if(Debug_NDataBlock>1)
|
---|
160 | cout<<"?_NDataBlock::~NDataBlock("<<this<<")"<<endl;
|
---|
161 |
|
---|
162 | Delete();
|
---|
163 | }
|
---|
164 |
|
---|
165 | ////////////////////////
|
---|
166 | // Gestion de donnees //
|
---|
167 | ////////////////////////
|
---|
168 |
|
---|
169 | //! Clone datas from \b a
|
---|
170 | template <class T>
|
---|
171 | void NDataBlock<T>::Clone(const NDataBlock<T>& a)
|
---|
172 | // Clone: copie de donnees a partir de "a"
|
---|
173 | {
|
---|
174 | if(Debug_NDataBlock>1)
|
---|
175 | cout<<"?_NDataBlock::Clone("<<this<<","<<&a<<") a.(mSz="
|
---|
176 | <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp()
|
---|
177 | <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
|
---|
178 |
|
---|
179 | if(&a==NULL) throw(NullPtrError("NDataBlock::Clone &a==NULL\n"));
|
---|
180 | if(!a.mSRef || a.mSz==0) throw(NullPtrError("NDataBlock::Clone a.mSz=0\n"));
|
---|
181 | Alloc(a.mSz, NULL, NULL, false); // pas de mise a zero
|
---|
182 | memcpy(Data(),a.Data(),mSz*sizeof(T));
|
---|
183 | }
|
---|
184 |
|
---|
185 | //! Share datas with \b a
|
---|
186 | template <class T>
|
---|
187 | void NDataBlock<T>::Share(const NDataBlock<T>& a)
|
---|
188 | // Share: Partage les donnees avec "a"
|
---|
189 | {
|
---|
190 | if(Debug_NDataBlock>1) {
|
---|
191 | cout<<"?_NDataBlock::Share("<<this<<","<<&a<<")";
|
---|
192 | if(&a!=NULL) cout<<" a.(mSz="<<a.mSz<<" mSRef="<<a.mSRef
|
---|
193 | <<" IsTemp="<<a.IsTemp()<<")";
|
---|
194 | cout<<", mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
|
---|
195 | }
|
---|
196 |
|
---|
197 | if(&a==NULL) throw(NullPtrError("NDataBlock::Share &a==NULL\n"));
|
---|
198 | if(!a.mSRef || a.mSz==0) throw(NullPtrError("NDataBlock::Share a.mSz=0\n"));
|
---|
199 | if(mSRef) Delete();
|
---|
200 | mSz = a.mSz; mSRef = a.mSRef; mSRef->nref++;
|
---|
201 |
|
---|
202 | if(Debug_NDataBlock>1)
|
---|
203 | cout<<"...?_NDataBlock::Share mSz="<<mSz<<" mSRef="<<mSRef
|
---|
204 | <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<< mSRef->data
|
---|
205 | <<" mSRef->bridge="<<mSRef->bridge
|
---|
206 | <<" IsTemp="<<mIsTemp<<endl;
|
---|
207 | }
|
---|
208 |
|
---|
209 | //! \b Share with \b a if \b temporary, \b clone from \b a if not.
|
---|
210 | /*! \warning For most purposes, users don't have to worry with
|
---|
211 | the "temporary" nature of a NDataBlock. That is used
|
---|
212 | internaly to avoid memory allocation in operation
|
---|
213 | like A = B + C + D for instance. The method is not
|
---|
214 | protected to allow users to write complicated functions
|
---|
215 | on NDataBlock.
|
---|
216 | \verbatim
|
---|
217 | ----------------------------------------------------------
|
---|
218 | Pourquoi une complication avec la notion de "temporaire" :
|
---|
219 | ----------------------------------------------------------
|
---|
220 | - Le constructeur par copie partageant les donnees,
|
---|
221 | dans une methode un { NDataBlock<T> result; ...; return result;}
|
---|
222 | ne va pas allouer de la memoire pour retourner "result".
|
---|
223 | - La gestion de temporaire sert quand on enchaine plusieurs
|
---|
224 | operations sur la meme ligne, par exemple : A = B+C+D;
|
---|
225 | Dans ce cas l'objet CD=C+D est d'abord alloue et rempli
|
---|
226 | avec C+D, puis CD est mis a "temporaire".
|
---|
227 | Quand on ajoute B a CD, la methode d'addition va se rendre compte
|
---|
228 | que CD est "temporaire" et additionner B "in-place" dans CD
|
---|
229 | sans allouer une fois de plus de la place (pas d'allocation
|
---|
230 | de place BCD pour mettre B+CD mais une operation CD += B).
|
---|
231 | Si la notion d'objet "temporaire" n'avait pas ete consideree
|
---|
232 | l'addition A = B+C+D aurait alloue de la place pour "CD=C+D"
|
---|
233 | puis pour BCD=B+CD : 2 allocations auraient ete necessaires
|
---|
234 | contre 1 seule dans notre cas de geston de "temporaire".
|
---|
235 | \endverbatim
|
---|
236 | */
|
---|
237 | template <class T>
|
---|
238 | void NDataBlock<T>::CloneOrShare(const NDataBlock<T>& a)
|
---|
239 | // CloneOrShare: Share si "a" temporaire, Clone sinon.
|
---|
240 | {
|
---|
241 | if(Debug_NDataBlock>1)
|
---|
242 | cout<<"?_NDataBlock::CloneOrShare("<<this<<","<<&a<<")"<<endl;
|
---|
243 |
|
---|
244 | if(&a==NULL) throw(NullPtrError("NDataBlock::CloneOrShare &a==NULL\n"));
|
---|
245 | if(a.IsTemp()) Share(a); else Clone(a);
|
---|
246 | }
|
---|
247 |
|
---|
248 | ////////////////////////////////////////////////////////////
|
---|
249 | // Allocation , destruction , remplissage et reallocation //
|
---|
250 | ////////////////////////////////////////////////////////////
|
---|
251 |
|
---|
252 | //! Allocation management
|
---|
253 | /*!
|
---|
254 | Allocation d'un NOUVEL espace de stoquage de "n" donnees
|
---|
255 | \verbatim
|
---|
256 | Si data==NULL : allocation de l'espace memoire
|
---|
257 | si zero == true , l'espace est remplis de zeros
|
---|
258 | data!=NULL : partage des donnees avec l'adresse data
|
---|
259 | Si br==NULL : les donnees nous appartiennent
|
---|
260 | br!=NULL : les donnees ne nous appartiennent pas (ex: Blitz)
|
---|
261 |
|
---|
262 | Exemple: on veut connecter a un tableau de T*
|
---|
263 | > float *x = new float[5]; ... remplissage de x[] ...;
|
---|
264 | 1- On veut que NDataBlock NE DESALLOUE PAS le tableau "x[]"
|
---|
265 | a- Premiere solution
|
---|
266 | > NDataBlock A(5,x,new Bridge);
|
---|
267 | ......
|
---|
268 | > delete [] x;
|
---|
269 | - Il faut deleter x[] explicitement.
|
---|
270 | - Le destructeur de "A" ne detruit pas x[].
|
---|
271 | ATTENTION: Une fois x[] detruit, "A" ne peut
|
---|
272 | plus acceder les donnees!
|
---|
273 | - Bridge est detruit par le destructeur de "A"
|
---|
274 | b- Autre solution:
|
---|
275 | > NDataBlock A(5); A.FillFrom(5,x);
|
---|
276 | > delete [] x;
|
---|
277 | ......
|
---|
278 | - Il faut deleter x[] explicitement.
|
---|
279 | - "A" possede une copie en local de x[].
|
---|
280 | - Le destructeur de "A" ne detruit pas x[] mais la copie locale.
|
---|
281 | 2- On veut que NDataBlock desalloue le tableau
|
---|
282 | > NDataBlock A(5,x);
|
---|
283 | - Ne Pas Faire "delete [] x;"
|
---|
284 | - "A" partage les donnees avec x[].
|
---|
285 | - Le destructeur de "A" detruit x[].
|
---|
286 |
|
---|
287 | --- REMARQUE SUR LE DANGER DE CERTAINES SITUATIONS (CMV):
|
---|
288 | 1-/ x = new float[n1]; NDataBlock A(n2,x);
|
---|
289 | 1er danger: si n2>n1 depassement de tableaux (core dump)
|
---|
290 | 2sd danger: celui qui alloue x[] ne doit pas faire le "delete"
|
---|
291 | en desaccord avec toutes les regles de bonne conduite.
|
---|
292 | 2-/ float x[5]={1,2,3,4,5}; {NDataBlock A(n2,&x[0]);} cout<<x[2];
|
---|
293 | Ici, a la sortie du bloc {}, le destructeur de "A" va detruire
|
---|
294 | l'adresse de &x[0]: je n'ose imaginer que ca se fasse sans probleme
|
---|
295 | et de toute facon, cout<<x[2]; va surement faire des etincelles.
|
---|
296 | 3-/ x = new float[n1]; NDataBlock A(n2,x,new Bridge);
|
---|
297 | 1er danger: si n2>n1 depassement de tableaux (core dump)
|
---|
298 | 2sd danger: si la methode bridgee (blitz?) detruit x[]
|
---|
299 | "A" n'a plus de donnees connectees!
|
---|
300 | --- CONCLUSION
|
---|
301 | Cette classe est franchement merdique.
|
---|
302 | - On peut accepter la prise de risque liee a NDataBlock(n2,x,new Bridge);
|
---|
303 | car je ne vois pas comment on pourrait faire autrement pour connecter
|
---|
304 | un tableau de type blitz par exemple.
|
---|
305 | - Par contre le createur NDataBlock(n2,x); doit etre interdit
|
---|
306 | dans sa forme actelle car trop dangereux et il me semble inutile.
|
---|
307 | - Dans cette nouvelle optique:
|
---|
308 | NDataBlock(n2,x,new Bridge) et NDataBlock(n2,x) disparaissent
|
---|
309 | On remplace par NDataBlock(n2,x) {Alloc(n2,x,new Bridge);}
|
---|
310 | qui force le Bridge dans tout les cas puisque NDataBlock
|
---|
311 | ne possede pas les donnees.
|
---|
312 | Mais puis-je encore le faire vu que NDataBlock est a la base
|
---|
313 | de TVector,TMatrix et qu'il faut donc reprendre tout le code DPC
|
---|
314 | - Quoiqu'il arrive Alloc est une methode privee et peut donc rester
|
---|
315 | sous sa forme actuelle.
|
---|
316 |
|
---|
317 | \endverbatim
|
---|
318 | */
|
---|
319 |
|
---|
320 |
|
---|
321 | template <class T>
|
---|
322 | void NDataBlock<T>::Alloc(size_t n,T* data,Bridge* br,bool zero)
|
---|
323 | {
|
---|
324 | if(Debug_NDataBlock>1)
|
---|
325 | cout<<"?_NDataBlock::Alloc("<<this<<","
|
---|
326 | <<n<<","<<data<<","<<br<<") mSz="<<mSz
|
---|
327 | <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
|
---|
328 |
|
---|
329 | if(br && !data)
|
---|
330 | throw(NullPtrError("NDataBlock::Alloc br!=NULL && data==NULL\n"));
|
---|
331 | if(n==0) throw(SzMismatchError("NDataBlock::Alloc n==0\n"));
|
---|
332 | if(mSRef) Delete();
|
---|
333 | mSz = n;
|
---|
334 | mSRef = new NDREF;
|
---|
335 | mSRef->nref = 1;
|
---|
336 | mSRef->dsid = AnyDataObj::getUniqueId();
|
---|
337 | if(data) mSRef->data = data;
|
---|
338 | else {mSRef->data = new T[n]; if (zero) memset(mSRef->data,0,n*sizeof(T));}
|
---|
339 | mSRef->bridge = br;
|
---|
340 |
|
---|
341 | if(Debug_NDataBlock>0) {
|
---|
342 | // Meme dans le cas data!=0 et br==0 (connexion d'un tableau
|
---|
343 | // avec destruction geree par ~NDataBlock (cas 2-) on compte
|
---|
344 | // comme si on avait fait une allocation du tableau (ce qui a ete
|
---|
345 | // fait au niveau du dessus!).
|
---|
346 | if(!br) NallocData++; NallocSRef++;
|
---|
347 | if(Debug_NDataBlock>1)
|
---|
348 | cout<<"...?_NDataBlock::Alloc mSz="<<mSz<<" mSRef="<<mSRef
|
---|
349 | <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<<mSRef->data
|
---|
350 | <<" mSRef->bridge="<<mSRef->bridge
|
---|
351 | <<" IsTemp="<<mIsTemp
|
---|
352 | <<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | //! Management of de-allocation
|
---|
357 | template <class T>
|
---|
358 | void NDataBlock<T>::Delete(void)
|
---|
359 | // Pour detruire les pointeurs en tenant compte des references
|
---|
360 | {
|
---|
361 | if(Debug_NDataBlock>1) {
|
---|
362 | cout<<"?_NDataBlock::Delete("<<this<<") mSz="<<mSz
|
---|
363 | <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp;
|
---|
364 | if(mSRef)
|
---|
365 | cout<<" mSRef->nref="<<mSRef->nref<<" mSRef->data="
|
---|
366 | <<mSRef->data<<" mSRef->bridge="<<mSRef->bridge;
|
---|
367 | cout<<endl;
|
---|
368 | }
|
---|
369 |
|
---|
370 | if(mSRef==NULL) return;
|
---|
371 |
|
---|
372 | mSRef->nref--;
|
---|
373 | if(mSRef->nref != 0) {
|
---|
374 |
|
---|
375 | if(Debug_NDataBlock>1)
|
---|
376 | cout<<"...?_NDataBlock::Delete() pas de desallocation il reste nref="
|
---|
377 | <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
|
---|
378 |
|
---|
379 | mSz = 0; mSRef=NULL;
|
---|
380 | return;
|
---|
381 | }
|
---|
382 |
|
---|
383 | if(Debug_NDataBlock>0) {
|
---|
384 | if(!mSRef->bridge) NallocData--; NallocSRef--;
|
---|
385 | if(Debug_NDataBlock>1)
|
---|
386 | cout<<"...?_NDataBlock::Delete() desallocation complete il reste nref="
|
---|
387 | <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
|
---|
388 | }
|
---|
389 |
|
---|
390 | // Si il y a un Bridge les donnees ne n'appartiennent pas, on detruit le Bridge
|
---|
391 | // sinon, les donnees ont ete allouees par nos soins, on libere l'espace
|
---|
392 | if(mSRef->bridge) {
|
---|
393 | if(Debug_NDataBlock>1)
|
---|
394 | cout<<"...?_NDataBlock::Delete() Bridge "<<mSRef->bridge<<" deleted"<<endl;
|
---|
395 | delete mSRef->bridge;
|
---|
396 | } else {
|
---|
397 | if(Debug_NDataBlock>1)
|
---|
398 | cout<<"...?_NDataBlock::Delete() data "<<mSRef->data<<" deleted"<<endl;
|
---|
399 | delete [] mSRef->data;
|
---|
400 | }
|
---|
401 | mSRef->bridge=NULL; mSRef->data=NULL;
|
---|
402 | delete mSRef; mSRef=NULL; mSz = 0;
|
---|
403 | }
|
---|
404 |
|
---|
405 | //! Fill dats of this NDataBlock with the \b n datas pointed by \b data
|
---|
406 | /*!
|
---|
407 | \warning If class empty : allocate space in memory
|
---|
408 | \warning If class already connected : overwrite with minimum size
|
---|
409 | (\b n or \b mSz)
|
---|
410 | */
|
---|
411 | template <class T>
|
---|
412 | void NDataBlock<T>::FillFrom(size_t n,T* data)
|
---|
413 | // Remplissage par un tableau de donnees
|
---|
414 | // - Si classe vide : creation de l'espace memoire
|
---|
415 | // - Si classe connectee : on ecrit selon la longueur minimale
|
---|
416 | // (cad this->mSz ou "n")
|
---|
417 | {
|
---|
418 | if(data==NULL) throw(NullPtrError("NDataBlock::FillFrom data==NULL\n"));
|
---|
419 | if(n==0) throw(ParmError("NDataBlock::FillFrom n<=0\n"));
|
---|
420 | if(mSRef==NULL) Alloc(n, NULL, NULL, false); // Pas de mise a zero
|
---|
421 | if(mSz<n) n = mSz;
|
---|
422 | memcpy(Data(),data,n*sizeof(T));
|
---|
423 | }
|
---|
424 |
|
---|
425 | //! Re-allocate space for \b nnew datas
|
---|
426 | /*!
|
---|
427 | \param nnnew : new size
|
---|
428 | \param force : to manage the way re-allocation will be done (see after).
|
---|
429 | \verbatim
|
---|
430 | Re-allocation de "nnew" place memoire pour les donnees
|
---|
431 | avec conservation des "nold" donnees precedentes si possible.
|
---|
432 | "force" gere la re-allocation de la place memoire pour les donnees.
|
---|
433 | Divers cas se presentent:
|
---|
434 | a-/ *** nnew>nold force=quelconque ***
|
---|
435 | place re-allouee, donnees [0,nold[ copiees, surplus [nold,new[ mis a zero
|
---|
436 | b-/ *** nnew<=nold force=true ***
|
---|
437 | place re-allouee, donnees [0,nnew[ copiees, pas de surplus
|
---|
438 | c-/ *** nnew<=nold force=false ***
|
---|
439 | place non re-allouee, seule la valeur de la taille est diminuee
|
---|
440 | - On tient compte du partage des donnees dans tous les cas.
|
---|
441 | - Si il n'y a pas de donnees connectees a la classe, on re-alloue
|
---|
442 | dans tous les cas
|
---|
443 | \endverbatim
|
---|
444 | */
|
---|
445 | template <class T>
|
---|
446 | void NDataBlock<T>::Realloc(size_t nnew,bool force)
|
---|
447 | {
|
---|
448 | if(nnew==0) throw(ParmError("NDataBlock::Realloc n<=0\n"));
|
---|
449 |
|
---|
450 | // Cas sans re-allocation memoire
|
---|
451 | if(mSRef && nnew<=mSz && ! force) { mSz=nnew; return;}
|
---|
452 |
|
---|
453 | // Cas avec re-allocation memoire
|
---|
454 | size_t ncop;
|
---|
455 | if(!mSRef || mSz==0) ncop=0; else if(mSz<nnew) ncop=mSz; else ncop=nnew;
|
---|
456 | T* dataloc = new T[nnew];
|
---|
457 | if(ncop>0) memcpy(dataloc,mSRef->data,ncop*sizeof(T));
|
---|
458 | if(nnew>ncop) memset(dataloc+ncop,0,(nnew-ncop)*sizeof(T));
|
---|
459 | Alloc(nnew,dataloc,NULL); //Alloc gere partage de reference et bridge
|
---|
460 | }
|
---|
461 |
|
---|
462 | ////////////////
|
---|
463 | // Impression //
|
---|
464 | ////////////////
|
---|
465 |
|
---|
466 | //! Give infos and print \b n datas beginning at \b i1 on stream \b os.
|
---|
467 | template <class T>
|
---|
468 | void NDataBlock<T>::Print(ostream& os,size_t i1,size_t n) const
|
---|
469 | // Impression de n elements a partir de i1
|
---|
470 | {
|
---|
471 | size_t nr = 0;
|
---|
472 | T* p = NULL; Bridge* br = NULL;
|
---|
473 | if(mSRef) {nr = mSRef->nref; p = mSRef->data; br = mSRef->bridge;}
|
---|
474 | os<<"NDataBlock::Print("<<this<<",Sz="<<mSz<<",IsTemp="<<mIsTemp<<")\n"
|
---|
475 | <<" mSRef="<<mSRef<<"(nref="<<nr<<",data="<<p
|
---|
476 | <<",bridge="<<br<<")"<<endl;
|
---|
477 | if(i1>=mSz || n<=0 || !p) return;
|
---|
478 | size_t i2 = i1+n; if(i2>mSz) i2=mSz;
|
---|
479 | size_t im = 1; bool enl=false;
|
---|
480 | while(i1<i2) {
|
---|
481 | enl = false;
|
---|
482 | os<<" "<<(*this)(i1); i1++;
|
---|
483 | if(im==8) {os<<"\n"; im=1; enl=true;} else im++;
|
---|
484 | }
|
---|
485 | if(!enl) os<<endl;
|
---|
486 | }
|
---|
487 |
|
---|
488 | //////////////////////////////////////////////
|
---|
489 | // Calcul de la somme / produit des donnees //
|
---|
490 | //////////////////////////////////////////////
|
---|
491 |
|
---|
492 | //! Return sum of \b n datas beginning at data \b i1.
|
---|
493 | template <class T>
|
---|
494 | T NDataBlock<T>::Sum(size_t i1,size_t n) const
|
---|
495 | // Somme des elements de i1 a i1+n-1
|
---|
496 | {
|
---|
497 | if(i1>=mSz) return 0;
|
---|
498 | if(n>mSz) n = mSz; if(n==0) n = mSz-i1;
|
---|
499 | T const *p=Begin()+i1, *pe=p+n;
|
---|
500 | T val = 0;
|
---|
501 | while (p<pe) val += *p++;
|
---|
502 | return val;
|
---|
503 | }
|
---|
504 |
|
---|
505 | //! Return product of \b n datas beginning at data \b i1.
|
---|
506 | template <class T>
|
---|
507 | T NDataBlock<T>::Product(size_t i1,size_t n) const
|
---|
508 | // Produit des elements de i1 a i1+n-1
|
---|
509 | {
|
---|
510 | if(i1>=mSz) return 0;
|
---|
511 | if(n>mSz) n = mSz; if(n==0) n = mSz-i1;
|
---|
512 | T const *p=Begin()+i1, *pe=p+n;
|
---|
513 | T val = 0;
|
---|
514 | while (p<pe) val *= *p++;
|
---|
515 | return val;
|
---|
516 | }
|
---|
517 |
|
---|
518 | ///////////////////////////////////////////////////////////////
|
---|
519 | // Surcharge de = : NDataBlock=NDataBlock; NDataBlock=<T> b; //
|
---|
520 | ///////////////////////////////////////////////////////////////
|
---|
521 |
|
---|
522 | //! Operator = : ND = NDa
|
---|
523 | /*! \warning Datas are copied (cloned) from \b a. */
|
---|
524 | template <class T>
|
---|
525 | NDataBlock<T>& NDataBlock<T>::operator = (const NDataBlock<T>& a)
|
---|
526 | // Affectation: partage des donnees si "a" temporaire, clone sinon.
|
---|
527 | {
|
---|
528 | if(Debug_NDataBlock>1)
|
---|
529 | cout<<"?_NDataBlock::operator=("<<this<<","<<&a<<") a.(mSz="
|
---|
530 | <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp()
|
---|
531 | <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
|
---|
532 |
|
---|
533 | if(this == &a) return *this;
|
---|
534 | if(a.mSz==0)
|
---|
535 | throw(SzMismatchError("NDataBlock::operator=A null size \n"));
|
---|
536 | if (mSz==0) { CloneOrShare(a); return *this; }
|
---|
537 | if (a.mSz != mSz)
|
---|
538 | throw(SzMismatchError("NDataBlock::operator=A Unequal sizes \n"));
|
---|
539 | memcpy(Data(),a.Data(),mSz*sizeof(T));
|
---|
540 | return *this;
|
---|
541 | }
|
---|
542 |
|
---|
543 | //! Operator = : ND = \b v (at dats set to \b v).
|
---|
544 | template <class T>
|
---|
545 | NDataBlock<T>& NDataBlock<T>::operator = (T v)
|
---|
546 | // Affectation de tous les elements a une constante "v"
|
---|
547 | {
|
---|
548 | if(Debug_NDataBlock>1)
|
---|
549 | cout<<"?_NDataBlock::operator=("<<this<<","<<v<<")"
|
---|
550 | <<" mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
|
---|
551 |
|
---|
552 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator=v null size\n"));
|
---|
553 | T *p=Begin(), *pe=End(); while (p<pe) *p++ = v;
|
---|
554 | return *this;
|
---|
555 | }
|
---|
556 |
|
---|
557 | //////////////////////////////////////////////////////////////
|
---|
558 | // Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += <T> b; //
|
---|
559 | //////////////////////////////////////////////////////////////
|
---|
560 |
|
---|
561 | //! Add a constant : ND += b
|
---|
562 | template <class T>
|
---|
563 | NDataBlock<T>& NDataBlock<T>::operator += (T b)
|
---|
564 | {
|
---|
565 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator+=v null size\n"));
|
---|
566 | T *p=Begin(), *pe=End(); while (p<pe) *p++ += b;
|
---|
567 | return *this;
|
---|
568 | }
|
---|
569 |
|
---|
570 | //! Substract a constant : ND -= b
|
---|
571 | template <class T>
|
---|
572 | NDataBlock<T>& NDataBlock<T>::operator -= (T b)
|
---|
573 | {
|
---|
574 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator-=v null size\n"));
|
---|
575 | T *p=Begin(), *pe=End(); while (p<pe) *p++ -= b;
|
---|
576 | return *this;
|
---|
577 | }
|
---|
578 |
|
---|
579 | //! Multiply by a constant : ND *= b
|
---|
580 | template <class T>
|
---|
581 | NDataBlock<T>& NDataBlock<T>::operator *= (T b)
|
---|
582 | {
|
---|
583 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator*=v null size\n"));
|
---|
584 | T *p=Begin(), *pe=End(); while (p<pe) *p++ *= b;
|
---|
585 | return *this;
|
---|
586 | }
|
---|
587 |
|
---|
588 | //! Divide by a constant : ND /= b
|
---|
589 | template <class T>
|
---|
590 | NDataBlock<T>& NDataBlock<T>::operator /= (T b)
|
---|
591 | {
|
---|
592 | if(b==(T) 0) throw(ParmError("NDataBlock::operator/=v divide by zero\n"));
|
---|
593 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator/=v null size\n"));
|
---|
594 | T *p=Begin(), *pe=End(); while (p<pe) *p++ /= b;
|
---|
595 | return *this;
|
---|
596 | }
|
---|
597 |
|
---|
598 | ////////////////////////////////////////////////////////////////////
|
---|
599 | // Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += NDataBlock1; //
|
---|
600 | ////////////////////////////////////////////////////////////////////
|
---|
601 |
|
---|
602 | //! Add a NDataBlock : ND += NDa
|
---|
603 | template <class T>
|
---|
604 | NDataBlock<T>& NDataBlock<T>::operator += (const NDataBlock<T>& a)
|
---|
605 | {
|
---|
606 | if(mSz==0 || mSz!=a.mSz)
|
---|
607 | throw(SzMismatchError("NDataBlock::operator+=A size mismatch/null"));
|
---|
608 | T *p=Begin(), *pe=End();
|
---|
609 | T const * pa=a.Begin();
|
---|
610 | while (p<pe) *p++ += *pa++;
|
---|
611 | return *this;
|
---|
612 | }
|
---|
613 |
|
---|
614 | //! Substract a NDataBlock : ND -= NDa
|
---|
615 | template <class T>
|
---|
616 | NDataBlock<T>& NDataBlock<T>::operator -= (const NDataBlock<T>& a)
|
---|
617 | {
|
---|
618 | if(mSz==0 || mSz!=a.mSz)
|
---|
619 | throw(SzMismatchError("NDataBlock::operator-=A size mismatch/null"));
|
---|
620 | T *p=Begin(), *pe=End();
|
---|
621 | T const *pa=a.Begin();
|
---|
622 | while (p<pe) *p++ -= *pa++;
|
---|
623 | return *this;
|
---|
624 | }
|
---|
625 |
|
---|
626 | //! Multiply by a NDataBlock : ND *= NDa
|
---|
627 | template <class T>
|
---|
628 | NDataBlock<T>& NDataBlock<T>::operator *= (const NDataBlock<T>& a)
|
---|
629 | {
|
---|
630 | if(mSz==0 || mSz!=a.mSz)
|
---|
631 | throw(SzMismatchError("NDataBlock::operator*=A size mismatch/null"));
|
---|
632 | T *p=Begin(), *pe=End();
|
---|
633 | T const *pa=a.Begin();
|
---|
634 | while (p<pe) *p++ *= *pa++;
|
---|
635 | return *this;
|
---|
636 | }
|
---|
637 |
|
---|
638 | //! Divide by a NDataBlock : ND /= NDa
|
---|
639 | template <class T>
|
---|
640 | NDataBlock<T>& NDataBlock<T>::operator /= (const NDataBlock<T>& a)
|
---|
641 | // Attention, aucune protection si un element de "a" est nul.
|
---|
642 | {
|
---|
643 | if(mSz==0 || mSz!=a.mSz)
|
---|
644 | throw(SzMismatchError("NDataBlock::operator/=A size mismatch/null"));
|
---|
645 | T *p=Begin(), *pe=End();
|
---|
646 | T const *pa=a.Begin();
|
---|
647 | while (p<pe) *p++ /= *pa++; // Division par zero non protegee
|
---|
648 | return *this;
|
---|
649 | }
|
---|
650 |
|
---|
651 | //////////////////////////////////////////////////////////////////
|
---|
652 | // Pour surcharge de +,-,*,/ : NDataBlock = NDataBlock1+<T>b; //
|
---|
653 | // NDataBlock = <T>b+NDataBlock1; //
|
---|
654 | // Pour la notion de "temporaire" voir blabla dans CloneOrShare //
|
---|
655 | //////////////////////////////////////////////////////////////////
|
---|
656 |
|
---|
657 | //! Add a constant and return NDataBlock : NDret = ND + b
|
---|
658 | template <class T>
|
---|
659 | NDataBlock<T> NDataBlock<T>::Add(T b) const
|
---|
660 | // Pour A+b
|
---|
661 | {
|
---|
662 | NDataBlock<T> result;
|
---|
663 | result.CloneOrShare(*this); result.SetTemp(true);
|
---|
664 | result += b;
|
---|
665 | return result;
|
---|
666 | }
|
---|
667 |
|
---|
668 | //! Substract a constant and return NDataBlock : NDret = ND - b or NDret = b - ND
|
---|
669 | /*! Substract a constant or from a constant
|
---|
670 | \param fginv==false : performs NDret = ND - b (default)
|
---|
671 | \param fginv==true : performs NDret = b - ND
|
---|
672 | */
|
---|
673 | template <class T>
|
---|
674 | NDataBlock<T> NDataBlock<T>::Sub(T b,bool fginv) const
|
---|
675 | // Pour A-b sauf si fginv==true b-A (- n'est pas commutatif!)
|
---|
676 | {
|
---|
677 | NDataBlock<T> result;
|
---|
678 | result.CloneOrShare(*this); result.SetTemp(true);
|
---|
679 | if(fginv) {
|
---|
680 | T *p=result.Begin(), *pe=result.End();
|
---|
681 | T const *pa=this->Begin();
|
---|
682 | while(p<pe) {*p++ = b - *pa++;}
|
---|
683 | } else result -= b;
|
---|
684 | return result;
|
---|
685 | }
|
---|
686 |
|
---|
687 | //! Multiply by a constant and return NDataBlock : NDret = ND * b
|
---|
688 | template <class T>
|
---|
689 | NDataBlock<T> NDataBlock<T>::Mul(T b) const
|
---|
690 | // Pour A*b
|
---|
691 | {
|
---|
692 | NDataBlock<T> result;
|
---|
693 | result.CloneOrShare(*this); result.SetTemp(true);
|
---|
694 | result *= b;
|
---|
695 | return result;
|
---|
696 | }
|
---|
697 |
|
---|
698 | //! Divide by a constant and return NDataBlock : NDret = ND / b or NDret = b / ND
|
---|
699 | /*! Divide by a constant or from a constant
|
---|
700 | \param fginv==false : performs NDret = ND / b (default)
|
---|
701 | \param fginv==true : performs NDret = b / ND
|
---|
702 | */
|
---|
703 | template <class T>
|
---|
704 | NDataBlock<T> NDataBlock<T>::Div(T b,bool fginv) const
|
---|
705 | // Pour A/b sauf si fginv==true b/A (/ n'est pas commutatif!)
|
---|
706 | {
|
---|
707 | NDataBlock<T> result;
|
---|
708 | result.CloneOrShare(*this); result.SetTemp(true);
|
---|
709 | if(fginv) {
|
---|
710 | T *p=result.Begin(), *pe=result.End();
|
---|
711 | T const *pa = this->Begin();
|
---|
712 | while(p<pe) {*p++ = b / *pa++;} // Division par zero non protegee
|
---|
713 | } else {
|
---|
714 | if( b == (T) 0 ) throw MathExc("NDataBlock<T>::Div(T) - Divide by zero ! ");
|
---|
715 | result /= b;
|
---|
716 | }
|
---|
717 | return result;
|
---|
718 | }
|
---|
719 |
|
---|
720 | ///////////////////////////////////////////////////////////////////////
|
---|
721 | // Pour surcharge de +,-,*,/ : NDataBlock = NDataBlock1+NDataBlock2; //
|
---|
722 | ///////////////////////////////////////////////////////////////////////
|
---|
723 |
|
---|
724 | //! Add a NDataBlock and return a NDataBlock: ND = NDthis + NDb
|
---|
725 | template <class T>
|
---|
726 | NDataBlock<T> NDataBlock<T>::Add(const NDataBlock<T>& b) const
|
---|
727 | // Pour A+B
|
---|
728 | {
|
---|
729 | if(mSz!=b.mSz)
|
---|
730 | throw(SzMismatchError("NDataBlock operator C=A+B size mismatch/null\n"));
|
---|
731 | NDataBlock<T> result; result.SetTemp(true);
|
---|
732 | if(b.IsTemp()) {result.Share(b); result += *this;}
|
---|
733 | else {result.CloneOrShare(*this); result += b;}
|
---|
734 | return result;
|
---|
735 | }
|
---|
736 |
|
---|
737 | //! Multiply by a NDataBlock and return a NDataBlock: ND = NDthis * NDb
|
---|
738 | template <class T>
|
---|
739 | NDataBlock<T> NDataBlock<T>::Mul(const NDataBlock<T>& b) const
|
---|
740 | // Pour A*B
|
---|
741 | {
|
---|
742 | if(mSz!=b.mSz)
|
---|
743 | throw(SzMismatchError("NDataBlock operator C=A*B size mismatch/null\n"));
|
---|
744 | NDataBlock<T> result; result.SetTemp(true);
|
---|
745 | if(b.IsTemp()) {result.Share(b); result *= *this;}
|
---|
746 | else {result.CloneOrShare(*this); result *= b;}
|
---|
747 | return result;
|
---|
748 | }
|
---|
749 |
|
---|
750 | //! Substract a NDataBlock and return a NDataBlock: ND = NDthis - NDb
|
---|
751 | template <class T>
|
---|
752 | NDataBlock<T> NDataBlock<T>::Sub(const NDataBlock<T>& b) const
|
---|
753 | // Pour A-B
|
---|
754 | {
|
---|
755 | if(mSz!=b.mSz)
|
---|
756 | throw(SzMismatchError("NDataBlock operator C=A-B size mismatch/null\n"));
|
---|
757 | NDataBlock<T> result; result.SetTemp(true);
|
---|
758 | if(b.IsTemp()) {
|
---|
759 | result.Share(b);
|
---|
760 | T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
|
---|
761 | while(p<pe) {*p = *pa++ - *p; p++;}
|
---|
762 | } else {result.CloneOrShare(*this); result -= b;}
|
---|
763 | return result;
|
---|
764 | }
|
---|
765 |
|
---|
766 | //! Divide by a NDataBlock and return a NDataBlock: ND = NDthis / NDb
|
---|
767 | template <class T>
|
---|
768 | NDataBlock<T> NDataBlock<T>::Div(const NDataBlock<T>& b) const
|
---|
769 | // Pour A/B
|
---|
770 | {
|
---|
771 | if(mSz!=b.mSz)
|
---|
772 | throw(SzMismatchError("NDataBlock operator C=A/B size mismatch/null\n"));
|
---|
773 | NDataBlock<T> result; result.SetTemp(true);
|
---|
774 | if(b.IsTemp()) {
|
---|
775 | result.Share(b);
|
---|
776 | T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
|
---|
777 | while(p<pe) {*p = *pa++ / *p; p++;} // Division par zero non protegee
|
---|
778 | } else {result.CloneOrShare(*this); result /= b;}
|
---|
779 | return result;
|
---|
780 | }
|
---|
781 |
|
---|
782 |
|
---|
783 | ///////////////////////////////////////////////////////////////
|
---|
784 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
785 | #pragma define_template NDataBlock<uint_1>
|
---|
786 | #pragma define_template NDataBlock<uint_2>
|
---|
787 | #pragma define_template NDataBlock<int_2>
|
---|
788 | #pragma define_template NDataBlock<int_4>
|
---|
789 | #pragma define_template NDataBlock<int_8>
|
---|
790 | #pragma define_template NDataBlock<uint_4>
|
---|
791 | #pragma define_template NDataBlock<uint_8>
|
---|
792 | #pragma define_template NDataBlock<r_4>
|
---|
793 | #pragma define_template NDataBlock<r_8>
|
---|
794 | #pragma define_template NDataBlock< complex<r_4> >
|
---|
795 | #pragma define_template NDataBlock< complex<r_8> >
|
---|
796 | #endif
|
---|
797 |
|
---|
798 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
799 | namespace SOPHYA {
|
---|
800 | template class NDataBlock<uint_1>;
|
---|
801 | template class NDataBlock<uint_2>;
|
---|
802 | template class NDataBlock<int_2>;
|
---|
803 | template class NDataBlock<int_4>;
|
---|
804 | template class NDataBlock<int_8>;
|
---|
805 | template class NDataBlock<uint_4>;
|
---|
806 | template class NDataBlock<uint_8>;
|
---|
807 | template class NDataBlock<r_4>;
|
---|
808 | template class NDataBlock<r_8>;
|
---|
809 | template class NDataBlock< complex<r_4> >;
|
---|
810 | template class NDataBlock< complex<r_8> >;
|
---|
811 | }
|
---|
812 | #endif
|
---|