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