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