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 License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * 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
25 #include "libguile/_scm.h"
26 #include "libguile/chars.h"
27 #include "libguile/ports.h"
28 #include "libguile/strings.h"
29 #include "libguile/symbols.h"
30 #include "libguile/vectors.h"
32 #include "libguile/validate.h"
33 #include "libguile/hash.h"
37 extern double floor();
42 scm_string_hash (const unsigned char *str
, size_t len
)
44 /* from suggestion at: */
45 /* http://srfi.schemers.org/srfi-13/mail-archive/msg00112.html */
54 scm_i_string_hash (SCM str
)
56 size_t len
= scm_i_string_length (str
);
61 h
= (unsigned long) scm_i_string_ref (str
, i
++) + h
* 37;
63 scm_remember_upto_here_1 (str
);
68 /* Dirk:FIXME:: why downcase for characters? (2x: scm_hasher, scm_ihashv) */
69 /* Dirk:FIXME:: scm_hasher could be made static. */
73 scm_hasher(SCM obj
, unsigned long n
, size_t d
)
75 switch (SCM_ITAG3 (obj
)) {
78 return SCM_I_INUM(obj
) % n
; /* SCM_INUMP(obj) */
81 return (unsigned)(scm_c_downcase(SCM_CHAR(obj
))) % n
;
82 switch (SCM_UNPACK (obj
)) {
84 case SCM_UNPACK(SCM_EOL
):
88 case SCM_UNPACK(SCM_BOOL_T
):
91 case SCM_UNPACK(SCM_BOOL_F
):
94 case SCM_UNPACK(SCM_EOF_VAL
):
98 d
= 263; /* perhaps should be error */
102 return 263 % n
; /* perhaps should be error */
104 switch SCM_TYP7(obj
) {
110 switch SCM_TYP16 (obj
) {
112 return scm_to_ulong (scm_modulo (obj
, scm_from_ulong (n
)));
115 double r
= SCM_REAL_VALUE (obj
);
118 obj
= scm_inexact_to_exact (obj
);
119 return scm_to_ulong (scm_modulo (obj
, scm_from_ulong (n
)));
123 case scm_tc16_complex
:
124 case scm_tc16_fraction
:
125 obj
= scm_number_to_string (obj
, scm_from_int (10));
132 scm_i_string_hash (obj
) % n
;
133 scm_remember_upto_here_1 (obj
);
137 return scm_i_symbol_hash (obj
) % n
;
141 size_t len
= SCM_SIMPLE_VECTOR_LENGTH (obj
);
148 SCM elt
= SCM_SIMPLE_VECTOR_REF (obj
, h
% len
);
149 h
= ((h
<< 8) + (scm_hasher (elt
, n
, 2))) % n
;
156 unsigned long h
= (n
)-1;
159 SCM elt
= SCM_SIMPLE_VECTOR_REF (obj
, h
% len
);
160 h
= ((h
<< 8) + (scm_hasher (elt
, n
, d
/len
))) % n
;
165 case scm_tcs_cons_imcar
:
166 case scm_tcs_cons_nimcar
:
167 if (d
) return (scm_hasher (SCM_CAR (obj
), n
, d
/2)
168 + scm_hasher (SCM_CDR (obj
), n
, d
/2)) % n
;
171 return ((SCM_RDNG
& SCM_CELL_WORD_0 (obj
)) ? 260 : 261) % n
;
172 case scm_tcs_closures
:
184 scm_ihashq (SCM obj
, unsigned long n
)
186 return (SCM_UNPACK (obj
) >> 1) % n
;
190 SCM_DEFINE (scm_hashq
, "hashq", 2, 0, 0,
192 "Determine a hash value for @var{key} that is suitable for\n"
193 "lookups in a hashtable of size @var{size}, where @code{eq?} is\n"
194 "used as the equality predicate. The function returns an\n"
195 "integer in the range 0 to @var{size} - 1. Note that\n"
196 "@code{hashq} may use internal addresses. Thus two calls to\n"
197 "hashq where the keys are @code{eq?} are not guaranteed to\n"
198 "deliver the same value if the key object gets garbage collected\n"
199 "in between. This can happen, for example with symbols:\n"
200 "@code{(hashq 'foo n) (gc) (hashq 'foo n)} may produce two\n"
201 "different values, since @code{foo} will be garbage collected.")
202 #define FUNC_NAME s_scm_hashq
204 unsigned long sz
= scm_to_unsigned_integer (size
, 1, ULONG_MAX
);
205 return scm_from_ulong (scm_ihashq (key
, sz
));
214 scm_ihashv (SCM obj
, unsigned long n
)
217 return ((unsigned long) (scm_c_downcase (SCM_CHAR (obj
)))) % n
; /* downcase!?!! */
220 return (unsigned long) scm_hasher(obj
, n
, 10);
222 return SCM_UNPACK (obj
) % n
;
226 SCM_DEFINE (scm_hashv
, "hashv", 2, 0, 0,
228 "Determine a hash value for @var{key} that is suitable for\n"
229 "lookups in a hashtable of size @var{size}, where @code{eqv?} is\n"
230 "used as the equality predicate. The function returns an\n"
231 "integer in the range 0 to @var{size} - 1. Note that\n"
232 "@code{(hashv key)} may use internal addresses. Thus two calls\n"
233 "to hashv where the keys are @code{eqv?} are not guaranteed to\n"
234 "deliver the same value if the key object gets garbage collected\n"
235 "in between. This can happen, for example with symbols:\n"
236 "@code{(hashv 'foo n) (gc) (hashv 'foo n)} may produce two\n"
237 "different values, since @code{foo} will be garbage collected.")
238 #define FUNC_NAME s_scm_hashv
240 unsigned long sz
= scm_to_unsigned_integer (size
, 1, ULONG_MAX
);
241 return scm_from_ulong (scm_ihashv (key
, sz
));
250 scm_ihash (SCM obj
, unsigned long n
)
252 return (unsigned long) scm_hasher (obj
, n
, 10);
255 SCM_DEFINE (scm_hash
, "hash", 2, 0, 0,
257 "Determine a hash value for @var{key} that is suitable for\n"
258 "lookups in a hashtable of size @var{size}, where @code{equal?}\n"
259 "is used as the equality predicate. The function returns an\n"
260 "integer in the range 0 to @var{size} - 1.")
261 #define FUNC_NAME s_scm_hash
263 unsigned long sz
= scm_to_unsigned_integer (size
, 1, ULONG_MAX
);
264 return scm_from_ulong (scm_ihash (key
, sz
));
275 #include "libguile/hash.x"