| 1 | // Gestion de block de donnees avec partage de references
 | 
|---|
| 2 | // malheureusement tres mal concu...  C.Magneville 04/99
 | 
|---|
| 3 | // LAL (Orsay) / IN2P3-CNRS  DAPNIA/SPP (Saclay) / CEA
 | 
|---|
| 4 | #include "sopnamsp.h"
 | 
|---|
| 5 | #include "machdefs.h"
 | 
|---|
| 6 | #include <stdio.h>
 | 
|---|
| 7 | #include <stdlib.h>
 | 
|---|
| 8 | #include <iostream>
 | 
|---|
| 9 | #include <string.h>
 | 
|---|
| 10 | #include <complex>
 | 
|---|
| 11 | #include "pexceptions.h"
 | 
|---|
| 12 | #include "ndatablock.h"
 | 
|---|
| 13 | #include "thsafeop.h"  //  for ThreadSafe operations (Ref.Count/Share) 
 | 
|---|
| 14 | 
 | 
|---|
| 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 | 
 | 
|---|
| 23 | /*!
 | 
|---|
| 24 |   \class SOPHYA::NDataBlock
 | 
|---|
| 25 |   \ingroup BaseTools
 | 
|---|
| 26 |   Management of data blocks
 | 
|---|
| 27 | */
 | 
|---|
| 28 | 
 | 
|---|
| 29 | //////////////////////////////////
 | 
|---|
| 30 | // Fonctionnement en mode debug //
 | 
|---|
| 31 | //////////////////////////////////
 | 
|---|
| 32 | 
 | 
|---|
| 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;
 | 
|---|
| 36 | template <class T> ThSafeOp* NDataBlock<T>::gThsop        = NULL;
 | 
|---|
| 37 | 
 | 
|---|
| 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 |  */
 | 
|---|
| 49 | template <class T>
 | 
|---|
| 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 | 
 | 
|---|
| 85 | //! Constructor for \b n datas. if \b zero=true, filled with zeros
 | 
|---|
| 86 | template <class T>
 | 
|---|
| 87 | NDataBlock<T>::NDataBlock(size_t n, bool fzero)
 | 
|---|
| 88 | // Createur d'une structure de "n" donnees
 | 
|---|
| 89 | : mSz(0), mSRef(NULL), mIsTemp(false)
 | 
|---|
| 90 | {
 | 
|---|
| 91 | if(Debug_NDataBlock>1)
 | 
|---|
| 92 |   cout<<"?_NDataBlock::NDataBlock("<<this<<",n="<<n<<")"<<endl;
 | 
|---|
| 93 | if (gThsop == NULL) gThsop = new ThSafeOp;
 | 
|---|
| 94 | 
 | 
|---|
| 95 | Alloc(n, NULL, NULL, fzero);   // allocation et mise a zero
 | 
|---|
| 96 | }
 | 
|---|
| 97 | 
 | 
|---|
| 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 |  */
 | 
|---|
| 104 | template <class T>
 | 
|---|
| 105 | NDataBlock<T>::NDataBlock(size_t n, T* data, Bridge* br)
 | 
|---|
| 106 | // Createur d'une structure de "n" donnees, avec donnees preallouees.
 | 
|---|
| 107 | // Attention createur TRES DANGEREUX (Voir explications dans Alloc()).
 | 
|---|
| 108 | : mSz(0), mSRef(NULL), mIsTemp(false)
 | 
|---|
| 109 | {
 | 
|---|
| 110 | if(Debug_NDataBlock>1)
 | 
|---|
| 111 |   cout<<"?_NDataBlock::NDataBlock("<<this
 | 
|---|
| 112 |       <<",data="<<data<<",br="<<br<<")"<<endl;
 | 
|---|
| 113 | if (gThsop == NULL) gThsop = new ThSafeOp;
 | 
|---|
| 114 | 
 | 
|---|
| 115 | Alloc(n,data,br);
 | 
|---|
| 116 | }
 | 
|---|
| 117 | 
 | 
|---|
| 118 | //! Default constructor
 | 
|---|
| 119 | template <class T>
 | 
|---|
| 120 | NDataBlock<T>::NDataBlock()
 | 
|---|
| 121 | // Createur par default
 | 
|---|
| 122 | : mSz(0), mSRef(NULL), mIsTemp(false)
 | 
