Skip to content

ghash: Add g_hash_table_new_similar

Jonas Ådahl requested to merge jadahl/glib:wip/hash-table-from into main

This function creates a new hash table, but inherits the functions used for the hash, comparison, and key/value memory management functions from another hash table.

The primary use case is to implement a behaviour where you maintain a hash table by regenerating it, letting the values not migrated be freed. See the following pseudo code:

GHashTable *ht;

init(GList *resources) {
  ht = g_hash_table_new (g_str_hash, g_str_equal, g_free, g_free);
  for (r in resources)
    g_hash_table_insert (ht, strdup (resource_get_key (r)), create_value (r));

update(GList *resources) {
  GHashTable *new_ht = g_hash_table_new_from (ht);

  for (r in resources) {
    if (g_hash_table_steal_extended (ht, resource_get_key (r), &key, &value))
      g_hash_table_insert (new_ht, key, value);
      g_hash_table_insert (new_ht, strdup (resource_get_key (r)), create_value (r));
  g_hash_table_unref (ht);
  ht = new_ht;

This can be implemented by always using an "update" function to initialize, but it's slightly awkward to not being able to initialize the data structure where data structures tend to be initialized (e.g. in a constructed() vfunc or init() of a GObject), so thought of adding a g_hash_table_new_from() function.

Edited by Jonas Ådahl

Merge request reports