From: Ryan Lortie Date: Wed, 28 Nov 2007 03:40:39 +0000 (+0000) Subject: insert/replace were identical except for a single line. Replace both with X-Git-Url: http://git.openbox.org/?a=commitdiff_plain;h=0adbacbff90cdab1b96e3409b6d510a00916b586;p=dana%2Fcg-glib.git insert/replace were identical except for a single line. Replace both with 2007-11-27 Ryan Lortie * glib/ghash.c (g_hash_table_insert, g_hash_table_replace, g_hash_table_insert_internal): insert/replace were identical except for a single line. Replace both with a common function. svn path=/trunk/; revision=5964 --- diff --git a/ChangeLog b/ChangeLog index 0f7e33d6..7819d2f5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2007-11-27 Ryan Lortie + + * glib/ghash.c (g_hash_table_insert, g_hash_table_replace, + g_hash_table_insert_internal): insert/replace were identical except + for a single line. Replace both with a common function. + 2007-11-27 Alexander Larsson * gio/Makefile.am: diff --git a/glib/ghash.c b/glib/ghash.c index 30dbce5e..f65bebcc 100644 --- a/glib/ghash.c +++ b/glib/ghash.c @@ -322,24 +322,11 @@ g_hash_table_lookup_extended (GHashTable *hash_table, return FALSE; } -/** - * g_hash_table_insert: - * @hash_table: a #GHashTable. - * @key: a key to insert. - * @value: the value to associate with the key. - * - * Inserts a new key and value into a #GHashTable. - * - * If the key already exists in the #GHashTable its current value is replaced - * with the new value. If you supplied a @value_destroy_func when creating the - * #GHashTable, the old value is freed using that function. If you supplied - * a @key_destroy_func when creating the #GHashTable, the passed key is freed - * using that function. - **/ -void -g_hash_table_insert (GHashTable *hash_table, - gpointer key, - gpointer value) +static void +g_hash_table_insert_internal (GHashTable *hash_table, + gpointer key, + gpointer value, + gboolean keep_new_key) { GHashNode **node; guint key_hash; @@ -351,14 +338,13 @@ g_hash_table_insert (GHashTable *hash_table, if (*node) { - /* do not reset node->key in this place, keeping - * the old key is the intended behaviour. - * g_hash_table_replace() can be used instead. - */ - - /* free the passed key */ if (hash_table->key_destroy_func) - hash_table->key_destroy_func (key); + { + if (keep_new_key) + hash_table->key_destroy_func ((*node)->key); + else + hash_table->key_destroy_func (key); + } if (hash_table->value_destroy_func) hash_table->value_destroy_func ((*node)->value); @@ -373,6 +359,28 @@ g_hash_table_insert (GHashTable *hash_table, } } +/** + * g_hash_table_insert: + * @hash_table: a #GHashTable. + * @key: a key to insert. + * @value: the value to associate with the key. + * + * Inserts a new key and value into a #GHashTable. + * + * If the key already exists in the #GHashTable its current value is replaced + * with the new value. If you supplied a @value_destroy_func when creating the + * #GHashTable, the old value is freed using that function. If you supplied + * a @key_destroy_func when creating the #GHashTable, the passed key is freed + * using that function. + **/ +void +g_hash_table_insert (GHashTable *hash_table, + gpointer key, + gpointer value) +{ + return g_hash_table_insert_internal (hash_table, key, value, FALSE); +} + /** * g_hash_table_replace: * @hash_table: a #GHashTable. @@ -391,31 +399,7 @@ g_hash_table_replace (GHashTable *hash_table, gpointer key, gpointer value) { - GHashNode **node; - guint key_hash; - - g_return_if_fail (hash_table != NULL); - g_return_if_fail (hash_table->ref_count > 0); - - node = g_hash_table_lookup_node (hash_table, key, &key_hash); - - if (*node) - { - if (hash_table->key_destroy_func) - hash_table->key_destroy_func ((*node)->key); - - if (hash_table->value_destroy_func) - hash_table->value_destroy_func ((*node)->value); - - (*node)->key = key; - (*node)->value = value; - } - else - { - *node = g_hash_node_new (key, value, key_hash); - hash_table->nnodes++; - G_HASH_TABLE_RESIZE (hash_table); - } + return g_hash_table_insert_internal (hash_table, key, value, TRUE); } /**