|---|
| 123 | {
 | 
|---|
| 124 | if(Debug_NDataBlock>1)
 | 
|---|
| 125 |   cout<<"?_NDataBlock::NDataBlock("<<this<<") default"<<endl;
 | 
|---|
| 126 | if (gThsop == NULL) gThsop = new ThSafeOp;
 | 
|---|
| 127 | }
 | 
|---|
| 128 | 
 | 
|---|
| 129 | //! Copy constructor
 | 
|---|
| 130 | /*!
 | 
|---|
| 131 |   \warning datas are \b SHARED with \b a.
 | 
|---|
| 132 |  */
 | 
|---|
| 133 | template <class T>
 | 
|---|
| 134 | NDataBlock<T>::NDataBlock(const NDataBlock<T>& a)
 | 
|---|
| 135 | // Createur par copie: partage les donnees dans tous les cas
 | 
|---|
| 136 | : mSz(0), mSRef(NULL), mIsTemp(false)
 | 
|---|
| 137 | {
 | 
|---|
| 138 | if(Debug_NDataBlock>1)
 | 
|---|
| 139 |   cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<" a.mSz="<<a.mSz<<")"<<endl;
 | 
|---|
| 140 | 
 | 
|---|
| 141 | if(a.mSRef && a.mSz>0) Share(a);
 | 
|---|
| 142 | }
 | 
|---|
| 143 | 
 | 
|---|
| 144 | //! Copy constructor with \b share option
 | 
|---|
| 145 | /*!
 | 
|---|
| 146 |   \warning datas are shared if \b share is \b true, cloned if not.
 | 
|---|
| 147 |  */
 | 
|---|
| 148 | template <class T>
 | 
|---|
| 149 | NDataBlock<T>::NDataBlock(const NDataBlock<T>& a,bool share)
 | 
|---|
| 150 | // Createur avec choix de partager ou non selon "share"
 | 
|---|
| 151 | : mSz(0), mSRef(NULL), mIsTemp(false)
 | 
|---|
| 152 | {
 | 
|---|
| 153 | if(Debug_NDataBlock>1)
 | 
|---|
| 154 |   cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a
 | 
|---|
| 155 |       <<",sh=<<"<<share<<")"<<endl;
 | 
|---|
| 156 | 
 | 
|---|
| 157 | if(a.mSRef && a.mSz>0) {if(share) Share(a); else Clone(a);}
 | 
|---|
| 158 | }
 | 
|---|
| 159 | 
 | 
|---|
| 160 | //! Destructor
 | 
|---|
| 161 | template <class T>
 | 
|---|
| 162 | NDataBlock<T>::~NDataBlock()
 | 
|---|
| 163 | // Destructeur
 | 
|---|
| 164 | {
 | 
|---|
| 165 | if(Debug_NDataBlock>1)
 | 
|---|
| 166 |   cout<<"?_NDataBlock::~NDataBlock("<<this<<")"<<endl;
 | 
|---|
| 167 | 
 | 
|---|
| 168 | Dealloc();  // ThreadSafe version of Delete()
 | 
|---|
| 169 | }
 | 
|---|
| 170 | 
 | 
|---|
| 171 | ////////////////////////
 | 
|---|
| 172 | // Gestion de donnees //
 | 
|---|
| 173 | ////////////////////////
 | 
|---|
| 174 | 
 | 
|---|
| 175 | //! Clone datas from \b a
 | 
|---|
| 176 | template <class T>
 | 
|---|
| 177 | void NDataBlock<T>::Clone(const NDataBlock<T>& a)
 | 
|---|
| 178 | // Clone: copie de donnees a partir de "a"
 | 
|---|
| 179 | {
 | 
|---|
| 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;
 | 
|---|
| 184 | 
 | 
|---|
| 185 | if(&a==NULL) throw(NullPtrError("NDataBlock::Clone  &a==NULL\n"));
 | 
|---|
| 186 | if(!a.mSRef || a.mSz==0) throw(NullPtrError("NDataBlock::Clone a.mSz=0\n"));
 | 
|---|
| 187 | Alloc(a.mSz, NULL, NULL, false);  // pas de mise a zero 
 | 
|---|
| 188 | memcpy(Data(),a.Data(),mSz*sizeof(T));
 | 
|---|
| 189 | }
 | 
|---|
| 190 | 
 | 
|---|
| 191 | //! Share datas with \b a
 | 
