Skip to content

Commit

Permalink
issue #24 Add bitset data structure - based on MIR implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dibyendumajumdar committed Jun 22, 2020
1 parent abdaabd commit 52e27ec
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 178 deletions.
58 changes: 29 additions & 29 deletions src/bitset.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@
#define BITMAP_ASSERT(EXPR, OP) ((void) (EXPR))

#else
static inline void mir_bitmap_assert_fail (const char *op) {
fprintf (stderr, "wrong %s for a bitmap", op);
static inline void mir_bitset_assert_fail (const char *op) {
fprintf (stderr, "wrong %s for a bitset", op);
assert (0);
}

#define BITMAP_ASSERT(EXPR, OP) (void) ((EXPR) ? 0 : (mir_bitmap_assert_fail (#OP), 0))
#define BITMAP_ASSERT(EXPR, OP) (void) ((EXPR) ? 0 : (mir_bitset_assert_fail (#OP), 0))

#endif

#define BITMAP_WORD_BITS 64

struct bitset_t * raviX_bitmap_create2(size_t init_bits_num) {
struct bitset_t * raviX_bitset_create2(size_t init_bits_num) {
struct bitset_t * bm = calloc(1, sizeof(struct bitset_t));
bm->els_num = 0;
bm->size = (init_bits_num + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS;
bm->varr = calloc(bm->size, sizeof(bitset_el_t));
return bm;
}

void raviX_bitmap_destroy(struct bitset_t * bm)
void raviX_bitset_destroy(struct bitset_t * bm)
{
free(bm->varr);
free(bm);
}

static void bitmap_expand (struct bitset_t * bm, size_t nb) {
static void bitset_expand (struct bitset_t * bm, size_t nb) {
size_t new_len = (nb + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS;
if (new_len > bm->els_num) {
if (new_len > bm->size) {
Expand All @@ -60,7 +60,7 @@ static void bitmap_expand (struct bitset_t * bm, size_t nb) {
}
}

int raviX_bitmap_bit_p(const struct bitset_t * bm, size_t nb) {
int raviX_bitset_bit_p(const struct bitset_t * bm, size_t nb) {
size_t nw, sh, len = bm->els_num;
bitset_el_t *addr = bm->varr;

Expand All @@ -70,12 +70,12 @@ int raviX_bitmap_bit_p(const struct bitset_t * bm, size_t nb) {
return (addr[nw] >> sh) & 1;
}

int raviX_bitmap_set_bit_p(struct bitset_t * bm, size_t nb) {
int raviX_bitset_set_bit_p(struct bitset_t * bm, size_t nb) {
size_t nw, sh;
bitset_el_t *addr;
int res;

bitmap_expand (bm, nb + 1);
bitset_expand (bm, nb + 1);
addr = bm->varr;
nw = nb / BITMAP_WORD_BITS;
sh = nb % BITMAP_WORD_BITS;
Expand All @@ -85,7 +85,7 @@ int raviX_bitmap_set_bit_p(struct bitset_t * bm, size_t nb) {
return res;
}

int raviX_bitmap_clear_bit_p(struct bitset_t * bm, size_t nb) {
int raviX_bitset_clear_bit_p(struct bitset_t * bm, size_t nb) {
size_t nw, sh, len = bm->els_num;
bitset_el_t *addr = bm->varr;
int res;
Expand All @@ -98,12 +98,12 @@ int raviX_bitmap_clear_bit_p(struct bitset_t * bm, size_t nb) {
return res;
}

int raviX_bitmap_set_or_clear_bit_range_p(struct bitset_t * bm, size_t nb, size_t len, int set_p) {
int raviX_bitset_set_or_clear_bit_range_p(struct bitset_t * bm, size_t nb, size_t len, int set_p) {
size_t nw, lsh, rsh, range_len;
bitset_el_t mask, *addr;
int res = 0;

bitmap_expand (bm, nb + len);
bitset_expand (bm, nb + len);
addr = bm->varr;
while (len > 0) {
nw = nb / BITMAP_WORD_BITS;
Expand All @@ -124,20 +124,20 @@ int raviX_bitmap_set_or_clear_bit_range_p(struct bitset_t * bm, size_t nb, size_
return res;
}

void raviX_bitmap_copy(struct bitset_t * dst, const struct bitset_t * src) {
void raviX_bitset_copy(struct bitset_t * dst, const struct bitset_t * src) {

size_t dst_len = dst->els_num;
size_t src_len = src->els_num;

if (dst_len >= src_len)
dst->els_num = src_len;
else
bitmap_expand (dst, src_len * BITMAP_WORD_BITS);
bitset_expand (dst, src_len * BITMAP_WORD_BITS);
memcpy (dst->varr, src->varr,
src_len * sizeof (bitset_el_t));
}

int raviX_bitmap_equal_p(const struct bitset_t * bm1, const struct bitset_t * bm2) {
int raviX_bitset_equal_p(const struct bitset_t * bm1, const struct bitset_t * bm2) {
const struct bitset_t * temp_bm;
size_t i, temp_len, bm1_len = bm1->els_num;
size_t bm2_len = bm2->els_num;
Expand All @@ -159,7 +159,7 @@ int raviX_bitmap_equal_p(const struct bitset_t * bm1, const struct bitset_t * bm
return true;
}

int raviX_bitmap_intersect_p(const struct bitset_t * bm1, const struct bitset_t * bm2) {
int raviX_bitset_intersect_p(const struct bitset_t * bm1, const struct bitset_t * bm2) {
size_t i, min_len, bm1_len = bm1->els_num;
size_t bm2_len = bm2->els_num;
bitset_el_t *addr1 = bm1->varr;
Expand All @@ -171,7 +171,7 @@ int raviX_bitmap_intersect_p(const struct bitset_t * bm1, const struct bitset_t
return false;
}

int raviX_bitmap_empty_p(const struct bitset_t * bm) {
int raviX_bitset_empty_p(const struct bitset_t * bm) {
size_t i, len = bm->els_num;
bitset_el_t *addr = bm->varr;

Expand All @@ -180,17 +180,17 @@ int raviX_bitmap_empty_p(const struct bitset_t * bm) {
return true;
}

static bitset_el_t bitmap_el_max2 (bitset_el_t el1, bitset_el_t el2) {
static bitset_el_t bitset_el_max2 (bitset_el_t el1, bitset_el_t el2) {
return el1 < el2 ? el2 : el1;
}

static bitset_el_t bitmap_el_max3 (bitset_el_t el1, bitset_el_t el2, bitset_el_t el3) {
static bitset_el_t bitset_el_max3 (bitset_el_t el1, bitset_el_t el2, bitset_el_t el3) {
if (el1 <= el2) return el2 < el3 ? el3 : el2;
return el1 < el3 ? el3 : el1;
}

/* Return the number of bits set in BM. */
size_t raviX_bitmap_bit_count(const struct bitset_t * bm) {
size_t raviX_bitset_bit_count(const struct bitset_t * bm) {
size_t i, len = bm->els_num;
bitset_el_t el, *addr = bm->varr;
size_t count = 0;
Expand All @@ -204,16 +204,16 @@ size_t raviX_bitmap_bit_count(const struct bitset_t * bm) {
return count;
}

int raviX_bitmap_op2(struct bitset_t * dst, const struct bitset_t * src1, const struct bitset_t * src2,
int raviX_bitset_op2(struct bitset_t * dst, const struct bitset_t * src1, const struct bitset_t * src2,
bitset_el_t (*op) (bitset_el_t, bitset_el_t)) {
size_t i, len, bound, src1_len, src2_len;
bitset_el_t old, *dst_addr, *src1_addr, *src2_addr;
int change_p = false;

src1_len = src1->els_num;
src2_len = src2->els_num;
len = bitmap_el_max2 (src1_len, src2_len);
bitmap_expand (dst, len * BITMAP_WORD_BITS);
len = bitset_el_max2 (src1_len, src2_len);
bitset_expand (dst, len * BITMAP_WORD_BITS);
dst_addr = dst->varr;
src1_addr = src1->varr;
src2_addr = src2->varr;
Expand All @@ -228,7 +228,7 @@ int raviX_bitmap_op2(struct bitset_t * dst, const struct bitset_t * src1, const
return change_p;
}

int raviX_bitmap_op3(struct bitset_t * dst, const struct bitset_t * src1, const struct bitset_t * src2,
int raviX_bitset_op3(struct bitset_t * dst, const struct bitset_t * src1, const struct bitset_t * src2,
const struct bitset_t * src3, bitset_el_t (*op) (bitset_el_t, bitset_el_t, bitset_el_t)) {
size_t i, len, bound, src1_len, src2_len, src3_len;
bitset_el_t old, *dst_addr, *src1_addr, *src2_addr, *src3_addr;
Expand All @@ -237,8 +237,8 @@ int raviX_bitmap_op3(struct bitset_t * dst, const struct bitset_t * src1, const
src1_len = src1->els_num;
src2_len = src2->els_num;
src3_len = src3->els_num;
len = bitmap_el_max3 (src1_len, src2_len, src3_len);
bitmap_expand (dst, len * BITMAP_WORD_BITS);
len = bitset_el_max3 (src1_len, src2_len, src3_len);
bitset_expand (dst, len * BITMAP_WORD_BITS);
dst_addr = dst->varr;
src1_addr = src1->varr;
src2_addr = src2->varr;
Expand All @@ -255,10 +255,10 @@ int raviX_bitmap_op3(struct bitset_t * dst, const struct bitset_t * src1, const
return change_p;
}

int raviX_bitmap_iterator_next(bitmap_iterator_t *iter, size_t *nbit) {
int raviX_bitset_iterator_next(bitset_iterator_t *iter, size_t *nbit) {
const size_t el_bits_num = sizeof (bitset_el_t) * CHAR_BIT;
size_t curr_nel = iter->nbit / el_bits_num, len = iter->bitmap->els_num;
bitset_el_t el, *addr = iter->bitmap->varr;
size_t curr_nel = iter->nbit / el_bits_num, len = iter->bitset->els_num;
bitset_el_t el, *addr = iter->bitset->varr;

for (; curr_nel < len; curr_nel++, iter->nbit = curr_nel * el_bits_num)
if ((el = addr[curr_nel]) != 0)
Expand Down
91 changes: 49 additions & 42 deletions src/bitset.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/* This file is a part of MIR project.
Copyright (C) 2018-2020 Vladimir Makarov <[email protected]>.
*/
/*
* Adapted for Ravi Compiler project
*/

#ifndef ravicomp_BITSET_H
#define ravicomp_BITSET_H

Expand All @@ -13,76 +20,76 @@ struct bitset_t {
bitset_el_t *varr;
};

extern struct bitset_t * raviX_bitmap_create2(size_t init_bits_num);
static inline struct bitset_t * raviX_bitmap_create(void)
extern struct bitset_t * raviX_bitset_create2(size_t init_bits_num);
static inline struct bitset_t * raviX_bitset_create(void)
{
return raviX_bitmap_create2(0);
return raviX_bitset_create2(0);
}
extern void raviX_bitmap_destroy(struct bitset_t * bm);
static inline void raviX_bitmap_clear(struct bitset_t * bm)
extern void raviX_bitset_destroy(struct bitset_t * bm);
static inline void raviX_bitset_clear(struct bitset_t * bm)
{
bm->els_num = 0;
}
extern int raviX_bitmap_bit_p(const struct bitset_t * bm, size_t nb);
extern int raviX_bitmap_set_bit_p(struct bitset_t * bm, size_t nb);
extern int raviX_bitmap_clear_bit_p(struct bitset_t * bm, size_t nb);
extern int raviX_bitmap_set_or_clear_bit_range_p(struct bitset_t * bm, size_t nb, size_t len, int set_p);
static inline int raviX_bitmap_set_bit_range_p(struct bitset_t * bm, size_t nb, size_t len) {
return raviX_bitmap_set_or_clear_bit_range_p(bm, nb, len, true);
extern int raviX_bitset_bit_p(const struct bitset_t * bm, size_t nb);
extern int raviX_bitset_set_bit_p(struct bitset_t * bm, size_t nb);
extern int raviX_bitset_clear_bit_p(struct bitset_t * bm, size_t nb);
extern int raviX_bitset_set_or_clear_bit_range_p(struct bitset_t * bm, size_t nb, size_t len, int set_p);
static inline int raviX_bitset_set_bit_range_p(struct bitset_t * bm, size_t nb, size_t len) {
return raviX_bitset_set_or_clear_bit_range_p(bm, nb, len, true);
}
static inline int raviX_bitmap_clear_bit_range_p(struct bitset_t * bm, size_t nb, size_t len) {
return raviX_bitmap_set_or_clear_bit_range_p(bm, nb, len, false);
static inline int raviX_bitset_clear_bit_range_p(struct bitset_t * bm, size_t nb, size_t len) {
return raviX_bitset_set_or_clear_bit_range_p(bm, nb, len, false);
}
extern void raviX_bitmap_copy(struct bitset_t * dst, const struct bitset_t * src);
extern int raviX_bitmap_equal_p(const struct bitset_t * bm1, const struct bitset_t * bm2);
extern int raviX_bitmap_intersect_p(const struct bitset_t * bm1, const struct bitset_t * bm2);
extern int raviX_bitmap_empty_p(const struct bitset_t * bm);
extern void raviX_bitset_copy(struct bitset_t * dst, const struct bitset_t * src);
extern int raviX_bitset_equal_p(const struct bitset_t * bm1, const struct bitset_t * bm2);
extern int raviX_bitset_intersect_p(const struct bitset_t * bm1, const struct bitset_t * bm2);
extern int raviX_bitset_empty_p(const struct bitset_t * bm);
/* Return the number of bits set in BM. */
extern size_t raviX_bitmap_bit_count(const struct bitset_t * bm);
extern int raviX_bitmap_op2(struct bitset_t * dst, const struct bitset_t * src1, const struct bitset_t * src2,
extern size_t raviX_bitset_bit_count(const struct bitset_t * bm);
extern int raviX_bitset_op2(struct bitset_t * dst, const struct bitset_t * src1, const struct bitset_t * src2,
bitset_el_t (*op) (bitset_el_t, bitset_el_t));
static inline bitset_el_t raviX_bitmap_el_and(bitset_el_t el1, bitset_el_t el2) { return el1 & el2; }
static inline int raviX_bitmap_and(struct bitset_t * dst, struct bitset_t * src1, struct bitset_t * src2) {
return raviX_bitmap_op2(dst, src1, src2, raviX_bitmap_el_and);
static inline bitset_el_t raviX_bitset_el_and(bitset_el_t el1, bitset_el_t el2) { return el1 & el2; }
static inline int raviX_bitset_and(struct bitset_t * dst, struct bitset_t * src1, struct bitset_t * src2) {
return raviX_bitset_op2(dst, src1, src2, raviX_bitset_el_and);
}
static inline bitset_el_t raviX_bitmap_el_and_compl(bitset_el_t el1, bitset_el_t el2) {
static inline bitset_el_t raviX_bitset_el_and_compl(bitset_el_t el1, bitset_el_t el2) {
return el1 & ~el2;
}
static inline int raviX_bitmap_and_compl(struct bitset_t * dst, struct bitset_t * src1, struct bitset_t * src2) {
return raviX_bitmap_op2(dst, src1, src2, raviX_bitmap_el_and_compl);
static inline int raviX_bitset_and_compl(struct bitset_t * dst, struct bitset_t * src1, struct bitset_t * src2) {
return raviX_bitset_op2(dst, src1, src2, raviX_bitset_el_and_compl);
}
static inline bitset_el_t raviX_bitmap_el_ior(bitset_el_t el1, bitset_el_t el2) { return el1 | el2; }
static inline int raviX_bitmap_ior(struct bitset_t * dst, struct bitset_t * src1, struct bitset_t * src2) {
return raviX_bitmap_op2(dst, src1, src2, raviX_bitmap_el_ior);
static inline bitset_el_t raviX_bitset_el_ior(bitset_el_t el1, bitset_el_t el2) { return el1 | el2; }
static inline int raviX_bitset_ior(struct bitset_t * dst, struct bitset_t * src1, struct bitset_t * src2) {
return raviX_bitset_op2(dst, src1, src2, raviX_bitset_el_ior);
}
int raviX_bitmap_op3(struct bitset_t * dst, const struct bitset_t * src1, const struct bitset_t * src2,
int raviX_bitset_op3(struct bitset_t * dst, const struct bitset_t * src1, const struct bitset_t * src2,
const struct bitset_t * src3, bitset_el_t (*op) (bitset_el_t, bitset_el_t, bitset_el_t));
static inline bitset_el_t raviX_bitmap_el_ior_and(bitset_el_t el1, bitset_el_t el2, bitset_el_t el3) {
static inline bitset_el_t raviX_bitset_el_ior_and(bitset_el_t el1, bitset_el_t el2, bitset_el_t el3) {
return el1 | (el2 & el3);
}
/* DST = SRC1 | (SRC2 & SRC3). Return true if DST changed. */
static inline int raviX_bitmap_ior_and(struct bitset_t * dst, struct bitset_t * src1, struct bitset_t * src2, struct bitset_t * src3) {
return raviX_bitmap_op3(dst, src1, src2, src3, raviX_bitmap_el_ior_and);
static inline int raviX_bitset_ior_and(struct bitset_t * dst, struct bitset_t * src1, struct bitset_t * src2, struct bitset_t * src3) {
return raviX_bitset_op3(dst, src1, src2, src3, raviX_bitset_el_ior_and);
}
static inline bitset_el_t raviX_bitmap_el_ior_and_compl(bitset_el_t el1, bitset_el_t el2, bitset_el_t el3) {
static inline bitset_el_t raviX_bitset_el_ior_and_compl(bitset_el_t el1, bitset_el_t el2, bitset_el_t el3) {
return el1 | (el2 & ~el3);
}
/* DST = SRC1 | (SRC2 & ~SRC3). Return true if DST changed. */
static inline int raviX_bitmap_ior_and_compl(struct bitset_t * dst, struct bitset_t * src1, struct bitset_t * src2, struct bitset_t * src3) {
return raviX_bitmap_op3(dst, src1, src2, src3, raviX_bitmap_el_ior_and_compl);
static inline int raviX_bitset_ior_and_compl(struct bitset_t * dst, struct bitset_t * src1, struct bitset_t * src2, struct bitset_t * src3) {
return raviX_bitset_op3(dst, src1, src2, src3, raviX_bitset_el_ior_and_compl);
}

typedef struct {
struct bitset_t * bitmap;
struct bitset_t * bitset;
size_t nbit;
} bitmap_iterator_t;
static inline void raviX_bitmap_iterator_init(bitmap_iterator_t *iter, struct bitset_t * bitmap) {
iter->bitmap = bitmap;
} bitset_iterator_t;
static inline void raviX_bitset_iterator_init(bitset_iterator_t *iter, struct bitset_t * bitset) {
iter->bitset = bitset;
iter->nbit = 0;
}
extern int raviX_bitmap_iterator_next(bitmap_iterator_t *iter, size_t *nbit);
#define FOREACH_BITMAP_BIT(iter, bitmap, nbit) \
for (raviX_bitmap_iterator_init (&iter, bitmap); raviX_bitmap_iterator_next (&iter, &nbit);)
extern int raviX_bitset_iterator_next(bitset_iterator_t *iter, size_t *nbit);
#define FOREACH_BITSET_BIT(iter, bitset, nbit) \
for (raviX_bitset_iterator_init (&iter, bitset); raviX_bitset_iterator_next (&iter, &nbit);)



Expand Down
Loading

0 comments on commit 52e27ec

Please sign in to comment.