00001 #ifndef GRINLIZ_BITARRAY_INCLUDED
00002 #define GRINLIZ_BITARRAY_INCLUDED
00003
00004
00005 #include "gltypes.h"
00006
00007 namespace grinliz {
00008
00014 template< int WIDTH, int HEIGHT >
00015 class BitArray
00016 {
00017 public:
00018 enum {
00019 ROW_WIDTH = ( ( WIDTH+31 ) / 32 ),
00020 WORDSIZE = ROW_WIDTH * HEIGHT,
00021 };
00022
00023 BitArray() { memset( array, 0, WORDSIZE*4 ); cache = 0; }
00024
00026 U32 IsSet( int x, int y ) { return array[ y*ROW_WIDTH + (x>>5) ] & ( 0x1 << (x & 31)); }
00028 void Set( int x, int y ) { array[ y*ROW_WIDTH + (x>>5) ] |= ( 0x1 << ( x & 31 ) ); }
00030 void Clear( int x, int y ) { array[ y*ROW_WIDTH + (x>>5) ] &= (~( 0x1 << ( x & 31 ) ) ); }
00032 void ClearRect( const Rectangle2I& rect ) {
00033
00034 for( int j=rect.min.y; j<=rect.max.y; ++j )
00035 for( int i=rect.min.x; i<=rect.max.x; ++i )
00036 Clear( i, j );
00037 }
00039 void SetRect( const Rectangle2I& rect ) {
00040
00041 for( int j=rect.min.y; j<=rect.max.y; ++j )
00042 for( int i=rect.min.x; i<=rect.max.x; ++i )
00043 Set( i, j );
00044 }
00046 bool IsRectEmpty( const Rectangle2I& rect ) {
00047
00048 for( int j=rect.min.y; j<=rect.max.y; ++j )
00049 for( int i=rect.min.x; i<=rect.max.x; ++i )
00050 if ( IsSet( i, j ) )
00051 return false;
00052 return true;
00053 }
00054
00056 bool IsRectSet( const Rectangle2I& rect ) {
00057
00058 for( int j=rect.min.y; j<=rect.max.y; ++j )
00059 for( int i=rect.min.x; i<=rect.max.x; ++i )
00060 if ( !IsSet( i, j ) )
00061 return false;
00062 return true;
00063 }
00065 void ClearAll() { memset( array, 0, WORDSIZE*4 ); }
00067 void SetAll() { memset( array, 0xff, WORDSIZE*4 ); }
00068
00070 void CacheY( int y ) { cache = &array[ROW_WIDTH*y]; }
00072 U32 IsSetCache( int x ) { return cache[ x>>5 ] & ( 0x1 << (x & 31)); }
00073
00074
00075 U32 array[ WORDSIZE ];
00076 U32* cache;
00077 };
00078
00082 template< int WIDTH, int HEIGHT >
00083 class BitArrayRowIterator
00084 {
00085 public:
00086 BitArrayRowIterator( const BitArray<WIDTH, HEIGHT>& _array ) : bitArray( _array ), mask( 0 ), loc( 0 ) {}
00087
00089 void Begin( int x, int y) { loc = &bitArray.array[ y*bitArray.ROW_WIDTH + (x/32) ];
00090 U32 bit = x & 31;
00091 mask = 0x01 << bit;
00092 }
00094 void Next() { mask <<= 1;
00095 if ( !mask ) {
00096 mask = 0x01;
00097 ++loc;
00098 }
00099 }
00101 U32 IsSet() { return ( *loc ) & ( mask ); }
00103 bool WordEmpty() { return !(*loc); }
00104
00105 private:
00106 const BitArray<WIDTH, HEIGHT>& bitArray;
00107 U32 mask;
00108 const U32 *loc;
00109 };
00110 };
00111 #endif
00112