|---|
| 192 | template <class T>
 | 
|---|
| 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"));
 | 
|---|
| 205 | //--- Start of atomic (in one block) operation for thread safety 
 | 
|---|
| 206 | gThsop->lock();   //  (ThreadSafe)
 | 
|---|
| 207 | if(mSRef) Delete();
 | 
|---|
| 208 | mSz = a.mSz; mSRef = a.mSRef; mSRef->nref++;
 | 
|---|
| 209 | gThsop->unlock();   //  (ThreadSafe)
 | 
|---|
| 210 | //--- End of atomic operation 
 | 
|---|
| 211 | 
 | 
|---|
| 212 | if(Debug_NDataBlock>1)
 | 
|---|
| 213 |   cout<<"...?_NDataBlock::Share mSz="<<mSz<<" mSRef="<<mSRef
 | 
|---|
| 214 |       <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<< mSRef->data
 | 
|---|
| 215 |       <<" mSRef->bridge="<<mSRef->bridge
 | 
|---|
| 216 |       <<" IsTemp="<<mIsTemp<<endl;
 | 
|---|
| 217 | }
 | 
|---|
| 218 | 
 | 
|---|
| 219 | //! \b Share with \b a if \b temporary, \b clone from \b a if not.
 | 
|---|
| 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 | */
 | 
|---|
| 247 | template <class T>
 | 
|---|
| 248 | void NDataBlock<T>::CloneOrShare(const NDataBlock<T>& a)
 | 
|---|
| 249 | // CloneOrShare: Share si "a" temporaire, Clone sinon.
 | 
|---|
| 250 | {
 | 
|---|
| 251 | if(Debug_NDataBlock>1)
 | 
|---|
| 252 |   cout<<"?_NDataBlock::CloneOrShare("<<this<<","<<&a<<")"<<endl;
 | 
|---|
| 253 | 
 | 
|---|
| 254 | if(&a==NULL) throw(NullPtrError("NDataBlock::CloneOrShare  &a==NULL\n"));
 | 
|---|
| 255 | if(a.IsTemp()) Share(a); else Clone(a);
 | 
|---|
| 256 | }
 | 
|---|
| 257 | 
 | 
|---|
| 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 |  */
 | 
|---|
| 329 | 
 | 
|---|
| 330 | 
 | 
|---|
| 331 | template <class T>
 | 
|---|
| 332 | void NDataBlock<T>::Alloc(size_t n,T* data,Bridge* br,bool zero)
 | 
|---|
| 333 | {
 | 
|---|
| 334 | if(Debug_NDataBlock>1)
 | 
|---|
| 335 |   cout<<"?_NDataBlock::Alloc("<<this<<","
 | 
|---|
| 336 |       <<n<<","<<data<<","<<br<<") mSz="<<mSz
 | 
|---|
| 337 |       <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
 | 
|---|
| 338 | 
 | 
|---|
| 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"));
 | 
|---|
| 342 | //--- Start of atomic (in one block) operation for thread safety (ThreadSafe)
 | 
|---|
| 343 | gThsop->lock();   //  (ThreadSafe)
 | 
|---|
| 344 | if(mSRef) Delete();
 | 
|---|
| 345 | mSz = n;
 | 
|---|
| 346 | mSRef = new NDREF;
 | 
|---|
| 347 | mSRef->nref = 1;
 | 
|---|
| 348 | mSRef->dsid = AnyDataObj::getUniqueId(); 
 | 
|---|
| 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;
 | 
|---|
| 352 | gThsop->unlock();   //  (ThreadSafe)
 | 
|---|
| 353 | //--- End of atomic operation (ThreadSafe)
 | 
|---|
| 354 | 
 | 
|---|
| 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
 | 
|---|
| 363 |         <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<<mSRef->data
 | 
|---|
| 364 |         <<" mSRef->bridge="<<mSRef->bridge
 | 
|---|
| 365 |         <<" IsTemp="<<mIsTemp
 | 
|---|
| 366 |         <<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
 | 
|---|
| 367 | }
 | 
|---|
| 368 | }
 | 
|---|
| 369 | 
 | 
|---|
| 370 | //! Management of de-allocation (NOT thread-safe)
 | 
|---|
| 371 | template <class T>
 | 
|---|
| 372 | void NDataBlock<T>::Delete(void)
 | 
