DGtal  1.2.0
Public Types | Public Member Functions | Protected Member Functions
DGtal::IteratorCompletion< TDerived > Class Template Reference

Aim: Class that uses CRTP to add reverse iterators and ranges to a derived class. More...

#include <DGtal/base/IteratorCompletion.h>

Inheritance diagram for DGtal::IteratorCompletion< TDerived >:
[legend]

Public Types

typedef IteratorCompletionTraits< TDerived >::Iterator Iterator
 Mutable iterator type. More...
 
typedef IteratorCompletionTraits< TDerived >::ConstIterator ConstIterator
 Constant iterator type. More...
 
typedef IteratorCompletionTraits< TDerived >::DistanceFunctor DistanceFunctor
 Type of the functor calculating the distance between iterators. More...
 
typedef boost::reverse_iterator< IteratorReverseIterator
 Mutable reverse iterator type. More...
 
typedef boost::reverse_iterator< ConstIteratorConstReverseIterator
 Constant reverse iterator type. More...
 
typedef SimpleRandomAccessRangeFromPoint< ConstIterator, Iterator, DistanceFunctorRange
 Mutable range type. More...
 
typedef SimpleRandomAccessConstRangeFromPoint< ConstIterator, DistanceFunctorConstRange
 Constant range type. More...
 
typedef std::ptrdiff_t Difference
 Type of the distance between two iterators. More...
 

Public Member Functions

ReverseIterator rbegin ()
 
ConstReverseIterator rbegin () const
 
ConstReverseIterator crbegin () const
 
ReverseIterator rend ()
 
ConstReverseIterator rend () const
 
ConstReverseIterator crend () const
 
Range range ()
 
ConstRange constRange () const
 

Protected Member Functions

 ~IteratorCompletion ()
 Protected destructor to avoid memory leak. More...
 

Detailed Description

template<typename TDerived>
class DGtal::IteratorCompletion< TDerived >

Aim: Class that uses CRTP to add reverse iterators and ranges to a derived class.

Description of template class 'IteratorCompletionTraits'

This class adds new iterators to a given class if it provides a minimal interface for random-access iterators related to points.

More precisely, it provides:

Each derived class of IteratorCompletion must specialize IteratorCompletionTraits in order to provide enough informations on his iterators, especially a distance functor between a given point and the begin iterator.

Template Parameters
TDerivedType of the derived class (CRTP).

The following snippets illustrate how to use it to easily build a concepts::CImage model from a C-style array (better use ArrayImageAdapter for that purpose).

After common includes:

#include <new>
#include <cstddef>
#include <algorithm>
#include <iostream>
#include <DGtal/base/Common.h>
#include <DGtal/base/IteratorCompletion.h>
#include <DGtal/kernel/domains/HyperRectDomain.h>
#include <DGtal/kernel/domains/Linearizer.h>
#include <DGtal/kernel/SpaceND.h>
#include <DGtal/images/CImage.h>

we start with the head of our class, including typedefs, constructor and destructor:

// A minimal image from a C-style array with reverse-iterators and ranges.
template < typename T, DGtal::Dimension N >
class MyImage
: public DGtal::IteratorCompletion< MyImage<T, N> > // CRTP
{
public:
// Typedefs
typedef MyImage<T, N> Self;
typedef typename DGtal::IteratorCompletionTraits<Self>::Iterator Iterator; // to be coherent with the traits that use IteratorCompletion
typedef T Value; // The image values type
typedef DGtal::SpaceND<N> Space; // The image space type
typedef DGtal::HyperRectDomain<Space> Domain; // The image domain type
typedef typename Space::Point Point; // The associated point type
// Contructor
MyImage( Domain const& aDomain = Domain() )
: myDomain(aDomain), myData( new T[ myDomain.size() ] )
{}
// Destructor
~MyImage()
{
delete[] myData;
}
Aim: Traits that must be specialized for each IteratorCompletion derived class.
Aim: Class that uses CRTP to add reverse iterators and ranges to a derived class.
IteratorCompletionTraits< TDerived >::ConstIterator ConstIterator
Constant iterator type.
IteratorCompletionTraits< TDerived >::Iterator Iterator
Mutable iterator type.
Aim: Linearization and de-linearization interface for domains.
Definition: Linearizer.h:78
MyPointD Point
Definition: testClone2.cpp:383
Linearizer< Domain, ColMajorStorage > Linearizer
HyperRectDomain< Space > Domain

As you can see, the MyImage class inherits from IteratorCompletion templated with his own type (CRTP). The public inheritance is necessary is order to make visible the methods and typedefs provided by IteratorCompletion.

Then, we add the basic interface needed for images:

// Unary Functor
Value operator() ( Point const& aPoint ) const
{
return myData[ Linearizer::getIndex(aPoint, myDomain) ];
}
// Getter
Value getValue( Point const& aPoint ) const
{
return operator()(aPoint);
}
// Setter
void setValue( Point const& aPoint, Value const& aValue )
{
myData[ Linearizer::getIndex(aPoint, myDomain) ] = aValue;
}
// Returns the associated domain
Domain const& domain() const
{
return myDomain;
}
// Assignability (needed for CImage concept)
Self& operator= ( Self const& other )
{
if ( this != &other )
{
myDomain = other.domain();
delete[] myData;
myData = new Value[ myDomain.size() ];
std::copy( other.begin(), other.end(), begin() );
}
return *this;
}
Domain domain
const Point aPoint(3, 4)

and the minimal iterator accessors:

// Begin mutable iterator
Iterator begin()
{
return myData;
}
// Begin constant iterator
ConstIterator begin() const
{
return myData;
}
// End mutable iterator
Iterator end()
{
return myData + myDomain.size();
}
// End constant iterator
ConstIterator end() const
{
return myData + myDomain.size();
}
MyDigitalSurface::ConstIterator ConstIterator

The class is finally closed with the private members:

private:
Domain myDomain;
T* myData;
};

