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

Last change on this file since 1026 was 1026, checked in by Peter, 16 years ago

not returning const VectorBase?& in vector and VectorView?

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.8 KB
Line 
1// $Id: VectorView.cc 1026 2008-02-01 18:34:35Z 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 Jari Häkkinen, Markus Ringnér, Peter Johansson
7  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
8
9  This file is part of the yat library, http://trac.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 2 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 this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "VectorView.h"
28#include "matrix.h"
29#include "utility.h"
30#include "yat/random/random.h"
31
32#include <algorithm>
33#include <cassert>
34#include <cmath>
35#include <iostream>
36#include <sstream>
37#include <utility>
38#include <vector>
39
40namespace theplu {
41namespace yat {
42namespace utility {
43
44
45  VectorView::VectorView(void)
46    : VectorBase(), view_(NULL), const_view_(NULL)
47  {
48  }
49
50
51  VectorView::VectorView(VectorView& other)
52    : VectorBase(), const_view_(NULL)
53  {
54    view_ = new gsl_vector_view(gsl_vector_subvector(other.gsl_vector_p(), 
55                                                     0, other.size()));
56    const_vec_ = vec_ = &(view_->vector);
57   
58  }
59
60
61  VectorView::VectorView(const VectorView& other)
62    : VectorBase(), view_(NULL)
63  {
64    const_view_ = new gsl_vector_const_view(
65        gsl_vector_const_subvector(other.gsl_vector_p(), 0, other.size()));
66    const_vec_ = &(const_view_->vector);
67  }
68
69
70  VectorView::VectorView(VectorBase& other)
71    : VectorBase(other.gsl_vector_p()), const_view_(NULL)
72  {
73    view_ = 
74      new gsl_vector_view(gsl_vector_subvector_with_stride(other.gsl_vector_p(), 
75                                                           0, 1,other.size()));
76    const_vec_ = vec_ = &(view_->vector);
77  }
78
79
80  /*
81  VectorView::VectorView(const VectorBase& other)
82    : VectorBase(other.const_vec_), view_(NULL)
83  {
84  }
85  */
86
87  VectorView::VectorView(VectorBase& v, size_t offset,size_t n,size_t stride)
88    : VectorBase(), const_view_(NULL)
89  {
90    view_ = 
91      new gsl_vector_view(gsl_vector_subvector_with_stride(v.gsl_vector_p(), 
92                                                           offset, stride,n));
93    const_vec_ = vec_ = &(view_->vector);
94  }
95
96
97  VectorView::VectorView(const VectorBase& v, size_t offset,size_t n,
98                         size_t stride)
99    : VectorBase(), view_(NULL)
100  {
101    const_view_ = new gsl_vector_const_view(
102                   gsl_vector_const_subvector_with_stride(v.gsl_vector_p(), 
103                                                          offset, stride,n));
104    const_vec_ = &(const_view_->vector);
105  }
106
107
108  VectorView::VectorView(matrix& m, size_t i, bool row)
109    : VectorBase(), const_view_(NULL)
110  {
111    view_=new gsl_vector_view(row ?
112                              gsl_matrix_row   (m.gsl_matrix_p(),i) :
113                              gsl_matrix_column(m.gsl_matrix_p(),i)  );
114    const_vec_ = vec_ = &(view_->vector);
115  }
116
117
118  VectorView::VectorView(const matrix& m, size_t i, bool row)
119    : VectorBase(), view_(NULL)
120  {
121    const_view_ = 
122      new gsl_vector_const_view(row ? 
123                                gsl_matrix_const_row(m.gsl_matrix_p(),i) :
124                                gsl_matrix_const_column(m.gsl_matrix_p(),i));
125    const_vec_ = &(const_view_->vector);
126  }
127
128
129  /*
130  VectorView::VectorView(proxy p)
131    : VectorBase()
132  {
133    view_ = new gsl_vector_view(gsl_vector_subvector(p.vec_, 0, p.vec_->size));
134    const_vec_ = vec_ = &(view_->vector);
135  }
136  */
137
138  VectorView::~VectorView(void)
139  {
140    delete_allocated_memory();
141  }
142
143
144  const VectorView& VectorView::assign(const VectorBase& other )
145  {
146    if (size()!=other.size())
147      throw utility::GSL_error("VectorView::dimension mis-match");
148    if (!other.size())
149      return *this;
150    gsl_vector_memcpy(vec_, other.gsl_vector_p());
151    const_vec_ = vec_;
152    return *this;
153  } 
154
155
156  bool VectorView::isview(void) const
157  {
158    return true;
159  }
160
161
162
163  const VectorView& VectorView::operator=(const VectorView& other )
164  {
165    return assign(other);
166  }
167
168 
169  const VectorView& VectorView::operator=(const VectorBase& other )
170  {
171    return assign(other);
172  }
173
174 
175  /*
176  const VectorView& VectorView::operator=(proxy p)
177  {
178    if (size()!=p.vec_->size)
179      throw utility::GSL_error("VectorView::dimension mis-match");
180    if (!size())
181      return *this;
182    clean_up();
183    view_ = new gsl_vector_view(gsl_vector_subvector(p.vec_,0, p.vec_->size));
184    const_vec_=vec_ = &view_->vector;
185    return *this;
186  }
187  */
188
189  void VectorView::delete_allocated_memory(void)
190  {
191    if (view_){
192      delete view_;
193      view_=NULL;
194    }
195    else if (const_view_){
196      delete const_view_;
197      const_view_=NULL;
198    }
199    const_vec_ = vec_ = NULL;
200  }
201
202
203}}} // of namespace utility, yat, and thep
Note: See TracBrowser for help on using the repository browser.