* alist.c, arbiters.c, async.c, backtrace.c, boolean.c, chars.c,
[bpt/guile.git] / libguile / hashtab.c
index 834b24a..1efb647 100644 (file)
@@ -1,4 +1,4 @@
-/*     Copyright (C) 1995, 1996, 1998, 1999 Free Software Foundation, Inc.
+/*     Copyright (C) 1995, 1996, 1998, 1999, 2000 Free Software Foundation, Inc.
  * 
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 \f
 
 #include <stdio.h>
-#include "_scm.h"
-#include "alist.h"
-#include "hash.h"
-#include "eval.h"
-
-#include "validate.h"
-#include "hashtab.h"
+#include "libguile/_scm.h"
+#include "libguile/alist.h"
+#include "libguile/hash.h"
+#include "libguile/eval.h"
+#include "libguile/vectors.h"
+
+#include "libguile/validate.h"
+#include "libguile/hashtab.h"
 \f
 
 
@@ -62,38 +63,36 @@ scm_hash_fn_get_handle (SCM table,SCM obj,unsigned int (*hash_fn)(),SCM (*assoc_
   SCM h;
 
   SCM_ASSERT (SCM_VECTORP (table), table, SCM_ARG1, "hash_fn_get_handle");
-  if (SCM_LENGTH (table) == 0)
+  if (SCM_VECTOR_LENGTH (table) == 0)
     return SCM_EOL;
-  k = hash_fn (obj, SCM_LENGTH (table), closure);
-  SCM_ASSERT ((0 <= k) && (k < SCM_LENGTH (table)),
-             scm_ulong2num (k),
-             SCM_OUTOFRANGE,
-             "hash_fn_get_handle");
+  k = hash_fn (obj, SCM_VECTOR_LENGTH (table), closure);
+  if (k >= SCM_VECTOR_LENGTH (table))
+    scm_out_of_range ("hash_fn_get_handle", scm_ulong2num (k));
   h = assoc_fn (obj, SCM_VELTS (table)[k], closure);
   return h;
 }
 
 
-
 SCM
 scm_hash_fn_create_handle_x (SCM table,SCM obj,SCM init,unsigned int (*hash_fn)(),
                              SCM (*assoc_fn)(),void * closure)
+#define FUNC_NAME "scm_hash_fn_create_handle_x"
 {
   unsigned int k;
   SCM it;
 
   SCM_ASSERT (SCM_VECTORP (table), table, SCM_ARG1, "hash_fn_create_handle_x");
-  if (SCM_LENGTH (table) == 0)
-    return SCM_EOL;
-  k = hash_fn (obj, SCM_LENGTH (table), closure);
-  SCM_ASSERT ((0 <= k) && (k < SCM_LENGTH (table)),
-             scm_ulong2num (k),
-             SCM_OUTOFRANGE,
-             "hash_fn_create_handle_x");
+  if (SCM_VECTOR_LENGTH (table) == 0)
+    SCM_MISC_ERROR ("void hashtable", SCM_EOL);
+
+  k = hash_fn (obj, SCM_VECTOR_LENGTH (table), closure);
+  if (k >= SCM_VECTOR_LENGTH (table))
+    scm_out_of_range ("hash_fn_create_handle_x", scm_ulong2num (k));
   SCM_REDEFER_INTS;
   it = assoc_fn (obj, SCM_VELTS (table)[k], closure);
   if (SCM_NIMP (it))
     {
+      SCM_REALLOW_INTS;
       return it;
     }
   {
@@ -106,8 +105,7 @@ scm_hash_fn_create_handle_x (SCM table,SCM obj,SCM init,unsigned int (*hash_fn)(
     return SCM_CAR (new_bucket);
   }
 }
-
-
+#undef FUNC_NAME
 
 
 SCM 
@@ -149,13 +147,11 @@ scm_hash_fn_remove_x (SCM table,SCM obj,unsigned int (*hash_fn)(),SCM (*assoc_fn
   SCM h;
 
   SCM_ASSERT (SCM_VECTORP (table), table, SCM_ARG1, "hash_fn_remove_x");
-  if (SCM_LENGTH (table) == 0)
+  if (SCM_VECTOR_LENGTH (table) == 0)
     return SCM_EOL;
-  k = hash_fn (obj, SCM_LENGTH (table), closure);
-  SCM_ASSERT ((0 <= k) && (k < SCM_LENGTH (table)),
-             scm_ulong2num (k),
-             SCM_OUTOFRANGE,
-             "hash_fn_remove_x");
+  k = hash_fn (obj, SCM_VECTOR_LENGTH (table), closure);
+  if (k >= SCM_VECTOR_LENGTH (table))
+    scm_out_of_range ("hash_fn_remove_x", scm_ulong2num (k));
   h = assoc_fn (obj, SCM_VELTS (table)[k], closure);
   SCM_VELTS(table)[k] = delete_fn (h, SCM_VELTS(table)[k]);
   return h;
@@ -199,7 +195,7 @@ SCM_DEFINE (scm_hashq_ref, "hashq-ref", 2, 1, 0,
            "supplied).  Uses `eq?' for equality testing.")
 #define FUNC_NAME s_scm_hashq_ref
 {
-  if (dflt == SCM_UNDEFINED)
+  if (SCM_UNBNDP (dflt))
     dflt = SCM_BOOL_F;
   return scm_hash_fn_ref (table, obj, dflt, scm_ihashq, scm_sloppy_assq, 0);
 }
@@ -267,7 +263,7 @@ SCM_DEFINE (scm_hashv_ref, "hashv-ref", 2, 1, 0,
            "supplied).  Uses `eqv?' for equality testing.")
 #define FUNC_NAME s_scm_hashv_ref
 {
-  if (dflt == SCM_UNDEFINED)
+  if (SCM_UNBNDP (dflt))
     dflt = SCM_BOOL_F;
   return scm_hash_fn_ref (table, obj, dflt, scm_ihashv, scm_sloppy_assv, 0);
 }
@@ -333,7 +329,7 @@ SCM_DEFINE (scm_hash_ref, "hash-ref", 2, 1, 0,
            "supplied).  Uses `equal?' for equality testing.")
 #define FUNC_NAME s_scm_hash_ref
 {
-  if (dflt == SCM_UNDEFINED)
+  if (SCM_UNBNDP (dflt))
     dflt = SCM_BOOL_F;
   return scm_hash_fn_ref (table, obj, dflt, scm_ihash, scm_sloppy_assoc, 0);
 }
@@ -379,11 +375,11 @@ static unsigned int
 scm_ihashx (SCM obj,unsigned int n,struct scm_ihashx_closure * closure)
 {
   SCM answer;
-  SCM_ALLOW_INTS;
+  SCM_DEFER_INTS;
   answer = scm_apply (closure->hash,
                      scm_listify (obj, scm_ulong2num ((unsigned long)n), SCM_UNDEFINED),
                      SCM_EOL);
-  SCM_DEFER_INTS;
+  SCM_ALLOW_INTS;
   return SCM_INUM (answer);
 }
 
@@ -393,11 +389,11 @@ static SCM
 scm_sloppy_assx (SCM obj,SCM alist,struct scm_ihashx_closure * closure)
 {
   SCM answer;
-  SCM_ALLOW_INTS;
+  SCM_DEFER_INTS;
   answer = scm_apply (closure->assoc,
                      scm_listify (obj, alist, SCM_UNDEFINED),
                      SCM_EOL);
-  SCM_DEFER_INTS;
+  SCM_ALLOW_INTS;
   return answer;
 }
 
@@ -408,11 +404,11 @@ static SCM
 scm_delx_x (SCM obj,SCM alist,struct scm_ihashx_closure * closure)
 {
   SCM answer;
-  SCM_ALLOW_INTS;
+  SCM_DEFER_INTS;
   answer = scm_apply (closure->delete,
                      scm_listify (obj, alist, SCM_UNDEFINED),
                      SCM_EOL);
-  SCM_DEFER_INTS;
+  SCM_ALLOW_INTS;
   return answer;
 }
 
@@ -437,7 +433,7 @@ SCM_DEFINE (scm_hashx_get_handle, "hashx-get-handle", 4, 0, 0,
 
 
 SCM_DEFINE (scm_hashx_create_handle_x, "hashx-create-handle!", 5, 0, 0, 
-            (SCM hash,SCM assoc,SCM table,SCM obj,SCM init),
+            (SCM hash, SCM assoc, SCM table, SCM obj, SCM init),
            "This behaves the same way as the corresponding @code{-create-handle}\n"
            "function, but uses @var{hasher} as a\n"
            "hash function and @var{assoc} to compare keys.  @code{hasher} must\n"
@@ -456,7 +452,7 @@ SCM_DEFINE (scm_hashx_create_handle_x, "hashx-create-handle!", 5, 0, 0,
 
 
 SCM_DEFINE (scm_hashx_ref, "hashx-ref", 4, 1, 0, 
-            (SCM hash,SCM assoc,SCM table,SCM obj,SCM dflt),
+            (SCM hash, SCM assoc, SCM table, SCM obj, SCM dflt),
            "This behaves the same way as the corresponding @code{ref}\n"
            "function, but uses @var{hasher} as a\n"
            "hash function and @var{assoc} to compare keys.  @code{hasher} must\n"
@@ -468,7 +464,7 @@ SCM_DEFINE (scm_hashx_ref, "hashx-ref", 4, 1, 0,
 #define FUNC_NAME s_scm_hashx_ref
 {
   struct scm_ihashx_closure closure;
-  if (dflt == SCM_UNDEFINED)
+  if (SCM_UNBNDP (dflt))
     dflt = SCM_BOOL_F;
   closure.hash = hash;
   closure.assoc = assoc;
@@ -513,7 +509,7 @@ scm_hashx_remove_x (SCM hash,SCM assoc,SCM delete,SCM table,SCM obj)
 static SCM
 fold_proc (void *proc, SCM key, SCM data, SCM value)
 {
-  return scm_apply ((SCM) proc, SCM_LIST3 (key, data, value), SCM_EOL);
+  return scm_apply (SCM_PACK (proc), SCM_LIST3 (key, data, value), SCM_EOL);
 }
 
 SCM_DEFINE (scm_hash_fold, "hash-fold", 3, 0, 0, 
@@ -530,14 +526,14 @@ SCM_DEFINE (scm_hash_fold, "hash-fold", 3, 0, 0,
 {
   SCM_VALIDATE_PROC (1,proc);
   SCM_VALIDATE_VECTOR (3,table);
-  return scm_internal_hash_fold (fold_proc, (void *) proc, init, table);
+  return scm_internal_hash_fold (fold_proc, (void *) SCM_UNPACK (proc), init, table);
 }
 #undef FUNC_NAME
 
 SCM
 scm_internal_hash_fold (SCM (*fn) (), void *closure, SCM init, SCM table)
 {
-  int i, n = SCM_LENGTH (table);
+  int i, n = SCM_VECTOR_LENGTH (table);
   SCM result = init;
   for (i = 0; i < n; ++i)
     {
@@ -562,5 +558,13 @@ scm_internal_hash_fold (SCM (*fn) (), void *closure, SCM init, SCM table)
 void
 scm_init_hashtab ()
 {
-#include "hashtab.x"
+#ifndef SCM_MAGIC_SNARFER
+#include "libguile/hashtab.x"
+#endif
 }
+
+/*
+  Local Variables:
+  c-file-style: "gnu"
+  End:
+*/