gsl_vector.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
5  *
6  * MIA is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef GSLPP_VECTOR_HH
22 #define GSLPP_VECTOR_HH
23 
24 
25 #include <iostream>
26 #include <mia/core/gsl_defines.hh>
27 #include <mia/core/gsl_iterator.hh>
28 #include <gsl/gsl_vector.h>
29 
30 namespace gsl {
31 
38 
39 public:
42  typedef size_t size_type;
43  typedef double value_type;
44  typedef double& reference;
45  typedef const double& const_reference;
46  typedef gsl_vector vector_type;
47  typedef gsl_vector *vector_pointer_type;
48  typedef const gsl_vector *vector_const_pointer_type;
49 
53  Vector();
54 
60  Vector(size_type size, bool clear);
61 
62 
68  Vector(size_type size, const double *init);
69 
77  Vector(gsl_vector *holder);
84  Vector(const gsl_vector *holder);
85 
89  Vector(const Vector& other);
90 
94  Vector(Vector&& other);
95 
96 
100  Vector& operator = (const Vector& other);
101 
105  Vector& operator = (Vector&& other);
106 
107 
109  ~Vector();
110 
114  iterator begin();
115 
119  iterator end();
120 
124  const_iterator begin()const;
125 
129  const_iterator end()const;
130 
134  size_type size() const;
135 
141  value_type operator[](size_t i) const {
142  assert(cdata);
143  return cdata->data[i * cdata->stride];
144  };
145 
151  reference operator[](size_t i) {
152  assert(data);
153  return data->data[i * data->stride];
154  }
155 
157  const gsl_vector * operator ->() const;
158 
160  gsl_vector * operator ->();
161 
163  operator Vector::vector_const_pointer_type () const;
164 
166  operator Vector::vector_pointer_type ();
167 
172  void print(std::ostream& os) const;
173 
174  bool is_writable() const;
175  bool is_valid() const;
176 
177 protected:
178 
179  void reset_holder(gsl_vector *holder){
180  cdata = data = holder;
181  owner = false;
182  }
183 
184  void reset_holder(const gsl_vector *holder){
185  cdata = holder;
186  owner = false;
187  }
188 
189 private:
190  gsl_vector *data;
191  const gsl_vector *cdata;
192  bool owner;
193 };
194 
195 
196 inline Vector operator + (const Vector& lhs, const Vector& rhs)
197 {
198  Vector result(lhs);
199  gsl_vector_add(result, rhs);
200  return result;
201 }
202 
203 inline Vector operator - (const Vector& lhs, const Vector& rhs)
204 {
205  Vector result(lhs);
206  gsl_vector_sub(result, rhs);
207  return result;
208 }
209 
210 inline Vector operator * (const Vector& lhs, double f)
211 {
212  Vector result(lhs);
213  gsl_vector_scale(result, f);
214  return result;
215 }
216 
221 class EXPORT_GSL VectorView :public Vector {
222 public:
223  VectorView(gsl_vector_view vv): m_view(vv)
224  {
225  reset_holder(&m_view.vector);
226  };
227 private:
228  gsl_vector_view m_view;
229 };
230 
231 
237 public:
238  ConstVectorView(gsl_vector_const_view vv):m_view(vv),
239  m_holder(&m_view.vector)
240  {
241 
242  };
243 
245  return m_holder.begin();
246  }
247 
249  return m_holder.end();
250  }
251 
253  return m_holder.size();
254  }
255 
256  double operator[](size_t i)const {
257 
258  return m_holder[i];
259  };
260 
261  operator const Vector&(){
262  return m_holder;
263  }
264 
265  const gsl_vector * operator ->() const{
266  return m_holder.operator ->();
267  }
268 
270  return m_holder.operator Vector::vector_const_pointer_type();
271  }
272 
273 private:
274  gsl_vector_const_view m_view;
275  const Vector m_holder;
276 };
277 
278 inline std::ostream& operator << (std::ostream& os, const Vector& v) {
279  v.print(os);
280  return os;
281 }
282 
283 }
284 
285 #endif
286 
287 
void print(std::ostream &os) const
void reset_holder(const gsl_vector *holder)
Definition: gsl_vector.hh:184
gsl_vector * vector_pointer_type
Definition: gsl_vector.hh:47
gsl_vector vector_type
Definition: gsl_vector.hh:46
reference operator[](size_t i)
Definition: gsl_vector.hh:151
const gsl_vector * vector_const_pointer_type
Definition: gsl_vector.hh:48
ConstVectorView(gsl_vector_const_view vv)
Definition: gsl_vector.hh:238
value_type operator[](size_t i) const
Definition: gsl_vector.hh:141
vector_iterator operator+(const vector_iterator &it, int dist)
const_vector_iterator const_iterator
Definition: gsl_vector.hh:41
vector_iterator operator-(const vector_iterator &it, int dist)
Vector::const_iterator end() const
Definition: gsl_vector.hh:248
double value_type
Definition: gsl_vector.hh:43
double & reference
Definition: gsl_vector.hh:44
void reset_holder(gsl_vector *holder)
Definition: gsl_vector.hh:179
double operator[](size_t i) const
Definition: gsl_vector.hh:256
#define EXPORT_GSL
Definition: gsl_defines.hh:38
VectorView(gsl_vector_view vv)
Definition: gsl_vector.hh:223
std::ostream & operator<<(std::ostream &os, const Matrix &m)
Definition: gsl_matrix.hh:442
Vector::size_type size() const
Definition: gsl_vector.hh:252
Vector::const_iterator begin() const
Definition: gsl_vector.hh:244
vector_iterator iterator
Definition: gsl_vector.hh:40
Matrix EXPORT_GSL operator*(const Matrix &lhs, const Matrix &rhs)
size_t size_type
Definition: gsl_vector.hh:42
const double & const_reference
Definition: gsl_vector.hh:45