Commit | Line | Data |
---|---|---|
cc7005bc | 1 | /* Copyright (C) 1995,1996,1997, 2000, 2001, 2003, 2004, 2006, 2008, 2009, 2010 Free Software Foundation, Inc. |
0f2d19dd | 2 | * |
73be1d9e | 3 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
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. | |
0f2d19dd | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * Lesser General Public License for more details. | |
0f2d19dd | 12 | * |
73be1d9e MV |
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 | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
73be1d9e | 17 | */ |
1bbd0b84 | 18 | |
1bbd0b84 | 19 | |
0f2d19dd | 20 | \f |
dbb605f5 LC |
21 | #ifdef HAVE_CONFIG_H |
22 | # include <config.h> | |
23 | #endif | |
0f2d19dd | 24 | |
a0599745 MD |
25 | #include "libguile/_scm.h" |
26 | #include "libguile/chars.h" | |
27 | #include "libguile/ports.h" | |
a002f1a2 DH |
28 | #include "libguile/strings.h" |
29 | #include "libguile/symbols.h" | |
a0599745 | 30 | #include "libguile/vectors.h" |
0f2d19dd | 31 | |
a0599745 MD |
32 | #include "libguile/validate.h" |
33 | #include "libguile/hash.h" | |
0f2d19dd JB |
34 | \f |
35 | ||
36 | #ifndef floor | |
37 | extern double floor(); | |
38 | #endif | |
39 | ||
1cc91f1b | 40 | |
c014a02e | 41 | unsigned long |
1be6b49c | 42 | scm_string_hash (const unsigned char *str, size_t len) |
ba393257 | 43 | { |
b4d59261 MV |
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; | |
ba393257 DH |
51 | } |
52 | ||
e23106d5 MG |
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 | ||
ba393257 | 67 | |
dba97178 DH |
68 | /* Dirk:FIXME:: why downcase for characters? (2x: scm_hasher, scm_ihashv) */ |
69 | /* Dirk:FIXME:: scm_hasher could be made static. */ | |
70 | ||
71 | ||
c014a02e ML |
72 | unsigned long |
73 | scm_hasher(SCM obj, unsigned long n, size_t d) | |
0f2d19dd | 74 | { |
dba97178 DH |
75 | switch (SCM_ITAG3 (obj)) { |
76 | case scm_tc3_int_1: | |
77 | case scm_tc3_int_2: | |
e11e83f3 | 78 | return SCM_I_INUM(obj) % n; /* SCM_INUMP(obj) */ |
dba97178 DH |
79 | case scm_tc3_imm24: |
80 | if (SCM_CHARP(obj)) | |
84fad130 | 81 | return (unsigned)(scm_c_downcase(SCM_CHAR(obj))) % n; |
dba97178 | 82 | switch (SCM_UNPACK (obj)) { |
0f2d19dd | 83 | #ifndef SICP |
f228362b | 84 | case SCM_UNPACK(SCM_EOL): |
94a5efac GB |
85 | d = 256; |
86 | break; | |
0f2d19dd | 87 | #endif |
f228362b | 88 | case SCM_UNPACK(SCM_BOOL_T): |
94a5efac GB |
89 | d = 257; |
90 | break; | |
f228362b | 91 | case SCM_UNPACK(SCM_BOOL_F): |
94a5efac GB |
92 | d = 258; |
93 | break; | |
f228362b | 94 | case SCM_UNPACK(SCM_EOF_VAL): |
94a5efac GB |
95 | d = 259; |
96 | break; | |
97 | default: | |
98 | d = 263; /* perhaps should be error */ | |
0f2d19dd JB |
99 | } |
100 | return d % n; | |
94a5efac GB |
101 | default: |
102 | return 263 % n; /* perhaps should be error */ | |
dba97178 | 103 | case scm_tc3_cons: |
0f2d19dd | 104 | switch SCM_TYP7(obj) { |
94a5efac GB |
105 | default: |
106 | return 263 % n; | |
0f2d19dd | 107 | case scm_tc7_smob: |
534c55a9 DH |
108 | return 263 % n; |
109 | case scm_tc7_number: | |
1be6b49c | 110 | switch SCM_TYP16 (obj) { |
950cc72b | 111 | case scm_tc16_big: |
e11e83f3 | 112 | return scm_to_ulong (scm_modulo (obj, scm_from_ulong (n))); |
950cc72b MD |
113 | case scm_tc16_real: |
114 | { | |
1be6b49c | 115 | double r = SCM_REAL_VALUE (obj); |
e11e83f3 MV |
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 | } | |
0f2d19dd | 121 | } |
534c55a9 | 122 | /* Fall through */ |
950cc72b | 123 | case scm_tc16_complex: |
f92e85f7 | 124 | case scm_tc16_fraction: |
e11e83f3 | 125 | obj = scm_number_to_string (obj, scm_from_int (10)); |
534c55a9 | 126 | /* Fall through */ |
0f2d19dd | 127 | } |
534c55a9 | 128 | /* Fall through */ |
0f2d19dd | 129 | case scm_tc7_string: |
8824ac88 | 130 | { |
5a6d139b | 131 | unsigned long hash = |
e23106d5 | 132 | scm_i_string_hash (obj) % n; |
8824ac88 MV |
133 | scm_remember_upto_here_1 (obj); |
134 | return hash; | |
135 | } | |
28b06554 | 136 | case scm_tc7_symbol: |
cc95e00a | 137 | return scm_i_symbol_hash (obj) % n; |
0f2d19dd JB |
138 | case scm_tc7_wvect: |
139 | case scm_tc7_vector: | |
140 | { | |
4057a3e0 | 141 | size_t len = SCM_SIMPLE_VECTOR_LENGTH (obj); |
1be6b49c | 142 | if (len > 5) |
0f2d19dd | 143 | { |
1be6b49c | 144 | size_t i = d/2; |
c014a02e | 145 | unsigned long h = 1; |
4057a3e0 MV |
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 | } | |
0f2d19dd JB |
151 | return h; |
152 | } | |
153 | else | |
154 | { | |
1be6b49c | 155 | size_t i = len; |
c014a02e | 156 | unsigned long h = (n)-1; |
4057a3e0 MV |
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 | } | |
0f2d19dd JB |
162 | return h; |
163 | } | |
164 | } | |
94a5efac GB |
165 | case scm_tcs_cons_imcar: |
166 | case scm_tcs_cons_nimcar: | |
1be6b49c ML |
167 | if (d) return (scm_hasher (SCM_CAR (obj), n, d/2) |
168 | + scm_hasher (SCM_CDR (obj), n, d/2)) % n; | |
0f2d19dd JB |
169 | else return 1; |
170 | case scm_tc7_port: | |
206d3de3 | 171 | return ((SCM_RDNG & SCM_CELL_WORD_0 (obj)) ? 260 : 261) % n; |
cc7005bc | 172 | case scm_tc7_program: |
0f2d19dd JB |
173 | return 262 % n; |
174 | } | |
175 | } | |
176 | } | |
177 | ||
178 | ||
179 | \f | |
180 | ||
1cc91f1b | 181 | |
c014a02e ML |
182 | unsigned long |
183 | scm_ihashq (SCM obj, unsigned long n) | |
0f2d19dd | 184 | { |
54778cd3 | 185 | return (SCM_UNPACK (obj) >> 1) % n; |
0f2d19dd JB |
186 | } |
187 | ||
188 | ||
3b3b36dd | 189 | SCM_DEFINE (scm_hashq, "hashq", 2, 0, 0, |
94a5efac | 190 | (SCM key, SCM size), |
5352393c MG |
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.") | |
1bbd0b84 | 201 | #define FUNC_NAME s_scm_hashq |
0f2d19dd | 202 | { |
a55c2b68 MV |
203 | unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX); |
204 | return scm_from_ulong (scm_ihashq (key, sz)); | |
0f2d19dd | 205 | } |
1bbd0b84 | 206 | #undef FUNC_NAME |
0f2d19dd JB |
207 | |
208 | ||
209 | \f | |
210 | ||
1cc91f1b | 211 | |
c014a02e ML |
212 | unsigned long |
213 | scm_ihashv (SCM obj, unsigned long n) | |
0f2d19dd | 214 | { |
7866a09b | 215 | if (SCM_CHARP(obj)) |
84fad130 | 216 | return ((unsigned long) (scm_c_downcase (SCM_CHAR (obj)))) % n; /* downcase!?!! */ |
0f2d19dd | 217 | |
0c95b57d | 218 | if (SCM_NUMP(obj)) |
c014a02e | 219 | return (unsigned long) scm_hasher(obj, n, 10); |
0f2d19dd | 220 | else |
54778cd3 | 221 | return SCM_UNPACK (obj) % n; |
0f2d19dd JB |
222 | } |
223 | ||
224 | ||
3b3b36dd | 225 | SCM_DEFINE (scm_hashv, "hashv", 2, 0, 0, |
94a5efac | 226 | (SCM key, SCM size), |
5352393c MG |
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.") | |
1bbd0b84 | 237 | #define FUNC_NAME s_scm_hashv |
0f2d19dd | 238 | { |
a55c2b68 MV |
239 | unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX); |
240 | return scm_from_ulong (scm_ihashv (key, sz)); | |
0f2d19dd | 241 | } |
1bbd0b84 | 242 | #undef FUNC_NAME |
0f2d19dd JB |
243 | |
244 | ||
245 | \f | |
246 | ||
1cc91f1b | 247 | |
c014a02e ML |
248 | unsigned long |
249 | scm_ihash (SCM obj, unsigned long n) | |
0f2d19dd | 250 | { |
c014a02e | 251 | return (unsigned long) scm_hasher (obj, n, 10); |
0f2d19dd JB |
252 | } |
253 | ||
3b3b36dd | 254 | SCM_DEFINE (scm_hash, "hash", 2, 0, 0, |
94a5efac | 255 | (SCM key, SCM size), |
5352393c MG |
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.") | |
1bbd0b84 | 260 | #define FUNC_NAME s_scm_hash |
0f2d19dd | 261 | { |
a55c2b68 MV |
262 | unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX); |
263 | return scm_from_ulong (scm_ihash (key, sz)); | |
0f2d19dd | 264 | } |
1bbd0b84 | 265 | #undef FUNC_NAME |
0f2d19dd JB |
266 | |
267 | ||
268 | \f | |
269 | ||
1cc91f1b | 270 | |
0f2d19dd JB |
271 | void |
272 | scm_init_hash () | |
0f2d19dd | 273 | { |
a0599745 | 274 | #include "libguile/hash.x" |
0f2d19dd JB |
275 | } |
276 | ||
89e00824 ML |
277 | |
278 | /* | |
279 | Local Variables: | |
280 | c-file-style: "gnu" | |
281 | End: | |
282 | */ |