Main Page   Class Hierarchy   Compound List   File List   Compound Members  

glbitarray.h

00001 #ifndef GLUTIL_BITARRAY_INCLUDED
00002 #define GLUTIL_BITARRAY_INCLUDED
00003 
00004 /*      A 2 dimensional bit array. Maps a bit boolean to a 2 dimensional
00005         coordinate. Very useful for efficiently storing boolean information.
00006         Done as a template. Could easily write not-T version to be dynamic.
00007 */
00008 
00009 #include "gltypes.h"
00010 
00011 template< int WIDTH, int HEIGHT >
00012 class GlBitArrayT
00013 {
00014   public:
00015         enum {
00016                 ROW_WIDTH  = ( ( WIDTH+31 ) / 32 ),     
00017                 WORDSIZE = ROW_WIDTH * HEIGHT
00018         };
00019 
00020         GlBitArrayT()                           { memset( array, 0, WORDSIZE*4 ); }     
00021 
00022         U32 IsSet( int x, int y )       { return array[ y*ROW_WIDTH + (x/32) ] & ( 0x1 << (x & 31)); }
00023         void Set( int x, int y )        { array[ y*ROW_WIDTH + (x/32) ] |=   ( 0x1 << ( x & 31 ) ); }
00024         void Clear( int x, int y )      { array[ y*ROW_WIDTH + (x/32) ] &= (~( 0x1 << ( x & 31 ) ) ); }
00025 
00026         void ClearAll()                         { memset( array, 0, WORDSIZE*4 ); }
00027         void SetAll()                           { memset( array, 0xff, WORDSIZE*4 ); }
00028 
00029         // Private. (But friend iterators are no fun.)
00030         U32 array[ WORDSIZE ];
00031 };
00032 
00033 
00034 template< int WIDTH, int HEIGHT >
00035 class GlBitArrayTRowIt
00036 {
00037   public:
00038         GlBitArrayTRowIt( const GlBitArrayT<WIDTH, HEIGHT>& _array ) : bitArray( _array ), mask( 0 ), loc( 0 )  {}
00039 
00040         void Begin( int x, int y)       {       loc = &bitArray.array[ y*bitArray.ROW_WIDTH + (x/32) ];
00041                                                                         U32 bit = x & 31;
00042                                                                         mask = 0x01 << bit;
00043                                                                 }
00044         void Next()                                     {       mask <<= 1;
00045                                                                         if ( !mask ) {
00046                                                                                 mask = 0x01;
00047                                                                                 ++loc;
00048                                                                         }
00049                                                                 }
00050         U32 IsSet()                                     {       return ( *loc ) & ( mask ); }
00051         bool WordEmpty()                        {       return !(*loc); }
00052 
00053   private:
00054         const GlBitArrayT<WIDTH, HEIGHT>& bitArray;
00055         U32 mask;
00056         const U32 *loc;
00057 };
00058 
00059 
00060 
00061 #endif

Generated on Mon Sep 15 12:01:10 2003 for Kyra by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001