Skip to content

Commit

Permalink
Clarify dict/set hash and eq functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdrake committed May 23, 2015
1 parent 0d58ef8 commit 0e02845
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
27 changes: 17 additions & 10 deletions extension/boolexpr/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ struct BoolExprDictItem {
};


static size_t
_hash(struct BoolExprDict *dict, struct BoolExpr *key)
{
return (size_t) key % _primes[dict->pridx];
}


static bool
_eq(struct BoolExpr *key1, struct BoolExpr *key2)
{
return key1 == key2;
}


static void
_list_del(struct BoolExprDictItem *list)
{
Expand All @@ -77,7 +91,7 @@ _list_search(struct BoolExprDictItem *list, struct BoolExpr *key)
{
if (list == (struct BoolExprDictItem *) NULL)
return (struct BoolExpr *) NULL;
else if (list->key == key)
else if (_eq(list->key, key))
return list->val;
else
return _list_search(list->tail, key);
Expand Down Expand Up @@ -122,13 +136,6 @@ BoolExprDict_Del(struct BoolExprDict *dict)
}


static size_t
_hash(struct BoolExprDict *dict, struct BoolExpr *key)
{
return (size_t) key % _primes[dict->pridx];
}


static bool
_insert(struct BoolExprDict *dict, struct BoolExpr *key, struct BoolExpr *val)
{
Expand All @@ -137,7 +144,7 @@ _insert(struct BoolExprDict *dict, struct BoolExpr *key, struct BoolExpr *val)
struct BoolExprDictItem *item;

for (item = dict->items[index]; item; item = item->tail) {
if (item->key == key) {
if (_eq(item->key, key)) {
BoolExpr_DecRef(item->key);
BoolExpr_DecRef(item->val);
item->key = BoolExpr_IncRef(key);
Expand Down Expand Up @@ -222,7 +229,7 @@ BoolExprDict_Remove(struct BoolExprDict *dict, struct BoolExpr *key)
struct BoolExprDictItem *item = dict->items[index];

while (item) {
if (item->key == key) {
if (_eq(item->key, key)) {
BoolExpr_DecRef(item->key);
BoolExpr_DecRef(item->val);
*p = item->tail;
Expand Down
27 changes: 17 additions & 10 deletions extension/boolexpr/set.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ struct BoolExprSetItem {
};


static size_t
_hash(struct BoolExprSet *set, struct BoolExpr *key)
{
return (size_t) key % _primes[set->pridx];
}


static bool
_eq(struct BoolExpr *key1, struct BoolExpr *key2)
{
return key1 == key2;
}


static void
_list_del(struct BoolExprSetItem *list)
{
Expand All @@ -75,7 +89,7 @@ _list_contains(struct BoolExprSetItem *list, struct BoolExpr *key)
{
if (list == (struct BoolExprSetItem *) NULL)
return false;
else if (list->key == key)
else if (_eq(list->key, key))
return true;
else
return _list_contains(list->tail, key);
Expand Down Expand Up @@ -120,13 +134,6 @@ BoolExprSet_Del(struct BoolExprSet *set)
}


static size_t
_hash(struct BoolExprSet *set, struct BoolExpr *key)
{
return (size_t) key % _primes[set->pridx];
}


static bool
_insert(struct BoolExprSet *set, struct BoolExpr *key)
{
Expand All @@ -135,7 +142,7 @@ _insert(struct BoolExprSet *set, struct BoolExpr *key)
struct BoolExprSetItem *item;

for (item = set->items[index]; item; item = item->tail) {
if (item->key == key) {
if (_eq(item->key, key)) {
BoolExpr_DecRef(item->key);
item->key = BoolExpr_IncRef(key);
return true;
Expand Down Expand Up @@ -217,7 +224,7 @@ BoolExprSet_Remove(struct BoolExprSet *set, struct BoolExpr *key)
struct BoolExprSetItem *item = set->items[index];

while (item) {
if (item->key == key) {
if (_eq(item->key, key)) {
BoolExpr_DecRef(item->key);
*p = item->tail;
free(item);
Expand Down

0 comments on commit 0e02845

Please sign in to comment.