| [268] | 1 | // Gestion de block de donnees avec partage de references
 | 
|---|
| [502] | 2 | // malheureusement tres mal concu...  C.Magneville 04/99
 | 
|---|
| [268] | 3 | // LAL (Orsay) / IN2P3-CNRS  DAPNIA/SPP (Saclay) / CEA
 | 
|---|
| [2615] | 4 | #include "sopnamsp.h"
 | 
|---|
| [245] | 5 | #include "machdefs.h"
 | 
|---|
 | 6 | #include <stdio.h>
 | 
|---|
 | 7 | #include <stdlib.h>
 | 
|---|
| [2322] | 8 | #include <iostream>
 | 
|---|
| [245] | 9 | #include <complex>
 | 
|---|
 | 10 | #include "pexceptions.h"
 | 
|---|
 | 11 | #include "ndatablock.h"
 | 
|---|
 | 12 | 
 | 
|---|
| [2657] | 13 | /* ---- Pour renvoyer un identificateur unique ---- */
 | 
|---|
 | 14 | static uint_8 _ndrefid_ = 0; // Identificateur de NDREF cree 
 | 
|---|
 | 15 | uint_8 AnyDataObj::getUniqueId()
 | 
|---|
 | 16 | {
 | 
|---|
 | 17 |   _ndrefid_++;
 | 
|---|
 | 18 |   return ( _ndrefid_ );
 | 
|---|
 | 19 | }
 | 
|---|
 | 20 | 
 | 
|---|
| [944] | 21 | /*!
 | 
|---|
 | 22 |   \class SOPHYA::NDataBlock
 | 
|---|
| [1607] | 23 |   \ingroup BaseTools
 | 
|---|
| [944] | 24 |   Management of data blocks
 | 
|---|
 | 25 | */
 | 
|---|
| [268] | 26 | 
 | 
|---|
| [944] | 27 | //////////////////////////////////
 | 
|---|
 | 28 | // Fonctionnement en mode debug //
 | 
|---|
 | 29 | //////////////////////////////////
 | 
|---|
| [245] | 30 | 
 | 
|---|
| [944] | 31 | template <class T> int    NDataBlock<T>::Debug_NDataBlock = 0;
 | 
|---|
 | 32 | template <class T> size_t NDataBlock<T>::NallocData       = 0;
 | 
|---|
 | 33 | template <class T> size_t NDataBlock<T>::NallocSRef       = 0;
 | 
|---|
| [245] | 34 | 
 | 
|---|
| [944] | 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 |  */
 | 
|---|
| [245] | 46 | template <class T>
 | 
|---|
| [944] | 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 | 
 | 
|---|
| [2565] | 82 | //! Constructor for \b n datas. if \b zero=true, filled with zeros
 | 
|---|
| [944] | 83 | template <class T>
 | 
|---|
| [2565] | 84 | NDataBlock<T>::NDataBlock(size_t n, bool fzero)
 | 
|---|
| [245] | 85 | // Createur d'une structure de "n" donnees
 | 
|---|
 | 86 | : mSz(0), mSRef(NULL), mIsTemp(false)
 | 
|---|
 | 87 | {
 | 
|---|
| [944] | 88 | if(Debug_NDataBlock>1)
 | 
|---|
 | 89 |   cout<<"?_NDataBlock::NDataBlock("<<this<<",n="<<n<<")"<<endl;
 | 
|---|
| [268] | 90 | 
 | 
|---|
| [2565] | 91 | Alloc(n, NULL, NULL, fzero);   // allocation et mise a zero
 | 
|---|
| [245] | 92 | }
 | 
|---|
 | 93 | 
 | 
|---|
| [944] | 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 |  */
 | 
|---|
| [245] | 100 | template <class T>
 | 
|---|
 | 101 | NDataBlock<T>::NDataBlock(size_t n, T* data, Bridge* br)
 | 
|---|
| [502] | 102 | // Createur d'une structure de "n" donnees, avec donnees preallouees.
 | 
|---|
 | 103 | // Attention createur TRES DANGEREUX (Voir explications dans Alloc()).
 | 
|---|
| [245] | 104 | : mSz(0), mSRef(NULL), mIsTemp(false)
 | 
|---|
 | 105 | {
 | 
|---|
| [944] | 106 | if(Debug_NDataBlock>1)
 | 
|---|
 | 107 |   cout<<"?_NDataBlock::NDataBlock("<<this
 | 
|---|
 | 108 |       <<",data="<<data<<",br="<<br<<")"<<endl;
 | 
|---|
| [268] | 109 | 
 | 
|---|
| [245] | 110 | Alloc(n,data,br);
 | 
|---|
 | 111 | }
 | 
|---|
 | 112 | 
 | 
|---|
| [944] | 113 | //! Default constructor
 | 
|---|
| [245] | 114 | template <class T>
 | 
|---|
 | 115 | NDataBlock<T>::NDataBlock()
 | 
|---|
 | 116 | // Createur par default
 | 
|---|
 | 117 | : mSz(0), mSRef(NULL), mIsTemp(false)
 | 
|---|
 | 118 | {
 | 
|---|
| [944] | 119 | if(Debug_NDataBlock>1)
 | 
|---|
 | 120 |   cout<<"?_NDataBlock::NDataBlock("<<this<<") default"<<endl;
 | 
|---|
| [245] | 121 | }
 | 
|---|
 | 122 | 
 | 
|---|
| [944] | 123 | //! Copy constructor
 | 
|---|
 | 124 | /*!
 | 
|---|
| [976] | 125 |   \warning datas are \b SHARED with \b a.
 | 
|---|
| [944] | 126 |  */
 | 
|---|
| [245] | 127 | template <class T>
 | 
|---|
| [268] | 128 | NDataBlock<T>::NDataBlock(const NDataBlock<T>& a)
 | 
