source: trunk/yat/utility/VectorConstView.cc @ 3104

Last change on this file since 3104 was 3104, checked in by Peter, 8 years ago

allow construction of view into zero-sized Vector.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
  • Property svndigest:ignore set to 1026
File size: 2.6 KB
Line 
1// $Id: VectorConstView.cc 3104 2013-11-02 02:42:56Z peter $
2
3/*
4  Copyright (C) 2003 Daniel Dalevi, Peter Johansson
5  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér
7  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2012 Peter Johansson
9
10  This file is part of the yat library, http://dev.thep.lu.se/trac/yat
11
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 3 of the
15  License, or (at your option) any later version.
16
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with yat. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include <config.h>
27
28#include "VectorConstView.h"
29#include "Matrix.h"
30
31namespace theplu {
32namespace yat {
33namespace utility {
34
35
36  VectorConstView::VectorConstView(const VectorConstView& other)
37    : VectorBase(), const_view_(NULL)
38  {
39    copy(other);
40  }
41
42
43  VectorConstView::VectorConstView(const VectorBase& other)
44    : VectorBase(), const_view_(NULL)
45  {
46    copy(other);
47  }
48
49
50  VectorConstView::VectorConstView(const VectorBase& v, size_t offset,size_t n,
51                                   size_t stride)
52    : VectorBase()
53  {
54    const_view_ = new gsl_vector_const_view(
55                   gsl_vector_const_subvector_with_stride(v.gsl_vector_p(),
56                                                          offset, stride,n));
57    const_vec_ = &(const_view_->vector);
58  }
59
60
61  VectorConstView::VectorConstView(const Matrix& m, size_t i, bool row)
62    : VectorBase(), const_view_(NULL)
63  {
64    const_view_ =
65      new gsl_vector_const_view(row ?
66                                gsl_matrix_const_row(m.gsl_matrix_p(),i) :
67                                gsl_matrix_const_column(m.gsl_matrix_p(),i));
68    const_vec_ = &(const_view_->vector);
69  }
70
71
72  VectorConstView::~VectorConstView(void)
73  {
74    delete_allocated_memory();
75  }
76
77
78  void VectorConstView::copy(const VectorBase& other)
79  {
80    if (!other.gsl_vector_p()) // nothing to do
81      return;
82    const_view_ =
83      new gsl_vector_const_view(gsl_vector_const_subvector(other.gsl_vector_p(),
84                                                           0, other.size()));
85    const_vec_ = &(const_view_->vector);
86  }
87
88
89  void VectorConstView::delete_allocated_memory(void)
90  {
91    delete const_view_;
92    const_view_=NULL;
93    const_vec_ = NULL;
94  }
95
96
97  bool VectorConstView::isview(void) const
98  {
99    return true;
100  }
101
102}}} // of namespace utility, yat, and thep
Note: See TracBrowser for help on using the repository browser.