Ignore:
Timestamp:
Nov 30, 2007, 11:55:30 PM (14 years ago)
Author:
Peter
Message:

Splitting vector class into three different classes for vector, view and const_view, and adding abstract classes appropriately. NOTE, the tests are not going through, not even compiling; sorry about that

File:
1 copied

Legend:

Unmodified
Added
Removed
  • branches/peter-dev/yat/utility/vectorView.h

    r994 r995  
    1 #ifndef _theplu_yat_utility_vector_
    2 #define _theplu_yat_utility_vector_
     1#ifndef _theplu_yat_utility_vector_view_
     2#define _theplu_yat_utility_vector_view_
    33
    44// $Id$
     
    2929*/
    3030
     31#include "vectorMutable.h"
     32
    3133#include "Exception.h"
    3234#include "Iterator.h"
     
    4446
    4547  class matrix;
     48  class vector;
    4649
    4750  /**
     
    8487  */
    8588
    86   class vector
     89  class vectorView : public vectorMutable
    8790  {
    8891  public:
    89 
    90     /// \brief vector::iterator
    91     typedef Iterator<double&, vector> iterator;
    92     /// \brief vector::const_iterator
    93     typedef Iterator<const double, const vector> const_iterator;
     92    /// \brief vectorView::iterator
     93    typedef Iterator<double&, vectorView> iterator;
    9494
    9595    /**
    96        \brief The default constructor.
     96       \brief Default constructor.
    9797    */
    98     vector(void);
    99 
    100     /**
    101        \brief Allocates memory space for \a n elements, and sets all
    102        elements to \a init_value.
    103        
    104        \throw GSL_error if memory allocation fails.
    105     */
    106     vector(size_t n, double init_value=0);
     98    vectorView(void);
    10799
    108100    /**
    109101       \brief The copy constructor.
    110 
    111        \note If the object to be copied is a vector view, the values
    112        of the view will be copied, i.e. the view is not copied.
    113 
    114        \throw A GSL_error is indirectly thrown if memory allocation
    115        fails.
    116102    */
    117     vector(const vector& other);
     103    vectorView(vectorMutable& other);
    118104
    119105    /**
    120        \brief Vector view constructor.
     106       \brief VectorView constructor.
    121107
    122        Create a view of vector \a v, with starting index \a offset,
     108       Create a view of vectorView \a v, with starting index \a offset,
    123109       size \a n, and an optional \a stride.
    124110
    125        A vector view can be used as any vector with the difference
     111       A vectorView view can be used as any vectorView with the difference
    126112       that changes made to the view will also change the object that
    127113       is viewed. Also, using the copy constructor will create a new
    128        vector object that is a copy of whatever is viewed. If a copy
     114       vectorView object that is a copy of whatever is viewed. If a copy
    129115       of the view is needed then you should use this constructor to
    130116       obtain a copy.
     
    136122       \throw GSL_error if a view cannot be set up.
    137123    */
    138     vector(vector& v, size_t offset, size_t n, size_t stride=1);
    139 
    140     /**
    141        \brief Vector const view constructor.
    142 
    143        Create a view of vector \a v, with starting index \a offset,
    144        size \a n, and an optional \a stride.
    145 
    146        A vector view can be used as any const vector. Using the copy
    147        constructor will create a new vector object that is a copy of
    148        whatever is viewed. If a copy of the view is needed then you
    149        should use this constructor to obtain a copy.
    150 
    151        \note If the object viewed by the view goes out of scope or is
    152        deleted, the view becomes invalid and the result of further use
    153        is undefined.
    154 
    155        \throw GSL_error if a view cannot be set up.
    156     */
    157     vector(const vector& v, size_t offset, size_t n, size_t stride=1);
     124    vectorView(vectorMutable& v, size_t offset, size_t n, size_t stride=1);
    158125
    159126    ///
    160127    /// Matrix row/column view constructor.
    161128    ///
    162     /// Create a row/column vector view of matrix \a m, pointing at
     129    /// Create a row/column vectorView view of matrix \a m, pointing at
    163130    /// row/column \a i. The parameter \a row is used to set whether
    164131    /// the view should be a row or column view. If \a row is set to
     
    166133    /// naturally, a column view otherwise.
    167134    ///
    168     /// A vector view can be used as any vector with the difference
     135    /// A vectorView view can be used as any vectorView with the difference
    169136    /// that changes made to the view will also change the object that
    170137    /// is viewed. Also, using the copy constructor will create a new
    171     /// vector object that is a copy of whatever is viewed. If a copy
    172     /// of the view is needed then you should use the vector view
     138    /// vectorView object that is a copy of whatever is viewed. If a copy
     139    /// of the view is needed then you should use the vectorView view
    173140    /// constructor to obtain a copy.
    174141    ///
     
    177144    /// use is undefined.
    178145    ///
    179     vector(matrix& m, size_t i, bool row=true);
    180 
    181     ///
    182     /// Matrix row/column const view constructor.
    183     ///
    184     /// Create a row/column vector view of matrix \a m, pointing at
    185     /// row/column \a i. The parameter \a row is used to set whether
    186     /// the view should be a row or column view. If \a row is set to
    187     /// true, the view will be a row view (default behaviour), and,
    188     /// naturally, a column view otherwise.
    189     ///
    190     /// A const vector view can be used as any const vector. Using the
    191     /// copy constructor will create a new vector object that is a
    192     /// copy of whatever is viewed. If a copy of the view is needed
    193     /// then you should use the vector view constructor to obtain a
    194     /// copy.
    195     ///
    196     /// @note If the object viewed by the view goes out of scope or is
    197     /// deleted, the view becomes invalid and the result of further
    198     /// use is undefined.
    199     ///
    200     vector(const matrix& m, size_t i, bool row=true);
     146    vectorView(matrix& m, size_t i, bool row=true);
    201147
    202148    /**
    203        \brief The istream constructor.
    204 
    205        Either elements should be separated with white space characters
    206        (default), or elements should be separated by the delimiter \a
    207        sep. When delimiter \a sep is used empty elements are stored as
    208        NaN's (except that empty lines are ignored). The end of input
    209        to the vector is at end of file marker.
    210 
    211        \throw GSL_error if memory allocation fails, IO_error if
    212        unexpected input is found in the input stream.
    213     */
    214     explicit vector(std::istream &, char sep='\0')
    215       throw(utility::IO_error, std::exception);
     149     */
     150    vectorView(proxy p);
    216151
    217152    ///
    218153    /// The destructor.
    219154    ///
    220     ~vector(void);
    221 
    222     /**
    223        \return mutable iterator to start of vector
    224      */
    225     iterator begin(void);
    226 
    227     /**
    228        \return read-only iterator to start of vector
    229      */
    230     const_iterator begin(void) const;
    231 
    232     /**
    233        \brief Make a copy of \a other.
    234 
    235        This function will make a deep copy of \a other. Memory is
    236        resized and view state is changed if needed.
    237     */
    238     const vector& clone(const vector& other);
    239 
    240     /**
    241        \brief This function performs element-wise division, \f$ this_i =
    242        this_i/other_i \; \forall i \f$.
    243 
    244        \throw GSL_error if dimensions mis-match.
    245     */
    246     void div(const vector& other);
    247 
    248     /**
    249        \return mutable iterator to end of vector
    250      */
    251     iterator end(void);
    252 
    253     /**
    254        \return read-only iterator to end of vector
    255      */
    256     const_iterator end(void) const;
    257 
    258     /**
    259        \brief Check whether vectors are equal within a user defined
    260        precision, set by \a precision.
    261 
    262        \return True if each element deviates less or equal than \a
    263        d. If any vector contain a NaN, false is always returned.
    264 
    265        \see operator== and operator!=
    266     */
    267     bool equal(const vector&, const double precision=0) const;
    268 
    269     ///
    270     /// @return A const pointer to the internal GSL vector,
    271     ///
    272     const gsl_vector* gsl_vector_p(void) const;
    273 
    274     ///
    275     /// @return A pointer to the internal GSL vector,
    276     ///
    277     gsl_vector* gsl_vector_p(void);
    278 
    279     ///
    280     /// Check if the vector object is a view (sub-vector) to another
    281     /// vector.
    282     ///
    283     /// @return True if the object is a view, false othwerwise.
    284     ///
    285     bool isview(void) const;
    286 
    287     /**
    288        \brief This function performs element-wise multiplication, \f$
    289        this_i = this_i * other_i \; \forall i \f$.
    290 
    291        \throw GSL_error if dimensions mis-match.
    292     */
    293     void mul(const vector& other);
    294 
    295     /**
    296        @brief Resize vector
    297        
    298        All elements are set to @a init_value.
    299 
    300        \note Underlying GSL vector is destroyed and a view into this
    301        vector becomes invalid.
    302     */
    303     void resize(size_t, double init_value=0);
    304 
    305     /**
    306        \brief Reverse the order of elements in the vector.
    307     */
    308     void reverse(void);
    309 
    310     ///
    311     /// Set all elements to \a value.
    312     ///
    313     void all(const double& value);
    314 
    315     ///
    316     /// @return the number of elements in the vector.
    317     ///
    318     size_t size(void) const;
    319 
    320     /**
    321        \brief Exchange elements \a i and \a j.
    322 
    323        \throw GSL_error if vector lengths differs.
    324     */
    325     void swap(size_t i, size_t j);
    326 
    327     /**
    328        \brief Element access operator.
    329 
    330        \return Reference to element \a i.
    331 
    332        \throw If GSL range checks are enabled in the underlying GSL
    333        library a GSL_error exception is thrown if either index is out
    334        of range.
    335     */
    336     double& operator()(size_t i);
    337 
    338     /**
    339        \brief Element access operator.
    340 
    341        \return Const reference to element \a i.
    342 
    343        \throw If GSL range checks are enabled in the underlying GSL
    344        library a GSL_error exception is thrown if either index is out
    345        of range.
    346     */
    347     const double& operator()(size_t i) const;
    348 
    349     ///
    350     /// Element access operator.
    351     ///
    352     /// @return Reference to element \a i.
    353     ///
    354     double& operator[](size_t i);
    355 
    356     ///
    357     /// Const element access operator.
    358     ///
    359     /// @return The value of element \a i.
    360     ///
    361     const double& operator[](size_t i) const;
    362 
    363     /**
    364        \brief Comparison operator. Takes linear time.
    365 
    366        Checks are performed with exact matching, i.e., rounding off
    367        effects may destroy comparison. Use the equal function for
    368        comparing elements within a user defined precision.
    369 
    370        \return True if all elements are equal otherwise false.
    371 
    372        \see equal(const vector&, const double precision=0)
    373     */
    374     bool operator==(const vector&) const;
    375 
    376     /**
    377        \brief Comparison operator. Takes linear time.
    378 
    379        Checks are performed with exact matching, i.e., rounding off
    380        effects may destroy comparison. Use the equal function for
    381        comparing elements within a user defined precision.
    382 
    383        \return False if all elements are equal otherwise true.
    384 
    385        \see equal(const vector&, const double precision=0)
    386     */
    387     bool operator!=(const vector&) const;
    388 
    389     ///
    390     /// @return The dot product.
    391     ///
    392     double operator*(const vector&) const;
     155    ~vectorView(void);
    393156
    394157    /**
    395158       \brief The assignment operator.
    396 
    397        Dimensions of the vectors must match. If the LHS vector is a
    398        view, the underlying data will be changed.
    399 
    400        \return A const reference to the resulting vector.
    401 
    402        \see void set(const vector&).
    403 
    404        \throw GSL_error if dimensions mis-match.
    405     */
    406     const vector& operator=(const vector&);
    407 
    408     /**
    409        \brief Addition and assign operator. Vector addition, \f$
    410        this_i = this_i + other_i \; \forall i \f$.
    411159
    412160       \return A const reference to the resulting vector.
     
    414162       \throw GSL_error if dimensions mis-match.
    415163    */
    416     const vector& operator+=(const vector&);
     164    const vectorMutable& operator=(vectorMutable&);
    417165
    418     /**
    419        \brief Add a constant to a vector, \f$ this_i = this_i + d \;
    420        \forall i \f$.
    421 
    422        \return A const reference to the resulting vector.
    423     */
    424     const vector& operator+=(double d);
    425 
    426     /**
    427        \brief Subtract and assign operator. Vector subtraction, \f$
    428        this_i = this_i - other_i \; \forall i \f$.
    429 
    430        \return A const reference to the resulting vector.
    431 
    432        \throw GSL_error if dimensions mis-match.
    433     */
    434     const vector& operator-=(const vector&);
    435 
    436     /**
    437        \brief Subtract a constant to a vector, \f$ this_i = this_i - d
    438        \; \forall i \f$.
    439 
    440        \return A const reference to the resulting vector.
    441     */
    442     const vector& operator-=(double d);
    443 
    444     /**
    445        \brief Multiply with scalar and assign operator, \f$ this_i =
    446        this_i * d \; \forall i \f$.
    447 
    448        \return A const reference to the resulting vector.
    449     */
    450     const vector& operator*=(double d);
    451 
     166    const vectorMutable& operator=(proxy);
    452167
    453168  private:
     169    void clean_up(void);
    454170
    455     /**
    456        \brief Create a new copy of the internal GSL vector.
    457 
    458        Necessary memory for the new GSL vector is allocated and the
    459        caller is responsible for freeing the allocated memory.
    460 
    461        \return A pointer to a copy of the internal GSL vector.
    462 
    463        \throw GSL_error if memory cannot be allocated for the new
    464        copy, or if dimensions mis-match.
    465     */
    466     gsl_vector* create_gsl_vector_copy(void) const;
    467 
    468     /**
    469        \brief Clear all dynamically allocated memory.
    470 
    471        Internal utility function.
    472     */
    473     void delete_allocated_memory(void);
    474 
    475     gsl_vector* v_;
    476171    gsl_vector_view* view_;
    477     const gsl_vector_const_view* view_const_;
    478     // proxy_v_ is used to access the proper underlying gsl_vector
    479     // in all const member functions. It is not used by design for
    480     // non-const vector functions and operators. This is to make sure
    481     // that runtime errors occur if a const vector is used in an
    482     // inappropriate manner such as on left hand side in assignment
    483     // (remember, v_ is null for const vector views).
    484     const gsl_vector* proxy_v_;
     172   
    485173  };
    486 
    487   /**
    488      \brief Check if all elements of the vector are zero.
    489 
    490      \return True if all elements in the vector is zero, false
    491      othwerwise.
    492   */
    493   bool isnull(const vector&);
    494 
    495   /**
    496      \brief Get the maximum value of the vector.
    497 
    498      \return The maximum value of the vector.
    499   */
    500   double max(const vector&);
    501 
    502   /**
    503      \brief Locate the maximum value in the vector.
    504 
    505      \return The index to the maximum value of the vector.
    506 
    507      \note Lower index has precedence.
    508   */
    509   size_t max_index(const vector&);
    510 
    511   /**
    512      \brief Get the minimum value of the vector.
    513 
    514      \return The minimum value of the vector.
    515   */
    516   double min(const vector&);
    517 
    518   /**
    519      \brief Locate the minimum value in the vector.
    520 
    521      \return The index to the minimum value of the vector.
    522 
    523      \note Lower index has precedence.
    524   */
    525   size_t min_index(const vector&);
    526 
    527   /**
    528      \brief Create a vector \a flag indicating NaN's in another vector
    529      \a templat.
    530 
    531      The \a flag vector is changed to contain 1's and 0's only. A 1
    532      means that the corresponding element in the \a templat vector is
    533      valid and a zero means that the corresponding element is a NaN.
    534 
    535      \note Space for vector \a flag is reallocated to fit the size of
    536      vector \a templat if sizes mismatch.
    537 
    538      \return True if the \a templat vector contains at least one NaN.
    539   */
    540   bool nan(const vector& templat, vector& flag);
    541 
    542   /**
    543      \brief Transforms a vector to a basis vector.
    544 
    545      All elements are set to zero except the \a i-th element which is
    546      set to one.
    547   */
    548   void set_basis(vector&, size_t i);
    549 
    550   /**
    551      Randomly shuffles the elements in vector \a invec
    552   */
    553   void shuffle(vector& invec);
    554 
    555   /**
    556      Sort the elements in the vector.
    557   */
    558   void sort(vector&);
    559 
    560   /**
    561      Create a vector \a sort_index containing the indeces of
    562      elements in a another vector \a invec.  The elements of \a
    563      sort_index give the index of the vector element which would
    564      have been stored in that position if the vector had been sorted
    565      in place. The first element of \a sort_index gives the index of the least
    566      element in \a invec, and the last element of \a sort_index gives the index of the
    567      greatest element in \a invec . The vector \a invec is not changed.
    568 
    569   */
    570   void sort_index(std::vector<size_t>& sort_index, const vector& invec);
    571 
    572   /** Similar to sort_index but creates a vector with indices to the \a k
    573   smallest elements in \a invec. 
    574   */
    575   void sort_smallest_index(std::vector<size_t>& sort_index, size_t k, const
    576   vector& invec);
    577 
    578   /** Similar to sort_index but creates a vector with indices to the \a k
    579   largest elements in \a invec. 
    580   */
    581   void sort_largest_index(std::vector<size_t>& sort_index, size_t k, const
    582   vector& invec);
    583 
    584  
    585 
    586   /**
    587      \brief Calculate the sum of all vector elements.
    588 
    589      \return The sum.
    590   */
    591   double sum(const vector&);
    592 
    593   /**
    594      \brief Swap vector elements by copying.
    595 
    596      The two vectors must have the same length.
    597 
    598      \throw GSL_error if vector lengths differs.
    599   */
    600   void swap(vector&, vector&);
    601 
    602   /**
    603      \brief The output operator for the vector class.
    604   */
    605   std::ostream& operator<<(std::ostream&, const vector&);
    606174
    607175}}} // of namespace utility, yat, and theplu
Note: See TracChangeset for help on using the changeset viewer.