[glib] Use hash tables as sets in various places



commit 3eacec158775b4ea956892ac3348923d3667cbdc
Author: Philip Withnall <withnall endlessm com>
Date:   Wed Oct 25 10:53:14 2017 +0100

    Use hash tables as sets in various places
    
    Where we were already treating GHashTables as sets, modify them to use
    the set-specific APIs g_hash_table_add() and g_hash_table_contains(), to
    make that usage more obvious and less prone to being broken.
    
    Heavily based on patches by Garrett Regier <garrettregier gmail com>.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>
    
    https://bugzilla.gnome.org/show_bug.cgi?id=749371

 gio/gdbus-tool.c       |    4 ++--
 gio/gdbusconnection.c  |   14 +++++++-------
 gio/giomodule.c        |    4 ++--
 gio/gsettingsschema.c  |   11 +++++++----
 glib/gscanner.c        |    2 +-
 glib/gstringchunk.c    |    2 +-
 glib/tests/gdatetime.c |    2 +-
 gobject/gobject.c      |    6 +++---
 gobject/gparam.c       |    2 +-
 9 files changed, 25 insertions(+), 22 deletions(-)
---
diff --git a/gio/gdbus-tool.c b/gio/gdbus-tool.c
index 286c507..77863a2 100644
--- a/gio/gdbus-tool.c
+++ b/gio/gdbus-tool.c
@@ -309,7 +309,7 @@ print_names (GDBusConnection *c,
     }
   g_variant_get (result, "(as)", &iter);
   while (g_variant_iter_loop (iter, "s", &str))
-    g_hash_table_insert (name_set, g_strdup (str), NULL);
+    g_hash_table_add (name_set, g_strdup (str));
   g_variant_iter_free (iter);
   g_variant_unref (result);
 
@@ -333,7 +333,7 @@ print_names (GDBusConnection *c,
     }
   g_variant_get (result, "(as)", &iter);
   while (g_variant_iter_loop (iter, "s", &str))
-    g_hash_table_insert (name_set, g_strdup (str), NULL);
+    g_hash_table_add (name_set, g_strdup (str));
   g_variant_iter_free (iter);
   g_variant_unref (result);
 
diff --git a/gio/gdbusconnection.c b/gio/gdbusconnection.c
index b56c5a8..74af984 100644
--- a/gio/gdbusconnection.c
+++ b/gio/gdbusconnection.c
@@ -635,7 +635,7 @@ g_dbus_connection_dispose (GObject *object)
   else
     {
       if (alive_connections != NULL)
-        g_warn_if_fail (g_hash_table_lookup (alive_connections, connection) == NULL);
+        g_warn_if_fail (!g_hash_table_contains (alive_connections, connection));
     }
   CONNECTION_UNLOCK (connection);
   G_UNLOCK (message_bus_lock);
