source: branches/peter-dev/yat/utility/vectorView.h @ 995

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

Splitting vector class into three different classes for vector, view and const_view, and adding abstract classes appropriately. NOTE, the tests are not going through, not even compiling; sorry about that

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1#ifndef _theplu_yat_utility_vector_view_
2#define _theplu_yat_utility_vector_view_
3
4// $Id: vectorView.h 995 2007-11-30 22:55:30Z 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
33#include "Exception.h"
34#include "Iterator.h"
35
36#include <iostream>
37#include <vector>
38#include <utility>
39
40#include <gsl/gsl_vector.h>
41#include <gsl/gsl_sort_vector.h>
42
43namespace theplu {
44namespace yat {
45namespace utility {
46
47  class matrix;
48  class vector;
49
50  /**
51     @brief This is the yat interface to GSL vector.
52
53     For time being 'double' is the only type supported.
54
55     \par File streams:
56     Reading and writing vectors to file streams are of course
57     supported. These are implemented without using GSL functionality,
58     and thus binary read and write to streams are not supported.
59
60     \par Vector views:
61     GSL vector views are supported and these are disguised as
62     ordinary utility::vectors. A support function is added,
63     utility::vector::isview(), that can be used to check if a vector
64     object is a view. Note that view vectors do not own the
65     underlying data, and a view is not valid if the vector/matrix
66     owing the data is deallocated.
67
68     \par
69     Currently there is no restriction on how a vector is used when
70     the vector is a const view into another vector or matrix. To
71     avoid unexpected runtime errors, the programmer must declare
72     const view vectors as 'const' in order to get compile time
73     diagnostics about improper use of a const view vector object. If
74     'const' is not used and the const view vector is used erroneously
75     (such as on the left hand side in assignments), the compiler will
76     not catch the error and runtime error will occur. assert(3) is
77     used to catch the runtime error during development. Example on
78     bad and proper use of const view vectors:
79     @code
80  const vector vm(13,1.0);
81  vector v1(vm,2,4);       // bad code! not const!
82  v1(0)=-123;              // accepted by compiler, runtime abort will occur
83                           // or catched by assert depending on compiler flags
84  const vector v2(vm,2,4); // proper code
85  v2(0)=-123;              // not acceptable for the compiler
86     @endcode
87  */
88
89  class vectorView : public vectorMutable
90  {
91  public:
92    /// \brief vectorView::iterator
93    typedef Iterator<double&, vectorView> iterator;
94
95    /**
96       \brief Default constructor.
97    */
98    vectorView(void);
99
100    /**
101       \brief The copy constructor.
102    */
103    vectorView(vectorMutable& other);
104
105    /**
106       \brief VectorView constructor.
107
108       Create a view of vectorView \a v, with starting index \a offset,
109       size \a n, and an optional \a stride.
110
111       A vectorView view can be used as any vectorView with the difference
112       that changes made to the view will also change the object that
113       is viewed. Also, using the copy constructor will create a new
114       vectorView object that is a copy of whatever is viewed. If a copy
115       of the view is needed then you should use this constructor to
116       obtain a copy.
117
118       \note If the object viewed by the view goes out of scope or is
119       deleted, the view becomes invalid and the result of further use
120       is undefined.
121
122       \throw GSL_error if a view cannot be set up.
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 vectorView with the difference
136    /// that changes made to the view will also change the object that
137    /// is viewed. Also, using the copy constructor will create a new
138    /// vectorView object that is a copy of whatever is viewed. If a copy
139    /// of the view is needed then you should use the vectorView view
140    /// constructor to obtain a copy.
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     */
150    vectorView(proxy p);
151
152    ///
153    /// The destructor.
154    ///
155    ~vectorView(void);
156
157    /**
158       \brief The assignment operator.
159
160       \return A const reference to the resulting vector.
161
162       \throw GSL_error if dimensions mis-match.
163    */
164    const vectorMutable& operator=(vectorMutable&);
165
166    const vectorMutable& operator=(proxy);
167
168  private:
169    void clean_up(void);
170
171    gsl_vector_view* view_;
172   
173  };
174
175}}} // of namespace utility, yat, and theplu
176
177#endif
Note: See TracBrowser for help on using the repository browser.