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

Last change on this file since 1027 was 1027, checked in by Peter, 15 years ago

going back to previous design in which view and const_view are in different classes. Having them in same didnt work as expected. There is a problem converting vector::iterator to vector::const_iterator. I'll open a separate ticket for this issue.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1#ifndef _theplu_yat_utility_vector_view_
2#define _theplu_yat_utility_vector_view_
3
4// $Id: VectorView.h 1027 2008-02-02 21:29:29Z 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 "VectorMutable.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 VectorMutable
88  {
89  public:
90    /**
91       \brief Default constructor.
92    */
93    VectorView(void);
94
95    /**
96       \brief The copy constructor.
97    */ 
98    VectorView(VectorMutable& other);
99    VectorView(VectorView& other);
100
101    /**
102       \brief VectorView constructor.
103
104       Create a view of VectorView \a v, with starting index \a offset,
105       size \a n, and an optional \a stride.
106
107       A VectorView view can be used as any VectorView with the difference
108       that changes made to the view will also change the object that
109       is viewed. Also, using the copy constructor will create a new
110       VectorView object that is a copy of whatever is viewed. If a copy
111       of the view is needed then you should use this constructor to
112       obtain a copy.
113
114       \note If the object viewed by the view goes out of scope or is
115       deleted, the view becomes invalid and the result of further use
116       is undefined.
117
118       \throw GSL_error if a view cannot be set up.
119    */
120    VectorView(VectorMutable& v, size_t offset, size_t n, size_t stride=1);
121
122    ///
123    /// Matrix row/column view constructor.
124    ///
125    /// Create a row/column VectorView view of matrix \a m, pointing at
126    /// row/column \a i. The parameter \a row is used to set whether
127    /// the view should be a row or column view. If \a row is set to
128    /// true, the view will be a row view (default behaviour), and,
129    /// naturally, a column view otherwise.
130    ///
131    /// A VectorView view can be used as any VectorView with the difference
132    /// that changes made to the view will also change the object that
133    /// is viewed. Also, using the copy constructor will create a new
134    /// VectorView object that is a copy of whatever is viewed. If a copy
135    /// of the view is needed then you should use the VectorView view
136    /// constructor to obtain a copy.
137    ///
138    /// @note If the object viewed by the view goes out of scope or is
139    /// deleted, the view becomes invalid and the result of further
140    /// use is undefined.
141    ///
142    VectorView(matrix& m, size_t i, bool row=true);
143
144    /**
145     */
146    VectorView(proxy p);
147
148    ///
149    /// The destructor.
150    ///
151    ~VectorView(void);
152
153    /**
154       \return true
155    */
156    bool isview(void) const; 
157
158    /**
159       \brief The assignment operator.
160
161       \return A const reference to the resulting vector.
162
163       \throw GSL_error if dimensions mis-match.
164    */
165    const VectorView& operator=(const VectorBase&);
166    const VectorView& operator=(const VectorView&);
167
168  private:
169    const VectorView& assign(const VectorBase& other);
170    void delete_allocated_memory(void);
171
172    gsl_vector_view* view_;
173    gsl_vector_const_view* const_view_;
174   
175  };
176
177}}} // of namespace utility, yat, and theplu
178
179#endif
Note: See TracBrowser for help on using the repository browser.