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

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

removed includes not needed

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
1#ifndef _theplu_yat_utility_vector_view_
2#define _theplu_yat_utility_vector_view_
3
4// $Id: VectorView.h 2013 2009-07-09 21:11:11Z 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 Jari Häkkinen, 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
32#include <gsl/gsl_vector.h>
33
34namespace theplu {
35namespace yat {
36namespace utility {
37
38  class Matrix;
39  class Vector;
40
41  /**
42     @brief This is the yat interface to gsl_vector_view.
43
44     This class can be used to create a vector view into a Matrix or a
45     VectorMutable. Modifying a view will also modify the underlying
46     data, i.e., the Matrix or VectorMutable that is viewed into. For
47     that reason all constructors are taking non-const references to
48     disallow mutable views into a const objects.
49
50     The fact that there is no copy constructor with const argument,
51     but only a so-called move constructor (copying a non-const
52     reference), implies that temporary objects such as objects
53     returned from functions can not be copied directly. Instead the
54     copying is done via a proxy class. Also since we can not bind a
55     temporary to a non-const reference, a VectorView returned from a
56     function cannot be sent directly to a function.
57     For example
58     @code
59     Matrix m(10,10);
60     sum(m.row_view(0));
61     @endcode
62     but you need to create a dummie object
63     @code
64     Matrix m(10,10);
65     VectorView vv = m.row_view(0);
66     sum(vv);
67     @endcode
68     or since sum is a const function, you can use VectorConstView
69     @code
70     Matrix m(10,10);
71     sum(m.row_const_view(0));
72     @endcode
73
74     Note that VectorView does not own underlying data, and a
75     VectorView is not valid if Vector/Matrix owning the data is
76     deallocated.
77  */
78  class VectorView : public VectorMutable
79  {
80  public:
81    /**
82       \brief Default constructor.
83
84       Creates a view into nothing and behaves like an empty Vector.
85    */
86    VectorView(void);
87
88    /**
89       \brief The copy constructor.
90
91       Modifications to created VectorView will also modify \a
92       other. Created VectorView is not dependent on \a other, but if
93       underlying data (Vector or Matrix) is deallocated VectorView is
94       invalid.
95    */ 
96    VectorView(VectorView& other);
97
98    /**
99       \brief copy another VectorMutable
100
101       \note If the object viewed by the view goes out of scope or is
102       deleted, the view becomes invalid and the result of further use
103       is undefined.
104    */
105    explicit VectorView(VectorMutable& other);
106
107    /**
108       \brief VectorView constructor.
109
110       Create a view of VectorMutable \a v, with starting index \a offset,
111       size \a n, and an optional \a stride.
112
113       \note If the object viewed by the view goes out of scope or is
114       deleted, the view becomes invalid and the result of further use
115       is undefined.
116    */
117    VectorView(VectorMutable& v, size_t offset, size_t n, size_t stride=1);
118
119    ///
120    /// Matrix row/column view constructor.
121    ///
122    /// Create a row/column VectorView view of matrix \a m, pointing at
123    /// row/column \a i. The parameter \a row is used to set whether
124    /// the view should be a row or column view. If \a row is set to
125    /// true, the view will be a row view (default behaviour), and,
126    /// naturally, a column view otherwise.
127    ///
128    /// A VectorView view can be used as any VectorMutable with the
129    /// difference that changes made to the view will also change the
130    /// object that is viewed.
131    ///
132    /// @note If the object viewed by the view goes out of scope or is
133    /// deleted, the view becomes invalid and the result of further
134    /// use is undefined.
135    ///
136    VectorView(Matrix& m, size_t i, bool row=true);
137
138    ///
139    /// The destructor.
140    ///
141    ~VectorView(void);
142
143    /**
144       \return true
145    */
146    bool isview(void) const; 
147
148    /**
149       \brief The assignment operator.
150
151       \return A const reference to the resulting vector.
152
153       \note modifies underlying data.
154
155       \throw GSL_error if dimensions mis-match or if assignment fails
156    */
157    const VectorView& operator=(const VectorView&);
158
159    /**
160       \brief The assignment operator.
161
162       \return A const reference to the resulting vector.
163
164       \note modifies underlying data.
165
166       \throw GSL_error if dimensions mis-match or if assignment fails
167    */
168    const VectorView& operator=(const VectorBase&);
169
170  private:
171    const VectorView& assign(const VectorBase& other);
172    void copy(gsl_vector*);
173    void delete_allocated_memory(void);
174
175    gsl_vector_view* view_;
176
177    /**
178       Proxy class used to allow copy and assignment of VectorView. By
179       design vectors and matrices are passed as non-const references
180       in all constructors of VectorView. Because the standard does
181       not allow temporary objects to be bound to non-const
182       references, it is not possible to directly construct a
183       VectorView from a temporary VectorView returned from a
184       function. Instead this proxy class is created from the
185       temporary object and then a VectorView can be created from this
186       proxy.
187     */
188    struct proxy
189    { 
190      /// pointer to GSL vector keeping everything we need to create a
191      /// new VectorMutable from a proxy.
192      gsl_vector* vec_;
193    };
194
195  public:
196    /**
197       \brief create VectorView from proxy class
198     */
199    VectorView(proxy p);
200
201    /**
202       conversion operator to private proxy class.
203     */
204    operator proxy();
205  };
206
207}}} // of namespace utility, yat, and theplu
208
209#endif
Note: See TracBrowser for help on using the repository browser.