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