source: trunk/yat/utility/vectorView.h @ 1009

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

merging branch peter-dev into trunk delta 1008:994

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
1#ifndef _theplu_yat_utility_vector_view_
2#define _theplu_yat_utility_vector_view_
3
4// $Id: vectorView.h 1009 2008-02-01 04:15:44Z 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
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 vectorBase
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(vectorBase& other);
104    vectorView(vectorView& other);
105    // Peter, privatize
106    vectorView(const vectorView& other);
107
108    /**
109       \brief VectorView constructor.
110
111       Create a view of vectorView \a v, with starting index \a offset,
112       size \a n, and an optional \a stride.
113
114       A vectorView view can be used as any vectorView with the difference
115       that changes made to the view will also change the object that
116       is viewed. Also, using the copy constructor will create a new
117       vectorView object that is a copy of whatever is viewed. If a copy
118       of the view is needed then you should use this constructor to
119       obtain a copy.
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(vectorBase& v, size_t offset, size_t n, size_t stride=1);
128    // Peter, privatize
129    vectorView(const vectorBase& v, size_t offset, size_t n, size_t stride=1);
130
131    ///
132    /// Matrix row/column view constructor.
133    ///
134    /// Create a row/column vectorView view of matrix \a m, pointing at
135    /// row/column \a i. The parameter \a row is used to set whether
136    /// the view should be a row or column view. If \a row is set to
137    /// true, the view will be a row view (default behaviour), and,
138    /// naturally, a column view otherwise.
139    ///
140    /// A vectorView view can be used as any vectorView with the difference
141    /// that changes made to the view will also change the object that
142    /// is viewed. Also, using the copy constructor will create a new
143    /// vectorView object that is a copy of whatever is viewed. If a copy
144    /// of the view is needed then you should use the vectorView view
145    /// constructor to obtain a copy.
146    ///
147    /// @note If the object viewed by the view goes out of scope or is
148    /// deleted, the view becomes invalid and the result of further
149    /// use is undefined.
150    ///
151    vectorView(matrix& m, size_t i, bool row=true);
152
153    // Peter should be private
154    vectorView(const matrix& m, size_t i, bool row=true);
155
156    /**
157     */
158    //vectorView(proxy p);
159
160    ///
161    /// The destructor.
162    ///
163    ~vectorView(void);
164
165    /**
166       \return true
167    */
168    bool isview(void) const; 
169
170    /**
171       \brief The assignment operator.
172
173       \return A const reference to the resulting vector.
174
175       \throw GSL_error if dimensions mis-match.
176    */
177    const vectorBase& operator=(const vectorBase&);
178    const vectorBase& operator=(const vectorView&);
179
180    //const vectorBase& operator=(proxy);
181
182  private:
183    const vectorBase& assign(const vectorBase& other);
184    void delete_allocated_memory(void);
185
186    gsl_vector_view* view_;
187    gsl_vector_const_view* const_view_;
188   
189  };
190
191}}} // of namespace utility, yat, and theplu
192
193#endif
Note: See TracBrowser for help on using the repository browser.