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

Last change on this file since 1598 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.4 KB
Line 
1#ifndef _theplu_yat_utility_vector_view_
2#define _theplu_yat_utility_vector_view_
3
4// $Id: VectorView.h 1487 2008-09-10 08:41:36Z jari $
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    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       \brief create VectorView from proxy class
147     */
148    VectorView(proxy p);
149
150    ///
151    /// The destructor.
152    ///
153    ~VectorView(void);
154
155    /**
156       \return true
157    */
158    bool isview(void) const; 
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 VectorView&);
170
171    /**
172       \brief The assignment operator.
173
174       \return A const reference to the resulting vector.
175
176       \note modifies underlying data.
177
178       \throw GSL_error if dimensions mis-match or if assignment fails
179    */
180    const VectorView& operator=(const VectorBase&);
181
182  private:
183    const VectorView& assign(const VectorBase& other);
184    void copy(gsl_vector*);
185    void delete_allocated_memory(void);
186
187    gsl_vector_view* view_;
188  };
189
190}}} // of namespace utility, yat, and theplu
191
192#endif
Note: See TracBrowser for help on using the repository browser.