source: trunk/yat/utility/VectorView.h

Last change on this file was 3661, checked in by Peter, 4 years ago

update copyright years

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