GNOME Bugzilla – Bug 795302
Add g_hash_table_steal_extended() API
Last modified: 2018-05-08 12:16:33 UTC
g_hash_table_steal() is useless without having looked up the value beforehand using g_hash_table_lookup(). In the interests of ease of API use, and reducing the number of hash table lookups needed, I suggest we add a new API like: gboolean g_hash_table_steal_extended (GHashTable *ht, gconstpointer lookup_key, gpointer *stolen_key, gpointer *stolen_value); It is essentially a combination of g_hash_table_lookup_extended() and g_hash_table_steal(), and would allow replacing this code: gpointer key, value; if (g_hash_table_lookup_extended (hash_table, lookup_key, &key, &value)) { g_autoptr(KeyType) stolen_key = key; g_autoptr(ValueType) stolen_value = value; g_hash_table_steal (hash_table, key); /* do something with the stolen @key and @value */ } with g_autoptr(KeyType) stolen_key = NULL; g_autoptr(ValueType) stolen_value = NULL; if (g_hash_table_steal_extended (hash_table, lookup_key, &stolen_key, &stolen_value)) { /* do something with the stolen @key and @value */ }
Created attachment 370992 [details] [review] ghash: Add g_hash_table_steal_extended() This is a combination of g_hash_table_lookup_extended() and g_hash_table_steal(), so that users can combine the two to reduce code and eliminate a pointless second hash table lookup by g_hash_table_steal(). Signed-off-by: Philip Withnall <withnall@endlessm.com>
Review of attachment 370992 [details] [review]: Looks like a good addition to me ::: glib/tests/hash.c @@ +1214,3 @@ + g_clear_pointer (&stolen_key, g_free); + g_clear_pointer (&stolen_value, g_free); + g_hash_table_insert (hash, g_strdup ("b"), g_strdup ("B")); Should probably move the test for the size of the hash table after the first steal_extended(); or, maybe, do something like: ``` g_hash_table_insert (hash, g_strdup ("f"), g_strdup ("F")); int old_size = g_hash_table_size (hash); g_assert_true (g_hash_table_steal_extended (hash, "a", ... g_assert_cmpint (old_size, !=, g_hash_table_size (hash)); ```
Created attachment 371792 [details] [review] ghash: Add g_hash_table_steal_extended() This is a combination of g_hash_table_lookup_extended() and g_hash_table_steal(), so that users can combine the two to reduce code and eliminate a pointless second hash table lookup by g_hash_table_steal(). Signed-off-by: Philip Withnall <withnall@endlessm.com>
Updated to add an additional hash table size check, thanks.
Review of attachment 371792 [details] [review]: Looks good!
Ta! Attachment 371792 [details] pushed as 6acece5 - ghash: Add g_hash_table_steal_extended()