source: trunk/yat/utility/VectorView.cc

Last change on this file was 3623, checked in by Peter, 4 years ago

merge release 0.14.1 into trunk

move functions 'create_gsl_vector' and 'create_gsl_vector_copy' from
Vector private to free functions in namespace detail, i.e., they are
available for other classes/functions, but not part of the public
API. In the same region add functions 'overlap' and 'serial_overlap'
to check if two GSL vectors overlap or can be used safely as LHS and
RHS in a function.

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