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