CWB
Functions | Variables
bitfields.c File Reference
#include <limits.h>
#include "globals.h"
#include "macros.h"
#include "bitfields.h"

Functions

Bitfield create_bitfield (int nr_of_elements)
 Create a new Bitfield object. More...
 
Bitfield copy_bitfield (Bitfield source)
 Copies a Bitfield. More...
 
int destroy_bitfield (Bitfield *bptr)
 Deletes a bitfield object. More...
 
int set_bit (Bitfield bitfield, int element)
 Sets a bit in a Bitfield to 1. More...
 
int clear_bit (Bitfield bitfield, int element)
 Sets a bit in a Bitfield to 0 (clears it). More...
 
int clear_all_bits (Bitfield bitfield)
 Clears an entire Bitfield (ie sets all bits to 0). More...
 
int set_all_bits (Bitfield bitfield)
 Sets an entire Bitfield (ie sets all bits to 1, all bytes to 0xff). More...
 
int get_bit (Bitfield bitfield, int element)
 Gets the value of the bit at the specified offset in the Bitfield. More...
 
int toggle_bit (Bitfield bitfield, int element)
 Switches the value of the specified bit in a Bitfield. More...
 
int bf_equal (Bitfield bf1, Bitfield bf2)
 Checks two Bitfield objects for equality of all bits. More...
 
int bf_compare (Bitfield bf1, Bitfield bf2)
 Compares two Bitfield objects. More...
 
int nr_bits_set (Bitfield bitfield)
 Gets the number of bits set to 1 in the given Bitfield. More...
 

Variables

static int BaseTypeSize = sizeof(BFBaseType)
 Size of the actual field of a Bitfield (in bytes). More...
 
static int BaseTypeBits = sizeof(BFBaseType) * CHAR_BIT
 Size of the actual field of a Bitfield (in bits). More...
 

Function Documentation

int bf_compare ( Bitfield  bf1,
Bitfield  bf2 
)

Compares two Bitfield objects.

Comparison is done from the start of the bitfield onwards. The Bitfields compared must have the same number of elements!

Returns
0 if the two are the same; 1 if bf1 is less; -1 if bf2 is less.

References BaseTypeBits, BaseTypeSize, BFBuf::bytes, BFBuf::elements, and BFBuf::field.

int bf_equal ( Bitfield  bf1,
Bitfield  bf2 
)

Checks two Bitfield objects for equality of all bits.

The Bitfields compared must have the same number of elements!

Returns
Boolean: false if the two bitfields are different, true if they are the same.

References BaseTypeBits, BaseTypeSize, BFBuf::bytes, BFBuf::elements, and BFBuf::field.

int clear_all_bits ( Bitfield  bitfield)

Clears an entire Bitfield (ie sets all bits to 0).

Returns
False if passed a NULL pointer; otherwise true.

References BFBuf::bytes, BFBuf::field, and BFBuf::nr_bits_set.

Referenced by matchfirstpattern().

int clear_bit ( Bitfield  bitfield,
int  element 
)

Sets a bit in a Bitfield to 0 (clears it).

The number of bits set is decreased by 1 iff the clearing of the specified bit has resulted in a change in the bitfield.

If the offset specified does not exist within this Bitfield, the function returns false (plus an error message is printed). Otherwise it returns true.

Parameters
bitfieldThe Bitfield object to work with.
elementThe offset of the bit to set.
Returns
Boolean (see description).

References BaseTypeBits, BFBuf::field, and BFBuf::nr_bits_set.

Bitfield copy_bitfield ( Bitfield  source)

Copies a Bitfield.

Creates a new bitfield object whihc is an exact duplicate of the source Bitfield.

Parameters
sourceThe Bitfield to copy
Returns
The duplicate Bitfield (or NULL if it was passed NULL).

References BFBuf::bytes, create_bitfield(), BFBuf::elements, BFBuf::field, and BFBuf::nr_bits_set.

Bitfield create_bitfield ( int  nr_of_elements)

Create a new Bitfield object.

Parameters
nr_of_elementsThe number of bits the field should contain.
Returns
The new Bitfield object.

References BaseTypeBits, BaseTypeSize, BFBuf::bytes, cl_malloc(), BFBuf::elements, BFBuf::field, and BFBuf::nr_bits_set.

Referenced by copy_bitfield(), do_delete_lines(), do_delete_lines_num(), do_reduce(), do_StandardQuery(), and matchfirstpattern().

int destroy_bitfield ( Bitfield bptr)

Deletes a bitfield object.

Returns
Always 1.

Referenced by do_delete_lines(), do_delete_lines_num(), do_reduce(), do_StandardQuery(), and matchfirstpattern().

int get_bit ( Bitfield  bitfield,
int  element 
)

Gets the value of the bit at the specified offset in the Bitfield.

Parameters
bitfieldThe Bitfield to work with.
elementOffset of the desired bit.
Returns
1 if the bit is set; 0 if it isn't; -1 if element is not a legal offset (ie if it's outside the bounds of the bitfield).

References BaseTypeBits, and BFBuf::field.

Referenced by delete_intervals(), matchfirstpattern(), and RangeSetop().

int nr_bits_set ( Bitfield  bitfield)

Gets the number of bits set to 1 in the given Bitfield.

References BFBuf::nr_bits_set.

Referenced by do_delete_lines_num().

int set_all_bits ( Bitfield  bitfield)

Sets an entire Bitfield (ie sets all bits to 1, all bytes to 0xff).

Returns
False if passed a NULL pointer; otherwise true.

References BFBuf::bytes, BFBuf::elements, BFBuf::field, and BFBuf::nr_bits_set.

Referenced by matchfirstpattern().

int set_bit ( Bitfield  bitfield,
int  element 
)

Sets a bit in a Bitfield to 1.

The number of bits set is increased by 1 iff the setting of the specified bit has resulted in a change in the bitfield.

If the offset specified does not exist within this Bitfield, the function returns false (plus an error message is printed). Otherwise it returns true.

Parameters
bitfieldThe Bitfield object to work with.
elementThe offset of the bit to set.
Returns
Boolean (see description).

References BaseTypeBits, BFBuf::field, and BFBuf::nr_bits_set.

Referenced by do_delete_lines(), do_delete_lines_num(), do_reduce(), do_StandardQuery(), and matchfirstpattern().

int toggle_bit ( Bitfield  bitfield,
int  element 
)

Switches the value of the specified bit in a Bitfield.

The number of set bits is either incremented or decremented, as appropriate.

If the offset specified does not exist within this Bitfield, the function returns false (plus an error message is printed). Otherwise it returns true.

Parameters
bitfieldThe Bitfield to work with.
elementOffset of the bit to flip.
Returns
Boolean (see description).

References BaseTypeBits, BFBuf::field, and BFBuf::nr_bits_set.

Referenced by copy_intervals().

Variable Documentation

int BaseTypeBits = sizeof(BFBaseType) * CHAR_BIT
static

Size of the actual field of a Bitfield (in bits).

Referenced by bf_compare(), bf_equal(), clear_bit(), create_bitfield(), get_bit(), set_bit(), and toggle_bit().

int BaseTypeSize = sizeof(BFBaseType)
static

Size of the actual field of a Bitfield (in bytes).

Referenced by bf_compare(), bf_equal(), and create_bitfield().