merge more common code into functions. Vastly simplify loop logic in

2007-12-03  Ryan Lortie  <desrt@desrt.ca>

        * glib/ghash.c: merge more common code into functions.  Vastly
        simplify loop logic in g_hash_table_foreach_remove_or_steal().


svn path=/trunk/; revision=6016
This commit is contained in:
Ryan Lortie 2007-12-03 09:29:47 +00:00 committed by Ryan Lortie
parent 6394ae6fdb
commit e9010a86f7
2 changed files with 59 additions and 76 deletions

View File

@ -1,3 +1,8 @@
2007-12-03 Ryan Lortie <desrt@desrt.ca>
* glib/ghash.c: merge more common code into functions. Vastly
simplify loop logic in g_hash_table_foreach_remove_or_steal().
2007-12-01 Behdad Esfahbod <behdad@gnome.org> 2007-12-01 Behdad Esfahbod <behdad@gnome.org>
* Makefile.am: Don't descend into build/. (#500875) * Makefile.am: Don't descend into build/. (#500875)

View File

@ -406,6 +406,44 @@ g_hash_table_replace (GHashTable *hash_table,
return g_hash_table_insert_internal (hash_table, key, value, TRUE); return g_hash_table_insert_internal (hash_table, key, value, TRUE);
} }
static void
g_hash_table_remove_node (GHashTable *hash_table,
GHashNode ***node_ptr_ptr,
gboolean notify)
{
GHashNode **node_ptr, *node;
node_ptr = *node_ptr_ptr;
node = *node_ptr;
*node_ptr = node->next;
g_hash_node_destroy (node,
notify ? hash_table->key_destroy_func : NULL,
notify ? hash_table->value_destroy_func : NULL);
hash_table->nnodes--;
}
static gboolean
g_hash_table_remove_internal (GHashTable *hash_table,
gconstpointer key,
gboolean notify)
{
GHashNode **node_ptr;
g_return_val_if_fail (hash_table != NULL, FALSE);
node_ptr = g_hash_table_lookup_node (hash_table, key, NULL);
if (*node_ptr == NULL)
return FALSE;
g_hash_table_remove_node (hash_table, &node_ptr, FALSE);
G_HASH_TABLE_RESIZE (hash_table);
return TRUE;
}
/** /**
* g_hash_table_remove: * g_hash_table_remove:
* @hash_table: a #GHashTable. * @hash_table: a #GHashTable.
@ -424,26 +462,7 @@ gboolean
g_hash_table_remove (GHashTable *hash_table, g_hash_table_remove (GHashTable *hash_table,
gconstpointer key) gconstpointer key)
{ {
GHashNode **node, *dest; return g_hash_table_remove_internal (hash_table, key, TRUE);
g_return_val_if_fail (hash_table != NULL, FALSE);
node = g_hash_table_lookup_node (hash_table, key, NULL);
if (*node)
{
dest = *node;
(*node) = dest->next;
g_hash_node_destroy (dest,
hash_table->key_destroy_func,
hash_table->value_destroy_func);
hash_table->nnodes--;
G_HASH_TABLE_RESIZE (hash_table);
return TRUE;
}
return FALSE;
} }
/** /**
@ -492,24 +511,7 @@ gboolean
g_hash_table_steal (GHashTable *hash_table, g_hash_table_steal (GHashTable *hash_table,
gconstpointer key) gconstpointer key)
{ {
GHashNode **node, *dest; return g_hash_table_remove_internal (hash_table, key, FALSE);
g_return_val_if_fail (hash_table != NULL, FALSE);
node = g_hash_table_lookup_node (hash_table, key, NULL);
if (*node)
{
dest = *node;
(*node) = dest->next;
g_hash_node_destroy (dest, NULL, NULL);
hash_table->nnodes--;
G_HASH_TABLE_RESIZE (hash_table);
return TRUE;
}
return FALSE;
} }
/** /**
@ -593,43 +595,19 @@ g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
gpointer user_data, gpointer user_data,
gboolean notify) gboolean notify)
{ {
GHashNode *node, *prev; GHashNode *node, **node_ptr;
gint i;
guint deleted = 0; guint deleted = 0;
gint i;
for (i = 0; i < hash_table->size; i++) for (i = 0; i < hash_table->size; i++)
{ for (node_ptr = &hash_table->nodes[i]; (node = *node_ptr) != NULL;)
restart:
prev = NULL;
for (node = hash_table->nodes[i]; node; prev = node, node = node->next)
{
if ((* func) (node->key, node->value, user_data)) if ((* func) (node->key, node->value, user_data))
{ {
deleted += 1; g_hash_table_remove_node (hash_table, &node_ptr, notify);
deleted++;
hash_table->nnodes -= 1;
if (prev)
{
prev->next = node->next;
g_hash_node_destroy (node,
notify ? hash_table->key_destroy_func : NULL,
notify ? hash_table->value_destroy_func : NULL);
node = prev;
} }
else else
{ node_ptr = &node->next;
hash_table->nodes[i] = node->next;
g_hash_node_destroy (node,
notify ? hash_table->key_destroy_func : NULL,
notify ? hash_table->value_destroy_func : NULL);
goto restart;
}
}
}
}
G_HASH_TABLE_RESIZE (hash_table); G_HASH_TABLE_RESIZE (hash_table);