|---|
| [969] | 129 | // Createur par copie: partage les donnees dans tous les cas
 | 
|---|
| [245] | 130 | : mSz(0), mSRef(NULL), mIsTemp(false)
 | 
|---|
 | 131 | {
 | 
|---|
| [944] | 132 | if(Debug_NDataBlock>1)
 | 
|---|
| [1349] | 133 |   cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<" a.mSz="<<a.mSz<<")"<<endl;
 | 
|---|
| [268] | 134 | 
 | 
|---|
| [1349] | 135 | if(a.mSRef && a.mSz>0) Share(a);
 | 
|---|
| [245] | 136 | }
 | 
|---|
 | 137 | 
 | 
|---|
| [944] | 138 | //! Copy constructor with \b share option
 | 
|---|
 | 139 | /*!
 | 
|---|
 | 140 |   \warning datas are shared if \b share is \b true, cloned if not.
 | 
|---|
 | 141 |  */
 | 
|---|
| [245] | 142 | template <class T>
 | 
|---|
| [268] | 143 | NDataBlock<T>::NDataBlock(const NDataBlock<T>& a,bool share)
 | 
|---|
| [289] | 144 | // Createur avec choix de partager ou non selon "share"
 | 
|---|
| [245] | 145 | : mSz(0), mSRef(NULL), mIsTemp(false)
 | 
|---|
 | 146 | {
 | 
|---|
| [944] | 147 | if(Debug_NDataBlock>1)
 | 
|---|
| [946] | 148 |   cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a
 | 
|---|
 | 149 |       <<",sh=<<"<<share<<")"<<endl;
 | 
|---|
| [268] | 150 | 
 | 
|---|
| [1349] | 151 | if(a.mSRef && a.mSz>0) {if(share) Share(a); else Clone(a);}
 | 
|---|
| [245] | 152 | }
 | 
|---|
 | 153 | 
 | 
|---|
| [944] | 154 | //! Destructor
 | 
|---|
| [245] | 155 | template <class T>
 | 
|---|
 | 156 | NDataBlock<T>::~NDataBlock()
 | 
|---|
 | 157 | // Destructeur
 | 
|---|
 | 158 | {
 | 
|---|
| [944] | 159 | if(Debug_NDataBlock>1)
 | 
|---|
 | 160 |   cout<<"?_NDataBlock::~NDataBlock("<<this<<")"<<endl;
 | 
|---|
| [268] | 161 | 
 | 
|---|
| [245] | 162 | Delete();
 | 
|---|
 | 163 | }
 | 
|---|
 | 164 | 
 | 
|---|
| [944] | 165 | ////////////////////////
 | 
|---|
 | 166 | // Gestion de donnees //
 | 
|---|
 | 167 | ////////////////////////
 | 
|---|
| [275] | 168 | 
 | 
|---|
| [944] | 169 | //! Clone datas from \b a
 | 
|---|
| [245] | 170 | template <class T>
 | 
|---|
| [268] | 171 | void NDataBlock<T>::Clone(const NDataBlock<T>& a)
 | 
|---|
| [289] | 172 | // Clone: copie de donnees a partir de "a"
 | 
|---|
| [245] | 173 | {
 | 
|---|
| [944] | 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;
 | 
|---|
| [275] | 178 | 
 | 
|---|
| [289] | 179 | if(&a==NULL) throw(NullPtrError("NDataBlock::Clone  &a==NULL\n"));
 | 
|---|
| [1349] | 180 | if(!a.mSRef || a.mSz==0) throw(NullPtrError("NDataBlock::Clone a.mSz=0\n"));
 | 
|---|
| [773] | 181 | Alloc(a.mSz, NULL, NULL, false);  // pas de mise a zero 
 | 
|---|
| [289] | 182 | memcpy(Data(),a.Data(),mSz*sizeof(T));
 | 
|---|
| [245] | 183 | }
 | 
|---|
 | 184 | 
 | 
|---|
| [944] | 185 | //! Share datas with \b a
 | 
|---|
| [245] | 186 | template <class T>
 | 
|---|
| [944] | 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
 | 
|---|
| [946] | 205 |       <<" mSRef->bridge="<<mSRef->bridge
 | 
|---|
 | 206 |       <<" IsTemp="<<mIsTemp<<endl;
 | 
|---|
| [944] | 207 | }
 | 
|---|
 | 208 | 
 | 
|---|
 | 209 | //! \b Share with \b a if \b temporary, \b clone from \b a if not.
 | 
|---|
| [976] | 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 | */
 | 
|---|
| [944] | 237 | template <class T>
 | 
|---|
| [289] | 238 | void NDataBlock<T>::CloneOrShare(const NDataBlock<T>& a)
 | 
|---|
 | 239 | // CloneOrShare: Share si "a" temporaire, Clone sinon.
 | 
|---|
 | 240 | {
 | 
|---|
| [944] | 241 | if(Debug_NDataBlock>1)
 | 
|---|
 | 242 |   cout<<"?_NDataBlock::CloneOrShare("<<this<<","<<&a<<")"<<endl;
 | 
|---|
| [289] | 243 | 
 | 
|---|
 | 244 | if(&a==NULL) throw(NullPtrError("NDataBlock::CloneOrShare  &a==NULL\n"));
 | 
|---|
 | 245 | if(a.IsTemp()) Share(a); else Clone(a);
 | 
|---|
 | 246 | }
 | 
|---|
 | 247 | 
 | 
|---|
| [944] | 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 |  */
 | 
|---|
| [1426] | 319 | 
 | 
|---|
 | 320 | 
 | 
|---|
| [289] | 321 | template <class T>
 | 
|---|
| [944] | 322 | void NDataBlock<T>::Alloc(size_t n,T* data,Bridge* br,bool zero)
 | 
