Generalize the backing type of bitvec

This commit is contained in:
Nils O. Selåsdal
2013-10-28 23:21:26 +01:00
parent 215035190e
commit eceda7be70
3 changed files with 20 additions and 16 deletions
+8 -3
View File
@@ -1,13 +1,18 @@
#ifndef UC_BITEVEC_H_ #ifndef UC_BITEVEC_H_
#define UC_BITEVEC_H_ #define UC_BITEVEC_H_
#include <limits.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/** Backing type of the bit vector*/
typedef unsigned long uc_bv_integer;
struct UCBitVec { struct UCBitVec {
//storage for the bits //storage for the bits
unsigned long *vec; uc_bv_integer *vec;
//length in bytes of the above vec. //length in bytes of the above vec.
size_t vec_len; size_t vec_len;
}; };
@@ -15,11 +20,11 @@ struct UCBitVec {
/** /**
* Evaluates the length required for a bitvec to store nbit bits. * Evaluates the length required for a bitvec to store nbit bits.
*/ */
#define UC_BV_LEN(nbits) ((nbits/(sizeof(unsigned long)*CHAR_BIT)) + (nbits % (sizeof(unsigned long)*CHAR_BIT) == 0 ? 0 : 1)) #define UC_BV_LEN(nbits) ((nbits/(sizeof(uc_bv_integer)*CHAR_BIT)) + (nbits % (sizeof(uc_bv_integer)*CHAR_BIT) == 0 ? 0 : 1))
/** /**
* Use as : * Use as :
* unsigned long v[10]; * uc_bv_integer v[10];
* struct UCBitVec v = BITVEC_STATIC_INIT(v); * struct UCBitVec v = BITVEC_STATIC_INIT(v);
*/ */
#define UC_BV_STATIC_INIT(vec_data)\ #define UC_BV_STATIC_INIT(vec_data)\
+5 -6
View File
@@ -1,7 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <limits.h>
#include "ucore/bitvec.h" #include "ucore/bitvec.h"
#ifdef __GNUC__ #ifdef __GNUC__
@@ -14,12 +13,12 @@
static inline int bit_index(int b) static inline int bit_index(int b)
{ {
return b / (sizeof(unsigned long) * CHAR_BIT); return b / (sizeof(uc_bv_integer) * CHAR_BIT);
} }
static inline int bit_offset(int b) static inline int bit_offset(int b)
{ {
return b % (sizeof(unsigned long) * CHAR_BIT); return b % (sizeof(uc_bv_integer) * CHAR_BIT);
} }
struct UCBitVec *uc_bv_new(size_t nbits) struct UCBitVec *uc_bv_new(size_t nbits)
@@ -29,7 +28,7 @@ struct UCBitVec *uc_bv_new(size_t nbits)
if (v == NULL) if (v == NULL)
return NULL; return NULL;
v->vec_len = UC_BV_LEN(nbits); v->vec_len = UC_BV_LEN(nbits);
v->vec = malloc(v->vec_len * sizeof(unsigned long)); v->vec = malloc(v->vec_len * sizeof(uc_bv_integer));
if (v->vec == NULL) { if (v->vec == NULL) {
free(v); free(v);
return NULL; return NULL;
@@ -81,11 +80,11 @@ void uc_bv_set_bits_from_array(struct UCBitVec *v,char *array,size_t array_len)
void uc_bv_clr_all(struct UCBitVec *v) void uc_bv_clr_all(struct UCBitVec *v)
{ {
memset(v->vec,0, v->vec_len * sizeof(unsigned long)); memset(v->vec,0, v->vec_len * sizeof(uc_bv_integer));
} }
void uc_bv_set_all(struct UCBitVec *v) void uc_bv_set_all(struct UCBitVec *v)
{ {
memset(v->vec,0xff,v->vec_len * sizeof(unsigned long)); memset(v->vec,0xff,v->vec_len * sizeof(uc_bv_integer));
} }
+7 -7
View File
@@ -3,7 +3,7 @@
START_TEST (test_bitvec_1) START_TEST (test_bitvec_1)
unsigned long s[3] = {0}; uc_bv_integer s[3] = {0};
struct UCBitVec v = UC_BV_STATIC_INIT(s); struct UCBitVec v = UC_BV_STATIC_INIT(s);
size_t i; size_t i;
@@ -12,7 +12,7 @@ START_TEST (test_bitvec_1)
END_TEST END_TEST
START_TEST (test_bitvec_2) START_TEST (test_bitvec_2)
unsigned long s[3] = {0}; uc_bv_integer s[3] = {0};
struct UCBitVec v = UC_BV_STATIC_INIT(s); struct UCBitVec v = UC_BV_STATIC_INIT(s);
size_t i; size_t i;
@@ -24,17 +24,17 @@ START_TEST (test_bitvec_2)
END_TEST END_TEST
START_TEST (test_bitvec_clearbit) START_TEST (test_bitvec_clearbit)
unsigned long s[3] = {-1, -1, -1}; uc_bv_integer s[3] = {-1, -1, -1};
struct UCBitVec v = UC_BV_STATIC_INIT(s); struct UCBitVec v = UC_BV_STATIC_INIT(s);
size_t i; size_t i;
for (i = 0; i < sizeof(unsigned long) * 8; i++) for (i = 0; i < sizeof(uc_bv_integer) * 8; i++)
fail_if(uc_bv_get_bit(&v, i) != 1 , "bit %zu is not 1", i); fail_if(uc_bv_get_bit(&v, i) != 1 , "bit %zu is not 1", i);
for (i = 0; i < sizeof(unsigned long) * 8; i++) for (i = 0; i < sizeof(uc_bv_integer) * 8; i++)
uc_bv_clr_bit(&v, i); uc_bv_clr_bit(&v, i);
for (i = 0; i < sizeof(unsigned long) * 8; i++) for (i = 0; i < sizeof(uc_bv_integer) * 8; i++)
fail_if(uc_bv_get_bit(&v, i) != 0 , "bit %zu is not 0", i); fail_if(uc_bv_get_bit(&v, i) != 0 , "bit %zu is not 0", i);
END_TEST END_TEST
@@ -63,7 +63,7 @@ START_TEST (test_bitvec_setall_clearall)
uc_bv_clr_all(v); uc_bv_clr_all(v);
for (i = 0; i < sizeof(unsigned long) * 8; i++) for (i = 0; i < sizeof(uc_bv_integer) * 8; i++)
fail_if(uc_bv_get_bit(v, i) != 0 , "bit %zu is not 0", i); fail_if(uc_bv_get_bit(v, i) != 0 , "bit %zu is not 0", i);
uc_bv_free(v); uc_bv_free(v);