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 shared if \b a is temporary, cloned if not.
|
---|
117 | */
|
---|
118 | template <class T>
|
---|
119 | NDataBlock<T>::NDataBlock(const NDataBlock<T>& a)
|
---|
120 | // Createur par copie: partage les donnees si "a" temporaire, clone sinon.
|
---|
121 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
122 | {
|
---|
123 | if(Debug_NDataBlock>1)
|
---|
124 | cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<")"<<endl;
|
---|
125 |
|
---|
126 | CloneOrShare(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(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(SzMismatchError("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 | template <class T>
|
---|
202 | void NDataBlock<T>::CloneOrShare(const NDataBlock<T>& a)
|
---|
203 | // CloneOrShare: Share si "a" temporaire, Clone sinon.
|
---|
204 | {
|
---|
205 | if(Debug_NDataBlock>1)
|
---|
206 | cout<<"?_NDataBlock::CloneOrShare("<<this<<","<<&a<<")"<<endl;
|
---|
207 |
|
---|
208 | if(&a==NULL) throw(NullPtrError("NDataBlock::CloneOrShare &a==NULL\n"));
|
---|
209 | if(a.IsTemp()) Share(a); else Clone(a);
|
---|
210 | }
|
---|
211 |
|
---|
212 | ////////////////////////////////////////////////////////////
|
---|
213 | // Allocation , destruction , remplissage et reallocation //
|
---|
214 | ////////////////////////////////////////////////////////////
|
---|
215 |
|
---|
216 | //! Allocation management
|
---|
217 | /*!
|
---|
218 | Allocation d'un NOUVEL espace de stoquage de "n" donnees
|
---|
219 | \verbatim
|
---|
220 | Si data==NULL : allocation de l'espace memoire
|
---|
221 | si zero == true , l'espace est remplis de zeros
|
---|
222 | data!=NULL : partage des donnees avec l'adresse data
|
---|
223 | Si br==NULL : les donnees nous appartiennent
|
---|
224 | br!=NULL : les donnees ne nous appartiennent pas (ex: Blitz)
|
---|
225 |
|
---|
226 | Exemple: on veut connecter a un tableau de T*
|
---|
227 | > float *x = new float[5]; ... remplissage de x[] ...;
|
---|
228 | 1- On veut que NDataBlock NE DESALLOUE PAS le tableau "x[]"
|
---|
229 | a- Premiere solution
|
---|
230 | > NDataBlock A(5,x,new Bridge);
|
---|
231 | ......
|
---|
232 | > delete [] x;
|
---|
233 | - Il faut deleter x[] explicitement.
|
---|
234 | - Le destructeur de "A" ne detruit pas x[].
|
---|
235 | ATTENTION: Une fois x[] detruit, "A" ne peut
|
---|
236 | plus acceder les donnees!
|
---|
237 | - Bridge est detruit par le destructeur de "A"
|
---|
238 | b- Autre solution:
|
---|
239 | > NDataBlock A(5); A.FillFrom(5,x);
|
---|
240 | > delete [] x;
|
---|
241 | ......
|
---|
242 | - Il faut deleter x[] explicitement.
|
---|
243 | - "A" possede une copie en local de x[].
|
---|
244 | - Le destructeur de "A" ne detruit pas x[] mais la copie locale.
|
---|
245 | 2- On veut que NDataBlock desalloue le tableau
|
---|
246 | > NDataBlock A(5,x);
|
---|
247 | - Ne Pas Faire "delete [] x;"
|
---|
248 | - "A" partage les donnees avec x[].
|
---|
249 | - Le destructeur de "A" detruit x[].
|
---|
250 |
|
---|
251 | --- REMARQUE SUR LE DANGER DE CERTAINES SITUATIONS (CMV):
|
---|
252 | 1-/ x = new float[n1]; NDataBlock A(n2,x);
|
---|
253 | 1er danger: si n2>n1 depassement de tableaux (core dump)
|
---|
254 | 2sd danger: celui qui alloue x[] ne doit pas faire le "delete"
|
---|
255 | en desaccord avec toutes les regles de bonne conduite.
|
---|
256 | 2-/ float x[5]={1,2,3,4,5}; {NDataBlock A(n2,&x[0]);} cout<<x[2];
|
---|
257 | Ici, a la sortie du bloc {}, le destructeur de "A" va detruire
|
---|
258 | l'adresse de &x[0]: je n'ose imaginer que ca se fasse sans probleme
|
---|
259 | et de toute facon, cout<<x[2]; va surement faire des etincelles.
|
---|
260 | 3-/ x = new float[n1]; NDataBlock A(n2,x,new Bridge);
|
---|
261 | 1er danger: si n2>n1 depassement de tableaux (core dump)
|
---|
262 | 2sd danger: si la methode bridgee (blitz?) detruit x[]
|
---|
263 | "A" n'a plus de donnees connectees!
|
---|
264 | --- CONCLUSION
|
---|
265 | Cette classe est franchement merdique.
|
---|
266 | - On peut accepter la prise de risque liee a NDataBlock(n2,x,new Bridge);
|
---|
267 | car je ne vois pas comment on pourrait faire autrement pour connecter
|
---|
268 | un tableau de type blitz par exemple.
|
---|
269 | - Par contre le createur NDataBlock(n2,x); doit etre interdit
|
---|
270 | dans sa forme actelle car trop dangereux et il me semble inutile.
|
---|
271 | - Dans cette nouvelle optique:
|
---|
272 | NDataBlock(n2,x,new Bridge) et NDataBlock(n2,x) disparaissent
|
---|
273 | On remplace par NDataBlock(n2,x) {Alloc(n2,x,new Bridge);}
|
---|
274 | qui force le Bridge dans tout les cas puisque NDataBlock
|
---|
275 | ne possede pas les donnees.
|
---|
276 | Mais puis-je encore le faire vu que NDataBlock est a la base
|
---|
277 | de TVector,TMatrix et qu'il faut donc reprendre tout le code DPC
|
---|
278 | - Quoiqu'il arrive Alloc est une methode privee et peut donc rester
|
---|
279 | sous sa forme actuelle.
|
---|
280 |
|
---|
281 | \endverbatim
|
---|
282 | */
|
---|
283 | template <class T>
|
---|
284 | void NDataBlock<T>::Alloc(size_t n,T* data,Bridge* br,bool zero)
|
---|
285 | {
|
---|
286 | if(Debug_NDataBlock>1)
|
---|
287 | cout<<"?_NDataBlock::Alloc("<<this<<","
|
---|
288 | <<n<<","<<data<<","<<br<<") mSz="<<mSz
|
---|
289 | <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
|
---|
290 |
|
---|
291 | if(br && !data)
|
---|
292 | throw(NullPtrError("NDataBlock::Alloc br!=NULL && data==NULL\n"));
|
---|
293 | if(n==0) throw(SzMismatchError("NDataBlock::Alloc n==0\n"));
|
---|
294 | if(mSRef) Delete();
|
---|
295 | mSz = n;
|
---|
296 | mSRef = new NDREF;
|
---|
297 | mSRef->nref = 1;
|
---|
298 | if(data) mSRef->data = data;
|
---|
299 | else {mSRef->data = new T[n]; if (zero) memset(mSRef->data,0,n*sizeof(T));}
|
---|
300 | mSRef->bridge = br;
|
---|
301 |
|
---|
302 | if(Debug_NDataBlock>0) {
|
---|
303 | // Meme dans le cas data!=0 et br==0 (connexion d'un tableau
|
---|
304 | // avec destruction geree par ~NDataBlock (cas 2-) on compte
|
---|
305 | // comme si on avait fait une allocation du tableau (ce qui a ete
|
---|
306 | // fait au niveau du dessus!).
|
---|
307 | if(!br) NallocData++; NallocSRef++;
|
---|
308 | if(Debug_NDataBlock>1)
|
---|
309 | cout<<"...?_NDataBlock::Alloc mSz="<<mSz<<" mSRef="<<mSRef
|
---|
310 | <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<<mSRef->data
|
---|
311 | <<" mSRef->bridge="<<mSRef->bridge
|
---|
312 | <<" IsTemp="<<mIsTemp
|
---|
313 | <<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | //! Management of de-allocation
|
---|
318 | template <class T>
|
---|
319 | void NDataBlock<T>::Delete(void)
|
---|
320 | // Pour detruire les pointeurs en tenant compte des references
|
---|
321 | {
|
---|
322 | if(Debug_NDataBlock>1) {
|
---|
323 | cout<<"?_NDataBlock::Delete("<<this<<") mSz="<<mSz
|
---|
324 | <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp;
|
---|
325 | if(mSRef)
|
---|
326 | cout<<" mSRef->nref="<<mSRef->nref<<" mSRef->data="
|
---|
327 | <<mSRef->data<<" mSRef->bridge="<<mSRef->bridge;
|
---|
328 | cout<<endl;
|
---|
329 | }
|
---|
330 |
|
---|
331 | if(mSRef==NULL) return;
|
---|
332 |
|
---|
333 | mSRef->nref--;
|
---|
334 | if(mSRef->nref != 0) {
|
---|
335 |
|
---|
336 | if(Debug_NDataBlock>1)
|
---|
337 | cout<<"...?_NDataBlock::Delete() pas de desallocation il reste nref="
|
---|
338 | <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
|
---|
339 |
|
---|
340 | mSz = 0; mSRef=NULL;
|
---|
341 | return;
|
---|
342 | }
|
---|
343 |
|
---|
344 | if(Debug_NDataBlock>0) {
|
---|
345 | if(!mSRef->bridge) NallocData--; NallocSRef--;
|
---|
346 | if(Debug_NDataBlock>1)
|
---|
347 | cout<<"...?_NDataBlock::Delete() desallocation complete il reste nref="
|
---|
348 | <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
|
---|
349 | }
|
---|
350 |
|
---|
351 | // Si il y a un Bridge les donnees ne n'appartiennent pas, on detruit le Bridge
|
---|
352 | // sinon, les donnees ont ete allouees par nos soins, on libere l'espace
|
---|
353 | if(mSRef->bridge) delete mSRef->bridge; else delete [] mSRef->data;
|
---|
354 | mSRef->bridge=NULL; mSRef->data=NULL;
|
---|
355 | delete mSRef; mSRef=NULL; mSz = 0;
|
---|
356 | }
|
---|
357 |
|
---|
358 | //! Fill dats of this NDataBlock with the \b n datas pointed by \b data
|
---|
359 | /*!
|
---|
360 | \warning If class empty : allocate space in memory
|
---|
361 | \warning If class already connected : overwrite with minimum size
|
---|
362 | (\b n or \b mSz)
|
---|
363 | */
|
---|
364 | template <class T>
|
---|
365 | void NDataBlock<T>::FillFrom(size_t n,T* data)
|
---|
366 | // Remplissage par un tableau de donnees
|
---|
367 | // - Si classe vide : creation de l'espace memoire
|
---|
368 | // - Si classe connectee : on ecrit selon la longueur minimale
|
---|
369 | // (cad this->mSz ou "n")
|
---|
370 | {
|
---|
371 | if(data==NULL) throw(NullPtrError("NDataBlock::FillFrom data==NULL\n"));
|
---|
372 | if(n==0) throw(ParmError("NDataBlock::FillFrom n<=0\n"));
|
---|
373 | if(mSRef==NULL) Alloc(n, NULL, NULL, false); // Pas de mise a zero
|
---|
374 | if(mSz<n) n = mSz;
|
---|
375 | memcpy(Data(),data,n*sizeof(T));
|
---|
376 | }
|
---|
377 |
|
---|
378 | //! Re-allocate space for \b nnew datas
|
---|
379 | /*!
|
---|
380 | \param nnnew : new size
|
---|
381 | \param force : to manage the way re-allocation will be done (see after).
|
---|
382 | \verbatim
|
---|
383 | Re-allocation de "nnew" place memoire pour les donnees
|
---|
384 | avec conservation des "nold" donnees precedentes si possible.
|
---|
385 | "force" gere la re-allocation de la place memoire pour les donnees.
|
---|
386 | Divers cas se presentent:
|
---|
387 | a-/ *** nnew>nold force=quelconque ***
|
---|
388 | place re-allouee, donnees [0,nold[ copiees, surplus [nold,new[ mis a zero
|
---|
389 | b-/ *** nnew<=nold force=true ***
|
---|
390 | place re-allouee, donnees [0,nnew[ copiees, pas de surplus
|
---|
391 | c-/ *** nnew<=nold force=false ***
|
---|
392 | place non re-allouee, seule la valeur de la taille est diminuee
|
---|
393 | - On tient compte du partage des donnees dans tous les cas.
|
---|
394 | - Si il n'y a pas de donnees connectees a la classe, on re-alloue
|
---|
395 | dans tous les cas
|
---|
396 | \endverbatim
|
---|
397 | */
|
---|
398 | template <class T>
|
---|
399 | void NDataBlock<T>::Realloc(size_t nnew,bool force)
|
---|
400 | {
|
---|
401 | if(nnew==0) throw(ParmError("NDataBlock::Realloc n<=0\n"));
|
---|
402 |
|
---|
403 | // Cas sans re-allocation memoire
|
---|
404 | if(mSRef && nnew<=mSz && ! force) { mSz=nnew; return;}
|
---|
405 |
|
---|
406 | // Cas avec re-allocation memoire
|
---|
407 | size_t ncop;
|
---|
408 | if(!mSRef || mSz==0) ncop=0; else if(mSz<nnew) ncop=mSz; else ncop=nnew;
|
---|
409 | T* dataloc = new T[nnew];
|
---|
410 | if(ncop>0) memcpy(dataloc,mSRef->data,ncop*sizeof(T));
|
---|
411 | if(nnew>ncop) memset(dataloc+ncop,0,(nnew-ncop)*sizeof(T));
|
---|
412 | Alloc(nnew,dataloc,NULL); //Alloc gere partage de reference et bridge
|
---|
413 | }
|
---|
414 |
|
---|
415 | ////////////////
|
---|
416 | // Impression //
|
---|
417 | ////////////////
|
---|
418 |
|
---|
419 | //! Give infos and print \b n datas beginning at \b i1 on stream \b os.
|
---|
420 | template <class T>
|
---|
421 | void NDataBlock<T>::Print(ostream& os,size_t i1,size_t n) const
|
---|
422 | // Impression de n elements a partir de i1
|
---|
423 | {
|
---|
424 | size_t nr = 0;
|
---|
425 | T* p = NULL; Bridge* br = NULL;
|
---|
426 | if(mSRef) {nr = mSRef->nref; p = mSRef->data; br = mSRef->bridge;}
|
---|
427 | os<<"NDataBlock::Print("<<this<<",Sz="<<mSz<<",IsTemp="<<mIsTemp<<")\n"
|
---|
428 | <<" mSRef="<<mSRef<<"(nref="<<nr<<",data="<<p
|
---|
429 | <<",bridge="<<br<<")"<<endl;
|
---|
430 | if(i1>=mSz || n<=0 || !p) return;
|
---|
431 | size_t i2 = i1+n; if(i2>mSz) i2=mSz;
|
---|
432 | size_t im = 1; bool enl=false;
|
---|
433 | while(i1<i2) {
|
---|
434 | enl = false;
|
---|
435 | os<<" "<<(*this)(i1); i1++;
|
---|
436 | if(im==8) {os<<"\n"; im=1; enl=true;} else im++;
|
---|
437 | }
|
---|
438 | if(!enl) os<<endl;
|
---|
439 | }
|
---|
440 |
|
---|
441 | //////////////////////////////////////////////
|
---|
442 | // Calcul de la somme / produit des donnees //
|
---|
443 | //////////////////////////////////////////////
|
---|
444 |
|
---|
445 | //! Return sum of \b n datas beginning at data \b i1.
|
---|
446 | template <class T>
|
---|
447 | T NDataBlock<T>::Sum(size_t i1,size_t n) const
|
---|
448 | // Somme des elements de i1 a i1+n-1
|
---|
449 | {
|
---|
450 | if(i1>=mSz) return 0;
|
---|
451 | if(n>mSz) n = mSz; if(n==0) n = mSz-i1;
|
---|
452 | T const *p=Begin()+i1, *pe=p+n;
|
---|
453 | T val = 0;
|
---|
454 | while (p<pe) val += *p++;
|
---|
455 | return val;
|
---|
456 | }
|
---|
457 |
|
---|
458 | //! Return product of \b n datas beginning at data \b i1.
|
---|
459 | template <class T>
|
---|
460 | T NDataBlock<T>::Product(size_t i1,size_t n) const
|
---|
461 | // Produit des elements de i1 a i1+n-1
|
---|
462 | {
|
---|
463 | if(i1>=mSz) return 0;
|
---|
464 | if(n>mSz) n = mSz; if(n==0) n = mSz-i1;
|
---|
465 | T const *p=Begin()+i1, *pe=p+n;
|
---|
466 | T val = 0;
|
---|
467 | while (p<pe) val *= *p++;
|
---|
468 | return val;
|
---|
469 | }
|
---|
470 |
|
---|
471 | ///////////////////////////////////////////////////////////////
|
---|
472 | // Surcharge de = : NDataBlock=NDataBlock; NDataBlock=<T> b; //
|
---|
473 | ///////////////////////////////////////////////////////////////
|
---|
474 |
|
---|
475 | //! Operator = : ND = ND1
|
---|
476 | /*! \warning share if \b a is temporary, clone if not */
|
---|
477 | template <class T>
|
---|
478 | NDataBlock<T>& NDataBlock<T>::operator = (const NDataBlock<T>& a)
|
---|
479 | // Affectation: partage des donnees si "a" temporaire, clone sinon.
|
---|
480 | {
|
---|
481 | if(Debug_NDataBlock>1)
|
---|
482 | cout<<"?_NDataBlock::operator=("<<this<<","<<&a<<") a.(mSz="
|
---|
483 | <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp()
|
---|
484 | <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
|
---|
485 |
|
---|
486 | if(this == &a) return *this;
|
---|
487 | if(a.mSz==0)
|
---|
488 | throw(SzMismatchError("NDataBlock::operator=A null size\n"));
|
---|
489 | CloneOrShare(a);
|
---|
490 | return *this;
|
---|
491 | }
|
---|
492 |
|
---|
493 | //! Operator = : ND = \b v (at dats set to \b v).
|
---|
494 | template <class T>
|
---|
495 | NDataBlock<T>& NDataBlock<T>::operator = (T v)
|
---|
496 | // Affectation de tous les elements a une constante "v"
|
---|
497 | {
|
---|
498 | if(Debug_NDataBlock>1)
|
---|
499 | cout<<"?_NDataBlock::operator=("<<this<<","<<v<<")"
|
---|
500 | <<" mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
|
---|
501 |
|
---|
502 |
|
---|
503 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator=v null size\n"));
|
---|
504 | T *p=Begin(), *pe=End();
|
---|
505 | while (p<pe) *p++ = v;
|
---|
506 | return *this;
|
---|
507 | }
|
---|
508 |
|
---|
509 | //////////////////////////////////////////////////////////////
|
---|
510 | // Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += <T> b; //
|
---|
511 | //////////////////////////////////////////////////////////////
|
---|
512 |
|
---|
513 | //! Add a constant : ND += b
|
---|
514 | template <class T>
|
---|
515 | NDataBlock<T>& NDataBlock<T>::operator += (T b)
|
---|
516 | {
|
---|
517 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator+=v null size\n"));
|
---|
518 | T *p=Begin(), *pe=End();
|
---|
519 | while (p<pe) *p++ += b;
|
---|
520 | return *this;
|
---|
521 | }
|
---|
522 |
|
---|
523 | //! Substract a constant : ND -= b
|
---|
524 | template <class T>
|
---|
525 | NDataBlock<T>& NDataBlock<T>::operator -= (T b)
|
---|
526 | {
|
---|
527 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator-=v null size\n"));
|
---|
528 | T *p=Begin(), *pe=End();
|
---|
529 | while (p<pe) *p++ -= b;
|
---|
530 | return *this;
|
---|
531 | }
|
---|
532 |
|
---|
533 | //! Multiply by a constant : ND *= b
|
---|
534 | template <class T>
|
---|
535 | NDataBlock<T>& NDataBlock<T>::operator *= (T b)
|
---|
536 | {
|
---|
537 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator*=v null size\n"));
|
---|
538 | T *p=Begin(), *pe=End();
|
---|
539 | while (p<pe) *p++ *= b;
|
---|
540 | return *this;
|
---|
541 | }
|
---|
542 |
|
---|
543 | //! Divide by a constant : ND *= b
|
---|
544 | template <class T>
|
---|
545 | NDataBlock<T>& NDataBlock<T>::operator /= (T b)
|
---|
546 | {
|
---|
547 | if(b==(T) 0) throw(ParmError("NDataBlock::operator/=v divide by zero\n"));
|
---|
548 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator/=v null size\n"));
|
---|
549 | T *p=Begin(), *pe=End();
|
---|
550 | while (p<pe) *p++ /= b;
|
---|
551 | return *this;
|
---|
552 | }
|
---|
553 |
|
---|
554 | ////////////////////////////////////////////////////////////////////
|
---|
555 | // Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += NDataBlock1; //
|
---|
556 | ////////////////////////////////////////////////////////////////////
|
---|
557 |
|
---|
558 | //! Add a NDataBlock : ND += ND1
|
---|
559 | template <class T>
|
---|
560 | NDataBlock<T>& NDataBlock<T>::operator += (const NDataBlock<T>& a)
|
---|
561 | {
|
---|
562 | if(mSz==0 || mSz!=a.mSz)
|
---|
563 | throw(SzMismatchError("NDataBlock::operator+=A size mismatch/null"));
|
---|
564 | T *p=Begin(), *pe=End();
|
---|
565 | T const * pa=a.Begin();
|
---|
566 | while (p<pe) *p++ += *pa++;
|
---|
567 | return *this;
|
---|
568 | }
|
---|
569 |
|
---|
570 | //! Substract a NDataBlock : ND -= ND1
|
---|
571 | template <class T>
|
---|
572 | NDataBlock<T>& NDataBlock<T>::operator -= (const NDataBlock<T>& a)
|
---|
573 | {
|
---|
574 | if(mSz==0 || mSz!=a.mSz)
|
---|
575 | throw(SzMismatchError("NDataBlock::operator-=A size mismatch/null"));
|
---|
576 | T *p=Begin(), *pe=End();
|
---|
577 | T const *pa=a.Begin();
|
---|
578 | while (p<pe) *p++ -= *pa++;
|
---|
579 | return *this;
|
---|
580 | }
|
---|
581 |
|
---|
582 | //! Multiply by a NDataBlock : ND *= ND1
|
---|
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 | //! Divide by a NDataBlock : ND *= ND1
|
---|
595 | template <class T>
|
---|
596 | NDataBlock<T>& NDataBlock<T>::operator /= (const NDataBlock<T>& a)
|
---|
597 | // Attention, aucune protection si un element de "a" est nul.
|
---|
598 | {
|
---|
599 | if(mSz==0 || mSz!=a.mSz)
|
---|
600 | throw(SzMismatchError("NDataBlock::operator/=A size mismatch/null"));
|
---|
601 | T *p=Begin(), *pe=End();
|
---|
602 | T const *pa=a.Begin();
|
---|
603 | while (p<pe) *p++ /= *pa++;
|
---|
604 | return *this;
|
---|
605 | }
|
---|
606 |
|
---|
607 | ////////////////////////////////////////////////////////////////
|
---|
608 | // Pour surcharge de +,-,*,/ : NDataBlock = NDataBlock1+<T>b; //
|
---|
609 | // NDataBlock = <T>b+NDataBlock1; //
|
---|
610 | ////////////////////////////////////////////////////////////////
|
---|
611 |
|
---|
612 | //! Add a constant and return NDataBlock : NDret = ND + b
|
---|
613 | template <class T>
|
---|
614 | NDataBlock<T> NDataBlock<T>::Add(T b) const
|
---|
615 | // Pour A+b
|
---|
616 | {
|
---|
617 | NDataBlock<T> result(*this); result.SetTemp(true);
|
---|
618 | result += b;
|
---|
619 | return result;
|
---|
620 | }
|
---|
621 |
|
---|
622 | //! Substract a constant and return NDataBlock : NDret = ND - b
|
---|
623 | template <class T>
|
---|
624 | NDataBlock<T> NDataBlock<T>::Sub(T b) const
|
---|
625 | // Pour A-b
|
---|
626 | {
|
---|
627 | NDataBlock<T> result(*this); result.SetTemp(true);
|
---|
628 | return result -= b;
|
---|
629 | }
|
---|
630 |
|
---|
631 | //! Substract from a constant and return NDataBlock : NDret = b - ND
|
---|
632 | template <class T>
|
---|
633 | NDataBlock<T> NDataBlock<T>::SubInv(T b) const
|
---|
634 | // Pour b-A
|
---|
635 | {
|
---|
636 | NDataBlock<T> result(*this); result.SetTemp(true);
|
---|
637 | T *p=result.Begin(), *pe=result.End();
|
---|
638 | T const *pa=this->Begin();
|
---|
639 | while(p<pe) {*p++ = b - *pa++;}
|
---|
640 | return result;
|
---|
641 | }
|
---|
642 |
|
---|
643 | //! Multiply by a constant and return NDataBlock : NDret = ND * b
|
---|
644 | template <class T>
|
---|
645 | NDataBlock<T> NDataBlock<T>::Mul(T b) const
|
---|
646 | // Pour A*b
|
---|
647 | {
|
---|
648 | NDataBlock<T> result(*this); result.SetTemp(true);
|
---|
649 | return result *= b;
|
---|
650 | }
|
---|
651 |
|
---|
652 | //! Divide by a constant and return NDataBlock : NDret = ND / b
|
---|
653 | template <class T>
|
---|
654 | NDataBlock<T> NDataBlock<T>::Div(T b) const
|
---|
655 | // Pour A/b
|
---|
656 | {
|
---|
657 | NDataBlock<T> result(*this); result.SetTemp(true);
|
---|
658 | return result /= b;
|
---|
659 | }
|
---|
660 |
|
---|
661 | //! Divide from a constant and return NDataBlock : NDret = b / ND
|
---|
662 | template <class T>
|
---|
663 | NDataBlock<T> NDataBlock<T>::DivInv(T b) const
|
---|
664 | // Pour b/A
|
---|
665 | {
|
---|
666 | NDataBlock<T> result(*this); result.SetTemp(true);
|
---|
667 | T *p=result.Begin(), *pe=result.End();
|
---|
668 | T const *pa = this->Begin();
|
---|
669 | while(p<pe) {*p++ = b / *pa++;}
|
---|
670 | return result;
|
---|
671 | }
|
---|
672 |
|
---|
673 | ///////////////////////////////////////////////////////////////////////
|
---|
674 | // Pour surcharge de +,-,*,/ : NDataBlock = NDataBlock1+NDataBlock2; //
|
---|
675 | ///////////////////////////////////////////////////////////////////////
|
---|
676 |
|
---|
677 | //! Add a NDataBlock and return a NDataBlock: ND = NDthis + NDb
|
---|
678 | template <class T>
|
---|
679 | NDataBlock<T> NDataBlock<T>::Add(const NDataBlock<T>& b) const
|
---|
680 | // Pour A+B
|
---|
681 | {
|
---|
682 | if(mSz!=b.mSz)
|
---|
683 | throw(SzMismatchError("NDataBlock operator C=A+B size mismatch/null\n"));
|
---|
684 | NDataBlock<T> result; result.SetTemp(true);
|
---|
685 | if(b.IsTemp()) {result.Share(b); result += *this;}
|
---|
686 | else {result.CloneOrShare(*this); result += b;}
|
---|
687 | return result;
|
---|
688 | }
|
---|
689 |
|
---|
690 | //! Multiply by a NDataBlock and return a NDataBlock: ND = NDthis * NDb
|
---|
691 | template <class T>
|
---|
692 | NDataBlock<T> NDataBlock<T>::Mul(const NDataBlock<T>& b) const
|
---|
693 | // Pour A*B
|
---|
694 | {
|
---|
695 | if(mSz!=b.mSz)
|
---|
696 | throw(SzMismatchError("NDataBlock operator C=A*B size mismatch/null\n"));
|
---|
697 | NDataBlock<T> result; result.SetTemp(true);
|
---|
698 | if(b.IsTemp()) {result.Share(b); result *= *this;}
|
---|
699 | else {result.CloneOrShare(*this); result *= b;}
|
---|
700 | return result;
|
---|
701 | }
|
---|
702 |
|
---|
703 | //! Substract a NDataBlock and return a NDataBlock: ND = NDthis - NDb
|
---|
704 | template <class T>
|
---|
705 | NDataBlock<T> NDataBlock<T>::Sub(const NDataBlock<T>& b) const
|
---|
706 | // Pour A-B
|
---|
707 | {
|
---|
708 | if(mSz!=b.mSz)
|
---|
709 | throw(SzMismatchError("NDataBlock operator C=A-B size mismatch/null\n"));
|
---|
710 | NDataBlock<T> result; result.SetTemp(true);
|
---|
711 | if(b.IsTemp()) {
|
---|
712 | result.Share(b);
|
---|
713 | T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
|
---|
714 | while(p<pe) {*p = *pa++ - *p; p++;}
|
---|
715 | } else {result.CloneOrShare(*this); result -= b;}
|
---|
716 | return result;
|
---|
717 | }
|
---|
718 |
|
---|
719 | //! Divide by a NDataBlock and return a NDataBlock: ND = NDthis / NDb
|
---|
720 | template <class T>
|
---|
721 | NDataBlock<T> NDataBlock<T>::Div(const NDataBlock<T>& b) const
|
---|
722 | // Pour A/B
|
---|
723 | {
|
---|
724 | if(mSz!=b.mSz)
|
---|
725 | throw(SzMismatchError("NDataBlock operator C=A/B size mismatch/null\n"));
|
---|
726 | NDataBlock<T> result; result.SetTemp(true);
|
---|
727 | if(b.IsTemp()) {
|
---|
728 | result.Share(b);
|
---|
729 | T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
|
---|
730 | while(p<pe) {*p = *pa++ / *p; p++;}
|
---|
731 | } else {result.CloneOrShare(*this); result /= b;}
|
---|
732 | return result;
|
---|
733 | }
|
---|
734 |
|
---|
735 |
|
---|
736 | ///////////////////////////////////////////////////////////////
|
---|
737 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
738 | #pragma define_template NDataBlock<uint_1>
|
---|
739 | #pragma define_template NDataBlock<uint_2>
|
---|
740 | #pragma define_template NDataBlock<int_2>
|
---|
741 | #pragma define_template NDataBlock<int_4>
|
---|
742 | #pragma define_template NDataBlock<int_8>
|
---|
743 | #pragma define_template NDataBlock<uint_4>
|
---|
744 | #pragma define_template NDataBlock<uint_8>
|
---|
745 | #pragma define_template NDataBlock<r_4>
|
---|
746 | #pragma define_template NDataBlock<r_8>
|
---|
747 | #pragma define_template NDataBlock< complex<r_4> >
|
---|
748 | #pragma define_template NDataBlock< complex<r_8> >
|
---|
749 | #endif
|
---|
750 |
|
---|
751 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
752 | template class NDataBlock<uint_1>;
|
---|
753 | template class NDataBlock<uint_2>;
|
---|
754 | template class NDataBlock<int_2>;
|
---|
755 | template class NDataBlock<int_4>;
|
---|
756 | template class NDataBlock<int_8>;
|
---|
757 | template class NDataBlock<uint_4>;
|
---|
758 | template class NDataBlock<uint_8>;
|
---|
759 | template class NDataBlock<r_4>;
|
---|
760 | template class NDataBlock<r_8>;
|
---|
761 | template class NDataBlock< complex<r_4> >;
|
---|
762 | template class NDataBlock< complex<r_8> >;
|
---|
763 | #endif
|
---|