My Project
gsl_iterator.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_iterator_hh
22#define gslpp_iterator_hh
23
24#include <cassert>
25#include <iterator>
26#include <iostream>
27
28namespace gsl
29{
30
32{
33public:
34 typedef double value_type;
35 typedef double *pointer;
36 typedef double& reference;
37 typedef size_t difference_type;
38
39 vector_iterator(double *base, int stride): m_current(base), m_stride(stride) {}
40
41 vector_iterator(const vector_iterator& other) = default;
42
43 vector_iterator(): m_current(nullptr), m_stride(0) {}
44
45 double& operator *()
46 {
47 return *m_current;
48 };
49
50 double *operator ->()
51 {
52 return m_current;
53 };
54
56 {
57 m_current += m_stride;
58 return *this;
59 };
60
62 {
63 vector_iterator result(*this);
64 ++(*this);
65 return result;
66 }
67
68
70 {
71 m_current -= m_stride;
72 return *this;
73 };
74
76 {
77 vector_iterator result(*this);
78 --(*this);
79 return result;
80 }
81
83 {
84 m_current += dist * m_stride;
85 return *this;
86 }
87
89 {
90 m_current -= dist * m_stride;
91 return *this;
92 }
93
94 double& operator[] (int idx)
95 {
96 return m_current[idx * m_stride];
97 }
98
100 {
101 assert(m_stride == other.m_stride);
102 return (m_current - other.m_current) / m_stride;
103 }
104
105 bool operator == (const vector_iterator& other) const
106 {
107 assert(m_stride == other.m_stride);
108 return m_current == other.m_current;
109 }
110
111 bool operator != (const vector_iterator& other) const
112 {
113 assert(m_stride == other.m_stride);
114 return m_current != other.m_current;
115 }
116
117 bool operator < (const vector_iterator& other) const
118 {
119 assert(m_stride == other.m_stride);
120 return m_current < other.m_current;
121 }
122
123 bool operator <= (const vector_iterator& other) const
124 {
125 assert(m_stride == other.m_stride);
126 return m_current <= other.m_current;
127 }
128
129 bool operator > (const vector_iterator& other) const
130 {
131 assert(m_stride == other.m_stride);
132 return m_current > other.m_current;
133 }
134
135 bool operator >= (const vector_iterator& other) const
136 {
137 assert(m_stride == other.m_stride);
138 return m_current >= other.m_current;
139 }
140
141private:
143 double *m_current;
144 int m_stride;
145};
146
147
148inline vector_iterator operator + (const vector_iterator& it, int dist)
149{
150 vector_iterator result(it);
151 result += dist;
152 return result;
153}
154
155inline vector_iterator operator - (const vector_iterator& it, int dist)
156{
157 vector_iterator result(it);
158 result -= dist;
159 return result;
160}
161
162inline vector_iterator operator + (int dist, const vector_iterator& it)
163{
164 vector_iterator result(it);
165 result += dist;
166 return result;
167}
168
169
171{
172public:
173 typedef const double value_type;
174 typedef const double *pointer;
175 typedef const double& reference;
176 typedef size_t difference_type;
177
178
179 const_vector_iterator(const double *base, int stride):
180 m_current(base), m_stride(stride)
181 {
182 }
183
185
187 m_current(other.m_current),
188 m_stride(other.m_stride)
189 {
190 }
191
193 m_current(nullptr),
194 m_stride(0)
195 {
196 }
197
198 const double& operator *() const
199 {
200 return *m_current;
201 };
202
203 const double *operator ->() const
204 {
205 return m_current;
206 };
207
209 {
210 m_current += m_stride;
211 return *this;
212 };
213
215 {
216 const_vector_iterator result(*this);
217 ++(*this);
218 return result;
219 }
220
222 {
223 m_current -= m_stride;
224 return *this;
225 };
226
228 {
229 const_vector_iterator result(*this);
230 --(*this);
231 return result;
232 }
233
235 {
236 m_current += dist * m_stride;
237 return *this;
238 }
239
241 {
242 m_current -= dist * m_stride;
243 return *this;
244 }
245
247 {
248 assert(m_stride == other.m_stride);
249 return (m_current - other.m_current) / m_stride;
250 }
251
252 const double& operator[] (int idx) const
253 {
254 return m_current[idx * m_stride];
255 }
256
257 bool operator == (const const_vector_iterator& other) const
258 {
259 assert(m_stride == other.m_stride);
260 return m_current == other.m_current;
261 }
262
263 bool operator != (const const_vector_iterator& other) const
264 {
265 assert(m_stride == other.m_stride);
266 return m_current != other.m_current;
267 }
268
269 bool operator < (const const_vector_iterator& other) const
270 {
271 assert(m_stride == other.m_stride);
272 return m_current < other.m_current;
273 }
274
275 bool operator <= (const const_vector_iterator& other) const
276 {
277 assert(m_stride == other.m_stride);
278 return m_current <= other.m_current;
279 }
280
281 bool operator > (const const_vector_iterator& other) const
282 {
283 assert(m_stride == other.m_stride);
284 return m_current > other.m_current;
285 }
286
287 bool operator >= (const const_vector_iterator& other) const
288 {
289 assert(m_stride == other.m_stride);
290 return m_current >= other.m_current;
291 }
292
293
294private:
295 const double *m_current;
296 int m_stride;
297};
298
299
301{
302 const_vector_iterator result(it);
303 result += dist;
304 return result;
305}
306
308{
309 const_vector_iterator result(it);
310 result -= dist;
311 return result;
312}
313
315{
316 const_vector_iterator result(it);
317 result += dist;
318 return result;
319}
320
321}
322
323namespace std
324{
325
326template <>
327class iterator_traits< gsl::const_vector_iterator >
328{
329public:
330 typedef size_t difference_type;
331 typedef double value_type;
332 typedef const double *pointer;
333 typedef const double& reference;
334 typedef random_access_iterator_tag iterator_category;
335};
336
337template <>
338class iterator_traits< gsl::vector_iterator >
339{
340public:
341 typedef size_t difference_type;
342 typedef double value_type;
343 typedef double *pointer;
344 typedef double& reference;
345 typedef random_access_iterator_tag iterator_category;
346};
347
348}
349
350#endif
difference_type operator-(const const_vector_iterator &other)
const_vector_iterator(const const_vector_iterator &other)=default
bool operator<=(const const_vector_iterator &other) const
bool operator>=(const const_vector_iterator &other) const
const_vector_iterator(const double *base, int stride)
const_vector_iterator & operator--()
const double * operator->() const
bool operator<(const const_vector_iterator &other) const
const double & operator[](int idx) const
const double & operator*() const
const_vector_iterator & operator++()
const_vector_iterator & operator-=(int dist)
bool operator!=(const const_vector_iterator &other) const
const_vector_iterator(const vector_iterator &other)
bool operator==(const const_vector_iterator &other) const
bool operator>(const const_vector_iterator &other) const
const_vector_iterator & operator+=(int dist)
bool operator==(const vector_iterator &other) const
bool operator>(const vector_iterator &other) const
vector_iterator(double *base, int stride)
Definition: gsl_iterator.hh:39
vector_iterator & operator+=(int dist)
Definition: gsl_iterator.hh:82
vector_iterator(const vector_iterator &other)=default
difference_type operator-(const vector_iterator &other)
Definition: gsl_iterator.hh:99
vector_iterator & operator-=(int dist)
Definition: gsl_iterator.hh:88
bool operator<=(const vector_iterator &other) const
vector_iterator & operator++()
Definition: gsl_iterator.hh:55
bool operator!=(const vector_iterator &other) const
double & operator[](int idx)
Definition: gsl_iterator.hh:94
vector_iterator & operator--()
Definition: gsl_iterator.hh:69
bool operator>=(const vector_iterator &other) const
bool operator<(const vector_iterator &other) const
random_access_iterator_tag iterator_category
vector_iterator operator-(const vector_iterator &it, int dist)
vector_iterator operator+(const vector_iterator &it, int dist)