DGtal  1.2.0
Public Types | Public Member Functions | Protected Attributes | Private Member Functions
DGtal::Circulator< TIterator > Class Template Reference

Aim: Provides an adapter for classical iterators that can iterate through the underlying data structure as in a loop. The increment (resp. decrement) operator encapsulates the validity test and the assignement to the begin (resp. end) iterator of a given range, when the end (resp. beginning) has been reached. For instance, the pre-increment operator does:
More...

#include <DGtal/base/Circulator.h>

Public Types

typedef TIterator Iterator
 
typedef Circulator< TIterator > Self
 
typedef CirculatorType Type
 
typedef boost::iterator_category< TIterator >::type iterator_category
 
typedef std::iterator_traits< TIterator >::value_type value_type
 
typedef std::iterator_traits< TIterator >::difference_type difference_type
 
typedef std::iterator_traits< TIterator >::pointer pointer
 
typedef std::iterator_traits< TIterator >::reference reference
 

Public Member Functions

 Circulator ()
 
 Circulator (const Iterator &i, const Iterator &itb, const Iterator &ite)
 
 ~Circulator ()
 
 Circulator (const Circulator &other)
 
template<typename other_Iterator >
 Circulator (const Circulator< other_Iterator > &other)
 
Circulatoroperator= (const Circulator &other)
 
template<typename other_Iterator >
Circulatoroperator= (const Circulator< other_Iterator > &other)
 
bool isValid () const
 
Iterator base () const
 
Iterator begin () const
 
Iterator end () const
 
reference operator* () const
 
pointer operator-> () const
 
Selfoperator++ ()
 
Self operator++ (int)
 
Selfoperator-- ()
 
Self operator-- (int)
 
bool operator== (const Self &other) const
 
bool operator!= (const Self &other) const
 
template<typename OtherIterator >
bool operator== (const OtherIterator &other) const
 
template<typename OtherIterator >
bool operator!= (const OtherIterator &other) const
 
Selfoperator+= (difference_type d)
 
Selfoperator-= (difference_type d)
 
Self operator+ (difference_type d) const
 
Self operator- (difference_type d) const
 
difference_type operator- (const Self &c) const
 
reference operator[] (difference_type d) const
 
bool operator< (const Self &) const
 
bool operator<= (const Self &) const
 
bool operator> (const Self &c) const
 
bool operator>= (const Self &c) const
 

Protected Attributes

Iterator myCurrentIt
 
Iterator myBeginIt
 
Iterator myEndIt
 
bool myFlagIsValid
 

Private Member Functions

 BOOST_STATIC_ASSERT ((boost::is_same< typename IteratorCirculatorTraits< TIterator >::Type, IteratorType >::value))
 

Detailed Description

template<typename TIterator>
class DGtal::Circulator< TIterator >

Aim: Provides an adapter for classical iterators that can iterate through the underlying data structure as in a loop. The increment (resp. decrement) operator encapsulates the validity test and the assignement to the begin (resp. end) iterator of a given range, when the end (resp. beginning) has been reached. For instance, the pre-increment operator does:

Description of template class 'Circulator'

return *this;
Iterator myBeginIt
Definition: Circulator.h:431
Iterator myEndIt
Definition: Circulator.h:432
Iterator myCurrentIt
Definition: Circulator.h:430

whereas the pre-decrement operator does (if at least bidirectional):

Be aware that random access operators are quite different for circular iterators and classical iterators. Firstly, the difference between two circular iterators c and d is always positive and such that (c-d) + (d-c) is equal to the size of the underlying range. Secondly, comparison operators 'less than' and 'less than or equal to' always return 'true', whereas 'greater than' and 'greater than or equal to' always return 'false'.

Template Parameters
TIteratorany forward, bidirectional or random access iterator

Definition at line 85 of file Circulator.h.

Member Typedef Documentation

◆ difference_type

template<typename TIterator >
typedef std::iterator_traits<TIterator>::difference_type DGtal::Circulator< TIterator >::difference_type

Definition at line 104 of file Circulator.h.

◆ Iterator

template<typename TIterator >
typedef TIterator DGtal::Circulator< TIterator >::Iterator

Definition at line 95 of file Circulator.h.

◆ iterator_category

