00001 #ifndef FILTER_ITERATOR_H
00002 #define FILTER_ITERATOR_H
00003
00004 #include "y2storage/AppUtil.h"
00005 #include "y2storage/IterPair.h"
00006
00007 template< class Pred, class Iter >
00008 class FilterIterator : public std::iterator<std::bidirectional_iterator_tag,
00009 typename Iter::value_type>
00010 {
00011 public:
00012 typedef typename Iter::value_type value_type;
00013 typedef typename Iter::reference reference;
00014 typedef typename Iter::pointer pointer;
00015 typedef typename Iter::difference_type difference_type;
00016 typedef typename Iter::iterator_category iterator_category;
00017
00018 FilterIterator() { }
00019
00020 FilterIterator( const Iter& begin, const Iter& end, Pred f, bool setend=false ) :
00021 m_begin(begin), m_end(end), m_cur(begin), m_f(f)
00022 {
00023 initialize( setend );
00024 }
00025 FilterIterator( const IterPair<Iter>& p, Pred f, bool setend=false ) :
00026 m_begin(p.begin()), m_end(p.end()), m_cur(p.begin()), m_f(f)
00027 {
00028 initialize( setend );
00029 }
00030
00031 FilterIterator( const FilterIterator& x )
00032 {
00033 copyMembers( x );
00034 }
00035
00036 FilterIterator& operator=(const FilterIterator& x)
00037 {
00038 copyMembers( x );
00039 return *this;
00040 }
00041
00042 FilterIterator& operator++()
00043 {
00044 ++m_cur;
00045 assertPred();
00046 return *this;
00047 }
00048 FilterIterator operator++(int)
00049 {
00050 y2warning( "Expensive ++ FilterIterator" );
00051 FilterIterator tmp(*this);
00052 ++m_cur;
00053 assertPred();
00054 return tmp;
00055 }
00056
00057 FilterIterator& operator--()
00058 {
00059 --m_cur;
00060 assertPred(false);
00061 return *this;
00062 }
00063 FilterIterator operator--(int)
00064 {
00065 y2warning( "Expensive -- FilterIterator" );
00066 FilterIterator tmp(*this);
00067 --m_cur;
00068 assertPred(false);
00069 return tmp;
00070 }
00071
00072 value_type operator*() const
00073 {
00074 return( *m_cur );
00075 }
00076
00077 pointer operator->() const
00078 {
00079 return( &(*m_cur) );
00080 }
00081 bool operator==(const FilterIterator& x) const
00082 {
00083 return( m_cur == x.cur() );
00084 }
00085 bool operator!=(const FilterIterator& x) const
00086 {
00087 return( m_cur != x.cur() );
00088 }
00089
00090 Pred pred() const {return m_f;}
00091 Iter end() const {return m_end;}
00092 Iter cur() const {return m_cur;}
00093 Iter begin() const {return m_begin;}
00094
00095 private:
00096 Iter m_begin;
00097 Iter m_end;
00098 Iter m_cur;
00099 Pred m_f;
00100
00101 void initialize( bool setend )
00102 {
00103 if( setend )
00104 m_cur = m_end;
00105 else
00106 assertPred();
00107 }
00108
00109 void copyMembers( const FilterIterator& x )
00110 {
00111 m_begin = x.begin();
00112 m_end = x.end();
00113 m_cur = x.cur();
00114 m_f = x.pred();
00115 }
00116
00117
00118 void assertPred( bool forward=true )
00119 {
00120 if( forward )
00121 while( m_cur!=m_end && !m_f(**m_cur) )
00122 ++m_cur;
00123 else
00124 while( m_cur!=m_begin && !m_f(**m_cur) )
00125 --m_cur;
00126 };
00127
00128 };
00129
00130 #endif