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

Last change on this file since 1680 was 1680, checked in by Peter, 12 years ago

correcting includes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.3 KB
Line 
1// $Id: VectorView.cc 1680 2008-12-29 18:57:30Z 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 Peter Johansson
8
9  This file is part of the yat library, http://dev.thep.lu.se/trac/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include "VectorView.h"
26#include "VectorMutable.h"
27#include "Matrix.h"
28#include "utility.h"
29
30#include <cassert>
31
32namespace theplu {
33namespace yat {
34namespace utility {
35
36
37  VectorView::VectorView(void)
38    : VectorMutable(), view_(NULL)
39  {
40  }
41
42
43  VectorView::VectorView(VectorView& other)
44    : VectorMutable()
45  {
46    view_ = new gsl_vector_view(gsl_vector_subvector(other.gsl_vector_p(), 
47                                                     0, other.size()));
48    const_vec_ = vec_ = &(view_->vector);
49   
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    view_ = new gsl_vector_view(gsl_vector_subvector(other,0,other->size));
109    const_vec_ = vec_ = &(view_->vector);
110  }
111
112
113  bool VectorView::isview(void) const
114  {
115    return true;
116  }
117
118
119  const VectorView& VectorView::operator=(const VectorView& other )
120  {
121    return assign(other);
122  }
123
124 
125  const VectorView& VectorView::operator=(const VectorBase& other )
126  {
127    return assign(other);
128  }
129
130 
131  void VectorView::delete_allocated_memory(void)
132  {
133    if (view_){
134      delete view_;
135      view_=NULL;
136    }
137    const_vec_ = vec_ = NULL;
138  }
139
140
141  VectorView::operator proxy()
142  {
143    assert(vec_==const_vec_);
144    proxy p;
145    p.vec_ = vec_;
146    vec_ = NULL; // proxy takes ownership and delivers to its receiver
147    const_vec_ = NULL;
148    return p;
149  }
150
151
152}}} // of namespace utility, yat, and thep
Note: See TracBrowser for help on using the repository browser.