cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vctFixedStrideMatrixIterator.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 _vctFixedStrideMatrixIterator_h
23 #define _vctFixedStrideMatrixIterator_h
24 
30 #include <iterator>
31 
32 #include <cisstCommon/cmnAssert.h>
34 
35 
89 template <class _elementType, vct::stride_type _columnStride, vct::size_type _numColumns, vct::stride_type _rowStride>
91  public std::iterator<std::random_access_iterator_tag, _elementType>
92 {
93 public:
94  /* define most types from vctContainerTraits */
95  VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType);
96 
99 
101  typedef std::iterator<std::random_access_iterator_tag, _elementType> BaseType;
102 
105  typedef typename BaseType::iterator_category iterator_category;
106 
107  enum {COL_STRIDE = _columnStride, ROW_STRIDE = _rowStride};
108  enum {NUM_COLUMNS = _numColumns};
109 
110 protected:
111  value_type * DataPtr;
113 
115 
121  inline void WrapToRight() {
122  if (CurrentColumn >= NUM_COLUMNS) {
125  }
126  }
127 
128 
133  inline void WrapToLeft() {
134  if (CurrentColumn < 0) {
137  }
138  }
139 
140 
141 public:
145  }
146 
147 
156  explicit vctFixedStrideMatrixConstIterator(value_type * dataPtr,
157  index_type initialColumn = 0)
158  : DataPtr(dataPtr)
159  , CurrentColumn(initialColumn) {
161  CMN_ASSERT( initialColumn < NUM_COLUMNS );
162  }
163 
164 
176  explicit vctFixedStrideMatrixConstIterator(const value_type * dataPtr,
177  index_type initialColumn = 0)
178  : DataPtr(const_cast<value_type *>(dataPtr))
179  , CurrentColumn(initialColumn) {
181  CMN_ASSERT( initialColumn < NUM_COLUMNS );
182  }
183 
184 
187  DataPtr += COL_STRIDE;
188  ++CurrentColumn;
189  WrapToRight();
190  return *this;
191  }
192 
193 
196  ThisType tmp(*this);
197  ++(*this);
198  return tmp;
199  }
200 
201 
204  DataPtr -= COL_STRIDE;
205  --CurrentColumn;
206  WrapToLeft();
207  return *this;
208  }
209 
210 
213  ThisType tmp(*this);
214  --(*this);
215  return tmp;
216  }
217 
218 
223  DataPtr += (NUM_COLUMNS == 0) ? 0
224  : (difference / NUM_COLUMNS) * ROW_STRIDE + (difference % NUM_COLUMNS) * COL_STRIDE;
225  CurrentColumn += (NUM_COLUMNS == 0) ? 0 : difference % NUM_COLUMNS;
226  if (difference >= 0)
227  WrapToRight();
228  else
229  WrapToLeft();
230  return *this;
231  }
232 
233 
238  DataPtr -= (NUM_COLUMNS == 0) ? 0
239  : (difference / NUM_COLUMNS) * ROW_STRIDE + (difference % NUM_COLUMNS) * COL_STRIDE;
240  CurrentColumn -= (NUM_COLUMNS == 0) ? 0 : difference % NUM_COLUMNS;
241  if (difference >= 0)
242  WrapToLeft();
243  else
244  WrapToRight();
245  return *this;
246  }
247 
248 
266  difference_type operator-(const ThisType & other) const {
267  const value_type * beginThisRow = DataPtr - CurrentColumn * COL_STRIDE;
268  const value_type * beginThatRow = other.DataPtr - other.CurrentColumn * COL_STRIDE;
269  const difference_type rowDiff = (beginThisRow - beginThatRow) / ROW_STRIDE;
270  const difference_type colDiff = CurrentColumn - other.CurrentColumn;
271  const difference_type result = rowDiff * NUM_COLUMNS + colDiff;
272  return result;
273  }
274 
275 
277  const value_type & operator[](difference_type index) const {
278  ThisType ptrCalc(*this);
279  ptrCalc += index;
280  return *ptrCalc;
281  }
282 
283 
285  const value_type & operator* () const {
286  return *DataPtr;
287  }
288 
289 
297  bool operator< (const ThisType & other) const {
298  return ((*this) - other) < 0;
299  }
300  bool operator<= (const ThisType & other) const {
301  return ((*this) < other) || (*this) == other;
302  }
303 
304 
306  bool operator== (const ThisType & other) const {
307  return (DataPtr == other.DataPtr) && (CurrentColumn == other.CurrentColumn);
308  }
309 
310 
312  bool operator> (const ThisType & other) const {
313  return other < (*this);
314  }
315  bool operator>= (const ThisType & other) const {
316  return ((*this) > other) || (*this) == other;
317  }
318 
319 
321  bool operator != (const ThisType & other) const {
322  return !( (*this) == other );
323  }
324 };
325 
326 
327 
329 template <class _elementType, vct::stride_type _columnStride, vct::index_type _numColumns, vct::stride_type _rowStride>
333 {
335  return result += difference;
336 }
337 
339 template <class _elementType, vct::stride_type _columnStride, vct::index_type _numColumns, vct::stride_type _rowStride>
343 {
345  return result += difference;
346 }
347 
349 template <class _elementType, vct::stride_type _columnStride, vct::index_type _numColumns, vct::stride_type _rowStride>
353 {
355  return result -= difference;
356 }
357 
358 
360 template <class _elementType, vct::stride_type _columnStride, vct::index_type _numColumns, vct::stride_type _rowStride>
362  public vctFixedStrideMatrixConstIterator<_elementType, _columnStride, _numColumns, _rowStride>
363 {
364 public:
365  /* documented in base class */
366  VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType);
369  typedef typename BaseType::iterator_category iterator_category;
370 
373  : BaseType()
374  {}
375 
376 
381  explicit vctFixedStrideMatrixIterator(value_type * dataPtr,
382  index_type initialColumn = 0)
383  : BaseType(dataPtr, initialColumn)
384  {}
385 
386 
390  return reinterpret_cast<ThisType &>(BaseType::operator++());
391  }
392 
393 
397  ThisType tmp(*this);
398  ++(*this);
399  return tmp;
400  }
401 
402 
406  return reinterpret_cast<ThisType &>(BaseType::operator--());
407  }
408 
409 
413  ThisType tmp(*this);
414  --(*this);
415  return tmp;
416  }
417 
418 
422  return reinterpret_cast<ThisType &>(BaseType::operator+=(difference));
423  }
424 
425 
429  return reinterpret_cast<ThisType &>(BaseType::operator-=(difference));
430  }
431 
432 
434  value_type & operator[](difference_type index) const {
435  ThisType ptrCalc(*this);
436  ptrCalc += index;
437  return *ptrCalc;
438  }
439 
440 
442  value_type & operator* () {
443  return *(this->DataPtr);
444  }
445 
446 };
447 
448 
450 template <class _elementType, vct::stride_type _columnStride, vct::index_type _numColumns, vct::stride_type _rowStride>
454 {
456  return result += difference;
457 }
458 
460 template <class _elementType, vct::stride_type _columnStride, vct::index_type _numColumns, vct::stride_type _rowStride>
464 {
466  return result += difference;
467 }
468 
470 template <class _elementType, vct::stride_type _columnStride, vct::index_type _numColumns, vct::stride_type _rowStride>
474 {
476  return result -= difference;
477 }
478 
479 
480 #endif // _vctFixedStrideMatrixIterator_h
481 
difference_type operator-(const ThisType &other) const
Definition: vctFixedStrideMatrixIterator.h:266
size_t index_type
Definition: vctContainerTraits.h:36
Definition: vctFixedStrideMatrixIterator.h:114
Assert macros definitions.
vctFixedStrideMatrixIterator(value_type *dataPtr, index_type initialColumn=0)
Definition: vctFixedStrideMatrixIterator.h:381
#define CMN_ASSERT(expr)
Definition: cmnAssert.h:90
vctFixedStrideMatrixConstIterator< _elementType, _columnStride, _numColumns, _rowStride > operator+(const vctFixedStrideMatrixConstIterator< _elementType, _columnStride, _numColumns, _rowStride > &iterator, typename vctFixedStrideMatrixConstIterator< _elementType, _columnStride, _numColumns, _rowStride >::difference_type difference)
Definition: vctFixedStrideMatrixIterator.h:331
bool operator>(const ThisType &other) const
Definition: vctFixedStrideMatrixIterator.h:312
void WrapToLeft()
Definition: vctFixedStrideMatrixIterator.h:133
ThisType & operator+=(difference_type difference)
Definition: vctFixedStrideMatrixIterator.h:222
vctFixedStrideMatrixConstIterator()
Definition: vctFixedStrideMatrixIterator.h:143
value_type * DataPtr
Definition: vctFixedStrideMatrixIterator.h:111
BaseType::iterator_category iterator_category
Definition: vctFixedStrideMatrixIterator.h:105
const value_type & operator*() const
Definition: vctFixedStrideMatrixIterator.h:285
bool operator<=(const ThisType &other) const
Definition: vctFixedStrideMatrixIterator.h:300
ThisType operator--(int)
Definition: vctFixedStrideMatrixIterator.h:212
BaseType::iterator_category iterator_category
Definition: vctFixedStrideMatrixIterator.h:369
ThisType & operator--()
Definition: vctFixedStrideMatrixIterator.h:405
std::iterator< std::random_access_iterator_tag, _elementType > BaseType
Definition: vctFixedStrideMatrixIterator.h:101
Definition: vctFixedStrideMatrixIterator.h:361
Definition: vctFixedStrideMatrixIterator.h:107
value_type & operator[](difference_type index) const
Definition: vctFixedStrideMatrixIterator.h:434
bool operator>=(const ThisType &other) const
Definition: vctFixedStrideMatrixIterator.h:315
const value_type & operator[](difference_type index) const
Definition: vctFixedStrideMatrixIterator.h:277
Definition: vctFixedStrideMatrixIterator.h:108
Matrix iterator.
Definition: vctFixedStrideMatrixIterator.h:90
bool operator!=(const ThisType &other) const
Definition: vctFixedStrideMatrixIterator.h:321
ThisType & operator-=(difference_type difference)
Definition: vctFixedStrideMatrixIterator.h:237
ThisType & operator--()
Definition: vctFixedStrideMatrixIterator.h:203
ThisType & operator-=(difference_type difference)
Definition: vctFixedStrideMatrixIterator.h:428
ThisType & operator++()
Definition: vctFixedStrideMatrixIterator.h:389
Definition: vctFixedStrideMatrixIterator.h:107
ptrdiff_t difference_type
Definition: vctContainerTraits.h:38
ThisType operator++(int)
Definition: vctFixedStrideMatrixIterator.h:195
Basic traits for the cisstVector containers.
vctFixedStrideMatrixConstIterator(const value_type *dataPtr, index_type initialColumn=0)
Definition: vctFixedStrideMatrixIterator.h:176
ThisType operator--(int)
Definition: vctFixedStrideMatrixIterator.h:412
difference_type CurrentColumn
Definition: vctFixedStrideMatrixIterator.h:112
vctFixedStrideMatrixIterator< _elementType, _columnStride, _numColumns, _rowStride > ThisType
Definition: vctFixedStrideMatrixIterator.h:367
osaGroup::ID operator++(osaGroup::ID &gid, int i)
vctFixedStrideMatrixIterator()
Definition: vctFixedStrideMatrixIterator.h:372
vctFixedStrideMatrixConstIterator< _elementType, _columnStride, _numColumns, _rowStride > BaseType
Definition: vctFixedStrideMatrixIterator.h:368
void WrapToRight()
Definition: vctFixedStrideMatrixIterator.h:121
bool operator<(const ThisType &other) const
Definition: vctFixedStrideMatrixIterator.h:297
ThisType operator++(int)
Definition: vctFixedStrideMatrixIterator.h:396
vctFixedStrideMatrixConstIterator(value_type *dataPtr, index_type initialColumn=0)
Definition: vctFixedStrideMatrixIterator.h:156
vctFixedStrideMatrixConstIterator< _elementType, _columnStride, _numColumns, _rowStride > ThisType
Definition: vctFixedStrideMatrixIterator.h:98
bool operator==(const ThisType &other) const
Definition: vctFixedStrideMatrixIterator.h:306
ThisType & operator++()
Definition: vctFixedStrideMatrixIterator.h:186
ThisType & operator+=(difference_type difference)
Definition: vctFixedStrideMatrixIterator.h:421
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)