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 
28 namespace gsl
29 {
30 
32 {
33 public:
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 
141 private:
142  friend class const_vector_iterator;
143  double *m_current;
144  int m_stride;
145 };
146 
147 
148 inline vector_iterator operator + (const vector_iterator& it, int dist)
149 {
150  vector_iterator result(it);
151  result += dist;
152  return result;
153 }
154 
155 inline vector_iterator operator - (const vector_iterator& it, int dist)
156 {
157  vector_iterator result(it);
158  result -= dist;
159  return result;
160 }
161 
162 inline 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 {
172 public:
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 
184  const_vector_iterator(const const_vector_iterator& other) = default;
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 
294 private:
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 
323 namespace std
324 {
325 
326 template <>
327 class iterator_traits< gsl::const_vector_iterator >
328 {
329 public:
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 
337 template <>
338 class iterator_traits< gsl::vector_iterator >
339 {
340 public:
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
gsl::vector_iterator::reference
double & reference
Definition: gsl_iterator.hh:36
std::iterator_traits< gsl::vector_iterator >::pointer
double * pointer
Definition: gsl_iterator.hh:343
std::iterator_traits< gsl::const_vector_iterator >::value_type
double value_type
Definition: gsl_iterator.hh:331
gsl::const_vector_iterator::const_vector_iterator
const_vector_iterator(const vector_iterator &other)
Definition: gsl_iterator.hh:186
std::iterator_traits< gsl::const_vector_iterator >::difference_type
size_t difference_type
Definition: gsl_iterator.hh:330
gsl::const_vector_iterator::operator--
const_vector_iterator & operator--()
Definition: gsl_iterator.hh:221
gsl::vector_iterator::vector_iterator
vector_iterator()
Definition: gsl_iterator.hh:43
gsl::vector_iterator::operator-=
vector_iterator & operator-=(int dist)
Definition: gsl_iterator.hh:88
gsl::const_vector_iterator::operator++
const_vector_iterator & operator++()
Definition: gsl_iterator.hh:208
gsl::vector_iterator::vector_iterator
vector_iterator(double *base, int stride)
Definition: gsl_iterator.hh:39
gsl::const_vector_iterator::const_vector_iterator
const_vector_iterator()
Definition: gsl_iterator.hh:192
gsl::vector_iterator::operator->
double * operator->()
Definition: gsl_iterator.hh:50
gsl::vector_iterator::operator<
bool operator<(const vector_iterator &other) const
Definition: gsl_iterator.hh:117
gsl::operator-
vector_iterator operator-(const vector_iterator &it, int dist)
Definition: gsl_iterator.hh:155
gsl::vector_iterator::pointer
double * pointer
Definition: gsl_iterator.hh:35
gsl::operator+
vector_iterator operator+(const vector_iterator &it, int dist)
Definition: gsl_iterator.hh:148
gsl::const_vector_iterator::reference
const typedef double & reference
Definition: gsl_iterator.hh:175
std::iterator_traits< gsl::vector_iterator >::reference
double & reference
Definition: gsl_iterator.hh:344
gsl::const_vector_iterator
Definition: gsl_iterator.hh:170
gsl::vector_iterator::operator+=
vector_iterator & operator+=(int dist)
Definition: gsl_iterator.hh:82
gsl::vector_iterator::operator==
bool operator==(const vector_iterator &other) const
Definition: gsl_iterator.hh:105
gsl::const_vector_iterator::operator<=
bool operator<=(const const_vector_iterator &other) const
Definition: gsl_iterator.hh:275
std::iterator_traits< gsl::const_vector_iterator >::reference
const typedef double & reference
Definition: gsl_iterator.hh:333
std::iterator_traits< gsl::const_vector_iterator >::pointer
const typedef double * pointer
Definition: gsl_iterator.hh:332
gsl::vector_iterator::difference_type
size_t difference_type
Definition: gsl_iterator.hh:37
std::iterator_traits< gsl::vector_iterator >::iterator_category
random_access_iterator_tag iterator_category
Definition: gsl_iterator.hh:345
gsl::vector_iterator
Definition: gsl_iterator.hh:31
gsl::vector_iterator::operator--
vector_iterator & operator--()
Definition: gsl_iterator.hh:69
gsl::vector_iterator::operator++
vector_iterator & operator++()
Definition: gsl_iterator.hh:55
gsl::const_vector_iterator::operator-
difference_type operator-(const const_vector_iterator &other)
Definition: gsl_iterator.hh:246
gsl::vector_iterator::operator*
double & operator*()
Definition: gsl_iterator.hh:45
gsl::vector_iterator::value_type
double value_type
Definition: gsl_iterator.hh:34
gsl::vector_iterator::operator>=
bool operator>=(const vector_iterator &other) const
Definition: gsl_iterator.hh:135
gsl::const_vector_iterator::operator!=
bool operator!=(const const_vector_iterator &other) const
Definition: gsl_iterator.hh:263
gsl::const_vector_iterator::operator+=
const_vector_iterator & operator+=(int dist)
Definition: gsl_iterator.hh:234
gsl::vector_iterator::operator<=
bool operator<=(const vector_iterator &other) const
Definition: gsl_iterator.hh:123
gsl::const_vector_iterator::operator[]
const double & operator[](int idx) const
Definition: gsl_iterator.hh:252
std::iterator_traits< gsl::vector_iterator >::value_type
double value_type
Definition: gsl_iterator.hh:342
gsl::const_vector_iterator::operator->
const double * operator->() const
Definition: gsl_iterator.hh:203
std
Definition: gsl_iterator.hh:323
gsl::const_vector_iterator::operator>
bool operator>(const const_vector_iterator &other) const
Definition: gsl_iterator.hh:281
gsl::const_vector_iterator::operator>=
bool operator>=(const const_vector_iterator &other) const
Definition: gsl_iterator.hh:287
gsl::const_vector_iterator::value_type
const typedef double value_type
Definition: gsl_iterator.hh:173
gsl::const_vector_iterator::const_vector_iterator
const_vector_iterator(const double *base, int stride)
Definition: gsl_iterator.hh:179
gsl::const_vector_iterator::difference_type
size_t difference_type
Definition: gsl_iterator.hh:176
gsl::const_vector_iterator::operator<
bool operator<(const const_vector_iterator &other) const
Definition: gsl_iterator.hh:269
gsl::const_vector_iterator::operator==
bool operator==(const const_vector_iterator &other) const
Definition: gsl_iterator.hh:257
gsl::vector_iterator::operator>
bool operator>(const vector_iterator &other) const
Definition: gsl_iterator.hh:129
gsl::vector_iterator::operator!=
bool operator!=(const vector_iterator &other) const
Definition: gsl_iterator.hh:111
gsl::vector_iterator::operator-
difference_type operator-(const vector_iterator &other)
Definition: gsl_iterator.hh:99
gsl::vector_iterator::operator[]
double & operator[](int idx)
Definition: gsl_iterator.hh:94
std::iterator_traits< gsl::vector_iterator >::difference_type
size_t difference_type
Definition: gsl_iterator.hh:341
gsl
Definition: gsl_iterator.hh:28
gsl::const_vector_iterator::operator-=
const_vector_iterator & operator-=(int dist)
Definition: gsl_iterator.hh:240
std::iterator_traits< gsl::const_vector_iterator >::iterator_category
random_access_iterator_tag iterator_category
Definition: gsl_iterator.hh:334
gsl::const_vector_iterator::operator*
const double & operator*() const
Definition: gsl_iterator.hh:198
gsl::const_vector_iterator::pointer
const typedef double * pointer
Definition: gsl_iterator.hh:174