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

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

modif ReSize() cmv 27/4/99

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