Commit 3eacec15 authored by Philip Withnall's avatar Philip Withnall

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's avatarPhilip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=749371
parent e130d2e5
......@@ -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);
......
......@@ -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);
}
......
......@@ -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 {
......
......@@ -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);
}
......
......@@ -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;
......
......@@ -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;
......
......@@ -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);
}
......
......@@ -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);
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment