[PATCH v2 1/3] Compare two hash tables for equality

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add function to compare two hash tables for equality.

---
 src/util/hash.c |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 src/util/hash.h |   12 ++++++++++++
 2 files changed, 58 insertions(+)

Index: libvirt-iterator/src/util/hash.c
===================================================================
--- libvirt-iterator.orig/src/util/hash.c
+++ libvirt-iterator/src/util/hash.c
@@ -663,3 +663,49 @@ virHashKeyValuePairPtr virHashGetItems(v
 
     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;
+}
Index: libvirt-iterator/src/util/hash.h
===================================================================
--- libvirt-iterator.orig/src/util/hash.h
+++ libvirt-iterator/src/util/hash.h
@@ -154,6 +154,18 @@ virHashKeyValuePairPtr virHashGetItems(v
                                        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
  */
 int virHashForEach(virHashTablePtr table, virHashIterator iter, void *data);

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]