Public Member Functions | Private Attributes | Friends

chomp::homology::BitField Class Reference

This class defines a bit field that is part of some larger array or that uses an allocated piece of memory. More...

#include <bitfield.h>

List of all members.

Public Member Functions

 BitField ()
 The constructor of an undefined bit field.
 ~BitField ()
 The destructor which actually does nothing.
bool defined () const
 Returns true if the bit field has already been defined, that is, it is bound with some memory piece, or its memory has been allocated.
void define (unsigned char *buf, int_t length)
 Define the bit field as a piece of a larger memory buffer.
void allocate (int_t length)
 Allocates memory for a bit field.
void free ()
 Releases the memory allocated for the bit field.
void set (int_t n)
 Sets the given bit to 1.
void clear (int_t n)
 Clears the given bit (sets it to 0).
int test (int_t n) const
 Tests the given bit. Returns 0 or 1.
void takebits (const BitField &from, int_t length)
 Copies all the bits from the given bitfield.
void clearall (int_t length)
 Clears all the bits in the entire bit field of specified length.
int find (int_t after, int_t length) const
 Finds the first bit that is set to 1, beginning at the given one.
int_t hashkey (int_t length) const
 Returns the first key for hashing.
int_t hashadd (int_t length) const
 Returns the second key for hashing.

Private Attributes

unsigned char * table
 The table of 8-bit cells which store the subsequent bits.

Friends

bool thesame (const BitField &b1, const BitField &b2, int_t length)
 Compares two bit fields of the giben length.
void int2bits (int bits, int_t length, BitField &field)
 Converts an integer into the bits of a bit field of the given length.
int bits2int (const BitField &field, int_t length)
 Converts the bits of a bit field of the given length into an integer.

Detailed Description

This class defines a bit field that is part of some larger array or that uses an allocated piece of memory.

This class may be useful for efficient management of multiple bit fields, or just one bit field. Note the very specific behavior of memory management!

Definition at line 73 of file bitfield.h.


Constructor & Destructor Documentation

chomp::homology::BitField::BitField (  )  [inline]

The constructor of an undefined bit field.

Definition at line 160 of file bitfield.h.

References table.

{
        table = NULL;
        return;
} /* BitField::BitField */

chomp::homology::BitField::~BitField (  )  [inline]

The destructor which actually does nothing.

Definition at line 178 of file bitfield.h.

{
        return;
} /* BitField::~BitField */


Member Function Documentation

void chomp::homology::BitField::allocate ( int_t  length  ) 

Allocates memory for a bit field.

The memory enough to store the given number of bits (the length of the bit field) is allocated with the 'new' operator.

void chomp::homology::BitField::clear ( int_t  n  )  [inline]

Clears the given bit (sets it to 0).

Definition at line 189 of file bitfield.h.

References table.

{
        table [n >> 3] &= static_cast<unsigned char> (~(0x01 << (n & 0x07)));
        return;
} /* BitField::clear */

void chomp::homology::BitField::clearall ( int_t  length  ) 

Clears all the bits in the entire bit field of specified length.

Note that the bit field itself does not store its length.

void chomp::homology::BitField::define ( unsigned char *  buf,
int_t  length 
)

Define the bit field as a piece of a larger memory buffer.

The memory enough to store the given number of bits (the length of the bit field) will be used.

bool chomp::homology::BitField::defined (  )  const [inline]

Returns true if the bit field has already been defined, that is, it is bound with some memory piece, or its memory has been allocated.

Definition at line 166 of file bitfield.h.

References table.

{
        return !!table;
} /* BitField::defined */

int chomp::homology::BitField::find ( int_t  after,
int_t  length 
) const

Finds the first bit that is set to 1, beginning at the given one.

Return the number of the bit, or -1 if not found. Note that the bit field itself does not store its length, so this length must be provided as an argument of this function.

void chomp::homology::BitField::free (  )  [inline]

Releases the memory allocated for the bit field.

This must be used if the memory was allocated, because the destructor does not deallocte the memory.

Definition at line 171 of file bitfield.h.

References table.

{
        delete [] table;
        table = NULL;
        return;
} /* BitField::free */

int_t chomp::homology::BitField::hashadd ( int_t  length  )  const

Returns the second key for hashing.

Note that the bit field itself does not store its length, so this length must be provided as an argument of this function.

int_t chomp::homology::BitField::hashkey ( int_t  length  )  const

Returns the first key for hashing.

Note that the bit field itself does not store its length, so this length must be provided as an argument of this function.

void chomp::homology::BitField::set ( int_t  n  )  [inline]

Sets the given bit to 1.

Definition at line 183 of file bitfield.h.

References table.

{
        table [n >> 3] |= static_cast<unsigned char> (0x01 << (n & 0x07));
        return;
} /* BitField::set */

void chomp::homology::BitField::takebits ( const BitField from,
int_t  length 
)

Copies all the bits from the given bitfield.

Assumes that both bit fields have the specified length. Note that the bit field itself does not store its length.

int chomp::homology::BitField::test ( int_t  n  )  const [inline]

Tests the given bit. Returns 0 or 1.

Definition at line 195 of file bitfield.h.

References table.

{
        return !!(table [n >> 3] & (0x01 << (n & 0x07)));
} /* BitField::test */


Friends And Related Function Documentation

int bits2int ( const BitField field,
int_t  length 
) [friend]

Converts the bits of a bit field of the given length into an integer.

The length must not exceed the size of the integer.

Definition at line 214 of file bitfield.h.

{
        const unsigned char *tab = field. table;
        if (!tab)
                throw "Trying to set values to an undefined bitfield.";
        int n = 0;
        int shiftvalue = 0;
        while (length >= 8)
        {
                n |= (*(tab ++)) << shiftvalue;
                length -= 8;
                shiftvalue += 8;
        }
        const int bitmasks [] = {0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF};
        if (length > 0)
                n |= ((*(tab ++)) & bitmasks [length]) << shiftvalue;
        return n;
} /* bits2int */

void int2bits ( int  bits,
int_t  length,
BitField field 
) [friend]

Converts an integer into the bits of a bit field of the given length.

The length must not exceed the size of the integer.

Definition at line 200 of file bitfield.h.

{
        unsigned char *tab = field. table;
        if (!tab)
                throw "Trying to set values to an undefined bitfield.";
        while (length >= 0)
        {
                *(tab ++) = static_cast<unsigned char> (bits & 0xFF);
                bits >>= 8;
                length -= 8;
        }
        return;
} /* int2bits */

bool thesame ( const BitField b1,
const BitField b2,
int_t  length 
) [friend]

Compares two bit fields of the giben length.

Returns true if they are the same, false otherwise.


Member Data Documentation

unsigned char* chomp::homology::BitField::table [private]

The table of 8-bit cells which store the subsequent bits.

It is either an address of some allocated memory, or an address of portion of some other memory, for example, allocated collectively for a large number of bit fields.

Definition at line 154 of file bitfield.h.

Referenced by BitField(), clear(), defined(), free(), set(), and test().


The documentation for this class was generated from the following file: