source: trunk/yat/utility/VectorView.h @ 1015

Last change on this file since 1015 was 1015, checked in by Peter, 14 years ago

changing class names vectorView => VectorView? and vectorBase => VectorBase?. Refs #256

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 KB
Line 
1#ifndef _theplu_yat_utility_vector_view_
2#define _theplu_yat_utility_vector_view_
3
4// $Id: VectorView.h 1015 2008-02-01 16:35:32Z peter $
5
6/*
7  Copyright (C) 2003 Daniel Dalevi, Peter Johansson
8  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
9  Copyright (C) 2005 Jari Häkkinen, Markus Ringnér, Peter Johansson
10  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér
11  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
12
13  This file is part of the yat library, http://trac.thep.lu.se/trac/yat
14
15  The yat library is free software; you can redistribute it and/or
16  modify it under the terms of the GNU General Public License as
17  published by the Free Software Foundation; either version 2 of the
18  License, or (at your option) any later version.
19
20  The yat library is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  General Public License for more details.
24
25  You should have received a copy of the GNU General Public License
26  along with this program; if not, write to the Free Software
27  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28  02111-1307, USA.
29*/
30
31#include "VectorBase.h"
32#include "Exception.h"
33
34#include <iostream>
35#include <vector>
36#include <utility>
37
38#include <gsl/gsl_vector.h>
39#include <gsl/gsl_sort_vector.h>
40
41namespace theplu {
42namespace yat {
43namespace utility {
44
45  class matrix;
46  class vector;
47
48  /**
49     @brief This is the yat interface to GSL vector.
50
51     For time being 'double' is the only type supported.
52
53     \par File streams:
54     Reading and writing vectors to file streams are of course
55     supported. These are implemented without using GSL functionality,
56     and thus binary read and write to streams are not supported.
57
58     \par Vector views:
59     GSL vector views are supported and these are disguised as
60     ordinary utility::vectors. A support function is added,
61     utility::vector::isview(), that can be used to check if a vector
62     object is a view. Note that view vectors do not own the
63     underlying data, and a view is not valid if the vector/matrix
64     owing the data is deallocated.
65
66     \par
67     Currently there is no restriction on how a vector is used when
68     the vector is a const view into another vector or matrix. To
69     avoid unexpected runtime errors, the programmer must declare
70     const view vectors as 'const' in order to get compile time
71     diagnostics about improper use of a const view vector object. If
72     'const' is not used and the const view vector is used erroneously
73     (such as on the left hand side in assignments), the compiler will
74     not catch the error and runtime error will occur. assert(3) is
75     used to catch the runtime error during development. Example on
76     bad and proper use of const view vectors:
77     @code
78  const vector vm(13,1.0);
79  vector v1(vm,2,4);       // bad code! not const!
80  v1(0)=-123;              // accepted by compiler, runtime abort will occur
81                           // or catched by assert depending on compiler flags
82  const vector v2(vm,2,4); // proper code
83  v2(0)=-123;              // not acceptable for the compiler
84     @endcode
85  */
86
87  class VectorView : public VectorBase
88  {
89  public:
90    /**
91       \brief Default constructor.
92    */
93    VectorView(void);
94
95    /**
96       \brief The copy constructor.
97    */ 
98    VectorView(VectorBase& other);
99    VectorView(VectorView& other);
100    // Peter, privatize
101    VectorView(const VectorView& other);
102
103    /**
104       \brief VectorView constructor.
105
106       Create a view of VectorView \a v, with starting index \a offset,
107       size \a n, and an optional \a stride.
108
109       A VectorView view can be used as any VectorView with the difference
110       that changes made to the view will also change the object that
111       is viewed. Also, using the copy constructor will create a new
112       VectorView object that is a copy of whatever is viewed. If a copy
113       of the view is needed then you should use this constructor to
114       obtain a copy.
115
116       \note If the object viewed by the view goes out of scope or is
117       deleted, the view becomes invalid and the result of further use
118       is undefined.
119
120       \throw GSL_error if a view cannot be set up.
121    */
122    VectorView(VectorBase& v, size_t offset, size_t n, size_t stride=1);
123    // Peter, privatize
124    VectorView(const VectorBase& v, size_t offset, size_t n, size_t stride=1);
125
126    ///
127    /// Matrix row/column view constructor.
128    ///
129    /// Create a row/column VectorView view of matrix \a m, pointing at
130    /// row/column \a i. The parameter \a row is used to set whether
131    /// the view should be a row or column view. If \a row is set to
132    /// true, the view will be a row view (default behaviour), and,
133    /// naturally, a column view otherwise.
134    ///
135    /// A VectorView view can be used as any VectorView with the difference
136    /// that changes made to the view will also change the object that
137    /// is viewed. Also, using the copy constructor will create a new
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
140    /// constructor to obtain a copy.
141    ///
142    /// @note If the object viewed by the view goes out of scope or is
143    /// deleted, the view becomes invalid and the result of further
144    /// use is undefined.
145    ///
146    VectorView(matrix& m, size_t i, bool row=true);
147
148    // Peter should be private
149    VectorView(const matrix& m, size_t i, bool row=true);
150
151    /**
152     */
153    //VectorView(proxy p);
154
155    ///
156    /// The destructor.
157    ///
158    ~VectorView(void);
159
160    /**
161       \return true
162    */
163    bool isview(void) const; 
164
165    /**
166       \brief The assignment operator.
167
168       \return A const reference to the resulting vector.
169
170       \throw GSL_error if dimensions mis-match.
171    */
172    const VectorBase& operator=(const VectorBase&);
173    const VectorBase& operator=(const VectorView&);
174
175    //const VectorBase& operator=(proxy);
176
177  private:
178    const VectorBase& assign(const VectorBase& other);
179    void delete_allocated_memory(void);
180
181    gsl_vector_view* view_;
182    gsl_vector_const_view* const_view_;
183   
184  };
185
186}}} // of namespace utility, yat, and theplu
187
188#endif
Note: See TracBrowser for help on using the repository browser.