|---|
| 373 | // Pour detruire les pointeurs en tenant compte des references
 | 
|---|
| 374 | {
 | 
|---|
| 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 | }
 | 
|---|
| 383 | 
 | 
|---|
| 384 | if(mSRef==NULL) return;
 | 
|---|
| 385 | 
 | 
|---|
| 386 | mSRef->nref--;
 | 
|---|
| 387 | if(mSRef->nref != 0) {
 | 
|---|
| 388 | 
 | 
|---|
| 389 | if(Debug_NDataBlock>1)
 | 
|---|
| 390 |   cout<<"...?_NDataBlock::Delete() pas de desallocation il reste nref="
 | 
|---|
| 391 |       <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
 | 
|---|
| 392 | 
 | 
|---|
| 393 |   mSz = 0; mSRef=NULL;
 | 
|---|
| 394 |   return;
 | 
|---|
| 395 | }
 | 
|---|
| 396 | 
 | 
|---|
| 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 | }
 | 
|---|
| 403 | 
 | 
|---|
| 404 | // Si il y a un Bridge les donnees ne n'appartiennent pas, on detruit le Bridge
 | 
|---|
| 405 | // sinon, les donnees ont ete allouees par nos soins, on libere l'espace
 | 
|---|
| 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 | }
 | 
|---|
| 415 | mSRef->bridge=NULL; mSRef->data=NULL;
 | 
|---|
| 416 | delete mSRef; mSRef=NULL; mSz = 0;
 | 
|---|
| 417 | }
 | 
|---|
| 418 | 
 | 
|---|
| 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 |  */
 | 
|---|
| 425 | template <class T>
 | 
|---|
| 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
 | 
|---|
| 429 | // - Si classe connectee : on ecrit selon la longueur minimale
 | 
|---|
| 430 | //                         (cad this->mSz ou "n")
 | 
|---|
| 431 | {
 | 
|---|
| 432 | if(data==NULL) throw(NullPtrError("NDataBlock::FillFrom  data==NULL\n"));
 | 
|---|
| 433 | if(n==0) throw(ParmError("NDataBlock::FillFrom  n<=0\n"));
 | 
|---|
| 434 | if(mSRef==NULL) Alloc(n, NULL, NULL, false);  // Pas de mise a zero
 | 
|---|
| 435 | if(mSz<n) n = mSz;
 | 
|---|
| 436 | memcpy(Data(),data,n*sizeof(T));
 | 
|---|
| 437 | }
 | 
|---|
| 438 | 
 | 
|---|
| 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 |  */
 | 
|---|
| 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 | 
 | 
|---|
| 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 | 
 | 
|---|
| 489 | ////////////////
 | 
|---|
| 490 | // Impression //
 | 
|---|
| 491 | ////////////////
 | 
|---|
| 492 | 
 | 
|---|
| 493 | //! Give infos and print \b n datas beginning at \b i1 on stream \b os.
 | 
|---|
| 494 | template <class T>
 | 
|---|
| 495 | void NDataBlock<T>::Print(ostream& os,size_t i1,size_t n) const
 | 
|---|
| 496 | // Impression de n elements a partir de i1
 | 
|---|
| 497 | {
 | 
|---|
| 498 | size_t nr = 0;
 | 
|---|
| 499 | T* p = NULL; Bridge* br = NULL;
 | 
|---|
| 500 | if(mSRef) {nr = mSRef->nref; p = mSRef->data; br = mSRef->bridge;}
 | 
|---|
| 501 | os<<"NDataBlock::Print("<<this<<",Sz="<<mSz<<",IsTemp="<<mIsTemp<<")\n"
 | 
|---|
| 502 |   <<"            mSRef="<<mSRef<<"(nref="<<nr<<",data="<<p
 | 
|---|
| 503 |   <<",bridge="<<br<<")"<<endl;
 | 
|---|
| 504 | if(i1>=mSz || n<=0 || !p) return;
 | 
|---|
| 505 | size_t i2 = i1+n; if(i2>mSz) i2=mSz;
 | 
|---|
| 506 | size_t im = 1; bool enl=false;
 | 
|---|
| 507 | while(i1<i2) {
 | 
|---|
| 508 |   enl = false;
 | 
|---|
| 509 |   os<<" "<<(*this)(i1);  i1++;
 | 
|---|
| 510 |   if(im==8) {os<<"\n"; im=1; enl=true;} else im++;
 | 
|---|
| 511 | }
 | 
|---|
| 512 | if(!enl) os<<endl;
 | 
|---|
| 513 | }
 | 
