Cygwin support contributed by Stefan Ondrejicka <ondrej@idata.sk>.
[platform/upstream/glib.git] / ghash.c
diff --git a/ghash.c b/ghash.c
index 7240363..8ac9ff2 100644 (file)
--- a/ghash.c
+++ b/ghash.c
@@ -2,25 +2,36 @@
  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
  *
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
+ * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
- * Library General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU Library General Public
+ * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
 
+/*
+ * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
+ * file for a list of people on the GLib Team.  See the ChangeLog
+ * files for a list of changes.  These files are distributed with
+ * GLib at ftp://ftp.gtk.org/pub/gtk/. 
+ */
+
 /* 
  * MT safe
  */
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
 #include "glib.h"
 
 
@@ -41,10 +52,9 @@ struct _GHashTable
 {
   gint size;
   gint nnodes;
-  guint frozen;
   GHashNode **nodes;
   GHashFunc hash_func;
-  GCompareFunc key_compare_func;
+  GEqualFunc key_equal_func;
 };
 
 
@@ -57,7 +67,7 @@ static void           g_hash_node_destroy      (GHashNode     *hash_node);
 static void            g_hash_nodes_destroy     (GHashNode     *hash_node);
 
 
-static G_LOCK_DEFINE(g_hash_global);
+G_LOCK_DEFINE_STATIC (g_hash_global);
 
 static GMemChunk *node_mem_chunk = NULL;
 static GHashNode *node_free_list = NULL;
