1 /* Copyright (C) 1995,1996,1997, 2000, 2001, 2003, 2004, 2006, 2008 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libguile/_scm.h"
25 #include "libguile/chars.h"
26 #include "libguile/ports.h"
27 #include "libguile/strings.h"
28 #include "libguile/symbols.h"
29 #include "libguile/vectors.h"
31 #include "libguile/validate.h"
32 #include "libguile/hash.h"
36 extern double floor();
41 scm_string_hash (const unsigned char *str, size_t len)
43 /* from suggestion at: */
44 /* http://srfi.schemers.org/srfi-13/mail-archive/msg00112.html */
53 /* Dirk:FIXME:: why downcase for characters? (2x: scm_hasher, scm_ihashv) */
54 /* Dirk:FIXME:: scm_hasher could be made static. */
58 scm_hasher(SCM obj, unsigned long n, size_t d)
60 switch (SCM_ITAG3 (obj)) {
63 return SCM_I_INUM(obj) % n; /* SCM_INUMP(obj) */
66 return (unsigned)(scm_c_downcase(SCM_CHAR(obj))) % n;
67 switch (SCM_UNPACK (obj)) {
69 case SCM_UNPACK(SCM_EOL):
73 case SCM_UNPACK(SCM_BOOL_T):
76 case SCM_UNPACK(SCM_BOOL_F):
79 case SCM_UNPACK(SCM_EOF_VAL):
83 d = 263; /* perhaps should be error */
87 return 263 % n; /* perhaps should be error */
89 switch SCM_TYP7(obj) {
95 switch SCM_TYP16 (obj) {
97 return scm_to_ulong (scm_modulo (obj, scm_from_ulong (n)));
100 double r = SCM_REAL_VALUE (obj);
103 obj = scm_inexact_to_exact (obj);
104 return scm_to_ulong (scm_modulo (obj, scm_from_ulong (n)));
108 case scm_tc16_complex:
109 case scm_tc16_fraction:
110 obj = scm_number_to_string (obj, scm_from_int (10));
117 scm_string_hash ((const unsigned char *) scm_i_string_chars (obj),
118 scm_i_string_length (obj)) % n;
119 scm_remember_upto_here_1 (obj);
123 return scm_i_symbol_hash (obj) % n;
127 size_t len = SCM_SIMPLE_VECTOR_LENGTH (obj);
134 SCM elt = SCM_SIMPLE_VECTOR_REF (obj, h % len);
135 h = ((h << 8) + (scm_hasher (elt, n, 2))) % n;
142 unsigned long h = (n)-1;
145 SCM elt = SCM_SIMPLE_VECTOR_REF (obj, h % len);
146 h = ((h << 8) + (scm_hasher (elt, n, d/len))) % n;
151 case scm_tcs_cons_imcar:
152 case scm_tcs_cons_nimcar:
153 if (d) return (scm_hasher (SCM_CAR (obj), n, d/2)
154 + scm_hasher (SCM_CDR (obj), n, d/2)) % n;
157 return ((SCM_RDNG & SCM_CELL_WORD_0 (obj)) ? 260 : 261) % n;
158 case scm_tcs_closures:
170 scm_ihashq (SCM obj, unsigned long n)
172 return (SCM_UNPACK (obj) >> 1) % n;
176 SCM_DEFINE (scm_hashq, "hashq", 2, 0, 0,
178 "Determine a hash value for @var{key} that is suitable for\n"
179 "lookups in a hashtable of size @var{size}, where @code{eq?} is\n"
180 "used as the equality predicate. The function returns an\n"
181 "integer in the range 0 to @var{size} - 1. Note that\n"
182 "@code{hashq} may use internal addresses. Thus two calls to\n"
183 "hashq where the keys are @code{eq?} are not guaranteed to\n"
184 "deliver the same value if the key object gets garbage collected\n"
185 "in between. This can happen, for example with symbols:\n"
186 "@code{(hashq 'foo n) (gc) (hashq 'foo n)} may produce two\n"
187 "different values, since @code{foo} will be garbage collected.")
188 #define FUNC_NAME s_scm_hashq
190 unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
191 return scm_from_ulong (scm_ihashq (key, sz));
200 scm_ihashv (SCM obj, unsigned long n)
203 return ((unsigned long) (scm_c_downcase (SCM_CHAR (obj)))) % n; /* downcase!?!! */
206 return (unsigned long) scm_hasher(obj, n, 10);
208 return SCM_UNPACK (obj) % n;
212 SCM_DEFINE (scm_hashv, "hashv", 2, 0, 0,
214 "Determine a hash value for @var{key} that is suitable for\n"
215 "lookups in a hashtable of size @var{size}, where @code{eqv?} is\n"
216 "used as the equality predicate. The function returns an\n"
217 "integer in the range 0 to @var{size} - 1. Note that\n"
218 "@code{(hashv key)} may use internal addresses. Thus two calls\n"
219 "to hashv where the keys are @code{eqv?} are not guaranteed to\n"
220 "deliver the same value if the key object gets garbage collected\n"
221 "in between. This can happen, for example with symbols:\n"
222 "@code{(hashv 'foo n) (gc) (hashv 'foo n)} may produce two\n"
223 "different values, since @code{foo} will be garbage collected.")
224 #define FUNC_NAME s_scm_hashv
226 unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
227 return scm_from_ulong (scm_ihashv (key, sz));
236 scm_ihash (SCM obj, unsigned long n)
238 return (unsigned long) scm_hasher (obj, n, 10);
241 SCM_DEFINE (scm_hash, "hash", 2, 0, 0,
243 "Determine a hash value for @var{key} that is suitable for\n"
244 "lookups in a hashtable of size @var{size}, where @code{equal?}\n"
245 "is used as the equality predicate. The function returns an\n"
246 "integer in the range 0 to @var{size} - 1.")
247 #define FUNC_NAME s_scm_hash
249 unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
250 return scm_from_ulong (scm_ihash (key, sz));
261 #include "libguile/hash.x"