|---|
| [245] | 323 | {
 | 
|---|
| [944] | 324 | if(Debug_NDataBlock>1)
 | 
|---|
 | 325 |   cout<<"?_NDataBlock::Alloc("<<this<<","
 | 
|---|
 | 326 |       <<n<<","<<data<<","<<br<<") mSz="<<mSz
 | 
|---|
 | 327 |       <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
 | 
|---|
| [268] | 328 | 
 | 
|---|
| [944] | 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"));
 | 
|---|
| [245] | 332 | if(mSRef) Delete();
 | 
|---|
| [944] | 333 | mSz = n;
 | 
|---|
 | 334 | mSRef = new NDREF;
 | 
|---|
 | 335 | mSRef->nref = 1;
 | 
|---|
| [2657] | 336 | mSRef->dsid = AnyDataObj::getUniqueId(); 
 | 
|---|
| [944] | 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;
 | 
|---|
| [268] | 340 | 
 | 
|---|
| [944] | 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
 | 
|---|
| [946] | 349 |         <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<<mSRef->data
 | 
|---|
 | 350 |         <<" mSRef->bridge="<<mSRef->bridge
 | 
|---|
 | 351 |         <<" IsTemp="<<mIsTemp
 | 
|---|
| [944] | 352 |         <<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
 | 
|---|
| [245] | 353 | }
 | 
|---|
| [944] | 354 | }
 | 
|---|
| [245] | 355 | 
 | 
|---|
| [944] | 356 | //! Management of de-allocation
 | 
|---|
| [245] | 357 | template <class T>
 | 
|---|
 | 358 | void NDataBlock<T>::Delete(void)
 | 
|---|
 | 359 | // Pour detruire les pointeurs en tenant compte des references
 | 
|---|
 | 360 | {
 | 
|---|
| [944] | 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 | }
 | 
|---|
| [268] | 369 | 
 | 
|---|
| [289] | 370 | if(mSRef==NULL) return;
 | 
|---|
| [946] | 371 | 
 | 
|---|
| [245] | 372 | mSRef->nref--;
 | 
|---|
 | 373 | if(mSRef->nref != 0) {
 | 
|---|
| [268] | 374 | 
 | 
|---|
| [944] | 375 | if(Debug_NDataBlock>1)
 | 
|---|
| [502] | 376 |   cout<<"...?_NDataBlock::Delete() pas de desallocation il reste nref="
 | 
|---|
 | 377 |       <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
 | 
|---|
| [268] | 378 | 
 | 
|---|
| [285] | 379 |   mSz = 0; mSRef=NULL;
 | 
|---|
| [245] | 380 |   return;
 | 
|---|
 | 381 | }
 | 
|---|
| [268] | 382 | 
 | 
|---|
| [944] | 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 | }
 | 
|---|
| [268] | 389 | 
 | 
|---|
| [245] | 390 | // Si il y a un Bridge les donnees ne n'appartiennent pas, on detruit le Bridge
 | 
|---|
| [285] | 391 | // sinon, les donnees ont ete allouees par nos soins, on libere l'espace
 | 
|---|
| [3113] | 392 | if(mSRef->bridge) {
 | 
|---|
 | 393 |   if(Debug_NDataBlock>1)
 | 
|---|
 | 394 |     cout<<"...?_NDataBlock::Delete() Bridge "<<mSRef->bridge<<" deleted"<<endl;
 | 
|---|
 | 395 |   delete mSRef->bridge;
 | 
|---|
 | 396 | } else {
 | 
|---|
 | 397 |   if(Debug_NDataBlock>1)
 | 
|---|
 | 398 |     cout<<"...?_NDataBlock::Delete() data "<<mSRef->data<<" deleted"<<endl;
 | 
|---|
 | 399 |   delete [] mSRef->data;
 | 
|---|
 | 400 | }
 | 
|---|
| [245] | 401 | mSRef->bridge=NULL; mSRef->data=NULL;
 | 
|---|
| [285] | 402 | delete mSRef; mSRef=NULL; mSz = 0;
 | 
|---|
| [245] | 403 | }
 | 
|---|
 | 404 | 
 | 
|---|
| [944] | 405 | //! Fill dats of this NDataBlock with the \b n datas pointed by \b data
 | 
|---|
 | 406 | /*!
 | 
|---|
 | 407 |   \warning If class empty : allocate space in memory
 | 
|---|
 | 408 |   \warning If class already connected : overwrite with minimum size
 | 
|---|
 | 409 |            (\b n or \b mSz)
 | 
|---|
 | 410 |  */
 | 
|---|
| [245] | 411 | template <class T>
 | 
|---|
| [257] | 412 | void NDataBlock<T>::FillFrom(size_t n,T* data)
 | 
|---|
 | 413 | // Remplissage par un tableau de donnees
 | 
|---|
 | 414 | // - Si classe vide : creation de l'espace memoire
 | 
|---|
| [289] | 415 | // - Si classe connectee : on ecrit selon la longueur minimale
 | 
|---|
 | 416 | //                         (cad this->mSz ou "n")
 | 
|---|
| [257] | 417 | {
 | 
|---|
 | 418 | if(data==NULL) throw(NullPtrError("NDataBlock::FillFrom  data==NULL\n"));
 | 
|---|
 | 419 | if(n==0) throw(ParmError("NDataBlock::FillFrom  n<=0\n"));
 | 
|---|
| [773] | 420 | if(mSRef==NULL) Alloc(n, NULL, NULL, false);  // Pas de mise a zero
 | 
|---|
| [285] | 421 | if(mSz<n) n = mSz;
 | 
|---|
| [257] | 422 | memcpy(Data(),data,n*sizeof(T));
 | 
|---|
 | 423 | }
 | 
|---|
 | 424 | 
 | 
|---|
| [944] | 425 | //! Re-allocate space for \b nnew datas
 | 
