My Project
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>
28#include <gsl/gsl_vector.h>
29
30namespace gsl
31{
32
39{
40
41public:
44 typedef size_t size_type;
45 typedef double value_type;
46 typedef double& reference;
47 typedef const double& const_reference;
48 typedef gsl_vector vector_type;
49 typedef gsl_vector *vector_pointer_type;
50 typedef const gsl_vector *vector_const_pointer_type;
51
56
62 Vector(size_type size, bool clear);
63
64
70 Vector(size_type size, const double *init);
71
79 Vector(gsl_vector *holder);
86 Vector(const gsl_vector *holder);
87
91 Vector(const Vector& other);
92
96 Vector(Vector&& other);
97
98
102 Vector& operator = (const Vector& other);
103
107 Vector& operator = (Vector&& other);
108
109
112
117
122
127
132
137
143 value_type operator[](size_t i) const
144 {
145 assert(cdata);
146 return cdata->data[i * cdata->stride];
147 };
148
155 {
156 assert(data);
157 return data->data[i * data->stride];
158 }
159
161 const gsl_vector *operator ->() const;
162
164 gsl_vector *operator ->();
165
168
171
176 void print(std::ostream& os) const;
177
178 bool is_writable() const;
179 bool is_valid() const;
180
181protected:
182
183 void reset_holder(gsl_vector *holder)
184 {
185 cdata = data = holder;
186 owner = false;
187 }
188
189 void reset_holder(const gsl_vector *holder)
190 {
191 cdata = holder;
192 owner = false;
193 }
194
195private:
196 gsl_vector *data;
197 const gsl_vector *cdata;
198 bool owner;
199};
200
201
202inline Vector operator + (const Vector& lhs, const Vector& rhs)
203{
204 Vector result(lhs);
205 gsl_vector_add(result, rhs);
206 return result;
207}
208
209inline Vector operator - (const Vector& lhs, const Vector& rhs)
210{
211 Vector result(lhs);
212 gsl_vector_sub(result, rhs);
213 return result;
214}
215
216inline Vector operator * (const Vector& lhs, double f)
217{
218 Vector result(lhs);
219 gsl_vector_scale(result, f);
220 return result;
221}
222
228{
229public:
230 VectorView(gsl_vector_view vv): m_view(vv)
231 {
232 reset_holder(&m_view.vector);
233 };
234private:
235 gsl_vector_view m_view;
236};
237
238
244{
245public:
246 ConstVectorView(gsl_vector_const_view vv): m_view(vv),
247 m_holder(&m_view.vector)
248 {
249 };
250
252 {
253 return m_holder.begin();
254 }
255
257 {
258 return m_holder.end();
259 }
260
262 {
263 return m_holder.size();
264 }
265
266 double operator[](size_t i)const
267 {
268 return m_holder[i];
269 };
270
271 operator const Vector& ()
272 {
273 return m_holder;
274 }
275
276 const gsl_vector *operator ->() const
277 {
278 return m_holder.operator ->();
279 }
280
282 {
283 return m_holder.operator Vector::vector_const_pointer_type();
284 }
285
286private:
287 gsl_vector_const_view m_view;
288 const Vector m_holder;
289};
290
291inline std::ostream& operator << (std::ostream& os, const Vector& v)
292{
293 v.print(os);
294 return os;
295}
296
297}
298
299#endif
300
301
Vector::const_iterator begin() const
Definition: gsl_vector.hh:251
Vector::size_type size() const
Definition: gsl_vector.hh:261
Vector::const_iterator end() const
Definition: gsl_vector.hh:256
ConstVectorView(gsl_vector_const_view vv)
Definition: gsl_vector.hh:246
double operator[](size_t i) const
Definition: gsl_vector.hh:266
VectorView(gsl_vector_view vv)
Definition: gsl_vector.hh:230
size_type size() const
gsl_vector vector_type
Definition: gsl_vector.hh:48
Vector(const Vector &other)
Vector(const gsl_vector *holder)
Vector(size_type size, const double *init)
double & reference
Definition: gsl_vector.hh:46
iterator end()
const_vector_iterator const_iterator
Definition: gsl_vector.hh:43
double value_type
Definition: gsl_vector.hh:45
bool is_valid() const
~Vector()
Destructor.
const double & const_reference
Definition: gsl_vector.hh:47
void reset_holder(gsl_vector *holder)
Definition: gsl_vector.hh:183
Vector(size_type size, bool clear)
vector_iterator iterator
Definition: gsl_vector.hh:42
const_iterator end() const
reference operator[](size_t i)
Definition: gsl_vector.hh:154
iterator begin()
void print(std::ostream &os) const
bool is_writable() const
Vector(gsl_vector *holder)
const gsl_vector * vector_const_pointer_type
Definition: gsl_vector.hh:50
Vector(Vector &&other)
gsl_vector * vector_pointer_type
Definition: gsl_vector.hh:49
void reset_holder(const gsl_vector *holder)
Definition: gsl_vector.hh:189
value_type operator[](size_t i) const
Definition: gsl_vector.hh:143
const_iterator begin() const
size_t size_type
Definition: gsl_vector.hh:44
#define EXPORT_GSL
Definition: gsl_defines.hh:38
vector_iterator operator-(const vector_iterator &it, int dist)
vector_iterator operator+(const vector_iterator &it, int dist)
Matrix EXPORT_GSL operator*(const Matrix &lhs, const Matrix &rhs)
std::ostream & operator<<(std::ostream &os, const Matrix &m)
Definition: gsl_matrix.hh:459