remove scm_tc7_gsubr
[bpt/guile.git] / libguile / hash.c
1 /* Copyright (C) 1995,1996,1997, 2000, 2001, 2003, 2004, 2006, 2008, 2009, 2010 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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
16 * 02110-1301 USA
17 */
18
19
20 \f
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
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"
31
32 #include "libguile/validate.h"
33 #include "libguile/hash.h"
34 \f
35
36 #ifndef floor
37 extern double floor();
38 #endif
39
40
41 unsigned long
42 scm_string_hash (const unsigned char *str, size_t len)
43 {
44 /* from suggestion at: */
45 /* http://srfi.schemers.org/srfi-13/mail-archive/msg00112.html */
46
47 unsigned long h = 0;
48 while (len-- > 0)
49 h = *str++ + h*37;
50 return h;
51 }
52
53 unsigned long
54 scm_i_string_hash (SCM str)
55 {
56 size_t len = scm_i_string_length (str);
57 size_t i = 0;
58
59 unsigned long h = 0;
60 while (len-- > 0)
61 h = (unsigned long) scm_i_string_ref (str, i++) + h * 37;
62
63 scm_remember_upto_here_1 (str);
64 return h;
65 }
66
67
68 /* Dirk:FIXME:: why downcase for characters? (2x: scm_hasher, scm_ihashv) */
69 /* Dirk:FIXME:: scm_hasher could be made static. */
70
71
72 unsigned long
73 scm_hasher(SCM obj, unsigned long n, size_t d)
74 {
75 switch (SCM_ITAG3 (obj)) {
76 case scm_tc3_int_1:
77 case scm_tc3_int_2:
78 return SCM_I_INUM(obj) % n; /* SCM_INUMP(obj) */
79 case scm_tc3_imm24:
80 if (SCM_CHARP(obj))
81 return (unsigned)(scm_c_downcase(SCM_CHAR(obj))) % n;
82 switch (SCM_UNPACK (obj)) {
83 #ifndef SICP
84 case SCM_UNPACK(SCM_EOL):
85 d = 256;
86 break;
87 #endif
88 case SCM_UNPACK(SCM_BOOL_T):
89 d = 257;
90 break;
91 case SCM_UNPACK(SCM_BOOL_F):
92 d = 258;
93 break;
94 case SCM_UNPACK(SCM_EOF_VAL):
95 d = 259;
96 break;
97 default:
98 d = 263; /* perhaps should be error */
99 }
100 return d % n;
101 default:
102 return 263 % n; /* perhaps should be error */
103 case scm_tc3_cons:
104 switch SCM_TYP7(obj) {
105 default:
106 return 263 % n;
107 case scm_tc7_smob:
108 return 263 % n;
109 case scm_tc7_number:
110 switch SCM_TYP16 (obj) {
111 case scm_tc16_big:
112 return scm_to_ulong (scm_modulo (obj, scm_from_ulong (n)));
113 case scm_tc16_real:
114 {
115 double r = SCM_REAL_VALUE (obj);
116 if (floor (r) == r)
117 {
118 obj = scm_inexact_to_exact (obj);
119 return scm_to_ulong (scm_modulo (obj, scm_from_ulong (n)));
120 }
121 }
122 /* Fall through */
123 case scm_tc16_complex:
124 case scm_tc16_fraction:
125 obj = scm_number_to_string (obj, scm_from_int (10));
126 /* Fall through */
127 }
128 /* Fall through */
129 case scm_tc7_string:
130 {
131 unsigned long hash =
132 scm_i_string_hash (obj) % n;
133 scm_remember_upto_here_1 (obj);
134 return hash;
135 }
136 case scm_tc7_symbol:
137 return scm_i_symbol_hash (obj) % n;
138 case scm_tc7_wvect:
139 case scm_tc7_vector:
140 {
141 size_t len = SCM_SIMPLE_VECTOR_LENGTH (obj);
142 if (len > 5)
143 {
144 size_t i = d/2;
145 unsigned long h = 1;
146 while (i--)
147 {
148 SCM elt = SCM_SIMPLE_VECTOR_REF (obj, h % len);
149 h = ((h << 8) + (scm_hasher (elt, n, 2))) % n;
150 }
151 return h;
152 }
153 else
154 {
155 size_t i = len;
156 unsigned long h = (n)-1;
157 while (i--)
158 {
159 SCM elt = SCM_SIMPLE_VECTOR_REF (obj, h % len);
160 h = ((h << 8) + (scm_hasher (elt, n, d/len))) % n;
161 }
162 return h;
163 }
164 }
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;
169 else return 1;
170 case scm_tc7_port:
171 return ((SCM_RDNG & SCM_CELL_WORD_0 (obj)) ? 260 : 261) % n;
172 case scm_tc7_program:
173 return 262 % n;
174 }
175 }
176 }
177
178
179 \f
180
181
182 unsigned long
183 scm_ihashq (SCM obj, unsigned long n)
184 {
185 return (SCM_UNPACK (obj) >> 1) % n;
186 }
187
188
189 SCM_DEFINE (scm_hashq, "hashq", 2, 0, 0,
190 (SCM key, SCM size),
191 "Determine a hash value for @var{key} that is suitable for\n"
192 "lookups in a hashtable of size @var{size}, where @code{eq?} is\n"
193 "used as the equality predicate. The function returns an\n"
194 "integer in the range 0 to @var{size} - 1. Note that\n"
195 "@code{hashq} may use internal addresses. Thus two calls to\n"
196 "hashq where the keys are @code{eq?} are not guaranteed to\n"
197 "deliver the same value if the key object gets garbage collected\n"
198 "in between. This can happen, for example with symbols:\n"
199 "@code{(hashq 'foo n) (gc) (hashq 'foo n)} may produce two\n"
200 "different values, since @code{foo} will be garbage collected.")
201 #define FUNC_NAME s_scm_hashq
202 {
203 unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
204 return scm_from_ulong (scm_ihashq (key, sz));
205 }
206 #undef FUNC_NAME
207
208
209 \f
210
211
212 unsigned long
213 scm_ihashv (SCM obj, unsigned long n)
214 {
215 if (SCM_CHARP(obj))
216 return ((unsigned long) (scm_c_downcase (SCM_CHAR (obj)))) % n; /* downcase!?!! */
217
218 if (SCM_NUMP(obj))
219 return (unsigned long) scm_hasher(obj, n, 10);
220 else
221 return SCM_UNPACK (obj) % n;
222 }
223
224
225 SCM_DEFINE (scm_hashv, "hashv", 2, 0, 0,
226 (SCM key, SCM size),
227 "Determine a hash value for @var{key} that is suitable for\n"
228 "lookups in a hashtable of size @var{size}, where @code{eqv?} is\n"
229 "used as the equality predicate. The function returns an\n"
230 "integer in the range 0 to @var{size} - 1. Note that\n"
231 "@code{(hashv key)} may use internal addresses. Thus two calls\n"
232 "to hashv where the keys are @code{eqv?} are not guaranteed to\n"
233 "deliver the same value if the key object gets garbage collected\n"
234 "in between. This can happen, for example with symbols:\n"
235 "@code{(hashv 'foo n) (gc) (hashv 'foo n)} may produce two\n"
236 "different values, since @code{foo} will be garbage collected.")
237 #define FUNC_NAME s_scm_hashv
238 {
239 unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
240 return scm_from_ulong (scm_ihashv (key, sz));
241 }
242 #undef FUNC_NAME
243
244
245 \f
246
247
248 unsigned long
249 scm_ihash (SCM obj, unsigned long n)
250 {
251 return (unsigned long) scm_hasher (obj, n, 10);
252 }
253
254 SCM_DEFINE (scm_hash, "hash", 2, 0, 0,
255 (SCM key, SCM size),
256 "Determine a hash value for @var{key} that is suitable for\n"
257 "lookups in a hashtable of size @var{size}, where @code{equal?}\n"
258 "is used as the equality predicate. The function returns an\n"
259 "integer in the range 0 to @var{size} - 1.")
260 #define FUNC_NAME s_scm_hash
261 {
262 unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
263 return scm_from_ulong (scm_ihash (key, sz));
264 }
265 #undef FUNC_NAME
266
267
268 \f
269
270
271 void
272 scm_init_hash ()
273 {
274 #include "libguile/hash.x"
275 }
276
277
278 /*
279 Local Variables:
280 c-file-style: "gnu"
281 End:
282 */