87 lines
2.2 KiB
C
87 lines
2.2 KiB
C
#ifndef UC_BITEVEC_H_
|
|
#define UC_BITEVEC_H_
|
|
|
|
#include <limits.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
/** Backing type of the bit vector*/
|
|
typedef unsigned long uc_bv_integer;
|
|
|
|
struct UCBitVec {
|
|
//storage for the bits
|
|
uc_bv_integer *vec;
|
|
//number of elements int the above vec.
|
|
size_t vec_len;
|
|
};
|
|
|
|
/**
|
|
* Evaluates the number of uc_bv_integer elements required for a bitvec to store nbit bits.
|
|
*/
|
|
#define UC_BV_LEN(nbits) ((nbits/(sizeof(uc_bv_integer)*CHAR_BIT)) + (nbits % (sizeof(uc_bv_integer)*CHAR_BIT) == 0 ? 0 : 1))
|
|
|
|
/** Initialize a bitvec with predefined backing array.
|
|
* The backing array element type must be uc_bv_integer
|
|
* Use as
|
|
* @code
|
|
* uc_bv_integer v[10];
|
|
* struct UCBitVec v = UC_BV_STATIC_INIT(v);
|
|
* @endcode
|
|
*/
|
|
#define UC_BV_STATIC_INIT(vec_data)\
|
|
{\
|
|
vec_data,\
|
|
sizeof vec_data/sizeof vec_data[0]}
|
|
|
|
|
|
/**
|
|
* Returns a newly malloced bitvec, capable of storing at least nbits bits
|
|
* All bits are initially zero.
|
|
*
|
|
*/
|
|
struct UCBitVec *uc_bv_new(size_t nbits);
|
|
|
|
/** free a bitvec previously allocated by bitvec_new
|
|
*/
|
|
void uc_bv_free(struct UCBitVec *v);
|
|
|
|
//Note that accessing a bit beyond the nbits originally initialized
|
|
//for the given bitvec is undedefined
|
|
|
|
/** Sets bit number b */
|
|
void uc_bv_set_bit(struct UCBitVec *v,int b);
|
|
|
|
/** Clears bit number b*/
|
|
void uc_bv_clr_bit(struct UCBitVec *v,int b);
|
|
|
|
/** Gets the current value (0 or 1) of bit number @b*/
|
|
int uc_bv_get_bit(const struct UCBitVec *v,int b);
|
|
|
|
/** Sets all bits to zero */
|
|
void uc_bv_clr_all(struct UCBitVec *v);
|
|
|
|
/** Sets all bits to one */
|
|
void uc_bv_set_all(struct UCBitVec *v);
|
|
|
|
/** Initialize bits from a array of ints, each array element maps to one bit.
|
|
* The bits are initialized from the int array so zero maps to zero and non-zero maps to one.
|
|
* e..g to set the 5 first bits, to 01110:
|
|
* int a[] = {0,1,1,1,0};
|
|
* set_bits_from_array(v,a,5);
|
|
* */
|
|
void uc_bv_set_bits_from_array(struct UCBitVec *v,const char *array,size_t array_len);
|
|
|
|
/** Find the index of the first zero bit in the bitvec.
|
|
* @param v The bitvec to search
|
|
* @return The index of the first zero bit, or -1 if all bits are set
|
|
*/
|
|
int uc_bv_find_first_zero(const struct UCBitVec *v);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif
|