Java iterators for C++:
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
Public Member Functions | Protected Attributes | Friends | List of all members
iterJava< Iter > Class Template Reference

An iterator over a collection. More...

#include <iterJava.h>

Public Member Functions

 iterJava ()
 Default constructor. More...
 
 iterJava (Iter first, Iter end)
 Constructor to traverse from first up to end. More...
 
 iterJava (const iterJava &o)
 Copy constructor. More...
 
 ~iterJava ()
 Destructor. More...
 
iterJavaoperator= (const iterJava &o)
 Copy. More...
 
bool hasNext () const
 Returns true if the iteration has more elements. More...
 
std::iterator_traits< Iter >
::reference 
next ()
 Returns the next element in the iteration. More...
 
std::iterator_traits< Iter >
::reference 
current () const
 Returns the value that the last invocation to next() returned. More...
 
 operator Iter () const
 Converts into an iterator over the underlying collection. More...
 
void set (Iter first, Iter end)
 Resets the iterator to traverse from first up to end. More...
 
template<typename C >
void set (C &CCC)
 Resets the iterator to traverse from C. More...
 
template<typename C >
void set (const C &CCC)
 Resets the iterator to traverse from C (const version). More...
 
void setReverse ()
 Sets the iterator to go reverse (backwards). More...
 
bool isForward () const
 Returns true if the iteration is natural, from first to last. More...
 
bool isReverse () const
 Returns true if the iteration goes in reverse (backwards). More...
 
void setBackward ()
 Sets the iterator to go reverse (backwards). More...
 
bool isBackward () const
 Returns true if the iteration goes in reverse (backwards). More...
 

Protected Attributes

Iter itPrev
 Remember last value returned. More...
 
Iter itNext
 Next value to return. More...
 
Iter itEnd
 Past the last value. More...
 
bool isFwd
 "false" if it is a reverse_iterator. More...
 

Friends

bool operator== (const iterJava< Iter > &l, const iterJava< Iter > &r)
 ( l == r ) ? More...
 
bool operator!= (const iterJava< Iter > &l, const iterJava< Iter > &r)
 ( l != r ) ? More...
 

Detailed Description

template<typename Iter>
class iterJava< Iter >

An iterator over a collection.

Iterators differ from enumerations in two ways:

{{ // test::example()
std::list<long> Lfwd, Lbck, L;
assertTrue( Lfwd.empty() && L.empty() );
L = makeList_long( "( 1 2 3 4 5 )" );
iterJava< std::list<long>::iterator > itFwd( L.begin(), L.end() );
iterJava< std::list<long>::iterator > itBck( L.begin(), L.end() );
itBck.setReverse();
while ( itFwd.hasNext() && itBck.hasNext() ) {
Lfwd.push_back( itFwd.next() );
Lbck.push_back( itBck.next() );
}
assertTrue( Lfwd == makeList_long( "( 1 2 3 4 5 )" ) );
assertTrue( Lbck == makeList_long( "( 5 4 3 2 1 )" ) );
}}

See Also
test_iterJava::test_example()
{{ // test::const_example()
#define const_error
#undef const_error
std::list<long> Lcp, L;
assertTrue( Lcp.empty() );
L = makeList_long( "( 1 2 3 4 5 )" );
iterJava< std::list<long>::const_iterator > iter( L.begin(),L.end() );
while ( iter.hasNext() ) {
Lcp.push_back( iter.next() );
#ifdef const_error
L.erase(iter); // forbidden for const_iterator
#endif
}
assertTrue( L == Lcp );
assertTrue( IT == iter && !( IT.hasNext() ) && !( IT != iter ) );
#undef const_error
}}

See Also
test_iterJava::test_const_example()
{{ // test::examplePtr()
#define dim(V) ( sizeof(V)/sizeof(*V) )
std::list<long> L;
int VEC[] = { 1, 2, 3, 4, 5 };
iterJava< int* > iter( VEC, VEC+dim(VEC) );
while ( iter.hasNext() ) {
L.push_back( iter.next() );
}
assertTrue( L == makeList_long( "( 1 2 3 4 5 )" ) );
}}

See Also
test_iterJava::test_examplePtr()

Definition at line 23 of file iterJava.h.

Constructor & Destructor Documentation

template<typename Iter>
iterJava< Iter >::iterJava ( )
inline

Default constructor.

Definition at line 30 of file iterJava.h.

template<typename Iter>
iterJava< Iter >::iterJava ( Iter  first,
Iter  end 
)
inline

Constructor to traverse from first up to end.

Definition at line 32 of file iterJava.h.

