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

Last change on this file since 1118 was 1118, checked in by Peter, 15 years ago

fixing memory bug in VectorView? - fixes #305

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