template<typename TIterator >
typedef boost::iterator_category<TIterator>::type DGtal::Circulator< TIterator >::iterator_category

Definition at line 101 of file Circulator.h.

◆ pointer

template<typename TIterator >
typedef std::iterator_traits<TIterator>::pointer DGtal::Circulator< TIterator >::pointer

Definition at line 105 of file Circulator.h.

◆ reference

template<typename TIterator >
typedef std::iterator_traits<TIterator>::reference DGtal::Circulator< TIterator >::reference

Definition at line 106 of file Circulator.h.

◆ Self

template<typename TIterator >
typedef Circulator<TIterator> DGtal::Circulator< TIterator >::Self

Definition at line 96 of file Circulator.h.

◆ Type

template<typename TIterator >
typedef CirculatorType DGtal::Circulator< TIterator >::Type

Definition at line 98 of file Circulator.h.

◆ value_type

template<typename TIterator >
typedef std::iterator_traits<TIterator>::value_type DGtal::Circulator< TIterator >::value_type

Definition at line 103 of file Circulator.h.

Constructor & Destructor Documentation

◆ Circulator() [1/4]

template<typename TIterator >
DGtal::Circulator< TIterator >::Circulator ( )
inline

Default constructor. Default-initializes iterator members NB: not valid

Definition at line 118 of file Circulator.h.

118 : myCurrentIt(), myBeginIt(), myEndIt(), myFlagIsValid(false) {}

◆ Circulator() [2/4]

template<typename TIterator >
DGtal::Circulator< TIterator >::Circulator ( const Iterator i,
const Iterator itb,
const Iterator ite 
)
inline

Constructor. This object can iterate through the underlying data structure as in a loop, because it knows the begin and end iterator of the range.

Parameters
iany iterator
itbbegin iterator
iteend iterator

Definition at line 129 of file Circulator.h.

132  : myCurrentIt(i), myBeginIt(itb), myEndIt(ite), myFlagIsValid(true)
133  { if (myBeginIt == myEndIt) myFlagIsValid = false; }

References DGtal::Circulator< TIterator >::myBeginIt, DGtal::Circulator< TIterator >::myEndIt, and DGtal::Circulator< TIterator >::myFlagIsValid.

◆ ~Circulator()

template<typename TIterator >
DGtal::Circulator< TIterator >::~Circulator ( )
inline

Destructor.

Definition at line 138 of file Circulator.h.

138 {}

◆ Circulator() [3/4]

template<typename TIterator >
DGtal::Circulator< TIterator >::Circulator ( const Circulator< TIterator > &  other)
inline

Copy constructor.

Parameters
otherthe object to clone.

Definition at line 144 of file Circulator.h.

145  : myCurrentIt(other.myCurrentIt),
146  myBeginIt(other.myBeginIt), myEndIt(other.myEndIt),
147  myFlagIsValid(other.myFlagIsValid)
148  {}

◆ Circulator() [4/4]

template<typename TIterator >
template<typename other_Iterator >
DGtal::Circulator< TIterator >::Circulator ( const Circulator< other_Iterator > &  other)
inline

Copy of circulators that adapts other iterator types (not const / const).

Parameters
otherthe object to clone.

Definition at line 155 of file Circulator.h.

156  : myCurrentIt(other.base()),
157  myBeginIt(other.begin()), myEndIt(other.end()),
158  myFlagIsValid(other.isValid())
159  {}

Member Function Documentation

◆ base()

template<typename TIterator >
Iterator DGtal::Circulator< TIterator >::base ( ) const
inline
Returns
member [myCurrentIt], the underlying iterator.

Definition at line 216 of file Circulator.h.

217  { return myCurrentIt; }

References DGtal::Circulator< TIterator >::myCurrentIt.

Referenced by basicForwardTest(), main(), DGtal::Circulator< TIterator >::operator=(), and DGtal::Circulator< TIterator >::operator==().

◆ begin()

template<typename TIterator >
Iterator DGtal::Circulator< TIterator >::begin ( ) const
inline
Returns
member [myBeginIt], begin iterator of the underlying range.

Definition at line 222 of file Circulator.h.

223  { return myBeginIt; }

References DGtal::Circulator< TIterator >::myBeginIt.