|---|
| 514 | 
 | 
|---|
| 515 | //////////////////////////////////////////////
 | 
|---|
| 516 | // Calcul de la somme / produit des donnees //
 | 
|---|
| 517 | //////////////////////////////////////////////
 | 
|---|
| 518 | 
 | 
|---|
| 519 | //! Return sum of \b n datas beginning at data \b i1.
 | 
|---|
| 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 | 
 | 
|---|
| 532 | //! Return product of \b n datas beginning at data \b i1.
 | 
|---|
| 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 | 
 | 
|---|
| 545 | ///////////////////////////////////////////////////////////////
 | 
|---|
| 546 | // Surcharge de = : NDataBlock=NDataBlock; NDataBlock=<T> b; //
 | 
|---|
| 547 | ///////////////////////////////////////////////////////////////
 | 
|---|
| 548 | 
 | 
|---|
| 549 | //! Operator = : ND = NDa
 | 
|---|
| 550 | /*! \warning Datas are copied (cloned) from \b a. */
 | 
|---|
| 551 | template <class T>
 | 
|---|
| 552 | NDataBlock<T>& NDataBlock<T>::operator = (const NDataBlock<T>& a)
 | 
|---|
| 553 | // Affectation: partage des donnees si "a" temporaire, clone sinon.
 | 
|---|
| 554 | {
 | 
|---|
| 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;
 | 
|---|
| 559 | 
 | 
|---|
| 560 | if(this == &a) return *this;
 | 
|---|
| 561 | if(a.mSz==0)
 | 
|---|
| 562 |   throw(SzMismatchError("NDataBlock::operator=A null size \n"));
 | 
|---|
| 563 | if (mSz==0) { CloneOrShare(a); return *this; }
 | 
|---|
| 564 | if (a.mSz != mSz)
 | 
|---|
| 565 |   throw(SzMismatchError("NDataBlock::operator=A Unequal sizes \n"));
 | 
|---|
| 566 | memcpy(Data(),a.Data(),mSz*sizeof(T));
 | 
|---|
| 567 | return *this;
 | 
|---|
| 568 | }
 | 
|---|
| 569 | 
 | 
|---|
| 570 | //! Operator = : ND = \b v (at dats set to \b v).
 | 
|---|
| 571 | template <class T>
 | 
|---|
| 572 | NDataBlock<T>& NDataBlock<T>::operator = (T v)
 | 
|---|
| 573 | // Affectation de tous les elements a une constante "v"
 | 
|---|
| 574 | {
 | 
|---|
| 575 | if(Debug_NDataBlock>1)
 | 
|---|
| 576 |   cout<<"?_NDataBlock::operator=("<<this<<","<<v<<")"
 | 
|---|
| 577 |       <<" mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
 | 
|---|
| 578 | 
 | 
|---|
| 579 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator=v null size\n"));
 | 
|---|
| 580 | T *p=Begin(), *pe=End(); while (p<pe) *p++ = v;
 | 
|---|
| 581 | return *this;
 | 
|---|
| 582 | }
 | 
|---|
| 583 | 
 | 
|---|
| 584 | //////////////////////////////////////////////////////////////
 | 
|---|
| 585 | // Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += <T> b; //
 | 
|---|
| 586 | //////////////////////////////////////////////////////////////
 | 
|---|
| 587 | 
 | 
|---|
| 588 | //! Add a constant : ND += b
 | 
|---|
| 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"));
 | 
|---|
| 593 | T *p=Begin(), *pe=End(); while (p<pe) *p++ += b;
 | 
|---|
| 594 | return *this;
 | 
|---|
| 595 | }
 | 
|---|
| 596 | 
 | 
|---|
| 597 | //! Substract a constant : ND -= b
 | 
|---|
| 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"));
 | 
|---|
| 602 | T *p=Begin(), *pe=End(); while (p<pe) *p++ -= b;
 | 
|---|
| 603 | return *this;
 | 
|---|
| 604 | }
 | 
|---|
| 605 | 
 | 
|---|
| 606 | //! Multiply by a constant : ND *= b
 | 
|---|
| 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"));
 | 
|---|
| 611 | T *p=Begin(), *pe=End(); while (p<pe) *p++ *= b;
 | 