A specialization of IteratorCompletionTraits is needed in order to provide IteratorCompletion with the Iterator and ConstIterator type, and a DistanceFunctor that returns the distance from the begin iterator to a given point (see SimpleRandomAccessRangeFromPoint and SimpleRandomAccessConstRangeFromPoint):

namespace DGtal
{
// Specialization of IteratorCompletionTraits for MyVector
template < typename T, Dimension N >
class IteratorCompletionTraits< MyImage<T, N> >
{
public:
typedef MyImage<T, N> Self;
typedef T* Iterator;
typedef T const* ConstIterator;
{
public:
typedef typename Self::Domain Domain;
typedef typename Self::Point Point;
typedef typename Self::Difference Difference;
DistanceFunctor( Self const* aVector )
: myDomain(aVector->domain())
{}
Difference operator() ( Point const& aPoint ) const
{
return Self::Linearizer::getIndex( aPoint, myDomain );
}
private:
Domain myDomain;
};
};
}
std::ptrdiff_t Difference
Type of the distance between two iterators.
IteratorCompletionTraits< TDerived >::DistanceFunctor DistanceFunctor
Type of the functor calculating the distance between iterators.
DGtal is the top-level namespace which contains all DGtal functions and types.

We have now a concepts::CImage model that can be used like any image:

using namespace std;
// Typedefs
typedef MyImage<double, 2> My2DImage;
// Checks CImage concept
BOOST_CONCEPT_ASSERT( (DGtal::concepts::CImage<My2DImage>) );
// Construction
My2DImage image( Domain( Point(0,0), Point(2,3) ) );
// Filling
Value i = 0;
for ( My2DImage::Iterator it = image.begin(), it_end = image.end() ; it != it_end; ++it )
{
*it = ++i;
}
// Modifying a value
image.setValue( Point(1,1), -1 );
// Forward reading using range
My2DImage::ConstRange range = image.constRange();
copy( range.begin(), range.end(), ostream_iterator<Value>( cout, " " ) );
cout << endl;
// Backward reading using reverse iterators
copy( image.rbegin(), image.rend(), ostream_iterator<Value>( cout, " " ) );
cout << endl;
Aim: Defines the concept describing a read/write image, having an output iterator.
Definition: CImage.h:103
Image image(domain)
Image::ConstRange ConstRange
See also
ArrayImageAdapter.h
exampleIteratorCompletion.cpp

Definition at line 116 of file IteratorCompletion.h.

Member Typedef Documentation

◆ ConstIterator

template<typename TDerived >
typedef IteratorCompletionTraits<TDerived>::ConstIterator DGtal::IteratorCompletion< TDerived >::ConstIterator

Constant iterator type.

Definition at line 121 of file IteratorCompletion.h.

◆ ConstRange

Constant range type.

Definition at line 127 of file IteratorCompletion.h.

◆ ConstReverseIterator

template<typename TDerived >
typedef boost::reverse_iterator<ConstIterator> DGtal::IteratorCompletion< TDerived >::ConstReverseIterator

Constant reverse iterator type.

Definition at line 125 of file IteratorCompletion.h.

◆ Difference

template<typename TDerived >
typedef std::ptrdiff_t DGtal::IteratorCompletion< TDerived >::Difference

Type of the distance between two iterators.

Definition at line 128 of file IteratorCompletion.h.

◆ DistanceFunctor

template<typename TDerived >
typedef IteratorCompletionTraits<TDerived>::DistanceFunctor DGtal::IteratorCompletion< TDerived >::DistanceFunctor

Type of the functor calculating the distance between iterators.

Definition at line 122 of file IteratorCompletion.h.

◆ Iterator

template<typename TDerived >
typedef IteratorCompletionTraits<TDerived>::Iterator DGtal::IteratorCompletion< TDerived >::Iterator

Mutable iterator type.

Definition at line 120 of file IteratorCompletion.h.

◆ Range

Mutable range type.

Definition at line 126 of file IteratorCompletion.h.

