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