template<typename Iter>
iterJava< Iter >::iterJava ( const iterJava< Iter > &  o)
inline

Copy constructor.

Definition at line 34 of file iterJava.h.

template<typename Iter>
iterJava< Iter >::~iterJava ( )
inline

Destructor.

Definition at line 37 of file iterJava.h.

Member Function Documentation

template<typename Iter>
iterJava< Iter >::operator= ( const iterJava< Iter > &  o)
inline

Copy.

{{ // test::assign()
std::list<long> L, Lcp;
L = makeList_long( "( 1 2 3 4 5 )" );
iter123.set( L.begin(), L.end() ); // full range
int i=1;
while ( i<=3 ) {
assertTrue( i == iter123.next() );
Lcp.push_back( i );
++i;
}
iter45 = iter123; // iter45 <==> iter123
while ( i<=5 ) {
assertTrue( i == iter45.next() );
Lcp.push_back( i );
++i;
}
assertTrue( Lcp == makeList_long( "( 1 2 3 4 5 )" ) );
}}

See Also
test_iterJava::test_assign()

Definition at line 38 of file iterJava.h.

template<typename Iter>
iterJava< Iter >::hasNext ( ) const
inline

Returns true if the iteration has more elements.

   (In other words, returns \c true if \c next() would return an element
   rather than breaking up.)
Returns
true if the iterator has more elements.

Definition at line 44 of file iterJava.h.

template<typename Iter>
iterJava< Iter >::next ( )
inline

Returns the next element in the iteration.

   Calling this method repeatedly until the \c hasNext() method returns
   \c false will return each element in the underlying collection exactly once.
Returns
The next element in the iteration.
{{ // test::next()
std::list<long> L = makeList_long( "( 1 2 3 4 5 )" );
iterJava< std::list<long>::iterator > iter( L.begin(), L.end() );
int trngl = 3;
while ( iter.hasNext() ) {
switch (trngl++ % 3) {
case 0: { // reference
long & l = iter.next(); l++;
break;
}
case 1: { // pointer
long * p = & iter.next(); (*p)++;
break;
}
case 2: { // direct
iter.next()++;
break;
}
}
}
assertTrue( L == makeList_long( "( 2 3 4 5 6 )" ) );
}}

See Also
test_iterJava::test_next()

Definition at line 48 of file iterJava.h.

template<typename Iter>
iterJava< Iter >::current ( ) const
inline

Returns the value that the last invocation to next() returned.

   - Calling this method after the iterator was set but before
     the first invocation to \c next() is an error.
Returns
The value that the last invocation to next() returned.

Definition at line 57 of file iterJava.h.

template<typename Iter>
iterJava< Iter >::operator Iter ( ) const
inline

Converts into an iterator over the underlying collection.

  • This conversion is usefull to remove from the underlaying collection the last value returned by next().
  • This conversion works both for const and non const iterations.
Returns
The value that the last invocation to next() returned.
{{ // test::erase()
std::list<long> L = makeList_long( "( 1 2 3 4 5 )" );
iterJava< std::list<long>::iterator > iter( L.begin(), L.end() );
int i = 0;
while ( iter.hasNext() ) {
assertTrue( ! L.empty() );
long *pL = & iter.next(); i++;
assertTrue( i == *pL );
L.erase( iter ); // L.erase( iter.current() );
}
assertTrue( L.empty() );
}}

See Also
test_iterJava::test_erase()

Definition at line 58 of file iterJava.h.

template<typename Iter>
iterJava< Iter >::set ( Iter  first,
Iter  end 
)
inline

Resets the iterator to traverse from first up to end.

Definition at line 60 of file iterJava.h.

template<typename Iter>
template<typename C >
iterJava< Iter >::set ( C &  CCC)
inline

Resets the iterator to traverse from C.

Definition at line 62 of file iterJava.h.

template<typename Iter>
template<typename C >
iterJava< Iter >::set ( const C &  CCC)
inline

Resets the iterator to traverse from C (const version).

{{ // test::set_CCC()
std::list<long> L1 = makeList_long( "( 1 2 3 4 5 )" );
std::list<long> L2 = makeList_long( "( 6 7 8 9 )" );
iterJava< std::list<long>::iterator > iter( L1.begin(), L1.end() );
int i = 0;
while ( iter.hasNext() ) { // L1
i++; assertTrue( i == iter.next() );
L1.erase( iter );
}
iter.set( L2 );
while ( iter.hasNext() ) { // L2
i++; assertTrue( i == iter.next() );
}
assertTrue( L1.empty() );
}}

