shared: add nm_utils_hash_values_to_array() helper

This commit is contained in:
Thomas Haller
2019-08-23 18:08:44 +02:00
parent 2667a46874
commit e688e70b37
2 changed files with 43 additions and 0 deletions

View File

@@ -2288,6 +2288,44 @@ nm_utils_hash_keys_to_array (GHashTable *hash,
return keys;
}
gpointer *
nm_utils_hash_values_to_array (GHashTable *hash,
GCompareDataFunc compare_func,
gpointer user_data,
guint *out_len)
{
GHashTableIter iter;
gpointer value;
gpointer *arr;
guint i, len;
if ( !hash
|| (len = g_hash_table_size (hash)) == 0u) {
NM_SET_OUT (out_len, 0);
return NULL;
}
arr = g_new (gpointer, ((gsize) len) + 1);
i = 0;
g_hash_table_iter_init (&iter, hash);
while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &value))
arr[i++] = value;
nm_assert (i == len);
arr[len] = NULL;
if ( len > 1
&& compare_func) {
g_qsort_with_data (arr,
len,
sizeof (gpointer),
compare_func,
user_data);
}
return arr;
}
gboolean
nm_utils_hashtable_same_keys (const GHashTable *a,
const GHashTable *b)

View File

@@ -955,6 +955,11 @@ gpointer *nm_utils_hash_keys_to_array (GHashTable *hash,
gpointer user_data,
guint *out_len);
gpointer *nm_utils_hash_values_to_array (GHashTable *hash,
GCompareDataFunc compare_func,
gpointer user_data,
guint *out_len);
static inline const char **
nm_utils_strdict_get_keys (const GHashTable *hash,
gboolean sorted,