Skip to content

Commit

Permalink
Add dict/set clear methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdrake committed May 23, 2015
1 parent 0e02845 commit 77e0007
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions extension/boolexpr/boolexpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ struct BoolExpr * BoolExprDict_Search(struct BoolExprDict *, struct BoolExpr *ke

bool BoolExprDict_Contains(struct BoolExprDict *, struct BoolExpr *key);

void BoolExprDict_Clear(struct BoolExprDict *);


/*
** Return a new set of Boolean expressions.
Expand All @@ -439,6 +441,8 @@ bool BoolExprSet_Contains(struct BoolExprSet *, struct BoolExpr *key);

bool BoolExprSet_Equal(struct BoolExprSet *, struct BoolExprSet *);

void BoolExprSet_Clear(struct BoolExprSet *);


#ifdef __cplusplus
}
Expand Down
14 changes: 14 additions & 0 deletions extension/boolexpr/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,17 @@ BoolExprDict_Contains(struct BoolExprDict *dict, struct BoolExpr *key)
return _list_search(dict->items[index], key) != (struct BoolExpr *) NULL;
}


void
BoolExprDict_Clear(struct BoolExprDict *dict)
{
for (size_t i = 0; i < _primes[dict->pridx]; ++i) {
if (dict->items[i] != (struct BoolExprDictItem *) NULL) {
_list_del(dict->items[i]);
dict->items[i] = (struct BoolExprDictItem *) NULL;
}
}

dict->length = 0;
}

14 changes: 14 additions & 0 deletions extension/boolexpr/set.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,17 @@ BoolExprSet_Equal(struct BoolExprSet *self, struct BoolExprSet *other)
return true;
}


void
BoolExprSet_Clear(struct BoolExprSet *set)
{
for (size_t i = 0; i < _primes[set->pridx]; ++i) {
if (set->items[i] != (struct BoolExprSetItem *) NULL) {
_list_del(set->items[i]);
set->items[i] = (struct BoolExprSetItem *) NULL;
}
}

set->length = 0;
}

17 changes: 17 additions & 0 deletions extension/boolexpr/test/test_dict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,20 @@ TEST_F(BoolExprDictTest, Removal)
BoolExprDict_Del(dict);
}


TEST_F(BoolExprDictTest, Clear)
{
BoolExprDict *dict = BoolExprDict_New();
int length = 0;

for (int i = 0; i < 32; ++i) {
BoolExprDict_Insert(dict, xns[i], xs[i]);
EXPECT_EQ(dict->length, ++length);
}

BoolExprDict_Clear(dict);
EXPECT_EQ(dict->length, 0);

BoolExprDict_Del(dict);
}

18 changes: 18 additions & 0 deletions extension/boolexpr/test/test_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,21 @@ TEST_F(BoolExprSetTest, Equality)
BoolExprSet_Del(c);
BoolExprSet_Del(d);
}


TEST_F(BoolExprSetTest, Clear)
{
BoolExprSet *a = BoolExprSet_New();

BoolExprSet_Insert(a, xns[0]);
BoolExprSet_Insert(a, xs[1]);
BoolExprSet_Insert(a, xns[2]);
BoolExprSet_Insert(a, xs[3]);

ASSERT_EQ(a->length, 4);
BoolExprSet_Clear(a);
ASSERT_EQ(a->length, 0);

BoolExprSet_Del(a);
}

0 comments on commit 77e0007

Please sign in to comment.