Changeset 944 in Sophya for trunk/SophyaLib/BaseTools/ndatablock.cc
- Timestamp:
- Apr 16, 2000, 1:49:13 PM (25 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/BaseTools/ndatablock.cc
r802 r944 10 10 #include "ndatablock.h" 11 11 12 // define DEBUG_NDATABLOCK 13 14 #ifdef DEBUG_NDATABLOCK 15 static size_t NallocData = 0; 16 static size_t NallocSRef = 0; 17 #endif 18 19 //////////////////////////////////////////////////////////////// 20 //************ Createur, Destructeur 21 12 /*! 13 \class SOPHYA::NDataBlock 14 \ingroup SysTools 15 Management of data blocks 16 */ 17 18 ////////////////////////////////// 19 // Fonctionnement en mode debug // 20 ////////////////////////////////// 21 22 template <class T> int NDataBlock<T>::Debug_NDataBlock = 0; 23 template <class T> size_t NDataBlock<T>::NallocData = 0; 24 template <class T> size_t NDataBlock<T>::NallocSRef = 0; 25 26 //! Set debug (and level print) for allocation and references debug. 27 /*! 28 \param prtlevel : activate/des-activate debug mode 29 and select print level 30 31 \arg prtlevel <= 0 : no debug 32 \arg prtlevel == 1 : debug activated, no print 33 \arg prtlevel >=2 : debug activated, 34 print infos in all routines that have something to do with 35 allocations or des-allocation of datas or references. 36 */ 37 template <class T> 38 void NDataBlock<T>::SetPrintDebug(int prtdbglevel) 39 { 40 Debug_NDataBlock = prtdbglevel; 41 } 42 43 //! Reset debug counter values. 44 /*! 45 \param nallocdata : reset number of allocated data structures to \b nallocdata 46 \param nallocsref : reset number of allocated references to \b nallocsref 47 \warning In principle this routine should not be use (only experts) 48 */ 49 template <class T> 50 void NDataBlock<T>::ResetDebug(size_t nallocdata, size_t nallocsref) 51 { 52 NallocData = nallocdata; 53 NallocSRef = nallocsref; 54 } 55 56 //! Print debug current status. 57 /*! 58 Print debug current status for number of allocated 59 data structures and number of allocated references. 60 */ 61 template <class T> 62 void NDataBlock<T>::PrintDebug() 63 { 64 cout<<"... ... ... NallocData = "<<NallocData 65 <<" , NallocSRef = "<<NallocSRef 66 <<" ... ... ..."<<endl; 67 } 68 69 /////////////////////////// 70 // Createur, Destructeur // 71 /////////////////////////// 72 73 //! Constructor for \b n datas 22 74 template <class T> 23 75 NDataBlock<T>::NDataBlock(size_t n) … … 25 77 : mSz(0), mSRef(NULL), mIsTemp(false) 26 78 { 27 #ifdef DEBUG_NDATABLOCK 28 cout<<"?_NDataBlock::NDataBlock("<<this<<",n="<<n<<")"<<endl; 29 #endif 79 if(Debug_NDataBlock>1) 80 cout<<"?_NDataBlock::NDataBlock("<<this<<",n="<<n<<")"<<endl; 30 81 31 82 Alloc(n, NULL, NULL, true); // allocation et mise a zero 32 83 } 33 84 85 //! Constructor for \b n datas shared with external 86 /*! 87 Datas are previously allocated by an other external source. 88 \warning This require particular care (see Alloc) 89 \sa Alloc 90 */ 34 91 template <class T> 35 92 NDataBlock<T>::NDataBlock(size_t n, T* data, Bridge* br) … … 38 95 : mSz(0), mSRef(NULL), mIsTemp(false) 39 96 { 40 #ifdef DEBUG_NDATABLOCK 41 cout<<"?_NDataBlock::NDataBlock("<<this 42 <<",data="<<data<<",br="<<br<<")"<<endl; 43 #endif 97 if(Debug_NDataBlock>1) 98 cout<<"?_NDataBlock::NDataBlock("<<this 99 <<",data="<<data<<",br="<<br<<")"<<endl; 44 100 45 101 Alloc(n,data,br); 46 102 } 47 103 104 //! Default constructor 48 105 template <class T> 49 106 NDataBlock<T>::NDataBlock() … … 51 108 : mSz(0), mSRef(NULL), mIsTemp(false) 52 109 { 53 #ifdef DEBUG_NDATABLOCK 54 cout<<"?_NDataBlock::NDataBlock("<<this<<") default"<<endl; 55 #endif 56 } 57 110 if(Debug_NDataBlock>1) 111 cout<<"?_NDataBlock::NDataBlock("<<this<<") default"<<endl; 112 } 113 114 //! Copy constructor 115 /*! 116 \warning datas are shared if \b a is temporary, cloned if not. 117 */ 58 118 template <class T> 59 119 NDataBlock<T>::NDataBlock(const NDataBlock<T>& a) … … 61 121 : mSz(0), mSRef(NULL), mIsTemp(false) 62 122 { 63 #ifdef DEBUG_NDATABLOCK 64 cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<")"<<endl; 65 #endif 123 if(Debug_NDataBlock>1) 124 cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<")"<<endl; 66 125 67 126 CloneOrShare(a); 68 127 } 69 128 129 //! Copy constructor with \b share option 130 /*! 131 \warning datas are shared if \b share is \b true, cloned if not. 132 */ 70 133 template <class T> 71 134 NDataBlock<T>::NDataBlock(const NDataBlock<T>& a,bool share) … … 73 136 : mSz(0), mSRef(NULL), mIsTemp(false) 74 137 { 75 #ifdef DEBUG_NDATABLOCK 76 cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<",sh=<<"<<share<<")"<<endl; 77 #endif 138 if(Debug_NDataBlock>1) 139 cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<",sh=<<"<<share<<")"<<endl; 78 140 79 141 if(share) Share(a); else Clone(a); 80 142 } 81 143 144 //! Destructor 82 145 template <class T> 83 146 NDataBlock<T>::~NDataBlock() 84 147 // Destructeur 85 148 { 86 #ifdef DEBUG_NDATABLOCK 87 cout<<"?_NDataBlock::~NDataBlock("<<this<<")"<<endl; 88 #endif 149 if(Debug_NDataBlock>1) 150 cout<<"?_NDataBlock::~NDataBlock("<<this<<")"<<endl; 89 151 90 152 Delete(); 91 153 } 92 154 93 //////////////////////////////////////////////////////////////// 94 //************ Gestion de donnees 95 155 //////////////////////// 156 // Gestion de donnees // 157 //////////////////////// 158 159 //! Clone datas from \b a 160 template <class T> 161 void NDataBlock<T>::Clone(const NDataBlock<T>& a) 162 // Clone: copie de donnees a partir de "a" 163 { 164 if(Debug_NDataBlock>1) 165 cout<<"?_NDataBlock::Clone("<<this<<","<<&a<<") a.(mSz=" 166 <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp() 167 <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl; 168 169 if(&a==NULL) throw(NullPtrError("NDataBlock::Clone &a==NULL\n")); 170 if(!a.mSRef || a.mSz==0) throw(SzMismatchError("NDataBlock::Clone a.mSz==0\n")); 171 Alloc(a.mSz, NULL, NULL, false); // pas de mise a zero 172 memcpy(Data(),a.Data(),mSz*sizeof(T)); 173 } 174 175 //! Share datas with \b a 176 template <class T> 177 void NDataBlock<T>::Share(const NDataBlock<T>& a) 178 // Share: Partage les donnees avec "a" 179 { 180 if(Debug_NDataBlock>1) { 181 cout<<"?_NDataBlock::Share("<<this<<","<<&a<<")"; 182 if(&a!=NULL) cout<<" a.(mSz="<<a.mSz<<" mSRef="<<a.mSRef 183 <<" IsTemp="<<a.IsTemp()<<")"; 184 cout<<", mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl; 185 } 186 187 if(&a==NULL) throw(NullPtrError("NDataBlock::Share &a==NULL\n")); 188 if(!a.mSRef || a.mSz==0) throw(NullPtrError("NDataBlock::Share a.mSz=0\n")); 189 if(mSRef) Delete(); 190 mSz = a.mSz; mSRef = a.mSRef; mSRef->nref++; 191 192 if(Debug_NDataBlock>1) 193 cout<<"...?_NDataBlock::Share mSz="<<mSz<<" mSRef="<<mSRef 194 <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<< mSRef->data 195 <<" mSRef->bridge="<<mSRef->bridge<<" IsTemp="<<mIsTemp<<endl; 196 } 197 198 //! \b Share with \b a if \b temporary, \b clone from \b a if not. 199 template <class T> 200 void NDataBlock<T>::CloneOrShare(const NDataBlock<T>& a) 201 // CloneOrShare: Share si "a" temporaire, Clone sinon. 202 { 203 if(Debug_NDataBlock>1) 204 cout<<"?_NDataBlock::CloneOrShare("<<this<<","<<&a<<")"<<endl; 205 206 if(&a==NULL) throw(NullPtrError("NDataBlock::CloneOrShare &a==NULL\n")); 207 if(a.IsTemp()) Share(a); else Clone(a); 208 } 209 210 //////////////////////////////////////////////////////////// 211 // Allocation , destruction , remplissage et reallocation // 212 //////////////////////////////////////////////////////////// 213 214 //! Allocation management 215 /*! 216 Allocation d'un NOUVEL espace de stoquage de "n" donnees 217 \verbatim 218 Si data==NULL : allocation de l'espace memoire 219 si zero == true , l'espace est remplis de zeros 220 data!=NULL : partage des donnees avec l'adresse data 221 Si br==NULL : les donnees nous appartiennent 222 br!=NULL : les donnees ne nous appartiennent pas (ex: Blitz) 223 224 Exemple: on veut connecter a un tableau de T* 225 > float *x = new float[5]; ... remplissage de x[] ...; 226 1- On veut que NDataBlock NE DESALLOUE PAS le tableau "x[]" 227 a- Premiere solution 228 > NDataBlock A(5,x,new Bridge); 229 ...... 230 > delete [] x; 231 - Il faut deleter x[] explicitement. 232 - Le destructeur de "A" ne detruit pas x[]. 233 ATTENTION: Une fois x[] detruit, "A" ne peut 234 plus acceder les donnees! 235 - Bridge est detruit par le destructeur de "A" 236 b- Autre solution: 237 > NDataBlock A(5); A.FillFrom(5,x); 238 > delete [] x; 239 ...... 240 - Il faut deleter x[] explicitement. 241 - "A" possede une copie en local de x[]. 242 - Le destructeur de "A" ne detruit pas x[] mais la copie locale. 243 2- On veut que NDataBlock desalloue le tableau 244 > NDataBlock A(5,x); 245 - Ne Pas Faire "delete [] x;" 246 - "A" partage les donnees avec x[]. 247 - Le destructeur de "A" detruit x[]. 248 249 --- REMARQUE SUR LE DANGER DE CERTAINES SITUATIONS (CMV): 250 1-/ x = new float[n1]; NDataBlock A(n2,x); 251 1er danger: si n2>n1 depassement de tableaux (core dump) 252 2sd danger: celui qui alloue x[] ne doit pas faire le "delete" 253 en desaccord avec toutes les regles de bonne conduite. 254 2-/ float x[5]={1,2,3,4,5}; {NDataBlock A(n2,&x[0]);} cout<<x[2]; 255 Ici, a la sortie du bloc {}, le destructeur de "A" va detruire 256 l'adresse de &x[0]: je n'ose imaginer que ca se fasse sans probleme 257 et de toute facon, cout<<x[2]; va surement faire des etincelles. 258 3-/ x = new float[n1]; NDataBlock A(n2,x,new Bridge); 259 1er danger: si n2>n1 depassement de tableaux (core dump) 260 2sd danger: si la methode bridgee (blitz?) detruit x[] 261 "A" n'a plus de donnees connectees! 262 --- CONCLUSION 263 Cette classe est franchement merdique. 264 - On peut accepter la prise de risque liee a NDataBlock(n2,x,new Bridge); 265 car je ne vois pas comment on pourrait faire autrement pour connecter 266 un tableau de type blitz par exemple. 267 - Par contre le createur NDataBlock(n2,x); doit etre interdit 268 dans sa forme actelle car trop dangereux et il me semble inutile. 269 - Dans cette nouvelle optique: 270 NDataBlock(n2,x,new Bridge) et NDataBlock(n2,x) disparaissent 271 On remplace par NDataBlock(n2,x) {Alloc(n2,x,new Bridge);} 272 qui force le Bridge dans tout les cas puisque NDataBlock 273 ne possede pas les donnees. 274 Mais puis-je encore le faire vu que NDataBlock est a la base 275 de TVector,TMatrix et qu'il faut donc reprendre tout le code DPC 276 - Quoiqu'il arrive Alloc est une methode privee et peut donc rester 277 sous sa forme actuelle. 278 279 \endverbatim 280 */ 96 281 template <class T> 97 282 void NDataBlock<T>::Alloc(size_t n,T* data,Bridge* br,bool zero) 98 // Allocation d'un NOUVEL espace de stoquage de "n" donnees 99 // Si data==NULL : allocation de l'espace memoire 100 // si zero == true , l'espace est remplis de zeros 101 // data!=NULL : partage des donnees avec l'adresse data 102 // Si br==NULL : les donnees nous appartiennent 103 // br!=NULL : les donnees ne nous appartiennent pas (ex: Blitz) 104 // 105 // Exemple: on veut connecter a un tableau de T* 106 // > float *x = new float[5]; ... remplissage de x[] ...; 107 // 1- On veut que NDataBlock NE DESALLOUE PAS le tableau "x[]" 108 // a- Premiere solution 109 // > NDataBlock A(5,x,new Bridge); 110 // ...... 111 // > delete [] x; 112 // - Il faut deleter x[] explicitement. 113 // - Le destructeur de "A" ne detruit pas x[]. 114 // ATTENTION: Une fois x[] detruit, "A" ne peut 115 // plus acceder les donnees! 116 // - Bridge est detruit par le destructeur de "A" 117 // b- Autre solution: 118 // > NDataBlock A(5); A.FillFrom(5,x); 119 // > delete [] x; 120 // ...... 121 // - Il faut deleter x[] explicitement. 122 // - "A" possede une copie en local de x[]. 123 // - Le destructeur de "A" ne detruit pas x[] mais la copie locale. 124 // 2- On veut que NDataBlock desalloue le tableau 125 // > NDataBlock A(5,x); 126 // - Ne Pas Faire "delete [] x;" 127 // - "A" partage les donnees avec x[]. 128 // - Le destructeur de "A" detruit x[]. 129 // 130 // --- REMARQUE SUR LE DANGER DE CERTAINES SITUATIONS (CMV): 131 // 1-/ x = new float[n1]; NDataBlock A(n2,x); 132 // 1er danger: si n2>n1 depassement de tableaux (core dump) 133 // 2sd danger: celui qui alloue x[] ne doit pas faire le "delete" 134 // en desaccord avec toutes les regles de bonne conduite. 135 // 2-/ float x[5]={1,2,3,4,5}; {NDataBlock A(n2,&x[0]);} cout<<x[2]; 136 // Ici, a la sortie du bloc {}, le destructeur de "A" va detruire 137 // l'adresse de &x[0]: je n'ose imaginer que ca se fasse sans probleme 138 // et de toute facon, cout<<x[2]; va surement faire des etincelles. 139 // 3-/ x = new float[n1]; NDataBlock A(n2,x,new Bridge); 140 // 1er danger: si n2>n1 depassement de tableaux (core dump) 141 // 2sd danger: si la methode bridgee (blitz?) detruit x[] 142 // "A" n'a plus de donnees connectees! 143 // --- CONCLUSION 144 // Cette classe est franchement merdique. 145 // - On peut accepter la prise de risque liee a NDataBlock(n2,x,new Bridge); 146 // car je ne vois pas comment on pourrait faire autrement pour connecter 147 // un tableau de type blitz par exemple. 148 // - Par contre le createur NDataBlock(n2,x); doit etre interdit 149 // dans sa forme actelle car trop dangereux et il me semble inutile. 150 // - Dans cette nouvelle optique: 151 // NDataBlock(n2,x,new Bridge) et NDataBlock(n2,x) disparaissent 152 // On remplace par NDataBlock(n2,x) {Alloc(n2,x,new Bridge);} 153 // qui force le Bridge dans tout les cas puisque NDataBlock 154 // ne possede pas les donnees. 155 // Mais puis-je encore le faire vu que NDataBlock est a la base 156 // de TVector,TMatrix et qu'il faut donc reprendre tout le code DPC 157 // - Quoiqu'il arrive Alloc est une methode privee et peut donc rester 158 // sous sa forme actuelle. 159 // 160 { 161 #ifdef DEBUG_NDATABLOCK 162 cout<<"?_NDataBlock::Alloc("<<this<<"," 163 <<n<<","<<data<<","<<br<<") mSz="<<mSz 164 <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl; 165 #endif 283 { 284 if(Debug_NDataBlock>1) 285 cout<<"?_NDataBlock::Alloc("<<this<<"," 286 <<n<<","<<data<<","<<br<<") mSz="<<mSz 287 <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl; 166 288 167 289 if(br && !data) … … 176 298 mSRef->bridge = br; 177 299 178 #ifdef DEBUG_NDATABLOCK 179 // Meme dans le cas data!=0 et br==0 (connexion d'un tableau 180 // avec destruction geree par ~NDataBlock (cas 2-) on compte 181 // comme si on avait fait une allocation du tableau (ce qui a ete 182 // fait au niveau du dessus!). 183 if(!br) NallocData++; NallocSRef++; 184 cout<<"...?_NDataBlock::Alloc mSz="<<mSz<<" mSRef="<<mSRef 185 <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<< mSRef->data 186 <<" mSRef->bridge="<<mSRef->bridge<<" IsTemp="<<mIsTemp 187 <<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl; 188 #endif 189 } 190 191 template <class T> 192 void NDataBlock<T>::Clone(const NDataBlock<T>& a) 193 // Clone: copie de donnees a partir de "a" 194 { 195 #ifdef DEBUG_NDATABLOCK 196 cout<<"?_NDataBlock::Clone("<<this<<","<<&a<<") a.(mSz=" 197 <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp() 198 <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl; 199 #endif 200 201 if(&a==NULL) throw(NullPtrError("NDataBlock::Clone &a==NULL\n")); 202 if(!a.mSRef || a.mSz==0) throw(SzMismatchError("NDataBlock::Clone a.mSz==0\n")); 203 Alloc(a.mSz, NULL, NULL, false); // pas de mise a zero 204 memcpy(Data(),a.Data(),mSz*sizeof(T)); 205 } 206 207 template <class T> 208 void NDataBlock<T>::CloneOrShare(const NDataBlock<T>& a) 209 // CloneOrShare: Share si "a" temporaire, Clone sinon. 210 { 211 #ifdef DEBUG_NDATABLOCK 212 cout<<"?_NDataBlock::CloneOrShare("<<this<<","<<&a<<")"<<endl; 213 #endif 214 215 if(&a==NULL) throw(NullPtrError("NDataBlock::CloneOrShare &a==NULL\n")); 216 if(a.IsTemp()) Share(a); else Clone(a); 217 } 218 219 template <class T> 220 void NDataBlock<T>::Share(const NDataBlock<T>& a) 221 // Share: Partage les donnees avec "a" 222 { 223 #ifdef DEBUG_NDATABLOCK 224 cout<<"?_NDataBlock::Share("<<this<<","<<&a<<")"; 225 if(&a!=NULL) cout<<" a.(mSz="<<a.mSz<<" mSRef="<<a.mSRef 226 <<" IsTemp="<<a.IsTemp()<<")"; 227 cout<<", mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl; 228 #endif 229 230 if(&a==NULL) throw(NullPtrError("NDataBlock::Share &a==NULL\n")); 231 if(!a.mSRef || a.mSz==0) throw(NullPtrError("NDataBlock::Share a.mSz=0\n")); 232 if(mSRef) Delete(); 233 mSz = a.mSz; mSRef = a.mSRef; mSRef->nref++; 234 235 #ifdef DEBUG_NDATABLOCK 236 cout<<"...?_NDataBlock::Share mSz="<<mSz<<" mSRef="<<mSRef 237 <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<< mSRef->data 238 <<" mSRef->bridge="<<mSRef->bridge<<" IsTemp="<<mIsTemp<<endl; 239 #endif 240 } 241 300 if(Debug_NDataBlock>0) { 301 // Meme dans le cas data!=0 et br==0 (connexion d'un tableau 302 // avec destruction geree par ~NDataBlock (cas 2-) on compte 303 // comme si on avait fait une allocation du tableau (ce qui a ete 304 // fait au niveau du dessus!). 305 if(!br) NallocData++; NallocSRef++; 306 if(Debug_NDataBlock>1) 307 cout<<"...?_NDataBlock::Alloc mSz="<<mSz<<" mSRef="<<mSRef 308 <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<< mSRef->data 309 <<" mSRef->bridge="<<mSRef->bridge<<" IsTemp="<<mIsTemp 310 <<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl; 311 } 312 } 313 314 //! Management of de-allocation 242 315 template <class T> 243 316 void NDataBlock<T>::Delete(void) 244 317 // Pour detruire les pointeurs en tenant compte des references 245 318 { 246 #ifdef DEBUG_NDATABLOCK 247 cout<<"?_NDataBlock::Delete("<<this<<") mSz="<<mSz248 <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp;249 if(mSRef)250 cout<<" mSRef->nref="<<mSRef->nref<<" mSRef->data="251 <<mSRef->data<<" mSRef->bridge="<<mSRef->bridge;252 cout<<endl;253 #endif 319 if(Debug_NDataBlock>1) { 320 cout<<"?_NDataBlock::Delete("<<this<<") mSz="<<mSz 321 <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp; 322 if(mSRef) 323 cout<<" mSRef->nref="<<mSRef->nref<<" mSRef->data=" 324 <<mSRef->data<<" mSRef->bridge="<<mSRef->bridge; 325 cout<<endl; 326 } 254 327 255 328 if(mSRef==NULL) return; … … 257 330 if(mSRef->nref != 0) { 258 331 259 #ifdef DEBUG_NDATABLOCK 332 if(Debug_NDataBlock>1) 260 333 cout<<"...?_NDataBlock::Delete() pas de desallocation il reste nref=" 261 334 <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl; 262 #endif263 335 264 336 mSz = 0; mSRef=NULL; … … 266 338 } 267 339 268 #ifdef DEBUG_NDATABLOCK 269 if(!mSRef->bridge) NallocData--; NallocSRef--; 270 cout<<"...?_NDataBlock::Delete() desallocation complete il reste nref=" 271 <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl; 272 #endif 340 if(Debug_NDataBlock>0) { 341 if(!mSRef->bridge) NallocData--; NallocSRef--; 342 if(Debug_NDataBlock>1) 343 cout<<"...?_NDataBlock::Delete() desallocation complete il reste nref=" 344 <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl; 345 } 273 346 274 347 // Si il y a un Bridge les donnees ne n'appartiennent pas, on detruit le Bridge … … 279 352 } 280 353 354 //! Fill dats of this NDataBlock with the \b n datas pointed by \b data 355 /*! 356 \warning If class empty : allocate space in memory 357 \warning If class already connected : overwrite with minimum size 358 (\b n or \b mSz) 359 */ 281 360 template <class T> 282 361 void NDataBlock<T>::FillFrom(size_t n,T* data) … … 293 372 } 294 373 374 //! Re-allocate space for \b nnew datas 375 /*! 376 \param nnnew : new size 377 \param force : to manage the way re-allocation will be done (see after). 378 \verbatim 379 Re-allocation de "nnew" place memoire pour les donnees 380 avec conservation des "nold" donnees precedentes si possible. 381 "force" gere la re-allocation de la place memoire pour les donnees. 382 Divers cas se presentent: 383 a-/ *** nnew>nold force=quelconque *** 384 place re-allouee, donnees [0,nold[ copiees, surplus [nold,new[ mis a zero 385 b-/ *** nnew<=nold force=true *** 386 place re-allouee, donnees [0,nnew[ copiees, pas de surplus 387 c-/ *** nnew<=nold force=false *** 388 place non re-allouee, seule la valeur de la taille est diminuee 389 - On tient compte du partage des donnees dans tous les cas. 390 - Si il n'y a pas de donnees connectees a la classe, on re-alloue 391 dans tous les cas 392 \endverbatim 393 */ 295 394 template <class T> 296 395 void NDataBlock<T>::Realloc(size_t nnew,bool force) 297 // Re-allocation de "nnew" place memoire pour les donnees298 // avec conservation des "nold" donnees precedentes si possible.299 // "force" gere la re-allocation de la place memoire pour les donnees.300 // Divers cas se presentent:301 // a-/ *** nnew>nold force=quelconque ***302 // place re-allouee, donnees [0,nold[ copiees, surplus [nold,new[ mis a zero303 // b-/ *** nnew<=nold force=true ***304 // place re-allouee, donnees [0,nnew[ copiees, pas de surplus305 // c-/ *** nnew<=nold force=false ***306 // place non re-allouee, seule la valeur de la taille est diminuee307 // - On tient compte du partage des donnees dans tous les cas.308 // - Si il n'y a pas de donnees connectees a la classe, on re-alloue309 // dans tous les cas310 396 { 311 397 if(nnew==0) throw(ParmError("NDataBlock::Realloc n<=0\n")); … … 323 409 } 324 410 325 //////////////////////////////////////////////////////////////// 326 //**** Impression 327 411 //////////////// 412 // Impression // 413 //////////////// 414 415 //! Give infos and print \b n datas beginning at \b i1 on stream \b os. 328 416 template <class T> 329 417 void NDataBlock<T>::Print(ostream& os,size_t i1,size_t n) const … … 347 435 } 348 436 349 //////////////////////////////////////////////////////////////// 350 437 ////////////////////////////////////////////// 438 // Calcul de la somme / produit des donnees // 439 ////////////////////////////////////////////// 440 441 //! Return sum of \b n datas beginning at data \b i1. 351 442 template <class T> 352 443 T NDataBlock<T>::Sum(size_t i1,size_t n) const … … 361 452 } 362 453 454 //! Return product of \b n datas beginning at data \b i1. 363 455 template <class T> 364 456 T NDataBlock<T>::Product(size_t i1,size_t n) const … … 373 465 } 374 466 375 //////////////////////////////////////////////////////////////// 376 //**** Surcharge de = : NDataBlock=NDataBlock; NDataBlock=<T> b; 377 467 /////////////////////////////////////////////////////////////// 468 // Surcharge de = : NDataBlock=NDataBlock; NDataBlock=<T> b; // 469 /////////////////////////////////////////////////////////////// 470 471 //! Operator = : ND = ND1 472 /*! \warning share if \b a is temporary, clone if not */ 378 473 template <class T> 379 474 NDataBlock<T>& NDataBlock<T>::operator = (const NDataBlock<T>& a) 380 475 // Affectation: partage des donnees si "a" temporaire, clone sinon. 381 476 { 382 #ifdef DEBUG_NDATABLOCK 383 cout<<"?_NDataBlock::operator=("<<this<<","<<&a<<") a.(mSz=" 384 <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp() 385 <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl; 386 #endif 477 if(Debug_NDataBlock>1) 478 cout<<"?_NDataBlock::operator=("<<this<<","<<&a<<") a.(mSz=" 479 <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp() 480 <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl; 387 481 388 482 if(this == &a) return *this; … … 393 487 } 394 488 489 //! Operator = : ND = \b v (at dats set to \b v). 395 490 template <class T> 396 491 NDataBlock<T>& NDataBlock<T>::operator = (T v) 397 492 // Affectation de tous les elements a une constante "v" 398 493 { 399 #ifdef DEBUG_NDATABLOCK 400 cout<<"?_NDataBlock::operator=("<<this<<","<<v<<")"401 <<" mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;402 #endif 494 if(Debug_NDataBlock>1) 495 cout<<"?_NDataBlock::operator=("<<this<<","<<v<<")" 496 <<" mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl; 497 403 498 404 499 if(mSz==0) throw(SzMismatchError("NDataBlock::operator=v null size\n")); … … 408 503 } 409 504 410 //////////////////////////////////////////////////////////////// 411 //**** Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += <T> b; 412 505 ////////////////////////////////////////////////////////////// 506 // Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += <T> b; // 507 ////////////////////////////////////////////////////////////// 508 509 //! Add a constant : ND += b 413 510 template <class T> 414 511 NDataBlock<T>& NDataBlock<T>::operator += (T b) … … 420 517 } 421 518 519 //! Substract a constant : ND -= b 422 520 template <class T> 423 521 NDataBlock<T>& NDataBlock<T>::operator -= (T b) … … 429 527 } 430 528 529 //! Multiply by a constant : ND *= b 431 530 template <class T> 432 531 NDataBlock<T>& NDataBlock<T>::operator *= (T b) … … 438 537 } 439 538 539 //! Divide by a constant : ND *= b 440 540 template <class T> 441 541 NDataBlock<T>& NDataBlock<T>::operator /= (T b) … … 448 548 } 449 549 450 //////////////////////////////////////////////////////////////// 451 //**** Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += NDataBlock; 452 550 //////////////////////////////////////////////////////////////////// 551 // Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += NDataBlock1; // 552 //////////////////////////////////////////////////////////////////// 553 554 //! Add a NDataBlock : ND += ND1 453 555 template <class T> 454 556 NDataBlock<T>& NDataBlock<T>::operator += (const NDataBlock<T>& a) … … 462 564 } 463 565 566 //! Substract a NDataBlock : ND -= ND1 464 567 template <class T> 465 568 NDataBlock<T>& NDataBlock<T>::operator -= (const NDataBlock<T>& a) … … 473 576 } 474 577 578 //! Multiply by a NDataBlock : ND *= ND1 475 579 template <class T> 476 580 NDataBlock<T>& NDataBlock<T>::operator *= (const NDataBlock<T>& a) … … 484 588 } 485 589 590 //! Divide by a NDataBlock : ND *= ND1 486 591 template <class T> 487 592 NDataBlock<T>& NDataBlock<T>::operator /= (const NDataBlock<T>& a) … … 497 602 498 603 //////////////////////////////////////////////////////////////// 499 //**** Surcharge de +,-,*,/ : NDataBlock = NDataBlock+<T>b; 500 // NDataBlock = <T>b+NDataBlock; 501 604 // Pour surcharge de +,-,*,/ : NDataBlock = NDataBlock1+<T>b; // 605 // NDataBlock = <T>b+NDataBlock1; // 606 //////////////////////////////////////////////////////////////// 607 608 //! Add a constant and return NDataBlock : NDret = ND + b 502 609 template <class T> 503 610 NDataBlock<T> NDataBlock<T>::Add(T b) const … … 509 616 } 510 617 618 //! Substract a constant and return NDataBlock : NDret = ND - b 511 619 template <class T> 512 620 NDataBlock<T> NDataBlock<T>::Sub(T b) const … … 517 625 } 518 626 627 //! Substract from a constant and return NDataBlock : NDret = b - ND 519 628 template <class T> 520 629 NDataBlock<T> NDataBlock<T>::SubInv(T b) const … … 528 637 } 529 638 639 //! Multiply by a constant and return NDataBlock : NDret = ND * b 530 640 template <class T> 531 641 NDataBlock<T> NDataBlock<T>::Mul(T b) const … … 536 646 } 537 647 648 //! Divide by a constant and return NDataBlock : NDret = ND / b 538 649 template <class T> 539 650 NDataBlock<T> NDataBlock<T>::Div(T b) const … … 544 655 } 545 656 657 //! Divide from a constant and return NDataBlock : NDret = b / ND 546 658 template <class T> 547 659 NDataBlock<T> NDataBlock<T>::DivInv(T b) const … … 555 667 } 556 668 557 //////////////////////////////////////////////////////////////// 558 //**** Surcharge de +,-,*,/ : NDataBlock = NDataBlock+NDataBlock; 559 669 /////////////////////////////////////////////////////////////////////// 670 // Pour surcharge de +,-,*,/ : NDataBlock = NDataBlock1+NDataBlock2; // 671 /////////////////////////////////////////////////////////////////////// 672 673 //! Add a NDataBlock and return a NDataBlock: ND = NDthis + NDb 560 674 template <class T> 561 675 NDataBlock<T> NDataBlock<T>::Add(const NDataBlock<T>& b) const … … 570 684 } 571 685 686 //! Multiply by a NDataBlock and return a NDataBlock: ND = NDthis * NDb 572 687 template <class T> 573 688 NDataBlock<T> NDataBlock<T>::Mul(const NDataBlock<T>& b) const … … 582 697 } 583 698 699 //! Substract a NDataBlock and return a NDataBlock: ND = NDthis - NDb 584 700 template <class T> 585 701 NDataBlock<T> NDataBlock<T>::Sub(const NDataBlock<T>& b) const … … 597 713 } 598 714 715 //! Divide by a NDataBlock and return a NDataBlock: ND = NDthis / NDb 599 716 template <class T> 600 717 NDataBlock<T> NDataBlock<T>::Div(const NDataBlock<T>& b) const
Note:
See TracChangeset
for help on using the changeset viewer.