Changeset 783


Ignore:
Timestamp:
Mar 5, 2007, 10:45:32 PM (15 years ago)
Author:
Jari Häkkinen
Message:

Fixes #155. Added asserts to check vectors in all non-const functions since no inline functions exists anymore.

Location:
trunk/yat/utility
Files:
2 edited

Legend:

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

    r782 r783  
    3030#include "utility.h"
    3131
     32#include <cassert>
    3233#include <iostream>
    3334#include <sstream>
     35#include <utility>
    3436#include <vector>
    35 #include <utility>
    3637
    3738namespace theplu {
     
    211212  void vector::div(const vector& other)
    212213  {
     214    assert(v_); assert(other.v_);
    213215    int status=gsl_vector_div(v_,other.v_);
    214216    if (status)
     
    237239  void vector::mul(const vector& other)
    238240  {
     241    assert(v_); assert(other.v_);
    239242    int status=gsl_vector_mul(v_,other.v_);
    240243    if (status)
     
    245248  void vector::reverse(void)
    246249  {
     250    assert(v_);
    247251    gsl_vector_reverse(v_);
    248252  }
     
    251255  void vector::set(const vector& vec)
    252256  {
     257    assert(v_); assert(vec.v_);
    253258    if (gsl_vector_memcpy(v_,vec.v_))
    254259      throw utility::GSL_error("vector::set dimension mis-match");
     
    258263  void vector::set_all(const double& value)
    259264  {
     265    assert(v_);
    260266    gsl_vector_set_all(v_,value);
    261267  }
     
    272278  void vector::swap(size_t i, size_t j)
    273279  {
     280    assert(v_);
    274281    int status=gsl_vector_swap_elements(v_, i, j);
    275282    if (status)
     
    280287  double& vector::operator()(size_t i)
    281288  {
     289    assert(v_);
    282290    double* d=gsl_vector_ptr(v_, i);
    283291    if (!d)
     
    348356  const vector& vector::operator+=(const vector& other)
    349357  {
    350     int status=gsl_vector_add(v_,other. v_);
     358    assert(v_);
     359    int status=gsl_vector_add(v_, other.v_);
    351360    if (status)
    352361      throw utility::GSL_error(std::string("vector::add", status));
     
    357366  const vector& vector::operator+=(double d)
    358367  {
     368    assert(v_);
    359369    gsl_vector_add_constant(v_, d);
    360370    return *this;
     
    364374  const vector& vector::operator-=(const vector& other)
    365375  {
     376    assert(v_);
    366377    int status=gsl_vector_sub(v_, other.v_);
    367378    if (status)
     
    373384  const vector& vector::operator*=(const double d)
    374385  {
     386    assert(v_);
    375387    gsl_vector_scale(v_, d);
    376388    return *this;
     
    426438  void set_basis(vector& v, size_t i)
    427439  {
     440    assert(v.gsl_vector_p());
    428441    gsl_vector_set_basis(v.gsl_vector_p(),i);
    429442  }
     
    432445  void sort(vector& v)
    433446  {
     447    assert(v.gsl_vector_p());
    434448    gsl_sort_vector(v.gsl_vector_p());
    435449  }
     
    448462  void swap(vector& v, vector& w)
    449463  {
     464    assert(v.gsl_vector_p()); assert(w.gsl_vector_p());
    450465    int status=gsl_vector_swap(v.gsl_vector_p(),w.gsl_vector_p());
    451466    if (status)
  • trunk/yat/utility/vector.h

    r782 r783  
    6363     \par
    6464     Currently there is no restriction on how a vector is used when
    65      the vector is a const view into another vector or
    66      matrix. To avoid unexpected runtime errors, the programmer must
    67      declare const view vectors as const in order to get compile time
    68      diagnostics about improper use of a const view vector object. An
    69      example of improper use of a const view vector is assignment of
    70      values to an element in the object. If the const view object was
    71      declared const the assigment will be caught by the compiler
    72      whereas it will cause a (un-catchable) runtime error. Example:
     65     the vector is a const view into another vector or matrix. To
     66     avoid unexpected runtime errors, the programmer must declare
     67     const view vectors as 'const' in order to get compile time
     68     diagnostics about improper use of a const view vector object. If
     69     'const' is not used and the const view vector is used erroneously
     70     (such as on the left hand side in assignments), the compiler will
     71     not catch the error and runtime error will occur. assert(3) is
     72     used to catch the runtime error during development. Example on
     73     bad and proper use of const view vectors:
    7374     @code
    7475  const vector vm(13,1.0);
    7576  vector v1(vm,2,4);       // bad code! not const!
    7677  v1(0)=-123;              // accepted by compiler, runtime abort will occur
     78                           // or catched by assert depending on compiler flags
    7779  const vector v2(vm,2,4); // proper code
    7880  v2(0)=-123;              // not acceptable for the compiler
     
    415417    gsl_vector_const_view* view_const_;
    416418    // proxy_v_ is used to access the proper underlying v_ or v_const_
    417     // in all const member functions.
     419    // in all const member functions. It is not used by design for
     420    // non-const vector functions and operators. This is to make sure
     421    // that runtime errors occur if a const vector is used in an
     422    // inappropriate manner such as on left hand side in assignment
     423    // (remember, v_ is null for const vector views).
    418424    const gsl_vector* proxy_v_;
    419425  };
Note: See TracChangeset for help on using the changeset viewer.