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

Last change on this file since 1651 was 1651, checked in by Peter, 12 years ago

fixes #467 - implicit conversion between Vector and its Views are no longer allowed

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.1 KB
Line 
1#ifndef _theplu_yat_utility_vector_view_
2#define _theplu_yat_utility_vector_view_
3
4// $Id: VectorView.h 1651 2008-12-15 12:57:43Z 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, Peter Johansson, Markus Ringnér
10  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér
11  Copyright (C) 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér
12  Copyright (C) 2008 Peter Johansson
13
14  This file is part of the yat library, http://dev.thep.lu.se/trac/yat
15
16  The yat library is free software; you can redistribute it and/or
17  modify it under the terms of the GNU General Public License as
18  published by the Free Software Foundation; either version 3 of the
19  License, or (at your option) any later version.
20
21  The yat library is distributed in the hope that it will be useful,
22  but WITHOUT ANY WARRANTY; without even the implied warranty of
23  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24  General Public License for more details.
25
26  You should have received a copy of the GNU General Public License
27  along with yat. If not, see <http://www.gnu.org/licenses/>.
28*/
29
30#include "VectorMutable.h"
31#include "Exception.h"
32
33#include <iostream>
34#include <vector>
35#include <utility>
36
37#include <gsl/gsl_vector.h>
38#include <gsl/gsl_sort_vector.h>
39
40namespace theplu {
41namespace yat {
42namespace utility {
43
44  class Matrix;
45  class Vector;
46
47  /**
48     @brief This is the yat interface to gsl_vector_view.
49
50     This class can be used to create a vector view into a Matrix or a
51     VectorMutable. Modifying a view will also modify the underlying
52     data, i.e., the Matrix or VectorMutable that is viewed into. For
53     that reason all constructors are taking non-const references to
54     disallow mutable views into a const objects.
55
56     The fact that there is no copy constructor with const argument,
57     but only a so-called move constructor (copying a non-const
58     reference), implies that temporary objects such as objects
59     returned from functions can not be copied directly. Instead the
60     copying is done via a proxy class (see VectorMutable). Also since
61     we can not bind a temporary to a non-const reference, a
62     VectorView returned from a function cannot be sent directly to a
63     function.
64     For example
65     @code
66     Matrix m(10,10);
67     sum(m.row_view(0));
68     @endcode
69     but you need to use create a dummie object
70     @code
71     Matrix m(10,10);
72     VectorView vv = m.row_view(0);
73     sum(vv);
74     @endcode
75     or since sum is a const function, you can use VectorConstView
76     @code
77     Matrix m(10,10);
78     sum(m.row_const_view(0));
79     @endcode
80
81     Note that VectorView does not own underlying data, and a
82     VectorView is not valid if Vector/Matrix owning the data is
83     deallocated.
84  */
85  class VectorView : public VectorMutable
86  {
87  public:
88    /**
89       \brief Default constructor.
90
91       Creates a view into nothing and behaves like an empty Vector.
92    */
93    VectorView(void);
94
95    /**
96       \brief The copy constructor.
97
98       Modifications to created VectorView will also modify \a
99       other. Created VectorView is not dependent on \a other, but if
100       underlying data (Vector or Matrix) is deallocated VectorView is
101       invalid.
102    */ 
103    VectorView(VectorView& other);
104
105    /**
106       \brief copy another VectorMutable
107
108       \note If the object viewed by the view goes out of scope or is
109       deleted, the view becomes invalid and the result of further use
110       is undefined.
111    */
112    explicit VectorView(VectorMutable& other);
113
114    /**
115       \brief VectorView constructor.
116
117       Create a view of VectorMutable \a v, with starting index \a offset,
118       size \a n, and an optional \a stride.
119
120       \note If the object viewed by the view goes out of scope or is
121       deleted, the view becomes invalid and the result of further use
122       is undefined.
123    */
124    VectorView(VectorMutable& 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 VectorMutable with the
136    /// difference that changes made to the view will also change the
137    /// object that is viewed.
138    ///
139    /// @note If the object viewed by the view goes out of scope or is
140    /// deleted, the view becomes invalid and the result of further
141    /// use is undefined.
142    ///
143    VectorView(Matrix& m, size_t i, bool row=true);
144
145    ///
146    /// The destructor.
147    ///
148    ~VectorView(void);
149
150    /**
151       \return true
152    */
153    bool isview(void) const; 
154
155    /**
156       \brief The assignment operator.
157
158       \return A const reference to the resulting vector.
159
160       \note modifies underlying data.
161
162       \throw GSL_error if dimensions mis-match or if assignment fails
163    */
164    const VectorView& operator=(const VectorView&);
165
166    /**
167       \brief The assignment operator.
168
169       \return A const reference to the resulting vector.
170
171       \note modifies underlying data.
172
173       \throw GSL_error if dimensions mis-match or if assignment fails
174    */
175    const VectorView& operator=(const VectorBase&);
176
177  private:
178    const VectorView& assign(const VectorBase& other);
179    void copy(gsl_vector*);
180    void delete_allocated_memory(void);
181
182    gsl_vector_view* view_;
183
184    /**
185       Proxy class used to allow copy and assignment of VectorView. By
186       design vectors and matrices are passed as non-const references
187       in all constructors of VectorView. Because the standard does
188       not allow temporary objects to be bound to non-const
189       references, it is not possible to directly construct a
190       VectorView from a temporary VectorView returned from a
191       function. Instead this proxy class is created from the
192       temporary object and then a VectorView can be created from this
193       proxy.
194     */
195    struct proxy
196    { 
197      /// pointer to GSL vector keeping everything we need to create a
198      /// new VectorMutable from a proxy.
199      gsl_vector* vec_;
200    };
201
202  public:
203    /**
204       \brief create VectorView from proxy class
205     */
206    VectorView(proxy p);
207
208    /**
209       conversion operator to private proxy class.
210     */
211    operator proxy();
212  };
213
214}}} // of namespace utility, yat, and theplu
215
216#endif
Note: See TracBrowser for help on using the repository browser.