See Also
test_iterJava::test_set_CCC()

Definition at line 64 of file iterJava.h.

template<typename Iter>
iterJava< Iter >::setReverse ( )
inline

Sets the iterator to go reverse (backwards).

Precondition
Method next() must not be invoked before using setReverse().
See Also
isPalindrome();
{{ // test::palindrome()
std::list<long> L = makeList_long( "( 1 2 3 2 1 )" );
typedef std::list<long>::const_iterator IterFwd;
typedef std::reverse_iterator<IterFwd> IterBck;
iterJava< IterFwd > itFwd( L.begin(), L.end() );
iterJava< IterBck > itBck( L.rbegin(), L.rend() );
while ( itFwd.hasNext() && itBck.hasNext() ) {
if ( itFwd.next() != itBck.next() ) {
assertTrue( "palindrome error" && false );
}
}
assertTrue( !itFwd.hasNext() && !itBck.hasNext() );
}}

See Also
test_iterJava::test_palindrome()
isPalindrome();

Definition at line 66 of file iterJava.h.

template<typename Iter>
iterJava< Iter >::isForward ( ) const
inline

Returns true if the iteration is natural, from first to last.

Definition at line 75 of file iterJava.h.

template<typename Iter>
iterJava< Iter >::isReverse ( ) const
inline

Returns true if the iteration goes in reverse (backwards).

   - Only after \c setReverse() is invoked will \c isReverse() return \c true.
{{ // test::setReverse()
std::list<long> L = makeList_long( "( 1 2 3 4 5 )" );
typedef std::list<long>::const_iterator Iter;
iterJava< Iter > itFwd, itBck;
itFwd.set(L);
assertTrue( itFwd.next() == 1 ); // next() !
itFwd.setReverse(); // NOP !!!
assertTrue( itFwd.isForward() );
itBck.set(L); itBck.setReverse();
assertTrue( !itBck.isForward() );
assertTrue( itBck.isBackward() && itBck.isReverse() );
assertTrue( itBck.next() == 5 );
itFwd.set(L); itFwd.setReverse();
assertTrue( ! itFwd.isForward() && itFwd.isReverse() );
}}

See Also
test_iterJava::test_setReverse()

Definition at line 76 of file iterJava.h.

template<typename Iter>
iterJava< Iter >::setBackward ( )
inline

Sets the iterator to go reverse (backwards).

Precondition
Method next() must not be invoked before using setReverse().

Definition at line 77 of file iterJava.h.

template<typename Iter>
iterJava< Iter >::isBackward ( ) const
inline

Returns true if the iteration goes in reverse (backwards).

   - Only after \c setReverse() is invoked will \c isReverse() return \c true.
   - Handy sinomim for \c isReverse().
{{ // test::setReverse()
std::list<long> L = makeList_long( "( 1 2 3 4 5 )" );
typedef std::list<long>::const_iterator Iter;
iterJava< Iter > itFwd, itBck;
itFwd.set(L);
assertTrue( itFwd.next() == 1 ); // next() !
itFwd.setReverse(); // NOP !!!
assertTrue( itFwd.isForward() );
itBck.set(L); itBck.setReverse();
assertTrue( !itBck.isForward() );
assertTrue( itBck.isBackward() && itBck.isReverse() );
assertTrue( itBck.next() == 5 );
itFwd.set(L); itFwd.setReverse();
assertTrue( ! itFwd.isForward() && itFwd.isReverse() );
}}

See Also
test_iterJava::test_setReverse()

Definition at line 78 of file iterJava.h.

Friends And Related Function Documentation

template<typename Iter>
bool operator== ( const iterJava< Iter > &  l,
const iterJava< Iter > &  r 
)
friend

( l == r ) ?

Definition at line 81 of file iterJava.h.

template<typename Iter>
bool operator!= ( const iterJava< Iter > &  l,
const iterJava< Iter > &  r 
)
friend

( l != r ) ?

Definition at line 87 of file iterJava.h.

Member Data Documentation

template<typename Iter>
iterJava< Iter >::itPrev
protected

Remember last value returned.

Definition at line 25 of file iterJava.h.

template<typename Iter>
iterJava< Iter >::itNext
protected

Next value to return.

Definition at line 26 of file iterJava.h.

template<typename Iter>
iterJava< Iter >::itEnd
protected

Past the last value.

Definition at line 27 of file iterJava.h.

template<typename Iter>
iterJava< Iter >::isFwd
protected

"false" if it is a reverse_iterator.

Definition at line 28 of file iterJava.h.


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