Skip to content

Commit

Permalink
Add BX_Support function, returns expr support set
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdrake committed Jun 18, 2015
1 parent 1291e11 commit df362d1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 5 deletions.
39 changes: 39 additions & 0 deletions extension/boolexpr/boolexpr.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,45 @@ BX_DecRef(struct BoolExpr * ex)
}


struct BX_Set *
BX_Support(struct BoolExpr *ex)
{
struct BX_Set *s;
struct BX_Iter *it;

s = BX_Set_New();
if (s == NULL)
return NULL; // LCOV_EXCL_LINE

it = BX_Iter_New(ex);
if (it == NULL) {
BX_Set_Del(s); // LCOV_EXCL_LINE
return NULL; // LCOV_EXCL_LINE
}

while (!it->done) {
bool status = true;
if (BX_IS_VAR(it->item)) {
status = BX_Set_Insert(s, it->item);
}
else if (BX_IS_COMP(it->item)) {
struct BoolExpr *var = BX_Not(it->item);
status = BX_Set_Insert(s, var);
BX_DecRef(var);
}
if (!status || !BX_Iter_Next(it)) {
BX_Set_Del(s); // LCOV_EXCL_LINE
BX_Iter_Del(it); // LCOV_EXCL_LINE
return NULL; // LCOV_EXCL_LINE
}
}

BX_Iter_Del(it);

return s;
}


unsigned long
BX_Depth(struct BoolExpr *ex)
{
Expand Down
6 changes: 6 additions & 0 deletions extension/boolexpr/boolexpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ struct BoolExpr * BX_IncRef(struct BoolExpr *);
void BX_DecRef(struct BoolExpr *);


/* Return the support set of an expression. */
struct BX_Set * BX_Support(struct BoolExpr *);


/*
** Return the depth of an expression tree.
**
Expand Down Expand Up @@ -440,6 +444,8 @@ bool BX_Set_GT(struct BX_Set *, struct BX_Set *);
bool BX_Set_GTE(struct BX_Set *, struct BX_Set *);
bool BX_Set_LT(struct BX_Set *, struct BX_Set *);

bool BX_Set_Update(struct BX_Set *, struct BX_Set *);

/* Remove all items from the set. */
void BX_Set_Clear(struct BX_Set *);

Expand Down
16 changes: 16 additions & 0 deletions extension/boolexpr/set.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,22 @@ BX_Set_LT(struct BX_Set *self, struct BX_Set *other)
}


bool
BX_Set_Update(struct BX_Set *self, struct BX_Set *other)
{
struct BX_SetItem *item;

for (size_t i = 0; i < _primes[other->_pridx]; ++i) {
for (item = other->items[i]; item; item = item->tail) {
if (!BX_Set_Insert(self, item->key))
return false; // LCOV_EXCL_LINE
}
}

return true;
}


void
BX_Set_Clear(struct BX_Set *set)
{
Expand Down
28 changes: 23 additions & 5 deletions extension/boolexpr/test/test_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ TEST_F(BoolExpr_Test, Iterate)

TEST_F(BoolExpr_Test, Properties)
{
ops[0] = BX_AndN(2, xs[0], xs[1]);
ops[1] = BX_XorN(2, xs[2], xs[3]);
ops[2] = BX_EqualN(2, xs[4], xs[5]);
ops[3] = BX_Implies(xs[6], xs[7]);
ops[4] = BX_ITE(xs[8], xs[9], xs[10]);
ops[0] = BX_AndN(2, xs[0], xns[1]);
ops[1] = BX_XorN(2, xs[2], xns[3]);
ops[2] = BX_EqualN(2, xs[4], xns[5]);
ops[3] = BX_Implies(xs[6], xns[7]);
ops[4] = BX_ITE(xs[8], xns[9], xs[10]);
ops[5] = BX_NorN(5, ops[0], ops[1], ops[2], ops[3], ops[4]);

EXPECT_EQ(BX_Depth(xs[0]), 0);
Expand Down Expand Up @@ -215,6 +215,24 @@ TEST_F(BoolExpr_Test, Properties)
EXPECT_EQ(BX_OpCount(ops[3]), 1);
EXPECT_EQ(BX_OpCount(ops[4]), 1);
EXPECT_EQ(BX_OpCount(ops[5]), 7);

struct BX_Set *s = BX_Set_New();
for (int i = 0; i <= 10; ++i)
BX_Set_Insert(s, xs[i]);
struct BX_Set *t = BX_Support(ops[5]);

struct BX_Set *u = BX_Support(&BX_Zero);
EXPECT_EQ(u->length, 0);

struct BX_Set *v = BX_Support(&BX_One);
EXPECT_EQ(v->length, 0);

EXPECT_TRUE(BX_Set_EQ(s, t));

BX_Set_Del(s);
BX_Set_Del(t);
BX_Set_Del(u);
BX_Set_Del(v);
}


Expand Down

0 comments on commit df362d1

Please sign in to comment.