[ovs-dev] [PATCH v3 01/10] lib: Add smap_equal().

Russell Bryant rbryant at redhat.com
Tue Jul 21 16:59:11 UTC 2015


Add a method to determine of two smaps are equal (have the exact same
set of key-value pairs).

Signed-off-by: Russell Bryant <rbryant at redhat.com>
---
 lib/smap.c | 34 ++++++++++++++++++++++++++++++++++
 lib/smap.h |  2 ++
 2 files changed, 36 insertions(+)

diff --git a/lib/smap.c b/lib/smap.c
index 7fe3ce4..86bf89d 100644
--- a/lib/smap.c
+++ b/lib/smap.c
@@ -302,6 +302,40 @@ smap_to_json(const struct smap *smap)
     }
     return json;
 }
+
+/* Returns true if the two maps are equal, meaning that they have the same set
+ * of key-value pairs.
+ */
+bool
+smap_equal(const struct smap *smap1, const struct smap *smap2)
+{
+    const struct smap_node **nodes1, **nodes2;
+    size_t i;
+    bool res = true;
+
+    if (smap_count(smap1) != smap_count(smap2)) {
+        return false;
+    }
+
+    nodes1 = smap_sort(smap1);
+    nodes2 = smap_sort(smap2);
+
+    for (i = 0; i < smap_count(smap1); i++) {
+        if (strcmp(nodes1[i]->key, nodes2[i]->key)) {
+            res = false;
+            break;
+        }
+        if (strcmp(nodes1[i]->value, nodes2[i]->value)) {
+            res = false;
+            break;
+        }
+    }
+
+    free(nodes1);
+    free(nodes2);
+
+    return res;
+}
 
 /* Private Helpers. */
 
diff --git a/lib/smap.h b/lib/smap.h
index caf3efc..cac3878 100644
--- a/lib/smap.h
+++ b/lib/smap.h
@@ -67,4 +67,6 @@ const struct smap_node **smap_sort(const struct smap *);
 void smap_from_json(struct smap *, const struct json *);
 struct json *smap_to_json(const struct smap *);
 
+bool smap_equal(const struct smap *, const struct smap *);
+
 #endif /* smap.h */
-- 
2.4.3




More information about the dev mailing list