1
/* Copyright (C) 1995,1996,1997, 2000, 2001, 2003, 2004, 2006 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
21
#include "libguile/_scm.h"
22
#include "libguile/chars.h"
23
#include "libguile/ports.h"
24
#include "libguile/strings.h"
25
#include "libguile/symbols.h"
26
#include "libguile/vectors.h"
28
#include "libguile/validate.h"
29
#include "libguile/hash.h"
33
extern double floor();
38
scm_string_hash (const unsigned char *str, size_t len)
40
/* from suggestion at: */
41
/* http://srfi.schemers.org/srfi-13/mail-archive/msg00112.html */
50
/* Dirk:FIXME:: why downcase for characters? (2x: scm_hasher, scm_ihashv) */
51
/* Dirk:FIXME:: scm_hasher could be made static. */
55
scm_hasher(SCM obj, unsigned long n, size_t d)
57
switch (SCM_ITAG3 (obj)) {
60
return SCM_I_INUM(obj) % n; /* SCM_INUMP(obj) */
63
return (unsigned)(scm_c_downcase(SCM_CHAR(obj))) % n;
64
switch (SCM_UNPACK (obj)) {
66
case SCM_UNPACK(SCM_EOL):
70
case SCM_UNPACK(SCM_BOOL_T):
73
case SCM_UNPACK(SCM_BOOL_F):
76
case SCM_UNPACK(SCM_EOF_VAL):
80
d = 263; /* perhaps should be error */
84
return 263 % n; /* perhaps should be error */
86
switch SCM_TYP7(obj) {
92
switch SCM_TYP16 (obj) {
94
return scm_to_ulong (scm_modulo (obj, scm_from_ulong (n)));
97
double r = SCM_REAL_VALUE (obj);
100
obj = scm_inexact_to_exact (obj);
101
return scm_to_ulong (scm_modulo (obj, scm_from_ulong (n)));
105
case scm_tc16_complex:
106
case scm_tc16_fraction:
107
obj = scm_number_to_string (obj, scm_from_int (10));
114
scm_string_hash ((const unsigned char *) scm_i_string_chars (obj),
115
scm_i_string_length (obj)) % n;
116
scm_remember_upto_here_1 (obj);
120
return scm_i_symbol_hash (obj) % n;
124
size_t len = SCM_SIMPLE_VECTOR_LENGTH (obj);
131
SCM elt = SCM_SIMPLE_VECTOR_REF (obj, h % len);
132
h = ((h << 8) + (scm_hasher (elt, n, 2))) % n;
139
unsigned long h = (n)-1;
142
SCM elt = SCM_SIMPLE_VECTOR_REF (obj, h % len);
143
h = ((h << 8) + (scm_hasher (elt, n, d/len))) % n;
148
case scm_tcs_cons_imcar:
149
case scm_tcs_cons_nimcar:
150
if (d) return (scm_hasher (SCM_CAR (obj), n, d/2)
151
+ scm_hasher (SCM_CDR (obj), n, d/2)) % n;
154
return ((SCM_RDNG & SCM_CELL_WORD_0 (obj)) ? 260 : 261) % n;
155
case scm_tcs_closures:
167
scm_ihashq (SCM obj, unsigned long n)
169
return (SCM_UNPACK (obj) >> 1) % n;
173
SCM_DEFINE (scm_hashq, "hashq", 2, 0, 0,
175
"Determine a hash value for @var{key} that is suitable for\n"
176
"lookups in a hashtable of size @var{size}, where @code{eq?} is\n"
177
"used as the equality predicate. The function returns an\n"
178
"integer in the range 0 to @var{size} - 1. Note that\n"
179
"@code{hashq} may use internal addresses. Thus two calls to\n"
180
"hashq where the keys are @code{eq?} are not guaranteed to\n"
181
"deliver the same value if the key object gets garbage collected\n"
182
"in between. This can happen, for example with symbols:\n"
183
"@code{(hashq 'foo n) (gc) (hashq 'foo n)} may produce two\n"
184
"different values, since @code{foo} will be garbage collected.")
185
#define FUNC_NAME s_scm_hashq
187
unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
188
return scm_from_ulong (scm_ihashq (key, sz));
197
scm_ihashv (SCM obj, unsigned long n)
200
return ((unsigned long) (scm_c_downcase (SCM_CHAR (obj)))) % n; /* downcase!?!! */
203
return (unsigned long) scm_hasher(obj, n, 10);
205
return SCM_UNPACK (obj) % n;
209
SCM_DEFINE (scm_hashv, "hashv", 2, 0, 0,
211
"Determine a hash value for @var{key} that is suitable for\n"
212
"lookups in a hashtable of size @var{size}, where @code{eqv?} is\n"
213
"used as the equality predicate. The function returns an\n"
214
"integer in the range 0 to @var{size} - 1. Note that\n"
215
"@code{(hashv key)} may use internal addresses. Thus two calls\n"
216
"to hashv where the keys are @code{eqv?} are not guaranteed to\n"
217
"deliver the same value if the key object gets garbage collected\n"
218
"in between. This can happen, for example with symbols:\n"
219
"@code{(hashv 'foo n) (gc) (hashv 'foo n)} may produce two\n"
220
"different values, since @code{foo} will be garbage collected.")
221
#define FUNC_NAME s_scm_hashv
223
unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
224
return scm_from_ulong (scm_ihashv (key, sz));
233
scm_ihash (SCM obj, unsigned long n)
235
return (unsigned long) scm_hasher (obj, n, 10);
238
SCM_DEFINE (scm_hash, "hash", 2, 0, 0,
240
"Determine a hash value for @var{key} that is suitable for\n"
241
"lookups in a hashtable of size @var{size}, where @code{equal?}\n"
242
"is used as the equality predicate. The function returns an\n"
243
"integer in the range 0 to @var{size} - 1.")
244
#define FUNC_NAME s_scm_hash
246
unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
247
return scm_from_ulong (scm_ihash (key, sz));
258
#include "libguile/hash.x"