Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in
G
GLib
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 924
    • Issues 924
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
  • Merge Requests 56
    • Merge Requests 56
  • CI / CD
    • CI / CD
    • Pipelines
    • Jobs
    • Schedules
  • Operations
    • Operations
    • Incidents
    • Environments
  • Packages & Registries
    • Packages & Registries
    • Container Registry
  • Analytics
    • Analytics
    • CI / CD
    • Repository
    • Value Stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • GNOME
  • GLib
  • Issues
  • #613

Closed
Open
Opened Oct 17, 2012 by bugzilla-migration@bugzilla-migrationReporter

g_hash_table_lookup_iter

Submitted by Allison (desrt)

Link to original bug (#686326)

Description

It's quite often that you want to perform 'complex' operations on a hashtable.

Things like "lookup and modify if exists, or create otherwise" or "lookup and modify, possibly removing".

GHashTableIter is quite a good way to support this. We already support remove/replace operations on it.

gboolean g_hash_table_lookup_iter       (GHashTable     *table,
                                         constgpointer   key,
                                         GHashTableIter *iter);

gpointer g_hash_table_iter_get_data     (GHashTableIter *iter);

Then we could do stuff like:

  if (g_hash_table_lookup_iter (table, key, &iter))
    {
      mydata = g_hash_table_iter_get_data (&iter);

      ...modify mydata...

      if (mydata->should_be_removed)
        g_hash_table_iter_remove (&iter);
    }

and a similar example with outright replacement.

Now onto the real crack:

One other common pattern that's a bit annoying is "lookup and insert if missing". That one is a bit more complicated to implement because we'd have to support GHashTableIter pointing to non-existent potential future items. I don't really care for that possibility, so we could have instead:

gboolean g_hash_table_insert_or_lookup_iter (GHashTable     *table,
                                             gconstpointer   key,
                                             GCopyFunc       key_copy_func,
                                             gpointer        copy_user_data,
                                             GHashTableIter *iter);

This would either lookup the iter (as above) or, if it didn't exist, copy the key and insert it with NULL data (presumably to be modified later with g_hash_table_iter_replace()).

Edited Oct 17, 2018 by Emmanuele Bassi
Assignee
Assign to
None
Milestone
None
Assign milestone
Time tracking
None
Due date
None
Reference: GNOME/glib#613