Referenced by basicForwardTest(), DGtal::Circulator< TIterator >::operator=(), and DGtal::Circulator< TIterator >::operator==().

◆ BOOST_STATIC_ASSERT()

template<typename TIterator >
DGtal::Circulator< TIterator >::BOOST_STATIC_ASSERT ( (boost::is_same< typename IteratorCirculatorTraits< TIterator >::Type, IteratorType >::value)  )
private

◆ end()

template<typename TIterator >
Iterator DGtal::Circulator< TIterator >::end ( ) const
inline
Returns
member [myEndIt], end iterator of the underlying range.

Definition at line 228 of file Circulator.h.

229  { return myEndIt; }

References DGtal::Circulator< TIterator >::myEndIt.

Referenced by basicForwardTest(), DGtal::Circulator< TIterator >::operator=(), and DGtal::Circulator< TIterator >::operator==().

◆ isValid()

template<typename TIterator >
bool DGtal::Circulator< TIterator >::isValid ( ) const
inline

◆ operator!=() [1/2]

template<typename TIterator >
template<typename OtherIterator >
bool DGtal::Circulator< TIterator >::operator!= ( const OtherIterator &  other) const
inline

Definition at line 334 of file Circulator.h.

334 { return !(*this == other); }

◆ operator!=() [2/2]

template<typename TIterator >
bool DGtal::Circulator< TIterator >::operator!= ( const Self other) const
inline

Definition at line 317 of file Circulator.h.

317 { return !(*this == other); }

◆ operator*()

template<typename TIterator >
reference DGtal::Circulator< TIterator >::operator* ( ) const
inline
Returns
*myCurrentIt.

Definition at line 234 of file Circulator.h.

235  {
236  //ASSERT( myCurrentIt != myEndIt ); //myCurrentIt == myEndIt when using reverse iterators on circulators
237  ASSERT( isValid() );
238  return *myCurrentIt;
239  }
bool isValid() const
Definition: Circulator.h:206

References DGtal::Circulator< TIterator >::isValid(), and DGtal::Circulator< TIterator >::myCurrentIt.

◆ operator+()

template<typename TIterator >
Self DGtal::Circulator< TIterator >::operator+ ( difference_type  d) const
inline

Definition at line 377 of file Circulator.h.

378  {
379  Self tmp = *this;
380  return tmp += d;
381  }
Circulator< TIterator > Self
Definition: Circulator.h:96

◆ operator++() [1/2]

template<typename TIterator >
Self& DGtal::Circulator< TIterator >::operator++ ( )
inline

◆ operator++() [2/2]

template<typename TIterator >
Self DGtal::Circulator< TIterator >::operator++ ( int  )
inline

Post-increment

Definition at line 269 of file Circulator.h.

270  {
271  Self tmp = *this;
272  operator++();
273  return tmp;
274  }
Self & operator++()
Definition: Circulator.h:258

References DGtal::Circulator< TIterator >::operator++().

◆ operator+=()

template<typename TIterator >
Self& DGtal::Circulator< TIterator >::operator+= ( difference_type  d)
inline

Definition at line 340 of file Circulator.h.

341  {
342  if ( isValid() )
343  {
344  //size range
345  typename Iterator::difference_type n = myEndIt - myBeginIt;
346  ASSERT( n > 0 );
347  //difference modulo n
348  if ( (d >= n)||(-d >= n) )
349  d = d%n;
350  ASSERT( (d < n)&&(-d < n) );
351  //position of the current iterator
352  typename Iterator::difference_type j = myCurrentIt - myBeginIt;
353  ASSERT( (j >= 0) && (j < n) );
354  //deviation between the position of the past-the-end value
355  //and the current iterator
356  typename Iterator::difference_type e = n - j;
357  if (d >= 0)
358  { //in case of positive distance
359  if (d < e) j += d;
360  else j = d - e;
361  }
362  else
363  { //in case of negative distance
364  if (-d <= j) j += d;
365  else j = j + d + n;
366  }
367  ASSERT( (j >= 0) && (j < n) );
368  myCurrentIt = myBeginIt + j;
369  return *this;
370  }
371  else
372  return *this;
373  }

