cisst-saw
Loading...
Searching...
No Matches
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-2025 Johns Hopkins University (JHU), All Rights Reserved.
10
11--- begin cisst license - do not edit ---
12
13This software is provided "as is" under an open source license, with
14no warranty. The complete license can be found in license.txt and
15http://www.cisst.org/cisst/license.txt.
16
17--- end cisst license ---
18*/
19
20#pragma once
21#ifndef _vctFixedStrideMatrixIterator_h
22#define _vctFixedStrideMatrixIterator_h
23
28
29#include <iterator>
30
33
34
88template <class _elementType, vct::stride_type _columnStride, vct::size_type _numColumns, vct::stride_type _rowStride>
90{
91public:
92 /* define most types from vctContainerTraits */
94
97
100 typedef typename std::random_access_iterator_tag iterator_category;
101
102 enum {COL_STRIDE = _columnStride, ROW_STRIDE = _rowStride};
103 enum {NUM_COLUMNS = _numColumns};
104
105protected:
107 difference_type CurrentColumn;
108
110
116 inline void WrapToRight() {
117 if (CurrentColumn >= NUM_COLUMNS) {
120 }
121 }
122
123
128 inline void WrapToLeft() {
129 if (CurrentColumn < 0) {
132 }
133 }
134
135
136public:
141
142
152 index_type initialColumn = 0)
153 : DataPtr(dataPtr)
154 , CurrentColumn(initialColumn) {
156 CMN_ASSERT( initialColumn < NUM_COLUMNS );
157 }
158
159
172 index_type initialColumn = 0)
173 : DataPtr(const_cast<value_type *>(dataPtr))
174 , CurrentColumn(initialColumn) {
176 CMN_ASSERT( initialColumn < NUM_COLUMNS );
177 }
178
179
184 WrapToRight();
185 return *this;
186 }
187
188
191 ThisType tmp(*this);
192 ++(*this);
193 return tmp;
194 }
195
196
201 WrapToLeft();
202 return *this;
203 }
204
205
208 ThisType tmp(*this);
209 --(*this);
210 return tmp;
211 }
212
213
217 ThisType & operator +=(difference_type difference) {
218 DataPtr += (NUM_COLUMNS == 0) ? 0
219 : (difference / NUM_COLUMNS) * ROW_STRIDE + (difference % NUM_COLUMNS) * COL_STRIDE;
220 CurrentColumn += (NUM_COLUMNS == 0) ? 0 : difference % NUM_COLUMNS;
221 if (difference >= 0)
222 WrapToRight();
223 else
224 WrapToLeft();
225 return *this;
226 }
227
228
232 ThisType & operator -=(difference_type difference) {
233 DataPtr -= (NUM_COLUMNS == 0) ? 0
234 : (difference / NUM_COLUMNS) * ROW_STRIDE + (difference % NUM_COLUMNS) * COL_STRIDE;
235 CurrentColumn -= (NUM_COLUMNS == 0) ? 0 : difference % NUM_COLUMNS;
236 if (difference >= 0)
237 WrapToLeft();
238 else
239 WrapToRight();
240 return *this;
241 }
242
243
261 difference_type operator-(const ThisType & other) const {
262 const value_type * beginThisRow = DataPtr - CurrentColumn * COL_STRIDE;
263 const value_type * beginThatRow = other.DataPtr - other.CurrentColumn * COL_STRIDE;
264 const difference_type rowDiff = (beginThisRow - beginThatRow) / ROW_STRIDE;
265 const difference_type colDiff = CurrentColumn - other.CurrentColumn;
266 const difference_type result = rowDiff * NUM_COLUMNS + colDiff;
267 return result;
268 }
269
270
272 const value_type & operator[](difference_type index) const {
273 ThisType ptrCalc(*this);
274 ptrCalc += index;
275 return *ptrCalc;
276 }
277
278
280 const value_type & operator* () const {
281 return *DataPtr;
282 }
283
284
292 bool operator< (const ThisType & other) const {
293 return ((*this) - other) < 0;
294 }
295 bool operator<= (const ThisType & other) const {
296 return ((*this) < other) || (*this) == other;
297 }
298
299
301 bool operator== (const ThisType & other) const {
302 return (DataPtr == other.DataPtr) && (CurrentColumn == other.CurrentColumn);
303 }
304
305
307 bool operator> (const ThisType & other) const {
308 return other < (*this);
309 }
310 bool operator>= (const ThisType & other) const {
311 return ((*this) > other) || (*this) == other;
312 }
313
314
316 bool operator != (const ThisType & other) const {
317 return !( (*this) == other );
318 }
319};
320
321
322
324template <class _elementType, vct::stride_type _columnStride, vct::index_type _numColumns, vct::stride_type _rowStride>
332
334template <class _elementType, vct::stride_type _columnStride, vct::index_type _numColumns, vct::stride_type _rowStride>
342
344template <class _elementType, vct::stride_type _columnStride, vct::index_type _numColumns, vct::stride_type _rowStride>
352
353
355template <class _elementType, vct::stride_type _columnStride, vct::index_type _numColumns, vct::stride_type _rowStride>
357 public vctFixedStrideMatrixConstIterator<_elementType, _columnStride, _numColumns, _rowStride>
358{
359public:
360 /* documented in base class */
365
370
371
377 index_type initialColumn = 0)
378 : BaseType(dataPtr, initialColumn)
379 {}
380
381
385 return reinterpret_cast<ThisType &>(BaseType::operator++());
386 }
387
388
392 ThisType tmp(*this);
393 ++(*this);
394 return tmp;
395 }
396
397
401 return reinterpret_cast<ThisType &>(BaseType::operator--());
402 }
403
404
408 ThisType tmp(*this);
409 --(*this);
410 return tmp;
411 }
412
413
416 ThisType & operator+=(difference_type difference) {
417 return reinterpret_cast<ThisType &>(BaseType::operator+=(difference));
418 }
419
420
423 ThisType & operator-=(difference_type difference) {
424 return reinterpret_cast<ThisType &>(BaseType::operator-=(difference));
425 }
426
427
429 value_type & operator[](difference_type index) const {
430 ThisType ptrCalc(*this);
431 ptrCalc += index;
432 return *ptrCalc;
433 }
434
435
438 return *(this->DataPtr);
439 }
440
441};
442
443
445template <class _elementType, vct::stride_type _columnStride, vct::index_type _numColumns, vct::stride_type _rowStride>
453
455template <class _elementType, vct::stride_type _columnStride, vct::index_type _numColumns, vct::stride_type _rowStride>
463
465template <class _elementType, vct::stride_type _columnStride, vct::index_type _numColumns, vct::stride_type _rowStride>
473
474
475#endif // _vctFixedStrideMatrixIterator_h
476
Matrix iterator.
Definition vctFixedStrideMatrixIterator.h:90
const value_type & operator*() const
Definition vctFixedStrideMatrixIterator.h:280
bool operator>(const ThisType &other) const
Definition vctFixedStrideMatrixIterator.h:307
difference_type operator-(const ThisType &other) const
Definition vctFixedStrideMatrixIterator.h:261
difference_type CurrentColumn
Definition vctFixedStrideMatrixIterator.h:107
bool operator>=(const ThisType &other) const
Definition vctFixedStrideMatrixIterator.h:310
ThisType & operator-=(difference_type difference)
Definition vctFixedStrideMatrixIterator.h:232
vctFixedStrideMatrixConstIterator()
Definition vctFixedStrideMatrixIterator.h:138
vctFixedStrideMatrixConstIterator(value_type *dataPtr, index_type initialColumn=0)
Definition vctFixedStrideMatrixIterator.h:151
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
ThisType & operator+=(difference_type difference)
Definition vctFixedStrideMatrixIterator.h:217
bool operator<(const ThisType &other) const
Definition vctFixedStrideMatrixIterator.h:292
ThisType operator++(int)
Definition vctFixedStrideMatrixIterator.h:190
bool operator==(const ThisType &other) const
Definition vctFixedStrideMatrixIterator.h:301
ThisType & operator++()
Definition vctFixedStrideMatrixIterator.h:181
vctFixedStrideMatrixConstIterator< _elementType, _columnStride, _numColumns, _rowStride > ThisType
Definition vctFixedStrideMatrixIterator.h:96
std::random_access_iterator_tag iterator_category
Definition vctFixedStrideMatrixIterator.h:100
bool operator<=(const ThisType &other) const
Definition vctFixedStrideMatrixIterator.h:295
ThisType operator--(int)
Definition vctFixedStrideMatrixIterator.h:207
bool operator!=(const ThisType &other) const
Definition vctFixedStrideMatrixIterator.h:316
ThisType & operator--()
Definition vctFixedStrideMatrixIterator.h:198
const value_type & operator[](difference_type index) const
Definition vctFixedStrideMatrixIterator.h:272
void WrapToLeft()
Definition vctFixedStrideMatrixIterator.h:128
vctFixedStrideMatrixConstIterator(const value_type *dataPtr, index_type initialColumn=0)
Definition vctFixedStrideMatrixIterator.h:171
void WrapToRight()
Definition vctFixedStrideMatrixIterator.h:116
value_type * DataPtr
Definition vctFixedStrideMatrixIterator.h:106
Definition vctFixedStrideMatrixIterator.h:358
ThisType & operator-=(difference_type difference)
Definition vctFixedStrideMatrixIterator.h:423
ThisType & operator+=(difference_type difference)
Definition vctFixedStrideMatrixIterator.h:416
vctFixedStrideMatrixIterator()
Definition vctFixedStrideMatrixIterator.h:367
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
ThisType & operator--()
Definition vctFixedStrideMatrixIterator.h:400
ThisType operator--(int)
Definition vctFixedStrideMatrixIterator.h:407
ThisType operator++(int)
Definition vctFixedStrideMatrixIterator.h:391
value_type & operator*() const
Definition vctFixedStrideMatrixIterator.h:437
vctFixedStrideMatrixConstIterator< _elementType, _columnStride, _numColumns, _rowStride > BaseType
Definition vctFixedStrideMatrixIterator.h:363
vctFixedStrideMatrixIterator< _elementType, _columnStride, _numColumns, _rowStride > ThisType
Definition vctFixedStrideMatrixIterator.h:362
value_type & operator[](difference_type index) const
Definition vctFixedStrideMatrixIterator.h:429
BaseType::iterator_category iterator_category
Definition vctFixedStrideMatrixIterator.h:364
ThisType & operator++()
Definition vctFixedStrideMatrixIterator.h:384
vctFixedStrideMatrixIterator(value_type *dataPtr, index_type initialColumn=0)
Definition vctFixedStrideMatrixIterator.h:376
Assert macros definitions.
#define CMN_ASSERT(expr)
Definition cmnAssert.h:97
Basic traits for the cisstVector containers.
OwnerType::iterator iterator
Definition vctDynamicConstMatrixBase.h:92
vctReturnDynamicMatrix< _elementType > operator-(const vctDynamicConstMatrixBase< _matrixOwnerType1, _elementType > &inputMatrix1, const vctDynamicConstMatrixBase< _matrixOwnerType2, _elementType > &inputMatrix2)
Definition vctDynamicMatrix.h:487
vctReturnDynamicMatrix< _elementType > operator+(const vctDynamicConstMatrixBase< _matrixOwnerType1, _elementType > &inputMatrix1, const vctDynamicConstMatrixBase< _matrixOwnerType2, _elementType > &inputMatrix2)
Definition vctDynamicMatrix.h:476