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

Last change on this file since 1710 was 1710, checked in by Peter, 12 years ago

avoid repeating code

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 KB
Line 
1#ifndef _theplu_yat_utility_vector_const_view_
2#define _theplu_yat_utility_vector_const_view_
3
4// $Id: VectorConstView.h 1710 2009-01-13 16:35: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, 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, 2009 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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
28*/
29
30#include "VectorBase.h"
31
32#include <gsl/gsl_vector.h>
33
34namespace theplu {
35namespace yat {
36namespace utility {
37
38  class Matrix;
39  class VectorView;
40
41  /**
42     @brief Read-only view.
43
44     Wrapper to gsl_vector_const_view
45
46     With this class you can create a view into const
47     objects - either a vector or matrix. By design all functionality
48     in this class is const to guarantee that the viewed object is not
49     modified.
50
51     \note that view vectors do not own the underlying data,
52     and a view is not valid if the vector/matrix owing the data is
53     deallocated.
54  */
55  class VectorConstView : public VectorBase
56  {
57  public:
58    /**
59       \brief The copy constructor.
60    */
61    // needed to override compiler generated copy constructor
62    VectorConstView(const VectorConstView& other);
63
64    /**
65       \brief Copy a VectorBase
66    */ 
67    explicit VectorConstView(const VectorBase& other);
68
69    /**
70       \brief const view into a vector
71
72       Create a const view of VectorBase \a v, with starting index \a offset,
73       size \a n, and an optional \a stride.
74
75       \note If the object viewed by the view goes out of scope or is
76       deleted, the view becomes invalid and the result of further use
77       is undefined.
78
79       \throw GSL_error if a view cannot be set up.
80    */
81    VectorConstView(const VectorBase& v,size_t offset,size_t n,size_t stride=1);
82
83    /**
84       \brief Matrix row/column view constructor.
85       
86       Create a view into a row/column of a matrix.
87
88       \param m matrix to view into.
89       \param i index telling which row/column to view into
90       \param row if true (defult) created view is a row vector, i.e.,
91       viewing into row \a i, and, naturally, a column vector
92       otherwise.
93
94       \see matrix::column_const_view(size_t) and
95       matrix::row_const_view(size_t)
96
97       @note If the object viewed by the view goes out of scope or is
98       deleted, the view becomes invalid and the result of further
99       use is undefined.
100    */
101    VectorConstView(const Matrix& m, size_t i, bool row=true);
102
103    /**
104       The destructor.
105    */
106    ~VectorConstView(void);
107
108    /**
109       \return true
110    */
111    bool isview(void) const; 
112
113  private:
114    void copy(const VectorBase& other);
115    void delete_allocated_memory(void);
116
117    // Perhaps not needed - only used to create a gsl_vector (that is
118    // stored in base class). For data access use data in base class
119    // because this pointer may be NULL.
120    gsl_vector_const_view* const_view_;
121   
122    // assignment not allowed
123    VectorConstView& operator=(const VectorConstView&);
124  };
125
126}}} // of namespace utility, yat, and theplu
127
128#endif
Note: See TracBrowser for help on using the repository browser.