Commit | Line | Data |
---|---|---|
dbb605f5 | 1 | /* Copyright (C) 1995,1996,1997, 2000, 2001, 2003, 2004, 2006, 2008 Free Software Foundation, Inc. |
0f2d19dd | 2 | * |
73be1d9e MV |
3 | * This library is free software; you can redistribute it and/or |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
0f2d19dd | 7 | * |
73be1d9e MV |
8 | * This library is distributed in the hope that it will be useful, |
9 | * but 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. | |
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 | |
92205699 | 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
73be1d9e | 16 | */ |
1bbd0b84 | 17 | |
1bbd0b84 | 18 | |
0f2d19dd | 19 | \f |
dbb605f5 LC |
20 | #ifdef HAVE_CONFIG_H |
21 | # include <config.h> | |
22 | #endif | |
0f2d19dd | 23 | |
a0599745 MD |
24 | #include "libguile/_scm.h" |
25 | #include "libguile/chars.h" | |
26 | #include "libguile/ports.h" | |
a002f1a2 DH |
27 | #include "libguile/strings.h" |
28 | #include "libguile/symbols.h" | |
a0599745 | 29 | #include "libguile/vectors.h" |
0f2d19dd | 30 | |
a0599745 MD |
31 | #include "libguile/validate.h" |
32 | #include "libguile/hash.h" | |
0f2d19dd JB |
33 | \f |
34 | ||
35 | #ifndef floor | |
36 | extern double floor(); | |
37 | #endif | |
38 | ||
1cc91f1b | 39 | |
c014a02e | 40 | unsigned long |
1be6b49c | 41 | scm_string_hash (const unsigned char *str, size_t len) |
ba393257 | 42 | { |
b4d59261 MV |
43 | /* from suggestion at: */ |
44 | /* http://srfi.schemers.org/srfi-13/mail-archive/msg00112.html */ | |
45 | ||
46 | unsigned long h = 0; | |
47 | while (len-- > 0) | |
48 | h = *str++ + h*37; | |
49 | return h; | |
ba393257 DH |
50 | } |
51 | ||
52 | ||
dba97178 DH |
53 | /* Dirk:FIXME:: why downcase for characters? (2x: scm_hasher, scm_ihashv) */ |
54 | /* Dirk:FIXME:: scm_hasher could be made static. */ | |
55 | ||
56 | ||
c014a02e ML |
57 | unsigned long |
58 | scm_hasher(SCM obj, unsigned long n, size_t d) | |
0f2d19dd | 59 | { |
dba97178 DH |
60 | switch (SCM_ITAG3 (obj)) { |
61 | case scm_tc3_int_1: | |
62 | case scm_tc3_int_2: | |
e11e83f3 | 63 | return SCM_I_INUM(obj) % n; /* SCM_INUMP(obj) */ |
dba97178 DH |
64 | case scm_tc3_imm24: |
65 | if (SCM_CHARP(obj)) | |
84fad130 | 66 | return (unsigned)(scm_c_downcase(SCM_CHAR(obj))) % n; |
dba97178 | 67 | switch (SCM_UNPACK (obj)) { |
0f2d19dd | 68 | #ifndef SICP |
f228362b | 69 | case SCM_UNPACK(SCM_EOL): |
94a5efac GB |
70 | d = 256; |
71 | break; | |
0f2d19dd | 72 | #endif |
f228362b | 73 | case SCM_UNPACK(SCM_BOOL_T): |
94a5efac GB |
74 | d = 257; |
75 | break; | |
f228362b | 76 | case SCM_UNPACK(SCM_BOOL_F): |
94a5efac GB |
77 | d = 258; |
78 | break; | |
f228362b | 79 | case SCM_UNPACK(SCM_EOF_VAL): |
94a5efac GB |
80 | d = 259; |
81 | break; | |
82 | default: | |
83 | d = 263; /* perhaps should be error */ | |
0f2d19dd JB |
84 | } |
85 | return d % n; | |
94a5efac GB |
86 | default: |
87 | return 263 % n; /* perhaps should be error */ | |
dba97178 | 88 | case scm_tc3_cons: |
0f2d19dd | 89 | switch SCM_TYP7(obj) { |
94a5efac GB |
90 | default: |
91 | return 263 % n; | |
0f2d19dd | 92 | case scm_tc7_smob: |
534c55a9 DH |
93 | return 263 % n; |
94 | case scm_tc7_number: | |
1be6b49c | 95 | switch SCM_TYP16 (obj) { |
950cc72b | 96 | case scm_tc16_big: |
e11e83f3 | 97 | return scm_to_ulong (scm_modulo (obj, scm_from_ulong (n))); |
950cc72b MD |
98 | case scm_tc16_real: |
99 | { | |
1be6b49c | 100 | double r = SCM_REAL_VALUE (obj); |
e11e83f3 MV |
101 | if (floor (r) == r) |
102 | { | |
103 | obj = scm_inexact_to_exact (obj); | |
104 | return scm_to_ulong (scm_modulo (obj, scm_from_ulong (n))); | |
105 | } | |
0f2d19dd | 106 | } |
534c55a9 | 107 | /* Fall through */ |
950cc72b | 108 | case scm_tc16_complex: |
f92e85f7 | 109 | case scm_tc16_fraction: |
e11e83f3 | 110 | obj = scm_number_to_string (obj, scm_from_int (10)); |
534c55a9 | 111 | /* Fall through */ |
0f2d19dd | 112 | } |
534c55a9 | 113 | /* Fall through */ |
0f2d19dd | 114 | case scm_tc7_string: |
8824ac88 | 115 | { |
5a6d139b NJ |
116 | unsigned long hash = |
117 | scm_string_hash ((const unsigned char *) scm_i_string_chars (obj), | |
118 | scm_i_string_length (obj)) % n; | |
8824ac88 MV |
119 | scm_remember_upto_here_1 (obj); |
120 | return hash; | |
121 | } | |
28b06554 | 122 | case scm_tc7_symbol: |
cc95e00a | 123 | return scm_i_symbol_hash (obj) % n; |
0f2d19dd JB |
124 | case scm_tc7_wvect: |
125 | case scm_tc7_vector: | |
126 | { | |
4057a3e0 | 127 | size_t len = SCM_SIMPLE_VECTOR_LENGTH (obj); |
1be6b49c | 128 | if (len > 5) |
0f2d19dd | 129 | { |
1be6b49c | 130 | size_t i = d/2; |
c014a02e | 131 | unsigned long h = 1; |
4057a3e0 MV |
132 | while (i--) |
133 | { | |
134 | SCM elt = SCM_SIMPLE_VECTOR_REF (obj, h % len); | |
135 | h = ((h << 8) + (scm_hasher (elt, n, 2))) % n; | |
136 | } | |
0f2d19dd JB |
137 | return h; |
138 | } | |
139 | else | |
140 | { | |
1be6b49c | 141 | size_t i = len; |
c014a02e | 142 | unsigned long h = (n)-1; |
4057a3e0 MV |
143 | while (i--) |
144 | { | |
145 | SCM elt = SCM_SIMPLE_VECTOR_REF (obj, h % len); | |
146 | h = ((h << 8) + (scm_hasher (elt, n, d/len))) % n; | |
147 | } | |
0f2d19dd JB |
148 | return h; |
149 | } | |
150 | } | |
94a5efac GB |
151 | case scm_tcs_cons_imcar: |
152 | case scm_tcs_cons_nimcar: | |
1be6b49c ML |
153 | if (d) return (scm_hasher (SCM_CAR (obj), n, d/2) |
154 | + scm_hasher (SCM_CDR (obj), n, d/2)) % n; | |
0f2d19dd JB |
155 | else return 1; |
156 | case scm_tc7_port: | |
206d3de3 | 157 | return ((SCM_RDNG & SCM_CELL_WORD_0 (obj)) ? 260 : 261) % n; |
94a5efac | 158 | case scm_tcs_closures: |
94a5efac | 159 | case scm_tcs_subrs: |
0f2d19dd JB |
160 | return 262 % n; |
161 | } | |
162 | } | |
163 | } | |
164 | ||
165 | ||
166 | \f | |
167 | ||
1cc91f1b | 168 | |
c014a02e ML |
169 | unsigned long |
170 | scm_ihashq (SCM obj, unsigned long n) | |
0f2d19dd | 171 | { |
54778cd3 | 172 | return (SCM_UNPACK (obj) >> 1) % n; |
0f2d19dd JB |
173 | } |
174 | ||
175 | ||
3b3b36dd | 176 | SCM_DEFINE (scm_hashq, "hashq", 2, 0, 0, |
94a5efac | 177 | (SCM key, SCM size), |
5352393c MG |
178 | "Determine a hash value for @var{key} that is suitable for\n" |
179 | "lookups in a hashtable of size @var{size}, where @code{eq?} is\n" | |
180 | "used as the equality predicate. The function returns an\n" | |
181 | "integer in the range 0 to @var{size} - 1. Note that\n" | |
182 | "@code{hashq} may use internal addresses. Thus two calls to\n" | |
183 | "hashq where the keys are @code{eq?} are not guaranteed to\n" | |
184 | "deliver the same value if the key object gets garbage collected\n" | |
185 | "in between. This can happen, for example with symbols:\n" | |
186 | "@code{(hashq 'foo n) (gc) (hashq 'foo n)} may produce two\n" | |
187 | "different values, since @code{foo} will be garbage collected.") | |
1bbd0b84 | 188 | #define FUNC_NAME s_scm_hashq |
0f2d19dd | 189 | { |
a55c2b68 MV |
190 | unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX); |
191 | return scm_from_ulong (scm_ihashq (key, sz)); | |
0f2d19dd | 192 | } |
1bbd0b84 | 193 | #undef FUNC_NAME |
0f2d19dd JB |
194 | |
195 | ||
196 | \f | |
197 | ||
1cc91f1b | 198 | |
c014a02e ML |
199 | unsigned long |
200 | scm_ihashv (SCM obj, unsigned long n) | |
0f2d19dd | 201 | { |
7866a09b | 202 | if (SCM_CHARP(obj)) |
84fad130 | 203 | return ((unsigned long) (scm_c_downcase (SCM_CHAR (obj)))) % n; /* downcase!?!! */ |
0f2d19dd | 204 | |
0c95b57d | 205 | if (SCM_NUMP(obj)) |
c014a02e | 206 | return (unsigned long) scm_hasher(obj, n, 10); |
0f2d19dd | 207 | else |
54778cd3 | 208 | return SCM_UNPACK (obj) % n; |
0f2d19dd JB |
209 | } |
210 | ||
211 | ||
3b3b36dd | 212 | SCM_DEFINE (scm_hashv, "hashv", 2, 0, 0, |
94a5efac | 213 | (SCM key, SCM size), |
5352393c MG |
214 | "Determine a hash value for @var{key} that is suitable for\n" |
215 | "lookups in a hashtable of size @var{size}, where @code{eqv?} is\n" | |
216 | "used as the equality predicate. The function returns an\n" | |
217 | "integer in the range 0 to @var{size} - 1. Note that\n" | |
218 | "@code{(hashv key)} may use internal addresses. Thus two calls\n" | |
219 | "to hashv where the keys are @code{eqv?} are not guaranteed to\n" | |
220 | "deliver the same value if the key object gets garbage collected\n" | |
221 | "in between. This can happen, for example with symbols:\n" | |
222 | "@code{(hashv 'foo n) (gc) (hashv 'foo n)} may produce two\n" | |
223 | "different values, since @code{foo} will be garbage collected.") | |
1bbd0b84 | 224 | #define FUNC_NAME s_scm_hashv |
0f2d19dd | 225 | { |
a55c2b68 MV |
226 | unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX); |
227 | return scm_from_ulong (scm_ihashv (key, sz)); | |
0f2d19dd | 228 | } |
1bbd0b84 | 229 | #undef FUNC_NAME |
0f2d19dd JB |
230 | |
231 | ||
232 | \f | |
233 | ||
1cc91f1b | 234 | |
c014a02e ML |
235 | unsigned long |
236 | scm_ihash (SCM obj, unsigned long n) | |
0f2d19dd | 237 | { |
c014a02e | 238 | return (unsigned long) scm_hasher (obj, n, 10); |
0f2d19dd JB |
239 | } |
240 | ||
3b3b36dd | 241 | SCM_DEFINE (scm_hash, "hash", 2, 0, 0, |
94a5efac | 242 | (SCM key, SCM size), |
5352393c MG |
243 | "Determine a hash value for @var{key} that is suitable for\n" |
244 | "lookups in a hashtable of size @var{size}, where @code{equal?}\n" | |
245 | "is used as the equality predicate. The function returns an\n" | |
246 | "integer in the range 0 to @var{size} - 1.") | |
1bbd0b84 | 247 | #define FUNC_NAME s_scm_hash |
0f2d19dd | 248 | { |
a55c2b68 MV |
249 | unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX); |
250 | return scm_from_ulong (scm_ihash (key, sz)); | |
0f2d19dd | 251 | } |
1bbd0b84 | 252 | #undef FUNC_NAME |
0f2d19dd JB |
253 | |
254 | ||
255 | \f | |
256 | ||
1cc91f1b | 257 | |
0f2d19dd JB |
258 | void |
259 | scm_init_hash () | |
0f2d19dd | 260 | { |
a0599745 | 261 | #include "libguile/hash.x" |
0f2d19dd JB |
262 | } |
263 | ||
89e00824 ML |
264 | |
265 | /* | |
266 | Local Variables: | |
267 | c-file-style: "gnu" | |
268 | End: | |
269 | */ |