STIR  6.2.0
Public Types | Public Member Functions | Friends | List of all members
stir::NestedIterator< topleveliterT, GetRestRangeFunctionT > Class Template Reference

Class NestedIterator implements a (forward) iterator using a pair of 'nested' iterators. More...

#include "stir/NestedIterator.h"

Public Types

typedef std::forward_iterator_tag iterator_category
 
typedef boost::iterator_difference< rest_iter_type >::type difference_type
 
typedef boost::iterator_value< rest_iter_type >::type value_type
 
typedef boost::iterator_reference< rest_iter_type >::type reference
 
typedef boost::iterator_pointer< rest_iter_type >::type pointer
 

Public Member Functions

 NestedIterator ()
 default constructor
 
 NestedIterator (const topleveliterT &top_level_iter, const topleveliterT &end_top_level_iter)
 constructor to initialise the members
 
template<typename othertopleveliterT , typename otherGetRestRangeFunctionT >
 NestedIterator (NestedIterator< othertopleveliterT, otherGetRestRangeFunctionT > other, typename boost::enable_if_convertible< othertopleveliterT, topleveliterT >::type *=0, typename boost::enable_if_convertible< typename otherGetRestRangeFunctionT::rest_iter_type, rest_iter_type >::type *=0)
 constructor to convert between nested iterators using convertible top and next level iterators More...
 
NestedIteratoroperator++ ()
 prefix increment
 
NestedIterator operator++ (int)
 postfix increment
 
bool operator== (const NestedIterator &) const
 test equality
 
bool operator!= (const NestedIterator &) const
 test equality
 
reference operator* () const
 dereferencing operator
 
pointer operator-> () const
 member-selection operator
 

Friends

template<class , class >
class NestedIterator
 

Detailed Description

template<typename topleveliterT, class GetRestRangeFunctionT = BeginEndFunction<topleveliterT>>
class stir::NestedIterator< topleveliterT, GetRestRangeFunctionT >

Class NestedIterator implements a (forward) iterator using a pair of 'nested' iterators.

Suppose you have a container where each element is a container, e.g. std::vector<std::list<int> > . Using NestedIterator, you can iterate through the 'elements of the elements' (i.e. the int's in the above example).

The template argument GetRestRangeFunctionT should be a function object that, given a top-level iterator, finds the first and last iterators for the sub-sequence. It defaults to just using

current_rest_iter = top_level_iter->begin();
end_rest_iter = top_level_iter->end();
See also
BeginEndFunction, PtrBeginEndFunction, ConstBeginEndFunction, ConstPtrBeginEndFunction, BeginEndAllFunction, PtrBeginEndAllFunction, ConstBeginEndAllFunction, ConstPtrBeginEndAllFunction

Syntax is somewhat awkward for technical reasons (see the source for operator==). You have to give the begin and end of the top-level iterators at construction time. (This would be far more natural when using boost::range).

examples
Here is an example using a vector of lists of integers.
typedef std::list<int> C2;
typedef std::vector<C2> C1;
C1 c;
typedef NestedIterator<C1::iterator> FullIter;
FullIter fiter(c.begin(),c.end());
const FullIter fiter_end(c.end(),c.end());
while (fiter != fiter_end)
{ ... }
Here is an example using a vector of (smart) pointers to 2D arrays, where we want to iterate over all elements of all 2D arrays.
typedef Array<2,int> C2;
typedef std::vector<shared_ptr<C2> > C1;
C1 c;
typedef NestedIterator<C1::iterator, PtrBeginEndAllFunction<C1::iterator> > FullIter;
FullIter fiter(c.begin(),c.end());
const FullIter fiter_end(c.end(),c.end());
while (fiter != fiter_end)
{ ... }
Implementation note

The 2nd template argument would really be better implemented as a template template. However, some compilers still don't support this.

Bug:
At present, iterator_category typedef is hard-wired to be std::forward_iterator_tag. This would be incorrect if topleveliterT or rest_iter_type is only an input or output iterator.

Constructor & Destructor Documentation

◆ NestedIterator()

template<typename topleveliterT, class GetRestRangeFunctionT = BeginEndFunction<topleveliterT>>
template<typename othertopleveliterT , typename otherGetRestRangeFunctionT >
stir::NestedIterator< topleveliterT, GetRestRangeFunctionT >::NestedIterator ( NestedIterator< othertopleveliterT, otherGetRestRangeFunctionT >  other,
typename boost::enable_if_convertible< othertopleveliterT, topleveliterT >::type *  = 0,
typename boost::enable_if_convertible< typename otherGetRestRangeFunctionT::rest_iter_type, rest_iter_type >::type *  = 0 
)
inline

constructor to convert between nested iterators using convertible top and next level iterators

Ignore the 2nd and 3rd argument. They are there to let the compiler check if the types are convertible (using the SFINAE principle).


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