source: trunk/yat/utility/VectorView.cc

Last change on this file was 4207, checked in by Peter, 5 weeks ago

update copyright statements

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
  • Property svndigest:ignore set to 1008
File size: 4.1 KB
Line 
1// $Id: VectorView.cc 4207 2022-08-26 04:36:28Z 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, 2021, 2022 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 "MatrixMutable.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(MatrixMutable& 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(gsl_vector* v)
89  {
90    copy(v);
91  }
92
93
94  VectorView::VectorView(double* v, size_t size, size_t stride)
95  {
96    assert(v);
97    assert(size);
98    assert(stride);
99    view_ =
100      new gsl_vector_view(gsl_vector_view_array_with_stride(v, stride, size));
101    const_vec_ = vec_ = &(view_->vector);
102  }
103
104
105  VectorView::~VectorView(void)
106  {
107    delete_allocated_memory();
108  }
109
110
111  const VectorView& VectorView::assign(const gsl_vector* other)
112  {
113    if (other == NULL) {
114      if (size())
115        throw utility::GSL_error("VectorView::assign dimension mis-match");
116      return *this;
117    }
118
119    if (size() != other->size)
120      throw utility::GSL_error("VectorView::assign dimension mis-match");
121
122    // indirect assignment
123    if (detail::serial_overlap(const_vec_, other)) {
124      gsl_vector* tmp = detail::create_gsl_vector_copy(other);
125      if (gsl_vector_memcpy(vec_, tmp)) {
126        gsl_vector_free(tmp);
127        throw utility::GSL_error("VectorView::assign memcpy failed.");
128      }
129      gsl_vector_free(tmp);
130    }
131    else if (gsl_vector_memcpy(vec_, other))
132      throw utility::GSL_error("VectorView::assign memcpy failed.");
133    const_vec_ = vec_;
134    return *this;
135  }
136
137  const VectorView& VectorView::assign(const VectorBase& other )
138  {
139    assign(other.gsl_vector_p());
140    return *this;
141  }
142
143
144  void VectorView::copy(gsl_vector* other )
145  {
146    if (!other) // nothing to do
147      return;
148    view_ = new gsl_vector_view(gsl_vector_subvector(other,0,other->size));
149    const_vec_ = vec_ = &(view_->vector);
150  }
151
152
153  bool VectorView::isview(void) const
154  {
155    return true;
156  }
157
158
159  const VectorView& VectorView::operator=(const VectorView& other )
160  {
161    return assign(other);
162  }
163
164
165  const VectorView& VectorView::operator=(const VectorBase& other )
166  {
167    return assign(other);
168  }
169
170
171  void VectorView::delete_allocated_memory(void)
172  {
173    if (view_){
174      delete view_;
175      view_=NULL;
176    }
177    const_vec_ = vec_ = NULL;
178  }
179
180
181  VectorView::operator proxy()
182  {
183    assert(vec_==const_vec_);
184    proxy p;
185    p.vec_ = vec_;
186    vec_ = NULL; // proxy takes ownership and delivers to its receiver
187    const_vec_ = NULL;
188    return p;
189  }
190
191
192}}} // of namespace utility, yat, and thep
Note: See TracBrowser for help on using the repository browser.