Changeset 970 in Sophya


Ignore:
Timestamp:
Apr 26, 2000, 7:55:11 PM (25 years ago)
Author:
ansari
Message:

Passage des TArray en partage de donnees par defaut pour constructeur de copie - Copie pour l'operateur = , cmv+Reza 26/4/2000

Location:
trunk/SophyaLib/TArray
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/TArray/tarray.cc

    r967 r970  
    140140TArray<T>& TArray<T>::Set(const TArray<T>& a)
    141141{
    142   if (this != &a) {
    143     CloneOrShare(a);
    144     if (mInfo) {delete mInfo; mInfo = NULL;}
    145     if (a.mInfo) mInfo = new DVList(*(a.mInfo));
    146   }
     142  if (this == &a) return(*this);
     143  if (a.NbDimensions() < 1)
     144    throw RangeCheckError("TArray<T>::Set(a ) - Array a not allocated ! ");
     145  if (NbDimensions() < 1) CloneOrShare(a);
     146  else CopyElt(a);
    147147  return(*this);
    148148}
     
    158158  if (a.mInfo) mInfo = new DVList(*(a.mInfo));
    159159}
     160
     161//! Clone if \b a is not temporary, share if temporary
     162template <class T>
     163void TArray<T>::CloneOrShare(const TArray<T>& a)
     164{
     165  string exmsg = "TArray<T>::CloneOrShare()";
     166  if (!UpdateSizes(a.ndim_, a.size_, a.step_, a.offset_, exmsg))  throw( ParmError(exmsg) );
     167  mNDBlock.CloneOrShare(a.mNDBlock);
     168}
     169
     170//! Share data with a
     171template <class T>
     172void TArray<T>::Share(const TArray<T>& a)
     173{
     174  string exmsg = "TArray<T>::Share()";
     175  if (!UpdateSizes(a.ndim_, a.size_, a.step_, a.offset_, exmsg))  throw( ParmError(exmsg) );
     176  mNDBlock.Share(a.mNDBlock);
     177}
     178
    160179
    161180//! Resize array
     
    261280    throw RangeCheckError("TArray<T>::PackElements() - Not Allocated Array ! ");
    262281  if ( !force && (AvgStep() == 1) ) {
    263     TArray<T> ra(*this, true);
     282    TArray<T> ra;
     283    ra.Share(*this);
    264284    ra.SetTemp(true);
    265285    return(ra);
     
    408428
    409429//! Substract a constant value \b x to an array
    410 template <class T>
    411 TArray<T>& TArray<T>::Sub(T x)
     430/*!
     431Substract a constant from the *this = *this-x
     432\param fginv == true : Perfoms the inverse subtraction (*this = x-(*this))
     433*/
     434template <class T>
     435TArray<T>& TArray<T>::Sub(T x, bool fginv)
    412436{
    413437  if (NbDimensions() < 1)
     
    419443    uint_8 maxx = totsize_*step;
    420444    pe = Data();
    421     for(k=0; k<maxx; k+=step )  pe[k] -= x;
     445    if (fginv)
     446      for(k=0; k<maxx; k+=step )  pe[k] = x-pe[k];
     447    else
     448      for(k=0; k<maxx; k+=step )  pe[k] -= x;
    422449  }
    423450  else {    // Non regular data spacing ...
     
    428455    for(j=0; j<naxa; j++)  {
    429456      pe = mNDBlock.Begin()+Offset(ka,j);
    430       for(k=0; k<gpas; k+=step)  pe[k] -= x;
     457      if (fginv)
     458        for(k=0; k<gpas; k+=step)  pe[k] = x-pe[k];
     459      else
     460        for(k=0; k<gpas; k+=step)  pe[k] -= x;
    431461    }
    432462  }
     
    462492
    463493//! Divide an array by a constant value \b x
    464 template <class T>
    465 TArray<T>& TArray<T>::Div(T x)
     494/*!
     495Divide the array by a constant *this = *this/x
     496\param fginv == true : Perfoms the inverse division (*this = x/(*this))
     497*/
     498template <class T>
     499TArray<T>& TArray<T>::Div(T x, bool fginv)
    466500{
    467501  if (NbDimensions() < 1)
    468502    throw RangeCheckError("TArray<T>::Div(T )  - Not Allocated Array ! ");
    469   if (x == (T) 0 )
     503  if (!fginv && (x == (T) 0) )
    470504    throw MathExc("TArray<T>::Div(T )  - Divide by zero ! ");
    471505  T * pe;
     
    475509    uint_8 maxx = totsize_*step;
    476510    pe = Data();
    477     for(k=0; k<maxx; k+=step )  pe[k] /= x;
     511    if (fginv)
     512      for(k=0; k<maxx; k+=step )  pe[k] = x/pe[k];
     513    else
     514      for(k=0; k<maxx; k+=step )  pe[k] /= x;
    478515  }
    479516  else {    // Non regular data spacing ...
     
    484521    for(j=0; j<naxa; j++)  {
    485522      pe = mNDBlock.Begin()+Offset(ka,j);
    486       for(k=0; k<gpas; k+=step)  pe[k] /= x;
    487     }
    488   }
    489   return(*this);
    490 }
    491 
    492 
    493 //! Inverse substract : A = \b x - A
    494 template <class T>
    495 TArray<T>& TArray<T>::SubInv(T x)
    496 {
    497   if (NbDimensions() < 1)
    498     throw RangeCheckError("TArray<T>::SubInv(T )  - Not Allocated Array ! ");
    499   T * pe;
    500   uint_8 j,k;
    501   if (AvgStep() > 0)   {  // regularly spaced elements
    502     uint_8 step = AvgStep();
    503     uint_8 maxx = totsize_*step;
    504     pe = Data();
    505     for(k=0; k<maxx; k+=step )  pe[k] = x-pe[k];
    506   }
    507   else {    // Non regular data spacing ...
    508     uint_4 ka = MaxSizeKA();
    509     uint_8 step = Step(ka);
    510     uint_8 gpas = Size(ka)*step;
    511     uint_8 naxa = Size()/Size(ka);
    512     for(j=0; j<naxa; j++)  {
    513       pe = mNDBlock.Begin()+Offset(ka,j);
    514       for(k=0; k<gpas; k+=step)  pe[k] = x-pe[k];
    515     }
    516   }
    517   return(*this);
    518 }
    519 
    520 //! Inverse Divide : A(i,j,...) = x / A(i,j,...)
    521 template <class T>
    522 TArray<T>& TArray<T>::DivInv(T x)
    523 {
    524   if (NbDimensions() < 1)
    525     throw RangeCheckError("TArray<T>::DivInv(T )  - Not Allocated Array ! ");
    526   T * pe;
    527   uint_8 j,k;
    528   if (AvgStep() > 0)   {  // regularly spaced elements
    529     uint_8 step = AvgStep();
    530     uint_8 maxx = totsize_*step;
    531     pe = Data();
    532     for(k=0; k<maxx; k+=step )  pe[k] = x/pe[k];
    533   }
    534   else {    // Non regular data spacing ...
    535     uint_4 ka = MaxSizeKA();
    536     uint_8 step = Step(ka);
    537     uint_8 gpas = Size(ka)*step;
    538     uint_8 naxa = Size()/Size(ka);
    539     for(j=0; j<naxa; j++)  {
    540       pe = mNDBlock.Begin()+Offset(ka,j);
    541       for(k=0; k<gpas; k+=step)  pe[k] = x/pe[k];
    542     }
    543   }
    544   return(*this);
    545 }
     523      if (fginv)
     524        for(k=0; k<gpas; k+=step)  pe[k] = x/pe[k];
     525      else
     526        for(k=0; k<gpas; k+=step)  pe[k] /= x;
     527    }
     528  }
     529  return(*this);
     530}
     531
     532
    546533
    547534
     
    583570
    584571//! Substract two TArrays
    585 template <class T>
    586 TArray<T>& TArray<T>::SubElt(const TArray<T>& a)
     572/*!
     573Substract two TArrays *this = *this-a
     574\param fginv == true : Perfoms the inverse subtraction (*this = a-(*this))
     575*/
     576template <class T>
     577TArray<T>& TArray<T>::SubElt(const TArray<T>& a, bool fginv)
    587578{
    588579  if (NbDimensions() < 1)
     
    600591    pe = Data();
    601592    pea = a.Data();
    602     for(k=0, ka=0;  k<maxx;  k+=step, ka+=stepa )  pe[k] -= pea[ka] ;
     593    if (fginv)
     594      for(k=0, ka=0;  k<maxx;  k+=step, ka+=stepa )  pe[k] = pea[ka]-pe[k] ;
     595    else
     596      for(k=0, ka=0;  k<maxx;  k+=step, ka+=stepa )  pe[k] -= pea[ka] ;
    603597  }
    604598  else {    // Non regular data spacing ...
     
    611605      pe = mNDBlock.Begin()+Offset(ax,j);
    612606      pea = a.DataBlock().Begin()+a.Offset(ax,j);
    613       for(k=0, ka=0;  k<gpas;  k+=step, ka+=stepa)  pe[k] -= pea[ka];
    614     }
    615   }
    616   return(*this);
    617 }
     607      if (fginv)
     608        for(k=0, ka=0;  k<gpas;  k+=step, ka+=stepa)  pe[k] = pea[ka]-pe[k] ;
     609      else
     610        for(k=0, ka=0;  k<gpas;  k+=step, ka+=stepa)  pe[k] -= pea[ka];
     611    }
     612  }
     613  return(*this);
     614}
     615
    618616
    619617//! Multiply two TArrays (elements by elements)
     
    654652
    655653//! Divide two TArrays (elements by elements)
    656 template <class T>
    657 TArray<T>& TArray<T>::DivElt(const TArray<T>& a)
     654/*!
     655Divide two TArrays *this = (*this)/a
     656\param fginv == true : Perfoms the inverse division (*this = a/(*this))
     657*/
     658template <class T>
     659TArray<T>& TArray<T>::DivElt(const TArray<T>& a, bool fginv)
    658660{
    659661  if (NbDimensions() < 1)
     
    671673    pe = Data();
    672674    pea = a.Data();
    673     for(k=0, ka=0;  k<maxx;  k+=step, ka+=stepa )  pe[k] /= pea[ka] ;
     675    if (fginv)
     676      for(k=0, ka=0;  k<maxx;  k+=step, ka+=stepa )  pe[k] = pea[ka]/pe[k];
     677    else
     678      for(k=0, ka=0;  k<maxx;  k+=step, ka+=stepa )  pe[k] /= pea[ka] ;
    674679  }
    675680  else {    // Non regular data spacing ...
     
    682687      pe = mNDBlock.Begin()+Offset(ax,j);
    683688      pea = a.DataBlock().Begin()+a.Offset(ax,j);
    684       for(k=0, ka=0;  k<gpas;  k+=step, ka+=stepa)  pe[k] /= pea[ka];
     689      if (fginv)
     690        for(k=0, ka=0;  k<gpas;  k+=step, ka+=stepa)  pe[k] = pea[ka]/pe[k];
     691      else
     692        for(k=0, ka=0;  k<gpas;  k+=step, ka+=stepa)  pe[k] /= pea[ka];
    685693    }
    686694  }
     
    835843}
    836844
    837 //! Clone if \b a is not temporary, share if temporary
    838 template <class T>
    839 void TArray<T>::CloneOrShare(const TArray<T>& a)
    840 {
    841   string exmsg = "TArray<T>::CloneOrShare()";
    842   if (!UpdateSizes(a.ndim_, a.size_, a.step_, a.offset_, exmsg))  throw( ParmError(exmsg) );
    843   mNDBlock.CloneOrShare(a.mNDBlock);
    844 }
    845 
    846 //! Share data with a
    847 template <class T>
    848 void TArray<T>::Share(const TArray<T>& a)
    849 {
    850   string exmsg = "TArray<T>::Share()";
    851   if (!UpdateSizes(a.ndim_, a.size_, a.step_, a.offset_, exmsg))  throw( ParmError(exmsg) );
    852   mNDBlock.Share(a.mNDBlock);
    853 }
    854 
    855845
    856846
  • trunk/SophyaLib/TArray/tarray.h

    r967 r970  
    4646  // Gestion taille/Remplissage
    4747  virtual void Clone(const TArray<T>& a);
     48  // partage les donnees si "a" temporaire, clone sinon.
     49  void CloneOrShare(const TArray<T>& a);
     50  // Share: partage les donnees de "a"
     51
     52  void Share(const TArray<T>& a);
    4853  void ReSize(uint_4 ndim, uint_4 * siz, uint_4 step=1);
    4954  void Realloc(uint_4 ndim, uint_4 * siz, uint_4 step=1, bool force=false);
     
    122127  //! Add \b x to all elements
    123128  inline  TArray<T>&  operator += (T x)            { return Add(x); }
    124   virtual TArray<T>&  Sub(T x);
     129  virtual TArray<T>&  Sub(T x, bool fginv=false);
    125130  //! Substract \b x to all elements
    126131  inline  TArray<T>&  operator -= (T x)            { return Sub(x); }
     
    128133  //! Multiply all elements by \b x
    129134  inline  TArray<T>&  operator *= (T x)            { return Mul(x); }
    130   virtual TArray<T>&  Div(T x);
     135  virtual TArray<T>&  Div(T x, bool fginv=false);
    131136  //! Divide all elements by \b x
    132137  inline  TArray<T>&  operator /= (T x)            { return Div(x); }
    133   virtual TArray<T>&  SubInv(T x);  //   A ---> x-A
    134   virtual TArray<T>&  DivInv(T x);  //   A ---> x/A
    135138
    136139// A += -=  (ajoute, soustrait element par element les deux tableaux )
     
    138141  //! Operator TArray += TArray
    139142  inline  TArray<T>&  operator += (const TArray<T>& a)  { return AddElt(a); }
    140   virtual TArray<T>&  SubElt(const TArray<T>& a);
     143  virtual TArray<T>&  SubElt(const TArray<T>& a, bool fginv=false);
    141144  //! Operator TArray -= TArray
    142145  inline  TArray<T>&  operator -= (const TArray<T>& a)  { return SubElt(a); }
    143146// Multiplication, division element par element les deux tableaux
    144147  virtual TArray<T>&  MulElt(const TArray<T>& a);
    145   virtual TArray<T>&  DivElt(const TArray<T>& a);
     148  virtual TArray<T>&  DivElt(const TArray<T>& a, bool fginv=false);
    146149// Recopie des valeurs, element par element
    147150  virtual TArray<T>&  CopyElt(const TArray<T>& a);
     
    159162
    160163protected:
    161   // partage les donnees si "a" temporaire, clone sinon.
    162   void CloneOrShare(const TArray<T>& a);
    163   // Share: partage les donnees de "a"
    164   void Share(const TArray<T>& a);
    165164
    166165  NDataBlock<T> mNDBlock; //!< Block for datas
     
    180179  \brief Operator TArray = TArray + constant */
    181180template <class T> inline TArray<T> operator + (const TArray<T>& a, T b)
    182     {TArray<T> result(a); result.SetTemp(true); result.Add(b); return result;}
     181    {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
     182    result.Add(b); return result;}
    183183
    184184/*! \ingroup TArray \fn operator+(T,const TArray<T>&)
    185185  \brief Operator TArray = constant + TArray */
    186186template <class T> inline TArray<T> operator + (T b,const TArray<T>& a)
    187     {TArray<T> result(a); result.SetTemp(true); result.Add(b); return result;}
     187    {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
     188    result.Add(b); return result;}
    188189
    189190/*! \ingroup TArray \fn operator-(const TArray<T>&,T)
    190191  \brief Operator TArray = TArray - constant */
    191192template <class T> inline TArray<T> operator - (const TArray<T>& a, T b)
    192     {TArray<T> result(a); result.SetTemp(true); result.Sub(b); return result;}
     193    {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
     194    result.Sub(b); return result;}
    193195
    194196/*! \ingroup TArray \fn operator-(T,const TArray<T>&)
    195197  \brief Operator TArray = constant - TArray */
    196198template <class T> inline TArray<T> operator - (T b,const TArray<T>& a)
    197     {TArray<T> result(a); result.SetTemp(true); result.SubInv(b); return result;}
     199    {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
     200    result.Sub(b,true); return result;}
    198201
    199202/*! \ingroup TArray \fn operator*(const TArray<T>&,T)
    200203  \brief Operator TArray = TArray * constant */
    201204template <class T> inline TArray<T> operator * (const TArray<T>& a, T b)
    202     {TArray<T> result(a); result.SetTemp(true); result.Mul(b); return result;}
     205    {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
     206    result.Mul(b); return result;}
    203207
    204208/*! \ingroup TArray \fn operator*(T,const TArray<T>&)
    205209  \brief Operator TArray = constant * TArray */
    206210template <class T> inline TArray<T> operator * (T b,const TArray<T>& a)
    207     {TArray<T> result(a); result.SetTemp(true); result.Mul(b); return result;}
     211    {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
     212    result.Mul(b); return result;}
    208213
    209214/*! \ingroup TArray \fn operator/(const TArray<T>&,T)
    210215  \brief Operator TArray = TArray / constant */
    211216template <class T> inline TArray<T> operator / (const TArray<T>& a, T b)
    212     {TArray<T> result(a); result.SetTemp(true); result.DivInv(b); return result;}
     217    {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
     218    result.Div(b); return result;}
     219
     220/*! \ingroup TArray \fn operator/(T,const TArray<T>&)
     221  \brief Operator TArray = constant / TArray  */
     222template <class T> inline TArray<T> operator / (T b, const TArray<T>& a)
     223    {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true);
     224    result.Div(b, true); return result;}
    213225
    214226////////////////////////////////////////////////////////////////
     
    219231template <class T>
    220232inline TArray<T> operator + (const TArray<T>& a,const TArray<T>& b)
    221     {TArray<T> result(a); result.SetTemp(true); result.AddElt(b); return result;}
     233    { TArray<T> result; result.SetTemp(true);
     234    if (b.IsTemp())  { result.Share(b); result.AddElt(a); }
     235    else { result.CloneOrShare(a); result.AddElt(b); }
     236    return result; }
    222237
    223238/*! \ingroup TArray \fn operator-(const TArray<T>&,const TArray<T>&)
     
    225240template <class T>
    226241inline TArray<T> operator - (const TArray<T>& a,const TArray<T>& b)
    227     {TArray<T> result(a); result.SetTemp(true); result.SubElt(b); return result;}
     242    { TArray<T> result; result.SetTemp(true);
     243    if (b.IsTemp())  { result.Share(b); result.SubElt(a, true); }
     244    else { result.CloneOrShare(a); result.SubElt(b); }
     245    return result; }
    228246
    229247
  • trunk/SophyaLib/TArray/tmatrix.cc

    r967 r970  
    1 // $Id: tmatrix.cc,v 1.9 2000-04-21 16:31:25 ansari Exp $
     1// $Id: tmatrix.cc,v 1.10 2000-04-26 17:55:10 ansari Exp $
    22//                         C.Magneville          04/99
    33#include "machdefs.h"
     
    110110    throw SzMismatchError("TMatrix<T>::Set(const TArray<T>& a) a.NbDimensions() > 2");
    111111  TArray<T>::Set(a);
    112   if (a.NbDimensions() == 1) {
     112  if (NbDimensions() == 1) {
    113113    size_[1] = 1;
    114114    step_[1] = size_[0]*step_[0];
    115115    ndim_ = 2;
    116116  }
    117   UpdateMemoryMapping(a, SameMemoryMapping);
     117  UpdateMemoryMapping(*this, SameMemoryMapping);
    118118  return(*this);
    119119}
  • trunk/SophyaLib/TArray/tmatrix.h

    r967 r970  
    136136template <class T>
    137137inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b)
    138     {TMatrix<T> result(a); result.SetTemp(true); result.AddElt(b); return result;}
     138    {TMatrix<T> result; result.SetTemp(true);
     139    if (b.IsTemp())  { result.Share(b); result.AddElt(a); }
     140    else { result.CloneOrShare(a); result.AddElt(b); }
     141    return result; }
     142
    139143
    140144/*! \ingroup TArray \fn operator-(const TMatrix<T>&,const TMatrix<T>&)
     
    142146template <class T>
    143147inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b)
    144     {TMatrix<T> result(a); result.SetTemp(true); result.SubElt(b); return result;}
     148    {TMatrix<T> result; result.SetTemp(true);
     149    if (b.IsTemp())  { result.Share(b); result.SubElt(a, true); }
     150    else { result.CloneOrShare(a); result.SubElt(b); }
     151    return result; }
    145152
    146153// Surcharge d'operateurs C = A * B
     
    148155  \brief * : multiply matrixes \b a and \b b */
    149156template <class T> inline TMatrix<T> operator * (const TMatrix<T>& a, const TMatrix<T>& b)
    150 { TMatrix<T> result(a); result.SetTemp(true); return(result.Multiply(b)); }
     157   { return(a.Multiply(b)); }
    151158
    152159// Typedef pour simplifier et compatibilite Peida
Note: See TracChangeset for help on using the changeset viewer.