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.
This commit is contained in:
Thomas Haller 2020-11-02 18:27:26 +01:00
parent d52c3b3c94
commit a3aa3725e5
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
5 changed files with 84 additions and 86 deletions

View file

@ -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)

View file

@ -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++) {

View file

@ -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
@ -3856,62 +3912,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

View file

@ -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)

View file

@ -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);
}