00001 #ifndef CLASSLIB_BIT_ITERATOR_H
00002 # define CLASSLIB_BIT_ITERATOR_H
00003
00004
00005
00006 # include "classlib/utils/BitOps.h"
00007 # include <iterator>
00008
00009 namespace lat {
00010
00011
00012
00013
00014
00015
00016
00038 template <class T, unsigned int BITS>
00039 class BitIterator ITERATOR_BASE(input_iterator, unsigned int, STDC::ptrdiff_t)
00040 {
00041 public:
00042 typedef std::input_iterator_tag iterator_category;
00043 typedef unsigned int value_type;
00044 typedef STDC::ptrdiff_t difference_type;
00045 typedef void reference;
00046 typedef void pointer;
00047
00048 BitIterator (void);
00049 BitIterator (const T *data, unsigned int bitpos);
00050
00051
00052
00053
00054 bool operator== (const BitIterator &x) const;
00055 bool operator!= (const BitIterator &x) const;
00056 bool operator< (const BitIterator &x) const;
00057 bool operator<= (const BitIterator &x) const;
00058 bool operator> (const BitIterator &x) const;
00059 bool operator>= (const BitIterator &x) const;
00060
00061 T operator* (void) const;
00062 T operator[] (difference_type n) const;
00063 BitIterator & operator++ (void);
00064 BitIterator operator++ (int);
00065 BitIterator & operator-- (void);
00066 BitIterator operator-- (int);
00067
00068 BitIterator & operator+= (difference_type n);
00069 difference_type operator- (const BitIterator &x) const;
00070 BitIterator & operator-= (difference_type n);
00071
00072 private:
00073 const T *m_data;
00074 unsigned int m_bit;
00075 };
00076
00077
00078
00079
00081 template <class T, unsigned int BITS>
00082 inline
00083 BitIterator<T,BITS>::BitIterator (void)
00084 : m_data (0),
00085 m_bit (0)
00086 {}
00087
00097 template <class T, unsigned int BITS>
00098 inline
00099 BitIterator<T,BITS>::BitIterator (const T *data, unsigned int bit)
00100 : m_data (data),
00101 m_bit (bit)
00102 {}
00103
00106 template <class T, unsigned int BITS>
00107 inline bool
00108 BitIterator<T,BITS>::operator== (const BitIterator &x) const
00109 { return m_data == x.m_data && m_bit == x.m_bit; }
00110
00114 template <class T, unsigned int BITS>
00115 inline bool
00116 BitIterator<T,BITS>::operator!= (const BitIterator &x) const
00117 { return !(*this == x); }
00118
00122 template <class T, unsigned int BITS>
00123 inline bool
00124 BitIterator<T,BITS>::operator< (const BitIterator &x) const
00125 { return m_bit < x.m_bit; }
00126
00130 template <class T, unsigned int BITS>
00131 inline bool
00132 BitIterator<T,BITS>::operator<= (const BitIterator &x) const
00133 { return m_bit <= x.m_bit; }
00134
00138 template <class T, unsigned int BITS>
00139 inline bool
00140 BitIterator<T,BITS>::operator> (const BitIterator &x) const
00141 { return m_bit > x.m_bit; }
00142
00146 template <class T, unsigned int BITS>
00147 inline bool
00148 BitIterator<T,BITS>::operator>= (const BitIterator &x) const
00149 { return m_bit >= x.m_bit; }
00150
00154 template <class T, unsigned int BITS>
00155 inline T
00156 BitIterator<T,BITS>::operator* (void) const
00157 { return (BitTraits<T>::Bits % BITS == 0
00158 ? BitOps<T>::extractSafe (m_data, m_bit * BITS, BITS)
00159 : BitOps<T>::extractStraddledSafe (m_data, m_bit * BITS, BITS)); }
00160
00165 template <class T, unsigned int BITS>
00166 inline T
00167 BitIterator<T,BITS>::operator[] (difference_type n) const
00168 { return *(*this + n); }
00169
00172 template <class T, unsigned int BITS>
00173 inline BitIterator<T,BITS> &
00174 BitIterator<T,BITS>::operator++ (void)
00175 { m_bit++; return *this; }
00176
00179 template <class T, unsigned int BITS>
00180 inline BitIterator<T,BITS>
00181 BitIterator<T,BITS>::operator++ (int)
00182 { BitIterator<T,BITS> old (*this); ++*this; return old; }
00183
00186 template <class T, unsigned int BITS>
00187 inline BitIterator<T,BITS> &
00188 BitIterator<T,BITS>::operator-- (void)
00189 { m_bit--; return *this; }
00190
00193 template <class T, unsigned int BITS>
00194 inline BitIterator<T,BITS>
00195 BitIterator<T,BITS>::operator-- (int)
00196 { BitIterator<T,BITS> old (*this); --*this; return old; }
00197
00200 template <class T, unsigned int BITS>
00201 inline BitIterator<T,BITS>
00202 operator+ (const BitIterator<T,BITS> &x,
00203 typename BitIterator<T,BITS>::difference_type n)
00204 { BitIterator<T,BITS> r (x); r += n; return r; }
00205
00208 template <class T, unsigned int BITS>
00209 inline BitIterator<T,BITS>
00210 operator+ (typename BitIterator<T,BITS>::difference_type n,
00211 const BitIterator<T,BITS> &x)
00212 { BitIterator<T,BITS> r (x); r += n; return r; }
00213
00216 template <class T, unsigned int BITS>
00217 inline BitIterator<T,BITS> &
00218 BitIterator<T,BITS>::operator+= (difference_type n)
00219 { return m_bit += n; }
00220
00223 template <class T, unsigned int BITS>
00224 inline BitIterator<T,BITS>
00225 operator- (const BitIterator<T,BITS> &x,
00226 typename BitIterator<T,BITS>::difference_type n)
00227 { BitIterator<T,BITS> r (x); r -= n; return r; }
00228
00232 template <class T, unsigned int BITS>
00233 inline typename BitIterator<T,BITS>::difference_type
00234 BitIterator<T,BITS>::operator- (const BitIterator<T,BITS> &x) const
00235 { return m_bit - x.m_bit; }
00236
00239 template <class T, unsigned int BITS>
00240 inline BitIterator<T,BITS> &
00241 BitIterator<T,BITS>::operator-= (difference_type n)
00242 { return m_bit -= n; }
00243
00244 }
00245 #endif // CLASSLIB_BIT_ITERATOR_H