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 
31 public:
32  typedef double value_type;
33  typedef double* pointer;
34  typedef double& reference;
35  typedef size_t difference_type;
36 
37  vector_iterator(double *base, int stride): m_current(base), m_stride(stride){}
38 
39  vector_iterator(const vector_iterator& other) = default;
40 
41  vector_iterator():m_current(nullptr), m_stride(0){}
42 
43  double& operator *() {
44  return *m_current;
45  };
46 
47  double* operator ->() {
48  return m_current;
49  };
50 
52  m_current += m_stride;
53  return *this;
54  };
55 
57  vector_iterator result(*this);
58  ++(*this);
59  return result;
60  }
61 
62 
64  m_current -= m_stride;
65  return *this;
66  };
67 
69  vector_iterator result(*this);
70  --(*this);
71  return result;
72  }
73 
75  m_current += dist * m_stride;
76  return *this;
77  }
78 
80  m_current -= dist * m_stride;
81  return *this;
82  }
83 
84  double& operator[] (int idx) {
85  return m_current[idx * m_stride];
86  }
87 
88  difference_type operator - (const vector_iterator& other) {
89  assert(m_stride == other.m_stride);
90  return (m_current - other.m_current) / m_stride;
91  }
92 
93  bool operator == (const vector_iterator& other) const {
94  assert(m_stride == other.m_stride);
95  return m_current == other.m_current;
96  }
97 
98  bool operator != (const vector_iterator& other) const {
99  assert(m_stride == other.m_stride);
100  return m_current != other.m_current;
101  }
102 
103  bool operator < (const vector_iterator& other) const {
104  assert(m_stride == other.m_stride);
105  return m_current < other.m_current;
106  }
107 
108  bool operator <= (const vector_iterator& other) const {
109  assert(m_stride == other.m_stride);
110  return m_current <= other.m_current;
111  }
112 
113  bool operator > (const vector_iterator& other) const {
114  assert(m_stride == other.m_stride);
115  return m_current > other.m_current;
116  }
117 
118  bool operator >= (const vector_iterator& other) const {
119  assert(m_stride == other.m_stride);
120  return m_current >= other.m_current;
121  }
122 
123 private:
124  friend class const_vector_iterator;
125  double* m_current;
126  int m_stride;
127 };
128 
129 
130 inline vector_iterator operator + (const vector_iterator& it, int dist)
131 {
132  vector_iterator result(it);
133  result += dist;
134  return result;
135 }
136 
137 inline vector_iterator operator - (const vector_iterator& it, int dist)
138 {
139  vector_iterator result(it);
140  result -= dist;
141  return result;
142 }
143 
144 inline vector_iterator operator + (int dist, const vector_iterator& it)
145 {
146  vector_iterator result(it);
147  result += dist;
148  return result;
149 }
150 
151 
153 public:
154  typedef const double value_type;
155  typedef const double* pointer;
156  typedef const double& reference;
157  typedef size_t difference_type;
158 
159 
160  const_vector_iterator(const double *base, int stride):
161  m_current(base), m_stride(stride)
162  {
163  }
164 
165  const_vector_iterator(const const_vector_iterator& other) = default;
166 
168  m_current(other.m_current),
169  m_stride(other.m_stride)
170  {
171  }
172 
174  m_current(nullptr),
175  m_stride(0)
176  {
177  }
178 
179  const double& operator *() const {
180  return *m_current;
181  };
182 
183  const double* operator ->() const {
184  return m_current;
185  };
186 
188  m_current += m_stride;
189  return *this;
190  };
191 
193  const_vector_iterator result(*this);
194  ++(*this);
195  return result;
196  }
197 
199  m_current -= m_stride;
200  return *this;
201  };
202 
204  const_vector_iterator result(*this);
205  --(*this);
206  return result;
207  }
208 
210  m_current += dist * m_stride;
211  return *this;
212  }
213 
215  m_current -= dist * m_stride;
216  return *this;
217  }
218 
219  difference_type operator - (const const_vector_iterator& other) {
220  assert(m_stride == other.m_stride);
221  return (m_current - other.m_current) / m_stride;
222  }
223 
224  const double& operator[] (int idx) const {
225  return m_current[idx * m_stride];
226  }
227 
228  bool operator == (const const_vector_iterator& other) const {
229  assert(m_stride == other.m_stride);
230  return m_current == other.m_current;
231  }
232 
233  bool operator != (const const_vector_iterator& other) const {
234  assert(m_stride == other.m_stride);
235  return m_current != other.m_current;
236  }
237 
238  bool operator < (const const_vector_iterator& other) const {
239  assert(m_stride == other.m_stride);
240  return m_current < other.m_current;
241  }
242 
243  bool operator <= (const const_vector_iterator& other) const {
244  assert(m_stride == other.m_stride);
245  return m_current <= other.m_current;
246  }
247 
248  bool operator > (const const_vector_iterator& other) const {
249  assert(m_stride == other.m_stride);
250  return m_current > other.m_current;
251  }
252 
253  bool operator >= (const const_vector_iterator& other) const {
254  assert(m_stride == other.m_stride);
255  return m_current >= other.m_current;
256  }
257 
258 
259 private:
260  const double* m_current;
261  int m_stride;
262 };
263 
264 
266 {
267  const_vector_iterator result(it);
268  result += dist;
269  return result;
270 }
271 
273 {
274  const_vector_iterator result(it);
275  result -= dist;
276  return result;
277 }
278 
280 {
281  const_vector_iterator result(it);
282  result += dist;
283  return result;
284 }
285 
286 }
287 
288 namespace std {
289 
290 template <>
291 class iterator_traits< gsl::const_vector_iterator > {
292 public:
293  typedef size_t difference_type;
294  typedef double value_type;
295  typedef const double* pointer;
296  typedef const double& reference;
297  typedef random_access_iterator_tag iterator_category;
298 };
299 
300 template <>
301 class iterator_traits< gsl::vector_iterator > {
302 public:
303  typedef size_t difference_type;
304  typedef double value_type;
305  typedef double* pointer;
306  typedef double& reference;
307  typedef random_access_iterator_tag iterator_category;
308 };
309 
310 }
311 
312 #endif
bool operator!=(const vector_iterator &other) const
Definition: gsl_iterator.hh:98
vector_iterator(double *base, int stride)
Definition: gsl_iterator.hh:37
bool operator>(const vector_iterator &other) const
const_vector_iterator(const vector_iterator &other)
double & operator[](int idx)
Definition: gsl_iterator.hh:84
vector_iterator & operator-=(int dist)
Definition: gsl_iterator.hh:79
bool operator<(const vector_iterator &other) const
friend class const_vector_iterator
vector_iterator & operator--()
Definition: gsl_iterator.hh:63
vector_iterator operator+(const vector_iterator &it, int dist)
vector_iterator & operator+=(int dist)
Definition: gsl_iterator.hh:74
bool operator==(const vector_iterator &other) const
Definition: gsl_iterator.hh:93
vector_iterator & operator++()
Definition: gsl_iterator.hh:51
random_access_iterator_tag iterator_category
bool operator>=(const vector_iterator &other) const
difference_type operator-(const vector_iterator &other)
Definition: gsl_iterator.hh:88
const_vector_iterator(const double *base, int stride)
bool operator<=(const vector_iterator &other) const