|
yast2-storage
|
00001 /* 00002 * Copyright (c) [2004-2009] Novell, Inc. 00003 * 00004 * All Rights Reserved. 00005 * 00006 * This program is free software; you can redistribute it and/or modify it 00007 * under the terms of version 2 of the GNU General Public License as published 00008 * by the Free Software Foundation. 00009 * 00010 * This program is distributed in the hope that it will be useful, but WITHOUT 00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 00013 * more details. 00014 * 00015 * You should have received a copy of the GNU General Public License along 00016 * with this program; if not, contact Novell, Inc. 00017 * 00018 * To contact Novell about this file by physical or electronic mail, you may 00019 * find current contact information at www.novell.com. 00020 */ 00021 00022 00023 #ifndef FILTER_ITERATOR_H 00024 #define FILTER_ITERATOR_H 00025 00026 #include "storage/AppUtil.h" 00027 #include "storage/IterPair.h" 00028 00029 namespace storage 00030 { 00031 00032 template< class Pred, class Iter > 00033 class FilterIterator : public std::iterator<std::bidirectional_iterator_tag, 00034 typename Iter::value_type> 00035 { 00036 public: 00037 typedef typename Iter::value_type value_type; 00038 typedef typename Iter::reference reference; 00039 typedef typename Iter::pointer pointer; 00040 typedef typename Iter::difference_type difference_type; 00041 typedef typename Iter::iterator_category iterator_category; 00042 00043 FilterIterator() { } 00044 00045 template<class It> 00046 FilterIterator( const It& begin, const It& end, Pred f, bool setend=false ) : 00047 m_begin(begin), m_end(end), m_cur(begin), m_f(f) 00048 { 00049 initialize( setend ); 00050 } 00051 FilterIterator( const IterPair<Iter>& p, Pred f, bool setend=false ) : 00052 m_begin(p.begin()), m_end(p.end()), m_cur(p.begin()), m_f(f) 00053 { 00054 initialize( setend ); 00055 } 00056 00057 FilterIterator( const FilterIterator& x ) 00058 { 00059 copyMembers( x ); 00060 } 00061 00062 FilterIterator& operator=(const FilterIterator& x) 00063 { 00064 copyMembers( x ); 00065 return *this; 00066 } 00067 00068 FilterIterator& operator++() 00069 { 00070 ++m_cur; 00071 assertPred(); 00072 return *this; 00073 } 00074 FilterIterator operator++(int) 00075 { 00076 y2war( "Expensive ++ FilterIterator" ); 00077 FilterIterator tmp(*this); 00078 ++m_cur; 00079 assertPred(); 00080 return tmp; 00081 } 00082 00083 FilterIterator& operator--() 00084 { 00085 --m_cur; 00086 assertPred(false); 00087 return *this; 00088 } 00089 FilterIterator operator--(int) 00090 { 00091 y2war( "Expensive -- FilterIterator" ); 00092 FilterIterator tmp(*this); 00093 --m_cur; 00094 assertPred(false); 00095 return tmp; 00096 } 00097 00098 value_type operator*() const 00099 { 00100 return( *m_cur ); 00101 } 00102 00103 pointer operator->() const 00104 { 00105 return( &(*m_cur) ); 00106 } 00107 template <class Other> 00108 bool operator==(const Other& x) const 00109 { 00110 return( m_cur == x.cur() ); 00111 } 00112 template <class Other> 00113 bool operator!=(const Other& x) const 00114 { 00115 return( m_cur != x.cur() ); 00116 } 00117 00118 Pred pred() const {return m_f;} 00119 Iter end() const {return m_end;} 00120 Iter cur() const {return m_cur;} 00121 Iter begin() const {return m_begin;} 00122 00123 protected: 00124 Iter m_begin; 00125 Iter m_end; 00126 Iter m_cur; 00127 Pred m_f; 00128 00129 void initialize( bool setend ) 00130 { 00131 if( setend ) 00132 m_cur = m_end; 00133 else 00134 assertPred(); 00135 } 00136 00137 void copyMembers( const FilterIterator& x ) 00138 { 00139 m_begin = x.begin(); 00140 m_end = x.end(); 00141 m_cur = x.cur(); 00142 m_f = x.pred(); 00143 } 00144 00145 void assertPred( bool forward=true ) 00146 { 00147 if( forward ) 00148 while( m_cur!=m_end && !m_f(**m_cur) ) 00149 ++m_cur; 00150 else 00151 while( m_cur!=m_begin && !m_f(**m_cur) ) 00152 --m_cur; 00153 }; 00154 00155 }; 00156 00157 } 00158 00159 #endif
1.7.3