Skip to content

Commit

Permalink
smap: Add smap_equal().
Browse files Browse the repository at this point in the history
Add a method to determine of two smaps are equal (have the exact same
set of key-value pairs).

Suggested-by: Ben Pfaff <[email protected]>
Signed-off-by: Russell Bryant <[email protected]>
Signed-off-by: Ben Pfaff <[email protected]>
  • Loading branch information
russellb authored and blp committed Jul 31, 2015
1 parent 29bae54 commit 7962b7f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/smap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/* Private Helpers. */

Expand Down
2 changes: 2 additions & 0 deletions lib/smap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

0 comments on commit 7962b7f

Please sign in to comment.