|---|
| 612 | return *this;
 | 
|---|
| 613 | }
 | 
|---|
| 614 | 
 | 
|---|
| 615 | //! Divide by a constant : ND /= b
 | 
|---|
| 616 | template <class T>
 | 
|---|
| 617 | NDataBlock<T>& NDataBlock<T>::operator /= (T b)
 | 
|---|
| 618 | {
 | 
|---|
| 619 | if(b==(T) 0) throw(ParmError("NDataBlock::operator/=v divide by zero\n"));
 | 
|---|
| 620 | if(mSz==0) throw(SzMismatchError("NDataBlock::operator/=v null size\n"));
 | 
|---|
| 621 | T *p=Begin(), *pe=End(); while (p<pe) *p++ /= b;
 | 
|---|
| 622 | return *this;
 | 
|---|
| 623 | }
 | 
|---|
| 624 | 
 | 
|---|
| 625 | ////////////////////////////////////////////////////////////////////
 | 
|---|
| 626 | // Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += NDataBlock1; //
 | 
|---|
| 627 | ////////////////////////////////////////////////////////////////////
 | 
|---|
| 628 | 
 | 
|---|
| 629 | //! Add a NDataBlock : ND += NDa
 | 
|---|
| 630 | template <class T>
 | 
|---|
| 631 | NDataBlock<T>& NDataBlock<T>::operator += (const NDataBlock<T>& a)
 | 
|---|
| 632 | {
 | 
|---|
| 633 | if(mSz==0 || mSz!=a.mSz)
 | 
|---|
| 634 |   throw(SzMismatchError("NDataBlock::operator+=A size mismatch/null"));
 | 
|---|
| 635 | T *p=Begin(), *pe=End();
 | 
|---|
| 636 | T const * pa=a.Begin();
 | 
|---|
| 637 | while (p<pe) *p++ += *pa++;
 | 
|---|
| 638 | return *this;
 | 
|---|
| 639 | }
 | 
|---|
| 640 | 
 | 
|---|
| 641 | //! Substract a NDataBlock : ND -= NDa
 | 
|---|
| 642 | template <class T>
 | 
|---|
| 643 | NDataBlock<T>& NDataBlock<T>::operator -= (const NDataBlock<T>& a)
 | 
|---|
| 644 | {
 | 
|---|
| 645 | if(mSz==0 || mSz!=a.mSz)
 | 
|---|
| 646 |   throw(SzMismatchError("NDataBlock::operator-=A size mismatch/null"));
 | 
|---|
| 647 | T *p=Begin(), *pe=End();
 | 
|---|
| 648 | T const *pa=a.Begin();
 | 
|---|
| 649 | while (p<pe) *p++ -= *pa++;
 | 
|---|
| 650 | return *this;
 | 
|---|
| 651 | }
 | 
|---|
| 652 | 
 | 
|---|
| 653 | //! Multiply by a NDataBlock : ND *= NDa
 | 
|---|
| 654 | template <class T>
 | 
|---|
| 655 | NDataBlock<T>& NDataBlock<T>::operator *= (const NDataBlock<T>& a)
 | 
|---|
| 656 | {
 | 
|---|
| 657 | if(mSz==0 || mSz!=a.mSz)
 | 
|---|
| 658 |   throw(SzMismatchError("NDataBlock::operator*=A size mismatch/null"));
 | 
|---|
| 659 | T *p=Begin(), *pe=End();
 | 
|---|
| 660 | T const *pa=a.Begin();
 | 
|---|
| 661 | while (p<pe) *p++ *= *pa++;
 | 
|---|
| 662 | return *this;
 | 
|---|
| 663 | }
 | 
|---|
| 664 | 
 | 
|---|
| 665 | //! Divide by a NDataBlock : ND /= NDa
 | 
|---|
| 666 | template <class T>
 | 
|---|
| 667 | NDataBlock<T>& NDataBlock<T>::operator /= (const NDataBlock<T>& a)
 | 
|---|
| 668 | // Attention, aucune protection si un element de "a" est nul.
 | 
