Changeset 1135


Ignore:
Timestamp:
Feb 24, 2008, 12:40:27 AM (16 years ago)
Author:
Peter
Message:

fixes self assignment in Vector - fixes #339

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/vector_test.cc

    r1132 r1135  
    314314  }
    315315
    316   /*
    317316  {
    318317    utility::Vector vec(10, 2.3);
     
    338337    }
    339338  }
    340   */
    341339
    342340  // test for ticket:285
  • trunk/yat/utility/Vector.cc

    r1130 r1135  
    156156  const Vector& Vector::assign(const VectorBase& other)
    157157  {
     158    // avoid self assignment
     159    if (this == &other)
     160      return *this;
     161    if (!other.size()){
     162      delete_allocated_memory();
     163      return *this;
     164    }
     165    // indirect self assignment if begin <= other.begin() < end
     166    if (begin() <= other.begin() && other.begin()<end()) {
     167      if (size() != other.size()) {
     168        gsl_vector* tmp = create_gsl_vector_copy(other);
     169        delete_allocated_memory();
     170        const_vec_ = vec_ = tmp;
     171      }
     172      return *this;
     173    }
    158174    delete_allocated_memory();
    159     if (other.size()) {
    160       if (size()!=other.size())
    161         vec_ = gsl_vector_alloc(other.size());
    162       if (!vec_)
    163         throw utility::GSL_error("Vector failed to allocate memory");
    164       if (gsl_vector_memcpy(vec_, other.gsl_vector_p()))
    165         throw utility::GSL_error("Vector::assign memcpy failed.");
    166       const_vec_ = vec_;
    167     }
     175    vec_ = create_gsl_vector_copy(other);
     176    const_vec_ = vec_;
    168177    return *this;
    169178  }
Note: See TracChangeset for help on using the changeset viewer.