|---|
 | 426 | /*!
 | 
|---|
 | 427 |   \param nnnew : new size
 | 
|---|
 | 428 |   \param force : to manage the way re-allocation will be done (see after).
 | 
|---|
 | 429 |   \verbatim
 | 
|---|
 | 430 |   Re-allocation de "nnew" place memoire pour les donnees
 | 
|---|
 | 431 |   avec conservation des "nold" donnees precedentes si possible.
 | 
|---|
 | 432 |   "force" gere la re-allocation de la place memoire pour les donnees.
 | 
|---|
 | 433 |   Divers cas se presentent:
 | 
|---|
 | 434 |   a-/ *** nnew>nold force=quelconque ***
 | 
|---|
 | 435 |       place re-allouee, donnees [0,nold[ copiees, surplus [nold,new[ mis a zero
 | 
|---|
 | 436 |   b-/ *** nnew<=nold force=true ***
 | 
|---|
 | 437 |       place re-allouee, donnees [0,nnew[ copiees, pas de surplus
 | 
|---|
 | 438 |   c-/ *** nnew<=nold force=false ***
 | 
|---|
 | 439 |       place non re-allouee, seule la valeur de la taille est diminuee
 | 
|---|
 | 440 |   - On tient compte du partage des donnees dans tous les cas.
 | 
|---|
 | 441 |   - Si il n'y a pas de donnees connectees a la classe, on re-alloue
 | 
|---|
 | 442 |     dans tous les cas
 | 
|---|
 | 443 |   \endverbatim
 | 
|---|
 | 444 |  */
 | 
|---|
| [502] | 445 | template <class T>
 | 
|---|
 | 446 | void NDataBlock<T>::Realloc(size_t nnew,bool force)
 | 
|---|
 | 447 | {
 | 
|---|
 | 448 | if(nnew==0) throw(ParmError("NDataBlock::Realloc  n<=0\n"));
 | 
|---|
 | 449 | 
 | 
|---|
 | 450 | // Cas sans re-allocation memoire
 | 
|---|
 | 451 | if(mSRef && nnew<=mSz && ! force) { mSz=nnew; return;}
 | 
|---|
 | 452 | 
 | 
|---|
 | 453 | // Cas avec re-allocation memoire
 | 
|---|
 | 454 | size_t ncop;
 | 
|---|
 | 455 | if(!mSRef || mSz==0) ncop=0; else if(mSz<nnew) ncop=mSz; else ncop=nnew;
 | 
|---|
 | 456 | T* dataloc = new T[nnew];
 | 
|---|
 | 457 | if(ncop>0) memcpy(dataloc,mSRef->data,ncop*sizeof(T));
 | 
|---|
 | 458 | if(nnew>ncop) memset(dataloc+ncop,0,(nnew-ncop)*sizeof(T));
 | 
|---|
 | 459 | Alloc(nnew,dataloc,NULL); //Alloc gere partage de reference et bridge
 | 
|---|
 | 460 | }
 | 
|---|
 | 461 | 
 | 
|---|
| [944] | 462 | ////////////////
 | 
|---|
 | 463 | // Impression //
 | 
|---|
 | 464 | ////////////////
 | 
|---|
| [245] | 465 | 
 | 
|---|
| [944] | 466 | //! Give infos and print \b n datas beginning at \b i1 on stream \b os.
 | 
|---|
| [245] | 467 | template <class T>
 | 
|---|
| [268] | 468 | void NDataBlock<T>::Print(ostream& os,size_t i1,size_t n) const
 | 
|---|
| [245] | 469 | // Impression de n elements a partir de i1
 | 
|---|
 | 470 | {
 | 
|---|
 | 471 | size_t nr = 0;
 | 
|---|
| [267] | 472 | T* p = NULL; Bridge* br = NULL;
 | 
|---|
 | 473 | if(mSRef) {nr = mSRef->nref; p = mSRef->data; br = mSRef->bridge;}
 | 
|---|
| [268] | 474 | os<<"NDataBlock::Print("<<this<<",Sz="<<mSz<<",IsTemp="<<mIsTemp<<")\n"
 | 
|---|
 | 475 |   <<"            mSRef="<<mSRef<<"(nref="<<nr<<",data="<<p
 | 
|---|
 | 476 |   <<",bridge="<<br<<")"<<endl;
 | 
|---|
| [249] | 477 | if(i1>=mSz || n<=0 || !p) return;
 | 
|---|
| [245] | 478 | size_t i2 = i1+n; if(i2>mSz) i2=mSz;
 | 
|---|
| [269] | 479 | size_t im = 1; bool enl=false;
 | 
|---|
| [245] | 480 | while(i1<i2) {
 | 
|---|
| [257] | 481 |   enl = false;
 | 
|---|
| [268] | 482 |   os<<" "<<(*this)(i1);  i1++;
 | 
|---|
 | 483 |   if(im==8) {os<<"\n"; im=1; enl=true;} else im++;
 | 
|---|
| [245] | 484 | }
 | 
|---|
| [268] | 485 | if(!enl) os<<endl;
 | 
|---|
| [245] | 486 | }
 | 
|---|
 | 487 | 
 | 
|---|
| [944] | 488 | //////////////////////////////////////////////
 | 
|---|
 | 489 | // Calcul de la somme / produit des donnees //
 | 
|---|
 | 490 | //////////////////////////////////////////////
 | 
|---|
| [289] | 491 | 
 | 
|---|
| [944] | 492 | //! Return sum of \b n datas beginning at data \b i1.
 | 
|---|
| [289] | 493 | template <class T>
 | 
|---|
 | 494 | T NDataBlock<T>::Sum(size_t i1,size_t n) const
 | 
|---|
 | 495 | // Somme des elements de i1 a i1+n-1
 | 
