source: trunk/yat/utility/VectorView.cc @ 3114

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

update copyright years

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
  • Property svndigest:ignore set to 1008
File size: 3.3 KB
Line 
1// $Id: VectorView.cc 3114 2013-11-10 23:51:47Z 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) 2012, 2013 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 "VectorView.h"
29#include "VectorMutable.h"
30#include "Matrix.h"
31#include "utility.h"
32
33#include <cassert>
34
35namespace theplu {
36namespace yat {
37namespace utility {
38
39
40  VectorView::VectorView(void)
41    : VectorMutable(), view_(NULL)
42  {
43  }
44
45
46  VectorView::VectorView(VectorView& other)
47    : VectorMutable()
48  {
49    copy(other.gsl_vector_p());
50  }
51
52
53  VectorView::VectorView(VectorMutable& other)
54    : VectorMutable()
55  {
56    copy(other.gsl_vector_p());
57  }
58
59
60  VectorView::VectorView(VectorMutable& v,size_t offset,size_t n,size_t stride)
61    : VectorMutable()
62  {
63    view_ =
64      new gsl_vector_view(gsl_vector_subvector_with_stride(v.gsl_vector_p(),
65                                                           offset, stride,n));
66    const_vec_ = vec_ = &(view_->vector);
67  }
68
69
70  VectorView::VectorView(Matrix& m, size_t i, bool row)
71    : VectorMutable()
72  {
73    view_=new gsl_vector_view(row ?
74                              gsl_matrix_row   (m.gsl_matrix_p(),i) :
75                              gsl_matrix_column(m.gsl_matrix_p(),i)  );
76    const_vec_ = vec_ = &(view_->vector);
77  }
78
79
80  VectorView::VectorView(proxy p)
81    : VectorMutable(), view_(NULL)
82  {
83    copy(p.vec_);
84  }
85
86
87  VectorView::~VectorView(void)
88  {
89    delete_allocated_memory();
90  }
91
92
93  const VectorView& VectorView::assign(const VectorBase& other )
94  {
95    if (size()!=other.size())
96      throw utility::GSL_error("VectorView::assign dimension mis-match");
97    if (!other.size())
98      return *this;
99    if (gsl_vector_memcpy(vec_, other.gsl_vector_p()))
100      throw utility::GSL_error("VectorView::assign memcpy failed.");
101    const_vec_ = vec_;
102    return *this;
103  }
104
105
106  void VectorView::copy(gsl_vector* other )
107  {
108    if (!other) // nothing to do
109      return;
110    view_ = new gsl_vector_view(gsl_vector_subvector(other,0,other->size));
111    const_vec_ = vec_ = &(view_->vector);
112  }
113
114
115  bool VectorView::isview(void) const
116  {
117    return true;
118  }
119
120
121  const VectorView& VectorView::operator=(const VectorView& other )
122  {
123    return assign(other);
124  }
125
126
127  const VectorView& VectorView::operator=(const VectorBase& other )
128  {
129    return assign(other);
130  }
131
132
133  void VectorView::delete_allocated_memory(void)
134  {
135    if (view_){
136      delete view_;
137      view_=NULL;
138    }
139    const_vec_ = vec_ = NULL;
140  }
141
142
143  VectorView::operator proxy()
144  {
145    assert(vec_==const_vec_);
146    proxy p;
147    p.vec_ = vec_;
148    vec_ = NULL; // proxy takes ownership and delivers to its receiver
149    const_vec_ = NULL;
150    return p;
151  }
152
153
154}}} // of namespace utility, yat, and thep
Note: See TracBrowser for help on using the repository browser.