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