1 /* Copyright (C) 1995, 1996, 1997, 2000, 2001, 2003, 2004, 2006, 2008,
2 * 2009, 2010, 2011, 2012, 2014 Free Software Foundation, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
33 #include "libguile/_scm.h"
34 #include "libguile/chars.h"
35 #include "libguile/ports.h"
36 #include "libguile/strings.h"
37 #include "libguile/symbols.h"
38 #include "libguile/vectors.h"
40 #include "libguile/validate.h"
41 #include "libguile/hash.h"
45 extern double floor();
48 #define SCM_MIN(A, B) ((A) < (B) ? (A) : (B))
51 scm_string_hash (const unsigned char *str
, size_t len
)
53 /* from suggestion at: */
54 /* http://srfi.schemers.org/srfi-13/mail-archive/msg00112.html */
63 scm_i_string_hash (SCM str
)
65 size_t len
= scm_i_string_length (str
);
70 h
= (unsigned long) scm_i_string_ref (str
, i
++) + h
* 37;
72 scm_remember_upto_here_1 (str
);
77 scm_i_locale_string_hash (const char *str
, size_t len
)
82 size_t byte_idx
= 0, nbytes
;
85 if (len
== (size_t) -1)
88 while ((nbytes
= mbrtowc (&c
, str
+ byte_idx
, len
- byte_idx
, &state
)) > 0)
90 if (nbytes
>= (size_t) -2)
91 /* Invalid input string; punt. */
92 return scm_i_string_hash (scm_from_locale_stringn (str
, len
));
94 h
= (unsigned long) c
+ h
* 37;
100 return scm_i_string_hash (scm_from_locale_stringn (str
, len
));
105 scm_i_latin1_string_hash (const char *str
, size_t len
)
107 const scm_t_uint8
*ustr
= (const scm_t_uint8
*) str
;
111 if (len
== (size_t) -1)
115 h
= (unsigned long) ustr
[i
] + h
* 37;
121 scm_i_utf8_string_hash (const char *str
, size_t len
)
123 const scm_t_uint8
*ustr
= (const scm_t_uint8
*) str
;
127 if (len
== (size_t) -1)
130 while (byte_idx
< len
)
135 nbytes
= u8_mbtouc (&c
, ustr
+ byte_idx
, len
- byte_idx
);
139 /* Bad UTF-8; punt. */
140 return scm_i_string_hash (scm_from_utf8_stringn (str
, len
));
142 h
= (unsigned long) c
+ h
* 37;
150 /* Dirk:FIXME:: why downcase for characters? (2x: scm_hasher, scm_ihashv) */
151 /* Dirk:FIXME:: scm_hasher could be made static. */
155 scm_hasher(SCM obj
, unsigned long n
, size_t d
)
157 switch (SCM_ITAG3 (obj
)) {
160 return SCM_I_INUM(obj
) % n
; /* SCM_INUMP(obj) */
163 return (unsigned)(scm_c_downcase(SCM_CHAR(obj
))) % n
;
164 switch (SCM_UNPACK (obj
)) {
168 case SCM_BOOL_T_BITS
:
171 case SCM_BOOL_F_BITS
:
174 case SCM_EOF_VAL_BITS
:
178 d
= 263; /* perhaps should be error */
182 return 263 % n
; /* perhaps should be error */
184 switch SCM_TYP7(obj
) {
190 switch SCM_TYP16 (obj
) {
192 return scm_to_ulong (scm_modulo (obj
, scm_from_ulong (n
)));
195 double r
= SCM_REAL_VALUE (obj
);
196 if (floor (r
) == r
&& !isinf (r
) && !isnan (r
))
198 obj
= scm_inexact_to_exact (obj
);
199 return scm_to_ulong (scm_modulo (obj
, scm_from_ulong (n
)));
203 case scm_tc16_complex
:
204 case scm_tc16_fraction
:
205 obj
= scm_number_to_string (obj
, scm_from_int (10));
212 scm_i_string_hash (obj
) % n
;
216 return scm_i_symbol_hash (obj
) % n
;
217 case scm_tc7_pointer
:
219 /* Pointer objects are typically used to store addresses of heap
220 objects. On most platforms, these are at least 3-byte
221 aligned (on x86_64-*-gnu, `malloc' returns 4-byte aligned
222 addresses), so get rid of the least significant bits. */
223 scm_t_uintptr significant_bits
;
225 significant_bits
= (scm_t_uintptr
) SCM_POINTER_VALUE (obj
) >> 4UL;
226 return (size_t) significant_bits
% n
;
229 return scm_i_struct_hash (obj
, n
, d
);
237 len
= SCM_SIMPLE_VECTOR_LENGTH (obj
);
242 d2
= SCM_MIN (2, d
- 1);
248 d2
= len
> 0 ? (d
- 1) / len
: 0;
253 SCM elt
= SCM_SIMPLE_VECTOR_REF (obj
, h
% len
);
254 h
= ((h
<< 8) + (scm_hasher (elt
, n
, d2
))) % n
;
260 case scm_tcs_cons_imcar
:
261 case scm_tcs_cons_nimcar
:
262 if (d
) return (scm_hasher (SCM_CAR (obj
), n
, d
/2)
263 + scm_hasher (SCM_CDR (obj
), n
, d
/2)) % n
;
266 return ((SCM_RDNG
& SCM_CELL_WORD_0 (obj
)) ? 260 : 261) % n
;
267 case scm_tc7_program
:
278 scm_ihashq (SCM obj
, unsigned long n
)
280 return (SCM_UNPACK (obj
) >> 1) % n
;
284 SCM_DEFINE (scm_hashq
, "hashq", 2, 0, 0,
286 "Determine a hash value for @var{key} that is suitable for\n"
287 "lookups in a hashtable of size @var{size}, where @code{eq?} is\n"
288 "used as the equality predicate. The function returns an\n"
289 "integer in the range 0 to @var{size} - 1. Note that\n"
290 "@code{hashq} may use internal addresses. Thus two calls to\n"
291 "hashq where the keys are @code{eq?} are not guaranteed to\n"
292 "deliver the same value if the key object gets garbage collected\n"
293 "in between. This can happen, for example with symbols:\n"
294 "@code{(hashq 'foo n) (gc) (hashq 'foo n)} may produce two\n"
295 "different values, since @code{foo} will be garbage collected.")
296 #define FUNC_NAME s_scm_hashq
298 unsigned long sz
= scm_to_unsigned_integer (size
, 1, ULONG_MAX
);
299 return scm_from_ulong (scm_ihashq (key
, sz
));
308 scm_ihashv (SCM obj
, unsigned long n
)
311 return ((unsigned long) (scm_c_downcase (SCM_CHAR (obj
)))) % n
; /* downcase!?!! */
314 return (unsigned long) scm_hasher(obj
, n
, 10);
316 return SCM_UNPACK (obj
) % n
;
320 SCM_DEFINE (scm_hashv
, "hashv", 2, 0, 0,
322 "Determine a hash value for @var{key} that is suitable for\n"
323 "lookups in a hashtable of size @var{size}, where @code{eqv?} is\n"
324 "used as the equality predicate. The function returns an\n"
325 "integer in the range 0 to @var{size} - 1. Note that\n"
326 "@code{(hashv key)} may use internal addresses. Thus two calls\n"
327 "to hashv where the keys are @code{eqv?} are not guaranteed to\n"
328 "deliver the same value if the key object gets garbage collected\n"
329 "in between. This can happen, for example with symbols:\n"
330 "@code{(hashv 'foo n) (gc) (hashv 'foo n)} may produce two\n"
331 "different values, since @code{foo} will be garbage collected.")
332 #define FUNC_NAME s_scm_hashv
334 unsigned long sz
= scm_to_unsigned_integer (size
, 1, ULONG_MAX
);
335 return scm_from_ulong (scm_ihashv (key
, sz
));
344 scm_ihash (SCM obj
, unsigned long n
)
346 return (unsigned long) scm_hasher (obj
, n
, 10);
349 SCM_DEFINE (scm_hash
, "hash", 2, 0, 0,
351 "Determine a hash value for @var{key} that is suitable for\n"
352 "lookups in a hashtable of size @var{size}, where @code{equal?}\n"
353 "is used as the equality predicate. The function returns an\n"
354 "integer in the range 0 to @var{size} - 1.")
355 #define FUNC_NAME s_scm_hash
357 unsigned long sz
= scm_to_unsigned_integer (size
, 1, ULONG_MAX
);
358 return scm_from_ulong (scm_ihash (key
, sz
));
369 #include "libguile/hash.x"