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

BitVector Class Reference

#include <BitVector.hpp>

List of all members.

Public Methods

 BitVector ()
 BitVector (UCHAR *init, int size=8)
 BitVector (char *binary)
virtual ~BitVector ()
void set (int bit)
void clear (int bit)
int read (int bit)
int bits ()
void bits (int sz)
void print (const char *msg="")

Private Attributes

UCHARbytes
int Bits
int numBytes


Constructor & Destructor Documentation

BitVector::BitVector  
 

Definition at line 9 of file BitVector.cpp.

00010  : numBytes( 0 ), Bits( 0 ), bytes( NULL ) { }

BitVector::BitVector UCHAR   init,
int    size = 8
 

Definition at line 13 of file BitVector.cpp.

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 }

BitVector::BitVector char *    binary
 

Definition at line 28 of file BitVector.cpp.

00028                                    {
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 }

BitVector::~BitVector   [virtual]
 

Definition at line 42 of file BitVector.cpp.

00042                       {
00043   free(bytes);
00044 }


Member Function Documentation

void BitVector::bits int    size
 

Definition at line 72 of file BitVector.cpp.

00072                              {
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 }

int BitVector::bits  
 

Definition at line 70 of file BitVector.cpp.

00070 { return Bits; }

void BitVector::clear int    bit
 

Definition at line 62 of file BitVector.cpp.

Referenced by bits().

00062                              {
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 }

void BitVector::print const char *    msg = ""
 

Definition at line 89 of file BitVector.cpp.

00089                                      {
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 }

int BitVector::read int    bit
 

Definition at line 54 of file BitVector.cpp.

Referenced by print().

00054                            {
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 }

void BitVector::set int    bit
 

Definition at line 46 of file BitVector.cpp.

Referenced by BitVector().

00046                            {
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 }


Member Data Documentation

int BitVector::Bits [private]
 

Definition at line 33 of file BitVector.hpp.

UCHAR* BitVector::bytes [private]
 

Definition at line 32 of file BitVector.hpp.

int BitVector::numBytes [private]
 

Definition at line 34 of file BitVector.hpp.


The documentation for this class was generated from the following files:
Generated on Sun Oct 14 18:48:27 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001