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