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