|---|
| 669 | {
 | 
|---|
| 670 | if(mSz==0 || mSz!=a.mSz)
 | 
|---|
| 671 |   throw(SzMismatchError("NDataBlock::operator/=A size mismatch/null"));
 | 
|---|
| 672 | T *p=Begin(), *pe=End();
 | 
|---|
| 673 | T const *pa=a.Begin();
 | 
|---|
| 674 | while (p<pe) *p++ /= *pa++; // Division par zero non protegee
 | 
|---|
| 675 | return *this;
 | 
|---|
| 676 | }
 | 
|---|
| 677 | 
 | 
|---|
| 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 | //////////////////////////////////////////////////////////////////
 | 
|---|
| 683 | 
 | 
|---|
| 684 | //! Add a constant and return NDataBlock : NDret = ND + b
 | 
|---|
| 685 | template <class T>
 | 
|---|
| 686 | NDataBlock<T> NDataBlock<T>::Add(T b) const
 | 
|---|
| 687 | // Pour A+b
 | 
|---|
| 688 | {
 | 
|---|
| 689 | NDataBlock<T> result; 
 | 
|---|
| 690 | result.CloneOrShare(*this); result.SetTemp(true);
 | 
|---|
| 691 | result += b;
 | 
|---|
| 692 | return result;
 | 
|---|
| 693 | }
 | 
|---|
| 694 | 
 | 
|---|
| 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 | */
 | 
|---|
| 700 | template <class T>
 | 
|---|
| 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!)
 | 
|---|
| 703 | {
 | 
|---|
| 704 | NDataBlock<T> result; 
 | 
|---|
| 705 | result.CloneOrShare(*this); result.SetTemp(true);
 | 
|---|
| 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;
 | 
|---|
| 711 | return result;
 | 
|---|
| 712 | }
 | 
|---|
| 713 | 
 | 
|---|
| 714 | //! Multiply by a constant and return NDataBlock : NDret = ND * b
 | 
|---|
| 715 | template <class T>
 | 
|---|
| 716 | NDataBlock<T> NDataBlock<T>::Mul(T b) const
 | 
|---|
| 717 | // Pour A*b
 | 
|---|
| 718 | {
 | 
|---|
| 719 | NDataBlock<T> result; 
 | 
|---|
| 720 | result.CloneOrShare(*this); result.SetTemp(true);
 | 
|---|
| 721 | result *= b;
 | 
|---|
| 722 | return result;
 | 
|---|
| 723 | }
 | 
|---|
| 724 | 
 | 
|---|
| 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 | */
 | 
|---|
| 730 | template <class T>
 | 
|---|
| 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!)
 | 
|---|
| 733 | {
 | 
|---|
| 734 | NDataBlock<T> result; 
 | 
|---|
| 735 | result.CloneOrShare(*this); result.SetTemp(true);
 | 
|---|
| 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;
 | 
|---|
| 743 | }
 | 
|---|
| 744 | return result;
 | 
|---|
| 745 | }
 | 
|---|
| 746 | 
 | 
|---|
| 747 | ///////////////////////////////////////////////////////////////////////
 | 
|---|
| 748 | // Pour surcharge de +,-,*,/ : NDataBlock = NDataBlock1+NDataBlock2; //
 | 
|---|
| 749 | ///////////////////////////////////////////////////////////////////////
 | 
|---|
| 750 | 
 | 
|---|
| 751 | //! Add a NDataBlock and return a NDataBlock: ND = NDthis + NDb
 | 
|---|
| 752 | template <class T>
 | 
|---|
| 753 | NDataBlock<T> NDataBlock<T>::Add(const NDataBlock<T>& b) const
 | 
|---|
| 754 | // Pour A+B
 | 
|---|
| 755 | {
 | 
|---|
| 756 | if(mSz!=b.mSz)
 | 
|---|
| 757 |   throw(SzMismatchError("NDataBlock operator C=A+B size mismatch/null\n"));
 | 
|---|
| 758 | NDataBlock<T> result; result.SetTemp(true);
 | 
|---|
| 759 | if(b.IsTemp()) {result.Share(b);            result += *this;}
 | 
|---|
| 760 |   else         {result.CloneOrShare(*this); result += b;}
 | 
|---|
| 761 | return result;
 | 
|---|
| 762 | }
 | 
|---|
| 763 | 
 | 
|---|
| 764 | //! Multiply by a NDataBlock and return a NDataBlock: ND = NDthis * NDb
 | 
|---|
| 765 | template <class T>
 | 
|---|
| 766 | NDataBlock<T> NDataBlock<T>::Mul(const NDataBlock<T>& b) const
 | 
