Author: mprobst
Date: 2007-06-04 16:37:43 -0400 (Mon, 04 Jun 2007)
New Revision: 78593

Added:
   trunk/mono/mono/utils/mono-internal-hash.c
   trunk/mono/mono/utils/mono-internal-hash.h
Modified:
   trunk/mono/mono/utils/ChangeLog
   trunk/mono/mono/utils/Makefile.am
Log:
2007-06-04  Mark Probst  <[EMAIL PROTECTED]>

        * Makefile.am:
        * mono-internal-hash.c, mono-internal-hash.h: Hash table which
        uses the value themselves as nodes, hence doesn't have to allocate
        nodes.



Modified: trunk/mono/mono/utils/ChangeLog
===================================================================
--- trunk/mono/mono/utils/ChangeLog     2007-06-04 20:37:41 UTC (rev 78592)
+++ trunk/mono/mono/utils/ChangeLog     2007-06-04 20:37:43 UTC (rev 78593)
@@ -1,3 +1,10 @@
+2007-06-04  Mark Probst  <[EMAIL PROTECTED]>
+
+       * Makefile.am:
+       * mono-internal-hash.c, mono-internal-hash.h: Hash table which
+       uses the value themselves as nodes, hence doesn't have to allocate
+       nodes.
+
 2007-05-24  Jonathan Chambers  <[EMAIL PROTECTED]>
 
        * mono-path.c: Add configure checks for header files.

Modified: trunk/mono/mono/utils/Makefile.am
===================================================================
--- trunk/mono/mono/utils/Makefile.am   2007-06-04 20:37:41 UTC (rev 78592)
+++ trunk/mono/mono/utils/Makefile.am   2007-06-04 20:37:43 UTC (rev 78593)
@@ -18,6 +18,7 @@
        mono-compiler.h \
        mono-dl.c       \
        mono-dl.h       \
+       mono-internal-hash.c    \
        monobitset.c    \
        mono-math.c  \
        mono-mmap.c  \
@@ -38,6 +39,7 @@
        mono-codeman.h  \
        mono-counters.h \
        mono-digest.h   \
+       mono-internal-hash.h    \
        mono-logger.h   \
        mono-hash.h     \
        mono-math.h     \

Added: trunk/mono/mono/utils/mono-internal-hash.c
===================================================================
--- trunk/mono/mono/utils/mono-internal-hash.c  2007-06-04 20:37:41 UTC (rev 
78592)
+++ trunk/mono/mono/utils/mono-internal-hash.c  2007-06-04 20:37:43 UTC (rev 
78593)
@@ -0,0 +1,123 @@
+/*
+ * mono-internal-hash.c: A hash table which uses the values themselves as 
nodes.
+ *
+ * Author:
+ *   Mark Probst ([EMAIL PROTECTED])
+ *
+ * (C) 2007 Novell, Inc.
+ *
+ */
+
+#include <config.h>
+#include <glib.h>
+#include <mono/utils/mono-compiler.h>
+#include <mono/utils/mono-internal-hash.h>
+
+#define MIN_SIZE       11
+#define HASH(k,f,s)    ((f)((k)) % (s))
+
+void
+mono_internal_hash_table_init (MonoInternalHashTable *table,
+                              GHashFunc hash_func,
+                              MonoInternalHashKeyExtractFunc key_extract,
+                              MonoInternalHashNextValueFunc next_value)
+{
+       table->hash_func = hash_func;
+       table->key_extract = key_extract;
+       table->next_value = next_value;
+
+       table->size = MIN_SIZE;
+       table->num_entries = 0;
+       table->table = g_new0 (gpointer, table->size);
+}
+
+void
+mono_internal_hash_table_destroy (MonoInternalHashTable *table)
+{
+       g_free (table->table);
+       table->table = NULL;
+}
+
+gpointer
+mono_internal_hash_table_lookup (MonoInternalHashTable *table, gpointer key)
+{
+       gpointer value;
+
+       g_assert (table->table != NULL);
+
+       for (value = table->table [HASH (key, table->hash_func, table->size)];
+            value != NULL;
+            value = *(table->next_value (value))) {
+               if (table->key_extract (value) == key)
+                       return value;
+       }
+       return NULL;
+}
+
+static void
+resize_if_needed (MonoInternalHashTable *table)
+{
+       gpointer *new_table;
+       gint new_size;
+       gint i;
+
+       if (table->num_entries < table->size * 3)
+               return;
+
+       new_size = g_spaced_primes_closest (table->num_entries);
+       new_table = g_new0 (gpointer, new_size);
+
+       for (i = 0; i < table->size; ++i) {
+               while (table->table[i] != NULL) {
+                       gpointer value;
+                       gint hash;
+
+                       value = table->table [i];
+                       table->table [i] = *(table->next_value (value));
+
+                       hash = HASH (table->key_extract (value), 
table->hash_func, new_size);
+                       *(table->next_value (value)) = new_table [hash];
+                       new_table [hash] = value;
+               }
+       }
+
+       g_free (table->table);
+
+       table->size = new_size;
+       table->table = new_table;
+}
+
+void
+mono_internal_hash_table_insert (MonoInternalHashTable *table,
+                                gpointer key, gpointer value)
+{
+       gint hash = HASH (key, table->hash_func, table->size);
+
+       g_assert(table->key_extract(value) == key);
+       g_assert (mono_internal_hash_table_lookup (table, key) == NULL);
+
+       *(table->next_value (value)) = table->table[hash];
+       table->table [hash] = value;
+
+       ++table->num_entries;
+
+       resize_if_needed (table);
+}
+
+void
+mono_internal_hash_table_remove (MonoInternalHashTable *table, gpointer key)
+{
+       gint hash = HASH (key, table->hash_func, table->size);
+       gpointer *value;
+
+       for (value = &table->table [hash];
+            *value != NULL;
+            value = table->next_value (*value)) {
+               if (table->key_extract (*value) == key)
+               {
+                       *value = table->next_value (*value);
+                       --table->num_entries;
+                       return;
+               }
+       }
+}

