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