source: Sophya/trunk/Poubelle/DPC:FitsIOServer/SysTools/ndatablock.cc@ 658

Last change on this file since 658 was 658, checked in by ansari, 26 years ago

no message

File size: 22.4 KB
Line 
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#include "objfio.h"
12
13// define DEBUG_NDATABLOCK
14
15#ifdef DEBUG_NDATABLOCK
16 static size_t NallocData = 0;
17 static size_t NallocSRef = 0;
18#endif
19
20////////////////////////////////////////////////////////////////
21//************ Createur, Destructeur
22
23template <class T>
24NDataBlock<T>::NDataBlock(size_t n)
25// Createur d'une structure de "n" donnees
26: mSz(0), mSRef(NULL), mIsTemp(false)
27{
28#ifdef DEBUG_NDATABLOCK
29cout<<"?_NDataBlock::NDataBlock("<<this<<",n="<<n<<")"<<endl;
30#endif
31
32Alloc(n);
33}
34
35template <class T>
36NDataBlock<T>::NDataBlock(size_t n, T* data, Bridge* br)
37// Createur d'une structure de "n" donnees, avec donnees preallouees.
38// Attention createur TRES DANGEREUX (Voir explications dans Alloc()).
39: mSz(0), mSRef(NULL), mIsTemp(false)
40{
41#ifdef DEBUG_NDATABLOCK
42cout<<"?_NDataBlock::NDataBlock("<<this
43 <<",data="<<data<<",br="<<br<<")"<<endl;
44#endif
45
46Alloc(n,data,br);
47}
48
49template <class T>
50NDataBlock<T>::NDataBlock()
51// Createur par default
52: mSz(0), mSRef(NULL), mIsTemp(false)
53{
54#ifdef DEBUG_NDATABLOCK
55cout<<"?_NDataBlock::NDataBlock("<<this<<") default"<<endl;
56#endif
57}
58
59template <class T>
60NDataBlock<T>::NDataBlock(const NDataBlock<T>& a)
61// Createur par copie: partage les donnees si "a" temporaire, clone sinon.
62: mSz(0), mSRef(NULL), mIsTemp(false)
63{
64#ifdef DEBUG_NDATABLOCK
65cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<")"<<endl;
66#endif
67
68CloneOrShare(a);
69}
70
71template <class T>
72NDataBlock<T>::NDataBlock(const NDataBlock<T>& a,bool share)
73// Createur avec choix de partager ou non selon "share"
74: mSz(0), mSRef(NULL), mIsTemp(false)
75{
76#ifdef DEBUG_NDATABLOCK
77cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<",sh=<<"<<share<<")"<<endl;
78#endif
79
80if(share) Share(a); else Clone(a);
81}
82
83template <class T>
84NDataBlock<T>::~NDataBlock()
85// Destructeur
86{
87#ifdef DEBUG_NDATABLOCK
88cout<<"?_NDataBlock::~NDataBlock("<<this<<")"<<endl;
89#endif
90
91Delete();
92}
93
94////////////////////////////////////////////////////////////////
95//************ Gestion de donnees
96
97template <class T>
98void NDataBlock<T>::Alloc(size_t n,T* data,Bridge* br)
99// Allocation d'un NOUVEL espace de stoquage de "n" donnees
100// Si data==NULL : allocation de l'espace memoire (remplit de zeros)
101// data!=NULL : partage des donnees avec l'adresse data
102// Si br==NULL : les donnees nous appartiennent
103// br!=NULL : les donnees ne nous appartiennent pas (ex: Blitz)
104//
105// Exemple: on veut connecter a un tableau de T*
106// > float *x = new float[5]; ... remplissage de x[] ...;
107// 1- On veut que NDataBlock NE DESALLOUE PAS le tableau "x[]"
108// a- Premiere solution
109// > NDataBlock A(5,x,new Bridge);
110// ......
111// > delete [] x;
112// - Il faut deleter x[] explicitement.
113// - Le destructeur de "A" ne detruit pas x[].
114// ATTENTION: Une fois x[] detruit, "A" ne peut
115// plus acceder les donnees!
116// - Bridge est detruit par le destructeur de "A"
117// b- Autre solution:
118// > NDataBlock A(5); A.FillFrom(5,x);
119// > delete [] x;
120// ......
121// - Il faut deleter x[] explicitement.
122// - "A" possede une copie en local de x[].
123// - Le destructeur de "A" ne detruit pas x[] mais la copie locale.
124// 2- On veut que NDataBlock desalloue le tableau
125// > NDataBlock A(5,x);
126// - Ne Pas Faire "delete [] x;"
127// - "A" partage les donnees avec x[].
128// - Le destructeur de "A" detruit x[].
129//
130// --- REMARQUE SUR LE DANGER DE CERTAINES SITUATIONS (CMV):
131// 1-/ x = new float[n1]; NDataBlock A(n2,x);
132// 1er danger: si n2>n1 depassement de tableaux (core dump)
133// 2sd danger: celui qui alloue x[] ne doit pas faire le "delete"
134// en desaccord avec toutes les regles de bonne conduite.
135// 2-/ float x[5]={1,2,3,4,5}; {NDataBlock A(n2,&x[0]);} cout<<x[2];
136// Ici, a la sortie du bloc {}, le destructeur de "A" va detruire
137// l'adresse de &x[0]: je n'ose imaginer que ca se fasse sans probleme
138// et de toute facon, cout<<x[2]; va surement faire des etincelles.
139// 3-/ x = new float[n1]; NDataBlock A(n2,x,new Bridge);
140// 1er danger: si n2>n1 depassement de tableaux (core dump)
141// 2sd danger: si la methode bridgee (blitz?) detruit x[]
142// "A" n'a plus de donnees connectees!
143// --- CONCLUSION
144// Cette classe est franchement merdique.
145// - On peut accepter la prise de risque liee a NDataBlock(n2,x,new Bridge);
146// car je ne vois pas comment on pourrait faire autrement pour connecter
147// un tableau de type blitz par exemple.
148// - Par contre le createur NDataBlock(n2,x); doit etre interdit
149// dans sa forme actelle car trop dangereux et il me semble inutile.
150// - Dans cette nouvelle optique:
151// NDataBlock(n2,x,new Bridge) et NDataBlock(n2,x) disparaissent
152// On remplace par NDataBlock(n2,x) {Alloc(n2,x,new Bridge);}
153// qui force le Bridge dans tout les cas puisque NDataBlock
154// ne possede pas les donnees.
155// Mais puis-je encore le faire vu que NDataBlock est a la base
156// de TVector,TMatrix et qu'il faut donc reprendre tout le code DPC
157// - Quoiqu'il arrive Alloc est une methode privee et peut donc rester
158// sous sa forme actuelle.
159//
160{
161#ifdef DEBUG_NDATABLOCK
162cout<<"?_NDataBlock::Alloc("<<this<<","
163 <<n<<","<<data<<","<<br<<") mSz="<<mSz
164 <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
165#endif
166
167if(br && !data)
168 throw(NullPtrError("NDataBlock::Alloc br!=NULL && data==NULL\n"));
169if(n==0) throw(SzMismatchError("NDataBlock::Alloc n==0\n"));
170if(mSRef) Delete();
171mSz = n;
172mSRef = new NDREF;
173mSRef->nref = 1;
174if(data) mSRef->data = data;
175else {mSRef->data = new T[n]; memset(mSRef->data,0,n*sizeof(T));}
176mSRef->bridge = br;
177
178#ifdef DEBUG_NDATABLOCK
179// Meme dans le cas data!=0 et br==0 (connexion d'un tableau
180// avec destruction geree par ~NDataBlock (cas 2-) on compte
181// comme si on avait fait une allocation du tableau (ce qui a ete
182// fait au niveau du dessus!).
183if(!br) NallocData++; NallocSRef++;
184cout<<"...?_NDataBlock::Alloc mSz="<<mSz<<" mSRef="<<mSRef
185 <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<< mSRef->data
186 <<" mSRef->bridge="<<mSRef->bridge<<" IsTemp="<<mIsTemp
187 <<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
188#endif
189}
190
191template <class T>
192void NDataBlock<T>::Clone(const NDataBlock<T>& a)
193// Clone: copie de donnees a partir de "a"
194{
195#ifdef DEBUG_NDATABLOCK
196cout<<"?_NDataBlock::Clone("<<this<<","<<&a<<") a.(mSz="
197 <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp()
198 <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
199#endif
200
201if(&a==NULL) throw(NullPtrError("NDataBlock::Clone &a==NULL\n"));
202if(!a.mSRef || a.mSz==0) throw(SzMismatchError("NDataBlock::Clone a.mSz==0\n"));
203Alloc(a.mSz);
204memcpy(Data(),a.Data(),mSz*sizeof(T));
205}
206
207template <class T>
208void NDataBlock<T>::CloneOrShare(const NDataBlock<T>& a)
209// CloneOrShare: Share si "a" temporaire, Clone sinon.
210{
211#ifdef DEBUG_NDATABLOCK
212cout<<"?_NDataBlock::CloneOrShare("<<this<<","<<&a<<")"<<endl;
213#endif
214
215if(&a==NULL) throw(NullPtrError("NDataBlock::CloneOrShare &a==NULL\n"));
216if(a.IsTemp()) Share(a); else Clone(a);
217}
218
219template <class T>
220void NDataBlock<T>::Share(const NDataBlock<T>& a)
221// Share: Partage les donnees avec "a"
222{
223#ifdef DEBUG_NDATABLOCK
224cout<<"?_NDataBlock::Share("<<this<<","<<&a<<")";
225if(&a!=NULL) cout<<" a.(mSz="<<a.mSz<<" mSRef="<<a.mSRef
226 <<" IsTemp="<<a.IsTemp()<<")";
227cout<<", mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
228#endif
229
230if(&a==NULL) throw(NullPtrError("NDataBlock::Share &a==NULL\n"));
231if(!a.mSRef || a.mSz==0) throw(NullPtrError("NDataBlock::Share a.mSz=0\n"));
232if(mSRef) Delete();
233mSz = a.mSz; mSRef = a.mSRef; mSRef->nref++;
234
235#ifdef DEBUG_NDATABLOCK
236cout<<"...?_NDataBlock::Share mSz="<<mSz<<" mSRef="<<mSRef
237 <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<< mSRef->data
238 <<" mSRef->bridge="<<mSRef->bridge<<" IsTemp="<<mIsTemp<<endl;
239#endif
240}
241
242template <class T>
243void NDataBlock<T>::Delete(void)
244// Pour detruire les pointeurs en tenant compte des references
245{
246#ifdef DEBUG_NDATABLOCK
247cout<<"?_NDataBlock::Delete("<<this<<") mSz="<<mSz
248 <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp;
249if(mSRef)
250 cout<<" mSRef->nref="<<mSRef->nref<<" mSRef->data="
251 <<mSRef->data<<" mSRef->bridge="<<mSRef->bridge;
252cout<<endl;
253#endif
254
255if(mSRef==NULL) return;
256mSRef->nref--;
257if(mSRef->nref != 0) {
258
259#ifdef DEBUG_NDATABLOCK
260 cout<<"...?_NDataBlock::Delete() pas de desallocation il reste nref="
261 <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
262#endif
263
264 mSz = 0; mSRef=NULL;
265 return;
266}
267
268#ifdef DEBUG_NDATABLOCK
269if(!mSRef->bridge) NallocData--; NallocSRef--;
270cout<<"...?_NDataBlock::Delete() desallocation complete il reste nref="
271 <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
272#endif
273
274// Si il y a un Bridge les donnees ne n'appartiennent pas, on detruit le Bridge
275// sinon, les donnees ont ete allouees par nos soins, on libere l'espace
276if(mSRef->bridge) delete mSRef->bridge; else delete [] mSRef->data;
277mSRef->bridge=NULL; mSRef->data=NULL;
278delete mSRef; mSRef=NULL; mSz = 0;
279}
280
281template <class T>
282void NDataBlock<T>::FillFrom(size_t n,T* data)
283// Remplissage par un tableau de donnees
284// - Si classe vide : creation de l'espace memoire
285// - Si classe connectee : on ecrit selon la longueur minimale
286// (cad this->mSz ou "n")
287{
288if(data==NULL) throw(NullPtrError("NDataBlock::FillFrom data==NULL\n"));
289if(n==0) throw(ParmError("NDataBlock::FillFrom n<=0\n"));
290if(mSRef==NULL) Alloc(n);
291if(mSz<n) n = mSz;
292memcpy(Data(),data,n*sizeof(T));
293}
294
295template <class T>
296void NDataBlock<T>::Realloc(size_t nnew,bool force)
297// Re-allocation de "nnew" place memoire pour les donnees
298// avec conservation des "nold" donnees precedentes si possible.
299// "force" gere la re-allocation de la place memoire pour les donnees.
300// Divers cas se presentent:
301// a-/ *** nnew>nold force=quelconque ***
302// place re-allouee, donnees [0,nold[ copiees, surplus [nold,new[ mis a zero
303// b-/ *** nnew<=nold force=true ***
304// place re-allouee, donnees [0,nnew[ copiees, pas de surplus
305// c-/ *** nnew<=nold force=false ***
306// place non re-allouee, seule la valeur de la taille est diminuee
307// - On tient compte du partage des donnees dans tous les cas.
308// - Si il n'y a pas de donnees connectees a la classe, on re-alloue
309// dans tous les cas
310{
311if(nnew==0) throw(ParmError("NDataBlock::Realloc n<=0\n"));
312
313// Cas sans re-allocation memoire
314if(mSRef && nnew<=mSz && ! force) { mSz=nnew; return;}
315
316// Cas avec re-allocation memoire
317size_t ncop;
318if(!mSRef || mSz==0) ncop=0; else if(mSz<nnew) ncop=mSz; else ncop=nnew;
319T* dataloc = new T[nnew];
320if(ncop>0) memcpy(dataloc,mSRef->data,ncop*sizeof(T));
321if(nnew>ncop) memset(dataloc+ncop,0,(nnew-ncop)*sizeof(T));
322Alloc(nnew,dataloc,NULL); //Alloc gere partage de reference et bridge
323}
324
325////////////////////////////////////////////////////////////////
326//**** Impression
327
328template <class T>
329void NDataBlock<T>::Print(ostream& os,size_t i1,size_t n) const
330// Impression de n elements a partir de i1
331{
332size_t nr = 0;
333T* p = NULL; Bridge* br = NULL;
334if(mSRef) {nr = mSRef->nref; p = mSRef->data; br = mSRef->bridge;}
335os<<"NDataBlock::Print("<<this<<",Sz="<<mSz<<",IsTemp="<<mIsTemp<<")\n"
336 <<" mSRef="<<mSRef<<"(nref="<<nr<<",data="<<p
337 <<",bridge="<<br<<")"<<endl;
338if(i1>=mSz || n<=0 || !p) return;
339size_t i2 = i1+n; if(i2>mSz) i2=mSz;
340size_t im = 1; bool enl=false;
341while(i1<i2) {
342 enl = false;
343 os<<" "<<(*this)(i1); i1++;
344 if(im==8) {os<<"\n"; im=1; enl=true;} else im++;
345}
346if(!enl) os<<endl;
347}
348
349////////////////////////////////////////////////////////////////
350
351template <class T>
352T NDataBlock<T>::Sum(size_t i1,size_t n) const
353// Somme des elements de i1 a i1+n-1
354{
355if(i1>=mSz) return 0;
356if(n>mSz) n = mSz; if(n==0) n = mSz-i1;
357T const *p=Begin()+i1, *pe=p+n;
358T val = 0;
359while (p<pe) val += *p++;
360return val;
361}
362
363template <class T>
364T NDataBlock<T>::Product(size_t i1,size_t n) const
365// Produit des elements de i1 a i1+n-1
366{
367if(i1>=mSz) return 0;
368if(n>mSz) n = mSz; if(n==0) n = mSz-i1;
369T const *p=Begin()+i1, *pe=p+n;
370T val = 0;
371while (p<pe) val *= *p++;
372return val;
373}
374
375////////////////////////////////////////////////////////////////
376//**** Surcharge de = : NDataBlock=NDataBlock; NDataBlock=<T> b;
377
378template <class T>
379NDataBlock<T>& NDataBlock<T>::operator = (const NDataBlock<T>& a)
380// Affectation: partage des donnees si "a" temporaire, clone sinon.
381{
382#ifdef DEBUG_NDATABLOCK
383cout<<"?_NDataBlock::operator=("<<this<<","<<&a<<") a.(mSz="
384 <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp()
385 <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
386#endif
387
388if(this == &a) return *this;
389if(a.mSz==0)
390 throw(SzMismatchError("NDataBlock::operator=A null size\n"));
391CloneOrShare(a);
392return *this;
393}
394
395template <class T>
396NDataBlock<T>& NDataBlock<T>::operator = (T v)
397// Affectation de tous les elements a une constante "v"
398{
399#ifdef DEBUG_NDATABLOCK
400cout<<"?_NDataBlock::operator=("<<this<<","<<v<<")"
401 <<" mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
402#endif
403
404if(mSz==0) throw(SzMismatchError("NDataBlock::operator=v null size\n"));
405T *p=Begin(), *pe=End();
406while (p<pe) *p++ = v;
407return *this;
408}
409
410////////////////////////////////////////////////////////////////
411//**** Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += <T> b;
412
413template <class T>
414NDataBlock<T>& NDataBlock<T>::operator += (T b)
415{
416if(mSz==0) throw(SzMismatchError("NDataBlock::operator+=v null size\n"));
417T *p=Begin(), *pe=End();
418while (p<pe) *p++ += b;
419return *this;
420}
421
422template <class T>
423NDataBlock<T>& NDataBlock<T>::operator -= (T b)
424{
425if(mSz==0) throw(SzMismatchError("NDataBlock::operator-=v null size\n"));
426T *p=Begin(), *pe=End();
427while (p<pe) *p++ -= b;
428return *this;
429}
430
431template <class T>
432NDataBlock<T>& NDataBlock<T>::operator *= (T b)
433{
434if(mSz==0) throw(SzMismatchError("NDataBlock::operator*=v null size\n"));
435T *p=Begin(), *pe=End();
436while (p<pe) *p++ *= b;
437return *this;
438}
439
440template <class T>
441NDataBlock<T>& NDataBlock<T>::operator /= (T b)
442{
443if(b==(T) 0) throw(ParmError("NDataBlock::operator/=v divide by zero\n"));
444if(mSz==0) throw(SzMismatchError("NDataBlock::operator/=v null size\n"));
445T *p=Begin(), *pe=End();
446while (p<pe) *p++ /= b;
447return *this;
448}
449
450////////////////////////////////////////////////////////////////
451//**** Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += NDataBlock;
452
453template <class T>
454NDataBlock<T>& NDataBlock<T>::operator += (const NDataBlock<T>& a)
455{
456if(mSz==0 || mSz!=a.mSz)
457 throw(SzMismatchError("NDataBlock::operator+=A size mismatch/null"));
458T *p=Begin(), *pe=End();
459T const * pa=a.Begin();
460while (p<pe) *p++ += *pa++;
461return *this;
462}
463
464template <class T>
465NDataBlock<T>& NDataBlock<T>::operator -= (const NDataBlock<T>& a)
466{
467if(mSz==0 || mSz!=a.mSz)
468 throw(SzMismatchError("NDataBlock::operator-=A size mismatch/null"));
469T *p=Begin(), *pe=End();
470T const *pa=a.Begin();
471while (p<pe) *p++ -= *pa++;
472return *this;
473}
474
475template <class T>
476NDataBlock<T>& NDataBlock<T>::operator *= (const NDataBlock<T>& a)
477{
478if(mSz==0 || mSz!=a.mSz)
479 throw(SzMismatchError("NDataBlock::operator*=A size mismatch/null"));
480T *p=Begin(), *pe=End();
481T const *pa=a.Begin();
482while (p<pe) *p++ *= *pa++;
483return *this;
484}
485
486template <class T>
487NDataBlock<T>& NDataBlock<T>::operator /= (const NDataBlock<T>& a)
488// Attention, aucune protection si un element de "a" est nul.
489{
490if(mSz==0 || mSz!=a.mSz)
491 throw(SzMismatchError("NDataBlock::operator/=A size mismatch/null"));
492T *p=Begin(), *pe=End();
493T const *pa=a.Begin();
494while (p<pe) *p++ /= *pa++;
495return *this;
496}
497
498////////////////////////////////////////////////////////////////
499//**** Surcharge de +,-,*,/ : NDataBlock = NDataBlock+<T>b;
500// NDataBlock = <T>b+NDataBlock;
501
502template <class T>
503NDataBlock<T> NDataBlock<T>::Add(T b) const
504// Pour A+b
505{
506NDataBlock<T> result(*this); result.SetTemp(true);
507result += b;
508return result;
509}
510
511template <class T>
512NDataBlock<T> NDataBlock<T>::Sub(T b) const
513// Pour A-b
514{
515NDataBlock<T> result(*this); result.SetTemp(true);
516return result -= b;
517}
518
519template <class T>
520NDataBlock<T> NDataBlock<T>::SubInv(T b) const
521// Pour b-A
522{
523NDataBlock<T> result(*this); result.SetTemp(true);
524T *p=result.Begin(), *pe=result.End();
525T const *pa=this->Begin();
526while(p<pe) {*p++ = b - *pa++;}
527return result;
528}
529
530template <class T>
531NDataBlock<T> NDataBlock<T>::Mul(T b) const
532// Pour A*b
533{
534NDataBlock<T> result(*this); result.SetTemp(true);
535return result *= b;
536}
537
538template <class T>
539NDataBlock<T> NDataBlock<T>::Div(T b) const
540// Pour A/b
541{
542NDataBlock<T> result(*this); result.SetTemp(true);
543return result /= b;
544}
545
546template <class T>
547NDataBlock<T> NDataBlock<T>::DivInv(T b) const
548// Pour b/A
549{
550NDataBlock<T> result(*this); result.SetTemp(true);
551T *p=result.Begin(), *pe=result.End();
552T const *pa = this->Begin();
553while(p<pe) {*p++ = b / *pa++;}
554return result;
555}
556
557////////////////////////////////////////////////////////////////
558//**** Surcharge de +,-,*,/ : NDataBlock = NDataBlock+NDataBlock;
559
560template <class T>
561NDataBlock<T> NDataBlock<T>::Add(const NDataBlock<T>& b) const
562// Pour A+B
563{
564if(mSz!=b.mSz)
565 throw(SzMismatchError("NDataBlock operator C=A+B size mismatch/null\n"));
566NDataBlock<T> result; result.SetTemp(true);
567if(b.IsTemp()) {result.Share(b); result += *this;}
568 else {result.CloneOrShare(*this); result += b;}
569return result;
570}
571
572template <class T>
573NDataBlock<T> NDataBlock<T>::Mul(const NDataBlock<T>& b) const
574// Pour A*B
575{
576if(mSz!=b.mSz)
577 throw(SzMismatchError("NDataBlock operator C=A*B size mismatch/null\n"));
578NDataBlock<T> result; result.SetTemp(true);
579if(b.IsTemp()) {result.Share(b); result *= *this;}
580 else {result.CloneOrShare(*this); result *= b;}
581return result;
582}
583
584template <class T>
585NDataBlock<T> NDataBlock<T>::Sub(const NDataBlock<T>& b) const
586// Pour A-B
587{
588if(mSz!=b.mSz)
589 throw(SzMismatchError("NDataBlock operator C=A-B size mismatch/null\n"));
590NDataBlock<T> result; result.SetTemp(true);
591if(b.IsTemp()) {
592 result.Share(b);
593 T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
594 while(p<pe) {*p = *pa++ - *p; p++;}
595} else {result.CloneOrShare(*this); result -= b;}
596return result;
597}
598
599template <class T>
600NDataBlock<T> NDataBlock<T>::Div(const NDataBlock<T>& b) const
601// Pour A/B
602{
603if(mSz!=b.mSz)
604 throw(SzMismatchError("NDataBlock operator C=A/B size mismatch/null\n"));
605NDataBlock<T> result; result.SetTemp(true);
606if(b.IsTemp()) {
607 result.Share(b);
608 T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
609 while(p<pe) {*p = *pa++ / *p; p++;}
610} else {result.CloneOrShare(*this); result /= b;}
611return result;
612}
613
614////////////////////////////////////////////////////////////////
615// -------------------------------------------------------------------------
616// Les objets delegues pour la gestion de persistance
617// -------------------------------------------------------------------------
618
619/*
620template <class T>
621void ObjFileIO< NDataBlock<T> >::ReadSelf(PInPersist& is)
622template <class T>
623void ObjFileIO< NDataBlock<T> >::WriteSelf(POutPersist& os)
624*/
625
626// Pour pouvoir ecrire des tableaux de complex, en attendant
627// PIn/POutPersist::Get/Put(complex<>)
628#include "piocmplx.h"
629
630template <class T>
631FIO_NDataBlock<T>::FIO_NDataBlock()
632{
633dobj=new NDataBlock<T>;
634ownobj=true;
635}
636
637template <class T>
638FIO_NDataBlock<T>::FIO_NDataBlock(string const & filename)
639{
640dobj=new NDataBlock<T>;
641ownobj=true;
642Read(filename);
643}
644
645template <class T>
646FIO_NDataBlock<T>::FIO_NDataBlock(const NDataBlock<T> & obj)
647{
648dobj = new NDataBlock<T>(obj);
649ownobj=true;
650}
651
652template <class T>
653FIO_NDataBlock<T>::FIO_NDataBlock(NDataBlock<T> * obj)
654{
655dobj = obj;
656ownobj=false;
657}
658
659template <class T>
660FIO_NDataBlock<T>::~FIO_NDataBlock()
661{
662if (ownobj && dobj) delete dobj;
663}
664
665template <class T>
666AnyDataObj* FIO_NDataBlock<T>::DataObj()
667{
668return(dobj);
669}
670
671
672template <class T>
673void FIO_NDataBlock<T>::ReadSelf(PInPersist& is)
674{
675// On lit les 3 premiers uint_8
676uint_8 itab[3];
677is.Get(itab, 3);
678if (dobj == NULL) dobj = new NDataBlock<T>(itab[1]);
679else if (itab[1] != dobj->Size()) dobj->ReSize(itab[1]);
680// On lit le tableau de nombres
681PIOSReadArray(is, dobj->Data(), dobj->Size());
682}
683
684
685template <class T>
686void FIO_NDataBlock<T>::WriteSelf(POutPersist& os) const
687{
688if (dobj == NULL) return; // Attention - $CHECK$ Reza 26/04/99
689// On ecrit 3 uint_8
690// 0 : Numero de version, 1 : Taille, 2 reserve a l
691uint_8 itab[3];
692itab[0] = 1;
693itab[1] = dobj->Size();
694itab[2] = 0;
695os.Put(itab, 3);
696// On ecrit le tableau de nombres
697PIOSWriteArray(os, dobj->Data(), dobj->Size());
698}
699
700///////////////////////////////////////////////////////////////
701#ifdef __CXX_PRAGMA_TEMPLATES__
702#pragma define_template NDataBlock<uint_1>
703#pragma define_template NDataBlock<uint_2>
704#pragma define_template NDataBlock<int_2>
705#pragma define_template NDataBlock<int_4>
706#pragma define_template NDataBlock<int_8>
707#pragma define_template NDataBlock<uint_4>
708#pragma define_template NDataBlock<uint_8>
709#pragma define_template NDataBlock<r_4>
710#pragma define_template NDataBlock<r_8>
711#pragma define_template NDataBlock< complex<float> >
712#pragma define_template NDataBlock< complex<double> >
713// Instances des delegues FileIO (PPersist)
714#pragma define_template FIO_NDataBlock<uint_1>
715#pragma define_template FIO_NDataBlock<uint_2>
716#pragma define_template FIO_NDataBlock<int_2>
717#pragma define_template FIO_NDataBlock<int_4>
718#pragma define_template FIO_NDataBlock<int_8>
719#pragma define_template FIO_NDataBlock<uint_4>
720#pragma define_template FIO_NDataBlock<uint_8>
721#pragma define_template FIO_NDataBlock<r_8>
722#pragma define_template FIO_NDataBlock<r_4>
723#pragma define_template FIO_NDataBlock< complex<float> >
724#pragma define_template FIO_NDataBlock< complex<double> >
725#endif
726
727#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
728template class NDataBlock<uint_1>;
729template class NDataBlock<uint_2>;
730template class NDataBlock<int_2>;
731template class NDataBlock<int_4>;
732template class NDataBlock<int_8>;
733template class NDataBlock<uint_4>;
734template class NDataBlock<uint_8>;
735template class NDataBlock<r_4>;
736template class NDataBlock<r_8>;
737template class NDataBlock< complex<float> >;
738template class NDataBlock< complex<double> >;
739// Instances des delegues FileIO (PPersist)
740template class FIO_NDataBlock<uint_1>;
741template class FIO_NDataBlock<uint_2>;
742template class FIO_NDataBlock<int_2>;
743template class FIO_NDataBlock<int_4>;
744template class FIO_NDataBlock<int_8>;
745template class FIO_NDataBlock<uint_4>;
746template class FIO_NDataBlock<uint_8>;
747template class FIO_NDataBlock<r_8>;
748template class FIO_NDataBlock<r_4>;
749template class FIO_NDataBlock< complex<float> >;
750template class FIO_NDataBlock< complex<double> >;
751#endif
Note: See TracBrowser for help on using the repository browser.