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