*** empty log message ***
[bpt/guile.git] / libguile / hash.c
index 26c4fcd..750aeaf 100644 (file)
@@ -1,4 +1,4 @@
-/*     Copyright (C) 1995,1996,1997, 2000, 2001 Free Software Foundation, Inc.
+/*     Copyright (C) 1995,1996,1997, 2000, 2001, 2003 Free Software Foundation, Inc.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -37,22 +37,13 @@ extern double floor();
 unsigned long 
 scm_string_hash (const unsigned char *str, size_t len)
 {
-  if (len > 5)
-    {
-      size_t i = 5;
-      unsigned long h = 264;
-      while (i--)
-       h = (h << 8) + (unsigned) str[h % len];
-      return h;
-    }
-  else
-    {
-      size_t i = len;
-      unsigned long h = 0;
-      while (i)
-       h = (h << 8) + (unsigned) str[--i];
-      return h;
-    }
+  /* from suggestion at: */
+  /* http://srfi.schemers.org/srfi-13/mail-archive/msg00112.html */
+
+  unsigned long h = 0;
+  while (len-- > 0)
+    h = *str++ + h*37;
+  return h;
 }
 
 
@@ -69,7 +60,7 @@ scm_hasher(SCM obj, unsigned long n, size_t d)
     return SCM_INUM(obj) % n;   /* SCM_INUMP(obj) */
   case scm_tc3_imm24:
     if (SCM_CHARP(obj))
-      return (unsigned)(scm_downcase(SCM_CHAR(obj))) % n;
+      return (unsigned)(scm_c_downcase(SCM_CHAR(obj))) % n;
     switch (SCM_UNPACK (obj)) {
 #ifndef SICP
     case SCM_UNPACK(SCM_EOL):
@@ -96,11 +87,11 @@ scm_hasher(SCM obj, unsigned long n, size_t d)
     default: 
       return 263 % n;
     case scm_tc7_smob:
+      return 263 % n;
+    case scm_tc7_number:
       switch SCM_TYP16 (obj) {
       case scm_tc16_big:
         return SCM_INUM (scm_modulo (obj, SCM_MAKINUM (n)));
-      default: 
-       return 263 % n;
       case scm_tc16_real:
        {
          double r = SCM_REAL_VALUE (obj);
@@ -110,9 +101,13 @@ scm_hasher(SCM obj, unsigned long n, size_t d)
            return SCM_INUM (scm_modulo (obj, SCM_MAKINUM (n)));
          }
        }
+        /* Fall through */
       case scm_tc16_complex:
+      case scm_tc16_fraction:
        obj = scm_number_to_string (obj, SCM_MAKINUM (10));
+        /* Fall through */
       }
+      /* Fall through */
     case scm_tc7_string:
       return scm_string_hash (SCM_STRING_UCHARS (obj), SCM_STRING_LENGTH (obj)) % n;
     case scm_tc7_symbol:
@@ -189,7 +184,7 @@ unsigned long
 scm_ihashv (SCM obj, unsigned long n)
 {
   if (SCM_CHARP(obj))
-    return ((unsigned long) (scm_downcase (SCM_CHAR (obj)))) % n; /* downcase!?!! */
+    return ((unsigned long) (scm_c_downcase (SCM_CHAR (obj)))) % n; /* downcase!?!! */
 
   if (SCM_NUMP(obj))
     return (unsigned long) scm_hasher(obj, n, 10);