[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
|
---|
[2615] | 4 | #include "sopnamsp.h"
|
---|
[245] | 5 | #include "machdefs.h"
|
---|
| 6 | #include <stdio.h>
|
---|
| 7 | #include <stdlib.h>
|
---|
[2322] | 8 | #include <iostream>
|
---|
[245] | 9 | #include <complex>
|
---|
| 10 | #include "pexceptions.h"
|
---|
| 11 | #include "ndatablock.h"
|
---|
| 12 |
|
---|
[944] | 13 | /*!
|
---|
| 14 | \class SOPHYA::NDataBlock
|
---|
[1607] | 15 | \ingroup BaseTools
|
---|
[944] | 16 | Management of data blocks
|
---|
| 17 | */
|
---|
[268] | 18 |
|
---|
[944] | 19 | //////////////////////////////////
|
---|
| 20 | // Fonctionnement en mode debug //
|
---|
| 21 | //////////////////////////////////
|
---|
[245] | 22 |
|
---|
[944] | 23 | template <class T> int NDataBlock<T>::Debug_NDataBlock = 0;
|
---|
| 24 | template <class T> size_t NDataBlock<T>::NallocData = 0;
|
---|
| 25 | template <class T> size_t NDataBlock<T>::NallocSRef = 0;
|
---|
[245] | 26 |
|
---|
[944] | 27 | //! Set debug (and level print) for allocation and references debug.
|
---|
| 28 | /*!
|
---|
| 29 | \param prtlevel : activate/des-activate debug mode
|
---|
| 30 | and select print level
|
---|
| 31 |
|
---|
| 32 | \arg prtlevel <= 0 : no debug
|
---|
| 33 | \arg prtlevel == 1 : debug activated, no print
|
---|
| 34 | \arg prtlevel >=2 : debug activated,
|
---|
| 35 | print infos in all routines that have something to do with
|
---|
| 36 | allocations or des-allocation of datas or references.
|
---|
| 37 | */
|
---|
[245] | 38 | template <class T>
|
---|
[944] | 39 | void NDataBlock<T>::SetPrintDebug(int prtdbglevel)
|
---|
| 40 | {
|
---|
| 41 | Debug_NDataBlock = prtdbglevel;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | //! Reset debug counter values.
|
---|
| 45 | /*!
|
---|
| 46 | \param nallocdata : reset number of allocated data structures to \b nallocdata
|
---|
| 47 | \param nallocsref : reset number of allocated references to \b nallocsref
|
---|
| 48 | \warning In principle this routine should not be use (only experts)
|
---|
| 49 | */
|
---|
| 50 | template <class T>
|
---|
| 51 | void NDataBlock<T>::ResetDebug(size_t nallocdata, size_t nallocsref)
|
---|
| 52 | {
|
---|
| 53 | NallocData = nallocdata;
|
---|
| 54 | NallocSRef = nallocsref;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | //! Print debug current status.
|
---|
| 58 | /*!
|
---|
| 59 | Print debug current status for number of allocated
|
---|
| 60 | data structures and number of allocated references.
|
---|
| 61 | */
|
---|
| 62 | template <class T>
|
---|
| 63 | void NDataBlock<T>::PrintDebug()
|
---|
| 64 | {
|
---|
| 65 | cout<<"... ... ... NallocData = "<<NallocData
|
---|
| 66 | <<" , NallocSRef = "<<NallocSRef
|
---|
| 67 | <<" ... ... ..."<<endl;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | ///////////////////////////
|
---|
| 71 | // Createur, Destructeur //
|
---|
| 72 | ///////////////////////////
|
---|
| 73 |
|
---|
[2565] | 74 | //! Constructor for \b n datas. if \b zero=true, filled with zeros
|
---|
[944] | 75 | template <class T>
|
---|
[2565] | 76 | NDataBlock<T>::NDataBlock(size_t n, bool fzero)
|
---|
[245] | 77 | // Createur d'une structure de "n" donnees
|
---|
| 78 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
| 79 | {
|
---|
[944] | 80 | if(Debug_NDataBlock>1)
|
---|
| 81 | cout<<"?_NDataBlock::NDataBlock("<<this<<",n="<<n<<")"<<endl;
|
---|
[268] | 82 |
|
---|
[2565] | 83 | Alloc(n, NULL, NULL, fzero); // allocation et mise a zero
|
---|
[245] | 84 | }
|
---|
| 85 |
|
---|
[944] | 86 | //! Constructor for \b n datas shared with external
|
---|
| 87 | /*!
|
---|
| 88 | Datas are previously allocated by an other external source.
|
---|
| 89 | \warning This require particular care (see Alloc)
|
---|
| 90 | \sa Alloc
|
---|
| 91 | */
|
---|
[245] | 92 | template <class T>
|
---|
| 93 | NDataBlock<T>::NDataBlock(size_t n, T* data, Bridge* br)
|
---|
[502] | 94 | // Createur d'une structure de "n" donnees, avec donnees preallouees.
|
---|
| 95 | // Attention createur TRES DANGEREUX (Voir explications dans Alloc()).
|
---|
[245] | 96 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
| 97 | {
|
---|
[944] | 98 | if(Debug_NDataBlock>1)
|
---|
| 99 | cout<<"?_NDataBlock::NDataBlock("<<this
|
---|
| 100 | <<",data="<<data<<",br="<<br<<")"<<endl;
|
---|
[268] | 101 |
|
---|
[245] | 102 | Alloc(n,data,br);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[944] | 105 | //! Default constructor
|
---|
[245] | 106 | template <class T>
|
---|
| 107 | NDataBlock<T>::NDataBlock()
|
---|
| 108 | // Createur par default
|
---|
| 109 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
| 110 | {
|
---|
[944] | 111 | if(Debug_NDataBlock>1)
|
---|
| 112 | cout<<"?_NDataBlock::NDataBlock("<<this<<") default"<<endl;
|
---|
[245] | 113 | }
|
---|
| 114 |
|
---|
[944] | 115 | //! Copy constructor
|
---|
| 116 | /*!
|
---|
[976] | 117 | \warning datas are \b SHARED with \b a.
|
---|
[944] | 118 | */
|
---|
[245] | 119 | template <class T>
|
---|
[268] | 120 | NDataBlock<T>::NDataBlock(const NDataBlock<T>& a)
|
---|
[969] | 121 | // Createur par copie: partage les donnees dans tous les cas
|
---|
[245] | 122 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
| 123 | {
|
---|
[944] | 124 | if(Debug_NDataBlock>1)
|
---|
[1349] | 125 | cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<" a.mSz="<<a.mSz<<")"<<endl;
|
---|
[268] | 126 |
|
---|
[1349] | 127 | if(a.mSRef && a.mSz>0) Share(a);
|
---|
[245] | 128 | }
|
---|
| 129 |
|
---|
[944] | 130 | //! Copy constructor with \b share option
|
---|
| 131 | /*!
|
---|
| 132 | \warning datas are shared if \b share is \b true, cloned if not.
|
---|
| 133 | */
|
---|
[245] | 134 | template <class T>
|
---|
[268] | 135 | NDataBlock<T>::NDataBlock(const NDataBlock<T>& a,bool share)
|
---|
[289] | 136 | // Createur avec choix de partager ou non selon "share"
|
---|
[245] | 137 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
| 138 | {
|
---|
[944] | 139 | if(Debug_NDataBlock>1)
|
---|
[946] | 140 | cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a
|
---|
| 141 | <<",sh=<<"<<share<<")"<<endl;
|
---|
[268] | 142 |
|
---|
[1349] | 143 | if(a.mSRef && a.mSz>0) {if(share) Share(a); else Clone(a);}
|
---|
[245] | 144 | }
|
---|
| 145 |
|
---|
[944] | 146 | //! Destructor
|
---|
[245] | 147 | template <class T>
|
---|
| 148 | NDataBlock<T>::~NDataBlock()
|
---|
| 149 | // Destructeur
|
---|
| 150 | {
|
---|
[944] | 151 | if(Debug_NDataBlock>1)
|
---|
| 152 | cout<<"?_NDataBlock::~NDataBlock("<<this<<")"<<endl;
|
---|
[268] | 153 |
|
---|
[245] | 154 | Delete();
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[944] | 157 | ////////////////////////
|
---|
| 158 | // Gestion de donnees //
|
---|
| 159 | ////////////////////////
|
---|
[275] | 160 |
|
---|
[944] | 161 | //! Clone datas from \b a
|
---|
[245] | 162 | template <class T>
|
---|
[268] | 163 | void NDataBlock<T>::Clone(const NDataBlock<T>& a)
|
---|
[289] | 164 | // Clone: copie de donnees a partir de "a"
|
---|
[245] | 165 | {
|
---|
[944] | 166 | if(Debug_NDataBlock>1)
|
---|
| 167 | cout<<"?_NDataBlock::Clone("<<this<<","<<&a<<") a.(mSz="
|
---|
| 168 | <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp()
|
---|
| 169 | <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
|
---|
[275] | 170 |
|
---|
[289] | 171 | if(&a==NULL) throw(NullPtrError("NDataBlock::Clone &a==NULL\n"));
|
---|
[1349] | 172 | if(!a.mSRef || a.mSz==0) throw(NullPtrError("NDataBlock::Clone a.mSz=0\n"));
|
---|
[773] | 173 | Alloc(a.mSz, NULL, NULL, false); // pas de mise a zero
|
---|
[289] | 174 | memcpy(Data(),a.Data(),mSz*sizeof(T));
|
---|
[245] | 175 | }
|
---|
| 176 |
|
---|
[944] | 177 | //! Share datas with \b a
|
---|
[245] | 178 | template <class T>
|
---|
[944] | 179 | void NDataBlock<T>::Share(const NDataBlock<T>& a)
|
---|
| 180 | // Share: Partage les donnees avec "a"
|
---|
| 181 | {
|
---|
| 182 | if(Debug_NDataBlock>1) {
|
---|
| 183 | cout<<"?_NDataBlock::Share("<<this<<","<<&a<<")";
|
---|
| 184 | if(&a!=NULL) cout<<" a.(mSz="<<a.mSz<<" mSRef="<<a.mSRef
|
---|
| 185 | <<" IsTemp="<<a.IsTemp()<<")";
|
---|
| 186 | cout<<", mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | if(&a==NULL) throw(NullPtrError("NDataBlock::Share &a==NULL\n"));
|
---|
| 190 | if(!a.mSRef || a.mSz==0) throw(NullPtrError("NDataBlock::Share a.mSz=0\n"));
|
---|
| 191 | if(mSRef) Delete();
|
---|
| 192 | mSz = a.mSz; mSRef = a.mSRef; mSRef->nref++;
|
---|
| 193 |
|
---|
| 194 | if(Debug_NDataBlock>1)
|
---|
| 195 | cout<<"...?_NDataBlock::Share mSz="<<mSz<<" mSRef="<<mSRef
|
---|
| 196 | <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<< mSRef->data
|
---|
[946] | 197 | <<" mSRef->bridge="<<mSRef->bridge
|
---|
| 198 | <<" IsTemp="<<mIsTemp<<endl;
|
---|
[944] | 199 | }
|
---|
| 200 |
|
---|
| 201 | //! \b Share with \b a if \b temporary, \b clone from \b a if not.
|
---|
[976] | 202 | /*! \warning For most purposes, users don't have to worry with
|
---|
| 203 | the "temporary" nature of a NDataBlock. That is used
|
---|
| 204 | internaly to avoid memory allocation in operation
|
---|
| 205 | like A = B + C + D for instance. The method is not
|
---|
| 206 | protected to allow users to write complicated functions
|
---|
| 207 | on NDataBlock.
|
---|
| 208 | \verbatim
|
---|
| 209 | ----------------------------------------------------------
|
---|
| 210 | Pourquoi une complication avec la notion de "temporaire" :
|
---|
| 211 | ----------------------------------------------------------
|
---|
| 212 | - Le constructeur par copie partageant les donnees,
|
---|
| 213 | dans une methode un { NDataBlock<T> result; ...; return result;}
|
---|
| 214 | ne va pas allouer de la memoire pour retourner "result".
|
---|
| 215 | - La gestion de temporaire sert quand on enchaine plusieurs
|
---|
| 216 | operations sur la meme ligne, par exemple : A = B+C+D;
|
---|
| 217 | Dans ce cas l'objet CD=C+D est d'abord alloue et rempli
|
---|
| 218 | avec C+D, puis CD est mis a "temporaire".
|
---|
| 219 | Quand on ajoute B a CD, la methode d'addition va se rendre compte
|
---|
| 220 | que CD est "temporaire" et additionner B "in-place" dans CD
|
---|
| 221 | sans allouer une fois de plus de la place (pas d'allocation
|
---|
| 222 | de place BCD pour mettre B+CD mais une operation CD += B).
|
---|
| 223 | Si la notion d'objet "temporaire" n'avait pas ete consideree
|
---|
| 224 | l'addition A = B+C+D aurait alloue de la place pour "CD=C+D"
|
---|
| 225 | puis pour BCD=B+CD : 2 allocations auraient ete necessaires
|
---|
| 226 | contre 1 seule dans notre cas de geston de "temporaire".
|
---|
| 227 | \endverbatim
|
---|
| 228 | */
|
---|
[944] | 229 | template <class T>
|
---|
[289] | 230 | void NDataBlock<T>::CloneOrShare(const NDataBlock<T>& a)
|
---|
| 231 | // CloneOrShare: Share si "a" temporaire, Clone sinon.
|
---|
| 232 | {
|
---|
[944] | 233 | if(Debug_NDataBlock>1)
|
---|
| 234 | cout<<"?_NDataBlock::CloneOrShare("<<this<<","<<&a<<")"<<endl;
|
---|
[289] | 235 |
|
---|
| 236 | if(&a==NULL) throw(NullPtrError("NDataBlock::CloneOrShare &a==NULL\n"));
|
---|
| 237 | if(a.IsTemp()) Share(a); else Clone(a);
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[944] | 240 | ////////////////////////////////////////////////////////////
|
---|
| 241 | // Allocation , destruction , remplissage et reallocation //
|
---|
| 242 | ////////////////////////////////////////////////////////////
|
---|
| 243 |
|
---|
| 244 | //! Allocation management
|
---|
| 245 | /*!
|
---|
| 246 | Allocation d'un NOUVEL espace de stoquage de "n" donnees
|
---|
| 247 | \verbatim
|
---|
| 248 | Si data==NULL : allocation de l'espace memoire
|
---|
| 249 | si zero == true , l'espace est remplis de zeros
|
---|
| 250 | data!=NULL : partage des donnees avec l'adresse data
|
---|
| 251 | Si br==NULL : les donnees nous appartiennent
|
---|
| 252 | br!=NULL : les donnees ne nous appartiennent pas (ex: Blitz)
|
---|
| 253 |
|
---|
| 254 | Exemple: on veut connecter a un tableau de T*
|
---|
| 255 | > float *x = new float[5]; ... remplissage de x[] ...;
|
---|
| 256 | 1- On veut que NDataBlock NE DESALLOUE PAS le tableau "x[]"
|
---|
| 257 | a- Premiere solution
|
---|
| 258 | > NDataBlock A(5,x,new Bridge);
|
---|
| 259 | ......
|
---|
| 260 | > delete [] x;
|
---|
| 261 | - Il faut deleter x[] explicitement.
|
---|
| 262 | - Le destructeur de "A" ne detruit pas x[].
|
---|
| 263 | ATTENTION: Une fois x[] detruit, "A" ne peut
|
---|
| 264 | plus acceder les donnees!
|
---|
| 265 | - Bridge est detruit par le destructeur de "A"
|
---|
| 266 | b- Autre solution:
|
---|
| 267 | > NDataBlock A(5); A.FillFrom(5,x);
|
---|
| 268 | > delete [] x;
|
---|
| 269 | ......
|
---|
| 270 | - Il faut deleter x[] explicitement.
|
---|
| 271 | - "A" possede une copie en local de x[].
|
---|
| 272 | - Le destructeur de "A" ne detruit pas x[] mais la copie locale.
|
---|
| 273 | 2- On veut que NDataBlock desalloue le tableau
|
---|
| 274 | > NDataBlock A(5,x);
|
---|
| 275 | - Ne Pas Faire "delete [] x;"
|
---|
| 276 | - "A" partage les donnees avec x[].
|
---|
| 277 | - Le destructeur de "A" detruit x[].
|
---|
| 278 |
|
---|
| 279 | --- REMARQUE SUR LE DANGER DE CERTAINES SITUATIONS (CMV):
|
---|
| 280 | 1-/ x = new float[n1]; NDataBlock A(n2,x);
|
---|
| 281 | 1er danger: si n2>n1 depassement de tableaux (core dump)
|
---|
| 282 | 2sd danger: celui qui alloue x[] ne doit pas faire le "delete"
|
---|
| 283 | en desaccord avec toutes les regles de bonne conduite.
|
---|
| 284 | 2-/ float x[5]={1,2,3,4,5}; {NDataBlock A(n2,&x[0]);} cout<<x[2];
|
---|
| 285 | Ici, a la sortie du bloc {}, le destructeur de "A" va detruire
|
---|
| 286 | l'adresse de &x[0]: je n'ose imaginer que ca se fasse sans probleme
|
---|
| 287 | et de toute facon, cout<<x[2]; va surement faire des etincelles.
|
---|
| 288 | 3-/ x = new float[n1]; NDataBlock A(n2,x,new Bridge);
|
---|
| 289 | 1er danger: si n2>n1 depassement de tableaux (core dump)
|
---|
| 290 | 2sd danger: si la methode bridgee (blitz?) detruit x[]
|
---|
| 291 | "A" n'a plus de donnees connectees!
|
---|
| 292 | --- CONCLUSION
|
---|
| 293 | Cette classe est franchement merdique.
|
---|
| 294 | - On peut accepter la prise de risque liee a NDataBlock(n2,x,new Bridge);
|
---|
| 295 | car je ne vois pas comment on pourrait faire autrement pour connecter
|
---|
| 296 | un tableau de type blitz par exemple.
|
---|
| 297 | - Par contre le createur NDataBlock(n2,x); doit etre interdit
|
---|
| 298 | dans sa forme actelle car trop dangereux et il me semble inutile.
|
---|
| 299 | - Dans cette nouvelle optique:
|
---|
| 300 | NDataBlock(n2,x,new Bridge) et NDataBlock(n2,x) disparaissent
|
---|
| 301 | On remplace par NDataBlock(n2,x) {Alloc(n2,x,new Bridge);}
|
---|
| 302 | qui force le Bridge dans tout les cas puisque NDataBlock
|
---|
| 303 | ne possede pas les donnees.
|
---|
| 304 | Mais puis-je encore le faire vu que NDataBlock est a la base
|
---|
| 305 | de TVector,TMatrix et qu'il faut donc reprendre tout le code DPC
|
---|
| 306 | - Quoiqu'il arrive Alloc est une methode privee et peut donc rester
|
---|
| 307 | sous sa forme actuelle.
|
---|
| 308 |
|
---|
| 309 | \endverbatim
|
---|
| 310 | */
|
---|
[1426] | 311 |
|
---|
| 312 | static uint_8 _ndrefid_ = 0; // Identificateur de NDREF cree
|
---|
| 313 |
|
---|
[289] | 314 | template <class T>
|
---|
[944] | 315 | void NDataBlock<T>::Alloc(size_t n,T* data,Bridge* br,bool zero)
|
---|
[245] | 316 | {
|
---|
[944] | 317 | if(Debug_NDataBlock>1)
|
---|
| 318 | cout<<"?_NDataBlock::Alloc("<<this<<","
|
---|
| 319 | <<n<<","<<data<<","<<br<<") mSz="<<mSz
|
---|
| 320 | <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
|
---|
[268] | 321 |
|
---|
[944] | 322 | if(br && !data)
|
---|
| 323 | throw(NullPtrError("NDataBlock::Alloc br!=NULL && data==NULL\n"));
|
---|
| 324 | if(n==0) throw(SzMismatchError("NDataBlock::Alloc n==0\n"));
|
---|
[245] | 325 | if(mSRef) Delete();
|
---|
[944] | 326 | mSz = n;
|
---|
| 327 | mSRef = new NDREF;
|
---|
| 328 | mSRef->nref = 1;
|
---|
[1426] | 329 | mSRef->dsid = ++_ndrefid_;
|
---|
[944] | 330 | if(data) mSRef->data = data;
|
---|
| 331 | else {mSRef->data = new T[n]; if (zero) memset(mSRef->data,0,n*sizeof(T));}
|
---|
| 332 | mSRef->bridge = br;
|
---|
[268] | 333 |
|
---|
[944] | 334 | if(Debug_NDataBlock>0) {
|
---|
| 335 | // Meme dans le cas data!=0 et br==0 (connexion d'un tableau
|
---|
| 336 | // avec destruction geree par ~NDataBlock (cas 2-) on compte
|
---|
| 337 | // comme si on avait fait une allocation du tableau (ce qui a ete
|
---|
| 338 | // fait au niveau du dessus!).
|
---|
| 339 | if(!br) NallocData++; NallocSRef++;
|
---|
| 340 | if(Debug_NDataBlock>1)
|
---|
| 341 | cout<<"...?_NDataBlock::Alloc mSz="<<mSz<<" mSRef="<<mSRef
|
---|
[946] | 342 | <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<<mSRef->data
|
---|
| 343 | <<" mSRef->bridge="<<mSRef->bridge
|
---|
| 344 | <<" IsTemp="<<mIsTemp
|
---|
[944] | 345 | <<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
|
---|
[245] | 346 | }
|
---|
[944] | 347 | }
|
---|
[245] | 348 |
|
---|
[944] | 349 | //! Management of de-allocation
|
---|
[245] | 350 | template <class T>
|
---|
| 351 | void NDataBlock<T>::Delete(void)
|
---|
| 352 | // Pour detruire les pointeurs en tenant compte des references
|
---|
| 353 | {
|
---|
[944] | 354 | if(Debug_NDataBlock>1) {
|
---|
| 355 | cout<<"?_NDataBlock::Delete("<<this<<") mSz="<<mSz
|
---|
| 356 | <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp;
|
---|
| 357 | if(mSRef)
|
---|
| 358 | cout<<" mSRef->nref="<<mSRef->nref<<" mSRef->data="
|
---|
| 359 | <<mSRef->data<<" mSRef->bridge="<<mSRef->bridge;
|
---|
| 360 | cout<<endl;
|
---|
| 361 | }
|
---|
[268] | 362 |
|
---|
[289] | 363 | if(mSRef==NULL) return;
|
---|
[946] | 364 |
|
---|
[245] | 365 | mSRef->nref--;
|
---|
| 366 | if(mSRef->nref != 0) {
|
---|
[268] | 367 |
|
---|
[944] | 368 | if(Debug_NDataBlock>1)
|
---|
[502] | 369 | cout<<"...?_NDataBlock::Delete() pas de desallocation il reste nref="
|
---|
| 370 | <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
|
---|
[268] | 371 |
|
---|
[285] | 372 | mSz = 0; mSRef=NULL;
|
---|
[245] | 373 | return;
|
---|
| 374 | }
|
---|
[268] | 375 |
|
---|
[944] | 376 | if(Debug_NDataBlock>0) {
|
---|
| 377 | if(!mSRef->bridge) NallocData--; NallocSRef--;
|
---|
| 378 | if(Debug_NDataBlock>1)
|
---|
| 379 | cout<<"...?_NDataBlock::Delete() desallocation complete il reste nref="
|
---|
| 380 | <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
|
---|
| 381 | }
|
---|
[268] | 382 |
|
---|
[245] | 383 | // Si il y a un Bridge les donnees ne n'appartiennent pas, on detruit le Bridge
|
---|
[285] | 384 | // sinon, les donnees ont ete allouees par nos soins, on libere l'espace
|
---|
| 385 | if(mSRef->bridge) delete mSRef->bridge; else delete [] mSRef->data;
|
---|
[245] | 386 | mSRef->bridge=NULL; mSRef->data=NULL;
|
---|
[285] | 387 | delete mSRef; mSRef=NULL; mSz = 0;
|
---|
[245] | 388 | }
|
---|
| 389 |
|
---|
[944] | 390 | //! Fill dats of this NDataBlock with the \b n datas pointed by \b data
|
---|
| 391 | /*!
|
---|
| 392 | \warning If class empty : allocate space in memory
|
---|
| 393 | \warning If class already connected : overwrite with minimum size
|
---|
| 394 | (\b n or \b mSz)
|
---|
| 395 | */
|
---|
[245] | 396 | template <class T>
|
---|
[257] | 397 | void NDataBlock<T>::FillFrom(size_t n,T* data)
|
---|
| 398 | // Remplissage par un tableau de donnees
|
---|
| 399 | // - Si classe vide : creation de l'espace memoire
|
---|
[289] | 400 | // - Si classe connectee : on ecrit selon la longueur minimale
|
---|
| 401 | // (cad this->mSz ou "n")
|
---|
[257] | 402 | {
|
---|
| 403 | if(data==NULL) throw(NullPtrError("NDataBlock::FillFrom data==NULL\n"));
|
---|
| 404 | if(n==0) throw(ParmError("NDataBlock::FillFrom n<=0\n"));
|
---|
[773] | 405 | if(mSRef==NULL) Alloc(n, NULL, NULL, false); // Pas de mise a zero
|
---|
[285] | 406 | if(mSz<n) n = mSz;
|
---|
[257] | 407 | memcpy(Data(),data,n*sizeof(T));
|
---|
| 408 | }
|
---|
| 409 |
|
---|
[944] | 410 | //! Re-allocate space for \b nnew datas
|
---|
| 411 | /*!
|
---|
| 412 | \param nnnew : new size
|
---|
| 413 | \param force : to manage the way re-allocation will be done (see after).
|
---|
| 414 | \verbatim
|
---|
| 415 | Re-allocation de "nnew" place memoire pour les donnees
|
---|
| 416 | avec conservation des "nold" donnees precedentes si possible.
|
---|
| 417 | "force" gere la re-allocation de la place memoire pour les donnees.
|
---|
| 418 | Divers cas se presentent:
|
---|
| 419 | a-/ *** nnew>nold force=quelconque ***
|
---|
| 420 | place re-allouee, donnees [0,nold[ copiees, surplus [nold,new[ mis a zero
|
---|
| 421 | b-/ *** nnew<=nold force=true ***
|
---|
| 422 | place re-allouee, donnees [0,nnew[ copiees, pas de surplus
|
---|
| 423 | c-/ *** nnew<=nold force=false ***
|
---|
| 424 | place non re-allouee, seule la valeur de la taille est diminuee
|
---|
| 425 | - On tient compte du partage des donnees dans tous les cas.
|
---|
| 426 | - Si il n'y a pas de donnees connectees a la classe, on re-alloue
|
---|
| 427 | dans tous les cas
|
---|
| 428 | \endverbatim
|
---|
| 429 | */
|
---|
[502] | 430 | template <class T>
|
---|
| 431 | void NDataBlock<T>::Realloc(size_t nnew,bool force)
|
---|
| 432 | {
|
---|
| 433 | if(nnew==0) throw(ParmError("NDataBlock::Realloc n<=0\n"));
|
---|
| 434 |
|
---|
| 435 | // Cas sans re-allocation memoire
|
---|
| 436 | if(mSRef && nnew<=mSz && ! force) { mSz=nnew; return;}
|
---|
| 437 |
|
---|
| 438 | // Cas avec re-allocation memoire
|
---|
| 439 | size_t ncop;
|
---|
| 440 | if(!mSRef || mSz==0) ncop=0; else if(mSz<nnew) ncop=mSz; else ncop=nnew;
|
---|
| 441 | T* dataloc = new T[nnew];
|
---|
| 442 | if(ncop>0) memcpy(dataloc,mSRef->data,ncop*sizeof(T));
|
---|
| 443 | if(nnew>ncop) memset(dataloc+ncop,0,(nnew-ncop)*sizeof(T));
|
---|
| 444 | Alloc(nnew,dataloc,NULL); //Alloc gere partage de reference et bridge
|
---|
| 445 | }
|
---|
| 446 |
|
---|
[944] | 447 | ////////////////
|
---|
| 448 | // Impression //
|
---|
| 449 | ////////////////
|
---|
[245] | 450 |
|
---|
[944] | 451 | //! Give infos and print \b n datas beginning at \b i1 on stream \b os.
|
---|
[245] | 452 | template <class T>
|
---|
[268] | 453 | void NDataBlock<T>::Print(ostream& os,size_t i1,size_t n) const
|
---|
[245] | 454 | // Impression de n elements a partir de i1
|
---|
| 455 | {
|
---|
| 456 | size_t nr = 0;
|
---|
[267] | 457 | T* p = NULL; Bridge* br = NULL;
|
---|
| 458 | if(mSRef) {nr = mSRef->nref; p = mSRef->data; br = mSRef->bridge;}
|
---|
[268] | 459 | os<<"NDataBlock::Print("<<this<<",Sz="<<mSz<<",IsTemp="<<mIsTemp<<")\n"
|
---|
| 460 | <<" mSRef="<<mSRef<<"(nref="<<nr<<",data="<<p
|
---|
| 461 | <<",bridge="<<br<<")"<<endl;
|
---|
[249] | 462 | if(i1>=mSz || n<=0 || !p) return;
|
---|
[245] | 463 | size_t i2 = i1+n; if(i2>mSz) i2=mSz;
|
---|
[269] | 464 | size_t im = 1; bool enl=false;
|
---|
[245] | 465 | while(i1<i2) {
|
---|
[257] | 466 | enl = false;
|
---|
[268] | 467 | os<<" "<<(*this)(i1); i1++;
|
---|
| 468 | if(im==8) {os<<"\n"; im=1; enl=true;} else im++;
|
---|
[245] | 469 | }
|
---|
[268] | 470 | if(!enl) os<<endl;
|
---|
[245] | 471 | }
|
---|
| 472 |
|
---|
[944] | 473 | //////////////////////////////////////////////
|
---|
| 474 | // Calcul de la somme / produit des donnees //
|
---|
| 475 | //////////////////////////////////////////////
|
---|
[289] | 476 |
|
---|
[944] | 477 | //! Return sum of \b n datas beginning at data \b i1.
|
---|
[289] | 478 | template <class T>
|
---|
| 479 | T NDataBlock<T>::Sum(size_t i1,size_t n) const
|
---|
| 480 | // Somme des elements de i1 a i1+n-1
|
---|
| 481 | {
|
---|
| 482 | if(i1>=mSz) return 0;
|
---|
| 483 | if(n>mSz) n = mSz; if(n==0) n = mSz-i1;
|
---|
| 484 | T const *p=Begin()+i1, *pe=p+n;
|
---|
| 485 | T val = 0;
|
---|
| 486 | while (p<pe) val += *p++;
|
---|
| 487 | return val;
|
---|
| 488 | }
|
---|
| 489 |
|
---|
[944] | 490 | //! Return product of \b n datas beginning at data \b i1.
|
---|
[289] | 491 | template <class T>
|
---|
| 492 | T NDataBlock<T>::Product(size_t i1,size_t n) const
|
---|
| 493 | // Produit des elements de i1 a i1+n-1
|
---|
| 494 | {
|
---|
| 495 | if(i1>=mSz) return 0;
|
---|
| 496 | if(n>mSz) n = mSz; if(n==0) n = mSz-i1;
|
---|
| 497 | T const *p=Begin()+i1, *pe=p+n;
|
---|
| 498 | T val = 0;
|
---|
| 499 | while (p<pe) val *= *p++;
|
---|
| 500 | return val;
|
---|
| 501 | }
|
---|
| 502 |
|
---|
[944] | 503 | ///////////////////////////////////////////////////////////////
|
---|
| 504 | // Surcharge de = : NDataBlock=NDataBlock; NDataBlock=<T> b; //
|
---|
| 505 | ///////////////////////////////////////////////////////////////
|
---|
[245] | 506 |
|
---|
[976] | 507 | //! Operator = : ND = NDa
|
---|
| 508 | /*! \warning Datas are copied (cloned) from \b a. */
|
---|
[245] | 509 | template <class T>
|
---|
[268] | 510 | NDataBlock<T>& NDataBlock<T>::operator = (const NDataBlock<T>& a)
|
---|
[289] | 511 | // Affectation: partage des donnees si "a" temporaire, clone sinon.
|
---|
[245] | 512 | {
|
---|
[944] | 513 | if(Debug_NDataBlock>1)
|
---|
| 514 | cout<<"?_NDataBlock::operator=("<<this<<","<<&a<<") a.(mSz="
|
---|
| 515 | <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp()
|
---|
| 516 | <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
|
---|
[268] | 517 |
|
---|
[245] | 518 | if(this == &a) return *this;
|
---|
[289] | 519 | if(a.mSz==0)
|
---|
[969] | 520 | throw(SzMismatchError("NDataBlock::operator=A null size \n"));
|
---|
[976] | 521 | if (mSz==0) { CloneOrShare(a); return *this; }
|
---|
[969] | 522 | if (a.mSz != mSz)
|
---|
| 523 | throw(SzMismatchError("NDataBlock::operator=A Unequal sizes \n"));
|
---|
| 524 | memcpy(Data(),a.Data(),mSz*sizeof(T));
|
---|
[245] | 525 | return *this;
|
---|
| 526 | }
|
---|
| 527 |
|
---|
[944] | 528 | //! Operator = : ND = \b v (at dats set to \b v).
|
---|
[245] | 529 | template <class T>
|
---|
[249] | 530 | NDataBlock<T>& NDataBlock<T>::operator = (T v)
|
---|
[289] | 531 | // Affectation de tous les elements a une constante "v"
|
---|
[245] | 532 | {
|
---|
[944] | 533 | if(Debug_NDataBlock>1)
|
---|
| 534 | cout<<"?_NDataBlock::operator=("<<this<<","<<v<<")"
|
---|
| 535 | <<" mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
|
---|
[268] | 536 |
|
---|
[245] | 537 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator=v null size\n"));
|
---|
[976] | 538 | T *p=Begin(), *pe=End(); while (p<pe) *p++ = v;
|
---|
[245] | 539 | return *this;
|
---|
| 540 | }
|
---|
| 541 |
|
---|
[944] | 542 | //////////////////////////////////////////////////////////////
|
---|
| 543 | // Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += <T> b; //
|
---|
| 544 | //////////////////////////////////////////////////////////////
|
---|
[245] | 545 |
|
---|
[944] | 546 | //! Add a constant : ND += b
|
---|
[245] | 547 | template <class T>
|
---|
| 548 | NDataBlock<T>& NDataBlock<T>::operator += (T b)
|
---|
| 549 | {
|
---|
| 550 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator+=v null size\n"));
|
---|
[976] | 551 | T *p=Begin(), *pe=End(); while (p<pe) *p++ += b;
|
---|
[245] | 552 | return *this;
|
---|
| 553 | }
|
---|
| 554 |
|
---|
[944] | 555 | //! Substract a constant : ND -= b
|
---|
[245] | 556 | template <class T>
|
---|
| 557 | NDataBlock<T>& NDataBlock<T>::operator -= (T b)
|
---|
| 558 | {
|
---|
| 559 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator-=v null size\n"));
|
---|
[976] | 560 | T *p=Begin(), *pe=End(); while (p<pe) *p++ -= b;
|
---|
[245] | 561 | return *this;
|
---|
| 562 | }
|
---|
| 563 |
|
---|
[944] | 564 | //! Multiply by a constant : ND *= b
|
---|
[245] | 565 | template <class T>
|
---|
| 566 | NDataBlock<T>& NDataBlock<T>::operator *= (T b)
|
---|
| 567 | {
|
---|
| 568 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator*=v null size\n"));
|
---|
[976] | 569 | T *p=Begin(), *pe=End(); while (p<pe) *p++ *= b;
|
---|
[245] | 570 | return *this;
|
---|
| 571 | }
|
---|
| 572 |
|
---|
[976] | 573 | //! Divide by a constant : ND /= b
|
---|
[245] | 574 | template <class T>
|
---|
| 575 | NDataBlock<T>& NDataBlock<T>::operator /= (T b)
|
---|
| 576 | {
|
---|
[249] | 577 | if(b==(T) 0) throw(ParmError("NDataBlock::operator/=v divide by zero\n"));
|
---|
[245] | 578 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator/=v null size\n"));
|
---|
[976] | 579 | T *p=Begin(), *pe=End(); while (p<pe) *p++ /= b;
|
---|
[245] | 580 | return *this;
|
---|
| 581 | }
|
---|
| 582 |
|
---|
[944] | 583 | ////////////////////////////////////////////////////////////////////
|
---|
| 584 | // Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += NDataBlock1; //
|
---|
| 585 | ////////////////////////////////////////////////////////////////////
|
---|
[245] | 586 |
|
---|
[976] | 587 | //! Add a NDataBlock : ND += NDa
|
---|
[245] | 588 | template <class T>
|
---|
[268] | 589 | NDataBlock<T>& NDataBlock<T>::operator += (const NDataBlock<T>& a)
|
---|
[245] | 590 | {
|
---|
[265] | 591 | if(mSz==0 || mSz!=a.mSz)
|
---|
| 592 | throw(SzMismatchError("NDataBlock::operator+=A size mismatch/null"));
|
---|
[268] | 593 | T *p=Begin(), *pe=End();
|
---|
| 594 | T const * pa=a.Begin();
|
---|
[289] | 595 | while (p<pe) *p++ += *pa++;
|
---|
[245] | 596 | return *this;
|
---|
| 597 | }
|
---|
| 598 |
|
---|
[976] | 599 | //! Substract a NDataBlock : ND -= NDa
|
---|
[245] | 600 | template <class T>
|
---|
[268] | 601 | NDataBlock<T>& NDataBlock<T>::operator -= (const NDataBlock<T>& a)
|
---|
[245] | 602 | {
|
---|
[265] | 603 | if(mSz==0 || mSz!=a.mSz)
|
---|
| 604 | throw(SzMismatchError("NDataBlock::operator-=A size mismatch/null"));
|
---|
[268] | 605 | T *p=Begin(), *pe=End();
|
---|
| 606 | T const *pa=a.Begin();
|
---|
[289] | 607 | while (p<pe) *p++ -= *pa++;
|
---|
[245] | 608 | return *this;
|
---|
| 609 | }
|
---|
| 610 |
|
---|
[976] | 611 | //! Multiply by a NDataBlock : ND *= NDa
|
---|
[245] | 612 | template <class T>
|
---|
[268] | 613 | NDataBlock<T>& NDataBlock<T>::operator *= (const NDataBlock<T>& a)
|
---|
[245] | 614 | {
|
---|
[265] | 615 | if(mSz==0 || mSz!=a.mSz)
|
---|
| 616 | throw(SzMismatchError("NDataBlock::operator*=A size mismatch/null"));
|
---|
[268] | 617 | T *p=Begin(), *pe=End();
|
---|
| 618 | T const *pa=a.Begin();
|
---|
[289] | 619 | while (p<pe) *p++ *= *pa++;
|
---|
[245] | 620 | return *this;
|
---|
| 621 | }
|
---|
| 622 |
|
---|
[976] | 623 | //! Divide by a NDataBlock : ND /= NDa
|
---|
[245] | 624 | template <class T>
|
---|
[268] | 625 | NDataBlock<T>& NDataBlock<T>::operator /= (const NDataBlock<T>& a)
|
---|
[289] | 626 | // Attention, aucune protection si un element de "a" est nul.
|
---|
[245] | 627 | {
|
---|
[265] | 628 | if(mSz==0 || mSz!=a.mSz)
|
---|
| 629 | throw(SzMismatchError("NDataBlock::operator/=A size mismatch/null"));
|
---|
[268] | 630 | T *p=Begin(), *pe=End();
|
---|
| 631 | T const *pa=a.Begin();
|
---|
[976] | 632 | while (p<pe) *p++ /= *pa++; // Division par zero non protegee
|
---|
[245] | 633 | return *this;
|
---|
| 634 | }
|
---|
| 635 |
|
---|
[976] | 636 | //////////////////////////////////////////////////////////////////
|
---|
| 637 | // Pour surcharge de +,-,*,/ : NDataBlock = NDataBlock1+<T>b; //
|
---|
| 638 | // NDataBlock = <T>b+NDataBlock1; //
|
---|
| 639 | // Pour la notion de "temporaire" voir blabla dans CloneOrShare //
|
---|
| 640 | //////////////////////////////////////////////////////////////////
|
---|
[245] | 641 |
|
---|
[944] | 642 | //! Add a constant and return NDataBlock : NDret = ND + b
|
---|
[245] | 643 | template <class T>
|
---|
[268] | 644 | NDataBlock<T> NDataBlock<T>::Add(T b) const
|
---|
[257] | 645 | // Pour A+b
|
---|
[245] | 646 | {
|
---|
[969] | 647 | NDataBlock<T> result;
|
---|
| 648 | result.CloneOrShare(*this); result.SetTemp(true);
|
---|
[268] | 649 | result += b;
|
---|
| 650 | return result;
|
---|
[245] | 651 | }
|
---|
| 652 |
|
---|
[976] | 653 | //! Substract a constant and return NDataBlock : NDret = ND - b or NDret = b - ND
|
---|
| 654 | /*! Substract a constant or from a constant
|
---|
| 655 | \param fginv==false : performs NDret = ND - b (default)
|
---|
| 656 | \param fginv==true : performs NDret = b - ND
|
---|
| 657 | */
|
---|
[245] | 658 | template <class T>
|
---|
[976] | 659 | NDataBlock<T> NDataBlock<T>::Sub(T b,bool fginv) const
|
---|
| 660 | // Pour A-b sauf si fginv==true b-A (- n'est pas commutatif!)
|
---|
[245] | 661 | {
|
---|
[969] | 662 | NDataBlock<T> result;
|
---|
| 663 | result.CloneOrShare(*this); result.SetTemp(true);
|
---|
[976] | 664 | if(fginv) {
|
---|
| 665 | T *p=result.Begin(), *pe=result.End();
|
---|
| 666 | T const *pa=this->Begin();
|
---|
| 667 | while(p<pe) {*p++ = b - *pa++;}
|
---|
| 668 | } else result -= b;
|
---|
[245] | 669 | return result;
|
---|
| 670 | }
|
---|
| 671 |
|
---|
[944] | 672 | //! Multiply by a constant and return NDataBlock : NDret = ND * b
|
---|
[245] | 673 | template <class T>
|
---|
[268] | 674 | NDataBlock<T> NDataBlock<T>::Mul(T b) const
|
---|
[257] | 675 | // Pour A*b
|
---|
[245] | 676 | {
|
---|
[969] | 677 | NDataBlock<T> result;
|
---|
| 678 | result.CloneOrShare(*this); result.SetTemp(true);
|
---|
[976] | 679 | result *= b;
|
---|
| 680 | return result;
|
---|
[245] | 681 | }
|
---|
| 682 |
|
---|
[976] | 683 | //! Divide by a constant and return NDataBlock : NDret = ND / b or NDret = b / ND
|
---|
| 684 | /*! Divide by a constant or from a constant
|
---|
| 685 | \param fginv==false : performs NDret = ND / b (default)
|
---|
| 686 | \param fginv==true : performs NDret = b / ND
|
---|
| 687 | */
|
---|
[245] | 688 | template <class T>
|
---|
[976] | 689 | NDataBlock<T> NDataBlock<T>::Div(T b,bool fginv) const
|
---|
| 690 | // Pour A/b sauf si fginv==true b/A (/ n'est pas commutatif!)
|
---|
[245] | 691 | {
|
---|
[969] | 692 | NDataBlock<T> result;
|
---|
| 693 | result.CloneOrShare(*this); result.SetTemp(true);
|
---|
[976] | 694 | if(fginv) {
|
---|
| 695 | T *p=result.Begin(), *pe=result.End();
|
---|
| 696 | T const *pa = this->Begin();
|
---|
| 697 | while(p<pe) {*p++ = b / *pa++;} // Division par zero non protegee
|
---|
| 698 | } else {
|
---|
| 699 | if( b == (T) 0 ) throw MathExc("NDataBlock<T>::Div(T) - Divide by zero ! ");
|
---|
| 700 | result /= b;
|
---|
[245] | 701 | }
|
---|
| 702 | return result;
|
---|
| 703 | }
|
---|
| 704 |
|
---|
[944] | 705 | ///////////////////////////////////////////////////////////////////////
|
---|
| 706 | // Pour surcharge de +,-,*,/ : NDataBlock = NDataBlock1+NDataBlock2; //
|
---|
| 707 | ///////////////////////////////////////////////////////////////////////
|
---|
[245] | 708 |
|
---|
[944] | 709 | //! Add a NDataBlock and return a NDataBlock: ND = NDthis + NDb
|
---|
[245] | 710 | template <class T>
|
---|
[268] | 711 | NDataBlock<T> NDataBlock<T>::Add(const NDataBlock<T>& b) const
|
---|
[257] | 712 | // Pour A+B
|
---|
[245] | 713 | {
|
---|
[285] | 714 | if(mSz!=b.mSz)
|
---|
[265] | 715 | throw(SzMismatchError("NDataBlock operator C=A+B size mismatch/null\n"));
|
---|
[268] | 716 | NDataBlock<T> result; result.SetTemp(true);
|
---|
[289] | 717 | if(b.IsTemp()) {result.Share(b); result += *this;}
|
---|
| 718 | else {result.CloneOrShare(*this); result += b;}
|
---|
[268] | 719 | return result;
|
---|
[245] | 720 | }
|
---|
| 721 |
|
---|
[944] | 722 | //! Multiply by a NDataBlock and return a NDataBlock: ND = NDthis * NDb
|
---|
[245] | 723 | template <class T>
|
---|
[268] | 724 | NDataBlock<T> NDataBlock<T>::Mul(const NDataBlock<T>& b) const
|
---|
[257] | 725 | // Pour A*B
|
---|
[245] | 726 | {
|
---|
[285] | 727 | if(mSz!=b.mSz)
|
---|
[265] | 728 | throw(SzMismatchError("NDataBlock operator C=A*B size mismatch/null\n"));
|
---|
[268] | 729 | NDataBlock<T> result; result.SetTemp(true);
|
---|
[289] | 730 | if(b.IsTemp()) {result.Share(b); result *= *this;}
|
---|
| 731 | else {result.CloneOrShare(*this); result *= b;}
|
---|
[268] | 732 | return result;
|
---|
[245] | 733 | }
|
---|
| 734 |
|
---|
[944] | 735 | //! Substract a NDataBlock and return a NDataBlock: ND = NDthis - NDb
|
---|
[245] | 736 | template <class T>
|
---|
[268] | 737 | NDataBlock<T> NDataBlock<T>::Sub(const NDataBlock<T>& b) const
|
---|
[257] | 738 | // Pour A-B
|
---|
[245] | 739 | {
|
---|
[285] | 740 | if(mSz!=b.mSz)
|
---|
[265] | 741 | throw(SzMismatchError("NDataBlock operator C=A-B size mismatch/null\n"));
|
---|
[268] | 742 | NDataBlock<T> result; result.SetTemp(true);
|
---|
[245] | 743 | if(b.IsTemp()) {
|
---|
[268] | 744 | result.Share(b);
|
---|
[289] | 745 | T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
|
---|
[245] | 746 | while(p<pe) {*p = *pa++ - *p; p++;}
|
---|
[289] | 747 | } else {result.CloneOrShare(*this); result -= b;}
|
---|
[268] | 748 | return result;
|
---|
[245] | 749 | }
|
---|
| 750 |
|
---|
[944] | 751 | //! Divide by a NDataBlock and return a NDataBlock: ND = NDthis / NDb
|
---|
[245] | 752 | template <class T>
|
---|
[268] | 753 | NDataBlock<T> NDataBlock<T>::Div(const NDataBlock<T>& b) const
|
---|
[257] | 754 | // Pour A/B
|
---|
[245] | 755 | {
|
---|
[285] | 756 | if(mSz!=b.mSz)
|
---|
[265] | 757 | throw(SzMismatchError("NDataBlock operator C=A/B size mismatch/null\n"));
|
---|
[268] | 758 | NDataBlock<T> result; result.SetTemp(true);
|
---|
[245] | 759 | if(b.IsTemp()) {
|
---|
[268] | 760 | result.Share(b);
|
---|
[289] | 761 | T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
|
---|
[976] | 762 | while(p<pe) {*p = *pa++ / *p; p++;} // Division par zero non protegee
|
---|
[289] | 763 | } else {result.CloneOrShare(*this); result /= b;}
|
---|
[268] | 764 | return result;
|
---|
[245] | 765 | }
|
---|
| 766 |
|
---|
[269] | 767 |
|
---|
[275] | 768 | ///////////////////////////////////////////////////////////////
|
---|
[245] | 769 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 770 | #pragma define_template NDataBlock<uint_1>
|
---|
| 771 | #pragma define_template NDataBlock<uint_2>
|
---|
| 772 | #pragma define_template NDataBlock<int_2>
|
---|
| 773 | #pragma define_template NDataBlock<int_4>
|
---|
| 774 | #pragma define_template NDataBlock<int_8>
|
---|
| 775 | #pragma define_template NDataBlock<uint_4>
|
---|
| 776 | #pragma define_template NDataBlock<uint_8>
|
---|
| 777 | #pragma define_template NDataBlock<r_4>
|
---|
| 778 | #pragma define_template NDataBlock<r_8>
|
---|
[802] | 779 | #pragma define_template NDataBlock< complex<r_4> >
|
---|
| 780 | #pragma define_template NDataBlock< complex<r_8> >
|
---|
[245] | 781 | #endif
|
---|
| 782 |
|
---|
[269] | 783 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
[245] | 784 | template class NDataBlock<uint_1>;
|
---|
| 785 | template class NDataBlock<uint_2>;
|
---|
| 786 | template class NDataBlock<int_2>;
|
---|
| 787 | template class NDataBlock<int_4>;
|
---|
| 788 | template class NDataBlock<int_8>;
|
---|
| 789 | template class NDataBlock<uint_4>;
|
---|
| 790 | template class NDataBlock<uint_8>;
|
---|
| 791 | template class NDataBlock<r_4>;
|
---|
| 792 | template class NDataBlock<r_8>;
|
---|
[802] | 793 | template class NDataBlock< complex<r_4> >;
|
---|
| 794 | template class NDataBlock< complex<r_8> >;
|
---|
[245] | 795 | #endif
|
---|