The Original CHomP Software
Public Member Functions | Private Attributes | Friends | List of all members
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>

Public Member Functions

 BitField ()
 The constructor of an undefined bit field. More...
 
 ~BitField ()
 The destructor which actually does nothing. More...
 
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. More...
 
void define (unsigned char *buf, int_t length)
 Define the bit field as a piece of a larger memory buffer. More...
 
void allocate (int_t length)
 Allocates memory for a bit field. More...
 
void free ()
 Releases the memory allocated for the bit field. More...
 
void set (int_t n)
 Sets the given bit to 1. More...
 
void clear (int_t n)
 Clears the given bit (sets it to 0). More...
 
int test (int_t n) const
 Tests the given bit. Returns 0 or 1. More...
 
void takebits (const BitField &from, int_t length)
 Copies all the bits from the given bitfield. More...
 
void clearall (int_t length)
 Clears all the bits in the entire bit field of specified length. More...
 
int find (int_t first, int_t length) const
 Finds the first bit that is set to 1, beginning at the given one. More...
 
int_t hashkey (int_t length) const
 Returns the first key for hashing. More...
 
int_t hashadd (int_t length) const
 Returns the second key for hashing. More...
 

Private Attributes

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

Friends

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

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 72 of file bitfield.h.

Constructor & Destructor Documentation

◆ BitField()

chomp::homology::BitField::BitField ( )
inline

The constructor of an undefined bit field.

Definition at line 161 of file bitfield.h.

162{
163 table = NULL;
164 return;
165} /* BitField::BitField */
unsigned char * table
The table of 8-bit cells which store the subsequent bits.
Definition: bitfield.h:155

References table.

◆ ~BitField()

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

The destructor which actually does nothing.

Definition at line 179 of file bitfield.h.

180{
181 return;
182} /* BitField::~BitField */

Member Function Documentation

◆ allocate()

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. The allocated memory is filled with zeros, which isequivalent to calling the clearall procedure.

◆ clear()

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

Clears the given bit (sets it to 0).

Definition at line 190 of file bitfield.h.

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

References table.

◆ clearall()

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.

◆ define()

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.

◆ defined()

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 167 of file bitfield.h.

168{
169 return !!table;
170} /* BitField::defined */

References table.

◆ find()

int chomp::homology::BitField::find ( int_t  first,
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.

◆ free()

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 172 of file bitfield.h.

173{
174 delete [] table;
175 table = NULL;
176 return;
177} /* BitField::free */

References table.

◆ hashadd()

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.

◆ hashkey()

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.

◆ set()

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

Sets the given bit to 1.

Definition at line 184 of file bitfield.h.

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

References table.

◆ takebits()

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.

◆ test()

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

Tests the given bit. Returns 0 or 1.

Definition at line 196 of file bitfield.h.

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

References table.

Friends And Related Function Documentation

◆ bits2int

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 215 of file bitfield.h.

216{
217 const unsigned char *tab = field. table;
218 if (!tab)
219 throw "Trying to set values to an undefined bitfield.";
220 int n = 0;
221 int shiftvalue = 0;
222 while (length >= 8)
223 {
224 n |= (*(tab ++)) << shiftvalue;
225 length -= 8;
226 shiftvalue += 8;
227 }
228 const int bitmasks [] = {0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF};
229 if (length > 0)
230 n |= ((*(tab ++)) & bitmasks [length]) << shiftvalue;
231 return n;
232} /* bits2int */

◆ int2bits

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 201 of file bitfield.h.

202{
203 unsigned char *tab = field. table;
204 if (!tab)
205 throw "Trying to set values to an undefined bitfield.";
206 while (length >= 0)
207 {
208 *(tab ++) = static_cast<unsigned char> (bits & 0xFF);
209 bits >>= 8;
210 length -= 8;
211 }
212 return;
213} /* int2bits */

◆ thesame

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

◆ table

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 155 of file bitfield.h.

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


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