cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-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 _vctVarStrideNArrayIterator_h
23 #define _vctVarStrideNArrayIterator_h
24 
30 #include <iterator>
34 #include <iostream>
35 
36 
45 template<class _ownerType, bool _forward>
47  public std::iterator<std::random_access_iterator_tag,
48  typename _ownerType::value_type>
49 {
50 public:
51  /* define most types from vctContainerOwnerTraits and vctNArrayTraits */
52  enum {DIMENSION = _ownerType::DIMENSION};
53  typedef typename _ownerType::value_type _elementType;
56 
58  enum {DIRECTION = _forward ? +1 : -1};
59 
62 
65  typedef _ownerType OwnerType;
66 
68  typedef std::iterator<std::random_access_iterator_tag, value_type> BaseType;
69 
72  typedef typename BaseType::iterator_category iterator_category;
73 
74 protected:
78 
82 
88  value_type * ElementPointer;
89 
90 
95  {
96  difference_type metaindex = MetaIndex;
97  difference_type offset = 0;
98  difference_type modulus;
99 
100  typename nstride_type::const_reverse_iterator stridesBegin = ContainerOwner->strides().rbegin();
101  typename nsize_type::const_reverse_iterator sizesBegin = ContainerOwner->sizes().rbegin();
102  typename nsize_type::const_reverse_iterator sizesEnd = ContainerOwner->sizes().rend();
103  size_type sizes_value;
104  for (; sizesBegin != sizesEnd;
105  ++sizesBegin, ++stridesBegin) {
106  sizes_value = (*sizesBegin == 0) ? 1 : *sizesBegin;
107  modulus = metaindex % static_cast<difference_type>(sizes_value);
108  offset += modulus * (*stridesBegin);
109  metaindex /= static_cast<difference_type>(sizes_value);
110  }
111 
112  offset += metaindex * ContainerOwner->sizes()[0] * ContainerOwner->strides()[0];
113 
114  value_type * pointer = const_cast<value_type *>( ContainerOwner->Pointer() );
115  ElementPointer = pointer + offset;
116  }
117 
118 
119 public:
122  ContainerOwner(0),
123  MetaIndex(0),
124  ElementPointer(0)
125  {}
126 
127 
132  ContainerOwner(container),
133  MetaIndex(index)
134  {
136  }
137 
138 
142  MetaIndex(other.MetaIndex),
144  {}
145 
146 
148  ThisType & operator = (const ThisType & other)
149  {
150  this->ContainerOwner = other.ContainerOwner;
151  this->MetaIndex = other.MetaIndex;
152  this->ElementPointer = other.ElementPointer;
153  return *this;
154  }
155 
156 
159  {
160  MetaIndex += DIRECTION;
162  return *this;
163  }
164 
165 
168  {
169  ThisType tmp(*this);
170  ++(*this);
171  return tmp;
172  }
173 
174 
177  {
178  MetaIndex -= DIRECTION;
180  return *this;
181  }
182 
183 
186  {
187  ThisType tmp(*this);
188  --(*this);
189  return tmp;
190  }
191 
192 
197  {
198  MetaIndex += difference * DIRECTION;
200  return *this;
201  }
202 
203 
208  {
209  MetaIndex -= difference * DIRECTION;
211  return *this;
212  }
213 
214 
219  difference_type operator - (const ThisType & other) const
220  {
221  return ( this->MetaIndex - other.MetaIndex );
222  }
223 
224 
226  const value_type & operator [] (difference_type index) const
227  {
228  difference_type metaindex = MetaIndex + index;
229  difference_type offset = 0;
230  difference_type modulus;
231 
232  typename nstride_type::const_reverse_iterator stridesBegin = ContainerOwner->strides().rbegin();
233  typename nsize_type::const_reverse_iterator sizesBegin = ContainerOwner->sizes().rbegin();
234  typename nsize_type::const_reverse_iterator sizesEnd = ContainerOwner->sizes().rend();
235  size_type sizes_value;
236  for (; sizesBegin != sizesEnd;
237  ++sizesBegin, ++stridesBegin) {
238  sizes_value = (*sizesBegin == 0) ? 1 : *sizesBegin;
239  modulus = metaindex % static_cast<difference_type>(sizes_value);
240  offset += modulus * (*stridesBegin);
241  metaindex /= static_cast<difference_type>(sizes_value);
242  }
243 
244  value_type * pointer = const_cast<value_type *>( ContainerOwner->Pointer() );
245  value_type * elementPointer = pointer + offset;
246 
247  return *elementPointer;
248  }
249 
250 
252  const value_type & operator * (void) const
253  {
254  return *ElementPointer;
255  }
256 
257 
259  bool operator == (const ThisType & other) const
260  {
261  return
262  ( this->ContainerOwner == other.ContainerOwner) &&
263  ( this->MetaIndex == other.MetaIndex ) &&
264  ( this->ElementPointer == other.ElementPointer );
265  }
266 
267 
269  bool operator != (const ThisType & other) const
270  {
271  return !( *this == other );
272  }
273 
274 
282  bool operator < (const ThisType & other) const
283  {
284  return ((*this) - other) < 0;
285  }
286  bool operator <= (const ThisType & other) const
287  {
288  return ((*this) < other) || ((*this) == other);
289  }
290 
291 
293  bool operator > (const ThisType & other) const
294  {
295  return other < (*this);
296  }
297  bool operator >= (const ThisType & other) const
298  {
299  return ((*this) > other) || ((*this) == other);
300  }
301 };
302 
303 
307 template<class _ownerType, bool _forward>
309  public vctVarStrideNArrayConstIterator<_ownerType, _forward>
310 {
311 public:
312  /* define most types from vctContainerOwnerTraits and vctNArrayTraits */
313  enum {DIMENSION = _ownerType::DIMENSION};
314  typedef typename _ownerType::value_type _elementType;
317 
318  enum {DIRECTION = _forward ? +1 : -1};
320  typedef _ownerType OwnerType;
323 
324 
327  BaseType()
328  {}
329 
330 
334  vctVarStrideNArrayIterator(const OwnerType * container, difference_type index = 0):
335  BaseType(container, index)
336  {}
337 
338 
341  BaseType(other)
342  {}
343 
344 
346  ThisType & operator = (const ThisType & other)
347  {
348  this->ContainerOwner = other.ContainerOwner;
349  this->MetaIndex = other.MetaIndex;
350  this->ElementPointer = other.ElementPointer;
351  return *this;
352  }
353 
354 
358  {
359  this->MetaIndex += DIRECTION;
360  this->UpdateElementPointer();
361  return *this;
362  }
363 
364 
368  {
369  ThisType tmp(*this);
370  ++(*this);
371  return tmp;
372  }
373 
374 
378  {
379  this->MetaIndex -= DIRECTION;
380  this->UpdateElementPointer();
381  return *this;
382  }
383 
384 
388  {
389  ThisType tmp(*this);
390  --(*this);
391  return tmp;
392  }
393 
397  {
398  this->MetaIndex += difference * DIRECTION;
399  this->UpdateElementPointer();
400  return *this;
401  }
402 
403 
407  {
408  this->MetaIndex -= difference * DIRECTION;
409  this->UpdateElementPointer();
410  return *this;
411  }
412 
413 
415  value_type & operator[](difference_type index)
416  {
417  difference_type metaindex = this->MetaIndex + index;
418  difference_type offset = 0;
419  difference_type modulus;
420 
421  typename nstride_type::const_reverse_iterator stridesBegin = this->ContainerOwner->strides().rbegin();
422  typename nsize_type::const_reverse_iterator sizesBegin = this->ContainerOwner->sizes().rbegin();
423  typename nsize_type::const_reverse_iterator sizesEnd = this->ContainerOwner->sizes().rend();
424  size_type sizes_value;
425  for (;
426  sizesBegin != sizesEnd;
427  ++sizesBegin, ++stridesBegin) {
428  sizes_value = (*sizesBegin == 0) ? 1 : *sizesBegin;
429  modulus = metaindex % static_cast<difference_type>(sizes_value);
430  offset += modulus * (*stridesBegin);
431  metaindex /= static_cast<difference_type>(sizes_value);
432  }
433 
434  value_type * pointer = const_cast<value_type *>(this->ContainerOwner->Pointer());
435  value_type * elementPointer = pointer + offset;
436 
437  return *elementPointer;
438  }
439 
440 
442  value_type & operator * (void)
443  {
444  return *(this->ElementPointer);
445  }
446 };
447 
448 
450 template<class _ownerType, bool _forward>
454 {
456  return result += difference;
457 }
458 
459 
461 template<class _ownerType, bool _forward>
465 {
467  return result += difference;
468 }
469 
470 
472 template<class _ownerType, bool _forward>
476 {
478  return result -= difference;
479 }
480 
481 
485 template<class _ownerType, bool _forward>
489 {
491  return result += difference;
492 
493 }
494 
495 
497 template<class _ownerType, bool _forward>
501 {
503  return result += difference;
504 }
505 
506 
508 template<class _ownerType, bool _forward>
512 {
514  return result -= difference;
515 }
516 
517 
518 #endif // _vctVarStrideNArrayIterator_h
519 
vctVarStrideNArrayIterator()
Definition: vctVarStrideNArrayIterator.h:326
vctVarStrideNArrayIterator(const ThisType &other)
Definition: vctVarStrideNArrayIterator.h:340
bool operator==(const ThisType &other) const
Definition: vctVarStrideNArrayIterator.h:259
const value_type & operator*(void) const
Definition: vctVarStrideNArrayIterator.h:252
_ownerType::value_type _elementType
Definition: vctVarStrideNArrayIterator.h:314
void UpdateElementPointer(void)
Definition: vctVarStrideNArrayIterator.h:94
std::iterator< std::random_access_iterator_tag, value_type > BaseType
Definition: vctVarStrideNArrayIterator.h:68
ThisType & operator+=(difference_type difference)
Definition: vctVarStrideNArrayIterator.h:196
ThisType & operator-=(difference_type difference)
Definition: vctVarStrideNArrayIterator.h:207
size_t size_type
Definition: vctContainerTraits.h:35
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
Declaration of vctDynamicNArrayOwner.
BaseType::iterator_category iterator_category
Definition: vctVarStrideNArrayIterator.h:72
vctVarStrideNArrayConstIterator(const ThisType &other)
Definition: vctVarStrideNArrayIterator.h:140
vctVarStrideNArrayConstIterator(void)
Definition: vctVarStrideNArrayIterator.h:121
vctVarStrideNArrayConstIterator< _ownerType, _forward > operator-(const vctVarStrideNArrayConstIterator< _ownerType, _forward > &iterator, typename vctVarStrideNArrayConstIterator< _ownerType, _forward >::difference_type difference)
Definition: vctVarStrideNArrayIterator.h:474
BaseType::iterator_category iterator_category
Definition: vctVarStrideNArrayIterator.h:322
vctVarStrideNArrayConstIterator< _ownerType, _forward > BaseType
Definition: vctVarStrideNArrayIterator.h:321
ThisType & operator+=(difference_type difference)
Definition: vctVarStrideNArrayIterator.h:396
ptrdiff_t difference_type
Definition: vctContainerTraits.h:38
Definition: vctForwardDeclarations.h:77
_ownerType OwnerType
Definition: vctVarStrideNArrayIterator.h:320
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
vctVarStrideNArrayIterator(const OwnerType *container, difference_type index=0)
Definition: vctVarStrideNArrayIterator.h:334
bool operator<=(const ThisType &other) const
Definition: vctVarStrideNArrayIterator.h:286
Basic traits for the cisstVector containers.
value_type & operator[](difference_type index)
Definition: vctVarStrideNArrayIterator.h:415
Declaration of vctVarStrideVectorConstIterator and vctVarStrideVectorIterator.
_ownerType::value_type _elementType
Definition: vctVarStrideNArrayIterator.h:53
bool operator!=(const ThisType &other) const
Definition: vctVarStrideNArrayIterator.h:269
ThisType & operator++(void)
Definition: vctVarStrideNArrayIterator.h:357
vctVarStrideNArrayIterator< _ownerType, _forward > ThisType
Definition: vctVarStrideNArrayIterator.h:319
Definition: vctVarStrideNArrayIterator.h:313
VCT_NARRAY_TRAITS_TYPEDEFS(DIMENSION)
ThisType & operator-=(difference_type difference)
Definition: vctVarStrideNArrayIterator.h:406
Definition: vctForwardDeclarations.h:74
ThisType & operator--(void)
Definition: vctVarStrideNArrayIterator.h:176
bool operator<(const ThisType &other) const
Definition: vctVarStrideNArrayIterator.h:282
value_type * ElementPointer
Definition: vctVarStrideNArrayIterator.h:88
ThisType & operator++(void)
Definition: vctVarStrideNArrayIterator.h:158
value_type & operator*(void)
Definition: vctVarStrideNArrayIterator.h:442
bool operator>(const ThisType &other) const
Definition: vctVarStrideNArrayIterator.h:293
vctVarStrideNArrayConstIterator(const OwnerType *container, difference_type index=0)
Definition: vctVarStrideNArrayIterator.h:131
ThisType & operator=(const ThisType &other)
Definition: vctVarStrideNArrayIterator.h:148
_ownerType OwnerType
Definition: vctVarStrideNArrayIterator.h:65
const OwnerType * ContainerOwner
Definition: vctVarStrideNArrayIterator.h:77
ThisType & operator--(void)
Definition: vctVarStrideNArrayIterator.h:377
difference_type MetaIndex
Definition: vctVarStrideNArrayIterator.h:81
Definition: vctVarStrideNArrayIterator.h:318
bool operator>=(const ThisType &other) const
Definition: vctVarStrideNArrayIterator.h:297
Definition: vctVarStrideNArrayIterator.h:58
difference_type operator-(const ThisType &other) const
Definition: vctVarStrideNArrayIterator.h:219
ThisType & operator=(const ThisType &other)
Definition: vctVarStrideNArrayIterator.h:346
vctVarStrideNArrayConstIterator< _ownerType, _forward > operator+(const vctVarStrideNArrayConstIterator< _ownerType, _forward > &iterator, typename vctVarStrideNArrayConstIterator< _ownerType, _forward >::difference_type difference)
Definition: vctVarStrideNArrayIterator.h:452
Definition: vctVarStrideNArrayIterator.h:52
vctVarStrideNArrayConstIterator< _ownerType, _forward > ThisType
Definition: vctVarStrideNArrayIterator.h:61
const value_type & operator[](difference_type index) const
Definition: vctVarStrideNArrayIterator.h:226