@@ -2226,7 +2226,7 @@ on_worker_message_received (GDBusWorker  *worker,
   gboolean alive;
 
   G_LOCK (message_bus_lock);
-  alive = (g_hash_table_lookup (alive_connections, user_data) != NULL);
+  alive = g_hash_table_contains (alive_connections, user_data);
   if (!alive)
     {
       G_UNLOCK (message_bus_lock);
@@ -2324,7 +2324,7 @@ on_worker_message_about_to_be_sent (GDBusWorker  *worker,
   gboolean alive;
 
   G_LOCK (message_bus_lock);
-  alive = (g_hash_table_lookup (alive_connections, user_data) != NULL);
+  alive = g_hash_table_contains (alive_connections, user_data);
   if (!alive)
     {
       G_UNLOCK (message_bus_lock);
@@ -2397,7 +2397,7 @@ on_worker_closed (GDBusWorker *worker,
   guint old_atomic_flags;
 
   G_LOCK (message_bus_lock);
-  alive = (g_hash_table_lookup (alive_connections, user_data) != NULL);
+  alive = g_hash_table_contains (alive_connections, user_data);
   if (!alive)
     {
       G_UNLOCK (message_bus_lock);
@@ -2572,7 +2572,7 @@ initable_init (GInitable     *initable,
   G_LOCK (message_bus_lock);
   if (alive_connections == NULL)
     alive_connections = g_hash_table_new (g_direct_hash, g_direct_equal);
-  g_hash_table_insert (alive_connections, connection, connection);
+  g_hash_table_add (alive_connections, connection);
   G_UNLOCK (message_bus_lock);
 
   connection->worker = _g_dbus_worker_new (connection->stream,
@@ -4708,8 +4708,8 @@ maybe_add_path (const gchar *path, gsize path_len, const gchar *object_path, GHa
       else
         s = g_strdup (begin);
 
-      if (g_hash_table_lookup (set, s) == NULL)
-        g_hash_table_insert (set, s, GUINT_TO_POINTER (1));
+      if (!g_hash_table_contains (set, s))
+        g_hash_table_add (set, s);
       else
         g_free (s);
     }
diff --git a/gio/giomodule.c b/gio/giomodule.c
index 6937932..4047695 100644
--- a/gio/giomodule.c
+++ b/gio/giomodule.c
@@ -202,14 +202,14 @@ g_io_module_scope_block (GIOModuleScope *scope,
   g_return_if_fail (basename != NULL);
 
   key = g_strdup (basename);
-  g_hash_table_insert (scope->basenames, key, key);
+  g_hash_table_add (scope->basenames, key);
 }
 
 static gboolean
 _g_io_module_scope_contains (GIOModuleScope *scope,
                              const gchar    *basename)
 {
-  return g_hash_table_lookup (scope->basenames, basename) ? TRUE : FALSE;
+  return g_hash_table_contains (scope->basenames, basename);
 }
 
 struct _GIOModule {
diff --git a/gio/gsettingsschema.c b/gio/gsettingsschema.c
index 5fbc2ed..4e12243 100644
--- a/gio/gsettingsschema.c
+++ b/gio/gsettingsschema.c
@@ -785,18 +785,21 @@ g_settings_schema_source_list_schemas (GSettingsSchemaSource   *source,
 
       for (i = 0; list[i]; i++)
         {
-          if (!g_hash_table_lookup (single, list[i]) &&
-              !g_hash_table_lookup (reloc, list[i]))
+          if (!g_hash_table_contains (single, list[i]) &&
+              !g_hash_table_contains (reloc, list[i]))
             {
+              gchar *schema;
               GvdbTable *table;
 
+              schema = g_strdup (list[i]);
+
               table = gvdb_table_get_table (s->table, list[i]);
               g_assert (table != NULL);
 
               if (gvdb_table_has_value (table, ".path"))
-                g_hash_table_insert (single, g_strdup (list[i]), NULL);
+                g_hash_table_add (single, schema);
               else
-                g_hash_table_insert (reloc, g_strdup (list[i]), NULL);
+                g_hash_table_add (reloc, schema);
 
               gvdb_table_unref (table);
             }
diff --git a/glib/gscanner.c b/glib/gscanner.c
index ebcd6e9..389376c 100644
--- a/glib/gscanner.c
+++ b/glib/gscanner.c
@@ -712,7 +712,7 @@ g_scanner_scope_add_symbol (GScanner        *scanner,
              c++;
            }
        }
-      g_hash_table_insert (scanner->symbol_table, key, key);
+      g_hash_table_add (scanner->symbol_table, key);
     }
   else
     key->value = value;
diff --git a/glib/gstringchunk.c b/glib/gstringchunk.c
index 6b7e7b4..524eadc 100644
--- a/glib/gstringchunk.c
+++ b/glib/gstringchunk.c
@@ -248,7 +248,7 @@ g_string_chunk_insert_const (GStringChunk *chunk,
   if (!lookup)
     {
       lookup = g_string_chunk_insert (chunk, string);
-      g_hash_table_insert (chunk->const_table, lookup, lookup);
+      g_hash_table_add (chunk->const_table, lookup);
     }
 
   return lookup;
diff --git a/glib/tests/gdatetime.c b/glib/tests/gdatetime.c
index fe8651e..40b1839 100644
--- a/glib/tests/gdatetime.c
+++ b/glib/tests/gdatetime.c
@@ -343,7 +343,7 @@ test_GDateTime_hash (void)
   h = g_hash_table_new_full (g_date_time_hash, g_date_time_equal,
                              (GDestroyNotify)g_date_time_unref,
                              NULL);
-  g_hash_table_insert (h, g_date_time_new_now_local (), NULL);
+  g_hash_table_add (h, g_date_time_new_now_local ());
   g_hash_table_remove_all (h);
   g_hash_table_destroy (h);
 }
diff --git a/gobject/gobject.c b/gobject/gobject.c
index c332819..99f6ec9 100644
--- a/gobject/gobject.c
+++ b/gobject/gobject.c
@@ -1007,7 +1007,7 @@ g_object_init (GObject            *object,
     {
       G_LOCK (debug_objects);
       debug_objects_count++;
-      g_hash_table_insert (debug_objects_ht, object, object);
+      g_hash_table_add (debug_objects_ht, object);
       G_UNLOCK (debug_objects);
     });
 }
@@ -1062,7 +1062,7 @@ g_object_finalize (GObject *object)
   GOBJECT_IF_DEBUG (OBJECTS,
     {
       G_LOCK (debug_objects);
-      g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
+      g_assert (g_hash_table_contains (debug_objects_ht, object));
       g_hash_table_remove (debug_objects_ht, object);
       debug_objects_count--;
       G_UNLOCK (debug_objects);
@@ -3335,7 +3335,7 @@ g_object_unref (gpointer _object)
            {
              /* catch objects not chaining finalize handlers */
              G_LOCK (debug_objects);
-             g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
+             g_assert (!g_hash_table_contains (debug_objects_ht, object));
              G_UNLOCK (debug_objects);
            });
           g_type_free_instance ((GTypeInstance*) object);
diff --git a/gobject/gparam.c b/gobject/gparam.c
index b7f71f5..0501bb0 100644
--- a/gobject/gparam.c
+++ b/gobject/gparam.c
@@ -948,7 +948,7 @@ g_param_spec_pool_insert (GParamSpecPool *pool,
       g_mutex_lock (&pool->mutex);
       pspec->owner_type = owner_type;
       g_param_spec_ref (pspec);
-      g_hash_table_insert (pool->hash_table, pspec, pspec);
+      g_hash_table_add (pool->hash_table, pspec);
       g_mutex_unlock (&pool->mutex);
     }
   else


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]