Files
libucore/include/ucore/ucore_bitvec.h
T
2012-11-15 18:26:03 +01:00

82 lines
1.9 KiB
C
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#ifndef BITEVEC_H_
#define BITEVEC_H_
#ifdef __cplusplus
extern "C" {
#endif
struct bitvec {
//storage for the bits
unsigned long *vec;
//length of the above vec. (Not the number of bits !)
size_t vec_len;
};
/**
* Evaluates the length required for a bitvec to store nbit bits.
*/
#define BITVEC_VEC_LEN(nbits) ((nbits/(sizeof(unsigned long)*CHAR_BIT)) + (nbits % (sizeof(unsigned long)*CHAR_BIT) == 0 ? 0 : 1))
/**
* Use as :
* unsigned long v[10];
* struct bitvec v = BITVEC_STATIC_INIT(v);
*/
#define BITVEC_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 bitvec *bitvec_new(size_t nbits);
/** free a bitvec previously allocated by bitvec_new
*/
void bitvec_free(struct bitvec *v);
//Note that accessing a bit beyond the nbits originally initialized
//for the given bitvec is undedefined
/** Sets bit no. b */
void set_bit(struct bitvec *v,int b);
/** Clears bit no. b*/
void clear_bit(struct bitvec *v,int b);
/** Gets the current value (0 or 1) of bit no. b*/
int get_bit(const struct bitvec *v,int b);
/** Sets all bits to zero */
void clear_all(struct bitvec *v);
/** Sets all bits to one */
void set_all(struct bitvec *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 set_bits_from_array(struct bitvec *v,char *array,size_t array_len);
// The _s ("secure") versions does boundary checking and assert() if they
// try to access a bit out of bounds.
void set_bit_s(struct bitvec *v,int b);
void clear_bit_s(struct bitvec *v,int b);
int get_bit_s(const struct bitvec *v,int b);
#ifdef __cplusplus
}
#endif
#endif