smap: Add smap_equal().
authorRussell Bryant <rbryant@redhat.com>
Fri, 31 Jul 2015 17:14:40 +0000 (13:14 -0400)
committerBen Pfaff <blp@nicira.com>
Fri, 31 Jul 2015 22:26:31 +0000 (15:26 -0700)
Add a method to determine of two smaps are equal (have the exact same
set of key-value pairs).

Suggested-by: Ben Pfaff <blp@nicira.com>
Signed-off-by: Russell Bryant <rbryant@redhat.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
lib/smap.c
lib/smap.h

index 7fe3ce4..8865a88 100644 (file)
@@ -302,6 +302,26 @@ 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)
+{
+    if (smap_count(smap1) != smap_count(smap2)) {
+        return false;
+    }
+
+    const struct smap_node *node;
+    SMAP_FOR_EACH (node, smap1) {
+        const char *value2 = smap_get(smap2, node->key);
+        if (!value2 || strcmp(node->value, value2)) {
+            return false;
+        }
+    }
+    return true;
+}
 \f
 /* Private Helpers. */
 
index caf3efc..cac3878 100644 (file)
@@ -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 */