|---|
 | 496 | {
 | 
|---|
 | 497 | if(i1>=mSz) return 0;
 | 
|---|
 | 498 | if(n>mSz) n = mSz; if(n==0) n = mSz-i1;
 | 
|---|
 | 499 | T const *p=Begin()+i1, *pe=p+n;
 | 
|---|
 | 500 | T val = 0;
 | 
|---|
 | 501 | while (p<pe) val += *p++;
 | 
|---|
 | 502 | return val;
 | 
|---|
 | 503 | }
 | 
|---|
 | 504 | 
 | 
|---|
| [944] | 505 | //! Return product of \b n datas beginning at data \b i1.
 | 
|---|
| [289] | 506 | template <class T>
 | 
|---|
 | 507 | T NDataBlock<T>::Product(size_t i1,size_t n) const
 | 
|---|
 | 508 | // Produit des elements de i1 a i1+n-1
 | 
|---|
 | 509 | {
 | 
|---|
 | 510 | if(i1>=mSz) return 0;
 | 
|---|
 | 511 | if(n>mSz) n = mSz; if(n==0) n = mSz-i1;
 | 
|---|
 | 512 | T const *p=Begin()+i1, *pe=p+n;
 | 
|---|
 | 513 | T val = 0;
 | 
|---|
 | 514 | while (p<pe) val *= *p++;
 | 
|---|
 | 515 | return val;
 | 
|---|
 | 516 | }
 | 
|---|
 | 517 | 
 | 
|---|
| [944] | 518 | ///////////////////////////////////////////////////////////////
 | 
|---|
 | 519 | // Surcharge de = : NDataBlock=NDataBlock; NDataBlock=<T> b; //
 | 
|---|
 | 520 | ///////////////////////////////////////////////////////////////
 | 
|---|
| [245] | 521 | 
 | 
|---|
| [976] | 522 | //! Operator = : ND = NDa
 | 
|---|
 | 523 | /*! \warning Datas are copied (cloned) from \b a. */
 | 
|---|
| [245] | 524 | template <class T>
 | 
|---|
| [268] | 525 | NDataBlock<T>& NDataBlock<T>::operator = (const NDataBlock<T>& a)
 | 
|---|
| [289] | 526 | // Affectation: partage des donnees si "a" temporaire, clone sinon.
 | 
|---|
| [245] | 527 | {
 | 
|---|
| [944] | 528 | if(Debug_NDataBlock>1)
 | 
|---|
 | 529 |   cout<<"?_NDataBlock::operator=("<<this<<","<<&a<<") a.(mSz="
 | 
|---|
 | 530 |       <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp()
 | 
|---|
 | 531 |       <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
 | 
|---|
| [268] | 532 | 
 | 
|---|
| [245] | 533 | if(this == &a) return *this;
 | 
|---|
| [289] | 534 | if(a.mSz==0)
 | 
|---|
| [969] | 535 |   throw(SzMismatchError("NDataBlock::operator=A null size \n"));
 | 
|---|
| [976] | 536 | if (mSz==0) { CloneOrShare(a); return *this; }
 | 
|---|
| [969] | 537 | if (a.mSz != mSz)
 | 
|---|
 | 538 |   throw(SzMismatchError("NDataBlock::operator=A Unequal sizes \n"));
 | 
|---|
 | 539 | memcpy(Data(),a.Data(),mSz*sizeof(T));
 | 
|---|
| [245] | 540 | return *this;
 | 
|---|
 | 541 | }
 | 
|---|
 | 542 | 
 | 
|---|
| [944] | 543 | //! Operator = : ND = \b v (at dats set to \b v).
 | 
|---|
| [245] | 544 | template <class T>
 | 
|---|
| [249] | 545 | NDataBlock<T>& NDataBlock<T>::operator = (T v)
 | 
|---|
| [289] | 546 | // Affectation de tous les elements a une constante "v"
 | 
|---|
| [245] | 547 | {
 | 
|---|
| [944] | 548 | if(Debug_NDataBlock>1)
 | 
|---|
 | 549 |   cout<<"?_NDataBlock::operator=("<<this<<","<<v<<")"
 | 
|---|
 | 550 |       <<" mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
 | 
|---|
| [268] | 551 | 
 | 
|---|
| [245] | 552 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator=v null size\n"));
 | 
|---|
| [976] | 553 | T *p=Begin(), *pe=End(); while (p<pe) *p++ = v;
 | 
|---|
| [245] | 554 | return *this;
 | 
|---|
 | 555 | }
 | 
|---|
 | 556 | 
 | 
|---|
| [944] | 557 | //////////////////////////////////////////////////////////////
 | 
|---|
 | 558 | // Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += <T> b; //
 | 
|---|
 | 559 | //////////////////////////////////////////////////////////////
 | 
|---|
| [245] | 560 | 
 | 
|---|
| [944] | 561 | //! Add a constant : ND += b
 | 
|---|
| [245] | 562 | template <class T>
 | 
|---|
 | 563 | NDataBlock<T>& NDataBlock<T>::operator += (T b)
 | 
|---|
 | 564 | {
 | 
|---|
 | 565 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator+=v null size\n"));
 | 
|---|
| [976] | 566 | T *p=Begin(), *pe=End(); while (p<pe) *p++ += b;
 | 
|---|
| [245] | 567 | return *this;
 | 
|---|
 | 568 | }
 | 
|---|
 | 569 | 
 | 
|---|
| [944] | 570 | //! Substract a constant : ND -= b
 | 
|---|
| [245] | 571 | template <class T>
 | 
|---|
 | 572 | NDataBlock<T>& NDataBlock<T>::operator -= (T b)
 | 
