cisst-saw
Loading...
Searching...
No Matches
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-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 _vctVarStrideMatrixIterator_h
22#define _vctVarStrideMatrixIterator_h
23
28
29#include <iterator>
31
32
38template<class _elementType>
40{
41public:
42 /* define most types from vctContainerTraits */
44
47
50 typedef typename std::random_access_iterator_tag iterator_category;
51
52protected:
60
61 /* Strides between the elements of a matrix. */
62 stride_type ColumnStride;
63 difference_type NumColumns;
64 stride_type RowStride;
65 difference_type CurrentColumn;
66
72 inline void WrapToRight() {
76 }
77 }
78
79
84 inline void WrapToLeft() {
85 if (CurrentColumn < 0) {
88 }
89 }
90
91
92public:
101
102
105 vctVarStrideMatrixConstIterator(value_type * dataPtr, stride_type columnStride,
106 size_type numColumns, stride_type rowStride,
107 size_type initialColumn = 0):
108 DataPtr(dataPtr),
109 ColumnStride(columnStride),
110 NumColumns(numColumns),
111 RowStride(rowStride),
112 CurrentColumn(initialColumn)
113 {}
114
115
124 vctVarStrideMatrixConstIterator(const value_type * dataPtr, difference_type columnStride,
125 size_type numColumns, stride_type rowStride,
126 size_type initialColumn = 0):
127 DataPtr(const_cast<value_type *>(dataPtr)),
128 ColumnStride(columnStride),
129 NumColumns(numColumns),
130 RowStride(rowStride),
131 CurrentColumn(initialColumn)
132 {}
133
134
139 WrapToRight();
140 return *this;
141 }
142
143
146 ThisType tmp(*this);
147 ++(*this);
148 return tmp;
149 }
150
151
156 WrapToLeft();
157 return *this;
158 }
159
160
163 ThisType tmp(*this);
164 --(*this);
165 return tmp;
166 }
167
168
172 ThisType & operator +=(difference_type difference) {
173 DataPtr += (difference / NumColumns) * RowStride + (difference % NumColumns) * ColumnStride;
174 CurrentColumn += difference % NumColumns;
175 if (difference >= 0)
176 WrapToRight();
177 else
178 WrapToLeft();
179 return *this;
180 }
181
182
186 ThisType & operator -=(difference_type difference) {
187 DataPtr -= (difference / NumColumns) * RowStride + (difference % NumColumns) * ColumnStride;
188 CurrentColumn -= difference % NumColumns;
189 if (difference >= 0)
190 WrapToLeft();
191 else
192 WrapToRight();
193 return *this;
194 }
195
196
214 difference_type operator-(const ThisType & other) const {
215 const value_type * beginThisRow = DataPtr - CurrentColumn * ColumnStride;
216 const value_type * beginThatRow = other.DataPtr - other.CurrentColumn * ColumnStride;
217 const difference_type rowDiff = (beginThisRow - beginThatRow) / RowStride;
218 const difference_type colDiff = CurrentColumn - other.CurrentColumn;
219 const difference_type result = rowDiff * NumColumns + colDiff;
220 return result;
221 }
222
223
225 const value_type & operator[](difference_type index) const {
226 ThisType ptrCalc(*this);
227 ptrCalc += index;
228 return *ptrCalc;
229 }
230
231
233 const value_type & operator* () const {
234 return *DataPtr;
235 }
236
237
245 bool operator< (const ThisType & other) const {
246 return ((*this) - other) < 0;
247 }
248 bool operator<= (const ThisType & other) const {
249 return ((*this) < other) || ((*this) == other);;
250 }
251
252
254 bool operator== (const ThisType & other) const {
255 return (DataPtr == other.DataPtr) && (CurrentColumn == other.CurrentColumn);
256 }
257
258
260 bool operator> (const ThisType & other) const {
261 return other < (*this);
262 }
263 bool operator>= (const ThisType & other) const {
264 return ((*this) > other) || ((*this) == other);;
265 }
266
267
269 bool operator != (const ThisType & other) const {
270 return !( (*this) == other );
271 }
272
273};
274
275
276
277
281template<class _elementType>
283{
284public:
285 /* documented in base class */
290
295
300 vctVarStrideMatrixIterator(value_type * dataPtr, stride_type columnStride,
301 size_type numColumns, stride_type rowStride,
302 size_type initialColumn = 0):
303 BaseType(dataPtr, columnStride, numColumns, rowStride, initialColumn)
304 {}
305
306
310 this->DataPtr += this->ColumnStride;
311 ++(this->CurrentColumn);
312 this->WrapToRight();
313 return *this;
314 }
315
316
320 ThisType tmp(*this);
321 ++(*this);
322 return tmp;
323 }
324
325
329 this->DataPtr -= this->ColumnStride;
330 --(this->CurrentColumn);
331 this->WrapToLeft();
332 return *this;
333 }
334
335
339 ThisType tmp(*this);
340 --(*this);
341 return tmp;
342 }
343
344
350 ThisType & operator+=(difference_type difference) {
351 if (this->NumColumns == 0)
352 return *this;
353 this->DataPtr += (difference / this->NumColumns) * this->RowStride + (difference % this->NumColumns) * this->ColumnStride;
354 this->CurrentColumn += difference % this->NumColumns;
355 if (difference >= 0)
356 this->WrapToRight();
357 else
358 this->WrapToLeft();
359 return *this;
360 }
361
362
368 ThisType & operator-=(difference_type difference) {
369 if (this->NumColumns == 0)
370 return *this;
371 this->DataPtr -= (difference / this->NumColumns) * this->RowStride + (difference % this->NumColumns) * this->ColumnStride;
372 this->CurrentColumn -= difference % this->NumColumns;
373 if (difference >= 0)
374 this->WrapToLeft();
375 else
376 this->WrapToRight();
377 return *this;
378 }
379
380
382 value_type & operator[](difference_type index) const {
383 ThisType ptrCalc(*this);
384 ptrCalc += index;
385 return *ptrCalc;
386 }
387
388
391 return *(this->DataPtr);
392 }
393};
394
395
396
398template<class _elementType>
406
407
409template<class _elementType>
417
418
420template<class _elementType>
428
429
434template<class _elementType>
442
443
445template<class _elementType>
453
454
456template<class _elementType>
464
465
466#endif // _vctVarStrideMatrixIterator_h
467
Definition vctVarStrideMatrixIterator.h:40
vctVarStrideMatrixConstIterator(value_type *dataPtr, stride_type columnStride, size_type numColumns, stride_type rowStride, size_type initialColumn=0)
Definition vctVarStrideMatrixIterator.h:105
vctVarStrideMatrixConstIterator(const value_type *dataPtr, difference_type columnStride, size_type numColumns, stride_type rowStride, size_type initialColumn=0)
Definition vctVarStrideMatrixIterator.h:124
difference_type operator-(const ThisType &other) const
Definition vctVarStrideMatrixIterator.h:214
bool operator==(const ThisType &other) const
Definition vctVarStrideMatrixIterator.h:254
value_type * DataPtr
Definition vctVarStrideMatrixIterator.h:59
stride_type RowStride
Definition vctVarStrideMatrixIterator.h:64
bool operator<=(const ThisType &other) const
Definition vctVarStrideMatrixIterator.h:248
ThisType & operator++()
Definition vctVarStrideMatrixIterator.h:136
void WrapToRight()
Definition vctVarStrideMatrixIterator.h:72
ThisType & operator+=(difference_type difference)
Definition vctVarStrideMatrixIterator.h:172
void WrapToLeft()
Definition vctVarStrideMatrixIterator.h:84
ThisType & operator--()
Definition vctVarStrideMatrixIterator.h:153
bool operator<(const ThisType &other) const
Definition vctVarStrideMatrixIterator.h:245
ThisType operator++(int)
Definition vctVarStrideMatrixIterator.h:145
ThisType & operator-=(difference_type difference)
Definition vctVarStrideMatrixIterator.h:186
bool operator!=(const ThisType &other) const
Definition vctVarStrideMatrixIterator.h:269
difference_type CurrentColumn
Definition vctVarStrideMatrixIterator.h:65
ThisType operator--(int)
Definition vctVarStrideMatrixIterator.h:162
stride_type ColumnStride
Definition vctVarStrideMatrixIterator.h:62
const value_type & operator*() const
Definition vctVarStrideMatrixIterator.h:233
std::random_access_iterator_tag iterator_category
Definition vctVarStrideMatrixIterator.h:50
difference_type NumColumns
Definition vctVarStrideMatrixIterator.h:63
bool operator>=(const ThisType &other) const
Definition vctVarStrideMatrixIterator.h:263
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
bool operator>(const ThisType &other) const
Definition vctVarStrideMatrixIterator.h:260
vctVarStrideMatrixConstIterator()
Definition vctVarStrideMatrixIterator.h:94
vctVarStrideMatrixConstIterator< value_type > ThisType
Definition vctVarStrideMatrixIterator.h:46
const value_type & operator[](difference_type index) const
Definition vctVarStrideMatrixIterator.h:225
Definition vctVarStrideMatrixIterator.h:283
ThisType & operator-=(difference_type difference)
Definition vctVarStrideMatrixIterator.h:368
ThisType & operator++()
Definition vctVarStrideMatrixIterator.h:309
vctVarStrideMatrixIterator(value_type *dataPtr, stride_type columnStride, size_type numColumns, stride_type rowStride, size_type initialColumn=0)
Definition vctVarStrideMatrixIterator.h:300
BaseType::iterator_category iterator_category
Definition vctVarStrideMatrixIterator.h:289
ThisType operator++(int)
Definition vctVarStrideMatrixIterator.h:319
ThisType & operator--()
Definition vctVarStrideMatrixIterator.h:328
ThisType operator--(int)
Definition vctVarStrideMatrixIterator.h:338
ThisType & operator+=(difference_type difference)
Definition vctVarStrideMatrixIterator.h:350
vctVarStrideMatrixIterator< value_type > ThisType
Definition vctVarStrideMatrixIterator.h:287
vctVarStrideMatrixIterator()
Definition vctVarStrideMatrixIterator.h:292
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
value_type & operator[](difference_type index) const
Definition vctVarStrideMatrixIterator.h:382
vctVarStrideMatrixConstIterator< value_type > BaseType
Definition vctVarStrideMatrixIterator.h:288
value_type & operator*() const
Definition vctVarStrideMatrixIterator.h:390
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