*** empty log message ***
[bpt/guile.git] / libguile / hash.c
index 9cb0d74..750aeaf 100644 (file)
@@ -1,46 +1,20 @@
-/*     Copyright (C) 1995,1996,1997, 2000, 2001 Free Software Foundation, Inc.
+/*     Copyright (C) 1995,1996,1997, 2000, 2001, 2003 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
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
- * 
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- * 
- * You should have received a copy of the GNU General Public License
- * along with this software; see the file COPYING.  If not, write to
- * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307 USA
- *
- * As a special exception, the Free Software Foundation gives permission
- * for additional uses of the text contained in its release of GUILE.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * The exception is that, if you link the GUILE library with other files
- * to produce an executable, this does not by itself cause the
- * resulting executable to be covered by the GNU General Public License.
- * Your use of that executable is in no way restricted on account of
- * linking the GUILE library code into it.
- *
- * This exception does not however invalidate any other reasons why
- * the executable file might be covered by the GNU General Public License.
- *
- * This exception applies only to the code released by the
- * Free Software Foundation under the name GUILE.  If you copy
- * code from other Free Software Foundation releases into a copy of
- * GUILE, as the General Public License permits, the exception does
- * not apply to the code that you add in this way.  To avoid misleading
- * anyone as to the status of such modified files, you must delete
- * this exception notice from them.
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * If you write modifications of your own for GUILE, it is your choice
- * whether to permit this exception to apply to your modifications.
- * If you do not wish that, delete this exception notice.  */
+ * 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
+ */
 
-/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
-   gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
 
 \f
 
@@ -63,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;
 }
 
 
@@ -95,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):
@@ -122,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);
@@ -136,11 +101,14 @@ 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:
-    case scm_tc7_substring:
       return scm_string_hash (SCM_STRING_UCHARS (obj), SCM_STRING_LENGTH (obj)) % n;
     case scm_tc7_symbol:
       return SCM_SYMBOL_HASH (obj) % n;
@@ -148,7 +116,7 @@ scm_hasher(SCM obj, unsigned long n, size_t d)
     case scm_tc7_vector:
       {
        size_t len = SCM_VECTOR_LENGTH(obj);
-       SCM *data = SCM_VELTS(obj);
+       SCM const *data = SCM_VELTS(obj);
        if (len > 5)
          {
            size_t i = d/2;
@@ -216,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);
@@ -274,9 +242,7 @@ SCM_DEFINE (scm_hash, "hash", 2, 0, 0,
 void
 scm_init_hash ()
 {
-#ifndef SCM_MAGIC_SNARFER
 #include "libguile/hash.x"
-#endif
 }