|---|
 | 573 | {
 | 
|---|
 | 574 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator-=v null size\n"));
 | 
|---|
| [976] | 575 | T *p=Begin(), *pe=End(); while (p<pe) *p++ -= b;
 | 
|---|
| [245] | 576 | return *this;
 | 
|---|
 | 577 | }
 | 
|---|
 | 578 | 
 | 
|---|
| [944] | 579 | //! Multiply by a constant : ND *= b
 | 
|---|
| [245] | 580 | template <class T>
 | 
|---|
 | 581 | NDataBlock<T>& NDataBlock<T>::operator *= (T b)
 | 
|---|
 | 582 | {
 | 
|---|
 | 583 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator*=v null size\n"));
 | 
|---|
| [976] | 584 | T *p=Begin(), *pe=End(); while (p<pe) *p++ *= b;
 | 
|---|
| [245] | 585 | return *this;
 | 
|---|
 | 586 | }
 | 
|---|
 | 587 | 
 | 
|---|
| [976] | 588 | //! Divide by a constant : ND /= b
 | 
|---|
| [245] | 589 | template <class T>
 | 
|---|
 | 590 | NDataBlock<T>& NDataBlock<T>::operator /= (T b)
 | 
|---|
 | 591 | {
 | 
|---|
| [249] | 592 | if(b==(T) 0) throw(ParmError("NDataBlock::operator/=v divide by zero\n"));
 | 
|---|
| [245] | 593 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator/=v null size\n"));
 | 
|---|
| [976] | 594 | T *p=Begin(), *pe=End(); while (p<pe) *p++ /= b;
 | 
|---|
| [245] | 595 | return *this;
 | 
|---|
 | 596 | }
 | 
|---|
 | 597 | 
 | 
|---|
| [944] | 598 | ////////////////////////////////////////////////////////////////////
 | 
|---|
 | 599 | // Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += NDataBlock1; //
 | 
|---|
 | 600 | ////////////////////////////////////////////////////////////////////
 | 
|---|
| [245] | 601 | 
 | 
|---|
| [976] | 602 | //! Add a NDataBlock : ND += NDa
 | 
|---|
| [245] | 603 | template <class T>
 | 
|---|
| [268] | 604 | NDataBlock<T>& NDataBlock<T>::operator += (const NDataBlock<T>& a)
 | 
|---|
| [245] | 605 | {
 | 
|---|
| [265] | 606 | if(mSz==0 || mSz!=a.mSz)
 | 
|---|
 | 607 |   throw(SzMismatchError("NDataBlock::operator+=A size mismatch/null"));
 | 
|---|
| [268] | 608 | T *p=Begin(), *pe=End();
 | 
|---|
 | 609 | T const * pa=a.Begin();
 | 
|---|
| [289] | 610 | while (p<pe) *p++ += *pa++;
 | 
|---|
| [245] | 611 | return *this;
 | 
|---|
 | 612 | }
 | 
|---|
 | 613 | 
 | 
|---|
| [976] | 614 | //! Substract a NDataBlock : ND -= NDa
 | 
|---|
| [245] | 615 | template <class T>
 | 
|---|
| [268] | 616 | NDataBlock<T>& NDataBlock<T>::operator -= (const NDataBlock<T>& a)
 | 
|---|
| [245] | 617 | {
 | 
|---|
| [265] | 618 | if(mSz==0 || mSz!=a.mSz)
 | 
|---|
 | 619 |   throw(SzMismatchError("NDataBlock::operator-=A size mismatch/null"));
 | 
|---|
| [268] | 620 | T *p=Begin(), *pe=End();
 | 
|---|
 | 621 | T const *pa=a.Begin();
 | 
|---|
| [289] | 622 | while (p<pe) *p++ -= *pa++;
 | 
|---|
| [245] | 623 | return *this;
 | 
|---|
 | 624 | }
 | 
|---|
 | 625 | 
 | 
|---|
| [976] | 626 | //! Multiply by a NDataBlock : ND *= NDa
 | 
|---|
| [245] | 627 | template <class T>
 | 
|---|
| [268] | 628 | NDataBlock<T>& NDataBlock<T>::operator *= (const NDataBlock<T>& a)
 | 
|---|
| [245] | 629 | {
 | 
|---|
| [265] | 630 | if(mSz==0 || mSz!=a.mSz)
 | 
|---|
 | 631 |   throw(SzMismatchError("NDataBlock::operator*=A size mismatch/null"));
 | 
|---|
| [268] | 632 | T *p=Begin(), *pe=End();
 | 
|---|
 | 633 | T const *pa=a.Begin();
 | 
|---|
| [289] | 634 | while (p<pe) *p++ *= *pa++;
 | 
|---|
| [245] | 635 | return *this;
 | 
|---|
 | 636 | }
 | 
|---|
 | 637 | 
 | 
|---|
| [976] | 638 | //! Divide by a NDataBlock : ND /= NDa
 | 
|---|
| [245] | 639 | template <class T>
 | 
|---|
| [268] | 640 | NDataBlock<T>& NDataBlock<T>::operator /= (const NDataBlock<T>& a)
 | 
|---|
| [289] | 641 | // Attention, aucune protection si un element de "a" est nul.
 | 
|---|
| [245] | 642 | {
 | 
|---|
| [265] | 643 | if(mSz==0 || mSz!=a.mSz)
 | 
|---|
 | 644 |   throw(SzMismatchError("NDataBlock::operator/=A size mismatch/null"));
 | 
|---|
| [268] | 645 | T *p=Begin(), *pe=End();
 | 
|---|
 | 646 | T const *pa=a.Begin();
 | 
|---|
| [976] | 647 | while (p<pe) *p++ /= *pa++; // Division par zero non protegee
 | 
|---|
| [245] | 648 | return *this;
 | 
|---|
 | 649 | }
 | 
|---|
 | 650 | 
 | 
|---|
| [976] | 651 | //////////////////////////////////////////////////////////////////
 | 
