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