Ignore:
Timestamp:
Oct 17, 2006, 1:33:46 AM (15 years ago)
Author:
Jari Häkkinen
Message:

Fixes #17. Constness of vectors and views are stricltly
maintained. Improper use of const view vectors will cause runtime
error. See vector class documentation for more information.

File:
1 edited

Legend:

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

    r680 r686  
    4343  class matrix;
    4444
    45   ///
    46   /// This is the yat interface to GSL vector. 'double' is the
    47   /// only type supported, maybe we should add a 'complex' type as
    48   /// well in the future.
    49   ///
    50   /// \par[File streams] Reading and writing vectors to file streams
    51   /// are of course supported. These are implemented without using GSL
    52   /// functionality, and thus binary read and write to streams are not
    53   /// supported.
    54   ///
    55   /// \par[Vector views] GSL vector views are supported and these are
    56   /// disguised as ordinary utility::vectors. A support function is
    57   /// added, utility::vector::isview(), that can be used to check if a
    58   /// vector object is a view. Note that view vectors do not own the
    59   /// undelying data, and a view is not valid if the vector owning the
    60   /// data is deallocated.
    61   ///
    62   /// @note Missing support to underlying GSL vector features can in
    63   /// principle be included to this class if requested by the user
    64   /// community.
    65   ///
     45  /**
     46     This is the yat interface to GSL vector. 'double' is the only
     47     type supported, maybe we should add a 'complex' type as well in
     48     the future.
     49
     50     \par File streams:
     51     Reading and writing vectors to file streams are of course
     52     supported. These are implemented without using GSL functionality,
     53     and thus binary read and write to streams are not supported.
     54
     55     \par Vector views:
     56     GSL vector views are supported and these are disguised as
     57     ordinary utility::vectors. A support function is added,
     58     utility::vector::isview(), that can be used to check if a vector
     59     object is a view. Note that view vectors do not own the
     60     underlying data, and a view is not valid if the vector owing the
     61     data is deallocated.
     62
     63     \par
     64     Currently there is no restriction on how a vector is used in
     65     cases when 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:
     73     @code
     74  const vector vm(13,1.0);
     75  vector v1(vm,2,4);       // bad code!
     76  v1(0)=-123;              // accepted by compiler, runtime failure will occur
     77  const vector v2(vm,2,4);
     78  v2(0)=-123;              // not acceptable for the compiler
     79     @endcode
     80
     81     @note Missing support to underlying GSL vector features can in
     82     principle be included to this class if requested by the user
     83     community.
     84  */
     85
    6686  class vector
    6787  {
     
    7191    /// The default constructor.
    7292    ///
    73     inline vector(void) : v_(NULL), view_(NULL), const_view_(NULL) {}
     93    vector(void);
    7494
    7595    ///
     
    7797    /// sets all elements to \a init_value.
    7898    ///
    79     inline vector(const size_t n,const double init_value=0)
    80       : view_(NULL), const_view_(NULL)
    81       { v_ = gsl_vector_alloc(n); set_all(init_value); }
     99    vector(size_t n, double init_value=0);
    82100
    83101    ///
     
    87105    /// of the view will be copied, i.e. the view is not copied.
    88106    ///
    89     inline vector(const vector& other) : view_(NULL), const_view_(NULL)
    90       { v_ = other.create_gsl_vector_copy(); }
     107    vector(const vector& other);
    91108
    92109    ///
     
    178195    /// end of input to the vector is at end of file marker.
    179196    ///
    180     explicit vector(std::istream &, char sep='\0') throw (utility::IO_error,std::exception);
    181 
     197    explicit vector(std::istream &, char sep='\0')
     198      throw(utility::IO_error, std::exception);
    182199
    183200    ///
     
    215232    /// @return A const pointer to the internal GSL vector,
    216233    ///
    217     inline const gsl_vector* gsl_vector_p(void) const { return v_; }
     234    inline const gsl_vector* gsl_vector_p(void) const { return proxy_v_; }
    218235
    219236    ///
     
    226243    /// othwerwise;
    227244    ///
    228     inline bool isnull(void) const { return gsl_vector_isnull(v_); }
     245    inline bool isnull(void) const { return gsl_vector_isnull(proxy_v_); }
    229246
    230247    ///
     
    234251    /// @return True if the object is a view, false othwerwise.
    235252    ///
    236     inline bool isview(void) const { return view_ || const_view_; }
     253    inline bool isview(void) const { return view_ || view_const_; }
    237254
    238255    ///
     
    240257    ///
    241258    // Jari, doxygen group as Finding maximum and minimum elements
    242     inline double max(void) const { return gsl_vector_max(v_); }
     259    inline double max(void) const { return gsl_vector_max(proxy_v_); }
    243260
    244261    ///
     
    247264    ///
    248265    // Jari, doxygen group as Finding maximum and minimum elements
    249     inline size_t max_index(void) const { return gsl_vector_max_index(v_); }
     266    inline size_t
     267    max_index(void) const { return gsl_vector_max_index(proxy_v_); }
    250268
    251269    ///
     
    253271    ///
    254272    // Jari, doxygen group as Finding maximum and minimum elements
    255     inline double min(void) const { return gsl_vector_min(v_); }
     273    inline double min(void) const { return gsl_vector_min(proxy_v_); }
    256274
    257275    ///
     
    260278    ///
    261279    // Jari, doxygen group as Finding maximum and minimum elements
    262     inline size_t min_index(void) const { return gsl_vector_min_index(v_); }
     280    inline size_t
     281    min_index(void) const { return gsl_vector_min_index(proxy_v_); }
    263282
    264283    ///
     
    338357    /// @return the number of elements in the vector.
    339358    ///
    340     inline size_t size(void) const { return v_->size; }
     359    inline size_t size(void) const { return proxy_v_->size; }
    341360
    342361    ///
     
    346365    ///
    347366    // Markus to Jari, doxygen group as Exchanging elements ????
    348     inline void sort(void) { gsl_sort_vector(v_);}
    349 
     367    inline void sort(void) { gsl_sort_vector(v_); }
    350368
    351369    ///
     
    396414    // Jari, doxygen group as Accessing vector elements
    397415    inline const double&
    398     operator()(size_t i) const { return *gsl_vector_const_ptr(v_,i); }
     416    operator()(size_t i) const { return *gsl_vector_const_ptr(proxy_v_,i); }
    399417
    400418    ///
     
    413431    // Jari, doxygen group as Accessing vector elements
    414432    inline const double&
    415     operator[](size_t i) const { return *gsl_vector_const_ptr(v_,i); }
     433    operator[](size_t i) const { return *gsl_vector_const_ptr(proxy_v_,i); }
    416434
    417435    ///
     
    481499
    482500    gsl_vector* v_;
     501    const gsl_vector* v_const_;
    483502    gsl_vector_view* view_;
    484     gsl_vector_const_view* const_view_;
     503    gsl_vector_const_view* view_const_;
     504    const gsl_vector* proxy_v_;
    485505  };
    486506
     
    491511
    492512
    493 }}} // of namespace utility, yat and theplu
     513}}} // of namespace utility, yat, and theplu
    494514
    495515#endif
Note: See TracChangeset for help on using the changeset viewer.