|---|
 | 652 | // Pour surcharge de +,-,*,/ : NDataBlock = NDataBlock1+<T>b;   //
 | 
|---|
 | 653 | //                             NDataBlock = <T>b+NDataBlock1;   //
 | 
|---|
 | 654 | // Pour la notion de "temporaire" voir blabla dans CloneOrShare //
 | 
|---|
 | 655 | //////////////////////////////////////////////////////////////////
 | 
|---|
| [245] | 656 | 
 | 
|---|
| [944] | 657 | //! Add a constant and return NDataBlock : NDret = ND + b
 | 
|---|
| [245] | 658 | template <class T>
 | 
|---|
| [268] | 659 | NDataBlock<T> NDataBlock<T>::Add(T b) const
 | 
|---|
| [257] | 660 | // Pour A+b
 | 
|---|
| [245] | 661 | {
 | 
|---|
| [969] | 662 | NDataBlock<T> result; 
 | 
|---|
 | 663 | result.CloneOrShare(*this); result.SetTemp(true);
 | 
|---|
| [268] | 664 | result += b;
 | 
|---|
 | 665 | return result;
 | 
|---|
| [245] | 666 | }
 | 
|---|
 | 667 | 
 | 
|---|
| [976] | 668 | //! Substract a constant and return NDataBlock : NDret = ND - b or NDret = b - ND
 | 
|---|
 | 669 | /*! Substract a constant or from a constant
 | 
|---|
 | 670 |    \param fginv==false : performs NDret = ND - b (default)
 | 
|---|
 | 671 |    \param fginv==true : performs NDret = b - ND
 | 
|---|
 | 672 | */
 | 
|---|
| [245] | 673 | template <class T>
 | 
|---|
| [976] | 674 | NDataBlock<T> NDataBlock<T>::Sub(T b,bool fginv) const
 | 
|---|
 | 675 | // Pour A-b sauf si fginv==true b-A (- n'est pas commutatif!)
 | 
|---|
| [245] | 676 | {
 | 
|---|
| [969] | 677 | NDataBlock<T> result; 
 | 
|---|
 | 678 | result.CloneOrShare(*this); result.SetTemp(true);
 | 
|---|
| [976] | 679 | if(fginv) {
 | 
|---|
 | 680 |   T *p=result.Begin(), *pe=result.End();
 | 
|---|
 | 681 |   T const *pa=this->Begin();
 | 
|---|
 | 682 |   while(p<pe) {*p++ = b - *pa++;}
 | 
|---|
 | 683 | } else result -= b;
 | 
|---|
| [245] | 684 | return result;
 | 
|---|
 | 685 | }
 | 
|---|
 | 686 | 
 | 
|---|
| [944] | 687 | //! Multiply by a constant and return NDataBlock : NDret = ND * b
 | 
|---|
| [245] | 688 | template <class T>
 | 
|---|
| [268] | 689 | NDataBlock<T> NDataBlock<T>::Mul(T b) const
 | 
|---|
| [257] | 690 | // Pour A*b
 | 
|---|
| [245] | 691 | {
 | 
|---|
| [969] | 692 | NDataBlock<T> result; 
 | 
|---|
 | 693 | result.CloneOrShare(*this); result.SetTemp(true);
 | 
|---|
| [976] | 694 | result *= b;
 | 
|---|
 | 695 | return result;
 | 
|---|
| [245] | 696 | }
 | 
|---|
 | 697 | 
 | 
|---|
| [976] | 698 | //! Divide by a constant and return NDataBlock : NDret = ND / b or NDret = b / ND
 | 
