From: Ryan Lortie Date: Mon, 3 Dec 2007 09:29:47 +0000 (+0000) Subject: merge more common code into functions. Vastly simplify loop logic in X-Git-Tag: GLIB_2_15_1~235 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e9010a86f763e6b7cb1fd5520985c929339f5523;p=platform%2Fupstream%2Fglib.git merge more common code into functions. Vastly simplify loop logic in 2007-12-03 Ryan Lortie * 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 --- diff --git a/ChangeLog b/ChangeLog index 5b56c73..611bfc5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2007-12-03 Ryan Lortie + + * 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 * Makefile.am: Don't descend into build/. (#500875) diff --git a/glib/ghash.c b/glib/ghash.c index cee7691..8581efd 100644 --- a/glib/ghash.c +++ b/glib/ghash.c @@ -406,6 +406,44 @@ g_hash_table_replace (GHashTable *hash_table, 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: * @hash_table: a #GHashTable. @@ -422,28 +460,9 @@ g_hash_table_replace (GHashTable *hash_table, **/ gboolean g_hash_table_remove (GHashTable *hash_table, - gconstpointer key) + gconstpointer key) { - GHashNode **node, *dest; - - 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; + return g_hash_table_remove_internal (hash_table, key, TRUE); } /** @@ -492,24 +511,7 @@ gboolean g_hash_table_steal (GHashTable *hash_table, gconstpointer key) { - GHashNode **node, *dest; - - 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; + return g_hash_table_remove_internal (hash_table, key, FALSE); } /** @@ -593,46 +595,22 @@ g_hash_table_foreach_remove_or_steal (GHashTable *hash_table, gpointer user_data, gboolean notify) { - GHashNode *node, *prev; - gint i; + GHashNode *node, **node_ptr; guint deleted = 0; - + gint i; + for (i = 0; i < hash_table->size; i++) - { - restart: - - prev = NULL; - - for (node = hash_table->nodes[i]; node; prev = node, node = node->next) - { - if ((* func) (node->key, node->value, user_data)) - { - deleted += 1; - - 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 - { - 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; - } - } - } - } - + for (node_ptr = &hash_table->nodes[i]; (node = *node_ptr) != NULL;) + if ((* func) (node->key, node->value, user_data)) + { + g_hash_table_remove_node (hash_table, &node_ptr, notify); + deleted++; + } + else + node_ptr = &node->next; + G_HASH_TABLE_RESIZE (hash_table); - + return deleted; }