[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"
|
---|
| 11 |
|
---|
[275] | 12 | // define DEBUG_NDATABLOCK
|
---|
[268] | 13 |
|
---|
[245] | 14 | #ifdef DEBUG_NDATABLOCK
|
---|
| 15 | static size_t NallocData = 0;
|
---|
| 16 | static size_t NallocSRef = 0;
|
---|
| 17 | #endif
|
---|
| 18 |
|
---|
[275] | 19 | ////////////////////////////////////////////////////////////////
|
---|
[289] | 20 | //************ Createur, Destructeur
|
---|
[245] | 21 |
|
---|
| 22 | template <class T>
|
---|
| 23 | NDataBlock<T>::NDataBlock(size_t n)
|
---|
| 24 | // Createur d'une structure de "n" donnees
|
---|
| 25 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
| 26 | {
|
---|
[268] | 27 | #ifdef DEBUG_NDATABLOCK
|
---|
[289] | 28 | cout<<"?_NDataBlock::NDataBlock("<<this<<",n="<<n<<")"<<endl;
|
---|
[268] | 29 | #endif
|
---|
| 30 |
|
---|
[773] | 31 | Alloc(n, NULL, NULL, true); // allocation et mise a zero
|
---|
[245] | 32 | }
|
---|
| 33 |
|
---|
| 34 | template <class T>
|
---|
| 35 | NDataBlock<T>::NDataBlock(size_t n, T* data, Bridge* br)
|
---|
[502] | 36 | // Createur d'une structure de "n" donnees, avec donnees preallouees.
|
---|
| 37 | // Attention createur TRES DANGEREUX (Voir explications dans Alloc()).
|
---|
[245] | 38 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
| 39 | {
|
---|
[268] | 40 | #ifdef DEBUG_NDATABLOCK
|
---|
[289] | 41 | cout<<"?_NDataBlock::NDataBlock("<<this
|
---|
[268] | 42 | <<",data="<<data<<",br="<<br<<")"<<endl;
|
---|
| 43 | #endif
|
---|
| 44 |
|
---|
[245] | 45 | Alloc(n,data,br);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | template <class T>
|
---|
| 49 | NDataBlock<T>::NDataBlock()
|
---|
| 50 | // Createur par default
|
---|
| 51 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
| 52 | {
|
---|
[268] | 53 | #ifdef DEBUG_NDATABLOCK
|
---|
[289] | 54 | cout<<"?_NDataBlock::NDataBlock("<<this<<") default"<<endl;
|
---|
[268] | 55 | #endif
|
---|
[245] | 56 | }
|
---|
| 57 |
|
---|
| 58 | template <class T>
|
---|
[268] | 59 | NDataBlock<T>::NDataBlock(const NDataBlock<T>& a)
|
---|
[289] | 60 | // Createur par copie: partage les donnees si "a" temporaire, clone sinon.
|
---|
[245] | 61 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
| 62 | {
|
---|
[268] | 63 | #ifdef DEBUG_NDATABLOCK
|
---|
[289] | 64 | cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<")"<<endl;
|
---|
[268] | 65 | #endif
|
---|
| 66 |
|
---|
[289] | 67 | CloneOrShare(a);
|
---|
[245] | 68 | }
|
---|
| 69 |
|
---|
| 70 | template <class T>
|
---|
[268] | 71 | NDataBlock<T>::NDataBlock(const NDataBlock<T>& a,bool share)
|
---|
[289] | 72 | // Createur avec choix de partager ou non selon "share"
|
---|
[245] | 73 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
| 74 | {
|
---|
[268] | 75 | #ifdef DEBUG_NDATABLOCK
|
---|
[289] | 76 | cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<",sh=<<"<<share<<")"<<endl;
|
---|
[268] | 77 | #endif
|
---|
| 78 |
|
---|
[245] | 79 | if(share) Share(a); else Clone(a);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | template <class T>
|
---|
| 83 | NDataBlock<T>::~NDataBlock()
|
---|
| 84 | // Destructeur
|
---|
| 85 | {
|
---|
[268] | 86 | #ifdef DEBUG_NDATABLOCK
|
---|
[289] | 87 | cout<<"?_NDataBlock::~NDataBlock("<<this<<")"<<endl;
|
---|
[268] | 88 | #endif
|
---|
| 89 |
|
---|
[245] | 90 | Delete();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[275] | 93 | ////////////////////////////////////////////////////////////////
|
---|
[289] | 94 | //************ Gestion de donnees
|
---|
[275] | 95 |
|
---|
[245] | 96 | template <class T>
|
---|
[773] | 97 | void NDataBlock<T>::Alloc(size_t n,T* data,Bridge* br,bool zero)
|
---|
[285] | 98 | // Allocation d'un NOUVEL espace de stoquage de "n" donnees
|
---|
[773] | 99 | // Si data==NULL : allocation de l'espace memoire
|
---|
| 100 | // si zero == true , l'espace est remplis 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] | 162 | cout<<"?_NDataBlock::Alloc("<<this<<","
|
---|
[268] | 163 | <<n<<","<<data<<","<<br<<") mSz="<<mSz
|
---|
| 164 | <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
|
---|
| 165 | #endif
|
---|
| 166 |
|
---|
[275] | 167 | if(br && !data)
|
---|
| 168 | throw(NullPtrError("NDataBlock::Alloc br!=NULL && data==NULL\n"));
|
---|
[245] | 169 | if(n==0) throw(SzMismatchError("NDataBlock::Alloc n==0\n"));
|
---|
| 170 | if(mSRef) Delete();
|
---|
| 171 | mSz = n;
|
---|
| 172 | mSRef = new NDREF;
|
---|
| 173 | mSRef->nref = 1;
|
---|
[285] | 174 | if(data) mSRef->data = data;
|
---|
[773] | 175 | else {mSRef->data = new T[n]; if (zero) memset(mSRef->data,0,n*sizeof(T));}
|
---|
[245] | 176 | mSRef->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!).
|
---|
| 183 | if(!br) NallocData++; NallocSRef++;
|
---|
[289] | 184 | cout<<"...?_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 |
|
---|
| 191 | template <class T>
|
---|
[268] | 192 | void 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] | 196 | cout<<"?_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] | 201 | if(&a==NULL) throw(NullPtrError("NDataBlock::Clone &a==NULL\n"));
|
---|
| 202 | if(!a.mSRef || a.mSz==0) throw(SzMismatchError("NDataBlock::Clone a.mSz==0\n"));
|
---|
[773] | 203 | Alloc(a.mSz, NULL, NULL, false); // pas de mise a zero
|
---|
[289] | 204 | memcpy(Data(),a.Data(),mSz*sizeof(T));
|
---|
[245] | 205 | }
|
---|
| 206 |
|
---|
| 207 | template <class T>
|
---|
[289] | 208 | void NDataBlock<T>::CloneOrShare(const NDataBlock<T>& a)
|
---|
| 209 | // CloneOrShare: Share si "a" temporaire, Clone sinon.
|
---|
| 210 | {
|
---|
| 211 | #ifdef DEBUG_NDATABLOCK
|
---|
| 212 | cout<<"?_NDataBlock::CloneOrShare("<<this<<","<<&a<<")"<<endl;
|
---|
| 213 | #endif
|
---|
| 214 |
|
---|
| 215 | if(&a==NULL) throw(NullPtrError("NDataBlock::CloneOrShare &a==NULL\n"));
|
---|
| 216 | if(a.IsTemp()) Share(a); else Clone(a);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | template <class T>
|
---|
[268] | 220 | void NDataBlock<T>::Share(const NDataBlock<T>& a)
|
---|
[289] | 221 | // Share: Partage les donnees avec "a"
|
---|
[245] | 222 | {
|
---|
[268] | 223 | #ifdef DEBUG_NDATABLOCK
|
---|
[289] | 224 | cout<<"?_NDataBlock::Share("<<this<<","<<&a<<")";
|
---|
[268] | 225 | if(&a!=NULL) cout<<" a.(mSz="<<a.mSz<<" mSRef="<<a.mSRef
|
---|
| 226 | <<" IsTemp="<<a.IsTemp()<<")";
|
---|
| 227 | cout<<", mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
|
---|
| 228 | #endif
|
---|
| 229 |
|
---|
[245] | 230 | if(&a==NULL) throw(NullPtrError("NDataBlock::Share &a==NULL\n"));
|
---|
[289] | 231 | if(!a.mSRef || a.mSz==0) throw(NullPtrError("NDataBlock::Share a.mSz=0\n"));
|
---|
[245] | 232 | if(mSRef) Delete();
|
---|
| 233 | mSz = a.mSz; mSRef = a.mSRef; mSRef->nref++;
|
---|
[268] | 234 |
|
---|
| 235 | #ifdef DEBUG_NDATABLOCK
|
---|
[289] | 236 | cout<<"...?_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 |
|
---|
| 242 | template <class T>
|
---|
| 243 | void NDataBlock<T>::Delete(void)
|
---|
| 244 | // Pour detruire les pointeurs en tenant compte des references
|
---|
| 245 | {
|
---|
[268] | 246 | #ifdef DEBUG_NDATABLOCK
|
---|
[289] | 247 | cout<<"?_NDataBlock::Delete("<<this<<") mSz="<<mSz
|
---|
[268] | 248 | <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp;
|
---|
| 249 | if(mSRef)
|
---|
| 250 | cout<<" mSRef->nref="<<mSRef->nref<<" mSRef->data="
|
---|
| 251 | <<mSRef->data<<" mSRef->bridge="<<mSRef->bridge;
|
---|
| 252 | cout<<endl;
|
---|
| 253 | #endif
|
---|
| 254 |
|
---|
[289] | 255 | if(mSRef==NULL) return;
|
---|
[245] | 256 | mSRef->nref--;
|
---|
| 257 | if(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
|
---|
| 269 | if(!mSRef->bridge) NallocData--; NallocSRef--;
|
---|
[289] | 270 | cout<<"...?_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
|
---|
| 276 | if(mSRef->bridge) delete mSRef->bridge; else delete [] mSRef->data;
|
---|
[245] | 277 | mSRef->bridge=NULL; mSRef->data=NULL;
|
---|
[285] | 278 | delete mSRef; mSRef=NULL; mSz = 0;
|
---|
[245] | 279 | }
|
---|
| 280 |
|
---|
| 281 | template <class T>
|
---|
[257] | 282 | void 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 | {
|
---|
| 288 | if(data==NULL) throw(NullPtrError("NDataBlock::FillFrom data==NULL\n"));
|
---|
| 289 | if(n==0) throw(ParmError("NDataBlock::FillFrom n<=0\n"));
|
---|
[773] | 290 | if(mSRef==NULL) Alloc(n, NULL, NULL, false); // Pas de mise a zero
|
---|
[285] | 291 | if(mSz<n) n = mSz;
|
---|
[257] | 292 | memcpy(Data(),data,n*sizeof(T));
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[502] | 295 | template <class T>
|
---|
| 296 | void 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 | {
|
---|
| 311 | if(nnew==0) throw(ParmError("NDataBlock::Realloc n<=0\n"));
|
---|
| 312 |
|
---|
| 313 | // Cas sans re-allocation memoire
|
---|
| 314 | if(mSRef && nnew<=mSz && ! force) { mSz=nnew; return;}
|
---|
| 315 |
|
---|
| 316 | // Cas avec re-allocation memoire
|
---|
| 317 | size_t ncop;
|
---|
| 318 | if(!mSRef || mSz==0) ncop=0; else if(mSz<nnew) ncop=mSz; else ncop=nnew;
|
---|
| 319 | T* dataloc = new T[nnew];
|
---|
| 320 | if(ncop>0) memcpy(dataloc,mSRef->data,ncop*sizeof(T));
|
---|
| 321 | if(nnew>ncop) memset(dataloc+ncop,0,(nnew-ncop)*sizeof(T));
|
---|
| 322 | Alloc(nnew,dataloc,NULL); //Alloc gere partage de reference et bridge
|
---|
| 323 | }
|
---|
| 324 |
|
---|
[275] | 325 | ////////////////////////////////////////////////////////////////
|
---|
[245] | 326 | //**** Impression
|
---|
| 327 |
|
---|
| 328 | template <class T>
|
---|
[268] | 329 | void NDataBlock<T>::Print(ostream& os,size_t i1,size_t n) const
|
---|
[245] | 330 | // Impression de n elements a partir de i1
|
---|
| 331 | {
|
---|
| 332 | size_t nr = 0;
|
---|
[267] | 333 | T* p = NULL; Bridge* br = NULL;
|
---|
| 334 | if(mSRef) {nr = mSRef->nref; p = mSRef->data; br = mSRef->bridge;}
|
---|
[268] | 335 | os<<"NDataBlock::Print("<<this<<",Sz="<<mSz<<",IsTemp="<<mIsTemp<<")\n"
|
---|
| 336 | <<" mSRef="<<mSRef<<"(nref="<<nr<<",data="<<p
|
---|
| 337 | <<",bridge="<<br<<")"<<endl;
|
---|
[249] | 338 | if(i1>=mSz || n<=0 || !p) return;
|
---|
[245] | 339 | size_t i2 = i1+n; if(i2>mSz) i2=mSz;
|
---|
[269] | 340 | size_t im = 1; bool enl=false;
|
---|
[245] | 341 | while(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] | 346 | if(!enl) os<<endl;
|
---|
[245] | 347 | }
|
---|
| 348 |
|
---|
[275] | 349 | ////////////////////////////////////////////////////////////////
|
---|
[289] | 350 |
|
---|
| 351 | template <class T>
|
---|
| 352 | T NDataBlock<T>::Sum(size_t i1,size_t n) const
|
---|
| 353 | // Somme des elements de i1 a i1+n-1
|
---|
| 354 | {
|
---|
| 355 | if(i1>=mSz) return 0;
|
---|
| 356 | if(n>mSz) n = mSz; if(n==0) n = mSz-i1;
|
---|
| 357 | T const *p=Begin()+i1, *pe=p+n;
|
---|
| 358 | T val = 0;
|
---|
| 359 | while (p<pe) val += *p++;
|
---|
| 360 | return val;
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | template <class T>
|
---|
| 364 | T NDataBlock<T>::Product(size_t i1,size_t n) const
|
---|
| 365 | // Produit des elements de i1 a i1+n-1
|
---|
| 366 | {
|
---|
| 367 | if(i1>=mSz) return 0;
|
---|
| 368 | if(n>mSz) n = mSz; if(n==0) n = mSz-i1;
|
---|
| 369 | T const *p=Begin()+i1, *pe=p+n;
|
---|
| 370 | T val = 0;
|
---|
| 371 | while (p<pe) val *= *p++;
|
---|
| 372 | return val;
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | ////////////////////////////////////////////////////////////////
|
---|
[245] | 376 | //**** Surcharge de = : NDataBlock=NDataBlock; NDataBlock=<T> b;
|
---|
| 377 |
|
---|
| 378 | template <class T>
|
---|
[268] | 379 | NDataBlock<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] | 383 | cout<<"?_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] | 388 | if(this == &a) return *this;
|
---|
[289] | 389 | if(a.mSz==0)
|
---|
| 390 | throw(SzMismatchError("NDataBlock::operator=A null size\n"));
|
---|
| 391 | CloneOrShare(a);
|
---|
[245] | 392 | return *this;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | template <class T>
|
---|
[249] | 396 | NDataBlock<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] | 400 | cout<<"?_NDataBlock::operator=("<<this<<","<<v<<")"
|
---|
[268] | 401 | <<" mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
|
---|
| 402 | #endif
|
---|
| 403 |
|
---|
[245] | 404 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator=v null size\n"));
|
---|
[249] | 405 | T *p=Begin(), *pe=End();
|
---|
[245] | 406 | while (p<pe) *p++ = v;
|
---|
| 407 | return *this;
|
---|
| 408 | }
|
---|
| 409 |
|
---|
[275] | 410 | ////////////////////////////////////////////////////////////////
|
---|
[245] | 411 | //**** Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += <T> b;
|
---|
| 412 |
|
---|
| 413 | template <class T>
|
---|
| 414 | NDataBlock<T>& NDataBlock<T>::operator += (T b)
|
---|
| 415 | {
|
---|
| 416 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator+=v null size\n"));
|
---|
[249] | 417 | T *p=Begin(), *pe=End();
|
---|
[245] | 418 | while (p<pe) *p++ += b;
|
---|
| 419 | return *this;
|
---|
| 420 | }
|
---|
| 421 |
|
---|
| 422 | template <class T>
|
---|
| 423 | NDataBlock<T>& NDataBlock<T>::operator -= (T b)
|
---|
| 424 | {
|
---|
| 425 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator-=v null size\n"));
|
---|
[249] | 426 | T *p=Begin(), *pe=End();
|
---|
[245] | 427 | while (p<pe) *p++ -= b;
|
---|
| 428 | return *this;
|
---|
| 429 | }
|
---|
| 430 |
|
---|
| 431 | template <class T>
|
---|
| 432 | NDataBlock<T>& NDataBlock<T>::operator *= (T b)
|
---|
| 433 | {
|
---|
| 434 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator*=v null size\n"));
|
---|
[249] | 435 | T *p=Begin(), *pe=End();
|
---|
[245] | 436 | while (p<pe) *p++ *= b;
|
---|
| 437 | return *this;
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 | template <class T>
|
---|
| 441 | NDataBlock<T>& NDataBlock<T>::operator /= (T b)
|
---|
| 442 | {
|
---|
[249] | 443 | if(b==(T) 0) throw(ParmError("NDataBlock::operator/=v divide by zero\n"));
|
---|
[245] | 444 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator/=v null size\n"));
|
---|
[249] | 445 | T *p=Begin(), *pe=End();
|
---|
[245] | 446 | while (p<pe) *p++ /= b;
|
---|
| 447 | return *this;
|
---|
| 448 | }
|
---|
| 449 |
|
---|
[275] | 450 | ////////////////////////////////////////////////////////////////
|
---|
[245] | 451 | //**** Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += NDataBlock;
|
---|
| 452 |
|
---|
| 453 | template <class T>
|
---|
[268] | 454 | NDataBlock<T>& NDataBlock<T>::operator += (const NDataBlock<T>& a)
|
---|
[245] | 455 | {
|
---|
[265] | 456 | if(mSz==0 || mSz!=a.mSz)
|
---|
| 457 | throw(SzMismatchError("NDataBlock::operator+=A size mismatch/null"));
|
---|
[268] | 458 | T *p=Begin(), *pe=End();
|
---|
| 459 | T const * pa=a.Begin();
|
---|
[289] | 460 | while (p<pe) *p++ += *pa++;
|
---|
[245] | 461 | return *this;
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 464 | template <class T>
|
---|
[268] | 465 | NDataBlock<T>& NDataBlock<T>::operator -= (const NDataBlock<T>& a)
|
---|
[245] | 466 | {
|
---|
[265] | 467 | if(mSz==0 || mSz!=a.mSz)
|
---|
| 468 | throw(SzMismatchError("NDataBlock::operator-=A size mismatch/null"));
|
---|
[268] | 469 | T *p=Begin(), *pe=End();
|
---|
| 470 | T const *pa=a.Begin();
|
---|
[289] | 471 | while (p<pe) *p++ -= *pa++;
|
---|
[245] | 472 | return *this;
|
---|
| 473 | }
|
---|
| 474 |
|
---|
| 475 | template <class T>
|
---|
[268] | 476 | NDataBlock<T>& NDataBlock<T>::operator *= (const NDataBlock<T>& a)
|
---|
[245] | 477 | {
|
---|
[265] | 478 | if(mSz==0 || mSz!=a.mSz)
|
---|
| 479 | throw(SzMismatchError("NDataBlock::operator*=A size mismatch/null"));
|
---|
[268] | 480 | T *p=Begin(), *pe=End();
|
---|
| 481 | T const *pa=a.Begin();
|
---|
[289] | 482 | while (p<pe) *p++ *= *pa++;
|
---|
[245] | 483 | return *this;
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 | template <class T>
|
---|
[268] | 487 | NDataBlock<T>& NDataBlock<T>::operator /= (const NDataBlock<T>& a)
|
---|
[289] | 488 | // Attention, aucune protection si un element de "a" est nul.
|
---|
[245] | 489 | {
|
---|
[265] | 490 | if(mSz==0 || mSz!=a.mSz)
|
---|
| 491 | throw(SzMismatchError("NDataBlock::operator/=A size mismatch/null"));
|
---|
[268] | 492 | T *p=Begin(), *pe=End();
|
---|
| 493 | T const *pa=a.Begin();
|
---|
[245] | 494 | while (p<pe) *p++ /= *pa++;
|
---|
| 495 | return *this;
|
---|
| 496 | }
|
---|
| 497 |
|
---|
[275] | 498 | ////////////////////////////////////////////////////////////////
|
---|
| 499 | //**** Surcharge de +,-,*,/ : NDataBlock = NDataBlock+<T>b;
|
---|
| 500 | // NDataBlock = <T>b+NDataBlock;
|
---|
[245] | 501 |
|
---|
| 502 | template <class T>
|
---|
[268] | 503 | NDataBlock<T> NDataBlock<T>::Add(T b) const
|
---|
[257] | 504 | // Pour A+b
|
---|
[245] | 505 | {
|
---|
[289] | 506 | NDataBlock<T> result(*this); result.SetTemp(true);
|
---|
[268] | 507 | result += b;
|
---|
| 508 | return result;
|
---|
[245] | 509 | }
|
---|
| 510 |
|
---|
| 511 | template <class T>
|
---|
[268] | 512 | NDataBlock<T> NDataBlock<T>::Sub(T b) const
|
---|
[257] | 513 | // Pour A-b
|
---|
[245] | 514 | {
|
---|
[289] | 515 | NDataBlock<T> result(*this); result.SetTemp(true);
|
---|
[259] | 516 | return result -= b;
|
---|
[245] | 517 | }
|
---|
| 518 |
|
---|
| 519 | template <class T>
|
---|
[268] | 520 | NDataBlock<T> NDataBlock<T>::SubInv(T b) const
|
---|
[257] | 521 | // Pour b-A
|
---|
[245] | 522 | {
|
---|
[289] | 523 | NDataBlock<T> result(*this); result.SetTemp(true);
|
---|
[268] | 524 | T *p=result.Begin(), *pe=result.End();
|
---|
| 525 | T const *pa=this->Begin();
|
---|
[245] | 526 | while(p<pe) {*p++ = b - *pa++;}
|
---|
| 527 | return result;
|
---|
| 528 | }
|
---|
| 529 |
|
---|
| 530 | template <class T>
|
---|
[268] | 531 | NDataBlock<T> NDataBlock<T>::Mul(T b) const
|
---|
[257] | 532 | // Pour A*b
|
---|
[245] | 533 | {
|
---|
[289] | 534 | NDataBlock<T> result(*this); result.SetTemp(true);
|
---|
[259] | 535 | return result *= b;
|
---|
[245] | 536 | }
|
---|
| 537 |
|
---|
| 538 | template <class T>
|
---|
[268] | 539 | NDataBlock<T> NDataBlock<T>::Div(T b) const
|
---|
[257] | 540 | // Pour A/b
|
---|
[245] | 541 | {
|
---|
[289] | 542 | NDataBlock<T> result(*this); result.SetTemp(true);
|
---|
[259] | 543 | return result /= b;
|
---|
[245] | 544 | }
|
---|
| 545 |
|
---|
| 546 | template <class T>
|
---|
[268] | 547 | NDataBlock<T> NDataBlock<T>::DivInv(T b) const
|
---|
[257] | 548 | // Pour b/A
|
---|
[245] | 549 | {
|
---|
[289] | 550 | NDataBlock<T> result(*this); result.SetTemp(true);
|
---|
[268] | 551 | T *p=result.Begin(), *pe=result.End();
|
---|
| 552 | T const *pa = this->Begin();
|
---|
[245] | 553 | while(p<pe) {*p++ = b / *pa++;}
|
---|
| 554 | return result;
|
---|
| 555 | }
|
---|
| 556 |
|
---|
[275] | 557 | ////////////////////////////////////////////////////////////////
|
---|
[245] | 558 | //**** Surcharge de +,-,*,/ : NDataBlock = NDataBlock+NDataBlock;
|
---|
| 559 |
|
---|
| 560 | template <class T>
|
---|
[268] | 561 | NDataBlock<T> NDataBlock<T>::Add(const NDataBlock<T>& b) const
|
---|
[257] | 562 | // Pour A+B
|
---|
[245] | 563 | {
|
---|
[285] | 564 | if(mSz!=b.mSz)
|
---|
[265] | 565 | throw(SzMismatchError("NDataBlock operator C=A+B size mismatch/null\n"));
|
---|
[268] | 566 | NDataBlock<T> result; result.SetTemp(true);
|
---|
[289] | 567 | if(b.IsTemp()) {result.Share(b); result += *this;}
|
---|
| 568 | else {result.CloneOrShare(*this); result += b;}
|
---|
[268] | 569 | return result;
|
---|
[245] | 570 | }
|
---|
| 571 |
|
---|
| 572 | template <class T>
|
---|
[268] | 573 | NDataBlock<T> NDataBlock<T>::Mul(const NDataBlock<T>& b) const
|
---|
[257] | 574 | // Pour A*B
|
---|
[245] | 575 | {
|
---|
[285] | 576 | if(mSz!=b.mSz)
|
---|
[265] | 577 | throw(SzMismatchError("NDataBlock operator C=A*B size mismatch/null\n"));
|
---|
[268] | 578 | NDataBlock<T> result; result.SetTemp(true);
|
---|
[289] | 579 | if(b.IsTemp()) {result.Share(b); result *= *this;}
|
---|
| 580 | else {result.CloneOrShare(*this); result *= b;}
|
---|
[268] | 581 | return result;
|
---|
[245] | 582 | }
|
---|
| 583 |
|
---|
| 584 | template <class T>
|
---|
[268] | 585 | NDataBlock<T> NDataBlock<T>::Sub(const NDataBlock<T>& b) const
|
---|
[257] | 586 | // Pour A-B
|
---|
[245] | 587 | {
|
---|
[285] | 588 | if(mSz!=b.mSz)
|
---|
[265] | 589 | throw(SzMismatchError("NDataBlock operator C=A-B size mismatch/null\n"));
|
---|
[268] | 590 | NDataBlock<T> result; result.SetTemp(true);
|
---|
[245] | 591 | if(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] | 596 | return result;
|
---|
[245] | 597 | }
|
---|
| 598 |
|
---|
| 599 | template <class T>
|
---|
[268] | 600 | NDataBlock<T> NDataBlock<T>::Div(const NDataBlock<T>& b) const
|
---|
[257] | 601 | // Pour A/B
|
---|
[245] | 602 | {
|
---|
[285] | 603 | if(mSz!=b.mSz)
|
---|
[265] | 604 | throw(SzMismatchError("NDataBlock operator C=A/B size mismatch/null\n"));
|
---|
[268] | 605 | NDataBlock<T> result; result.SetTemp(true);
|
---|
[245] | 606 | if(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] | 611 | return result;
|
---|
[245] | 612 | }
|
---|
| 613 |
|
---|
[269] | 614 |
|
---|
[275] | 615 | ///////////////////////////////////////////////////////////////
|
---|
[245] | 616 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 617 | #pragma define_template NDataBlock<uint_1>
|
---|
| 618 | #pragma define_template NDataBlock<uint_2>
|
---|
| 619 | #pragma define_template NDataBlock<int_2>
|
---|
| 620 | #pragma define_template NDataBlock<int_4>
|
---|
| 621 | #pragma define_template NDataBlock<int_8>
|
---|
| 622 | #pragma define_template NDataBlock<uint_4>
|
---|
| 623 | #pragma define_template NDataBlock<uint_8>
|
---|
| 624 | #pragma define_template NDataBlock<r_4>
|
---|
| 625 | #pragma define_template NDataBlock<r_8>
|
---|
[802] | 626 | #pragma define_template NDataBlock< complex<r_4> >
|
---|
| 627 | #pragma define_template NDataBlock< complex<r_8> >
|
---|
[245] | 628 | #endif
|
---|
| 629 |
|
---|
[269] | 630 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
[245] | 631 | template class NDataBlock<uint_1>;
|
---|
| 632 | template class NDataBlock<uint_2>;
|
---|
| 633 | template class NDataBlock<int_2>;
|
---|
| 634 | template class NDataBlock<int_4>;
|
---|
| 635 | template class NDataBlock<int_8>;
|
---|
| 636 | template class NDataBlock<uint_4>;
|
---|
| 637 | template class NDataBlock<uint_8>;
|
---|
| 638 | template class NDataBlock<r_4>;
|
---|
| 639 | template class NDataBlock<r_8>;
|
---|
[802] | 640 | template class NDataBlock< complex<r_4> >;
|
---|
| 641 | template class NDataBlock< complex<r_8> >;
|
---|
[245] | 642 | #endif
|
---|