Skip to content

Commit

Permalink
Change the way expression iteration works
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdrake committed Jun 7, 2015
1 parent 47af72c commit b9c2196
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 38 deletions.
66 changes: 45 additions & 21 deletions extension/boolexpr/boolexpr.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,21 @@ BoolExprIter_New(struct BoolExpr *ex)
if (it == NULL)
return NULL; // LCOV_EXCL_LINE

it->_ex = ex;
it->done = false;
it->ex = ex;

if (IS_OP(ex)) {
it->index = 0;
it->it = BoolExprIter_New(ex->data.xs->items[0]);
if (it->it == NULL) {
free(it); // LCOV_EXCL_LINE
return NULL; // LCOV_EXCL_LINE
}

if (IS_ATOM(ex)) {
it->item = ex;
return it;
}

it->_index = 0;
it->_it = BoolExprIter_New(ex->data.xs->items[it->_index]);
if (it->_it == NULL) {
free(it); // LCOV_EXCL_LINE
return NULL; // LCOV_EXCL_LINE
}
it->item = it->_it->item;

return it;
}
Expand All @@ -68,27 +72,47 @@ BoolExprIter_Del(struct BoolExprIter *it)
}


struct BoolExpr *
bool
BoolExprIter_Next(struct BoolExprIter *it)
{
if (IS_ATOM(it->ex)) {
if (it->done)
return true;

if (IS_ATOM(it->_ex)) {
it->done = true;
return it->ex;
return true;
}

if (it->it->done) {
BoolExprIter_Del(it->it);
it->index += 1;
if (it->index < it->ex->data.xs->length) {
CHECK_NULL(it->it, BoolExprIter_New(it->ex->data.xs->items[it->index]));
return BoolExprIter_Next(it->it);
if (it->_it) {
if (!BoolExprIter_Next(it->_it)) {
free(it->_it); // LCOV_EXCL_LINE
return false; // LCOV_EXCL_LINE
}

it->done = true;
return it->ex;
if (!it->_it->done) {
it->item = it->_it->item;
return true;
}

free(it->_it);
it->_index += 1;

if (it->_index < it->_ex->data.xs->length) {
it->_it = BoolExprIter_New(it->_ex->data.xs->items[it->_index]);
if (it->_it == NULL)
return false; // LCOV_EXCL_LINE
it->item = it->_it->item;
return true;
}

it->_it = (struct BoolExprIter *) NULL;
it->item = it->_ex;
return true;
}

return BoolExprIter_Next(it->it);
it->item = (struct BoolExpr *) NULL;
it->done = true;
return true;
}


Expand Down
10 changes: 6 additions & 4 deletions extension/boolexpr/boolexpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,12 @@ struct BoolExpr {


struct BoolExprIter {
struct BoolExpr *_ex;
size_t _index;
struct BoolExprIter *_it;

struct BoolExpr *item;
bool done;
struct BoolExpr *ex;
size_t index;
struct BoolExprIter *it;
};


Expand Down Expand Up @@ -396,7 +398,7 @@ struct BoolExprIter * BoolExprIter_New(struct BoolExpr *ex);
void BoolExprIter_Del(struct BoolExprIter *);

/* Return the next Boolean expression in an iteration. */
struct BoolExpr * BoolExprIter_Next(struct BoolExprIter *);
bool BoolExprIter_Next(struct BoolExprIter *);


/* Return a new array of Boolean expressions. */
Expand Down
1 change: 1 addition & 0 deletions extension/boolexpr/set.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ BoolExprSetIter_Next(struct BoolExprSetIter *it)
}
}

it->item = (struct BoolExprSetItem *) NULL;
it->done = true;
}

Expand Down
36 changes: 28 additions & 8 deletions extension/boolexpr/test/test_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ TEST_F(BoolExprTest, DegenerateForms)

TEST_F(BoolExprTest, Iterate)
{
int i;
BoolExprIter *it;
BoolExpr *ex;

ops[0] = AndN(2, xs[0], xs[1]);
ops[1] = XorN(2, xs[2], xs[3]);
Expand All @@ -123,7 +120,30 @@ TEST_F(BoolExprTest, Iterate)
ops[4] = ITE(xs[8], xs[9], xs[10]);
ops[5] = NorN(5, ops[0], ops[1], ops[2], ops[3], ops[4]);

BoolExpr *expected[] = {
size_t count = 0;
struct BoolExprIter *it;

struct BoolExpr *exp0[] = {xs[0]};

it = BoolExprIter_New(xs[0]);
for (count = 0; !it->done; ++count) {
EXPECT_EQ(it->item, exp0[0]);
BoolExprIter_Next(it);
}
BoolExprIter_Del(it);
EXPECT_EQ(count, 1);

struct BoolExpr *exp1[] = {xs[0], xs[1], ops[0]};

it = BoolExprIter_New(ops[0]);
for (count = 0; !it->done; ++count) {
EXPECT_EQ(it->item, exp1[count]);
BoolExprIter_Next(it);
}
BoolExprIter_Del(it);
EXPECT_EQ(count, 3);

struct BoolExpr *exp2[] = {
xs[0], xs[1], ops[0],
xs[2], xs[3], ops[1],
xs[4], xs[5], ops[2],
Expand All @@ -134,13 +154,13 @@ TEST_F(BoolExprTest, Iterate)
};

it = BoolExprIter_New(ops[5]);
for (i = 0; !it->done; ++i) {
ex = BoolExprIter_Next(it);
EXPECT_EQ(ex, expected[i]);
for (count = 0; !it->done; ++count) {
EXPECT_EQ(it->item, exp2[count]);
BoolExprIter_Next(it);
}
BoolExprIter_Del(it);

EXPECT_EQ(i, 18);
EXPECT_EQ(count, 18);
}


Expand Down
13 changes: 8 additions & 5 deletions pyeda/boolalg/exprnodemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,21 +485,24 @@ ExprNode_T = {
static PyObject *
ExprNode_next(ExprNode *self)
{
ExprNode *node;

if (self->it->done) {
BoolExprIter_Del(self->it);
/* StopIteration */
return NULL;
}

struct BoolExpr *ex = BoolExprIter_Next(self->it);
ExprNode *pyex = (ExprNode *) PyObject_CallObject((PyObject *) &ExprNode_T, NULL);
if (pyex == NULL) {
node = (ExprNode *) PyObject_CallObject((PyObject *) &ExprNode_T, NULL);
if (node == NULL) {
BoolExprIter_Del(self->it);
return NULL;
}
pyex->ex = BoolExpr_IncRef(ex);
node->ex = BoolExpr_IncRef(self->it->item);

return (PyObject *) pyex;
BoolExprIter_Next(self->it);

return (PyObject *) node;
}


Expand Down

0 comments on commit b9c2196

Please sign in to comment.