// ============================================================ // // bitIterator.h // // Copyright 2002, Dennis Meilicke and Rene Peralta // // ============================================================ // // Description: // // This is the beginnings of a bit-iterator class for ln3. // It should be considered experimental. In fact, I'd be // very surprised if it worked at all. // // To do: // // This should be re-thought. It should be closer to the STL // implementation. In particular, it should be implemented as // a random access iterator. // // ============================================================ #ifndef __ln3_bitIterator__ #define __ln3_bitIterator__ #include #include // // This should throw an exception if the iterator // is not valid. I think? Check STL iterators... // // // The calls to GetData in the method implementations // break the reference. Need to find another way... // class bitIterator : public refCount< Base > { private: mutable bool valid; mutable bool initialized; mutable digit_t mask; mutable size_t index; mutable digit_t lastBit; public: bitIterator( ) : refCount< Base >( ), initialized(false) { } bitIterator( refCount< Base >& x ) : refCount< Base >( x ), initialized(false) { } short operator*( ) const { if( !initialized ) this->begin( ); if( valid ) return ( this->GetData( )->digit[index] & mask ) ? 1 : 0; return 0; } bitIterator operator++( ) const // prefix { if( !initialized ) this->begin( ); if( valid ) { mask <<= 1; if( mask == 0 ) { index++; if( index >= this->GetData( )->size ) valid = false; } } return *this; } bitIterator operator++( int ) const // postfix { if( !initialized ) this->begin( ); bitIterator retVal( *this ); if( valid ) { mask <<= 1; if( mask == 0 ) { index++; if( index >= this->GetData( )->size ) valid = false; } else { if( index == ( this->GetData( )->size - 1 ) ) if( mask > lastBit ) valid = false; } } return retVal; } bitIterator operator--( ) const // prefix { if( !initialized ) this->end( ); if( valid ) { mask >>= 1; if( mask == 0 ) if( index == 0 ) valid = false; else index--; } return *this; } bitIterator operator--( int ) const // postfix { if( !initialized ) this->end( ); bitIterator retVal( *this ); if( valid ) { mask >>= 1; if( mask == 0 ) if( index == 0 ) valid = false; else index--; } return retVal; } const bitIterator& begin( void ) const { initialized = true; index = 0; lastBit = mask = 1; size_t msb = NumBits( this->GetData( )->digit[ index ] ); if( msb > 1 ) lastBit <<= ( msb - 1 ); valid = ( this->GetData( )->size != 0 ); return *this; } const bitIterator& end( void ) const { initialized = true; if( this->GetData( )->size == 0 ) valid = false; else { valid = true; index = this->GetData( )->size - 1; size_t msb = NumBits( this->GetData( )->digit[ index ] ); mask = 1; if( msb > 1 ) mask <<= ( msb - 1 ); lastBit = mask; } return *this; } // // Test the state of the iterator. // operator void*( ) const { if( ! initialized ) this->begin( ); return ( (valid)? (void *)1 : (void *)0 ); } bool operator!( ) const { if( ! initialized ) this->begin( ); return (!valid); } }; #endif