Added: trunk/mono/mono/utils/mono-internal-hash.h
===================================================================
--- trunk/mono/mono/utils/mono-internal-hash.h  2007-06-04 20:37:41 UTC (rev 
78592)
+++ trunk/mono/mono/utils/mono-internal-hash.h  2007-06-04 20:37:43 UTC (rev 
78593)
@@ -0,0 +1,76 @@
+/*
+ * mono-internal-hash.h: A hash table which uses the values themselves as 
nodes.
+ *
+ * Author:
+ *   Mark Probst ([EMAIL PROTECTED])
+ *
+ * (C) 2007 Novell, Inc.
+ *
+ */
+#ifndef __MONO_UTILS_MONO_INTERNAL_HASH__
+#define __MONO_UTILS_MONO_INTERNAL_HASH__
+
+/* A MonoInternalHashTable is a hash table that does not allocate hash
+   nodes.  It can be used if the following conditions are fulfilled:
+
+   * The key is contained (directly or indirectly) in the value.
+
+   * Each value is in at most one internal hash table at the same
+     time.
+
+   The value data structure must then be extended to contain a
+   pointer, used by the internal hash table to chain values in the
+   same bucket.
+
+   Apart from the hash function, two other functions must be provided,
+   namely for extracting the key out of a value, and for getting the
+   next value pointer.  The latter must actually return a pointer to
+   the next value pointer, because the internal hash table must be
+   able to modify it.
+
+   See the class_cache internal hash table in MonoImage for an
+   example.
+*/
+
+typedef struct _MonoInternalHashTable MonoInternalHashTable;
+
+typedef gpointer (*MonoInternalHashKeyExtractFunc) (gpointer value);
+typedef gpointer* (*MonoInternalHashNextValueFunc) (gpointer value);
+
+struct _MonoInternalHashTable
+{
+       GHashFunc hash_func;
+       MonoInternalHashKeyExtractFunc key_extract;
+       MonoInternalHashNextValueFunc next_value;
+       gint size;
+       gint num_entries;
+       gpointer *table;
+};
+
+void
+mono_internal_hash_table_init (MonoInternalHashTable *table,
+                              GHashFunc hash_func,
+                              MonoInternalHashKeyExtractFunc key_extract,
+                              MonoInternalHashNextValueFunc next_value) 
MONO_INTERNAL;
+
+void
+mono_internal_hash_table_destroy (MonoInternalHashTable *table) MONO_INTERNAL;
+
+gpointer
+mono_internal_hash_table_lookup (MonoInternalHashTable *table, gpointer key) 
MONO_INTERNAL;
+
+/* mono_internal_hash_table_insert requires that there is no entry for
+   key in the hash table.  If you want to change the value for a key
+   already in the hash table, remove it first and then insert the new
+   one.
+
+   The key pointer is actually only passed here to check a debugging
+   assertion and to make the API look more familiar. */
+void
+mono_internal_hash_table_insert (MonoInternalHashTable *table,
+                                gpointer key, gpointer value) MONO_INTERNAL;
+
+void
+mono_internal_hash_table_remove (MonoInternalHashTable *table, gpointer key) 
MONO_INTERNAL;
+
+#endif

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to