// ============================================================ // // digitIterator.h // // Copyright 2002, Dennis Meilicke and Rene Peralta // // ============================================================ // // Description: // // This is the beginnings of a digit-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_digitIterator__ #define __ln3_digitIterator__ #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 digitIterator : public refCount< Base > { private: mutable bool valid; mutable size_t index; public: digitIterator( ) : refCount< Base >( ), index(0), valid(false) { } digitIterator( refCount< Base >& x ) : refCount< Base >( x ), index(0) { valid = ( this->GetData( )->size > 0 ); } digit_t operator*( ) const { if( valid ) return this->GetData( )->digit[index]; return 0; } digitIterator operator++( ) const // prefix { if( valid ) { index++; if( index >= this->GetData( )->size ) valid = false; } return *this; } digitIterator operator++( int ) const // postfix { digitIterator retVal( *this ); if( valid ) { index++; if( index >= this->GetData( )->size ) valid = false; } return retVal; } digitIterator operator--( ) const // prefix { if( valid ) { if( index == 0 ) valid = false; else index--; } return *this; } digitIterator operator--( int ) const // postfix { digitIterator retVal( *this ); if( valid ) { if( index == 0 ) valid = false; else index--; } return retVal; } const digitIterator& begin( void ) const { index = 0; valid = ( this->GetData( )->size != 0 ); return *this; } const digitIterator& end( void ) const { if( this->GetData( )->size == 0 ) valid = false; else { valid = true; index = this->GetData( )->size - 1; } return *this; } // // Test the state of the iterator. // operator void*( ) const { return ( (valid)? (void *)1 : (void *)0 ); } bool operator!( ) const { return (!valid); } }; #endif