Changeset 289 in Sophya for trunk/SophyaLib/BaseTools


Ignore:
Timestamp:
May 3, 1999, 6:56:48 PM (26 years ago)
Author:
ansari
Message:

grosses modif avec refonte code dans cmv 3/5/99

Location:
trunk/SophyaLib/BaseTools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/BaseTools/ndatablock.cc

    r285 r289  
    2020#endif
    2121
    22 // LOGIQUE DE BASE:
    23 //   - le createur par copie et la surcharge de l operateur = "partage" les donnees
    24 //   - gestion du partage de reference
    25 
    26 ////////////////////////////////////////////////////////////////
    27 //************ Createur, Destructeur, gestion des donnees
     22////////////////////////////////////////////////////////////////
     23//************ Createur, Destructeur
    2824
    2925template <class T>
     
    3329{
    3430#ifdef DEBUG_NDATABLOCK
    35 cout<<"DEBUG_NDataBlock::NDataBlock("<<this<<",n="<<n<<")"<<endl;
     31cout<<"?_NDataBlock::NDataBlock("<<this<<",n="<<n<<")"<<endl;
    3632#endif
    3733
     
    4642{
    4743#ifdef DEBUG_NDATABLOCK
    48 cout<<"DEBUG_NDataBlock::NDataBlock("<<this
     44cout<<"?_NDataBlock::NDataBlock("<<this
    4945    <<",data="<<data<<",br="<<br<<")"<<endl;
    5046#endif
     
    5955{
    6056#ifdef DEBUG_NDATABLOCK
    61 cout<<"DEBUG_NDataBlock::NDataBlock("<<this<<") default"<<endl;
     57cout<<"?_NDataBlock::NDataBlock("<<this<<") default"<<endl;
    6258#endif
    6359}
     
    6561template <class T>
    6662NDataBlock<T>::NDataBlock(const NDataBlock<T>& a)
    67 // Createur par copie
    68 // ATTENTION: partage les donnees avec "a"
    69 // Ecriture: NDataBlock a = b;
    70 //           NDataBlock a(b)
     63// Createur par copie: partage les donnees si "a" temporaire, clone sinon.
    7164: mSz(0), mSRef(NULL), mIsTemp(false)
    7265{
    7366#ifdef DEBUG_NDATABLOCK
    74 cout<<"DEBUG_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<") a.(mSz="
    75     <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp()<<")"<<endl;
    76 #endif
    77 
    78 Share(a);
     67cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<")"<<endl;
     68#endif
     69
     70CloneOrShare(a);
    7971}
    8072
    8173template <class T>
    8274NDataBlock<T>::NDataBlock(const NDataBlock<T>& a,bool share)
    83 // Createur avec choix de partager ou non
    84 // Si "a" temporaire alors partage meme si share=false
     75// Createur avec choix de partager ou non selon "share"
    8576: mSz(0), mSRef(NULL), mIsTemp(false)
    8677{
    8778#ifdef DEBUG_NDATABLOCK
    88 cout<<"DEBUG_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<",sh=<<"<<share<<")"
    89     <<" a.(mSz="<<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp()<<")"<<endl;
     79cout<<"?_NDataBlock::NDataBlock("<<this<<",&a="<<&a<<",sh=<<"<<share<<")"<<endl;
    9080#endif
    9181
     
    9888{
    9989#ifdef DEBUG_NDATABLOCK
    100 cout<<"DEBUG_NDataBlock::~NDataBlock("<<this<<")"<<endl;
     90cout<<"?_NDataBlock::~NDataBlock("<<this<<")"<<endl;
    10191#endif
    10292
     
    10595
    10696////////////////////////////////////////////////////////////////
    107 
    108 template <class T>
    109 void NDataBlock<T>::SetTemp(bool temp) const
    110 // Set temporary
    111 {
    112 mIsTemp=temp;
    113 
    114 #ifdef DEBUG_NDATABLOCK
    115 cout<<"DEBUG_NDataBlock::SetTemp("<<this<<","<<temp
    116     <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
    117 #endif
    118 }
     97//************ Gestion de donnees
    11998
    12099template <class T>
     
    142121{
    143122#ifdef DEBUG_NDATABLOCK
    144 cout<<"DEBUG_NDataBlock::Alloc("<<this<<","
     123cout<<"?_NDataBlock::Alloc("<<this<<","
    145124    <<n<<","<<data<<","<<br<<") mSz="<<mSz
    146125    <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
     
    164143// fait au niveau du dessus!).
    165144if(!br) NallocData++; NallocSRef++;
    166 cout<<"...DEBUG_NDataBlock::Alloc mSz="<<mSz<<" mSRef="<<mSRef
     145cout<<"...?_NDataBlock::Alloc mSz="<<mSz<<" mSRef="<<mSRef
    167146    <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<< mSRef->data
    168147    <<" mSRef->bridge="<<mSRef->bridge<<" IsTemp="<<mIsTemp
     
    173152template <class T>
    174153void NDataBlock<T>::Clone(const NDataBlock<T>& a)
    175 // Clone (copie de donnee) a partir de "a"
    176 // sauf si "a" est une classe temporaire (dans ce cas share!)
    177 {
    178 #ifdef DEBUG_NDATABLOCK
    179 cout<<"DEBUG_NDataBlock::Clone("<<this<<","<<&a<<") a.(mSz="
     154// Clone: copie de donnees a partir de "a"
     155{
     156#ifdef DEBUG_NDATABLOCK
     157cout<<"?_NDataBlock::Clone("<<this<<","<<&a<<") a.(mSz="
    180158    <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp()
    181159    <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
    182160#endif
    183161
    184 if(a.mSz==0) throw(SzMismatchError("NDataBlock::Clone a.mSz==0\n"));
    185 else if(a.IsTemp()) Share(a);
    186 else {Alloc(a.mSz); memcpy(Data(),a.Data(),mSz*sizeof(T));}
     162if(&a==NULL) throw(NullPtrError("NDataBlock::Clone  &a==NULL\n"));
     163if(!a.mSRef || a.mSz==0) throw(SzMismatchError("NDataBlock::Clone a.mSz==0\n"));
     164Alloc(a.mSz);
     165memcpy(Data(),a.Data(),mSz*sizeof(T));
     166}
     167
     168template <class T>
     169void NDataBlock<T>::CloneOrShare(const NDataBlock<T>& a)
     170// CloneOrShare: Share si "a" temporaire, Clone sinon.
     171{
     172#ifdef DEBUG_NDATABLOCK
     173cout<<"?_NDataBlock::CloneOrShare("<<this<<","<<&a<<")"<<endl;
     174#endif
     175
     176if(&a==NULL) throw(NullPtrError("NDataBlock::CloneOrShare  &a==NULL\n"));
     177if(a.IsTemp()) Share(a); else Clone(a);
    187178}
    188179
    189180template <class T>
    190181void NDataBlock<T>::Share(const NDataBlock<T>& a)
    191 // Partage des donnees avec "a"
    192 {
    193 #ifdef DEBUG_NDATABLOCK
    194 cout<<"DEBUG_NDataBlock::Share("<<this<<","<<&a<<")";
     182// Share: Partage les donnees avec "a"
     183{
     184#ifdef DEBUG_NDATABLOCK
     185cout<<"?_NDataBlock::Share("<<this<<","<<&a<<")";
    195186if(&a!=NULL) cout<<" a.(mSz="<<a.mSz<<" mSRef="<<a.mSRef
    196187                 <<" IsTemp="<<a.IsTemp()<<")";
     
    199190
    200191if(&a==NULL) throw(NullPtrError("NDataBlock::Share  &a==NULL\n"));
    201 // on ne peut partager si "a" pas alloue
    202 if(!a.mSRef) throw(NullPtrError("NDataBlock::Share not allocated a\n"));
     192if(!a.mSRef || a.mSz==0) throw(NullPtrError("NDataBlock::Share a.mSz=0\n"));
    203193if(mSRef) Delete();
    204194mSz = a.mSz; mSRef = a.mSRef; mSRef->nref++;
    205195
    206196#ifdef DEBUG_NDATABLOCK
    207 cout<<"...DEBUG_NDataBlock::Share mSz="<<mSz<<" mSRef="<<mSRef
     197cout<<"...?_NDataBlock::Share mSz="<<mSz<<" mSRef="<<mSRef
    208198    <<" mSRef->nref="<<mSRef->nref<<" mSRef->data="<< mSRef->data
    209199    <<" mSRef->bridge="<<mSRef->bridge<<" IsTemp="<<mIsTemp<<endl;
     
    216206{
    217207#ifdef DEBUG_NDATABLOCK
    218 cout<<"DEBUG_NDataBlock::Delete("<<this<<") mSz="<<mSz
     208cout<<"?_NDataBlock::Delete("<<this<<") mSz="<<mSz
    219209    <<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp;
    220210if(mSRef)
     
    224214#endif
    225215
    226 if(mSRef==NULL) return; // cas du createur par defaut
     216if(mSRef==NULL) return;
    227217mSRef->nref--;
    228218if(mSRef->nref != 0) {
    229219
    230220#ifdef DEBUG_NDATABLOCK
    231 cout<<"...DEBUG_NDataBlock::Delete() pas de desallocation il reste nref="
     221cout<<"...?_NDataBlock::Delete() pas de desallocation il reste nref="
    232222    <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
    233223#endif
     
    239229#ifdef DEBUG_NDATABLOCK
    240230if(!mSRef->bridge) NallocData--; NallocSRef--;
    241 cout<<"...DEBUG_NDataBlock::Delete() desallocation complete il reste nref="
     231cout<<"...?_NDataBlock::Delete() desallocation complete il reste nref="
    242232    <<mSRef->nref<<" Total("<<NallocData<<","<<NallocSRef<<")"<<endl;
    243233#endif
     
    250240}
    251241
    252 ////////////////////////////////////////////////////////////////
    253 
    254 template <class T>
    255 void NDataBlock<T>::Reset(T v)
    256 {
    257 if(mSRef==NULL || mSRef->data==NULL || mSz==0) return;
    258 T *p=Begin(), *pe=End(); while(p<pe) *p++ = v;
    259 }
    260 
    261 template <class T>
    262 void NDataBlock<T>::ReSize(size_t n,bool force_alloc)
    263 // Re-dimension, avec re-allocation de la place si n != mSz
    264 // Si n==mSz, la place n'est re-allouee que si force_alloc=true
    265 {
    266 if(!force_alloc && n == mSz)  return;
    267 Alloc(n);
    268 }
    269 
    270242template <class T>
    271243void NDataBlock<T>::FillFrom(size_t n,T* data)
    272244// Remplissage par un tableau de donnees
    273245// - Si classe vide : creation de l'espace memoire
    274 // - Si classe connectee : on ecrit selon la longueur
     246// - Si classe connectee : on ecrit selon la longueur minimale
     247//                         (cad this->mSz ou "n")
    275248{
    276249if(data==NULL) throw(NullPtrError("NDataBlock::FillFrom  data==NULL\n"));
    277250if(n==0) throw(ParmError("NDataBlock::FillFrom  n<=0\n"));
    278 if(mSRef==NULL) Alloc(n); // cas du createur par default
     251if(mSRef==NULL) Alloc(n);
    279252if(mSz<n) n = mSz;
    280253memcpy(Data(),data,n*sizeof(T));
     
    306279
    307280////////////////////////////////////////////////////////////////
    308 //**** Surcharge de = : NDataBlock=NDataBlock; NDataBlock=<T> b;
    309 
    310 template <class T>
    311 NDataBlock<T>& NDataBlock<T>::operator = (const NDataBlock<T>& a)
    312 // surcharge avec partage des donnees
    313 // Ecriture:  NDataBlock a; a = b;
    314 //            NDataBlock a(10); a = b; (a est re-affecte)
    315 {
    316 #ifdef DEBUG_NDATABLOCK
    317 cout<<"DEBUG_NDataBlock::operator=("<<this<<","<<&a<<") a.(mSz="
    318     <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp()
    319     <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
    320 #endif
    321 
    322 if(this == &a) return *this;
    323 if(a.mSz!=mSz)
    324   throw(SzMismatchError("NDataBlock::operator=A size mismatch/null\n"));
    325 Share(a);
    326 return *this;
    327 }
    328 
    329 template <class T>
    330 NDataBlock<T>& NDataBlock<T>::operator = (T v)
    331 // surcharge avec copie des donnees (pas de partage)
    332 // "this" est sur-ecrit, attention au partage de reference!
    333 // NDataBlock a; a = v; ou bien NDataBlock a(10); a = v;
    334 {
    335 #ifdef DEBUG_NDATABLOCK
    336 cout<<"DEBUG_NDataBlock::operator=("<<this<<","<<v<<")"
    337     <<" mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
    338 #endif
    339 
    340 if(mSz==0) throw(SzMismatchError("NDataBlock::operator=v null size\n"));
    341 T *p=Begin(), *pe=End();
    342 while (p<pe) *p++ = v;
    343 return *this;
    344 }
    345 
    346 ////////////////////////////////////////////////////////////////
    347281
    348282template <class T>
     
    371305
    372306////////////////////////////////////////////////////////////////
     307//**** Surcharge de = : NDataBlock=NDataBlock; NDataBlock=<T> b;
     308
     309template <class T>
     310NDataBlock<T>& NDataBlock<T>::operator = (const NDataBlock<T>& a)
     311// Affectation: partage des donnees si "a" temporaire, clone sinon.
     312{
     313#ifdef DEBUG_NDATABLOCK
     314cout<<"?_NDataBlock::operator=("<<this<<","<<&a<<") a.(mSz="
     315    <<a.mSz<<" mSRef="<<a.mSRef<<" IsTemp="<<a.IsTemp()
     316    <<"), mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
     317#endif
     318
     319if(this == &a) return *this;
     320if(a.mSz==0)
     321  throw(SzMismatchError("NDataBlock::operator=A null size\n"));
     322CloneOrShare(a);
     323return *this;
     324}
     325
     326template <class T>
     327NDataBlock<T>& NDataBlock<T>::operator = (T v)
     328// Affectation de tous les elements a une constante "v"
     329{
     330#ifdef DEBUG_NDATABLOCK
     331cout<<"?_NDataBlock::operator=("<<this<<","<<v<<")"
     332    <<" mSz="<<mSz<<" mSRef="<<mSRef<<" IsTemp="<<mIsTemp<<endl;
     333#endif
     334
     335if(mSz==0) throw(SzMismatchError("NDataBlock::operator=v null size\n"));
     336T *p=Begin(), *pe=End();
     337while (p<pe) *p++ = v;
     338return *this;
     339}
     340
     341////////////////////////////////////////////////////////////////
    373342//**** Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += <T> b;
    374343
     
    410379}
    411380
    412 
    413381////////////////////////////////////////////////////////////////
    414382//**** Surcharge de +=,-=,*=,/= (INPLACE): NDataBlock += NDataBlock;
     
    421389T *p=Begin(), *pe=End();
    422390T const * pa=a.Begin();
    423 while (p<pe) *p++ += *pa++; // ca marche meme si *this=a
     391while (p<pe) *p++ += *pa++;
    424392return *this;
    425393}
     
    432400T *p=Begin(), *pe=End();
    433401T const *pa=a.Begin();
    434 while (p<pe) *p++ -= *pa++; // ca marche meme si *this=a
     402while (p<pe) *p++ -= *pa++;
    435403return *this;
    436404}
     
    443411T *p=Begin(), *pe=End();
    444412T const *pa=a.Begin();
    445 while (p<pe) *p++ *= *pa++; // ca marche meme si *this=a
     413while (p<pe) *p++ *= *pa++;
    446414return *this;
    447415}
     
    449417template <class T>
    450418NDataBlock<T>& NDataBlock<T>::operator /= (const NDataBlock<T>& a)
     419// Attention, aucune protection si un element de "a" est nul.
    451420{
    452421if(mSz==0 || mSz!=a.mSz)
     
    461430//**** Surcharge de +,-,*,/ : NDataBlock = NDataBlock+<T>b;
    462431//                            NDataBlock = <T>b+NDataBlock;
    463 // ATTENTION: re-affectation imposee
    464432
    465433template <class T>
     
    467435// Pour A+b
    468436{
    469 NDataBlock<T> result(*this,false); result.SetTemp(true);
     437NDataBlock<T> result(*this); result.SetTemp(true);
    470438result += b;
    471439return result;
     
    476444// Pour A-b
    477445{
    478 NDataBlock<T> result(*this,false); result.SetTemp(true);
     446NDataBlock<T> result(*this); result.SetTemp(true);
    479447return result -= b;
    480448}
     
    484452// Pour b-A
    485453{
    486 NDataBlock<T> result(*this,false); result.SetTemp(true);
     454NDataBlock<T> result(*this); result.SetTemp(true);
    487455T *p=result.Begin(), *pe=result.End();
    488456T const *pa=this->Begin();
     
    495463// Pour A*b
    496464{
    497 NDataBlock<T> result(*this,false); result.SetTemp(true);
     465NDataBlock<T> result(*this); result.SetTemp(true);
    498466return result *= b;
    499467}
     
    503471// Pour A/b
    504472{
    505 NDataBlock<T> result(*this,false); result.SetTemp(true);
     473NDataBlock<T> result(*this); result.SetTemp(true);
    506474return result /= b;
    507475}
     
    511479// Pour b/A
    512480{
    513 NDataBlock<T> result(*this,false); result.SetTemp(true);
     481NDataBlock<T> result(*this); result.SetTemp(true);
    514482T *p=result.Begin(), *pe=result.End();
    515483T const *pa = this->Begin();
     
    518486}
    519487
    520 
    521488////////////////////////////////////////////////////////////////
    522489//**** Surcharge de +,-,*,/ : NDataBlock = NDataBlock+NDataBlock;
     
    529496  throw(SzMismatchError("NDataBlock operator C=A+B size mismatch/null\n"));
    530497NDataBlock<T> result; result.SetTemp(true);
    531 if(b.IsTemp()) {
    532   result.Share(b);
    533   result += *this;
    534 } else {
    535   result.Clone(*this);
    536   result += b;
    537 }
     498if(b.IsTemp()) {result.Share(b);            result += *this;}
     499  else         {result.CloneOrShare(*this); result += b;}
    538500return result;
    539501}
     
    546508  throw(SzMismatchError("NDataBlock operator C=A*B size mismatch/null\n"));
    547509NDataBlock<T> result; result.SetTemp(true);
    548 if(b.IsTemp()) {
    549   result.Share(b);
    550   result *= *this;
    551 } else {
    552   result.Clone(*this);
    553   result *= b;
    554 }
     510if(b.IsTemp()) {result.Share(b);            result *= *this;}
     511  else         {result.CloneOrShare(*this); result *= b;}
    555512return result;
    556513}
     
    565522if(b.IsTemp()) {
    566523  result.Share(b);
    567   T *p=result.Begin(), *pe=result.End();
    568   T const *pa=Begin();
     524  T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
    569525  while(p<pe) {*p = *pa++  - *p; p++;}
    570 } else {
    571   result.Clone(*this);
    572   result -= b;
    573 }
     526} else {result.CloneOrShare(*this); result -= b;}
    574527return result;
    575528}
     
    584537if(b.IsTemp()) {
    585538  result.Share(b);
    586   T *p=result.Begin(), *pe=result.End();
    587   T const *pa=Begin();
     539  T *p=result.Begin(), *pe=result.End(); T const *pa=Begin();
    588540  while(p<pe) {*p = *pa++  / *p; p++;}
    589 } else {
    590   result.Clone(*this);
    591   result /= b;
    592 }
     541} else {result.CloneOrShare(*this); result /= b;}
    593542return result;
    594543}
     
    659608is.Get(itab, 3);
    660609if (dobj == NULL) dobj = new NDataBlock<T>(itab[1]);
    661 else dobj->ReSize(itab[1], false);
     610else dobj->ReSize(itab[1]);
    662611// On lit le tableau de nombres
    663612PIOSReadArray(is, dobj->Data(), dobj->Size());
     
    707656#endif
    708657
    709 
    710658#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
    711659template class NDataBlock<uint_1>;
  • trunk/SophyaLib/BaseTools/ndatablock.h

    r285 r289  
    3737  // Temporaire?
    3838  inline bool IsTemp(void) const {return mIsTemp;}
    39   void SetTemp(bool temp=false) const;
     39  inline void SetTemp(bool temp=false) const {mIsTemp = temp;}
    4040
    4141  // Gestion taille/Remplissage
    4242  void Clone(const NDataBlock<T>& a);
    43   void Reset(T v=0);
    44   void ReSize(size_t n,bool force_alloc=true);
     43  void CloneOrShare(const NDataBlock<T>& a);
     44  void Share(const NDataBlock<T>& a);
    4545  void FillFrom(size_t n,T* data);
     46  inline void Reset(T v=0)
     47         {if(mSz==0) return; T *p=Begin(),*pe=End(); while(p<pe) *p++=v;}
     48  inline void ReSize(size_t n) {Alloc(n);}
    4649 
    4750  // Informations pointeur/data
     
    5356  inline T& operator()(size_t i)       {return *(mSRef->data+i);}
    5457  inline T  operator()(size_t i) const {return *(mSRef->data+i);}
    55 
    5658  inline T*        Begin()        {return mSRef->data;}
    5759  inline T const*  Begin() const  {return mSRef->data;}
     
    6769  T Product(size_t i1=0,size_t n=0) const;
    6870
    69   // Surcharge d operateurs
     71  // Surcharge d'operateurs INPLACE: A = x , A = B , A @= x , A @= B
    7072  NDataBlock<T>& operator = (const NDataBlock<T>& a);
    7173  NDataBlock<T>& operator = (T v);
     
    8183  NDataBlock<T>& operator /= (const NDataBlock<T>& a);
    8284
     85  // Surcharge d'operateurs: C = A @ x , C = A @ B
    8386  NDataBlock<T> Add(T b) const;
    8487  NDataBlock<T> Sub(T b) const;
     
    99102
    100103  void Alloc(size_t n,T* data=NULL,Bridge* br=NULL);
    101   void Share(const NDataBlock<T>& a);
    102104  void Delete(void);
    103105
     
    153155template <class T>
    154156class FIO_NDataBlock : public  PPersist  {
    155 
    156157public:
    157             FIO_NDataBlock();
    158             FIO_NDataBlock(string const & filename);
    159             FIO_NDataBlock(const NDataBlock<T> & obj);
    160             FIO_NDataBlock(NDataBlock<T> * obj);
    161   virtual   ~FIO_NDataBlock();
    162 
    163   virtual   AnyDataObj* DataObj();
     158  FIO_NDataBlock();
     159  FIO_NDataBlock(string const & filename);
     160  FIO_NDataBlock(const NDataBlock<T> & obj);
     161  FIO_NDataBlock(NDataBlock<T> * obj);
     162  virtual ~FIO_NDataBlock();
     163  virtual AnyDataObj* DataObj();
    164164  inline operator NDataBlock<T>() { return(*dobj); }
    165 
    166165protected :
    167166  virtual void       ReadSelf(PInPersist&);           
Note: See TracChangeset for help on using the changeset viewer.