cisst-saw
Loading...
Searching...
No Matches
vctVarStrideNArrayIterator.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): Daniel Li, Ofri Sadowsky, Anton Deguet
7 Created on: 2006-07-11
8
9 (C) Copyright 2006-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 _vctVarStrideNArrayIterator_h
22#define _vctVarStrideNArrayIterator_h
23
28
29#include <iterator>
33#include <iostream>
34
35
44template<class _ownerType, bool _forward>
46{
47public:
48 /* define most types from vctContainerOwnerTraits and vctNArrayTraits */
49 enum {DIMENSION = _ownerType::DIMENSION};
50 typedef typename _ownerType::value_type _elementType;
53
55 enum {DIRECTION = _forward ? +1 : -1};
56
59
62 typedef _ownerType OwnerType;
63
66 typedef typename std::random_access_iterator_tag iterator_category;
67
68protected:
72
75 difference_type MetaIndex;
76
83
84
89 {
90 difference_type metaindex = MetaIndex;
91 difference_type offset = 0;
92 difference_type modulus;
93
94 typename nstride_type::const_reverse_iterator stridesBegin = ContainerOwner->strides().rbegin();
95 typename nsize_type::const_reverse_iterator sizesBegin = ContainerOwner->sizes().rbegin();
96 typename nsize_type::const_reverse_iterator sizesEnd = ContainerOwner->sizes().rend();
97 size_type sizes_value;
98 for (; sizesBegin != sizesEnd;
99 ++sizesBegin, ++stridesBegin) {
100 sizes_value = (*sizesBegin == 0) ? 1 : *sizesBegin;
101 modulus = metaindex % static_cast<difference_type>(sizes_value);
102 offset += modulus * (*stridesBegin);
103 metaindex /= static_cast<difference_type>(sizes_value);
104 }
105
106 offset += metaindex * ContainerOwner->sizes()[0] * ContainerOwner->strides()[0];
107
108 value_type * pointer = const_cast<value_type *>( ContainerOwner->Pointer() );
109 ElementPointer = pointer + offset;
110 }
111
112
113public:
120
121
125 vctVarStrideNArrayConstIterator(const OwnerType * container, difference_type index = 0):
126 ContainerOwner(container),
127 MetaIndex(index)
128 {
130 }
131
132
139
140
143 {
144 this->ContainerOwner = other.ContainerOwner;
145 this->MetaIndex = other.MetaIndex;
146 this->ElementPointer = other.ElementPointer;
147 return *this;
148 }
149
150
153 {
156 return *this;
157 }
158
159
162 {
163 ThisType tmp(*this);
164 ++(*this);
165 return tmp;
166 }
167
168
171 {
174 return *this;
175 }
176
177
180 {
181 ThisType tmp(*this);
182 --(*this);
183 return tmp;
184 }
185
186
190 ThisType & operator += (difference_type difference)
191 {
192 MetaIndex += difference * DIRECTION;
194 return *this;
195 }
196
197
201 ThisType & operator -= (difference_type difference)
202 {
203 MetaIndex -= difference * DIRECTION;
205 return *this;
206 }
207
208
213 difference_type operator - (const ThisType & other) const
214 {
215 return ( this->MetaIndex - other.MetaIndex );
216 }
217
218
220 const value_type & operator [] (difference_type index) const
221 {
222 difference_type metaindex = MetaIndex + index;
223 difference_type offset = 0;
224 difference_type modulus;
225
226 typename nstride_type::const_reverse_iterator stridesBegin = ContainerOwner->strides().rbegin();
227 typename nsize_type::const_reverse_iterator sizesBegin = ContainerOwner->sizes().rbegin();
228 typename nsize_type::const_reverse_iterator sizesEnd = ContainerOwner->sizes().rend();
229 size_type sizes_value;
230 for (; sizesBegin != sizesEnd;
231 ++sizesBegin, ++stridesBegin) {
232 sizes_value = (*sizesBegin == 0) ? 1 : *sizesBegin;
233 modulus = metaindex % static_cast<difference_type>(sizes_value);
234 offset += modulus * (*stridesBegin);
235 metaindex /= static_cast<difference_type>(sizes_value);
236 }
237
238 value_type * pointer = const_cast<value_type *>( ContainerOwner->Pointer() );
239 value_type * elementPointer = pointer + offset;
240
241 return *elementPointer;
242 }
243
244
246 const value_type & operator * (void) const
247 {
248 return *ElementPointer;
249 }
250
251
253 bool operator == (const ThisType & other) const
254 {
255 return
256 ( this->ContainerOwner == other.ContainerOwner) &&
257 ( this->MetaIndex == other.MetaIndex ) &&
258 ( this->ElementPointer == other.ElementPointer );
259 }
260
261
263 bool operator != (const ThisType & other) const
264 {
265 return !( *this == other );
266 }
267
268
276 bool operator < (const ThisType & other) const
277 {
278 return ((*this) - other) < 0;
279 }
280 bool operator <= (const ThisType & other) const
281 {
282 return ((*this) < other) || ((*this) == other);
283 }
284
285
287 bool operator > (const ThisType & other) const
288 {
289 return other < (*this);
290 }
291 bool operator >= (const ThisType & other) const
292 {
293 return ((*this) > other) || ((*this) == other);
294 }
295};
296
297
301template<class _ownerType, bool _forward>
303 public vctVarStrideNArrayConstIterator<_ownerType, _forward>
304{
305public:
306 /* define most types from vctContainerOwnerTraits and vctNArrayTraits */
307 enum {DIMENSION = _ownerType::DIMENSION};
308 typedef typename _ownerType::value_type _elementType;
311
312 enum {DIRECTION = _forward ? +1 : -1};
314 typedef _ownerType OwnerType;
317
318
323
324
328 vctVarStrideNArrayIterator(const OwnerType * container, difference_type index = 0):
329 BaseType(container, index)
330 {}
331
332
335 BaseType(other)
336 {}
337
338
341 {
342 this->ContainerOwner = other.ContainerOwner;
343 this->MetaIndex = other.MetaIndex;
344 this->ElementPointer = other.ElementPointer;
345 return *this;
346 }
347
348
352 {
353 this->MetaIndex += DIRECTION;
354 this->UpdateElementPointer();
355 return *this;
356 }
357
358
362 {
363 ThisType tmp(*this);
364 ++(*this);
365 return tmp;
366 }
367
368
372 {
373 this->MetaIndex -= DIRECTION;
374 this->UpdateElementPointer();
375 return *this;
376 }
377
378
382 {
383 ThisType tmp(*this);
384 --(*this);
385 return tmp;
386 }
387
390 ThisType & operator += (difference_type difference)
391 {
392 this->MetaIndex += difference * DIRECTION;
393 this->UpdateElementPointer();
394 return *this;
395 }
396
397
400 ThisType & operator -= (difference_type difference)
401 {
402 this->MetaIndex -= difference * DIRECTION;
403 this->UpdateElementPointer();
404 return *this;
405 }
406
407
409 value_type & operator[](difference_type index)
410 {
411 difference_type metaindex = this->MetaIndex + index;
412 difference_type offset = 0;
413 difference_type modulus;
414
415 typename nstride_type::const_reverse_iterator stridesBegin = this->ContainerOwner->strides().rbegin();
416 typename nsize_type::const_reverse_iterator sizesBegin = this->ContainerOwner->sizes().rbegin();
417 typename nsize_type::const_reverse_iterator sizesEnd = this->ContainerOwner->sizes().rend();
418 size_type sizes_value;
419 for (;
420 sizesBegin != sizesEnd;
421 ++sizesBegin, ++stridesBegin) {
422 sizes_value = (*sizesBegin == 0) ? 1 : *sizesBegin;
423 modulus = metaindex % static_cast<difference_type>(sizes_value);
424 offset += modulus * (*stridesBegin);
425 metaindex /= static_cast<difference_type>(sizes_value);
426 }
427
428 value_type * pointer = const_cast<value_type *>(this->ContainerOwner->Pointer());
429 value_type * elementPointer = pointer + offset;
430
431 return *elementPointer;
432 }
433
434
437 {
438 return *(this->ElementPointer);
439 }
440};
441
442
444template<class _ownerType, bool _forward>
452
453
455template<class _ownerType, bool _forward>
463
464
466template<class _ownerType, bool _forward>
474
475
479template<class _ownerType, bool _forward>
488
489
491template<class _ownerType, bool _forward>
499
500
502template<class _ownerType, bool _forward>
510
511
512#endif // _vctVarStrideNArrayIterator_h
513
Definition vctVarStrideNArrayIterator.h:46
bool operator>=(const ThisType &other) const
Definition vctVarStrideNArrayIterator.h:291
bool operator<(const ThisType &other) const
Definition vctVarStrideNArrayIterator.h:276
std::random_access_iterator_tag iterator_category
Definition vctVarStrideNArrayIterator.h:66
bool operator==(const ThisType &other) const
Definition vctVarStrideNArrayIterator.h:253
@ DIMENSION
Definition vctVarStrideNArrayIterator.h:49
bool operator<=(const ThisType &other) const
Definition vctVarStrideNArrayIterator.h:280
difference_type MetaIndex
Definition vctVarStrideNArrayIterator.h:75
vctVarStrideNArrayConstIterator(void)
Definition vctVarStrideNArrayIterator.h:115
const OwnerType * ContainerOwner
Definition vctVarStrideNArrayIterator.h:71
vctVarStrideNArrayConstIterator(const ThisType &other)
Definition vctVarStrideNArrayIterator.h:134
ThisType & operator+=(difference_type difference)
Definition vctVarStrideNArrayIterator.h:190
void UpdateElementPointer(void)
Definition vctVarStrideNArrayIterator.h:88
const value_type & operator[](difference_type index) const
Definition vctVarStrideNArrayIterator.h:220
value_type * ElementPointer
Definition vctVarStrideNArrayIterator.h:82
ThisType & operator++(void)
Definition vctVarStrideNArrayIterator.h:152
const value_type & operator*(void) const
Definition vctVarStrideNArrayIterator.h:246
vctVarStrideNArrayConstIterator< ThisType, _forward > ThisType
Definition vctVarStrideNArrayIterator.h:58
ThisType OwnerType
Definition vctVarStrideNArrayIterator.h:62
vctVarStrideNArrayConstIterator(const OwnerType *container, difference_type index=0)
Definition vctVarStrideNArrayIterator.h:125
ThisType & operator=(const ThisType &other)
Definition vctVarStrideNArrayIterator.h:142
ThisType::value_type _elementType
Definition vctVarStrideNArrayIterator.h:50
@ DIRECTION
Definition vctVarStrideNArrayIterator.h:55
difference_type operator-(const ThisType &other) const
Definition vctVarStrideNArrayIterator.h:213
ThisType & operator-=(difference_type difference)
Definition vctVarStrideNArrayIterator.h:201
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
bool operator>(const ThisType &other) const
Definition vctVarStrideNArrayIterator.h:287
ThisType & operator--(void)
Definition vctVarStrideNArrayIterator.h:170
bool operator!=(const ThisType &other) const
Definition vctVarStrideNArrayIterator.h:263
Definition vctVarStrideNArrayIterator.h:304
vctVarStrideNArrayIterator()
Definition vctVarStrideNArrayIterator.h:320
vctVarStrideNArrayConstIterator< ThisType, _forward > BaseType
Definition vctVarStrideNArrayIterator.h:315
ThisType & operator+=(difference_type difference)
Definition vctVarStrideNArrayIterator.h:390
vctVarStrideNArrayIterator(const OwnerType *container, difference_type index=0)
Definition vctVarStrideNArrayIterator.h:328
ThisType & operator--(void)
Definition vctVarStrideNArrayIterator.h:371
ThisType & operator-=(difference_type difference)
Definition vctVarStrideNArrayIterator.h:400
value_type & operator[](difference_type index)
Definition vctVarStrideNArrayIterator.h:409
ThisType & operator=(const ThisType &other)
Definition vctVarStrideNArrayIterator.h:340
@ DIMENSION
Definition vctVarStrideNArrayIterator.h:307
BaseType::iterator_category iterator_category
Definition vctVarStrideNArrayIterator.h:316
VCT_NARRAY_TRAITS_TYPEDEFS(DIMENSION)
ThisType OwnerType
Definition vctVarStrideNArrayIterator.h:314
ThisType::value_type _elementType
Definition vctVarStrideNArrayIterator.h:308
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
vctVarStrideNArrayIterator< ThisType, _forward > ThisType
Definition vctVarStrideNArrayIterator.h:313
vctVarStrideNArrayIterator(const ThisType &other)
Definition vctVarStrideNArrayIterator.h:334
ThisType & operator++(void)
Definition vctVarStrideNArrayIterator.h:351
@ DIRECTION
Definition vctVarStrideNArrayIterator.h:312
value_type & operator*(void) const
Definition vctVarStrideNArrayIterator.h:436
Basic traits for the cisstVector containers.
OwnerType::iterator iterator
Definition vctDynamicConstMatrixBase.h:92
Declaration of vctDynamicNArrayOwner.
vctVarStrideNArrayConstIterator< _ownerType, _forward > operator+(const vctVarStrideNArrayConstIterator< _ownerType, _forward > &iterator, typename vctVarStrideNArrayConstIterator< _ownerType, _forward >::difference_type difference)
Definition vctVarStrideNArrayIterator.h:446
vctVarStrideNArrayConstIterator< _ownerType, _forward > operator-(const vctVarStrideNArrayConstIterator< _ownerType, _forward > &iterator, typename vctVarStrideNArrayConstIterator< _ownerType, _forward >::difference_type difference)
Definition vctVarStrideNArrayIterator.h:468
Declaration of vctVarStrideVectorConstIterator and vctVarStrideVectorIterator.