source: trunk/yat/utility/VectorConstView.h @ 1027

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

going back to previous design in which view and const_view are in different classes. Having them in same didnt work as expected. There is a problem converting vector::iterator to vector::const_iterator. I'll open a separate ticket for this issue.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1#ifndef _theplu_yat_utility_vector_const_view_
2#define _theplu_yat_utility_vector_const_view_
3
4// $Id: VectorConstView.h 1027 2008-02-02 21:29:29Z 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 "VectorBase.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.
50
51     For time being 'double' is the only type supported.
52
53     \par File streams:
54     Reading and writing vectors to file streams are of course
55     supported. These are implemented without using GSL functionality,
56     and thus binary read and write to streams are not supported.
57
58     \par Vector views:
59     GSL vector views are supported and these are disguised as
60     ordinary utility::vectors. A support function is added,
61     utility::vector::isview(), that can be used to check if a vector
62     object is a view. Note that view vectors do not own the
63     underlying data, and a view is not valid if the vector/matrix
64     owing the data is deallocated.
65
66     \par
67     Currently there is no restriction on how a vector is used when
68     the vector is a const view into another vector or matrix. To
69     avoid unexpected runtime errors, the programmer must declare
70     const view vectors as 'const' in order to get compile time
71     diagnostics about improper use of a const view vector object. If
72     'const' is not used and the const view vector is used erroneously
73     (such as on the left hand side in assignments), the compiler will
74     not catch the error and runtime error will occur. assert(3) is
75     used to catch the runtime error during development. Example on
76     bad and proper use of const view vectors:
77     @code
78  const vector vm(13,1.0);
79  vector v1(vm,2,4);       // bad code! not const!
80  v1(0)=-123;              // accepted by compiler, runtime abort will occur
81                           // or catched by assert depending on compiler flags
82  const vector v2(vm,2,4); // proper code
83  v2(0)=-123;              // not acceptable for the compiler
84     @endcode
85  */
86
87  class VectorConstView : public VectorBase
88  {
89  public:
90    /**
91       \brief The copy constructor.
92    */ 
93    VectorConstView(const VectorBase& other);
94    VectorConstView(const VectorConstView& other);
95
96    /**
97       \brief VectorConstView constructor.
98
99       Create a view of VectorConstView \a v, with starting index \a offset,
100       size \a n, and an optional \a stride.
101
102       A VectorConstView view can be used as any VectorConstView with the difference
103       that changes made to the view will also change the object that
104       is viewed. Also, using the copy constructor will create a new
105       VectorConstView object that is a copy of whatever is viewed. If a copy
106       of the view is needed then you should use this constructor to
107       obtain a copy.
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       \throw GSL_error if a view cannot be set up.
114    */
115    VectorConstView(const VectorBase& v,size_t offset,size_t n,size_t stride=1);
116
117    ///
118    /// Matrix row/column view constructor.
119    ///
120    /// Create a row/column VectorConstView view of matrix \a m, pointing at
121    /// row/column \a i. The parameter \a row is used to set whether
122    /// the view should be a row or column view. If \a row is set to
123    /// true, the view will be a row view (default behaviour), and,
124    /// naturally, a column view otherwise.
125    ///
126    /// A VectorConstView view can be used as any VectorConstView with the difference
127    /// that changes made to the view will also change the object that
128    /// is viewed. Also, using the copy constructor will create a new
129    /// VectorConstView object that is a copy of whatever is viewed. If a copy
130    /// of the view is needed then you should use the VectorConstView view
131    /// constructor to obtain a copy.
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    VectorConstView(const matrix& m, size_t i, bool row=true);
138
139    ///
140    /// The destructor.
141    ///
142    ~VectorConstView(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       \throw GSL_error if dimensions mis-match.
155    */
156    const VectorConstView& operator=(const VectorBase&);
157    const VectorConstView& operator=(const VectorConstView&);
158
159  private:
160    const VectorConstView& assign(const VectorBase& other);
161    void delete_allocated_memory(void);
162
163    gsl_vector_const_view* const_view_;
164   
165  };
166
167}}} // of namespace utility, yat, and theplu
168
169#endif
Note: See TracBrowser for help on using the repository browser.