◆ ReverseIterator

template<typename TDerived >
typedef boost::reverse_iterator<Iterator> DGtal::IteratorCompletion< TDerived >::ReverseIterator

Mutable reverse iterator type.

Definition at line 124 of file IteratorCompletion.h.

Constructor & Destructor Documentation

◆ ~IteratorCompletion()

template<typename TDerived >
DGtal::IteratorCompletion< TDerived >::~IteratorCompletion ( )
inlineprotected

Protected destructor to avoid memory leak.

Definition at line 224 of file IteratorCompletion.h.

225  {}

Member Function Documentation

◆ constRange()

template<typename TDerived >
ConstRange DGtal::IteratorCompletion< TDerived >::constRange ( ) const
inline
Returns
a constant range over the derived class values.
Warning
the derived class must have begin() and end() methods that return constant random-access iterators.
In addition, the class must provide a distance functor to a point.

Definition at line 211 of file IteratorCompletion.h.

212  {
213  TDerived const* const derived = static_cast<TDerived const*>(this);
214  return ConstRange(
215  derived->begin(),
216  derived->end(),
217  typename IteratorCompletionTraits<TDerived>::DistanceFunctor( derived )
218  );
219  }
SimpleRandomAccessConstRangeFromPoint< ConstIterator, DistanceFunctor > ConstRange
Constant range type.

◆ crbegin()

template<typename TDerived >
ConstReverseIterator DGtal::IteratorCompletion< TDerived >::crbegin ( ) const
inline
Returns
a constant reverse-iterator pointing to the last value (C++11).
Warning
the derived class must have a cend() method that returns a constant bidirectional iterator.

Definition at line 154 of file IteratorCompletion.h.

155  {
156  return ConstReverseIterator( static_cast<TDerived*>(this)->cend() );
157  }
boost::reverse_iterator< ConstIterator > ConstReverseIterator
Constant reverse iterator type.

◆ crend()

template<typename TDerived >
ConstReverseIterator DGtal::IteratorCompletion< TDerived >::crend ( ) const
inline
Returns
a constant reverse-iterator pointing before the first value (C++11).
Warning
the derived class must have a cbegin() method that returns a constant bidirectional iterator.

Definition at line 184 of file IteratorCompletion.h.

185  {
186  return ConstReverseIterator( static_cast<TDerived*>(this)->cbegin() );
187  }

◆ range()

template<typename TDerived >
Range DGtal::IteratorCompletion< TDerived >::range ( )
inline
Returns
a mutable range over the derived class values.
Warning
the derived class must have begin() and end() methods that return mutable random-access iterators.
In addition, the class must provide a distance functor to a point.

Definition at line 195 of file IteratorCompletion.h.

196  {
197  TDerived* const derived = static_cast<TDerived*>(this);
198  return Range(
199  derived->begin(),
200  derived->end(),
201  typename IteratorCompletionTraits<TDerived>::DistanceFunctor( derived )
202  );
203  }
SimpleRandomAccessRangeFromPoint< ConstIterator, Iterator, DistanceFunctor > Range
Mutable range type.

◆ rbegin() [1/2]

template<typename TDerived >
ReverseIterator DGtal::IteratorCompletion< TDerived >::rbegin ( )
inline
Returns
a mutable reverse-iterator pointing to the last value.
Warning
the derived class must have a end() method that returns a mutable bidirectional iterator.

Definition at line 134 of file IteratorCompletion.h.

135  {
136  return ReverseIterator( static_cast<TDerived*>(this)->end() );
137  }
boost::reverse_iterator< Iterator > ReverseIterator
Mutable reverse iterator type.

◆ rbegin() [2/2]

template<typename TDerived >
ConstReverseIterator DGtal::IteratorCompletion< TDerived >::rbegin ( ) const
inline
Returns
a constant reverse-iterator pointing to the last value.
Warning
the derived class must have a end() method that returns a constant bidirectional iterator.

Definition at line 144 of file IteratorCompletion.h.

145  {
146  return ConstReverseIterator( static_cast<TDerived*>(this)->end() );
147  }

◆ rend() [1/2]

template<typename TDerived >
ReverseIterator DGtal::IteratorCompletion< TDerived >::rend ( )
inline
Returns
a mutable reverse-iterator pointing before the first value.
Warning
the derived class must have a begin() method that returns a mutable bidirectional iterator.

Definition at line 164 of file IteratorCompletion.h.

165  {
166  return ReverseIterator( static_cast<TDerived*>(this)->begin() );
167  }

◆ rend() [2/2]

template<typename TDerived >
ConstReverseIterator DGtal::IteratorCompletion< TDerived >::rend ( ) const
inline
Returns
a constant reverse-iterator pointing before the first value.
Warning
the derived class must have a begin() method that returns a constant bidirectional iterator.

Definition at line 174 of file IteratorCompletion.h.

175  {
176  return ConstReverseIterator( static_cast<TDerived*>(this)->begin() );
177  }

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