cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vctVarStrideMatrixIterator.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 _vctVarStrideMatrixIterator_h
23 #define _vctVarStrideMatrixIterator_h
24 
30 #include <iterator>
32 
33 
39 template<class _elementType>
41  public std::iterator<std::random_access_iterator_tag, _elementType>
42 {
43 public:
44  /* define most types from vctContainerTraits */
45  VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType);
46 
49 
51  typedef std::iterator<std::random_access_iterator_tag, _elementType> BaseType;
52 
55  typedef typename BaseType::iterator_category iterator_category;
56 
57 protected:
64  value_type * DataPtr;
65 
66  /* Strides between the elements of a matrix. */
71 
77  inline void WrapToRight() {
78  if (CurrentColumn >= NumColumns) {
81  }
82  }
83 
84 
89  inline void WrapToLeft() {
90  if (CurrentColumn < 0) {
93  }
94  }
95 
96 
97 public:
100  DataPtr(0),
101  ColumnStride(1),
102  NumColumns(0),
103  RowStride(1),
104  CurrentColumn(0)
105  {}
106 
107 
110  vctVarStrideMatrixConstIterator(value_type * dataPtr, stride_type columnStride,
111  size_type numColumns, stride_type rowStride,
112  size_type initialColumn = 0):
113  DataPtr(dataPtr),
114  ColumnStride(columnStride),
115  NumColumns(numColumns),
116  RowStride(rowStride),
117  CurrentColumn(initialColumn)
118  {}
119 
120 
129  vctVarStrideMatrixConstIterator(const value_type * dataPtr, difference_type columnStride,
130  size_type numColumns, stride_type rowStride,
131  size_type initialColumn = 0):
132  DataPtr(const_cast<value_type *>(dataPtr)),
133  ColumnStride(columnStride),
134  NumColumns(numColumns),
135  RowStride(rowStride),
136  CurrentColumn(initialColumn)
137  {}
138 
139 
143  ++CurrentColumn;
144  WrapToRight();
145  return *this;
146  }
147 
148 
151  ThisType tmp(*this);
152  ++(*this);
153  return tmp;
154  }
155 
156 
160  --CurrentColumn;
161  WrapToLeft();
162  return *this;
163  }
164 
165 
168  ThisType tmp(*this);
169  --(*this);
170  return tmp;
171  }
172 
173 
178  DataPtr += (difference / NumColumns) * RowStride + (difference % NumColumns) * ColumnStride;
179  CurrentColumn += difference % NumColumns;
180  if (difference >= 0)
181  WrapToRight();
182  else
183  WrapToLeft();
184  return *this;
185  }
186 
187 
192  DataPtr -= (difference / NumColumns) * RowStride + (difference % NumColumns) * ColumnStride;
193  CurrentColumn -= difference % NumColumns;
194  if (difference >= 0)
195  WrapToLeft();
196  else
197  WrapToRight();
198  return *this;
199  }
200 
201 
219  difference_type operator-(const ThisType & other) const {
220  const value_type * beginThisRow = DataPtr - CurrentColumn * ColumnStride;
221  const value_type * beginThatRow = other.DataPtr - other.CurrentColumn * ColumnStride;
222  const difference_type rowDiff = (beginThisRow - beginThatRow) / RowStride;
223  const difference_type colDiff = CurrentColumn - other.CurrentColumn;
224  const difference_type result = rowDiff * NumColumns + colDiff;
225  return result;
226  }
227 
228 
230  const value_type & operator[](difference_type index) const {
231  ThisType ptrCalc(*this);
232  ptrCalc += index;
233  return *ptrCalc;
234  }
235 
236 
238  const value_type & operator* () const {
239  return *DataPtr;
240  }
241 
242 
250  bool operator< (const ThisType & other) const {
251  return ((*this) - other) < 0;
252  }
253  bool operator<= (const ThisType & other) const {
254  return ((*this) < other) || ((*this) == other);;
255  }
256 
257 
259  bool operator== (const ThisType & other) const {
260  return (DataPtr == other.DataPtr) && (CurrentColumn == other.CurrentColumn);
261  }
262 
263 
265  bool operator> (const ThisType & other) const {
266  return other < (*this);
267  }
268  bool operator>= (const ThisType & other) const {
269  return ((*this) > other) || ((*this) == other);;
270  }
271 
272 
274  bool operator != (const ThisType & other) const {
275  return !( (*this) == other );
276  }
277 
278 };
279 
280 
281 
282 
286 template<class _elementType>
288 {
289 public:
290  /* documented in base class */
291  VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType);
295 
298  BaseType()
299  {}
300 
305  vctVarStrideMatrixIterator(value_type * dataPtr, stride_type columnStride,
306  size_type numColumns, stride_type rowStride,
307  size_type initialColumn = 0):
308  BaseType(dataPtr, columnStride, numColumns, rowStride, initialColumn)
309  {}
310 
311 
315  this->DataPtr += this->ColumnStride;
316  ++(this->CurrentColumn);
317  this->WrapToRight();
318  return *this;
319  }
320 
321 
325  ThisType tmp(*this);
326  ++(*this);
327  return tmp;
328  }
329 
330 
334  this->DataPtr -= this->ColumnStride;
335  --(this->CurrentColumn);
336  this->WrapToLeft();
337  return *this;
338  }
339 
340 
344  ThisType tmp(*this);
345  --(*this);
346  return tmp;
347  }
348 
349 
356  if (this->NumColumns == 0)
357  return *this;
358  this->DataPtr += (difference / this->NumColumns) * this->RowStride + (difference % this->NumColumns) * this->ColumnStride;
359  this->CurrentColumn += difference % this->NumColumns;
360  if (difference >= 0)
361  this->WrapToRight();
362  else
363  this->WrapToLeft();
364  return *this;
365  }
366 
367 
374  if (this->NumColumns == 0)
375  return *this;
376  this->DataPtr -= (difference / this->NumColumns) * this->RowStride + (difference % this->NumColumns) * this->ColumnStride;
377  this->CurrentColumn -= difference % this->NumColumns;
378  if (difference >= 0)
379  this->WrapToLeft();
380  else
381  this->WrapToRight();
382  return *this;
383  }
384 
385 
387  value_type & operator[](difference_type index) const {
388  ThisType ptrCalc(*this);
389  ptrCalc += index;
390  return *ptrCalc;
391  }
392 
393 
395  value_type & operator* () {
396  return *(this->DataPtr);
397  }
398 };
399 
400 
401 
403 template<class _elementType>
407 {
409  return result += difference;
410 }
411 
412 
414 template<class _elementType>
418 {
420  return result += difference;
421 }
422 
423 
425 template<class _elementType>
429 {
431  return result -= difference;
432 }
433 
434 
439 template<class _elementType>
443 {
445  return result += difference;
446 }
447 
448 
450 template<class _elementType>
454 {
456  return result += difference;
457 }
458 
459 
461 template<class _elementType>
465 {
467  return result -= difference;
468 }
469 
470 
471 #endif // _vctVarStrideMatrixIterator_h
472 
vctVarStrideMatrixConstIterator< _elementType > operator+(const vctVarStrideMatrixConstIterator< _elementType > &iterator, typename vctVarStrideMatrixConstIterator< _elementType >::difference_type difference)
Definition: vctVarStrideMatrixIterator.h:405
ThisType & operator--()
Definition: vctVarStrideMatrixIterator.h:158
bool operator!=(const ThisType &other) const
Definition: vctVarStrideMatrixIterator.h:274
std::iterator< std::random_access_iterator_tag, _elementType > BaseType
Definition: vctVarStrideMatrixIterator.h:51
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
ThisType operator++(int)
Definition: vctVarStrideMatrixIterator.h:324
bool operator>(const ThisType &other) const
Definition: vctVarStrideMatrixIterator.h:265
ThisType & operator++()
Definition: vctVarStrideMatrixIterator.h:314
size_t size_type
Definition: vctContainerTraits.h:35
stride_type RowStride
Definition: vctVarStrideMatrixIterator.h:69
ThisType operator--(int)
Definition: vctVarStrideMatrixIterator.h:343
ThisType operator++(int)
Definition: vctVarStrideMatrixIterator.h:150
vctVarStrideMatrixConstIterator< _elementType > BaseType
Definition: vctVarStrideMatrixIterator.h:293
value_type & operator*()
Definition: vctVarStrideMatrixIterator.h:395
vctVarStrideMatrixConstIterator< _elementType > ThisType
Definition: vctVarStrideMatrixIterator.h:48
ThisType & operator++()
Definition: vctVarStrideMatrixIterator.h:141
stride_type ColumnStride
Definition: vctVarStrideMatrixIterator.h:67
bool operator<(const ThisType &other) const
Definition: vctVarStrideMatrixIterator.h:250
const value_type & operator[](difference_type index) const
Definition: vctVarStrideMatrixIterator.h:230
ThisType & operator-=(difference_type difference)
Definition: vctVarStrideMatrixIterator.h:191
ptrdiff_t difference_type
Definition: vctContainerTraits.h:38
vctVarStrideMatrixConstIterator(value_type *dataPtr, stride_type columnStride, size_type numColumns, stride_type rowStride, size_type initialColumn=0)
Definition: vctVarStrideMatrixIterator.h:110
difference_type CurrentColumn
Definition: vctVarStrideMatrixIterator.h:70
value_type & operator[](difference_type index) const
Definition: vctVarStrideMatrixIterator.h:387
Basic traits for the cisstVector containers.
void WrapToRight()
Definition: vctVarStrideMatrixIterator.h:77
difference_type NumColumns
Definition: vctVarStrideMatrixIterator.h:68
ThisType operator--(int)
Definition: vctVarStrideMatrixIterator.h:167
bool operator>=(const ThisType &other) const
Definition: vctVarStrideMatrixIterator.h:268
vctVarStrideMatrixIterator()
Definition: vctVarStrideMatrixIterator.h:297
vctVarStrideMatrixConstIterator()
Definition: vctVarStrideMatrixIterator.h:99
ThisType & operator+=(difference_type difference)
Definition: vctVarStrideMatrixIterator.h:355
bool operator==(const ThisType &other) const
Definition: vctVarStrideMatrixIterator.h:259
void WrapToLeft()
Definition: vctVarStrideMatrixIterator.h:89
value_type * DataPtr
Definition: vctVarStrideMatrixIterator.h:64
vctVarStrideMatrixConstIterator< _elementType > operator-(const vctVarStrideMatrixConstIterator< _elementType > &iterator, typename vctVarStrideMatrixConstIterator< _elementType >::difference_type difference)
Definition: vctVarStrideMatrixIterator.h:427
ThisType & operator-=(difference_type difference)
Definition: vctVarStrideMatrixIterator.h:373
BaseType::iterator_category iterator_category
Definition: vctVarStrideMatrixIterator.h:55
vctVarStrideMatrixConstIterator(const value_type *dataPtr, difference_type columnStride, size_type numColumns, stride_type rowStride, size_type initialColumn=0)
Definition: vctVarStrideMatrixIterator.h:129
ThisType & operator--()
Definition: vctVarStrideMatrixIterator.h:333
ptrdiff_t stride_type
Definition: vctContainerTraits.h:37
const value_type & operator*() const
Definition: vctVarStrideMatrixIterator.h:238
vctVarStrideMatrixIterator(value_type *dataPtr, stride_type columnStride, size_type numColumns, stride_type rowStride, size_type initialColumn=0)
Definition: vctVarStrideMatrixIterator.h:305
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
Definition: vctVarStrideMatrixIterator.h:40
difference_type operator-(const ThisType &other) const
Definition: vctVarStrideMatrixIterator.h:219
bool operator<=(const ThisType &other) const
Definition: vctVarStrideMatrixIterator.h:253
BaseType::iterator_category iterator_category
Definition: vctVarStrideMatrixIterator.h:294
Definition: vctVarStrideMatrixIterator.h:287
vctVarStrideMatrixIterator< _elementType > ThisType
Definition: vctVarStrideMatrixIterator.h:292
ThisType & operator+=(difference_type difference)
Definition: vctVarStrideMatrixIterator.h:177