Changeset 810 for trunk/yat/utility


Ignore:
Timestamp:
Mar 16, 2007, 12:31:22 AM (15 years ago)
Author:
Jari Häkkinen
Message:

Changed to obey the general wisdom: If you write the same code snipet more than 2 times, make it a function.

Location:
trunk/yat/utility
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/matrix.cc

    r808 r810  
    151151  matrix::~matrix(void)
    152152  {
     153    delete_allocated_memory();
     154    if (blas_result_)
     155      gsl_matrix_free(blas_result_);
     156  }
     157
     158
     159  const matrix& matrix::clone(const matrix& other)
     160  {
     161    if (this!=&other) {
     162
     163      delete_allocated_memory();
     164
     165      if (other.view_) {
     166        view_ = new gsl_matrix_view(*other.view_);
     167        proxy_m_ = m_ = &(view_->matrix);
     168      }
     169      else if (other.view_const_) {
     170        view_const_ = new gsl_matrix_const_view(*other.view_const_);
     171        proxy_m_ = &(view_const_->matrix);
     172      }
     173      else if (other.m_)
     174        proxy_m_ = m_ = other.create_gsl_matrix_copy();
     175
     176      // no need to delete blas_result_ if the number of rows fit, it
     177      // may be useful later.
     178      if (blas_result_ && (blas_result_->size1!=rows())) {
     179        gsl_matrix_free(blas_result_);
     180        blas_result_=NULL;
     181      }
     182    }
     183    return *this;
     184  }
     185
     186
     187  size_t matrix::columns(void) const
     188  {
     189    if (!proxy_m_)
     190      return 0;
     191    return proxy_m_->size2;
     192  }
     193
     194
     195  gsl_matrix* matrix::create_gsl_matrix_copy(void) const
     196  {
     197    gsl_matrix* m = gsl_matrix_alloc(rows(),columns());
     198    if (!m)
     199      throw utility::GSL_error("matrix::create_gsl_matrix_copy failed to allocate memory");
     200    if (gsl_matrix_memcpy(m,proxy_m_))
     201      throw utility::GSL_error("matrix::create_gsl_matrix_copy dimension mis-match");
     202    return m;
     203  }
     204
     205
     206  void matrix::delete_allocated_memory(void)
     207  {
    153208    if (view_)
    154209      delete view_;
     
    157212    else if (m_)
    158213      gsl_matrix_free(m_);
    159     if (blas_result_)
    160       gsl_matrix_free(blas_result_);
    161214    blas_result_=NULL;
    162     m_=NULL;
    163   }
    164 
    165 
    166   const matrix& matrix::clone(const matrix& other)
    167   {
    168     if (this!=&other) {
    169 
    170       if (view_)
    171         delete view_;
    172       else if (view_const_)
    173         delete view_const_;
    174       else if (m_)
    175         gsl_matrix_free(m_);
    176       proxy_m_=m_=NULL;
    177 
    178       if (other.view_) {
    179         view_ = new gsl_matrix_view(*other.view_);
    180         proxy_m_ = m_ = &(view_->matrix);
    181       }
    182       else if (other.view_const_) {
    183         view_const_ = new gsl_matrix_const_view(*other.view_const_);
    184         proxy_m_ = &(view_const_->matrix);
    185       }
    186       else if (other.m_)
    187         proxy_m_ = m_ = other.create_gsl_matrix_copy();
    188 
    189       // no need to delete blas_result_ if the number of rows fit, it
    190       // may be useful later.
    191       if (blas_result_ && (blas_result_->size1!=rows())) {
    192         gsl_matrix_free(blas_result_);
    193         blas_result_=NULL;
    194       }
    195     }
    196     return *this;
    197   }
    198 
    199 
    200   size_t matrix::columns(void) const
    201   {
    202     if (!proxy_m_)
    203       return 0;
    204     return proxy_m_->size2;
    205   }
    206 
    207 
    208   gsl_matrix* matrix::create_gsl_matrix_copy(void) const
    209   {
    210     gsl_matrix* m = gsl_matrix_alloc(rows(),columns());
    211     if (!m)
    212       throw utility::GSL_error("matrix::create_gsl_matrix_copy failed to allocate memory");
    213     if (gsl_matrix_memcpy(m,proxy_m_))
    214       throw utility::GSL_error("matrix::create_gsl_matrix_copy dimension mis-match");
    215     return m;
     215    proxy_m_=m_=NULL;
    216216  }
    217217
     
    271271  void matrix::resize(size_t r, size_t c, double init_value)
    272272  {
    273       if (view_)
    274         delete view_;
    275       else if (view_const_)
    276         delete view_const_;
    277       else if (m_)
    278         gsl_matrix_free(m_);
    279       proxy_m_ = m_ = gsl_matrix_alloc(r,c);
    280       if (!m_)
    281         throw utility::GSL_error("matrix::matrix failed to allocate memory");
    282       set_all(init_value);
    283 
    284       // no need to delete blas_result_ if the number of rows fit, it
    285       // may be useful later.
    286       if (blas_result_ && (blas_result_->size1!=rows())) {
    287         gsl_matrix_free(blas_result_);
    288         blas_result_=NULL;
    289       }
    290   }
     273    delete_allocated_memory();
     274
     275    proxy_m_ = m_ = gsl_matrix_alloc(r,c);
     276    if (!m_)
     277      throw utility::GSL_error("matrix::matrix failed to allocate memory");
     278    set_all(init_value);
     279
     280    // no need to delete blas_result_ if the number of rows fit, it
     281    // may be useful later.
     282    if (blas_result_ && (blas_result_->size1!=rows())) {
     283      gsl_matrix_free(blas_result_);
     284      blas_result_=NULL;
     285    }
     286  }
    291287
    292288
  • trunk/yat/utility/matrix.h

    r809 r810  
    421421    gsl_matrix* create_gsl_matrix_copy(void) const;
    422422
     423    /**
     424       \brief Clear all dynamically allocated memory.
     425
     426       Internal utility function.
     427    */
     428    void delete_allocated_memory(void);
     429
    423430    // blas_result_ is used to temporarily store result in BLAS calls
    424431    // and when not NULL it should always have the same size as
  • trunk/yat/utility/vector.cc

    r808 r810  
    190190  vector::~vector(void)
    191191  {
    192     if (view_)
    193       delete view_;
    194     else if (view_const_)
    195       delete view_const_;
    196     else if (v_)
    197       gsl_vector_free(v_);
     192    delete_allocated_memory();
    198193  }
    199194
     
    203198    if (this!=&other) {
    204199
    205       if (view_)
    206         delete view_;
    207       else if (view_const_)
    208         delete view_const_;
    209       else if (v_)
    210         gsl_vector_free(v_);
     200      delete_allocated_memory();
    211201
    212202      if (other.view_) {
     
    235225      throw utility::GSL_error("vector::create_gsl_matrix_copy dimension mis-match");
    236226    return vec;
     227  }
     228
     229
     230  void vector::delete_allocated_memory(void)
     231  {
     232    if (view_)
     233      delete view_;
     234    else if (view_const_)
     235      delete view_const_;
     236    else if (v_)
     237      gsl_vector_free(v_);
     238    proxy_v_=v_=NULL;
    237239  }
    238240
     
    293295  void vector::resize(size_t n, double init_value)
    294296  {
    295     if (view_)
    296       delete view_;
    297     else if (view_const_)
    298       delete view_const_;
    299     else if (v_)
    300       gsl_vector_free(v_);
     297    delete_allocated_memory();
    301298    proxy_v_ = v_ = gsl_vector_alloc(n);
    302 
    303299    if (!v_)
    304300      throw utility::GSL_error("vector::vector failed to allocate memory");
  • trunk/yat/utility/vector.h

    r809 r810  
    448448    gsl_vector* create_gsl_vector_copy(void) const;
    449449
     450    /**
     451       \brief Clear all dynamically allocated memory.
     452
     453       Internal utility function.
     454    */
     455    void delete_allocated_memory(void);
     456
    450457    gsl_vector* v_;
    451458    gsl_vector_view* view_;
Note: See TracChangeset for help on using the changeset viewer.