Skip to content

Commit

Permalink
Add dict update method
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdrake committed Jun 28, 2015
1 parent ee2b8eb commit 511ca5f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions extension/boolexpr/boolexpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ void BX_Dict_Clear(struct BX_Dict *);
/* Return true if two dicts are equal. */
bool BX_Dict_Equal(struct BX_Dict *, struct BX_Dict *);

bool BX_Dict_Update(struct BX_Dict *, struct BX_Dict *);


/*
** Return a new set of Boolean expressions.
Expand Down
16 changes: 16 additions & 0 deletions extension/boolexpr/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,19 @@ BX_Dict_Equal(struct BX_Dict *self, struct BX_Dict *other)
return true;
}


bool
BX_Dict_Update(struct BX_Dict *self, struct BX_Dict *other)
{
struct BX_DictItem *item;

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

return true;
}

25 changes: 25 additions & 0 deletions extension/boolexpr/test/test_dict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,28 @@ TEST_F(BX_Dict_Test, Equal)
BX_Dict_Del(B);
}


TEST_F(BX_Dict_Test, Update)
{
struct BX_Dict *a = BX_Dict_New();
struct BX_Dict *b = BX_Dict_New();
struct BX_Dict *c = BX_Dict_New();

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

BX_Dict_Insert(b, xs[3], xns[3]);
BX_Dict_Insert(b, xs[4], xns[4]);
BX_Dict_Insert(b, xs[5], xns[5]);

for (int i = 0; i < 6; ++i)
BX_Dict_Insert(c, xs[i], xns[i]);

BX_Dict_Update(a, b);
ASSERT_TRUE(BX_Dict_Equal(a, c));

BX_Dict_Del(a);
BX_Dict_Del(b);
BX_Dict_Del(c);
}

0 comments on commit 511ca5f

Please sign in to comment.