Merge pull request #2113 from ludovic-henry/coop-socket-abort
[mono.git] / eglib / src / ghashtable.c
index e8f5fe41c967697ab3c29e265c9dd5cd88744c51..5fac0eb0f553dbd3d92e46b44d0d19ab5dac6dfb 100644 (file)
@@ -47,10 +47,17 @@ struct _GHashTable {
        int   table_size;
        int   in_use;
        int   threshold;
+       int   last_rehash;
        GDestroyNotify value_destroy_func, key_destroy_func;
 };
 
-static int prime_tbl[] = {
+typedef struct {
+       GHashTable *ht;
+       int slot_index;
+       Slot *slot;
+} Iter;
+
+static const guint prime_tbl[] = {
        11, 19, 37, 73, 109, 163, 251, 367, 557, 823, 1237,
        1861, 2777, 4177, 6247, 9371, 14057, 21089, 31627,
        47431, 71143, 106721, 160073, 240101, 360163,
@@ -85,8 +92,8 @@ calc_prime (int x)
        return x;
 }
 
-static int
-to_prime (int x)
+guint
+g_spaced_primes_closest (guint x)
 {
        int i;
        
@@ -97,41 +104,23 @@ to_prime (int x)
        return calc_prime (x);
 }
 
-static void
-adjust_threshold (GHashTable *hash)
-{
-       int size = hash->table_size;
-
-       hash->threshold = (int) hash->table_size * 0.75;
-       if (hash->threshold >= hash->table_size)
-               hash->threshold = hash->table_size-1;
-       if (hash->threshold == 0)
-               hash->threshold = 1;
-}
-       
-static void
-set_table (GHashTable *hash, Slot **table)
-{
-       hash->table = table;
-       adjust_threshold (hash);
-}
-
 GHashTable *
 g_hash_table_new (GHashFunc hash_func, GEqualFunc key_equal_func)
 {
        GHashTable *hash;
 
-       g_return_val_if_fail (hash_func != NULL, NULL);
-       g_return_val_if_fail (key_equal_func != NULL, NULL);
-                         
+       if (hash_func == NULL)
+               hash_func = g_direct_hash;
+       if (key_equal_func == NULL)
+               key_equal_func = g_direct_equal;
        hash = g_new0 (GHashTable, 1);
 
        hash->hash_func = hash_func;
        hash->key_equal_func = key_equal_func;
 
-       hash->table_size = to_prime (1);
+       hash->table_size = g_spaced_primes_closest (1);
        hash->table = g_new0 (Slot *, hash->table_size);
-       adjust_threshold (hash);
+       hash->last_rehash = hash->table_size;
        
        return hash;
 }
@@ -150,10 +139,91 @@ g_hash_table_new_full (GHashFunc hash_func, GEqualFunc key_equal_func,
        return hash;
 }
 
-void
+#if 0
+static void
+dump_hash_table (GHashTable *hash)
+{
+       int i;
+
+       for (i = 0; i < hash->table_size; i++) {
+               Slot *s;
+
+               for (s = hash->table [i]; s != NULL; s = s->next){
+                       guint hashcode = (*hash->hash_func) (s->key);
+                       guint slot = (hashcode) % hash->table_size;
+                       printf ("key %p hash %x on slot %d correct slot %d tb size %d\n", s->key, hashcode, i, slot, hash->table_size);
+               }
+       }
+}
+#endif
+
+#ifdef SANITY_CHECK
+static void
+sanity_check (GHashTable *hash)
+{
+       int i;
+
+       for (i = 0; i < hash->table_size; i++) {
+               Slot *s;
+
+               for (s = hash->table [i]; s != NULL; s = s->next){
+                       guint hashcode = (*hash->hash_func) (s->key);
+                       guint slot = (hashcode) % hash->table_size;
+                       if (slot != i) {
+                               dump_hashcode_func = 1;
+                               hashcode = (*hash->hash_func) (s->key);
+                               dump_hashcode_func = 0;
+                               g_error ("Key %p (bucket %d) on invalid bucket %d (hashcode %x) (tb size %d)", s->key, slot, i, hashcode, hash->table_size);
+                       }
+               }
+       }
+}
+#else
+
+#define sanity_check(HASH) do {}while(0)
+
+#endif
+
+static void
+do_rehash (GHashTable *hash)
+{
+       int current_size, i;
+       Slot **table;
+
+       /* printf ("Resizing diff=%d slots=%d\n", hash->in_use - hash->last_rehash, hash->table_size); */
+       hash->last_rehash = hash->table_size;
+       current_size = hash->table_size;
+       hash->table_size = g_spaced_primes_closest (hash->in_use);
+       /* printf ("New size: %d\n", hash->table_size); */
+       table = hash->table;
+       hash->table = g_new0 (Slot *, hash->table_size);
+       
+       for (i = 0; i < current_size; i++){
+               Slot *s, *next;
+
+               for (s = table [i]; s != NULL; s = next){
+                       guint hashcode = ((*hash->hash_func) (s->key)) % hash->table_size;
+                       next = s->next;
+
+                       s->next = hash->table [hashcode];
+                       hash->table [hashcode] = s;
+               }
+       }
+       g_free (table);
+}
+
+static void
 rehash (GHashTable *hash)
 {
-       /* FIXME */
+       int diff = ABS (hash->last_rehash - hash->in_use);
+
+       /* These are the factors to play with to change the rehashing strategy */
+       /* I played with them with a large range, and could not really get */
+       /* something that was too good, maybe the tests are not that great */
+       if (!(diff * 0.75 > hash->table_size * 2))
+               return;
+       do_rehash (hash);
+       sanity_check (hash);
 }
 
 void
@@ -164,6 +234,7 @@ g_hash_table_insert_replace (GHashTable *hash, gpointer key, gpointer value, gbo
        GEqualFunc equal;
        
        g_return_if_fail (hash != NULL);
+       sanity_check (hash);
 
        equal = hash->key_equal_func;
        if (hash->in_use >= hash->threshold)
@@ -180,6 +251,7 @@ g_hash_table_insert_replace (GHashTable *hash, gpointer key, gpointer value, gbo
                        if (hash->value_destroy_func != NULL)
                                (*hash->value_destroy_func) (s->value);
                        s->value = value;
+                       sanity_check (hash);
                        return;
                }
        }
@@ -189,12 +261,44 @@ g_hash_table_insert_replace (GHashTable *hash, gpointer key, gpointer value, gbo
        s->next = hash->table [hashcode];
        hash->table [hashcode] = s;
        hash->in_use++;
+       sanity_check (hash);
 }
 
+GList*
+g_hash_table_get_keys (GHashTable *hash)
+{
+       GHashTableIter iter;
+       GList *rv = NULL;
+       gpointer key;
+
+       g_hash_table_iter_init (&iter, hash);
+
+       while (g_hash_table_iter_next (&iter, &key, NULL))
+               rv = g_list_prepend (rv, key);
+
+       return g_list_reverse (rv);
+}
+
+GList*
+g_hash_table_get_values (GHashTable *hash)
+{
+       GHashTableIter iter;
+       GList *rv = NULL;
+       gpointer value;
+
+       g_hash_table_iter_init (&iter, hash);
+
+       while (g_hash_table_iter_next (&iter, NULL, &value))
+               rv = g_list_prepend (rv, value);
+
+       return g_list_reverse (rv);
+}
+
+
 guint
 g_hash_table_size (GHashTable *hash)
 {
-       g_return_if_fail (hash != NULL);
+       g_return_val_if_fail (hash != NULL, 0);
        
        return hash->in_use;
 }
@@ -217,14 +321,18 @@ g_hash_table_lookup_extended (GHashTable *hash, gconstpointer key, gpointer *ori
        Slot *s;
        guint hashcode;
        
-       g_return_if_fail (hash != NULL);
+       g_return_val_if_fail (hash != NULL, FALSE);
+       sanity_check (hash);
        equal = hash->key_equal_func;
 
-       hashcode = ((*hash->hash_func) (key)) % hash->table_size;       
+       hashcode = ((*hash->hash_func) (key)) % hash->table_size;
+       
        for (s = hash->table [hashcode]; s != NULL; s = s->next){
                if ((*equal)(s->key, key)){
-                       *orig_key = s->key;
-                       *value = s->value;
+                       if (orig_key)
+                               *orig_key = s->key;
+                       if (value)
+                               *value = s->value;
                        return TRUE;
                }
        }
@@ -252,15 +360,33 @@ g_hash_table_find (GHashTable *hash, GHRFunc predicate, gpointer user_data)
 {
        int i;
        
-       g_return_if_fail (hash != NULL);
-       g_return_if_fail (predicate != NULL);
+       g_return_val_if_fail (hash != NULL, NULL);
+       g_return_val_if_fail (predicate != NULL, NULL);
 
        for (i = 0; i < hash->table_size; i++){
                Slot *s;
 
                for (s = hash->table [i]; s != NULL; s = s->next)
                        if ((*predicate)(s->key, s->value, user_data))
-                               return;
+                               return s->value;
+       }
+       return NULL;
+}
+
+void
+g_hash_table_remove_all (GHashTable *hash)
+{
+       int i;
+       
+       g_return_if_fail (hash != NULL);
+
+       for (i = 0; i < hash->table_size; i++){
+               Slot *s;
+
+               while (hash->table [i]) {
+                       s = hash->table [i];
+                       g_hash_table_remove (hash, s->key);
+               }
        }
 }
 
@@ -268,27 +394,33 @@ gboolean
 g_hash_table_remove (GHashTable *hash, gconstpointer key)
 {
        GEqualFunc equal;
-       Slot *s, **last;
+       Slot *s, *last;
        guint hashcode;
        
        g_return_val_if_fail (hash != NULL, FALSE);
+       sanity_check (hash);
        equal = hash->key_equal_func;
 
        hashcode = ((*hash->hash_func)(key)) % hash->table_size;
-       last = &hash->table [hashcode];
-       for (s = *last; s != NULL; s = s->next){
+       last = NULL;
+       for (s = hash->table [hashcode]; s != NULL; s = s->next){
                if ((*equal)(s->key, key)){
                        if (hash->key_destroy_func != NULL)
                                (*hash->key_destroy_func)(s->key);
                        if (hash->value_destroy_func != NULL)
                                (*hash->value_destroy_func)(s->value);
-                       *last = s->next;
+                       if (last == NULL)
+                               hash->table [hashcode] = s->next;
+                       else
+                               last->next = s->next;
                        g_free (s);
                        hash->in_use--;
+                       sanity_check (hash);
                        return TRUE;
                }
-               last = &s;
+               last = s;
        }
+       sanity_check (hash);
        return FALSE;
 }
 
@@ -301,25 +433,112 @@ g_hash_table_foreach_remove (GHashTable *hash, GHRFunc func, gpointer user_data)
        g_return_val_if_fail (hash != NULL, 0);
        g_return_val_if_fail (func != NULL, 0);
 
+       sanity_check (hash);
        for (i = 0; i < hash->table_size; i++){
-               Slot *s, **last;
+               Slot *s, *last;
 
-               last = &hash->table [i];
-               for (s = hash->table [i]; s != NULL; s = s->next){
+               last = NULL;
+               for (s = hash->table [i]; s != NULL; ){
                        if ((*func)(s->key, s->value, user_data)){
+                               Slot *n;
+
                                if (hash->key_destroy_func != NULL)
                                        (*hash->key_destroy_func)(s->key);
                                if (hash->value_destroy_func != NULL)
                                        (*hash->value_destroy_func)(s->value);
-                               *last = s->next;
+                               if (last == NULL){
+                                       hash->table [i] = s->next;
+                                       n = s->next;
+                               } else  {
+                                       last->next = s->next;
+                                       n = last->next;
+                               }
                                g_free (s);
                                hash->in_use--;
                                count++;
+                               s = n;
+                       } else {
+                               last = s;
+                               s = s->next;
                        }
                }
        }
+       sanity_check (hash);
        if (count > 0)
                rehash (hash);
+       return count;
+}
+
+gboolean
+g_hash_table_steal (GHashTable *hash, gconstpointer key)
+{
+       GEqualFunc equal;
+       Slot *s, *last;
+       guint hashcode;
+       
+       g_return_val_if_fail (hash != NULL, FALSE);
+       sanity_check (hash);
+       equal = hash->key_equal_func;
+       
+       hashcode = ((*hash->hash_func)(key)) % hash->table_size;
+       last = NULL;
+       for (s = hash->table [hashcode]; s != NULL; s = s->next){
+               if ((*equal)(s->key, key)) {
+                       if (last == NULL)
+                               hash->table [hashcode] = s->next;
+                       else
+                               last->next = s->next;
+                       g_free (s);
+                       hash->in_use--;
+                       sanity_check (hash);
+                       return TRUE;
+               }
+               last = s;
+       }
+       sanity_check (hash);
+       return FALSE;
+       
+}
+
+guint
+g_hash_table_foreach_steal (GHashTable *hash, GHRFunc func, gpointer user_data)
+{
+       int i;
+       int count = 0;
+       
+       g_return_val_if_fail (hash != NULL, 0);
+       g_return_val_if_fail (func != NULL, 0);
+
+       sanity_check (hash);
+       for (i = 0; i < hash->table_size; i++){
+               Slot *s, *last;
+
+               last = NULL;
+               for (s = hash->table [i]; s != NULL; ){
+                       if ((*func)(s->key, s->value, user_data)){
+                               Slot *n;
+
+                               if (last == NULL){
+                                       hash->table [i] = s->next;
+                                       n = s->next;
+                               } else  {
+                                       last->next = s->next;
+                                       n = last->next;
+                               }
+                               g_free (s);
+                               hash->in_use--;
+                               count++;
+                               s = n;
+                       } else {
+                               last = s;
+                               s = s->next;
+                       }
+               }
+       }
+       sanity_check (hash);
+       if (count > 0)
+               rehash (hash);
+       return count;
 }
 
 void
@@ -347,6 +566,68 @@ g_hash_table_destroy (GHashTable *hash)
        g_free (hash);
 }
 
+void
+g_hash_table_print_stats (GHashTable *table)
+{
+       int i, max_chain_index, chain_size, max_chain_size;
+       Slot *node;
+
+       max_chain_size = 0;
+       max_chain_index = -1;
+       for (i = 0; i < table->table_size; i++) {
+               chain_size = 0;
+               for (node = table->table [i]; node; node = node->next)
+                       chain_size ++;
+               if (chain_size > max_chain_size) {
+                       max_chain_size = chain_size;
+                       max_chain_index = i;
+               }
+       }
+
+       printf ("Size: %d Table Size: %d Max Chain Length: %d at %d\n", table->in_use, table->table_size, max_chain_size, max_chain_index);
+}
+
+void
+g_hash_table_iter_init (GHashTableIter *it, GHashTable *hash_table)
+{
+       Iter *iter = (Iter*)it;
+
+       memset (iter, 0, sizeof (Iter));
+       iter->ht = hash_table;
+       iter->slot_index = -1;
+}
+
+gboolean g_hash_table_iter_next (GHashTableIter *it, gpointer *key, gpointer *value)
+{
+       Iter *iter = (Iter*)it;
+
+       GHashTable *hash = iter->ht;
+
+       g_assert (iter->slot_index != -2);
+       g_assert (sizeof (Iter) <= sizeof (GHashTableIter));
+
+       if (!iter->slot) {
+               while (TRUE) {
+                       iter->slot_index ++;
+                       if (iter->slot_index >= hash->table_size) {
+                               iter->slot_index = -2;
+                               return FALSE;
+                       }
+                       if (hash->table [iter->slot_index])
+                               break;
+               }
+               iter->slot = hash->table [iter->slot_index];
+       }
+
+       if (key)
+               *key = iter->slot->key;
+       if (value)
+               *value = iter->slot->value;
+       iter->slot = iter->slot->next;
+
+       return TRUE;
+}
+
 gboolean
 g_direct_equal (gconstpointer v1, gconstpointer v2)
 {
@@ -362,13 +643,13 @@ g_direct_hash (gconstpointer v1)
 gboolean
 g_int_equal (gconstpointer v1, gconstpointer v2)
 {
-       return GPOINTER_TO_INT (v1) == GPOINTER_TO_INT (v2);
+       return *(gint *)v1 == *(gint *)v2;
 }
 
 guint
 g_int_hash (gconstpointer v1)
 {
-       return GPOINTER_TO_UINT(v1);
+       return *(guint *)v1;
 }
 
 gboolean