cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vctVarStrideVectorIterator.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: 2004-07-02
8 
9  (C) Copyright 2004-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 _vctVarStrideVectorIterator_h
23 #define _vctVarStrideVectorIterator_h
24 
30 #include <iterator>
32 
33 
55 template<class _elementType>
57  public std::iterator<std::random_access_iterator_tag, _elementType>
58 {
59 public:
60  /* define most types from vctContainerTraits */
61  VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType);
62 
65 
67  typedef std::iterator<std::random_access_iterator_tag, _elementType> BaseType;
68 
71  typedef typename BaseType::iterator_category iterator_category;
72 
73 protected:
80  value_type * DataPtr;
81 
82  /* Stride between the elements of a vector. */
84 
85 
86 
87 public:
90  DataPtr(0),
91  Stride(1)
92  {}
93 
94 
97  explicit vctVarStrideVectorConstIterator(value_type * dataPtr, difference_type stride = 1)
98  : DataPtr(dataPtr), Stride(stride)
99  {}
100 
101 
110  explicit vctVarStrideVectorConstIterator(const value_type * dataPtr, difference_type stride = 1)
111  : DataPtr(const_cast<value_type *>(dataPtr)), Stride(stride)
112  {}
113 
114 
117  DataPtr += Stride;
118  return *this;
119  }
120 
121 
124  ThisType tmp(*this);
125  ++(*this);
126  return tmp;
127  }
128 
129 
132  DataPtr -= Stride;
133  return *this;
134  }
135 
136 
139  ThisType tmp(*this);
140  --(*this);
141  return tmp;
142  }
143 
144 
147  DataPtr += difference * Stride;
148  return *this;
149  }
150 
151 
154  DataPtr -= difference * Stride;
155  return *this;
156  }
157 
158 
164  difference_type operator-(const ThisType & other) const {
165  return (DataPtr - other.DataPtr) / Stride;
166  }
167 
168 
170  const value_type & operator[](difference_type index) const {
171  return *(DataPtr + index * Stride);
172  }
173 
174 
176  const value_type & operator* () const {
177  return *DataPtr;
178  }
179 
180 
186  bool operator< (const ThisType & other) const {
187  return ((*this) - other) < 0;
188  }
189  bool operator<= (const ThisType & other) const {
190  return ((*this) < other) || ((*this) == other);
191  }
192 
193 
195  bool operator== (const ThisType & other) const {
196  return DataPtr == other.DataPtr;
197  }
198 
199 
201  bool operator> (const ThisType & other) const {
202  return other < (*this);
203  }
204  bool operator>= (const ThisType & other) const {
205  return ((*this) > other) || ((*this) == other);
206  }
207 
208 
210  bool operator != (const ThisType & other) const {
211  return !( (*this) == other );
212  }
213 };
214 
215 
216 
217 
221 template<class _elementType>
223 {
224 public:
225  /* documented in base class */
226  VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType);
230 
233  : BaseType()
234  {}
235 
236 
241  explicit vctVarStrideVectorIterator(value_type * dataPtr, difference_type stride = 1)
242  : BaseType(dataPtr, stride)
243  {}
244 
245 
249  this->DataPtr += this->Stride;
250  return *this;
251  }
252 
253 
257  ThisType tmp(*this);
258  this->DataPtr += this->Stride;
259  return tmp;
260  }
261 
262 
266  this->DataPtr -= this->Stride;
267  return *this;
268  }
269 
270 
274  ThisType tmp(*this);
275  this->DataPtr -= this->Stride;
276  return tmp;
277  }
278 
279 
283  this->DataPtr += difference * this->Stride;
284  return *this;
285  }
286 
287 
291  this->DataPtr -= difference * this->Stride;
292  return *this;
293  }
294 
295 
297  value_type & operator[](difference_type index) const {
298  return *(this->DataPtr + index * this->Stride);
299  }
300 
301 
303  value_type & operator* () {
304  return *(this->DataPtr);
305  }
306 };
307 
308 
309 
311 template<class _elementType>
315 {
317  result += difference;
318  return result;
319 }
320 
321 
323 template<class _elementType>
327 {
329  return result += difference;
330 }
331 
332 
334 template<class _elementType>
338 {
340  return result -= difference;
341 }
342 
343 
348 template<class _elementType>
352 {
354  return result += difference;
355 }
356 
357 
359 template<class _elementType>
363 {
365  return result += difference;
366 }
367 
368 
370 template<class _elementType>
374 {
376  return result -= difference;
377 }
378 
379 
380 #endif // _vctVarStrideVectorIterator_h
381 
ThisType & operator--()
Definition: vctVarStrideVectorIterator.h:131
value_type & operator[](difference_type index) const
Definition: vctVarStrideVectorIterator.h:297
ThisType operator--(int)
Definition: vctVarStrideVectorIterator.h:273
vctVarStrideVectorConstIterator< _elementType > ThisType
Definition: vctVarStrideVectorIterator.h:64
vctVarStrideVectorConstIterator(value_type *dataPtr, difference_type stride=1)
Definition: vctVarStrideVectorIterator.h:97
ThisType & operator--()
Definition: vctVarStrideVectorIterator.h:265
value_type * DataPtr
Definition: vctVarStrideVectorIterator.h:80
bool operator!=(const ThisType &other) const
Definition: vctVarStrideVectorIterator.h:210
vctVarStrideVectorConstIterator< _elementType > operator-(const vctVarStrideVectorConstIterator< _elementType > &iterator, typename vctVarStrideVectorConstIterator< _elementType >::difference_type difference)
Definition: vctVarStrideVectorIterator.h:336
ThisType operator++(int)
Definition: vctVarStrideVectorIterator.h:256
vctVarStrideVectorIterator< _elementType > ThisType
Definition: vctVarStrideVectorIterator.h:227
bool operator<(const ThisType &other) const
Definition: vctVarStrideVectorIterator.h:186
ThisType & operator-=(difference_type difference)
Definition: vctVarStrideVectorIterator.h:153
BaseType::iterator_category iterator_category
Definition: vctVarStrideVectorIterator.h:229
ThisType & operator+=(difference_type difference)
Definition: vctVarStrideVectorIterator.h:282
BaseType::iterator_category iterator_category
Definition: vctVarStrideVectorIterator.h:71
vctVarStrideVectorIterator()
Definition: vctVarStrideVectorIterator.h:232
ThisType & operator++()
Definition: vctVarStrideVectorIterator.h:248
vctVarStrideVectorIterator(value_type *dataPtr, difference_type stride=1)
Definition: vctVarStrideVectorIterator.h:241
ThisType & operator+=(difference_type difference)
Definition: vctVarStrideVectorIterator.h:146
ThisType & operator-=(difference_type difference)
Definition: vctVarStrideVectorIterator.h:290
ptrdiff_t difference_type
Definition: vctContainerTraits.h:38
vctVarStrideVectorConstIterator(const value_type *dataPtr, difference_type stride=1)
Definition: vctVarStrideVectorIterator.h:110
bool operator<=(const ThisType &other) const
Definition: vctVarStrideVectorIterator.h:189
value_type & operator*()
Definition: vctVarStrideVectorIterator.h:303
std::iterator< std::random_access_iterator_tag, _elementType > BaseType
Definition: vctVarStrideVectorIterator.h:67
ThisType & operator++()
Definition: vctVarStrideVectorIterator.h:116
bool operator==(const ThisType &other) const
Definition: vctVarStrideVectorIterator.h:195
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
Basic traits for the cisstVector containers.
vctVarStrideVectorConstIterator< _elementType > BaseType
Definition: vctVarStrideVectorIterator.h:228
difference_type operator-(const ThisType &other) const
Definition: vctVarStrideVectorIterator.h:164
ThisType operator++(int)
Definition: vctVarStrideVectorIterator.h:123
vctVarStrideVectorConstIterator()
Definition: vctVarStrideVectorIterator.h:89
Definition: vctVarStrideVectorIterator.h:222
bool operator>=(const ThisType &other) const
Definition: vctVarStrideVectorIterator.h:204
vctVarStrideVectorConstIterator< _elementType > operator+(const vctVarStrideVectorConstIterator< _elementType > &iterator, typename vctVarStrideVectorConstIterator< _elementType >::difference_type difference)
Definition: vctVarStrideVectorIterator.h:313
const value_type & operator[](difference_type index) const
Definition: vctVarStrideVectorIterator.h:170
bool operator>(const ThisType &other) const
Definition: vctVarStrideVectorIterator.h:201
const value_type & operator*() const
Definition: vctVarStrideVectorIterator.h:176
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
Definition: vctVarStrideVectorIterator.h:56
difference_type Stride
Definition: vctVarStrideVectorIterator.h:83
ThisType operator--(int)
Definition: vctVarStrideVectorIterator.h:138