提交 da094fe2 编写于 作者: S Stefan Berger 提交者: Stefan Berger

Compare two hash tables for equality

Add function to compare two hash tables for equality.
上级 4c18acff
......@@ -663,3 +663,49 @@ virHashKeyValuePairPtr virHashGetItems(virHashTablePtr table,
return iter.sortArray;
}
struct virHashEqualData
{
bool equal;
const virHashTablePtr table2;
virHashValueComparator compar;
};
static int virHashEqualSearcher(const void *payload, const void *name,
const void *data)
{
struct virHashEqualData *vhed = (void *)data;
const void *value;
value = virHashLookup(vhed->table2, name);
if (!value ||
vhed->compar(value, payload) != 0) {
/* key is missing in 2nd table or values are different */
vhed->equal = false;
/* stop 'iteration' */
return 1;
}
return 0;
}
bool virHashEqual(const virHashTablePtr table1,
const virHashTablePtr table2,
virHashValueComparator compar)
{
struct virHashEqualData data = {
.equal = true,
.table2 = table2,
.compar = compar,
};
if (table1 == table2)
return true;
if (!table1 || !table2 ||
virHashSize(table1) != virHashSize(table2))
return false;
virHashSearch(table1, virHashEqualSearcher, &data);
return data.equal;
}
......@@ -153,6 +153,18 @@ typedef int (*virHashKeyComparator)(const virHashKeyValuePairPtr,
virHashKeyValuePairPtr virHashGetItems(virHashTablePtr table,
virHashKeyComparator compar);
/*
* Compare two tables for equality: the lookup of a key's value in
* both tables must result in an equivalent value.
* The caller must pass in a comparator function for comparing the values
* of two keys.
*/
typedef int (*virHashValueComparator)(const void *value1, const void *value2);
bool virHashEqual(const virHashTablePtr table1,
const virHashTablePtr table2,
virHashValueComparator compar);
/*
* Iterators
*/
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册