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

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

set svndigest:ignore proprty to reflect when files were initially copied from other file (and early history should be ignored). closes #750

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