[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>
|
---|
[3619] | 9 | #include <string.h>
|
---|
[245] | 10 | #include <complex>
|
---|
| 11 | #include "pexceptions.h"
|
---|
[3831] | 12 |
|
---|
| 13 | #define NDATABLOCK_CC_BFILE // avoid extern template declarations
|
---|
[245] | 14 | #include "ndatablock.h"
|
---|
[3831] | 15 |
|
---|
[3212] | 16 | #include "thsafeop.h" // for ThreadSafe operations (Ref.Count/Share)
|
---|
[245] | 17 |
|
---|
[2657] | 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 |
|
---|
[944] | 26 | /*!
|
---|
| 27 | \class SOPHYA::NDataBlock
|
---|
[1607] | 28 | \ingroup BaseTools
|
---|
[944] | 29 | Management of data blocks
|
---|
| 30 | */
|
---|
[268] | 31 |
|
---|
[944] | 32 | //////////////////////////////////
|
---|
| 33 | // Fonctionnement en mode debug //
|
---|
| 34 | //////////////////////////////////
|
---|
[245] | 35 |
|
---|
[944] | 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;
|
---|
[3208] | 39 | template <class T> ThSafeOp* NDataBlock<T>::gThsop = NULL;
|
---|
[245] | 40 |
|
---|
[944] | 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 | */
|
---|
[245] | 52 | template <class T>
|
---|
[944] | 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 |
|
---|
[2565] | 88 | //! Constructor for \b n datas. if \b zero=true, filled with zeros
|
---|
[944] | 89 | template <class T>
|
---|
[2565] | 90 | NDataBlock<T>::NDataBlock(size_t n, bool fzero)
|
---|
[245] | 91 | // Createur d'une structure de "n" donnees
|
---|
| 92 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
| 93 | {
|
---|
[944] | 94 | if(Debug_NDataBlock>1)
|
---|
| 95 | cout<<"?_NDataBlock::NDataBlock("<<this<<",n="<<n<<")"<<endl;
|
---|
[3208] | 96 | if (gThsop == NULL) gThsop = new ThSafeOp;
|
---|
[268] | 97 |
|
---|
[2565] | 98 | Alloc(n, NULL, NULL, fzero); // allocation et mise a zero
|
---|
[245] | 99 | }
|
---|
| 100 |
|
---|
[944] | 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 | */
|
---|
[245] | 107 | template <class T>
|
---|
| 108 | NDataBlock<T>::NDataBlock(size_t n, T* data, Bridge* br)
|
---|
[502] | 109 | // Createur d'une structure de "n" donnees, avec donnees preallouees.
|
---|
| 110 | // Attention createur TRES DANGEREUX (Voir explications dans Alloc()).
|
---|
[245] | 111 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
| 112 | {
|
---|
[944] | 113 | if(Debug_NDataBlock>1)
|
---|
| 114 | cout<<"?_NDataBlock::NDataBlock("<<this
|
---|
| 115 | <<",data="<<data<<",br="<<br<<")"<<endl;
|
---|
[3208] | 116 | if (gThsop == NULL) gThsop = new ThSafeOp;
|
---|
[268] | 117 |
|
---|
[245] | 118 | Alloc(n,data,br);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[944] | 121 | //! Default constructor
|
---|
[245] | 122 | template <class T>
|
---|
| 123 | NDataBlock<T>::NDataBlock()
|
---|
| 124 | // Createur par default
|
---|
| 125 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
| 126 | {
|
---|
[944] | 127 | if(Debug_NDataBlock>1)
|
---|
| 128 | cout<<"?_NDataBlock::NDataBlock("<<this<<") default"<<endl;
|
---|
[3208] | 129 | if (gThsop == NULL) gThsop = new ThSafeOp;
|
---|
[245] | 130 | }
|
---|
| 131 |
|
---|
[944] | 132 | //! Copy constructor
|
---|
| 133 | /*!
|
---|
[976] | 134 | \warning datas are \b SHARED with \b a.
|
---|
[944] | 135 | */
|
---|
[245] | 136 | template <class T>
|
---|
[268] | 137 | NDataBlock<T>::NDataBlock(const NDataBlock<T>& a)
|
---|
[969] | 138 | // Createur par copie: partage les donnees dans tous les cas
|
---|
[245] | 139 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
| 140 | {
|
---|
[944] | 141 | if(Debug_NDataBlock>1)
|
---|
[1349] | 142 | cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<" a.mSz="<<a.mSz<<")"<<endl;
|
---|
[268] | 143 |
|
---|
[1349] | 144 | if(a.mSRef && a.mSz>0) Share(a);
|
---|
[245] | 145 | }
|
---|
| 146 |
|
---|
[944] | 147 | //! Copy constructor with \b share option
|
---|
| 148 | /*!
|
---|
| 149 | \warning datas are shared if \b share is \b true, cloned if not.
|
---|
| 150 | */
|
---|
[245] | 151 | template <class T>
|
---|
[268] | 152 | NDataBlock<T>::NDataBlock(const NDataBlock<T>& a,bool share)
|
---|
[289] | 153 | // Createur avec choix de partager ou non selon "share"
|
---|
[245] | 154 | : mSz(0), mSRef(NULL), mIsTemp(false)
|
---|
| 155 | {
|
---|
[944] | 156 | if(Debug_NDataBlock>1)
|
---|
[946] | 157 | cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a
|
---|
| 158 | <<",sh=<<"<<share<<")"<<endl;
|
---|
[268] | 159 |
|
---|
[1349] | 160 | if(a.mSRef && a.mSz>0) {if(share) Share(a); else Clone(a);}
|
---|
[245] | 161 | }
|
---|
| 162 |
|
---|
[944] | 163 | //! Destructor
|
---|
[245] | 164 | template <class T>
|
---|
| 165 | NDataBlock<T>::~NDataBlock()
|
---|
| 166 | // Destructeur
|
---|
| 167 | {
|
---|
[944] | 168 | if(Debug_NDataBlock>1)
|
---|
| 169 | cout<<"?_NDataBlock::~NDataBlock("<<this<<")"<<endl;
|
---|
[268] | 170 |
|
---|
[3208] | 171 | Dealloc(); // ThreadSafe version of Delete()
|
---|
[245] | 172 | }
|
---|
| 173 |
|
---|
[944] | 174 | ////////////////////////
|
---|
| 175 | // Gestion de donnees //
|
---|
| 176 | ////////////////////////
|
---|
[275] | 177 |
|
---|
[944] | 178 | //! Clone datas from \b a
|
---|
[245] | 179 | template <class T>
|
---|
[268] | 180 | void NDataBlock<T>::Clone(const NDataBlock<T>& a)
|
---|
[289] | 181 | // Clone: copie de donnees a partir de "a"
|
---|
[245] | 182 | {
|
---|
[944] | 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;
|
---|
[275] | 187 |
|
---|
[289] | 188 | if(&a==NULL) throw(NullPtrError("NDataBlock::Clone &a==NULL\n"));
|
---|
[1349] | 189 | if(!a.mSRef || a.mSz==0) throw(NullPtrError("NDataBlock::Clone a.mSz=0\n"));
|
---|
[773] | 190 | Alloc(a.mSz, NULL, NULL, false); // pas de mise a zero
|
---|
[289] | 191 | memcpy(Data(),a.Data(),mSz*sizeof(T));
|
---|
[245] | 192 | }
|
---|
| 193 |
|
---|
[944] | 194 | //! Share datas with \b a
|
---|
[245] | 195 | template <class T>
|
---|
[944] | 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"));
|
---|
[3208] | 208 | //--- Start of atomic (in one block) operation for thread safety
|
---|
| 209 | gThsop->lock(); // (ThreadSafe)
|
---|
[944] | 210 | if(mSRef) Delete();
|
---|
| 211 | mSz = a.mSz; mSRef = a.mSRef; mSRef->nref++;
|
---|
[3208] | 212 | gThsop->unlock(); // (ThreadSafe)
|
---|
| 213 | //--- End of atomic operation
|
---|
[944] | 214 |
|
---|
| 215 | if(Debug_NDataBlock>1)
|
---|
| 216 | cout<<"...?_NDataBlock::Share mSz="<<mSz<<" mSRef="<<mSRef
|
---|
| 217 | <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<< mSRef->data
|
---|
[946] | 218 | <<" mSRef->bridge="<<mSRef->bridge
|
---|
| 219 | <<" IsTemp="<<mIsTemp<<endl;
|
---|
[944] | 220 | }
|
---|
| 221 |
|
---|
| 222 | //! \b Share with \b a if \b temporary, \b clone from \b a if not.
|
---|
[976] | 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 | */
|
---|
[944] | 250 | template <class T>
|
---|
[289] | 251 | void NDataBlock<T>::CloneOrShare(const NDataBlock<T>& a)
|
---|
| 252 | // CloneOrShare: Share si "a" temporaire, Clone sinon.
|
---|
| 253 | {
|
---|
[944] | 254 | if(Debug_NDataBlock>1)
|
---|
| 255 | cout<<"?_NDataBlock::CloneOrShare("<<this<<","<<&a<<")"<<endl;
|
---|
[289] | 256 |
|
---|
| 257 | if(&a==NULL) throw(NullPtrError("NDataBlock::CloneOrShare &a==NULL\n"));
|
---|
| 258 | if(a.IsTemp()) Share(a); else Clone(a);
|
---|
| 259 | }
|
---|
| 260 |
|
---|
[944] | 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 | */
|
---|
[1426] | 332 |
|
---|
| 333 |
|
---|
[289] | 334 | template <class T>
|
---|
[944] | 335 | void NDataBlock<T>::Alloc(size_t n,T* data,Bridge* br,bool zero)
|
---|
[245] | 336 | {
|
---|
[944] | 337 | if(Debug_NDataBlock>1)
|
---|
| 338 | cout<<"?_NDataBlock::Alloc("<<this<<","
|
---|
| 339 | <<n<<","<<data<<","<<br<<") mSz="<<mSz
|
---|
| 340 | <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
|
---|
[268] | 341 |
|
---|
[944] | 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"));
|
---|
[3208] | 345 | //--- Start of atomic (in one block) operation for thread safety (ThreadSafe)
|
---|
| 346 | gThsop->lock(); // (ThreadSafe)
|
---|
[245] | 347 | if(mSRef) Delete();
|
---|
[944] | 348 | mSz = n;
|
---|
| 349 | mSRef = new NDREF;
|
---|
| 350 | mSRef->nref = 1;
|
---|
[2657] | 351 | mSRef->dsid = AnyDataObj::getUniqueId();
|
---|
[944] | 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;
|
---|
[3208] | 355 | gThsop->unlock(); // (ThreadSafe)
|
---|
| 356 | //--- End of atomic operation (ThreadSafe)
|
---|
[268] | 357 |
|
---|
[944] | 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
|
---|
[946] | 366 | <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<<mSRef->data
|
---|
| 367 | <<" mSRef->bridge="<<mSRef->bridge
|
---|
| 368 | <<" IsTemp="<<mIsTemp
|
---|
[944] | 369 | <<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
|
---|
[245] | 370 | }
|
---|
[944] | 371 | }
|
---|
[245] | 372 |
|
---|
[3208] | 373 | //! Management of de-allocation (NOT thread-safe)
|
---|
[245] | 374 | template <class T>
|
---|
| 375 | void NDataBlock<T>::Delete(void)
|
---|
| 376 | // Pour detruire les pointeurs en tenant compte des references
|
---|
| 377 | {
|
---|
[944] | 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 | }
|
---|
[268] | 386 |
|
---|
[289] | 387 | if(mSRef==NULL) return;
|
---|
[946] | 388 |
|
---|
[245] | 389 | mSRef->nref--;
|
---|
| 390 | if(mSRef->nref != 0) {
|
---|
[268] | 391 |
|
---|
[944] | 392 | if(Debug_NDataBlock>1)
|
---|
[502] | 393 | cout<<"...?_NDataBlock::Delete() pas de desallocation il reste nref="
|
---|
| 394 | <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
|
---|
[268] | 395 |
|
---|
[285] | 396 | mSz = 0; mSRef=NULL;
|
---|
[245] | 397 | return;
|
---|
| 398 | }
|
---|
[268] | 399 |
|
---|
[944] | 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 | }
|
---|
[268] | 406 |
|
---|
[245] | 407 | // Si il y a un Bridge les donnees ne n'appartiennent pas, on detruit le Bridge
|
---|
[285] | 408 | // sinon, les donnees ont ete allouees par nos soins, on libere l'espace
|
---|
[3113] | 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 | }
|
---|
[245] | 418 | mSRef->bridge=NULL; mSRef->data=NULL;
|
---|
[285] | 419 | delete mSRef; mSRef=NULL; mSz = 0;
|
---|
[245] | 420 | }
|
---|
| 421 |
|
---|
[944] | 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 | */
|
---|
[245] | 428 | template <class T>
|
---|
[257] | 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
|
---|
[289] | 432 | // - Si classe connectee : on ecrit selon la longueur minimale
|
---|
| 433 | // (cad this->mSz ou "n")
|
---|
[257] | 434 | {
|
---|
| 435 | if(data==NULL) throw(NullPtrError("NDataBlock::FillFrom data==NULL\n"));
|
---|
| 436 | if(n==0) throw(ParmError("NDataBlock::FillFrom n<=0\n"));
|
---|
[773] | 437 | if(mSRef==NULL) Alloc(n, NULL, NULL, false); // Pas de mise a zero
|
---|
[285] | 438 | if(mSz<n) n = mSz;
|
---|
[257] | 439 | memcpy(Data(),data,n*sizeof(T));
|
---|
| 440 | }
|
---|
| 441 |
|
---|
[944] | 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 | */
|
---|
[502] | 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 |
|
---|
[3208] | 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 |
|
---|
[944] | 492 | ////////////////
|
---|
| 493 | // Impression //
|
---|
| 494 | ////////////////
|
---|
[245] | 495 |
|
---|
[944] | 496 | //! Give infos and print \b n datas beginning at \b i1 on stream \b os.
|
---|
[245] | 497 | template <class T>
|
---|
[268] | 498 | void NDataBlock<T>::Print(ostream& os,size_t i1,size_t n) const
|
---|
[245] | 499 | // Impression de n elements a partir de i1
|
---|
| 500 | {
|
---|
| 501 | size_t nr = 0;
|
---|
[267] | 502 | T* p = NULL; Bridge* br = NULL;
|
---|
| 503 | if(mSRef) {nr = mSRef->nref; p = mSRef->data; br = mSRef->bridge;}
|
---|
[268] | 504 | os<<"NDataBlock::Print("<<this<<",Sz="<<mSz<<",IsTemp="<<mIsTemp<<")\n"
|
---|
| 505 | <<" mSRef="<<mSRef<<"(nref="<<nr<<",data="<<p
|
---|
| 506 | <<",bridge="<<br<<")"<<endl;
|
---|
[249] | 507 | if(i1>=mSz || n<=0 || !p) return;
|
---|
[245] | 508 | size_t i2 = i1+n; if(i2>mSz) i2=mSz;
|
---|
[269] | 509 | size_t im = 1; bool enl=false;
|
---|
[245] | 510 | while(i1<i2) {
|
---|
[257] | 511 | enl = false;
|
---|
[268] | 512 | os<<" "<<(*this)(i1); i1++;
|
---|
| 513 | if(im==8) {os<<"\n"; im=1; enl=true;} else im++;
|
---|
[245] | 514 | }
|
---|
[268] | 515 | if(!enl) os<<endl;
|
---|
[245] | 516 | }
|
---|
| 517 |
|
---|
[944] | 518 | //////////////////////////////////////////////
|
---|
| 519 | // Calcul de la somme / produit des donnees //
|
---|
| 520 | //////////////////////////////////////////////
|
---|
[289] | 521 |
|
---|
[944] | 522 | //! Return sum of \b n datas beginning at data \b i1.
|
---|
[289] | 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 |
|
---|
[944] | 535 | //! Return product of \b n datas beginning at data \b i1.
|
---|
[289] | 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 |
|
---|
[944] | 548 | ///////////////////////////////////////////////////////////////
|
---|
| 549 | // Surcharge de = : NDataBlock=NDataBlock; NDataBlock=<T> b; //
|
---|
| 550 | ///////////////////////////////////////////////////////////////
|
---|
[245] | 551 |
|
---|
[976] | 552 | //! Operator = : ND = NDa
|
---|
| 553 | /*! \warning Datas are copied (cloned) from \b a. */
|
---|
[245] | 554 | template <class T>
|
---|
[268] | 555 | NDataBlock<T>& NDataBlock<T>::operator = (const NDataBlock<T>& a)
|
---|
[289] | 556 | // Affectation: partage des donnees si "a" temporaire, clone sinon.
|
---|
[245] | 557 | {
|
---|
[944] | 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;
|
---|
[268] | 562 |
|
---|
[245] | 563 | if(this == &a) return *this;
|
---|
[289] | 564 | if(a.mSz==0)
|
---|
[969] | 565 | throw(SzMismatchError("NDataBlock::operator=A null size \n"));
|
---|
[976] | 566 | if (mSz==0) { CloneOrShare(a); return *this; }
|
---|
[969] | 567 | if (a.mSz != mSz)
|
---|
| 568 | throw(SzMismatchError("NDataBlock::operator=A Unequal sizes \n"));
|
---|
| 569 | memcpy(Data(),a.Data(),mSz*sizeof(T));
|
---|
[245] | 570 | return *this;
|
---|
| 571 | }
|
---|
| 572 |
|
---|
[944] | 573 | //! Operator = : ND = \b v (at dats set to \b v).
|
---|
[245] | 574 | template <class T>
|
---|
[249] | 575 | NDataBlock<T>& NDataBlock<T>::operator = (T v)
|
---|
[289] | 576 | // Affectation de tous les elements a une constante "v"
|
---|
[245] | 577 | {
|
---|
[944] | 578 | if(Debug_NDataBlock>1)
|
---|
| 579 | cout<<"?_NDataBlock::operator=("<<this<<","<<v<<")"
|
---|
| 580 | <<" mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
|
---|
[268] | 581 |
|
---|
[245] | 582 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator=v null size\n"));
|
---|
[976] | 583 | T *p=Begin(), *pe=End(); while (p<pe) *p++ = v;
|
---|
[245] | 584 | return *this;
|
---|
| 585 | }
|
---|
| 586 |
|
---|
[944] | 587 | //////////////////////////////////////////////////////////////
|
---|
| 588 | // Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += <T> b; //
|
---|
| 589 | //////////////////////////////////////////////////////////////
|
---|
[245] | 590 |
|
---|
[944] | 591 | //! Add a constant : ND += b
|
---|
[245] | 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"));
|
---|
[976] | 596 | T *p=Begin(), *pe=End(); while (p<pe) *p++ += b;
|
---|
[245] | 597 | return *this;
|
---|
| 598 | }
|
---|
| 599 |
|
---|
[944] | 600 | //! Substract a constant : ND -= b
|
---|
[245] | 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"));
|
---|
[976] | 605 | T *p=Begin(), *pe=End(); while (p<pe) *p++ -= b;
|
---|
[245] | 606 | return *this;
|
---|
| 607 | }
|
---|
| 608 |
|
---|
[944] | 609 | //! Multiply by a constant : ND *= b
|
---|
[245] | 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"));
|
---|
[976] | 614 | T *p=Begin(), *pe=End(); while (p<pe) *p++ *= b;
|
---|
[245] | 615 | return *this;
|
---|
| 616 | }
|
---|
| 617 |
|
---|
[976] | 618 | //! Divide by a constant : ND /= b
|
---|
[245] | 619 | template <class T>
|
---|
| 620 | NDataBlock<T>& NDataBlock<T>::operator /= (T b)
|
---|
| 621 | {
|
---|
[249] | 622 | if(b==(T) 0) throw(ParmError("NDataBlock::operator/=v divide by zero\n"));
|
---|
[245] | 623 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator/=v null size\n"));
|
---|
[976] | 624 | T *p=Begin(), *pe=End(); while (p<pe) *p++ /= b;
|
---|
[245] | 625 | return *this;
|
---|
| 626 | }
|
---|
| 627 |
|
---|
[944] | 628 | ////////////////////////////////////////////////////////////////////
|
---|
| 629 | // Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += NDataBlock1; //
|
---|
| 630 | ////////////////////////////////////////////////////////////////////
|
---|
[245] | 631 |
|
---|
[976] | 632 | //! Add a NDataBlock : ND += NDa
|
---|
[245] | 633 | template <class T>
|
---|
[268] | 634 | NDataBlock<T>& NDataBlock<T>::operator += (const NDataBlock<T>& a)
|
---|
[245] | 635 | {
|
---|
[265] | 636 | if(mSz==0 || mSz!=a.mSz)
|
---|
| 637 | throw(SzMismatchError("NDataBlock::operator+=A size mismatch/null"));
|
---|
[268] | 638 | T *p=Begin(), *pe=End();
|
---|
| 639 | T const * pa=a.Begin();
|
---|
[289] | 640 | while (p<pe) *p++ += *pa++;
|
---|
[245] | 641 | return *this;
|
---|
| 642 | }
|
---|
| 643 |
|
---|
[976] | 644 | //! Substract a NDataBlock : ND -= NDa
|
---|
[245] | 645 | template <class T>
|
---|
[268] | 646 | NDataBlock<T>& NDataBlock<T>::operator -= (const NDataBlock<T>& a)
|
---|
[245] | 647 | {
|
---|
[265] | 648 | if(mSz==0 || mSz!=a.mSz)
|
---|
| 649 | throw(SzMismatchError("NDataBlock::operator-=A size mismatch/null"));
|
---|
[268] | 650 | T *p=Begin(), *pe=End();
|
---|
| 651 | T const *pa=a.Begin();
|
---|
[289] | 652 | while (p<pe) *p++ -= *pa++;
|
---|
[245] | 653 | return *this;
|
---|
| 654 | }
|
---|
| 655 |
|
---|
[976] | 656 | //! Multiply by a NDataBlock : ND *= NDa
|
---|
[245] | 657 | template <class T>
|
---|
[268] | 658 | NDataBlock<T>& NDataBlock<T>::operator *= (const NDataBlock<T>& a)
|
---|
[245] | 659 | {
|
---|
[265] | 660 | if(mSz==0 || mSz!=a.mSz)
|
---|
| 661 | throw(SzMismatchError("NDataBlock::operator*=A size mismatch/null"));
|
---|
[268] | 662 | T *p=Begin(), *pe=End();
|
---|
| 663 | T const *pa=a.Begin();
|
---|
[289] | 664 | while (p<pe) *p++ *= *pa++;
|
---|
[245] | 665 | return *this;
|
---|
| 666 | }
|
---|
| 667 |
|
---|
[976] | 668 | //! Divide by a NDataBlock : ND /= NDa
|
---|
[245] | 669 | template <class T>
|
---|
[268] | 670 | NDataBlock<T>& NDataBlock<T>::operator /= (const NDataBlock<T>& a)
|
---|
[289] | 671 | // Attention, aucune protection si un element de "a" est nul.
|
---|
[245] | 672 | {
|
---|
[265] | 673 | if(mSz==0 || mSz!=a.mSz)
|
---|
| 674 | throw(SzMismatchError("NDataBlock::operator/=A size mismatch/null"));
|
---|
[268] | 675 | T *p=Begin(), *pe=End();
|
---|
| 676 | T const *pa=a.Begin();
|
---|
[976] | 677 | while (p<pe) *p++ /= *pa++; // Division par zero non protegee
|
---|
[245] | 678 | return *this;
|
---|
| 679 | }
|
---|
| 680 |
|
---|
[976] | 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 | //////////////////////////////////////////////////////////////////
|
---|
[245] | 686 |
|
---|
[944] | 687 | //! Add a constant and return NDataBlock : NDret = ND + b
|
---|
[245] | 688 | template <class T>
|
---|
[268] | 689 | NDataBlock<T> NDataBlock<T>::Add(T b) const
|
---|
[257] | 690 | // Pour A+b
|
---|
[245] | 691 | {
|
---|
[969] | 692 | NDataBlock<T> result;
|
---|
| 693 | result.CloneOrShare(*this); result.SetTemp(true);
|
---|
[268] | 694 | result += b;
|
---|
| 695 | return result;
|
---|
[245] | 696 | }
|
---|
| 697 |
|
---|
[976] | 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 | */
|
---|
[245] | 703 | template <class T>
|
---|
[976] | 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!)
|
---|
[245] | 706 | {
|
---|
[969] | 707 | NDataBlock<T> result;
|
---|
| 708 | result.CloneOrShare(*this); result.SetTemp(true);
|
---|
[976] | 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;
|
---|
[245] | 714 | return result;
|
---|
| 715 | }
|
---|
| 716 |
|
---|
[944] | 717 | //! Multiply by a constant and return NDataBlock : NDret = ND * b
|
---|
[245] | 718 | template <class T>
|
---|
[268] | 719 | NDataBlock<T> NDataBlock<T>::Mul(T b) const
|
---|
[257] | 720 | // Pour A*b
|
---|
[245] | 721 | {
|
---|
[969] | 722 | NDataBlock<T> result;
|
---|
| 723 | result.CloneOrShare(*this); result.SetTemp(true);
|
---|
[976] | 724 | result *= b;
|
---|
| 725 | return result;
|
---|
[245] | 726 | }
|
---|
| 727 |
|
---|
[976] | 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 | */
|
---|
[245] | 733 | template <class T>
|
---|
[976] | 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!)
|
---|
[245] | 736 | {
|
---|
[969] | 737 | NDataBlock<T> result;
|
---|
| 738 | result.CloneOrShare(*this); result.SetTemp(true);
|
---|
[976] | 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;
|
---|
[245] | 746 | }
|
---|
| 747 | return result;
|
---|
| 748 | }
|
---|
| 749 |
|
---|
[944] | 750 | ///////////////////////////////////////////////////////////////////////
|
---|
| 751 | // Pour surcharge de +,-,*,/ : NDataBlock = NDataBlock1+NDataBlock2; //
|
---|
| 752 | ///////////////////////////////////////////////////////////////////////
|
---|
[245] | 753 |
|
---|
[944] | 754 | //! Add a NDataBlock and return a NDataBlock: ND = NDthis + NDb
|
---|
[245] | 755 | template <class T>
|
---|
[268] | 756 | NDataBlock<T> NDataBlock<T>::Add(const NDataBlock<T>& b) const
|
---|
[257] | 757 | // Pour A+B
|
---|
[245] | 758 | {
|
---|
[285] | 759 | if(mSz!=b.mSz)
|
---|
[265] | 760 | throw(SzMismatchError("NDataBlock operator C=A+B size mismatch/null\n"));
|
---|
[268] | 761 | NDataBlock<T> result; result.SetTemp(true);
|
---|
[289] | 762 | if(b.IsTemp()) {result.Share(b); result += *this;}
|
---|
| 763 | else {result.CloneOrShare(*this); result += b;}
|
---|
[268] | 764 | return result;
|
---|
[245] | 765 | }
|
---|
| 766 |
|
---|
[944] | 767 | //! Multiply by a NDataBlock and return a NDataBlock: ND = NDthis * NDb
|
---|
[245] | 768 | template <class T>
|
---|
[268] | 769 | NDataBlock<T> NDataBlock<T>::Mul(const NDataBlock<T>& b) const
|
---|
[257] | 770 | // Pour A*B
|
---|
[245] | 771 | {
|
---|
[285] | 772 | if(mSz!=b.mSz)
|
---|
[265] | 773 | throw(SzMismatchError("NDataBlock operator C=A*B size mismatch/null\n"));
|
---|
[268] | 774 | NDataBlock<T> result; result.SetTemp(true);
|
---|
[289] | 775 | if(b.IsTemp()) {result.Share(b); result *= *this;}
|
---|
| 776 | else {result.CloneOrShare(*this); result *= b;}
|
---|
[268] | 777 | return result;
|
---|
[245] | 778 | }
|
---|
| 779 |
|
---|
[944] | 780 | //! Substract a NDataBlock and return a NDataBlock: ND = NDthis - NDb
|
---|
[245] | 781 | template <class T>
|
---|
[268] | 782 | NDataBlock<T> NDataBlock<T>::Sub(const NDataBlock<T>& b) const
|
---|
[257] | 783 | // Pour A-B
|
---|
[245] | 784 | {
|
---|
[285] | 785 | if(mSz!=b.mSz)
|
---|
[265] | 786 | throw(SzMismatchError("NDataBlock operator C=A-B size mismatch/null\n"));
|
---|
[268] | 787 | NDataBlock<T> result; result.SetTemp(true);
|
---|
[245] | 788 | if(b.IsTemp()) {
|
---|
[268] | 789 | result.Share(b);
|
---|
[289] | 790 | T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
|
---|
[245] | 791 | while(p<pe) {*p = *pa++ - *p; p++;}
|
---|
[289] | 792 | } else {result.CloneOrShare(*this); result -= b;}
|
---|
[268] | 793 | return result;
|
---|
[245] | 794 | }
|
---|
| 795 |
|
---|
[944] | 796 | //! Divide by a NDataBlock and return a NDataBlock: ND = NDthis / NDb
|
---|
[245] | 797 | template <class T>
|
---|
[268] | 798 | NDataBlock<T> NDataBlock<T>::Div(const NDataBlock<T>& b) const
|
---|
[257] | 799 | // Pour A/B
|
---|
[245] | 800 | {
|
---|
[285] | 801 | if(mSz!=b.mSz)
|
---|
[265] | 802 | throw(SzMismatchError("NDataBlock operator C=A/B size mismatch/null\n"));
|
---|
[268] | 803 | NDataBlock<T> result; result.SetTemp(true);
|
---|
[245] | 804 | if(b.IsTemp()) {
|
---|
[268] | 805 | result.Share(b);
|
---|
[289] | 806 | T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
|
---|
[976] | 807 | while(p<pe) {*p = *pa++ / *p; p++;} // Division par zero non protegee
|
---|
[289] | 808 | } else {result.CloneOrShare(*this); result /= b;}
|
---|
[268] | 809 | return result;
|
---|
[245] | 810 | }
|
---|
| 811 |
|
---|
[269] | 812 |
|
---|
[275] | 813 | ///////////////////////////////////////////////////////////////
|
---|
[245] | 814 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 815 | #pragma define_template NDataBlock<uint_1>
|
---|
| 816 | #pragma define_template NDataBlock<uint_2>
|
---|
[3661] | 817 | #pragma define_template NDataBlock<int_1>
|
---|
[245] | 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>
|
---|
[802] | 825 | #pragma define_template NDataBlock< complex<r_4> >
|
---|
| 826 | #pragma define_template NDataBlock< complex<r_8> >
|
---|
[3750] | 827 | #ifdef SO_LDBLE128
|
---|
| 828 | #pragma define_template NDataBlock<r_16>
|
---|
| 829 | #pragma define_template NDataBlock< complex<r_16> >
|
---|
[245] | 830 | #endif
|
---|
| 831 |
|
---|
[3750] | 832 | #endif
|
---|
| 833 |
|
---|
[269] | 834 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
[2867] | 835 | namespace SOPHYA {
|
---|
[245] | 836 | template class NDataBlock<uint_1>;
|
---|
| 837 | template class NDataBlock<uint_2>;
|
---|
[3661] | 838 | template class NDataBlock<int_1>;
|
---|
[245] | 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>;
|
---|
[802] | 846 | template class NDataBlock< complex<r_4> >;
|
---|
| 847 | template class NDataBlock< complex<r_8> >;
|
---|
[3750] | 848 | #ifdef SO_LDBLE128
|
---|
| 849 | template class NDataBlock<r_16>;
|
---|
| 850 | template class NDataBlock< complex<r_16> >;
|
---|
| 851 | #endif
|
---|
[2867] | 852 | }
|
---|
[245] | 853 | #endif
|
---|