|---|
| 767 | // Pour A*B
 | 
|---|
| 768 | {
 | 
|---|
| 769 | if(mSz!=b.mSz)
 | 
|---|
| 770 |   throw(SzMismatchError("NDataBlock operator C=A*B size mismatch/null\n"));
 | 
|---|
| 771 | NDataBlock<T> result; result.SetTemp(true);
 | 
|---|
| 772 | if(b.IsTemp()) {result.Share(b);            result *= *this;}
 | 
|---|
| 773 |   else         {result.CloneOrShare(*this); result *= b;}
 | 
|---|
| 774 | return result;
 | 
|---|
| 775 | }
 | 
|---|
| 776 | 
 | 
|---|
| 777 | //! Substract a NDataBlock and return a NDataBlock: ND = NDthis - NDb
 | 
|---|
| 778 | template <class T>
 | 
|---|
| 779 | NDataBlock<T> NDataBlock<T>::Sub(const NDataBlock<T>& b) const
 | 
|---|
| 780 | // Pour A-B
 | 
|---|
| 781 | {
 | 
|---|
| 782 | if(mSz!=b.mSz)
 | 
|---|
| 783 |   throw(SzMismatchError("NDataBlock operator C=A-B size mismatch/null\n"));
 | 
|---|
| 784 | NDataBlock<T> result; result.SetTemp(true);
 | 
|---|
| 785 | if(b.IsTemp()) {
 | 
|---|
| 786 |   result.Share(b);
 | 
|---|
| 787 |   T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
 | 
|---|
| 788 |   while(p<pe) {*p = *pa++  - *p; p++;}
 | 
|---|
| 789 | } else {result.CloneOrShare(*this); result -= b;}
 | 
|---|
| 790 | return result;
 | 
|---|
| 791 | }
 | 
|---|
| 792 | 
 | 
|---|
| 793 | //! Divide by a NDataBlock and return a NDataBlock: ND = NDthis / NDb
 | 
|---|
| 794 | template <class T>
 | 
|---|
| 795 | NDataBlock<T> NDataBlock<T>::Div(const NDataBlock<T>& b) const
 | 
|---|
| 796 | // Pour A/B
 | 
|---|
| 797 | {
 | 
|---|
| 798 | if(mSz!=b.mSz)
 | 
|---|
| 799 |   throw(SzMismatchError("NDataBlock operator C=A/B size mismatch/null\n"));
 | 
|---|
| 800 | NDataBlock<T> result; result.SetTemp(true);
 | 
|---|
| 801 | if(b.IsTemp()) {
 | 
|---|
| 802 |   result.Share(b);
 | 
|---|
| 803 |   T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
 | 
|---|
| 804 |   while(p<pe) {*p = *pa++  / *p; p++;} // Division par zero non protegee
 | 
|---|
| 805 | } else {result.CloneOrShare(*this); result /= b;}
 | 
|---|
| 806 | return result;
 | 
|---|
| 807 | }
 | 
|---|
| 808 | 
 | 
|---|
| 809 | 
 | 
|---|
| 810 | ///////////////////////////////////////////////////////////////
 | 
|---|
| 811 | #ifdef __CXX_PRAGMA_TEMPLATES__
 | 
|---|
| 812 | #pragma define_template NDataBlock<uint_1>
 | 
|---|
| 813 | #pragma define_template NDataBlock<uint_2>
 | 
|---|
| 814 | #pragma define_template NDataBlock<int_1>
 | 
|---|
| 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>
 | 
|---|
| 822 | #pragma define_template NDataBlock< complex<r_4> >
 | 
|---|
| 823 | #pragma define_template NDataBlock< complex<r_8> >
 | 
|---|
| 824 | #endif
 | 
|---|
| 825 | 
 | 
|---|
| 826 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
 | 
|---|
| 827 | namespace SOPHYA {
 | 
|---|
| 828 | template class NDataBlock<uint_1>;
 | 
|---|
| 829 | template class NDataBlock<uint_2>;
 | 
|---|
| 830 | template class NDataBlock<int_1>;
 | 
|---|
| 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>;
 | 
|---|
| 838 | template class NDataBlock< complex<r_4> >;
 | 
|---|
| 839 | template class NDataBlock< complex<r_8> >;
 | 
|---|
| 840 | }
 | 
|---|
| 841 | #endif
 | 
|---|