Skip to content

Commit

Permalink
Miscellaneous refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdrake committed May 30, 2015
1 parent 06231f5 commit 3fc5c84
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 34 deletions.
3 changes: 1 addition & 2 deletions extension/boolexpr/boolexpr.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,8 @@ BoolExpr_IsCNF(struct BoolExpr *ex)
if (IS_AND(ex)) {
for (size_t i = 0; i < ex->data.xs->length; ++i) {
struct BoolExpr *x = ex->data.xs->items[i];
if (!IS_LIT(x) && !(IS_OR(x) && _is_clause(x))) {
if (!IS_LIT(x) && !(IS_OR(x) && _is_clause(x)))
return false;
}
}
return true;
}
Expand Down
21 changes: 18 additions & 3 deletions extension/boolexpr/boolexpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ struct BoolExprDictItem {


struct BoolExprDict {
size_t _pridx;

size_t length;
size_t pridx;
struct BoolExprDictItem **items;
};

Expand All @@ -194,8 +195,9 @@ struct BoolExprSetItem {


struct BoolExprSet {
size_t _pridx;

size_t length;
size_t pridx;
struct BoolExprSetItem **items;
};

Expand Down Expand Up @@ -459,13 +461,16 @@ void BoolExprDict_Del(struct BoolExprDict *);
/* Insert an expression into the dictionary. */
bool BoolExprDict_Insert(struct BoolExprDict *, struct BoolExpr *key, struct BoolExpr *val);

/* Delete an expression from the dictionary. */
/* Remove an expression from the dictionary. */
bool BoolExprDict_Remove(struct BoolExprDict *, struct BoolExpr *key);

/* If the dict contains the key, return its value. */
struct BoolExpr * BoolExprDict_Search(struct BoolExprDict *, struct BoolExpr *key);

/* Return true if the dict contains the key. */
bool BoolExprDict_Contains(struct BoolExprDict *, struct BoolExpr *key);

/* Remove all items from the dict. */
void BoolExprDict_Clear(struct BoolExprDict *);


Expand All @@ -474,27 +479,37 @@ void BoolExprDict_Clear(struct BoolExprDict *);
*/
struct BoolExprSet * BoolExprSet_New(void);

/* Delete a set of Boolean expressions. */
void BoolExprSet_Del(struct BoolExprSet *);

/*
** Return a new Boolean expression set iterator.
*/
struct BoolExprSetIter * BoolExprSetIter_New(struct BoolExprSet *);

/* Delete a Boolean expression set iterator. */
void BoolExprSetIter_Del(struct BoolExprSetIter *);

void BoolExprSetIter_Next(struct BoolExprSetIter *);

/* Insert an expression into the set. */
bool BoolExprSet_Insert(struct BoolExprSet *, struct BoolExpr *key);

/* Remove an expression from the set. */
bool BoolExprSet_Remove(struct BoolExprSet *, struct BoolExpr *key);

/* Return true if the set contains the key. */
bool BoolExprSet_Contains(struct BoolExprSet *, struct BoolExpr *key);

/* Set comparison operators */
bool BoolExprSet_EQ(struct BoolExprSet *, struct BoolExprSet *);
bool BoolExprSet_NE(struct BoolExprSet *, struct BoolExprSet *);
bool BoolExprSet_LTE(struct BoolExprSet *, struct BoolExprSet *);
bool BoolExprSet_GT(struct BoolExprSet *, struct BoolExprSet *);
bool BoolExprSet_GTE(struct BoolExprSet *, struct BoolExprSet *);
bool BoolExprSet_LT(struct BoolExprSet *, struct BoolExprSet *);

/* Remove all items from the set. */
void BoolExprSet_Clear(struct BoolExprSet *);


Expand Down
20 changes: 10 additions & 10 deletions extension/boolexpr/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static size_t _primes[] = {
static size_t
_hash(struct BoolExprDict *dict, struct BoolExpr *key)
{
return (size_t) key % _primes[dict->pridx];
return (size_t) key % _primes[dict->_pridx];
}


Expand Down Expand Up @@ -112,8 +112,8 @@ BoolExprDict_New(void)
for (size_t i = 0; i < width; ++i)
dict->items[i] = (struct BoolExprDictItem *) NULL;

dict->_pridx = pridx;
dict->length = 0;
dict->pridx = pridx;

return dict;
}
Expand All @@ -122,7 +122,7 @@ BoolExprDict_New(void)
void
BoolExprDict_Del(struct BoolExprDict *dict)
{
for (size_t i = 0; i < _primes[dict->pridx]; ++i)
for (size_t i = 0; i < _primes[dict->_pridx]; ++i)
_list_del(dict->items[i]);

free(dict->items);
Expand Down Expand Up @@ -167,13 +167,13 @@ _enlarge(struct BoolExprDict *dict)
{
struct BoolExprDictItem *item;

size_t pridx = dict->pridx;
size_t pridx = dict->_pridx;
struct BoolExprDictItem **items = dict->items;

dict->_pridx += 1;
dict->length = 0;
dict->pridx += 1;
dict->items = (struct BoolExprDictItem **) malloc(_primes[dict->pridx] * sizeof(struct BoolExprDictItem *));
for (size_t i = 0; i < _primes[dict->pridx]; ++i)
dict->items = (struct BoolExprDictItem **) malloc(_primes[dict->_pridx] * sizeof(struct BoolExprDictItem *));
for (size_t i = 0; i < _primes[dict->_pridx]; ++i)
dict->items[i] = (struct BoolExprDictItem *) NULL;

for (size_t i = 0; i < _primes[pridx]; ++i) {
Expand Down Expand Up @@ -203,9 +203,9 @@ BoolExprDict_Insert(struct BoolExprDict *dict, struct BoolExpr *key, struct Bool
if (!_insert(dict, key, val))
return false; // LCOV_EXCL_LINE

load = (double) dict->length / (double) _primes[dict->pridx];
load = (double) dict->length / (double) _primes[dict->_pridx];

if (dict->pridx < _MAX_IDX && load > MAX_LOAD) {
if (dict->_pridx < _MAX_IDX && load > MAX_LOAD) {
if (!_enlarge(dict))
return false; // LCOV_EXCL_LINE
}
Expand Down Expand Up @@ -262,7 +262,7 @@ BoolExprDict_Contains(struct BoolExprDict *dict, struct BoolExpr *key)
void
BoolExprDict_Clear(struct BoolExprDict *dict)
{
for (size_t i = 0; i < _primes[dict->pridx]; ++i) {
for (size_t i = 0; i < _primes[dict->_pridx]; ++i) {
if (dict->items[i]) {
_list_del(dict->items[i]);
dict->items[i] = (struct BoolExprDictItem *) NULL;
Expand Down
34 changes: 17 additions & 17 deletions extension/boolexpr/set.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static size_t _primes[] = {
static size_t
_hash(struct BoolExprSet *set, struct BoolExpr *key)
{
return (size_t) key % _primes[set->pridx];
return (size_t) key % _primes[set->_pridx];
}


Expand Down Expand Up @@ -111,8 +111,8 @@ BoolExprSet_New(void)
for (size_t i = 0; i < width; ++i)
set->items[i] = (struct BoolExprSetItem *) NULL;

set->_pridx = pridx;
set->length = 0;
set->pridx = pridx;

return set;
}
Expand All @@ -121,7 +121,7 @@ BoolExprSet_New(void)
void
BoolExprSet_Del(struct BoolExprSet *set)
{
for (size_t i = 0; i < _primes[set->pridx]; ++i)
for (size_t i = 0; i < _primes[set->_pridx]; ++i)
_list_del(set->items[i]);

free(set->items);
Expand All @@ -143,7 +143,7 @@ BoolExprSetIter_New(struct BoolExprSet *set)
it->done = true;
it->item = (struct BoolExprSetItem *) NULL;

for (it->_index = 0; it->_index < _primes[set->pridx]; it->_index += 1) {
for (it->_index = 0; it->_index < _primes[set->_pridx]; it->_index += 1) {
if (set->items[it->_index]) {
it->done = false;
it->item = set->items[it->_index];
Expand Down Expand Up @@ -173,7 +173,7 @@ BoolExprSetIter_Next(struct BoolExprSetIter *it)
return;
}

for (it->_index += 1; it->_index < _primes[it->_set->pridx]; it->_index += 1) {
for (it->_index += 1; it->_index < _primes[it->_set->_pridx]; it->_index += 1) {
if (it->_set->items[it->_index]) {
it->item = it->_set->items[it->_index];
return;
Expand Down Expand Up @@ -218,13 +218,13 @@ _enlarge(struct BoolExprSet *set)
{
struct BoolExprSetItem *item;

size_t pridx = set->pridx;
size_t pridx = set->_pridx;
struct BoolExprSetItem **items = set->items;

set->_pridx += 1;
set->length = 0;
set->pridx += 1;
set->items = (struct BoolExprSetItem **) malloc(_primes[set->pridx] * sizeof(struct BoolExprSetItem *));
for (size_t i = 0; i < _primes[set->pridx]; ++i)
set->items = (struct BoolExprSetItem **) malloc(_primes[set->_pridx] * sizeof(struct BoolExprSetItem *));
for (size_t i = 0; i < _primes[set->_pridx]; ++i)
set->items[i] = (struct BoolExprSetItem *) NULL;

for (size_t i = 0; i < _primes[pridx]; ++i) {
Expand Down Expand Up @@ -254,9 +254,9 @@ BoolExprSet_Insert(struct BoolExprSet *set, struct BoolExpr *key)
if (!_insert(set, key))
return false; // LCOV_EXCL_LINE

load = (double) set->length / (double) _primes[set->pridx];
load = (double) set->length / (double) _primes[set->_pridx];

if (set->pridx < _MAX_IDX && load > MAX_LOAD) {
if (set->_pridx < _MAX_IDX && load > MAX_LOAD) {
if (!_enlarge(set))
return false; // LCOV_EXCL_LINE
}
Expand Down Expand Up @@ -309,7 +309,7 @@ BoolExprSet_EQ(struct BoolExprSet *self, struct BoolExprSet *other)
struct BoolExprSetItem *item;

// All items in self must also be in other (and vice versa)
for (size_t i = 0; i < _primes[self->pridx]; ++i)
for (size_t i = 0; i < _primes[self->_pridx]; ++i)
for (item = self->items[i]; item; item = item->tail)
if (!BoolExprSet_Contains(other, item->key))
return false;
Expand All @@ -334,7 +334,7 @@ BoolExprSet_LTE(struct BoolExprSet *self, struct BoolExprSet *other)
struct BoolExprSetItem *item;

// All items in self must also be in other
for (size_t i = 0; i < _primes[self->pridx]; ++i)
for (size_t i = 0; i < _primes[self->_pridx]; ++i)
for (item = self->items[i]; item; item = item->tail)
if (!BoolExprSet_Contains(other, item->key))
return false;
Expand All @@ -352,7 +352,7 @@ BoolExprSet_GT(struct BoolExprSet *self, struct BoolExprSet *other)
struct BoolExprSetItem *item;

// All items in other must also be in self
for (size_t i = 0; i < _primes[other->pridx]; ++i)
for (size_t i = 0; i < _primes[other->_pridx]; ++i)
for (item = other->items[i]; item; item = item->tail)
if (!BoolExprSet_Contains(self, item->key))
return false;
Expand All @@ -370,7 +370,7 @@ BoolExprSet_GTE(struct BoolExprSet *self, struct BoolExprSet *other)
struct BoolExprSetItem *item;

// All items in other must also be in self
for (size_t i = 0; i < _primes[other->pridx]; ++i)
for (size_t i = 0; i < _primes[other->_pridx]; ++i)
for (item = other->items[i]; item; item = item->tail)
if (!BoolExprSet_Contains(self, item->key))
return false;
Expand All @@ -388,7 +388,7 @@ BoolExprSet_LT(struct BoolExprSet *self, struct BoolExprSet *other)
struct BoolExprSetItem *item;

// All items in self must also be in other
for (size_t i = 0; i < _primes[self->pridx]; ++i)
for (size_t i = 0; i < _primes[self->_pridx]; ++i)
for (item = self->items[i]; item; item = item->tail)
if (!BoolExprSet_Contains(other, item->key))
return false;
Expand All @@ -400,7 +400,7 @@ BoolExprSet_LT(struct BoolExprSet *self, struct BoolExprSet *other)
void
BoolExprSet_Clear(struct BoolExprSet *set)
{
for (size_t i = 0; i < _primes[set->pridx]; ++i) {
for (size_t i = 0; i < _primes[set->_pridx]; ++i) {
if (set->items[i]) {
_list_del(set->items[i]);
set->items[i] = (struct BoolExprSetItem *) NULL;
Expand Down
2 changes: 1 addition & 1 deletion extension/boolexpr/test/test_dict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class BoolExprDictTest: public BoolExprTest {};
TEST_F(BoolExprDictTest, MinimumSize)
{
BoolExprDict *dict = BoolExprDict_New();
EXPECT_EQ(dict->pridx, 4);
EXPECT_EQ(dict->_pridx, 4);

BoolExprDict_Del(dict);
}
Expand Down
2 changes: 1 addition & 1 deletion extension/boolexpr/test/test_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class BoolExprSetTest: public BoolExprTest {};
TEST_F(BoolExprSetTest, MinimumSize)
{
BoolExprSet *set = BoolExprSet_New();
EXPECT_EQ(set->pridx, 4);
EXPECT_EQ(set->_pridx, 4);

BoolExprSet_Del(set);
}
Expand Down

0 comments on commit 3fc5c84

Please sign in to comment.