From: Ben Pfaff Date: Wed, 2 Jan 2019 04:57:17 +0000 (-0800) Subject: string-map: New function string_map_equals(). X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?p=pspp;a=commitdiff_plain;h=672c9b7e1c60763d279ecc781b7bf939b8bab4b4 string-map: New function string_map_equals(). --- diff --git a/src/libpspp/string-map.c b/src/libpspp/string-map.c index ac32a35869..edbd39e924 100644 --- a/src/libpspp/string-map.c +++ b/src/libpspp/string-map.c @@ -357,6 +357,25 @@ string_map_get_values (const struct string_map *map, struct string_set *values) STRING_MAP_FOR_EACH_VALUE (value, node, map) string_set_insert (values, value); } + +/* Returns true if A and B have the same content, false otherwise. */ +bool +string_map_equals (const struct string_map *a, const struct string_map *b) +{ + if (string_map_count (a) != string_map_count (b)) + return false; + + const struct string_map_node *a_node; + STRING_MAP_FOR_EACH_NODE (a_node, a) + { + const struct string_map_node *b_node = string_map_find_node_with_hash ( + b, a_node->key, strlen (a_node->key), a_node->hmap_node.hash); + if (!b_node || strcmp (a_node->value, b_node->value)) + return false; + } + + return true; +} static struct string_map_node * string_map_find_node_with_hash (const struct string_map *map, const char *key, diff --git a/src/libpspp/string-map.h b/src/libpspp/string-map.h index 3abc4ecc65..7f2da1738c 100644 --- a/src/libpspp/string-map.h +++ b/src/libpspp/string-map.h @@ -116,6 +116,8 @@ void string_map_replace_map (struct string_map *, const struct string_map *); void string_map_get_keys (const struct string_map *, struct string_set *); void string_map_get_values (const struct string_map *, struct string_set *); +bool string_map_equals (const struct string_map *, const struct string_map *); + static inline struct string_map_node *string_map_first ( const struct string_map *); static inline struct string_map_node *string_map_next (