|---|
 | 699 | /*! Divide by 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>::Div(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++;} // Division par zero non protegee
 | 
|---|
 | 713 | } else {
 | 
|---|
 | 714 |   if( b == (T) 0 ) throw MathExc("NDataBlock<T>::Div(T)  - Divide by zero ! ");
 | 
|---|
 | 715 |   result /= b;
 | 
|---|
| [245] | 716 | }
 | 
|---|
 | 717 | return result;
 | 
|---|
 | 718 | }
 | 
|---|
 | 719 | 
 | 
|---|
| [944] | 720 | ///////////////////////////////////////////////////////////////////////
 | 
|---|
 | 721 | // Pour surcharge de +,-,*,/ : NDataBlock = NDataBlock1+NDataBlock2; //
 | 
|---|
 | 722 | ///////////////////////////////////////////////////////////////////////
 | 
|---|
| [245] | 723 | 
 | 
|---|
| [944] | 724 | //! Add a NDataBlock and return a NDataBlock: ND = NDthis + NDb
 | 
|---|
| [245] | 725 | template <class T>
 | 
|---|
| [268] | 726 | NDataBlock<T> NDataBlock<T>::Add(const NDataBlock<T>& b) const
 | 
|---|
| [257] | 727 | // Pour A+B
 | 
|---|
| [245] | 728 | {
 | 
|---|
| [285] | 729 | if(mSz!=b.mSz)
 | 
|---|
| [265] | 730 |   throw(SzMismatchError("NDataBlock operator C=A+B size mismatch/null\n"));
 | 
|---|
| [268] | 731 | NDataBlock<T> result; result.SetTemp(true);
 | 
|---|
| [289] | 732 | if(b.IsTemp()) {result.Share(b);            result += *this;}
 | 
|---|
 | 733 |   else         {result.CloneOrShare(*this); result += b;}
 | 
|---|
| [268] | 734 | return result;
 | 
|---|
| [245] | 735 | }
 | 
|---|
 | 736 | 
 | 
|---|
| [944] | 737 | //! Multiply by a NDataBlock and return a NDataBlock: ND = NDthis * NDb
 | 
|---|
| [245] | 738 | template <class T>
 | 
|---|
| [268] | 739 | NDataBlock<T> NDataBlock<T>::Mul(const NDataBlock<T>& b) const
 | 
|---|
| [257] | 740 | // Pour A*B
 | 
|---|
| [245] | 741 | {
 | 
|---|
| [285] | 742 | if(mSz!=b.mSz)
 | 
|---|
| [265] | 743 |   throw(SzMismatchError("NDataBlock operator C=A*B size mismatch/null\n"));
 | 
|---|
| [268] | 744 | NDataBlock<T> result; result.SetTemp(true);
 | 
|---|
| [289] | 745 | if(b.IsTemp()) {result.Share(b);            result *= *this;}
 | 
|---|
 | 746 |   else         {result.CloneOrShare(*this); result *= b;}
 | 
|---|
| [268] | 747 | return result;
 | 
|---|
| [245] | 748 | }
 | 
|---|
 | 749 | 
 | 
|---|
| [944] | 750 | //! Substract a NDataBlock and return a NDataBlock: ND = NDthis - NDb
 | 
|---|
| [245] | 751 | template <class T>
 | 
|---|
| [268] | 752 | NDataBlock<T> NDataBlock<T>::Sub(const NDataBlock<T>& b) const
 | 
|---|
| [257] | 753 | // Pour A-B
 | 
|---|
| [245] | 754 | {
 | 
|---|
| [285] | 755 | if(mSz!=b.mSz)
 | 
|---|
| [265] | 756 |   throw(SzMismatchError("NDataBlock operator C=A-B size mismatch/null\n"));
 | 
|---|
| [268] | 757 | NDataBlock<T> result; result.SetTemp(true);
 | 
|---|
| [245] | 758 | if(b.IsTemp()) {
 | 
|---|
| [268] | 759 |   result.Share(b);
 | 
|---|
| [289] | 760 |   T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
 | 
|---|
| [245] | 761 |   while(p<pe) {*p = *pa++  - *p; p++;}
 | 
|---|
| [289] | 762 | } else {result.CloneOrShare(*this); result -= b;}
 | 
|---|
| [268] | 763 | return result;
 | 
|---|
| [245] | 764 | }
 | 
|---|
 | 765 | 
 | 
|---|
| [944] | 766 | //! Divide by a NDataBlock and return a NDataBlock: ND = NDthis / NDb
 | 
|---|
| [245] | 767 | template <class T>
 | 
|---|
| [268] | 768 | NDataBlock<T> NDataBlock<T>::Div(const NDataBlock<T>& b) const
 | 
|---|
| [257] | 769 | // Pour A/B
 | 
|---|
| [245] | 770 | {
 | 
|---|
| [285] | 771 | if(mSz!=b.mSz)
 | 
|---|
| [265] | 772 |   throw(SzMismatchError("NDataBlock operator C=A/B size mismatch/null\n"));
 | 
|---|
| [268] | 773 | NDataBlock<T> result; result.SetTemp(true);
 | 
|---|
| [245] | 774 | if(b.IsTemp()) {
 | 
|---|
| [268] | 775 |   result.Share(b);
 | 
|---|
| [289] | 776 |   T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
 | 
|---|
| [976] | 777 |   while(p<pe) {*p = *pa++  / *p; p++;} // Division par zero non protegee
 | 
|---|
| [289] | 778 | } else {result.CloneOrShare(*this); result /= b;}
 | 
|---|
| [268] | 779 | return result;
 | 
|---|
| [245] | 780 | }
 | 
|---|
 | 781 | 
 | 
|---|
| [269] | 782 | 
 | 
|---|
| [275] | 783 | ///////////////////////////////////////////////////////////////
 | 
|---|
| [245] | 784 | #ifdef __CXX_PRAGMA_TEMPLATES__
 | 
|---|
 | 785 | #pragma define_template NDataBlock<uint_1>
 | 
|---|
 | 786 | #pragma define_template NDataBlock<uint_2>
 | 
|---|
 | 787 | #pragma define_template NDataBlock<int_2>
 | 
|---|
 | 788 | #pragma define_template NDataBlock<int_4>
 | 
|---|
 | 789 | #pragma define_template NDataBlock<int_8>
 | 
|---|
 | 790 | #pragma define_template NDataBlock<uint_4>
 | 
|---|
 | 791 | #pragma define_template NDataBlock<uint_8>
 | 
|---|
 | 792 | #pragma define_template NDataBlock<r_4>
 | 
|---|
 | 793 | #pragma define_template NDataBlock<r_8>
 | 
|---|
| [802] | 794 | #pragma define_template NDataBlock< complex<r_4> >
 | 
|---|
 | 795 | #pragma define_template NDataBlock< complex<r_8> >
 | 
|---|
| [245] | 796 | #endif
 | 
|---|
 | 797 | 
 | 
|---|
| [269] | 798 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
 | 
|---|
| [2867] | 799 | namespace SOPHYA {
 | 
|---|
| [245] | 800 | template class NDataBlock<uint_1>;
 | 
|---|
 | 801 | template class NDataBlock<uint_2>;
 | 
|---|
 | 802 | template class NDataBlock<int_2>;
 | 
|---|
 | 803 | template class NDataBlock<int_4>;
 | 
|---|
 | 804 | template class NDataBlock<int_8>;
 | 
|---|
 | 805 | template class NDataBlock<uint_4>;
 | 
|---|
 | 806 | template class NDataBlock<uint_8>;
 | 
|---|
 | 807 | template class NDataBlock<r_4>;
 | 
|---|
 | 808 | template class NDataBlock<r_8>;
 | 
|---|
| [802] | 809 | template class NDataBlock< complex<r_4> >;
 | 
|---|
 | 810 | template class NDataBlock< complex<r_8> >;
 | 
|---|
| [2867] | 811 | }
 | 
|---|
| [245] | 812 | #endif
 | 
|---|