Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Beta/Math/BitVector.cpp

Go to the documentation of this file.
00001 #ifndef __J2K__BitVector_CPP__
00002 #define __J2K__BitVector_CPP__
00003 
00004 #include <j2k/Fred/Math/BitVector.hpp>
00005 
00006 // A byte with the high bit set:
00007 const unsigned char highbit = 1 << (CHAR_BIT - 1);
00008 
00009 BitVector::BitVector()
00010  : numBytes( 0 ), Bits( 0 ), bytes( NULL ) { }
00011 
00012 // Notice default args are not duplicated:
00013 BitVector::BitVector(UCHAR* init, int size)
00014  : numBytes( size ), Bits( numBytes * CHAR_BIT )
00015 {
00016   bytes = (UCHAR*)calloc(numBytes, 1);
00017   assert( bytes );
00018 
00019   if ( init == NULL ) return; // Default to all 0
00020 
00021   // Translate from bytes into bit sequence:
00022   for( int index = 0; index < numBytes; index++)
00023     for( int offset = 0; offset < CHAR_BIT; offset++)
00024       if ( init[index] & (highbit >> offset) )
00025          set( index * CHAR_BIT + offset );
00026 }
00027 
00028 BitVector::BitVector( char* binary ) {
00029   Bits = strlen( binary );
00030   numBytes = Bits / CHAR_BIT;
00031 
00032   // If there's a remainder, add 1 byte:
00033   if ( Bits % CHAR_BIT ) numBytes++;
00034 
00035   bytes = (unsigned char*)calloc(numBytes, 1);
00036   assert(bytes);
00037 
00038   for(int i = 0; i < Bits; i++)
00039     if (binary[i] == '1') set(i);
00040 }
00041 
00042 BitVector::~BitVector() {
00043   free(bytes);
00044 }
00045 
00046 void BitVector::set(int bit) {
00047   assert(bit >= 0 && bit < Bits);
00048   int index  = bit / CHAR_BIT;
00049   int offset = bit % CHAR_BIT;
00050   UCHAR mask = (1 << offset);
00051   bytes[index] |= mask;
00052 }
00053 
00054 int BitVector::read(int bit) {
00055   assert(bit >= 0 && bit < Bits);
00056   int index  = bit / CHAR_BIT;
00057   int offset = bit % CHAR_BIT;
00058   UCHAR mask = (1 << offset);
00059   return bytes[index] & mask;
00060 }
00061 
00062 void BitVector::clear(int bit) {
00063   assert(bit >= 0 && bit < Bits);
00064   int index  = bit / CHAR_BIT;
00065   int offset = bit % CHAR_BIT;
00066   UCHAR mask = ~(1 << offset);
00067   bytes[index] &= mask;
00068 }
00069 
00070 int BitVector::bits() { return Bits; }
00071 
00072 void BitVector::bits(int size) {
00073   int oldsize = Bits;
00074   Bits = size;
00075   numBytes =  Bits / CHAR_BIT;
00076 
00077   // If there's a remainder, add 1 byte:
00078   if ( Bits % CHAR_BIT ) numBytes++;
00079 
00080   void* v = realloc(bytes, numBytes);
00081   assert(v);
00082 
00083   bytes = (unsigned char*)v;
00084 
00085   for(int i = oldsize; i < Bits; i++)
00086     clear(i); // Erase additional bits
00087 }
00088 
00089 void BitVector::print(const char* msg) {
00090   puts(msg);
00091 
00092   for(int i = 0; i < Bits; i++){
00093     if ( read(i) ) {
00094       putchar('1');
00095     } else {
00096       putchar('0');
00097     }
00098 
00099     // Format into byte blocks:
00100     if((i + 1) % CHAR_BIT == 0) putchar(' ');
00101   }
00102   putchar('\n');
00103 }
00104 
00105 
00106 #endif

Generated on Sun Oct 14 18:46:12 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001