source: Sophya/trunk/SophyaLib/BaseTools/ndatablock.cc@ 764

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

Restruction de Sophya en modules plus petit (TArray , SkyMap, HiStats, ...)

Reza 2/3/2000

File size: 22.6 KB
RevLine 
[268]1// Gestion de block de donnees avec partage de references
[502]2// malheureusement tres mal concu... C.Magneville 04/99
[268]3// LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
[245]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"
[269]11#include "objfio.h"
[245]12
[275]13// define DEBUG_NDATABLOCK
[268]14
[245]15#ifdef DEBUG_NDATABLOCK
16 static size_t NallocData = 0;
17 static size_t NallocSRef = 0;
18#endif
19
[275]20////////////////////////////////////////////////////////////////
[289]21//************ Createur, Destructeur
[245]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{
[268]28#ifdef DEBUG_NDATABLOCK
[289]29cout<<"?_NDataBlock::NDataBlock("<<this<<",n="<<n<<")"<<endl;
[268]30#endif
31
[245]32Alloc(n);
33}
34
35template <class T>
36NDataBlock<T>::NDataBlock(size_t n, T* data, Bridge* br)
[502]37// Createur d'une structure de "n" donnees, avec donnees preallouees.
38// Attention createur TRES DANGEREUX (Voir explications dans Alloc()).
[245]39: mSz(0), mSRef(NULL), mIsTemp(false)
40{
[268]41#ifdef DEBUG_NDATABLOCK
[289]42cout<<"?_NDataBlock::NDataBlock("<<this
[268]43 <<",data="<<data<<",br="<<br<<")"<<endl;
44#endif
45
[245]46Alloc(n,data,br);
47}
48
49template <class T>
50NDataBlock<T>::NDataBlock()
51// Createur par default
52: mSz(0), mSRef(NULL), mIsTemp(false)
53{
[268]54#ifdef DEBUG_NDATABLOCK
[289]55cout<<"?_NDataBlock::NDataBlock("<<this<<") default"<<endl;
[268]56#endif
[245]57}
58
59template <class T>
[268]60NDataBlock<T>::NDataBlock(const NDataBlock<T>& a)
[289]61// Createur par copie: partage les donnees si "a" temporaire, clone sinon.
[245]62: mSz(0), mSRef(NULL), mIsTemp(false)
63{
[268]64#ifdef DEBUG_NDATABLOCK
[289]65cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<")"<<endl;
[268]66#endif
67
[289]68CloneOrShare(a);
[245]69}
70
71template <class T>
[268]72NDataBlock<T>::NDataBlock(const NDataBlock<T>& a,bool share)
[289]73// Createur avec choix de partager ou non selon "share"
[245]74: mSz(0), mSRef(NULL), mIsTemp(false)
75{
[268]76#ifdef DEBUG_NDATABLOCK
[289]77cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<",sh=<<"<<share<<")"<<endl;
[268]78#endif
79
[245]80if(share) Share(a); else Clone(a);
81}
82
83template <class T>
84NDataBlock<T>::~NDataBlock()
85// Destructeur
86{
[268]87#ifdef DEBUG_NDATABLOCK
[289]88cout<<"?_NDataBlock::~NDataBlock("<<this<<")"<<endl;
[268]89#endif
90
[245]91Delete();
92}
93
[275]94////////////////////////////////////////////////////////////////
[289]95//************ Gestion de donnees
[275]96
[245]97template <class T>
98void NDataBlock<T>::Alloc(size_t n,T* data,Bridge* br)
[285]99// Allocation d'un NOUVEL espace de stoquage de "n" donnees
[502]100// Si data==NULL : allocation de l'espace memoire (remplit de zeros)
[245]101// data!=NULL : partage des donnees avec l'adresse data
102// Si br==NULL : les donnees nous appartiennent
[275]103// br!=NULL : les donnees ne nous appartiennent pas (ex: Blitz)
104//
[257]105// Exemple: on veut connecter a un tableau de T*
[502]106// > float *x = new float[5]; ... remplissage de x[] ...;
107// 1- On veut que NDataBlock NE DESALLOUE PAS le tableau "x[]"
[275]108// a- Premiere solution
[502]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"
[275]117// b- Autre solution:
[502]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.
[275]124// 2- On veut que NDataBlock desalloue le tableau
[502]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//
[245]160{
[268]161#ifdef DEBUG_NDATABLOCK
[289]162cout<<"?_NDataBlock::Alloc("<<this<<","
[268]163 <<n<<","<<data<<","<<br<<") mSz="<<mSz
164 <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
165#endif
166
[275]167if(br && !data)
168 throw(NullPtrError("NDataBlock::Alloc br!=NULL && data==NULL\n"));
[245]169if(n==0) throw(SzMismatchError("NDataBlock::Alloc n==0\n"));
170if(mSRef) Delete();
171mSz = n;
172mSRef = new NDREF;
173mSRef->nref = 1;
[285]174if(data) mSRef->data = data;
175else {mSRef->data = new T[n]; memset(mSRef->data,0,n*sizeof(T));}
[245]176mSRef->bridge = br;
[268]177
178#ifdef DEBUG_NDATABLOCK
[275]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++;
[289]184cout<<"...?_NDataBlock::Alloc mSz="<<mSz<<" mSRef="<<mSRef
[268]185 <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<< mSRef->data
186 <<" mSRef->bridge="<<mSRef->bridge<<" IsTemp="<<mIsTemp
187 <<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
188#endif
[245]189}
190
191template <class T>
[268]192void NDataBlock<T>::Clone(const NDataBlock<T>& a)
[289]193// Clone: copie de donnees a partir de "a"
[245]194{
[275]195#ifdef DEBUG_NDATABLOCK
[289]196cout<<"?_NDataBlock::Clone("<<this<<","<<&a<<") a.(mSz="
[275]197 <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp()
198 <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
199#endif
200
[289]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));
[245]205}
206
207template <class T>
[289]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>
[268]220void NDataBlock<T>::Share(const NDataBlock<T>& a)
[289]221// Share: Partage les donnees avec "a"
[245]222{
[268]223#ifdef DEBUG_NDATABLOCK
[289]224cout<<"?_NDataBlock::Share("<<this<<","<<&a<<")";
[268]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
[245]230if(&a==NULL) throw(NullPtrError("NDataBlock::Share &a==NULL\n"));
[289]231if(!a.mSRef || a.mSz==0) throw(NullPtrError("NDataBlock::Share a.mSz=0\n"));
[245]232if(mSRef) Delete();
233mSz = a.mSz; mSRef = a.mSRef; mSRef->nref++;
[268]234
235#ifdef DEBUG_NDATABLOCK
[289]236cout<<"...?_NDataBlock::Share mSz="<<mSz<<" mSRef="<<mSRef
[268]237 <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<< mSRef->data
238 <<" mSRef->bridge="<<mSRef->bridge<<" IsTemp="<<mIsTemp<<endl;
239#endif
[245]240}
241
242template <class T>
243void NDataBlock<T>::Delete(void)
244// Pour detruire les pointeurs en tenant compte des references
245{
[268]246#ifdef DEBUG_NDATABLOCK
[289]247cout<<"?_NDataBlock::Delete("<<this<<") mSz="<<mSz
[268]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
[289]255if(mSRef==NULL) return;
[245]256mSRef->nref--;
257if(mSRef->nref != 0) {
[268]258
259#ifdef DEBUG_NDATABLOCK
[502]260 cout<<"...?_NDataBlock::Delete() pas de desallocation il reste nref="
261 <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
[268]262#endif
263
[285]264 mSz = 0; mSRef=NULL;
[245]265 return;
266}
[268]267
268#ifdef DEBUG_NDATABLOCK
269if(!mSRef->bridge) NallocData--; NallocSRef--;
[289]270cout<<"...?_NDataBlock::Delete() desallocation complete il reste nref="
[268]271 <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
272#endif
273
[245]274// Si il y a un Bridge les donnees ne n'appartiennent pas, on detruit le Bridge
[285]275// sinon, les donnees ont ete allouees par nos soins, on libere l'espace
276if(mSRef->bridge) delete mSRef->bridge; else delete [] mSRef->data;
[245]277mSRef->bridge=NULL; mSRef->data=NULL;
[285]278delete mSRef; mSRef=NULL; mSz = 0;
[245]279}
280
281template <class T>
[257]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
[289]285// - Si classe connectee : on ecrit selon la longueur minimale
286// (cad this->mSz ou "n")
[257]287{
288if(data==NULL) throw(NullPtrError("NDataBlock::FillFrom data==NULL\n"));
289if(n==0) throw(ParmError("NDataBlock::FillFrom n<=0\n"));
[289]290if(mSRef==NULL) Alloc(n);
[285]291if(mSz<n) n = mSz;
[257]292memcpy(Data(),data,n*sizeof(T));
293}
294
[502]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
[275]325////////////////////////////////////////////////////////////////
[245]326//**** Impression
327
328template <class T>
[268]329void NDataBlock<T>::Print(ostream& os,size_t i1,size_t n) const
[245]330// Impression de n elements a partir de i1
331{
332size_t nr = 0;
[267]333T* p = NULL; Bridge* br = NULL;
334if(mSRef) {nr = mSRef->nref; p = mSRef->data; br = mSRef->bridge;}
[268]335os<<"NDataBlock::Print("<<this<<",Sz="<<mSz<<",IsTemp="<<mIsTemp<<")\n"
336 <<" mSRef="<<mSRef<<"(nref="<<nr<<",data="<<p
337 <<",bridge="<<br<<")"<<endl;
[249]338if(i1>=mSz || n<=0 || !p) return;
[245]339size_t i2 = i1+n; if(i2>mSz) i2=mSz;
[269]340size_t im = 1; bool enl=false;
[245]341while(i1<i2) {
[257]342 enl = false;
[268]343 os<<" "<<(*this)(i1); i1++;
344 if(im==8) {os<<"\n"; im=1; enl=true;} else im++;
[245]345}
[268]346if(!enl) os<<endl;
[245]347}
348
[275]349////////////////////////////////////////////////////////////////
[289]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////////////////////////////////////////////////////////////////
[245]376//**** Surcharge de = : NDataBlock=NDataBlock; NDataBlock=<T> b;
377
378template <class T>
[268]379NDataBlock<T>& NDataBlock<T>::operator = (const NDataBlock<T>& a)
[289]380// Affectation: partage des donnees si "a" temporaire, clone sinon.
[245]381{
[268]382#ifdef DEBUG_NDATABLOCK
[289]383cout<<"?_NDataBlock::operator=("<<this<<","<<&a<<") a.(mSz="
[268]384 <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp()
385 <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
386#endif
387
[245]388if(this == &a) return *this;
[289]389if(a.mSz==0)
390 throw(SzMismatchError("NDataBlock::operator=A null size\n"));
391CloneOrShare(a);
[245]392return *this;
393}
394
395template <class T>
[249]396NDataBlock<T>& NDataBlock<T>::operator = (T v)
[289]397// Affectation de tous les elements a une constante "v"
[245]398{
[268]399#ifdef DEBUG_NDATABLOCK
[289]400cout<<"?_NDataBlock::operator=("<<this<<","<<v<<")"
[268]401 <<" mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
402#endif
403
[245]404if(mSz==0) throw(SzMismatchError("NDataBlock::operator=v null size\n"));
[249]405T *p=Begin(), *pe=End();
[245]406while (p<pe) *p++ = v;
407return *this;
408}
409
[275]410////////////////////////////////////////////////////////////////
[245]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"));
[249]417T *p=Begin(), *pe=End();
[245]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"));
[249]426T *p=Begin(), *pe=End();
[245]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"));
[249]435T *p=Begin(), *pe=End();
[245]436while (p<pe) *p++ *= b;
437return *this;
438}
439
440template <class T>
441NDataBlock<T>& NDataBlock<T>::operator /= (T b)
442{
[249]443if(b==(T) 0) throw(ParmError("NDataBlock::operator/=v divide by zero\n"));
[245]444if(mSz==0) throw(SzMismatchError("NDataBlock::operator/=v null size\n"));
[249]445T *p=Begin(), *pe=End();
[245]446while (p<pe) *p++ /= b;
447return *this;
448}
449
[275]450////////////////////////////////////////////////////////////////
[245]451//**** Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += NDataBlock;
452
453template <class T>
[268]454NDataBlock<T>& NDataBlock<T>::operator += (const NDataBlock<T>& a)
[245]455{
[265]456if(mSz==0 || mSz!=a.mSz)
457 throw(SzMismatchError("NDataBlock::operator+=A size mismatch/null"));
[268]458T *p=Begin(), *pe=End();
459T const * pa=a.Begin();
[289]460while (p<pe) *p++ += *pa++;
[245]461return *this;
462}
463
464template <class T>
[268]465NDataBlock<T>& NDataBlock<T>::operator -= (const NDataBlock<T>& a)
[245]466{
[265]467if(mSz==0 || mSz!=a.mSz)
468 throw(SzMismatchError("NDataBlock::operator-=A size mismatch/null"));
[268]469T *p=Begin(), *pe=End();
470T const *pa=a.Begin();
[289]471while (p<pe) *p++ -= *pa++;
[245]472return *this;
473}
474
475template <class T>
[268]476NDataBlock<T>& NDataBlock<T>::operator *= (const NDataBlock<T>& a)
[245]477{
[265]478if(mSz==0 || mSz!=a.mSz)
479 throw(SzMismatchError("NDataBlock::operator*=A size mismatch/null"));
[268]480T *p=Begin(), *pe=End();
481T const *pa=a.Begin();
[289]482while (p<pe) *p++ *= *pa++;
[245]483return *this;
484}
485
486template <class T>
[268]487NDataBlock<T>& NDataBlock<T>::operator /= (const NDataBlock<T>& a)
[289]488// Attention, aucune protection si un element de "a" est nul.
[245]489{
[265]490if(mSz==0 || mSz!=a.mSz)
491 throw(SzMismatchError("NDataBlock::operator/=A size mismatch/null"));
[268]492T *p=Begin(), *pe=End();
493T const *pa=a.Begin();
[245]494while (p<pe) *p++ /= *pa++;
495return *this;
496}
497
[275]498////////////////////////////////////////////////////////////////
499//**** Surcharge de +,-,*,/ : NDataBlock = NDataBlock+<T>b;
500// NDataBlock = <T>b+NDataBlock;
[245]501
502template <class T>
[268]503NDataBlock<T> NDataBlock<T>::Add(T b) const
[257]504// Pour A+b
[245]505{
[289]506NDataBlock<T> result(*this); result.SetTemp(true);
[268]507result += b;
508return result;
[245]509}
510
511template <class T>
[268]512NDataBlock<T> NDataBlock<T>::Sub(T b) const
[257]513// Pour A-b
[245]514{
[289]515NDataBlock<T> result(*this); result.SetTemp(true);
[259]516return result -= b;
[245]517}
518
519template <class T>
[268]520NDataBlock<T> NDataBlock<T>::SubInv(T b) const
[257]521// Pour b-A
[245]522{
[289]523NDataBlock<T> result(*this); result.SetTemp(true);
[268]524T *p=result.Begin(), *pe=result.End();
525T const *pa=this->Begin();
[245]526while(p<pe) {*p++ = b - *pa++;}
527return result;
528}
529
530template <class T>
[268]531NDataBlock<T> NDataBlock<T>::Mul(T b) const
[257]532// Pour A*b
[245]533{
[289]534NDataBlock<T> result(*this); result.SetTemp(true);
[259]535return result *= b;
[245]536}
537
538template <class T>
[268]539NDataBlock<T> NDataBlock<T>::Div(T b) const
[257]540// Pour A/b
[245]541{
[289]542NDataBlock<T> result(*this); result.SetTemp(true);
[259]543return result /= b;
[245]544}
545
546template <class T>
[268]547NDataBlock<T> NDataBlock<T>::DivInv(T b) const
[257]548// Pour b/A
[245]549{
[289]550NDataBlock<T> result(*this); result.SetTemp(true);
[268]551T *p=result.Begin(), *pe=result.End();
552T const *pa = this->Begin();
[245]553while(p<pe) {*p++ = b / *pa++;}
554return result;
555}
556
[275]557////////////////////////////////////////////////////////////////
[245]558//**** Surcharge de +,-,*,/ : NDataBlock = NDataBlock+NDataBlock;
559
560template <class T>
[268]561NDataBlock<T> NDataBlock<T>::Add(const NDataBlock<T>& b) const
[257]562// Pour A+B
[245]563{
[285]564if(mSz!=b.mSz)
[265]565 throw(SzMismatchError("NDataBlock operator C=A+B size mismatch/null\n"));
[268]566NDataBlock<T> result; result.SetTemp(true);
[289]567if(b.IsTemp()) {result.Share(b); result += *this;}
568 else {result.CloneOrShare(*this); result += b;}
[268]569return result;
[245]570}
571
572template <class T>
[268]573NDataBlock<T> NDataBlock<T>::Mul(const NDataBlock<T>& b) const
[257]574// Pour A*B
[245]575{
[285]576if(mSz!=b.mSz)
[265]577 throw(SzMismatchError("NDataBlock operator C=A*B size mismatch/null\n"));
[268]578NDataBlock<T> result; result.SetTemp(true);
[289]579if(b.IsTemp()) {result.Share(b); result *= *this;}
580 else {result.CloneOrShare(*this); result *= b;}
[268]581return result;
[245]582}
583
584template <class T>
[268]585NDataBlock<T> NDataBlock<T>::Sub(const NDataBlock<T>& b) const
[257]586// Pour A-B
[245]587{
[285]588if(mSz!=b.mSz)
[265]589 throw(SzMismatchError("NDataBlock operator C=A-B size mismatch/null\n"));
[268]590NDataBlock<T> result; result.SetTemp(true);
[245]591if(b.IsTemp()) {
[268]592 result.Share(b);
[289]593 T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
[245]594 while(p<pe) {*p = *pa++ - *p; p++;}
[289]595} else {result.CloneOrShare(*this); result -= b;}
[268]596return result;
[245]597}
598
599template <class T>
[268]600NDataBlock<T> NDataBlock<T>::Div(const NDataBlock<T>& b) const
[257]601// Pour A/B
[245]602{
[285]603if(mSz!=b.mSz)
[265]604 throw(SzMismatchError("NDataBlock operator C=A/B size mismatch/null\n"));
[268]605NDataBlock<T> result; result.SetTemp(true);
[245]606if(b.IsTemp()) {
[268]607 result.Share(b);
[289]608 T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
[245]609 while(p<pe) {*p = *pa++ / *p; p++;}
[289]610} else {result.CloneOrShare(*this); result /= b;}
[268]611return result;
[245]612}
613
[275]614////////////////////////////////////////////////////////////////
[269]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
[277]626// Pour pouvoir ecrire des tableaux de complex, en attendant
627// PIn/POutPersist::Get/Put(complex<>)
[441]628#include "piocmplx.h"
[277]629
[269]630template <class T>
631FIO_NDataBlock<T>::FIO_NDataBlock()
632{
633dobj=new NDataBlock<T>;
[277]634ownobj=true;
[269]635}
636
637template <class T>
638FIO_NDataBlock<T>::FIO_NDataBlock(string const & filename)
639{
[277]640dobj=new NDataBlock<T>;
641ownobj=true;
[269]642Read(filename);
643}
644
645template <class T>
646FIO_NDataBlock<T>::FIO_NDataBlock(const NDataBlock<T> & obj)
647{
[277]648dobj = new NDataBlock<T>(obj);
649ownobj=true;
[269]650}
651
652template <class T>
[277]653FIO_NDataBlock<T>::FIO_NDataBlock(NDataBlock<T> * obj)
[269]654{
[277]655dobj = obj;
656ownobj=false;
[269]657}
658
659template <class T>
660FIO_NDataBlock<T>::~FIO_NDataBlock()
661{
[277]662if (ownobj && dobj) delete dobj;
[269]663}
664
665template <class T>
666AnyDataObj* FIO_NDataBlock<T>::DataObj()
667{
668return(dobj);
669}
670
[754]671template <class T>
672void FIO_NDataBlock<T>::SetDataObj(AnyDataObj & o)
673{
674NDataBlock<T> * po = dynamic_cast< NDataBlock<T> * >(&o);
675if (po == NULL) return;
676if (ownobj && dobj) delete dobj;
677dobj = po; ownobj = false;
678}
[269]679
680template <class T>
681void FIO_NDataBlock<T>::ReadSelf(PInPersist& is)
682{
683// On lit les 3 premiers uint_8
684uint_8 itab[3];
685is.Get(itab, 3);
686if (dobj == NULL) dobj = new NDataBlock<T>(itab[1]);
[290]687else if (itab[1] != dobj->Size()) dobj->ReSize(itab[1]);
[269]688// On lit le tableau de nombres
689PIOSReadArray(is, dobj->Data(), dobj->Size());
690}
691
692
693template <class T>
694void FIO_NDataBlock<T>::WriteSelf(POutPersist& os) const
695{
696if (dobj == NULL) return; // Attention - $CHECK$ Reza 26/04/99
[305]697// On ecrit 3 uint_8
[269]698// 0 : Numero de version, 1 : Taille, 2 reserve a l
699uint_8 itab[3];
700itab[0] = 1;
701itab[1] = dobj->Size();
702itab[2] = 0;
703os.Put(itab, 3);
704// On ecrit le tableau de nombres
705PIOSWriteArray(os, dobj->Data(), dobj->Size());
706}
707
[275]708///////////////////////////////////////////////////////////////
[245]709#ifdef __CXX_PRAGMA_TEMPLATES__
710#pragma define_template NDataBlock<uint_1>
711#pragma define_template NDataBlock<uint_2>
712#pragma define_template NDataBlock<int_2>
713#pragma define_template NDataBlock<int_4>
714#pragma define_template NDataBlock<int_8>
715#pragma define_template NDataBlock<uint_4>
716#pragma define_template NDataBlock<uint_8>
717#pragma define_template NDataBlock<r_4>
718#pragma define_template NDataBlock<r_8>
[268]719#pragma define_template NDataBlock< complex<float> >
720#pragma define_template NDataBlock< complex<double> >
[269]721// Instances des delegues FileIO (PPersist)
[273]722#pragma define_template FIO_NDataBlock<uint_1>
723#pragma define_template FIO_NDataBlock<uint_2>
724#pragma define_template FIO_NDataBlock<int_2>
725#pragma define_template FIO_NDataBlock<int_4>
726#pragma define_template FIO_NDataBlock<int_8>
727#pragma define_template FIO_NDataBlock<uint_4>
728#pragma define_template FIO_NDataBlock<uint_8>
729#pragma define_template FIO_NDataBlock<r_8>
730#pragma define_template FIO_NDataBlock<r_4>
731#pragma define_template FIO_NDataBlock< complex<float> >
732#pragma define_template FIO_NDataBlock< complex<double> >
[245]733#endif
734
[269]735#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
[245]736template class NDataBlock<uint_1>;
737template class NDataBlock<uint_2>;
738template class NDataBlock<int_2>;
739template class NDataBlock<int_4>;
740template class NDataBlock<int_8>;
741template class NDataBlock<uint_4>;
742template class NDataBlock<uint_8>;
743template class NDataBlock<r_4>;
744template class NDataBlock<r_8>;
[249]745template class NDataBlock< complex<float> >;
746template class NDataBlock< complex<double> >;
[269]747// Instances des delegues FileIO (PPersist)
748template class FIO_NDataBlock<uint_1>;
749template class FIO_NDataBlock<uint_2>;
750template class FIO_NDataBlock<int_2>;
751template class FIO_NDataBlock<int_4>;
752template class FIO_NDataBlock<int_8>;
753template class FIO_NDataBlock<uint_4>;
754template class FIO_NDataBlock<uint_8>;
755template class FIO_NDataBlock<r_8>;
756template class FIO_NDataBlock<r_4>;
757template class FIO_NDataBlock< complex<float> >;
758template class FIO_NDataBlock< complex<double> >;
[245]759#endif
Note: See TracBrowser for help on using the repository browser.