References DGtal::Circulator< TIterator >::isValid(), DGtal::Circulator< TIterator >::myBeginIt, DGtal::Circulator< TIterator >::myCurrentIt, and DGtal::Circulator< TIterator >::myEndIt.

Referenced by DGtal::Circulator< TIterator >::operator-=().

◆ operator-() [1/2]

template<typename TIterator >
difference_type DGtal::Circulator< TIterator >::operator- ( const Self c) const
inline

Definition at line 389 of file Circulator.h.

390  {
391  typename Iterator::difference_type d = (myCurrentIt - c.myCurrentIt);
392  if (d >= 0)
393  return d;
394  else
395  {
396  typename Iterator::difference_type n = myEndIt - myBeginIt;
397  ASSERT( n > 0 );
398  return (n + d);
399  }
400  }

References DGtal::Circulator< TIterator >::myBeginIt, DGtal::Circulator< TIterator >::myCurrentIt, and DGtal::Circulator< TIterator >::myEndIt.

◆ operator-() [2/2]

template<typename TIterator >
Self DGtal::Circulator< TIterator >::operator- ( difference_type  d) const
inline

Definition at line 383 of file Circulator.h.

384  {
385  Self tmp = *this;
386  return tmp += -d;
387  }

◆ operator--() [1/2]

template<typename TIterator >
Self& DGtal::Circulator< TIterator >::operator-- ( )
inline

◆ operator--() [2/2]

template<typename TIterator >
Self DGtal::Circulator< TIterator >::operator-- ( int  )
inline

Post-decrement

Definition at line 291 of file Circulator.h.

292  {
293  Self tmp = *this;
294  operator--();
295  return tmp;
296  }
Self & operator--()
Definition: Circulator.h:280

References DGtal::Circulator< TIterator >::operator--().

◆ operator-=()

template<typename TIterator >
Self& DGtal::Circulator< TIterator >::operator-= ( difference_type  d)
inline

Definition at line 375 of file Circulator.h.

375 { return operator+=( -d); }
Self & operator+=(difference_type d)
Definition: Circulator.h:340

References DGtal::Circulator< TIterator >::operator+=().

◆ operator->()

template<typename TIterator >
pointer DGtal::Circulator< TIterator >::operator-> ( ) const
inline
Returns
pointer to myCurrentIt

Definition at line 244 of file Circulator.h.

245  {
246  //ASSERT( myCurrentIt != myEndIt ); //myCurrentIt == myEndIt when using reverse iterators on circulators
247  ASSERT( isValid() );
248  return myCurrentIt.operator->();
249  }

References DGtal::Circulator< TIterator >::isValid(), and DGtal::Circulator< TIterator >::myCurrentIt.

◆ operator<()

template<typename TIterator >
bool DGtal::Circulator< TIterator >::operator< ( const Self ) const
inline

Definition at line 409 of file Circulator.h.

410  {
411  return true;
412  }

◆ operator<=()

template<typename TIterator >
bool DGtal::Circulator< TIterator >::operator<= ( const Self ) const
inline

Definition at line 413 of file Circulator.h.

414  {
415  return true;
416  }

Referenced by DGtal::Circulator< TIterator >::operator>().

◆ operator=() [1/2]

template<typename TIterator >
Circulator& DGtal::Circulator< TIterator >::operator= ( const Circulator< TIterator > &  other)
inline

Assignment.

Parameters
otherthe object to copy.
Returns
a reference on 'this'.

Definition at line 166 of file Circulator.h.

167  {
168  if ( this != &other )
169  {
170  myCurrentIt = other.myCurrentIt;
171  myBeginIt = other.myBeginIt;
172  myEndIt = other.myEndIt;
173  if (myBeginIt != myEndIt)
174  myFlagIsValid = true;
175  else
176  myFlagIsValid = false;
177  }
178  return *this;
179  }

References DGtal::Circulator< TIterator >::myBeginIt, DGtal::Circulator< TIterator >::myCurrentIt, DGtal::Circulator< TIterator >::myEndIt, and DGtal::Circulator< TIterator >::myFlagIsValid.

◆ operator=() [2/2]

template<typename TIterator >
template<typename other_Iterator >
Circulator& DGtal::Circulator< TIterator >::operator= ( const Circulator< other_Iterator > &  other)
inline

