summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2020-11-02 18:27:26 +0100
committerThomas Haller <thaller@redhat.com>2020-11-09 17:25:25 +0100
commita3aa3725e5faf707d76eaa1009a71152c37564dc (patch)
tree351e3decf2032594367f5010d977c23778c03a2d
parentd52c3b3c94507c5dd2738c8d16782735886a53fe (diff)
downloadNetworkManager-a3aa3725e5faf707d76eaa1009a71152c37564dc.tar.gz
shared,all: cleanup nm_utils_hashtable_equal*() functions
We have: - nm_utils_hashtable_cmp(): this does a full cmp of two hash tables, with the intent to provide a stable sort order. It thus takes a GCompareDataFunc() argument. - nm_utils_hashtable_cmp_equal(): this is like nm_utils_hashtable_cmp(), except that the caller won't get a compare value, only a boolean value that indicates equality. This was previously called nm_utils_hashtable_equal(). - nm_utils_hashtable_equal(): this takes a GEqualFunc function for comparing the values for equality. It takes thus a different kind of predicate, but otherwise is similar to nm_utils_hashtable_cmp_equal(). This was previously called nm_utils_hash_table_equal(). Unify the naming of these functions.
-rw-r--r--libnm-core/nm-setting-user.c10
-rw-r--r--libnm-core/nm-setting.c8
-rw-r--r--shared/nm-glib-aux/nm-shared-utils.c124
-rw-r--r--shared/nm-glib-aux/nm-shared-utils.h18
-rw-r--r--shared/nm-glib-aux/tests/test-shared-general.c10
5 files changed, 84 insertions, 86 deletions
diff --git a/libnm-core/nm-setting-user.c b/libnm-core/nm-setting-user.c
index dd8600f752..ad3010e65c 100644
--- a/libnm-core/nm-setting-user.c
+++ b/libnm-core/nm-setting-user.c
@@ -416,11 +416,11 @@ compare_property(const NMSettInfoSetting *sett_info,
priv = NM_SETTING_USER_GET_PRIVATE(NM_SETTING_USER(set_a));
pri2 = NM_SETTING_USER_GET_PRIVATE(NM_SETTING_USER(set_b));
- return nm_utils_hash_table_equal(priv->data, pri2->data, TRUE, g_str_equal)
- && nm_utils_hash_table_equal(priv->data_invalid,
- pri2->data_invalid,
- TRUE,
- g_str_equal);
+ return nm_utils_hashtable_equal(priv->data, pri2->data, TRUE, g_str_equal)
+ && nm_utils_hashtable_equal(priv->data_invalid,
+ pri2->data_invalid,
+ TRUE,
+ g_str_equal);
}
return NM_SETTING_CLASS(nm_setting_user_parent_class)
diff --git a/libnm-core/nm-setting.c b/libnm-core/nm-setting.c
index 0f62128908..28f1fedb06 100644
--- a/libnm-core/nm-setting.c
+++ b/libnm-core/nm-setting.c
@@ -1481,10 +1481,10 @@ _nm_setting_compare(NMConnection * con_a,
GenData *a_gendata = _gendata_hash(a, FALSE);
GenData *b_gendata = _gendata_hash(b, FALSE);
- return nm_utils_hash_table_equal(a_gendata ? a_gendata->hash : NULL,
- b_gendata ? b_gendata->hash : NULL,
- TRUE,
- g_variant_equal);
+ return nm_utils_hashtable_equal(a_gendata ? a_gendata->hash : NULL,
+ b_gendata ? b_gendata->hash : NULL,
+ TRUE,
+ g_variant_equal);
}
for (i = 0; i < sett_info->property_infos_len; i++) {
diff --git a/shared/nm-glib-aux/nm-shared-utils.c b/shared/nm-glib-aux/nm-shared-utils.c
index 060631a84a..b46e0c486e 100644
--- a/shared/nm-glib-aux/nm-shared-utils.c
+++ b/shared/nm-glib-aux/nm-shared-utils.c
@@ -3292,6 +3292,62 @@ nm_utils_hash_values_to_array(GHashTable * hash,
return arr;
}
+/*****************************************************************************/
+
+/**
+ * nm_utils_hashtable_equal:
+ * @a: one #GHashTable
+ * @b: other #GHashTable
+ * @treat_null_as_empty: if %TRUE, when either @a or @b is %NULL, it is
+ * treated like an empty hash. It means, a %NULL hash will compare equal
+ * to an empty hash.
+ * @equal_func: the equality function, for comparing the values.
+ * If %NULL, the values are not compared. In that case, the function
+ * only checks, if both dictionaries have the same keys -- according
+ * to @b's key equality function.
+ * Note that the values of @a will be passed as first argument
+ * to @equal_func.
+ *
+ * Compares two hash tables, whether they have equal content.
+ * This only makes sense, if @a and @b have the same key types and
+ * the same key compare-function.
+ *
+ * Returns: %TRUE, if both dictionaries have the same content.
+ */
+gboolean
+nm_utils_hashtable_equal(const GHashTable *a,
+ const GHashTable *b,
+ gboolean treat_null_as_empty,
+ GEqualFunc equal_func)
+{
+ guint n;
+ GHashTableIter iter;
+ gconstpointer key, v_a, v_b;
+
+ if (a == b)
+ return TRUE;
+ if (!treat_null_as_empty) {
+ if (!a || !b)
+ return FALSE;
+ }
+
+ n = a ? g_hash_table_size((GHashTable *) a) : 0;
+ if (n != (b ? g_hash_table_size((GHashTable *) b) : 0))
+ return FALSE;
+
+ if (n > 0) {
+ g_hash_table_iter_init(&iter, (GHashTable *) a);
+ while (g_hash_table_iter_next(&iter, (gpointer *) &key, (gpointer *) &v_a)) {
+ if (!g_hash_table_lookup_extended((GHashTable *) b, key, NULL, (gpointer *) &v_b))
+ return FALSE;
+ if (equal_func && !equal_func(v_a, v_b))
+ return FALSE;
+ }
+ }
+
+ return TRUE;
+}
+
static gboolean
_utils_hashtable_equal(GHashTable * hash_a,
GHashTable * hash_b,
@@ -3331,7 +3387,7 @@ _utils_hashtable_equal(GHashTable * hash_a,
}
/**
- * nm_utils_hashtable_equal:
+ * nm_utils_hashtable_cmp_equal:
* @a: (allow-none): the hash table or %NULL
* @b: (allow-none): the other hash table or %NULL
* @cmp_values: (allow-none): if %NULL, only the keys
@@ -3346,10 +3402,10 @@ _utils_hashtable_equal(GHashTable * hash_a,
* @cmp_values is given) all values are the same.
*/
gboolean
-nm_utils_hashtable_equal(const GHashTable *a,
- const GHashTable *b,
- GCompareDataFunc cmp_values,
- gpointer user_data)
+nm_utils_hashtable_cmp_equal(const GHashTable *a,
+ const GHashTable *b,
+ GCompareDataFunc cmp_values,
+ gpointer user_data)
{
GHashTable *hash_a = (GHashTable *) a;
GHashTable *hash_b = (GHashTable *) b;
@@ -3404,7 +3460,7 @@ _hashtable_cmp_func(gconstpointer a, gconstpointer b, gpointer user_data)
* @a: (allow-none): the hash to compare. May be %NULL.
* @b: (allow-none): the other hash to compare. May be %NULL.
* @do_fast_precheck: if %TRUE, assume that the hashes are equal
- * and that it is worth calling nm_utils_hashtable_equal() first.
+ * and that it is worth calling nm_utils_hashtable_cmp_equal() first.
* That requires, that both hashes have the same equals function
* which is compatible with the @cmp_keys function.
* @cmp_keys: the compare function for keys. Usually, the hash/equal function
@@ -3857,62 +3913,6 @@ nm_utils_array_find_binary_search(gconstpointer list,
/*****************************************************************************/
/**
- * nm_utils_hash_table_equal:
- * @a: one #GHashTable
- * @b: other #GHashTable
- * @treat_null_as_empty: if %TRUE, when either @a or @b is %NULL, it is
- * treated like an empty hash. It means, a %NULL hash will compare equal
- * to an empty hash.
- * @equal_func: the equality function, for comparing the values.
- * If %NULL, the values are not compared. In that case, the function
- * only checks, if both dictionaries have the same keys -- according
- * to @b's key equality function.
- * Note that the values of @a will be passed as first argument
- * to @equal_func.
- *
- * Compares two hash tables, whether they have equal content.
- * This only makes sense, if @a and @b have the same key types and
- * the same key compare-function.
- *
- * Returns: %TRUE, if both dictionaries have the same content.
- */
-gboolean
-nm_utils_hash_table_equal(const GHashTable *a,
- const GHashTable *b,
- gboolean treat_null_as_empty,
- GEqualFunc equal_func)
-{
- guint n;
- GHashTableIter iter;
- gconstpointer key, v_a, v_b;
-
- if (a == b)
- return TRUE;
- if (!treat_null_as_empty) {
- if (!a || !b)
- return FALSE;
- }
-
- n = a ? g_hash_table_size((GHashTable *) a) : 0;
- if (n != (b ? g_hash_table_size((GHashTable *) b) : 0))
- return FALSE;
-
- if (n > 0) {
- g_hash_table_iter_init(&iter, (GHashTable *) a);
- while (g_hash_table_iter_next(&iter, (gpointer *) &key, (gpointer *) &v_a)) {
- if (!g_hash_table_lookup_extended((GHashTable *) b, key, NULL, (gpointer *) &v_b))
- return FALSE;
- if (equal_func && !equal_func(v_a, v_b))
- return FALSE;
- }
- }
-
- return TRUE;
-}
-
-/*****************************************************************************/
-
-/**
* nm_utils_get_start_time_for_pid:
* @pid: the process identifier
* @out_state: return the state character, like R, S, Z. See `man 5 proc`.
diff --git a/shared/nm-glib-aux/nm-shared-utils.h b/shared/nm-glib-aux/nm-shared-utils.h
index a7e894de89..cf01169d1c 100644
--- a/shared/nm-glib-aux/nm-shared-utils.h
+++ b/shared/nm-glib-aux/nm-shared-utils.h
@@ -1487,13 +1487,18 @@ nm_utils_strdict_get_keys(const GHashTable *hash, gboolean sorted, guint *out_le
gboolean nm_utils_hashtable_equal(const GHashTable *a,
const GHashTable *b,
- GCompareDataFunc cmp_values,
- gpointer user_data);
+ gboolean treat_null_as_empty,
+ GEqualFunc equal_func);
+
+gboolean nm_utils_hashtable_cmp_equal(const GHashTable *a,
+ const GHashTable *b,
+ GCompareDataFunc cmp_values,
+ gpointer user_data);
static inline gboolean
nm_utils_hashtable_same_keys(const GHashTable *a, const GHashTable *b)
{
- return nm_utils_hashtable_equal(a, b, NULL, NULL);
+ return nm_utils_hashtable_cmp_equal(a, b, NULL, NULL);
}
int nm_utils_hashtable_cmp(const GHashTable *a,
@@ -1700,13 +1705,6 @@ gssize nm_utils_array_find_binary_search(gconstpointer list,
/*****************************************************************************/
-gboolean nm_utils_hash_table_equal(const GHashTable *a,
- const GHashTable *b,
- gboolean treat_null_as_empty,
- GEqualFunc equal_func);
-
-/*****************************************************************************/
-
void _nm_utils_strv_sort(const char **strv, gssize len);
#define nm_utils_strv_sort(strv, len) _nm_utils_strv_sort(NM_CAST_STRV_MC(strv), len)
diff --git a/shared/nm-glib-aux/tests/test-shared-general.c b/shared/nm-glib-aux/tests/test-shared-general.c
index dbb8ed275e..13dd26f240 100644
--- a/shared/nm-glib-aux/tests/test-shared-general.c
+++ b/shared/nm-glib-aux/tests/test-shared-general.c
@@ -1190,8 +1190,8 @@ test_utils_hashtable_cmp(void)
}
g_assert(nm_utils_hashtable_same_keys(h1, h2));
- g_assert(nm_utils_hashtable_equal(h1, h2, NULL, NULL));
- g_assert(nm_utils_hashtable_equal(h1, h2, func_val_cmp, NULL));
+ g_assert(nm_utils_hashtable_cmp_equal(h1, h2, NULL, NULL));
+ g_assert(nm_utils_hashtable_cmp_equal(h1, h2, func_val_cmp, NULL));
g_assert(nm_utils_hashtable_cmp(h1, h2, FALSE, func_key_cmp, NULL, NULL) == 0);
g_assert(nm_utils_hashtable_cmp(h1, h2, TRUE, func_key_cmp, NULL, NULL) == 0);
g_assert(nm_utils_hashtable_cmp(h1, h2, FALSE, func_key_cmp, func_val_cmp, NULL) == 0);
@@ -1221,16 +1221,16 @@ again:
if (has_same_keys) {
g_assert(nm_utils_hashtable_same_keys(h1, h2));
- g_assert(nm_utils_hashtable_equal(h1, h2, NULL, NULL));
+ g_assert(nm_utils_hashtable_cmp_equal(h1, h2, NULL, NULL));
g_assert(nm_utils_hashtable_cmp(h1, h2, FALSE, func_key_cmp, NULL, NULL) == 0);
g_assert(nm_utils_hashtable_cmp(h1, h2, TRUE, func_key_cmp, NULL, NULL) == 0);
} else {
g_assert(!nm_utils_hashtable_same_keys(h1, h2));
- g_assert(!nm_utils_hashtable_equal(h1, h2, NULL, NULL));
+ g_assert(!nm_utils_hashtable_cmp_equal(h1, h2, NULL, NULL));
g_assert(nm_utils_hashtable_cmp(h1, h2, FALSE, func_key_cmp, NULL, NULL) != 0);
g_assert(nm_utils_hashtable_cmp(h1, h2, TRUE, func_key_cmp, NULL, NULL) != 0);
}
- g_assert(!nm_utils_hashtable_equal(h1, h2, func_val_cmp, NULL));
+ g_assert(!nm_utils_hashtable_cmp_equal(h1, h2, func_val_cmp, NULL));
g_assert(nm_utils_hashtable_cmp(h1, h2, FALSE, func_key_cmp, func_val_cmp, NULL) != 0);
g_assert(nm_utils_hashtable_cmp(h1, h2, TRUE, func_key_cmp, func_val_cmp, NULL) != 0);
}