source: trunk/yat/utility/VectorView.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 1008
File size: 3.3 KB
RevLine 
[22]1// $Id: VectorView.cc 3104 2013-11-02 02:42:56Z peter $
[12]2
[570]3/*
4  Copyright (C) 2003 Daniel Dalevi, Peter Johansson
[2119]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
[2787]8  Copyright (C) 2012 Peter Johansson
[570]9
[1437]10  This file is part of the yat library, http://dev.thep.lu.se/trac/yat
[570]11
[675]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
[1486]14  published by the Free Software Foundation; either version 3 of the
[675]15  License, or (at your option) any later version.
[570]16
[675]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
[570]20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
[1487]23  along with yat. If not, see <http://www.gnu.org/licenses/>.
[570]24*/
25
[2881]26#include <config.h>
27
[1015]28#include "VectorView.h"
[1027]29#include "VectorMutable.h"
[1121]30#include "Matrix.h"
[680]31#include "utility.h"
[125]32
[783]33#include <cassert>
[42]34
35namespace theplu {
[680]36namespace yat {
[616]37namespace utility {
[12]38
39
[1015]40  VectorView::VectorView(void)
[1027]41    : VectorMutable(), view_(NULL)
[686]42  {
43  }
[12]44
[686]45
[1015]46  VectorView::VectorView(VectorView& other)
[1027]47    : VectorMutable()
[686]48  {
[3102]49    copy(other.gsl_vector_p());
[686]50  }
51
52
[1027]53  VectorView::VectorView(VectorMutable& other)
54    : VectorMutable()
[686]55  {
[1118]56    copy(other.gsl_vector_p());
[1008]57  }
58
59
[1027]60  VectorView::VectorView(VectorMutable& v,size_t offset,size_t n,size_t stride)
61    : VectorMutable()
[1008]62  {
[2687]63    view_ =
64      new gsl_vector_view(gsl_vector_subvector_with_stride(v.gsl_vector_p(),
[995]65                                                           offset, stride,n));
66    const_vec_ = vec_ = &(view_->vector);
[686]67  }
68
69
[1121]70  VectorView::VectorView(Matrix& m, size_t i, bool row)
[1027]71    : VectorMutable()
[42]72  {
[420]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)  );
[995]76    const_vec_ = vec_ = &(view_->vector);
[257]77  }
78
79
[1027]80  VectorView::VectorView(proxy p)
[1118]81    : VectorMutable(), view_(NULL)
[1008]82  {
[1118]83    copy(p.vec_);
[1008]84  }
85
86
[1015]87  VectorView::~VectorView(void)
[42]88  {
[1008]89    delete_allocated_memory();
[42]90  }
[22]91
[42]92
[1026]93  const VectorView& VectorView::assign(const VectorBase& other )
[42]94  {
[995]95    if (size()!=other.size())
[1130]96      throw utility::GSL_error("VectorView::assign dimension mis-match");
[995]97    if (!other.size())
98      return *this;
[1130]99    if (gsl_vector_memcpy(vec_, other.gsl_vector_p()))
100      throw utility::GSL_error("VectorView::assign memcpy failed.");
[1008]101    const_vec_ = vec_;
[995]102    return *this;
[2687]103  }
[12]104
105
[1118]106  void VectorView::copy(gsl_vector* other )
107  {
[3104]108    if (!other) // nothing to do
109      return;
[1118]110    view_ = new gsl_vector_view(gsl_vector_subvector(other,0,other->size));
111    const_vec_ = vec_ = &(view_->vector);
112  }
113
114
[1015]115  bool VectorView::isview(void) const
[880]116  {
[1008]117    return true;
118  }
119
120
[1026]121  const VectorView& VectorView::operator=(const VectorView& other )
[1008]122  {
123    return assign(other);
124  }
125
[2687]126
[1026]127  const VectorView& VectorView::operator=(const VectorBase& other )
[1008]128  {
129    return assign(other);
130  }
131
[2687]132
[1015]133  void VectorView::delete_allocated_memory(void)
[791]134  {
[1008]135    if (view_){
[810]136      delete view_;
[1008]137      view_=NULL;
138    }
139    const_vec_ = vec_ = NULL;
[810]140  }
141
142
[1651]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
[686]154}}} // of namespace utility, yat, and thep
Note: See TracBrowser for help on using the repository browser.