@@ -65,7 +75,7 @@ static GHashNode *node_free_list = NULL;
 
 GHashTable*
 g_hash_table_new (GHashFunc    hash_func,
-                 GCompareFunc key_compare_func)
+                 GEqualFunc   key_equal_func)
 {
   GHashTable *hash_table;
   guint i;
@@ -73,9 +83,8 @@ g_hash_table_new (GHashFunc    hash_func,
   hash_table = g_new (GHashTable, 1);
   hash_table->size = HASH_TABLE_MIN_SIZE;
   hash_table->nnodes = 0;
-  hash_table->frozen = FALSE;
   hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
-  hash_table->key_compare_func = key_compare_func;
+  hash_table->key_equal_func = key_equal_func;
   hash_table->nodes = g_new (GHashNode*, hash_table->size);
   
   for (i = 0; i < hash_table->size; i++)
@@ -109,11 +118,11 @@ g_hash_table_lookup_node (GHashTable      *hash_table,
   
   /* Hash table lookup needs to be fast.
    *  We therefore remove the extra conditional of testing
-   *  whether to call the key_compare_func or not from
+   *  whether to call the key_equal_func or not from
    *  the inner loop.
    */
-  if (hash_table->key_compare_func)
-    while (*node && !(*hash_table->key_compare_func) ((*node)->key, key))
+  if (hash_table->key_equal_func)
+    while (*node && !(*hash_table->key_equal_func) ((*node)->key, key))
       node = &(*node)->next;
   else
     while (*node && (*node)->key != key)
@@ -160,29 +169,32 @@ g_hash_table_insert (GHashTable *hash_table,
     {
       *node = g_hash_node_new (key, value);
       hash_table->nnodes++;
-      if (!hash_table->frozen)
-       g_hash_table_resize (hash_table);
+      g_hash_table_resize (hash_table);
     }
 }
 
-void
-g_hash_table_remove (GHashTable             *hash_table,
-                    gconstpointer    key)
+gboolean
+g_hash_table_remove (GHashTable          *hash_table,
+                    gconstpointer key)
 {
   GHashNode **node, *dest;
   
-  g_return_if_fail (hash_table != NULL);
+  g_return_val_if_fail (hash_table != NULL, FALSE);
   
-  while (*(node = g_hash_table_lookup_node (hash_table, key)))
+  node = g_hash_table_lookup_node (hash_table, key);
+  if (*node)
     {
       dest = *node;
       (*node) = dest->next;
       g_hash_node_destroy (dest);
       hash_table->nnodes--;
-    }
   
-  if (!hash_table->frozen)
-    g_hash_table_resize (hash_table);
+      g_hash_table_resize (hash_table);
+
+      return TRUE;
+    }
+
+  return FALSE;
 }
 
 gboolean
@@ -212,29 +224,30 @@ g_hash_table_lookup_extended (GHashTable  *hash_table,
 void
 g_hash_table_freeze (GHashTable *hash_table)
 {
-  g_return_if_fail (hash_table != NULL);
-  
-  hash_table->frozen++;
+#ifdef G_ENABLE_DEBUG
+  static gboolean first_call = TRUE;
+
+  if (first_call)
+    {
+      g_warning("g_hash_table_freeze and g_hash_table_thaw are deprecated.");
+      first_call = FALSE;
+    }
+#endif /* G_ENABLE_DEBUG */
 }
 
 void
 g_hash_table_thaw (GHashTable *hash_table)
 {
-  g_return_if_fail (hash_table != NULL);
-  
-  if (hash_table->frozen)
-    if (!(--hash_table->frozen))
-      g_hash_table_resize (hash_table);
 }
 
-gint
+guint
 g_hash_table_foreach_remove (GHashTable        *hash_table,
                             GHRFunc     func,
                             gpointer    user_data)
 {
   GHashNode *node, *prev;
   guint i;
-  gint deleted = 0;
+  guint deleted = 0;
   
   g_return_val_if_fail (hash_table != NULL, 0);
   g_return_val_if_fail (func != NULL, 0);
@@ -269,8 +282,7 @@ g_hash_table_foreach_remove (GHashTable     *hash_table,
        }
     }
   
-  if (!hash_table->frozen)
-    g_hash_table_resize (hash_table);
+  g_hash_table_resize (hash_table);
   
   return deleted;
 }
@@ -292,7 +304,7 @@ g_hash_table_foreach (GHashTable *hash_table,
 }
 
 /* Returns the number of elements contained in the hash table. */
-gint
+guint
 g_hash_table_size (GHashTable *hash_table)
 {
   g_return_val_if_fail (hash_table != NULL, 0);
@@ -320,16 +332,15 @@ g_hash_table_resize (GHashTable *hash_table)
   new_size = CLAMP(g_spaced_primes_closest (hash_table->nnodes),
                   HASH_TABLE_MIN_SIZE,
                   HASH_TABLE_MAX_SIZE);
-  new_nodes = g_new (GHashNode*, new_size);
-  
-  for (i = 0; i < new_size; i++)
-    new_nodes[i] = NULL;
+  new_nodes = g_new0 (GHashNode*, new_size);
   
   for (i = 0; i < hash_table->size; i++)
     for (node = hash_table->nodes[i]; node; node = next)
       {
        next = node->next;
+
        hash_val = (* hash_table->hash_func) (node->key) % new_size;
+
        node->next = new_nodes[hash_val];
        new_nodes[hash_val] = node;
       }
@@ -345,7 +356,7 @@ g_hash_node_new (gpointer key,
 {
   GHashNode *hash_node;
   
-  g_lock (g_hash_global);
+  G_LOCK (g_hash_global);
   if (node_free_list)
     {
       hash_node = node_free_list;
@@ -360,7 +371,7 @@ g_hash_node_new (gpointer key,
       
       hash_node = g_chunk_new (GHashNode, node_mem_chunk);
     }
-  g_unlock (g_hash_global);
+  G_UNLOCK (g_hash_global);
   
   hash_node->key = key;
   hash_node->value = value;
@@ -372,27 +383,42 @@ g_hash_node_new (gpointer key,
 static void
 g_hash_node_destroy (GHashNode *hash_node)
 {
-  g_lock (g_hash_global);
+
+#ifdef ENABLE_GC_FRIENDLY
+  hash_node->key = NULL;
+  hash_node->value = NULL;
+#endif /* ENABLE_GC_FRIENDLY */
+
+  G_LOCK (g_hash_global);
   hash_node->next = node_free_list;
   node_free_list = hash_node;
-  g_unlock (g_hash_global);
+  G_UNLOCK (g_hash_global);
 }
 
 static void
 g_hash_nodes_destroy (GHashNode *hash_node)
 {
-  GHashNode *node;
-  
-  if (!hash_node)
-    return;
-  
-  node = hash_node;
-  
-  while (node->next)
-    node = node->next;
+  if (hash_node)
+    {
+      GHashNode *node = hash_node;
   
-  g_lock (g_hash_global);
-  node->next = node_free_list;
-  node_free_list = hash_node;
-  g_unlock (g_hash_global);
+      while (node->next)
+       {
+#ifdef ENABLE_GC_FRIENDLY
+         node->key = NULL;
+         node->value = NULL;
+#endif /* ENABLE_GC_FRIENDLY */
+         node = node->next;
+       }
+
+#ifdef ENABLE_GC_FRIENDLY
+      node->key = NULL;
+      node->value = NULL;
+#endif /* ENABLE_GC_FRIENDLY */
+      G_LOCK (g_hash_global);
+      node->next = node_free_list;
+      node_free_list = hash_node;
+      G_UNLOCK (g_hash_global);
+    }
 }