(scm_string_filter, scm_string_delete): For char and
[bpt/guile.git] / libguile / hashtab.c
index 453a674..24f16a0 100644 (file)
@@ -12,7 +12,7 @@
  *
  * 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
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 
@@ -74,14 +74,13 @@ static unsigned long hashtable_size[] = {
 
 #define HASHTABLE_SIZE_N (sizeof(hashtable_size)/sizeof(unsigned long))
 
-/* Turn an empty vector hash table into an opaque resizable one. */
-
 static char *s_hashtable = "hashtable";
 
 SCM weak_hashtables = SCM_EOL;
 
 static SCM
-make_hash_table (int flags, unsigned long k, const char *func_name) {
+make_hash_table (int flags, unsigned long k, const char *func_name) 
+{
   SCM table, vector;
   scm_t_hashtable *t;
   int i = 0, n = k ? k : 31;
@@ -103,6 +102,7 @@ make_hash_table (int flags, unsigned long k, const char *func_name) {
   t->lower = 0;
   t->upper = 9 * n / 10;
   t->flags = flags;
+  t->hash_fn = NULL;
   if (flags)
     {
       SCM_NEWSMOB3 (table, scm_tc16_hashtable, vector, t, weak_hashtables);
@@ -113,7 +113,6 @@ make_hash_table (int flags, unsigned long k, const char *func_name) {
   return table;
 }
 
-
 void
 scm_i_rehash (SCM table,
              unsigned long (*hash_fn)(),
@@ -140,9 +139,13 @@ scm_i_rehash (SCM table,
       if (i >= HASHTABLE_SIZE_N)
        /* don't rehash */
        return;
-      /* store for use in rehash_after_gc */
-      SCM_HASHTABLE (table)->hash_fn = hash_fn;
-      SCM_HASHTABLE (table)->closure = closure;
+
+      /* Remember HASH_FN for rehash_after_gc, but only when CLOSURE
+        is not needed since CLOSURE can not be guaranteed to be valid
+        after this function returns.
+      */
+      if (closure == NULL)
+       SCM_HASHTABLE (table)->hash_fn = hash_fn;
     }
   SCM_HASHTABLE (table)->size_index = i;
   
@@ -162,25 +165,41 @@ scm_i_rehash (SCM table,
   else
     new_buckets = scm_c_make_vector (new_size, SCM_EOL);
 
+  /* When this is a weak hashtable, running the GC might change it.
+     We need to cope with this while rehashing its elements.  We do
+     this by first installing the new, empty bucket vector and turning
+     the old bucket vector into a regularily scanned weak vector.
+     Then we remove the elements from the old bucket vector and insert
+     them into the new one.
+  */
+
+  SCM_SET_HASHTABLE_VECTOR (table, new_buckets);
+  SCM_SET_HASHTABLE_N_ITEMS (table, 0);
+  if (SCM_HASHTABLE_WEAK_P (table))
+    SCM_I_SET_WVECT_TYPE (buckets, (SCM_HASHTABLE_FLAGS (table)));
+
   old_size = SCM_SIMPLE_VECTOR_LENGTH (buckets);
   for (i = 0; i < old_size; ++i)
     {
-      SCM ls = SCM_SIMPLE_VECTOR_REF (buckets, i), handle;
-      while (!scm_is_null (ls))
+      SCM ls, cell, handle;
+
+      ls = SCM_SIMPLE_VECTOR_REF (buckets, i);
+      SCM_SIMPLE_VECTOR_SET (buckets, i, SCM_EOL);
+
+      while (scm_is_pair (ls))
        {
          unsigned long h;
-         handle = SCM_CAR (ls);
+         cell = ls;
+         handle = SCM_CAR (cell);
+         ls = SCM_CDR (ls);
          h = hash_fn (SCM_CAR (handle), new_size, closure);
          if (h >= new_size)
            scm_out_of_range (func_name, scm_from_ulong (h));
-         SCM_SIMPLE_VECTOR_SET 
-           (new_buckets, h,
-            scm_cons (handle,
-                      SCM_SIMPLE_VECTOR_REF (new_buckets, h)));
-         ls = SCM_CDR (ls);
+         SCM_SETCDR (cell, SCM_SIMPLE_VECTOR_REF (new_buckets, h));
+         SCM_SIMPLE_VECTOR_SET (new_buckets, h, cell);
+         SCM_HASHTABLE_INCREMENT (table);
        }
     }
-  SCM_SET_HASHTABLE_VECTOR (table, new_buckets);
 }
 
 
@@ -283,11 +302,11 @@ rehash_after_gc (void *dummy1 SCM_UNUSED,
       h = first;
       do
        {
-         scm_i_rehash (h,
-                       /* use same hash_fn and closure as last time */
-                       SCM_HASHTABLE (h)->hash_fn,
-                       SCM_HASHTABLE (h)->closure,
-                       "rehash_after_gc");
+         /* Rehash only when we have a hash_fn.
+          */
+         if (SCM_HASHTABLE (h)->hash_fn)
+           scm_i_rehash (h, SCM_HASHTABLE (h)->hash_fn, NULL,
+                         "rehash_after_gc");
          last = h;
          h = SCM_HASHTABLE_NEXT (h);
        } while (!scm_is_null (h));
@@ -314,7 +333,7 @@ scm_c_make_hash_table (unsigned long k)
 
 SCM_DEFINE (scm_make_hash_table, "make-hash-table", 0, 1, 0,
            (SCM n),
-           "Make a hash table with optional minimum number of buckets @var{n}\n")
+           "Make a new abstract hash table object with minimum number of buckets @var{n}\n")
 #define FUNC_NAME s_scm_make_hash_table
 {
   if (SCM_UNBNDP (n))
@@ -328,9 +347,7 @@ SCM_DEFINE (scm_make_weak_key_hash_table, "make-weak-key-hash-table", 0, 1, 0,
            (SCM n),
            "@deffnx {Scheme Procedure} make-weak-value-hash-table size\n"
            "@deffnx {Scheme Procedure} make-doubly-weak-hash-table size\n"
-           "Return a weak hash table with @var{size} buckets. As with any\n"
-           "hash table, choosing a good size for the table requires some\n"
-           "caution.\n"
+           "Return a weak hash table with @var{size} buckets.\n"
            "\n"
            "You can modify weak hash tables in exactly the same way you\n"
            "would modify regular hash tables. (@pxref{Hash Tables})")
@@ -384,7 +401,7 @@ SCM_DEFINE (scm_make_doubly_weak_hash_table, "make-doubly-weak-hash-table", 1, 0
 
 SCM_DEFINE (scm_hash_table_p, "hash-table?", 1, 0, 0, 
             (SCM obj),
-           "Return @code{#t} if @var{obj} is a hash table.")
+           "Return @code{#t} if @var{obj} is an abstract hash table object.")
 #define FUNC_NAME s_scm_hash_table_p
 {
   return scm_from_bool (SCM_HASHTABLE_P (obj));
@@ -475,13 +492,33 @@ scm_hash_fn_create_handle_x (SCM table, SCM obj, SCM init, unsigned long (*hash_
     return it;
   else
     {
-      SCM old_bucket = SCM_SIMPLE_VECTOR_REF (buckets, k);
-      SCM new_bucket = scm_acons (obj, init, old_bucket);
+      /* When this is a weak hashtable, running the GC can change it.
+        Thus, we must allocate the new cells first and can only then
+        access BUCKETS.  Also, we need to fetch the bucket vector
+        again since the hashtable might have been rehashed.  This
+        necessitates a new hash value as well.
+      */
+      SCM new_bucket = scm_acons (obj, init, SCM_EOL);
+      if (!scm_is_eq (table, buckets)
+         && !scm_is_eq (SCM_HASHTABLE_VECTOR (table), buckets))
+       {
+         buckets = SCM_HASHTABLE_VECTOR (table);
+         k = hash_fn (obj, SCM_SIMPLE_VECTOR_LENGTH (buckets), closure);
+         if (k >= SCM_SIMPLE_VECTOR_LENGTH (buckets))
+           scm_out_of_range ("hash_fn_create_handle_x", scm_from_ulong (k));
+       }
+      SCM_SETCDR (new_bucket, SCM_SIMPLE_VECTOR_REF (buckets, k));
       SCM_SIMPLE_VECTOR_SET (buckets, k, new_bucket);
-      if (table != buckets)
+      if (!scm_is_eq (table, buckets))
        {
+         /* Update element count and maybe rehash the table.  The
+            table might have too few entries here since weak hash
+            tables used with the hashx_* functions can not be
+            rehashed after GC.
+         */
          SCM_HASHTABLE_INCREMENT (table);
-         if (SCM_HASHTABLE_N_ITEMS (table) > SCM_HASHTABLE_UPPER (table))
+         if (SCM_HASHTABLE_N_ITEMS (table) < SCM_HASHTABLE_LOWER (table)
+             || SCM_HASHTABLE_N_ITEMS (table) > SCM_HASHTABLE_UPPER (table))
            scm_i_rehash (table, hash_fn, closure, FUNC_NAME);
        }
       return SCM_CAR (new_bucket);
@@ -516,12 +553,11 @@ scm_hash_fn_set_x (SCM table, SCM obj, SCM val, unsigned long (*hash_fn)(),
 }
 
 
-
-
-
 SCM 
-scm_hash_fn_remove_x (SCM table, SCM obj, unsigned long (*hash_fn)(), SCM (*assoc_fn)(),
-                      SCM (*delete_fn)(), void * closure)
+scm_hash_fn_remove_x (SCM table, SCM obj,
+                     unsigned long (*hash_fn)(),
+                     SCM (*assoc_fn)(),
+                      void *closure)
 {
   unsigned long k;
   SCM buckets, h;
@@ -544,9 +580,8 @@ scm_hash_fn_remove_x (SCM table, SCM obj, unsigned long (*hash_fn)(), SCM (*asso
   if (scm_is_true (h))
     {
       SCM_SIMPLE_VECTOR_SET 
-       (buckets, k,
-        delete_fn (h, SCM_SIMPLE_VECTOR_REF (buckets, k)));
-      if (table != buckets)
+       (buckets, k, scm_delq_x (h, SCM_SIMPLE_VECTOR_REF (buckets, k)));
+      if (!scm_is_eq (table, buckets))
        {
          SCM_HASHTABLE_DECREMENT (table);
          if (SCM_HASHTABLE_N_ITEMS (table) < SCM_HASHTABLE_LOWER (table))
@@ -558,12 +593,16 @@ scm_hash_fn_remove_x (SCM table, SCM obj, unsigned long (*hash_fn)(), SCM (*asso
 
 SCM_DEFINE (scm_hash_clear_x, "hash-clear!", 1, 0, 0,
            (SCM table),
-           "Remove all items from TABLE (without triggering a resize).")
+           "Remove all items from @var{table} (without triggering a resize).")
 #define FUNC_NAME s_scm_hash_clear_x
 {
-  SCM_VALIDATE_HASHTABLE (1, table);
-  scm_vector_fill_x (SCM_HASHTABLE_VECTOR (table), SCM_EOL);
-  SCM_SET_HASHTABLE_N_ITEMS (table, 0);
+  if (SCM_HASHTABLE_P (table))
+    {
+      scm_vector_fill_x (SCM_HASHTABLE_VECTOR (table), SCM_EOL);
+      SCM_SET_HASHTABLE_N_ITEMS (table, 0);
+    }
+  else
+    scm_vector_fill_x (table, SCM_EOL);
   return SCM_UNSPECIFIED;
 }
 #undef FUNC_NAME
@@ -629,8 +668,7 @@ SCM_DEFINE (scm_hashq_remove_x, "hashq-remove!", 2, 0, 0,
            "@var{table}.  Uses @code{eq?} for equality tests.")
 #define FUNC_NAME s_scm_hashq_remove_x
 {
-  return scm_hash_fn_remove_x (table, key, scm_ihashq, scm_sloppy_assq,
-                              scm_delq_x, 0);
+  return scm_hash_fn_remove_x (table, key, scm_ihashq, scm_sloppy_assq, 0);
 }
 #undef FUNC_NAME
 
@@ -696,8 +734,7 @@ SCM_DEFINE (scm_hashv_remove_x, "hashv-remove!", 2, 0, 0,
            "@var{table}.  Uses @code{eqv?} for equality tests.")
 #define FUNC_NAME s_scm_hashv_remove_x
 {
-  return scm_hash_fn_remove_x (table, key, scm_ihashv, scm_sloppy_assv,
-                              scm_delv_x, 0);
+  return scm_hash_fn_remove_x (table, key, scm_ihashv, scm_sloppy_assv, 0);
 }
 #undef FUNC_NAME
 
@@ -763,8 +800,7 @@ SCM_DEFINE (scm_hash_remove_x, "hash-remove!", 2, 0, 0,
            "@var{table}.  Uses @code{equal?} for equality tests.")
 #define FUNC_NAME s_scm_hash_remove_x
 {
-  return scm_hash_fn_remove_x (table, key, scm_ihash, scm_sloppy_assoc,
-                              scm_delete_x, 0);
+  return scm_hash_fn_remove_x (table, key, scm_ihash, scm_sloppy_assoc, 0);
 }
 #undef FUNC_NAME
 
@@ -775,7 +811,6 @@ typedef struct scm_t_ihashx_closure
 {
   SCM hash;
   SCM assoc;
-  SCM delete;
 } scm_t_ihashx_closure;
 
 
@@ -796,16 +831,6 @@ scm_sloppy_assx (SCM obj, SCM alist, scm_t_ihashx_closure *closure)
 }
 
 
-
-
-static SCM
-scm_delx_x (SCM obj, SCM alist, scm_t_ihashx_closure *closure)
-{
-  return scm_call_2 (closure->delete, obj, alist);
-}
-
-
-
 SCM_DEFINE (scm_hashx_get_handle, "hashx-get-handle", 4, 0, 0, 
             (SCM hash, SCM assoc, SCM table, SCM key),
            "This behaves the same way as the corresponding\n"
@@ -892,17 +917,26 @@ SCM_DEFINE (scm_hashx_set_x, "hashx-set!", 5, 0, 0,
 }
 #undef FUNC_NAME
 
-
-
-SCM
-scm_hashx_remove_x (SCM hash, SCM assoc, SCM delete, SCM table, SCM obj)
+SCM_DEFINE (scm_hashx_remove_x, "hashx-remove!", 4, 0, 0,
+           (SCM hash, SCM assoc, SCM table, SCM obj),
+           "This behaves the same way as the corresponding @code{remove!}\n"
+           "function, but uses @var{hash} as a hash function and\n"
+           "@var{assoc} to compare keys.  @code{hash} must be a function\n"
+           "that takes two arguments, a key to be hashed and a table size.\n"
+           "@code{assoc} must be an associator function, like @code{assoc},\n"
+           "@code{assq} or @code{assv}.\n"
+           "\n"
+           " By way of illustration, @code{hashq-remove! table key} is\n"
+           "equivalent to @code{hashx-remove!  hashq assq #f table key}.")
+#define FUNC_NAME s_scm_hashx_remove_x
 {
   scm_t_ihashx_closure closure;
   closure.hash = hash;
   closure.assoc = assoc;
-  closure.delete = delete;
-  return scm_hash_fn_remove_x (table, obj, scm_ihashx, scm_sloppy_assx, scm_delx_x, 0);
+  return scm_hash_fn_remove_x (table, obj, scm_ihashx, scm_sloppy_assx,
+                               (void *) &closure);
 }
+#undef FUNC_NAME
 
 /* Hash table iterators */