insert/replace were identical except for a single line. Replace both with
authorRyan Lortie <desrt@desrt.ca>
Wed, 28 Nov 2007 03:40:39 +0000 (03:40 +0000)
committerRyan Lortie <ryanl@src.gnome.org>
Wed, 28 Nov 2007 03:40:39 +0000 (03:40 +0000)
2007-11-27  Ryan Lortie  <desrt@desrt.ca>

        * 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

ChangeLog
glib/ghash.c

index 0f7e33d..7819d2f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2007-11-27  Ryan Lortie  <desrt@desrt.ca>
+
+       * 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  <alexl@redhat.com>
 
         * gio/Makefile.am:
index 30dbce5..f65bebc 100644 (file)
@@ -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);
@@ -374,6 +360,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.
  * @key: a key to insert.
@@ -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);
 }
 
 /**