cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vctFixedStrideVectorIterator.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Ofri Sadowsky, Anton Deguet
7  Created on: 2003-09-30
8 
9  (C) Copyright 2003-2013 Johns Hopkins University (JHU), All Rights
10  Reserved.
11 
12 --- begin cisst license - do not edit ---
13 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 */
20 
21 #pragma once
22 #ifndef _vctFixedStrideVectorIterator_h
23 #define _vctFixedStrideVectorIterator_h
24 
32 
33 #include <iterator>
34 
61 template <class _elementType, vct::stride_type _stride>
63  public std::iterator<std::random_access_iterator_tag, _elementType>
64 {
65 public:
66  /* define most types from vctContainerTraits */
67  VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType);
68 
71 
73  typedef std::iterator<std::random_access_iterator_tag, _elementType> BaseType;
74 
77  typedef typename BaseType::iterator_category iterator_category;
78 
79  /* Stride between the elements of a vector. */
80  enum {STRIDE = _stride};
81 
82 protected:
89  value_type * DataPtr;
90 
91 public:
94  {}
95 
96 
99  explicit vctFixedStrideVectorConstIterator(value_type * dataPtr)
100  : DataPtr(dataPtr)
101  {}
102 
103 
112  explicit vctFixedStrideVectorConstIterator(const value_type * dataPtr)
113  : DataPtr(const_cast<value_type *>(dataPtr))
114  {}
115 
116 
119  DataPtr += STRIDE;
120  return *this;
121  }
122 
123 
126  ThisType tmp(*this);
127  ++(*this);
128  return tmp;
129  }
130 
131 
134  DataPtr -= STRIDE;
135  return *this;
136  }
137 
138 
141  ThisType tmp(*this);
142  --(*this);
143  return tmp;
144  }
145 
146 
149  DataPtr += difference * STRIDE;
150  return *this;
151  }
152 
153 
156  DataPtr -= difference * STRIDE;
157  return *this;
158  }
159 
160 
166  difference_type operator-(const ThisType & other) const {
167  return (DataPtr - other.DataPtr) / STRIDE;
168  }
169 
170 
172  const value_type & operator[](difference_type index) const {
173  return *(DataPtr + index * STRIDE);
174  }
175 
176 
178  const value_type & operator* () const {
179  return *DataPtr;
180  }
181 
182 
188  bool operator< (const ThisType & other) const {
189  return ((*this) - other) < 0;
190  }
191  bool operator<= (const ThisType & other) const {
192  return (*this < other) || (*this == other);
193  }
194 
195 
197  bool operator== (const ThisType & other) const {
198  return DataPtr == other.DataPtr;
199  }
200 
201 
203  bool operator> (const ThisType & other) const {
204  return other < (*this);
205  }
206  bool operator>= (const ThisType & other) const {
207  return (*this > other) || (*this == other);
208  }
209 
210 
212  bool operator != (const ThisType & other) const {
213  return !( (*this) == other );
214  }
215 };
216 
217 
218 
219 
223 template<class _elementType, vct::stride_type _stride>
225 {
226 public:
227  /* documented in base class */
228  VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType);
232 
235  : BaseType()
236  {}
237 
238 
243  explicit vctFixedStrideVectorIterator(value_type * dataPtr)
244  : BaseType(dataPtr)
245  {}
246 
247 
251  this->DataPtr += this->STRIDE;
252  return *this;
253  }
254 
255 
259  ThisType tmp(*this);
260  this->DataPtr += this->STRIDE;
261  return tmp;
262  }
263 
264 
268  this->DataPtr -= this->STRIDE;
269  return *this;
270  }
271 
272 
276  ThisType tmp(*this);
277  this->DataPtr -= this->STRIDE;
278  return tmp;
279  }
280 
281 
285  this->DataPtr += difference * this->STRIDE;
286  return *this;
287  }
288 
289 
293  this->DataPtr -= difference * this->STRIDE;
294  return *this;
295  }
296 
297 
299  value_type & operator[](difference_type index) const {
300  return *(this->DataPtr + index * this->STRIDE);
301  }
302 
303 
305  value_type & operator* () {
306  return *(this->DataPtr);
307  }
308 };
309 
310 
311 
313 template<class _elementType, vct::stride_type _stride>
317 {
319  return result += difference;
320 }
321 
322 
324 template<class _elementType, vct::stride_type _stride>
328 {
330  return result += difference;
331 }
332 
333 
335 template<class _elementType, vct::stride_type _stride>
339 {
341  return result -= difference;
342 }
343 
344 
349 template<class _elementType, vct::stride_type _stride>
353 {
355  return result += difference;
356 }
357 
358 
360 template<class _elementType, vct::stride_type _stride>
364 {
366  return result += difference;
367 }
368 
369 
371 template<class _elementType, vct::stride_type _stride>
375 {
377  return result -= difference;
378 }
379 
380 
381 #endif // _vctFixedStrideVectorIterator_h
ThisType & operator++()
Definition: vctFixedStrideVectorIterator.h:118
Definition: vctFixedStrideVectorIterator.h:224
value_type * DataPtr
Definition: vctFixedStrideVectorIterator.h:89
Definition: vctFixedStrideVectorIterator.h:80
value_type & operator*()
Definition: vctFixedStrideVectorIterator.h:305
vctFixedStrideVectorConstIterator(value_type *dataPtr)
Definition: vctFixedStrideVectorIterator.h:99
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
Portability across compilers and operating systems tools.
bool operator>=(const ThisType &other) const
Definition: vctFixedStrideVectorIterator.h:206
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
ThisType & operator-=(difference_type difference)
Definition: vctFixedStrideVectorIterator.h:292
ThisType & operator+=(difference_type difference)
Definition: vctFixedStrideVectorIterator.h:148
const value_type & operator*() const
Definition: vctFixedStrideVectorIterator.h:178
vctFixedStrideVectorIterator< _elementType, _stride > ThisType
Definition: vctFixedStrideVectorIterator.h:229
vctFixedStrideVectorConstIterator()
Definition: vctFixedStrideVectorIterator.h:93
ThisType & operator-=(difference_type difference)
Definition: vctFixedStrideVectorIterator.h:155
vctFixedStrideVectorConstIterator< _elementType, _stride > ThisType
Definition: vctFixedStrideVectorIterator.h:70
vctFixedStrideVectorConstIterator< _elementType, _stride > operator+(const vctFixedStrideVectorConstIterator< _elementType, _stride > &iterator, typename vctFixedStrideVectorConstIterator< _elementType, _stride >::difference_type difference)
Definition: vctFixedStrideVectorIterator.h:315
Definition: vctFixedStrideVectorIterator.h:62
bool operator<=(const ThisType &other) const
Definition: vctFixedStrideVectorIterator.h:191
ptrdiff_t difference_type
Definition: vctContainerTraits.h:38
bool operator<(const ThisType &other) const
Definition: vctFixedStrideVectorIterator.h:188
ThisType & operator--()
Definition: vctFixedStrideVectorIterator.h:267
ThisType & operator--()
Definition: vctFixedStrideVectorIterator.h:133
ThisType operator--(int)
Definition: vctFixedStrideVectorIterator.h:275
Basic traits for the cisstVector containers.
vctFixedStrideVectorIterator(value_type *dataPtr)
Definition: vctFixedStrideVectorIterator.h:243
vctFixedStrideVectorConstIterator< _elementType, _stride > operator-(const vctFixedStrideVectorConstIterator< _elementType, _stride > &iterator, typename vctFixedStrideVectorConstIterator< _elementType, _stride >::difference_type difference)
Definition: vctFixedStrideVectorIterator.h:337
ThisType & operator++()
Definition: vctFixedStrideVectorIterator.h:250
bool operator>(const ThisType &other) const
Definition: vctFixedStrideVectorIterator.h:203
vctFixedStrideVectorConstIterator< _elementType, _stride > BaseType
Definition: vctFixedStrideVectorIterator.h:230
vctFixedStrideVectorIterator()
Definition: vctFixedStrideVectorIterator.h:234
bool operator!=(const ThisType &other) const
Definition: vctFixedStrideVectorIterator.h:212
const value_type & operator[](difference_type index) const
Definition: vctFixedStrideVectorIterator.h:172
ThisType operator++(int)
Definition: vctFixedStrideVectorIterator.h:125
bool operator==(const ThisType &other) const
Definition: vctFixedStrideVectorIterator.h:197
BaseType::iterator_category iterator_category
Definition: vctFixedStrideVectorIterator.h:77
ThisType operator--(int)
Definition: vctFixedStrideVectorIterator.h:140
std::iterator< std::random_access_iterator_tag, _elementType > BaseType
Definition: vctFixedStrideVectorIterator.h:73
ThisType operator++(int)
Definition: vctFixedStrideVectorIterator.h:258
ThisType & operator+=(difference_type difference)
Definition: vctFixedStrideVectorIterator.h:284
BaseType::iterator_category iterator_category
Definition: vctFixedStrideVectorIterator.h:231
vctFixedStrideVectorConstIterator(const value_type *dataPtr)
Definition: vctFixedStrideVectorIterator.h:112
value_type & operator[](difference_type index) const
Definition: vctFixedStrideVectorIterator.h:299
difference_type operator-(const ThisType &other) const
Definition: vctFixedStrideVectorIterator.h:166