1 /* Copyright (C) 1995,1996,1997, 2000, 2001, 2003, 2004, 2006, 2008, 2009, 2010, 2011 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
32 #include "libguile/_scm.h"
33 #include "libguile/chars.h"
34 #include "libguile/ports.h"
35 #include "libguile/strings.h"
36 #include "libguile/symbols.h"
37 #include "libguile/vectors.h"
39 #include "libguile/validate.h"
40 #include "libguile/hash.h"
44 extern double floor();
49 scm_string_hash (const unsigned char *str
, size_t len
)
51 /* from suggestion at: */
52 /* http://srfi.schemers.org/srfi-13/mail-archive/msg00112.html */
61 scm_i_string_hash (SCM str
)
63 size_t len
= scm_i_string_length (str
);
68 h
= (unsigned long) scm_i_string_ref (str
, i
++) + h
* 37;
70 scm_remember_upto_here_1 (str
);
75 scm_i_locale_string_hash (const char *str
, size_t len
)
80 size_t byte_idx
= 0, nbytes
;
83 if (len
== (size_t) -1)
86 while ((nbytes
= mbrtowc (&c
, str
+ byte_idx
, len
- byte_idx
, &state
)) > 0)
88 if (nbytes
>= (size_t) -2)
89 /* Invalid input string; punt. */
90 return scm_i_string_hash (scm_from_locale_stringn (str
, len
));
92 h
= (unsigned long) c
+ h
* 37;
98 return scm_i_string_hash (scm_from_locale_stringn (str
, len
));
103 scm_i_latin1_string_hash (const char *str
, size_t len
)
105 const scm_t_uint8
*ustr
= (const scm_t_uint8
*) str
;
109 if (len
== (size_t) -1)
113 h
= (unsigned long) ustr
[i
] + h
* 37;
119 scm_i_utf8_string_hash (const char *str
, size_t len
)
121 const scm_t_uint8
*ustr
= (const scm_t_uint8
*) str
;
125 if (len
== (size_t) -1)
128 while (byte_idx
< len
)
133 nbytes
= u8_mbtouc (&c
, ustr
+ byte_idx
, len
- byte_idx
);
137 /* Bad UTF-8; punt. */
138 return scm_i_string_hash (scm_from_utf8_stringn (str
, len
));
140 h
= (unsigned long) c
+ h
* 37;
148 /* Dirk:FIXME:: why downcase for characters? (2x: scm_hasher, scm_ihashv) */
149 /* Dirk:FIXME:: scm_hasher could be made static. */
153 scm_hasher(SCM obj
, unsigned long n
, size_t d
)
155 switch (SCM_ITAG3 (obj
)) {
158 return SCM_I_INUM(obj
) % n
; /* SCM_INUMP(obj) */
161 return (unsigned)(scm_c_downcase(SCM_CHAR(obj
))) % n
;
162 switch (SCM_UNPACK (obj
)) {
166 case SCM_BOOL_T_BITS
:
169 case SCM_BOOL_F_BITS
:
172 case SCM_EOF_VAL_BITS
:
176 d
= 263; /* perhaps should be error */
180 return 263 % n
; /* perhaps should be error */
182 switch SCM_TYP7(obj
) {
188 switch SCM_TYP16 (obj
) {
190 return scm_to_ulong (scm_modulo (obj
, scm_from_ulong (n
)));
193 double r
= SCM_REAL_VALUE (obj
);
194 if (floor (r
) == r
&& !isinf (r
) && !isnan (r
))
196 obj
= scm_inexact_to_exact (obj
);
197 return scm_to_ulong (scm_modulo (obj
, scm_from_ulong (n
)));
201 case scm_tc16_complex
:
202 case scm_tc16_fraction
:
203 obj
= scm_number_to_string (obj
, scm_from_int (10));
210 scm_i_string_hash (obj
) % n
;
214 return scm_i_symbol_hash (obj
) % n
;
215 case scm_tc7_pointer
:
217 /* Pointer objects are typically used to store addresses of heap
218 objects. On most platforms, these are at least 3-byte
219 aligned (on x86_64-*-gnu, `malloc' returns 4-byte aligned
220 addresses), so get rid of the least significant bits. */
221 scm_t_uintptr significant_bits
;
223 significant_bits
= (scm_t_uintptr
) SCM_POINTER_VALUE (obj
) >> 4UL;
224 return (size_t) significant_bits
% n
;
229 size_t len
= SCM_SIMPLE_VECTOR_LENGTH (obj
);
236 SCM elt
= SCM_SIMPLE_VECTOR_REF (obj
, h
% len
);
237 h
= ((h
<< 8) + (scm_hasher (elt
, n
, 2))) % n
;
244 unsigned long h
= (n
)-1;
247 SCM elt
= SCM_SIMPLE_VECTOR_REF (obj
, h
% len
);
248 h
= ((h
<< 8) + (scm_hasher (elt
, n
, d
/len
))) % n
;
253 case scm_tcs_cons_imcar
:
254 case scm_tcs_cons_nimcar
:
255 if (d
) return (scm_hasher (SCM_CAR (obj
), n
, d
/2)
256 + scm_hasher (SCM_CDR (obj
), n
, d
/2)) % n
;
259 return ((SCM_RDNG
& SCM_CELL_WORD_0 (obj
)) ? 260 : 261) % n
;
260 case scm_tc7_program
:
271 scm_ihashq (SCM obj
, unsigned long n
)
273 return (SCM_UNPACK (obj
) >> 1) % n
;
277 SCM_DEFINE (scm_hashq
, "hashq", 2, 0, 0,
279 "Determine a hash value for @var{key} that is suitable for\n"
280 "lookups in a hashtable of size @var{size}, where @code{eq?} is\n"
281 "used as the equality predicate. The function returns an\n"
282 "integer in the range 0 to @var{size} - 1. Note that\n"
283 "@code{hashq} may use internal addresses. Thus two calls to\n"
284 "hashq where the keys are @code{eq?} are not guaranteed to\n"
285 "deliver the same value if the key object gets garbage collected\n"
286 "in between. This can happen, for example with symbols:\n"
287 "@code{(hashq 'foo n) (gc) (hashq 'foo n)} may produce two\n"
288 "different values, since @code{foo} will be garbage collected.")
289 #define FUNC_NAME s_scm_hashq
291 unsigned long sz
= scm_to_unsigned_integer (size
, 1, ULONG_MAX
);
292 return scm_from_ulong (scm_ihashq (key
, sz
));
301 scm_ihashv (SCM obj
, unsigned long n
)
304 return ((unsigned long) (scm_c_downcase (SCM_CHAR (obj
)))) % n
; /* downcase!?!! */
307 return (unsigned long) scm_hasher(obj
, n
, 10);
309 return SCM_UNPACK (obj
) % n
;
313 SCM_DEFINE (scm_hashv
, "hashv", 2, 0, 0,
315 "Determine a hash value for @var{key} that is suitable for\n"
316 "lookups in a hashtable of size @var{size}, where @code{eqv?} is\n"
317 "used as the equality predicate. The function returns an\n"
318 "integer in the range 0 to @var{size} - 1. Note that\n"
319 "@code{(hashv key)} may use internal addresses. Thus two calls\n"
320 "to hashv where the keys are @code{eqv?} are not guaranteed to\n"
321 "deliver the same value if the key object gets garbage collected\n"
322 "in between. This can happen, for example with symbols:\n"
323 "@code{(hashv 'foo n) (gc) (hashv 'foo n)} may produce two\n"
324 "different values, since @code{foo} will be garbage collected.")
325 #define FUNC_NAME s_scm_hashv
327 unsigned long sz
= scm_to_unsigned_integer (size
, 1, ULONG_MAX
);
328 return scm_from_ulong (scm_ihashv (key
, sz
));
337 scm_ihash (SCM obj
, unsigned long n
)
339 return (unsigned long) scm_hasher (obj
, n
, 10);
342 SCM_DEFINE (scm_hash
, "hash", 2, 0, 0,
344 "Determine a hash value for @var{key} that is suitable for\n"
345 "lookups in a hashtable of size @var{size}, where @code{equal?}\n"
346 "is used as the equality predicate. The function returns an\n"
347 "integer in the range 0 to @var{size} - 1.")
348 #define FUNC_NAME s_scm_hash
350 unsigned long sz
= scm_to_unsigned_integer (size
, 1, ULONG_MAX
);
351 return scm_from_ulong (scm_ihash (key
, sz
));
362 #include "libguile/hash.x"