1 | #ifndef _theplu_yat_utility_vector_view_ |
---|
2 | #define _theplu_yat_utility_vector_view_ |
---|
3 | |
---|
4 | // $Id: VectorView.h 1437 2008-08-25 17:55:00Z 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 2 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 this program; if not, write to the Free Software |
---|
28 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
29 | 02111-1307, USA. |
---|
30 | */ |
---|
31 | |
---|
32 | #include "VectorMutable.h" |
---|
33 | #include "Exception.h" |
---|
34 | |
---|
35 | #include <iostream> |
---|
36 | #include <vector> |
---|
37 | #include <utility> |
---|
38 | |
---|
39 | #include <gsl/gsl_vector.h> |
---|
40 | #include <gsl/gsl_sort_vector.h> |
---|
41 | |
---|
42 | namespace theplu { |
---|
43 | namespace yat { |
---|
44 | namespace utility { |
---|
45 | |
---|
46 | class Matrix; |
---|
47 | class Vector; |
---|
48 | |
---|
49 | /** |
---|
50 | @brief This is the yat interface to gsl_vector_view. |
---|
51 | |
---|
52 | This class can be used to create a vector view into a Matrix or a |
---|
53 | VectorMutable. Modifying a view will also modify the underlying |
---|
54 | data, i.e., the Matrix or VectorMutable that is viewed into. For |
---|
55 | that reason all constructors are taking non-const references to |
---|
56 | disallow mutable views into a const objects. |
---|
57 | |
---|
58 | The fact that there is no copy constructor with const argument, |
---|
59 | but only a so-called move constructor (copying a non-const |
---|
60 | reference), implies that temporary objects such as objects |
---|
61 | returned from functions can not be copied directly. Instead the |
---|
62 | copying is done via a proxy class (see VectorMutable). Also since |
---|
63 | we can not bind a temporary to a non-const reference, a |
---|
64 | VectorView returned from a function cannot be sent directly to a |
---|
65 | function. |
---|
66 | For example |
---|
67 | @code |
---|
68 | Matrix m(10,10); |
---|
69 | sum(m.row_view(0)); |
---|
70 | @endcode |
---|
71 | but you need to use create a dummie object |
---|
72 | @code |
---|
73 | Matrix m(10,10); |
---|
74 | VectorView vv = m.row_view(0); |
---|
75 | sum(vv); |
---|
76 | @endcode |
---|
77 | or since sum is a const function, you can use VectorConstView |
---|
78 | @code |
---|
79 | Matrix m(10,10); |
---|
80 | sum(m.row_const_view(0)); |
---|
81 | @endcode |
---|
82 | |
---|
83 | Note that VectorView does not own underlying data, and a |
---|
84 | VectorView is not valid if Vector/Matrix owning the data is |
---|
85 | deallocated. |
---|
86 | */ |
---|
87 | class VectorView : public VectorMutable |
---|
88 | { |
---|
89 | public: |
---|
90 | /** |
---|
91 | \brief Default constructor. |
---|
92 | |
---|
93 | Creates a view into nothing and behaves like an empty Vector. |
---|
94 | */ |
---|
95 | VectorView(void); |
---|
96 | |
---|
97 | /** |
---|
98 | \brief The copy constructor. |
---|
99 | |
---|
100 | Modifications to created VectorView will also modify \a |
---|
101 | other. Created VectorView is not dependent on \a other, but if |
---|
102 | underlying data (Vector or Matrix) is deallocated VectorView is |
---|
103 | invalid. |
---|
104 | */ |
---|
105 | VectorView(VectorView& other); |
---|
106 | |
---|
107 | /** |
---|
108 | \brief copy another VectorMutable |
---|
109 | |
---|
110 | \note If the object viewed by the view goes out of scope or is |
---|
111 | deleted, the view becomes invalid and the result of further use |
---|
112 | is undefined. |
---|
113 | */ |
---|
114 | VectorView(VectorMutable& other); |
---|
115 | |
---|
116 | /** |
---|
117 | \brief VectorView constructor. |
---|
118 | |
---|
119 | Create a view of VectorMutable \a v, with starting index \a offset, |
---|
120 | size \a n, and an optional \a stride. |
---|
121 | |
---|
122 | \note If the object viewed by the view goes out of scope or is |
---|
123 | deleted, the view becomes invalid and the result of further use |
---|
124 | is undefined. |
---|
125 | */ |
---|
126 | VectorView(VectorMutable& v, size_t offset, size_t n, size_t stride=1); |
---|
127 | |
---|
128 | /// |
---|
129 | /// Matrix row/column view constructor. |
---|
130 | /// |
---|
131 | /// Create a row/column VectorView view of matrix \a m, pointing at |
---|
132 | /// row/column \a i. The parameter \a row is used to set whether |
---|
133 | /// the view should be a row or column view. If \a row is set to |
---|
134 | /// true, the view will be a row view (default behaviour), and, |
---|
135 | /// naturally, a column view otherwise. |
---|
136 | /// |
---|
137 | /// A VectorView view can be used as any VectorMutable with the |
---|
138 | /// difference that changes made to the view will also change the |
---|
139 | /// object that is viewed. |
---|
140 | /// |
---|
141 | /// @note If the object viewed by the view goes out of scope or is |
---|
142 | /// deleted, the view becomes invalid and the result of further |
---|
143 | /// use is undefined. |
---|
144 | /// |
---|
145 | VectorView(Matrix& m, size_t i, bool row=true); |
---|
146 | |
---|
147 | /** |
---|
148 | \brief create VectorView from proxy class |
---|
149 | */ |
---|
150 | VectorView(proxy p); |
---|
151 | |
---|
152 | /// |
---|
153 | /// The destructor. |
---|
154 | /// |
---|
155 | ~VectorView(void); |
---|
156 | |
---|
157 | /** |
---|
158 | \return true |
---|
159 | */ |
---|
160 | bool isview(void) const; |
---|
161 | |
---|
162 | /** |
---|
163 | \brief The assignment operator. |
---|
164 | |
---|
165 | \return A const reference to the resulting vector. |
---|
166 | |
---|
167 | \note modifies underlying data. |
---|
168 | |
---|
169 | \throw GSL_error if dimensions mis-match or if assignment fails |
---|
170 | */ |
---|
171 | const VectorView& operator=(const VectorView&); |
---|
172 | |
---|
173 | /** |
---|
174 | \brief The assignment operator. |
---|
175 | |
---|
176 | \return A const reference to the resulting vector. |
---|
177 | |
---|
178 | \note modifies underlying data. |
---|
179 | |
---|
180 | \throw GSL_error if dimensions mis-match or if assignment fails |
---|
181 | */ |
---|
182 | const VectorView& operator=(const VectorBase&); |
---|
183 | |
---|
184 | private: |
---|
185 | const VectorView& assign(const VectorBase& other); |
---|
186 | void copy(gsl_vector*); |
---|
187 | void delete_allocated_memory(void); |
---|
188 | |
---|
189 | gsl_vector_view* view_; |
---|
190 | }; |
---|
191 | |
---|
192 | }}} // of namespace utility, yat, and theplu |
---|
193 | |
---|
194 | #endif |
---|