Assignment that adapts other iterator types (not const / const).

Parameters
otherthe object to copy.
Returns
a reference on 'this'.

Definition at line 187 of file Circulator.h.

188  {
189  if ( this != &other )
190  {
191  myCurrentIt = other.base();
192  myBeginIt = other.begin();
193  myEndIt = other.end();
194  if (myBeginIt != myEndIt)
195  myFlagIsValid = true;
196  else
197  myFlagIsValid = false;
198  }
199  return *this;
200  }

References DGtal::Circulator< TIterator >::base(), DGtal::Circulator< TIterator >::begin(), DGtal::Circulator< TIterator >::end(), DGtal::Circulator< TIterator >::myBeginIt, DGtal::Circulator< TIterator >::myCurrentIt, DGtal::Circulator< TIterator >::myEndIt, and DGtal::Circulator< TIterator >::myFlagIsValid.

◆ operator==() [1/2]

template<typename TIterator >
template<typename OtherIterator >
bool DGtal::Circulator< TIterator >::operator== ( const OtherIterator &  other) const
inline

Definition at line 320 of file Circulator.h.

321  {
322  return ( ( (!isValid())&&(!other.isValid()) )
323  ||
324  ( ( isValid() && other.isValid())
325  &&
326  ( (myBeginIt == other.begin())
327  &&(myEndIt == other.end())
328  &&(myCurrentIt == other.base())
329  )
330  )
331  );
332  }

References DGtal::Circulator< TIterator >::isValid(), DGtal::Circulator< TIterator >::myBeginIt, DGtal::Circulator< TIterator >::myCurrentIt, and DGtal::Circulator< TIterator >::myEndIt.

◆ operator==() [2/2]

template<typename TIterator >
bool DGtal::Circulator< TIterator >::operator== ( const Self other) const
inline

Definition at line 304 of file Circulator.h.

305  {
306  return ( ( (!isValid())&&(!other.isValid()) )
307  ||
308  ( ( isValid() && other.isValid())
309  &&
310  ( (myBeginIt == other.begin())
311  &&(myEndIt == other.end())
312  &&(myCurrentIt == other.base())
313  )
314  )
315  );
316  }

References DGtal::Circulator< TIterator >::base(), DGtal::Circulator< TIterator >::begin(), DGtal::Circulator< TIterator >::end(), DGtal::Circulator< TIterator >::isValid(), DGtal::Circulator< TIterator >::myBeginIt, DGtal::Circulator< TIterator >::myCurrentIt, and DGtal::Circulator< TIterator >::myEndIt.

◆ operator>()

template<typename TIterator >
bool DGtal::Circulator< TIterator >::operator> ( const Self c) const
inline

Definition at line 417 of file Circulator.h.

418  {
419  return !operator<=(c);
420  }
bool operator<=(const Self &) const
Definition: Circulator.h:413

References DGtal::Circulator< TIterator >::operator<=().

Referenced by DGtal::Circulator< TIterator >::operator>=().

◆ operator>=()

template<typename TIterator >
bool DGtal::Circulator< TIterator >::operator>= ( const Self c) const
inline

Definition at line 421 of file Circulator.h.

422  {
423  return !operator>(c);
424  }
bool operator>(const Self &c) const
Definition: Circulator.h:417

References DGtal::Circulator< TIterator >::operator>().

◆ operator[]()

template<typename TIterator >
reference DGtal::Circulator< TIterator >::operator[] ( difference_type  d) const
inline

Definition at line 401 of file Circulator.h.

402  {
403  Self tmp = *this;
404  tmp += d;
405  return *tmp;
406  }

Field Documentation

◆ myBeginIt

template<typename TIterator >
Iterator DGtal::Circulator< TIterator >::myBeginIt
protected

◆ myCurrentIt

template<typename TIterator >
Iterator DGtal::Circulator< TIterator >::myCurrentIt
protected

◆ myEndIt

template<typename TIterator >
Iterator DGtal::Circulator< TIterator >::myEndIt
protected

◆ myFlagIsValid

template<typename TIterator >
bool DGtal::Circulator< TIterator >::myFlagIsValid
protected

The documentation for this class was generated from the following file: