Commit | Line | Data |
---|---|---|
22a52da1 | 1 | /* Copyright (C) 1995,1996,1998,1999,2000,2001 Free Software Foundation, Inc. |
0f2d19dd JB |
2 | * |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2, or (at your option) | |
6 | * any later version. | |
7 | * | |
8 | * This program 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 | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this software; see the file COPYING. If not, write to | |
82892bed JB |
15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
16 | * Boston, MA 02111-1307 USA | |
0f2d19dd JB |
17 | * |
18 | * As a special exception, the Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of GUILE. | |
20 | * | |
21 | * The exception is that, if you link the GUILE library with other files | |
22 | * to produce an executable, this does not by itself cause the | |
23 | * resulting executable to be covered by the GNU General Public License. | |
24 | * Your use of that executable is in no way restricted on account of | |
25 | * linking the GUILE library code into it. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
30 | * This exception applies only to the code released by the | |
31 | * Free Software Foundation under the name GUILE. If you copy | |
32 | * code from other Free Software Foundation releases into a copy of | |
33 | * GUILE, as the General Public License permits, the exception does | |
34 | * not apply to the code that you add in this way. To avoid misleading | |
35 | * anyone as to the status of such modified files, you must delete | |
36 | * this exception notice from them. | |
37 | * | |
38 | * If you write modifications of your own for GUILE, it is your choice | |
39 | * whether to permit this exception to apply to your modifications. | |
82892bed | 40 | * If you do not wish that, delete this exception notice. */ |
1bbd0b84 | 41 | |
1bbd0b84 | 42 | |
0f2d19dd JB |
43 | \f |
44 | ||
a0599745 MD |
45 | #include "libguile/_scm.h" |
46 | #include "libguile/alist.h" | |
47 | #include "libguile/hash.h" | |
48 | #include "libguile/eval.h" | |
fdc28395 | 49 | #include "libguile/root.h" |
a0599745 MD |
50 | #include "libguile/vectors.h" |
51 | ||
52 | #include "libguile/validate.h" | |
53 | #include "libguile/hashtab.h" | |
0f2d19dd JB |
54 | \f |
55 | ||
00ffa0e7 | 56 | SCM |
c014a02e | 57 | scm_c_make_hash_table (unsigned long k) |
00ffa0e7 KN |
58 | { |
59 | return scm_c_make_vector (k, SCM_EOL); | |
60 | } | |
1cc91f1b | 61 | |
22a52da1 | 62 | |
0f2d19dd | 63 | SCM |
34d19ef6 | 64 | scm_hash_fn_get_handle (SCM table, SCM obj, unsigned long (*hash_fn)(), SCM (*assoc_fn)(), void * closure) |
22a52da1 | 65 | #define FUNC_NAME "scm_hash_fn_get_handle" |
0f2d19dd | 66 | { |
c014a02e | 67 | unsigned long k; |
0f2d19dd JB |
68 | SCM h; |
69 | ||
22a52da1 | 70 | SCM_VALIDATE_VECTOR (1, table); |
bfa974f0 | 71 | if (SCM_VECTOR_LENGTH (table) == 0) |
22a52da1 | 72 | return SCM_BOOL_F; |
bfa974f0 DH |
73 | k = hash_fn (obj, SCM_VECTOR_LENGTH (table), closure); |
74 | if (k >= SCM_VECTOR_LENGTH (table)) | |
685c0d71 | 75 | scm_out_of_range ("hash_fn_get_handle", scm_ulong2num (k)); |
0f2d19dd JB |
76 | h = assoc_fn (obj, SCM_VELTS (table)[k], closure); |
77 | return h; | |
78 | } | |
22a52da1 | 79 | #undef FUNC_NAME |
0f2d19dd JB |
80 | |
81 | ||
0f2d19dd | 82 | SCM |
34d19ef6 HWN |
83 | scm_hash_fn_create_handle_x (SCM table, SCM obj, SCM init, unsigned long (*hash_fn)(), |
84 | SCM (*assoc_fn)(), void * closure) | |
cbaadf02 | 85 | #define FUNC_NAME "scm_hash_fn_create_handle_x" |
0f2d19dd | 86 | { |
c014a02e | 87 | unsigned long k; |
0f2d19dd JB |
88 | SCM it; |
89 | ||
0c95b57d | 90 | SCM_ASSERT (SCM_VECTORP (table), table, SCM_ARG1, "hash_fn_create_handle_x"); |
bfa974f0 | 91 | if (SCM_VECTOR_LENGTH (table) == 0) |
cbaadf02 DH |
92 | SCM_MISC_ERROR ("void hashtable", SCM_EOL); |
93 | ||
bfa974f0 DH |
94 | k = hash_fn (obj, SCM_VECTOR_LENGTH (table), closure); |
95 | if (k >= SCM_VECTOR_LENGTH (table)) | |
685c0d71 | 96 | scm_out_of_range ("hash_fn_create_handle_x", scm_ulong2num (k)); |
0f2d19dd JB |
97 | SCM_REDEFER_INTS; |
98 | it = assoc_fn (obj, SCM_VELTS (table)[k], closure); | |
ee083ac2 | 99 | if (!SCM_FALSEP (it)) |
0f2d19dd | 100 | { |
686765af | 101 | SCM_REALLOW_INTS; |
0f2d19dd JB |
102 | return it; |
103 | } | |
ee083ac2 DH |
104 | else |
105 | { | |
106 | SCM new_bucket; | |
107 | SCM old_bucket; | |
108 | old_bucket = SCM_VELTS (table)[k]; | |
109 | new_bucket = scm_acons (obj, init, old_bucket); | |
34d19ef6 | 110 | SCM_VECTOR_SET (table, k, new_bucket); |
ee083ac2 DH |
111 | SCM_REALLOW_INTS; |
112 | return SCM_CAR (new_bucket); | |
113 | } | |
0f2d19dd | 114 | } |
cbaadf02 | 115 | #undef FUNC_NAME |
0f2d19dd | 116 | |
1cc91f1b | 117 | |
0f2d19dd | 118 | SCM |
34d19ef6 HWN |
119 | scm_hash_fn_ref (SCM table, SCM obj, SCM dflt, unsigned long (*hash_fn)(), |
120 | SCM (*assoc_fn)(), void * closure) | |
0f2d19dd | 121 | { |
22a52da1 DH |
122 | SCM it = scm_hash_fn_get_handle (table, obj, hash_fn, assoc_fn, closure); |
123 | if (SCM_CONSP (it)) | |
0f2d19dd | 124 | return SCM_CDR (it); |
22a52da1 DH |
125 | else |
126 | return dflt; | |
0f2d19dd JB |
127 | } |
128 | ||
129 | ||
130 | ||
1cc91f1b | 131 | |
0f2d19dd | 132 | SCM |
34d19ef6 HWN |
133 | scm_hash_fn_set_x (SCM table, SCM obj, SCM val, unsigned long (*hash_fn)(), |
134 | SCM (*assoc_fn)(), void * closure) | |
0f2d19dd JB |
135 | { |
136 | SCM it; | |
137 | ||
138 | it = scm_hash_fn_create_handle_x (table, obj, SCM_BOOL_F, hash_fn, assoc_fn, closure); | |
139 | SCM_SETCDR (it, val); | |
140 | return val; | |
141 | } | |
142 | ||
143 | ||
144 | ||
145 | ||
1cc91f1b | 146 | |
0f2d19dd | 147 | SCM |
34d19ef6 HWN |
148 | scm_hash_fn_remove_x (SCM table, SCM obj, unsigned long (*hash_fn)(), SCM (*assoc_fn)(), |
149 | SCM (*delete_fn)(), void * closure) | |
0f2d19dd | 150 | { |
c014a02e | 151 | unsigned long k; |
0f2d19dd JB |
152 | SCM h; |
153 | ||
0c95b57d | 154 | SCM_ASSERT (SCM_VECTORP (table), table, SCM_ARG1, "hash_fn_remove_x"); |
bfa974f0 | 155 | if (SCM_VECTOR_LENGTH (table) == 0) |
0f2d19dd | 156 | return SCM_EOL; |
bfa974f0 DH |
157 | k = hash_fn (obj, SCM_VECTOR_LENGTH (table), closure); |
158 | if (k >= SCM_VECTOR_LENGTH (table)) | |
685c0d71 | 159 | scm_out_of_range ("hash_fn_remove_x", scm_ulong2num (k)); |
0f2d19dd | 160 | h = assoc_fn (obj, SCM_VELTS (table)[k], closure); |
34d19ef6 | 161 | SCM_VECTOR_SET (table, k, delete_fn (h, SCM_VELTS(table)[k])); |
0f2d19dd JB |
162 | return h; |
163 | } | |
164 | ||
165 | ||
166 | \f | |
167 | ||
a1ec6916 | 168 | SCM_DEFINE (scm_hashq_get_handle, "hashq-get-handle", 2, 0, 0, |
22a52da1 DH |
169 | (SCM table, SCM key), |
170 | "This procedure returns the @code{(key . value)} pair from the\n" | |
171 | "hash table @var{table}. If @var{table} does not hold an\n" | |
172 | "associated value for @var{key}, @code{#f} is returned.\n" | |
173 | "Uses @code{eq?} for equality testing.") | |
1bbd0b84 | 174 | #define FUNC_NAME s_scm_hashq_get_handle |
0f2d19dd | 175 | { |
22a52da1 | 176 | return scm_hash_fn_get_handle (table, key, scm_ihashq, scm_sloppy_assq, 0); |
0f2d19dd | 177 | } |
1bbd0b84 | 178 | #undef FUNC_NAME |
0f2d19dd JB |
179 | |
180 | ||
a1ec6916 | 181 | SCM_DEFINE (scm_hashq_create_handle_x, "hashq-create-handle!", 3, 0, 0, |
d550d22a GB |
182 | (SCM table, SCM key, SCM init), |
183 | "This function looks up @var{key} in @var{table} and returns its handle.\n" | |
b380b885 MD |
184 | "If @var{key} is not already present, a new handle is created which\n" |
185 | "associates @var{key} with @var{init}.") | |
1bbd0b84 | 186 | #define FUNC_NAME s_scm_hashq_create_handle_x |
0f2d19dd | 187 | { |
d550d22a | 188 | return scm_hash_fn_create_handle_x (table, key, init, scm_ihashq, scm_sloppy_assq, 0); |
0f2d19dd | 189 | } |
1bbd0b84 | 190 | #undef FUNC_NAME |
0f2d19dd JB |
191 | |
192 | ||
a1ec6916 | 193 | SCM_DEFINE (scm_hashq_ref, "hashq-ref", 2, 1, 0, |
1e6808ea | 194 | (SCM table, SCM key, SCM dflt), |
b380b885 MD |
195 | "Look up @var{key} in the hash table @var{table}, and return the\n" |
196 | "value (if any) associated with it. If @var{key} is not found,\n" | |
5352393c MG |
197 | "return @var{default} (or @code{#f} if no @var{default} argument\n" |
198 | "is supplied). Uses @code{eq?} for equality testing.") | |
1bbd0b84 | 199 | #define FUNC_NAME s_scm_hashq_ref |
0f2d19dd | 200 | { |
54778cd3 | 201 | if (SCM_UNBNDP (dflt)) |
0f2d19dd | 202 | dflt = SCM_BOOL_F; |
1e6808ea | 203 | return scm_hash_fn_ref (table, key, dflt, scm_ihashq, scm_sloppy_assq, 0); |
0f2d19dd | 204 | } |
1bbd0b84 | 205 | #undef FUNC_NAME |
0f2d19dd JB |
206 | |
207 | ||
208 | ||
a1ec6916 | 209 | SCM_DEFINE (scm_hashq_set_x, "hashq-set!", 3, 0, 0, |
1e6808ea | 210 | (SCM table, SCM key, SCM val), |
5352393c MG |
211 | "Find the entry in @var{table} associated with @var{key}, and\n" |
212 | "store @var{value} there. Uses @code{eq?} for equality testing.") | |
1bbd0b84 | 213 | #define FUNC_NAME s_scm_hashq_set_x |
0f2d19dd | 214 | { |
1e6808ea | 215 | return scm_hash_fn_set_x (table, key, val, scm_ihashq, scm_sloppy_assq, 0); |
0f2d19dd | 216 | } |
1bbd0b84 | 217 | #undef FUNC_NAME |
0f2d19dd JB |
218 | |
219 | ||
220 | ||
a1ec6916 | 221 | SCM_DEFINE (scm_hashq_remove_x, "hashq-remove!", 2, 0, 0, |
1e6808ea | 222 | (SCM table, SCM key), |
5352393c MG |
223 | "Remove @var{key} (and any value associated with it) from\n" |
224 | "@var{table}. Uses @code{eq?} for equality tests.") | |
1bbd0b84 | 225 | #define FUNC_NAME s_scm_hashq_remove_x |
0f2d19dd | 226 | { |
1e6808ea MG |
227 | return scm_hash_fn_remove_x (table, key, scm_ihashq, scm_sloppy_assq, |
228 | scm_delq_x, 0); | |
0f2d19dd | 229 | } |
1bbd0b84 | 230 | #undef FUNC_NAME |
0f2d19dd JB |
231 | |
232 | ||
233 | \f | |
234 | ||
a1ec6916 | 235 | SCM_DEFINE (scm_hashv_get_handle, "hashv-get-handle", 2, 0, 0, |
22a52da1 DH |
236 | (SCM table, SCM key), |
237 | "This procedure returns the @code{(key . value)} pair from the\n" | |
238 | "hash table @var{table}. If @var{table} does not hold an\n" | |
239 | "associated value for @var{key}, @code{#f} is returned.\n" | |
240 | "Uses @code{eqv?} for equality testing.") | |
1bbd0b84 | 241 | #define FUNC_NAME s_scm_hashv_get_handle |
0f2d19dd | 242 | { |
22a52da1 | 243 | return scm_hash_fn_get_handle (table, key, scm_ihashv, scm_sloppy_assv, 0); |
0f2d19dd | 244 | } |
1bbd0b84 | 245 | #undef FUNC_NAME |
0f2d19dd JB |
246 | |
247 | ||
a1ec6916 | 248 | SCM_DEFINE (scm_hashv_create_handle_x, "hashv-create-handle!", 3, 0, 0, |
d550d22a GB |
249 | (SCM table, SCM key, SCM init), |
250 | "This function looks up @var{key} in @var{table} and returns its handle.\n" | |
251 | "If @var{key} is not already present, a new handle is created which\n" | |
252 | "associates @var{key} with @var{init}.") | |
1bbd0b84 | 253 | #define FUNC_NAME s_scm_hashv_create_handle_x |
0f2d19dd | 254 | { |
1e6808ea MG |
255 | return scm_hash_fn_create_handle_x (table, key, init, scm_ihashv, |
256 | scm_sloppy_assv, 0); | |
0f2d19dd | 257 | } |
1bbd0b84 | 258 | #undef FUNC_NAME |
0f2d19dd JB |
259 | |
260 | ||
a1ec6916 | 261 | SCM_DEFINE (scm_hashv_ref, "hashv-ref", 2, 1, 0, |
1e6808ea | 262 | (SCM table, SCM key, SCM dflt), |
d550d22a GB |
263 | "Look up @var{key} in the hash table @var{table}, and return the\n" |
264 | "value (if any) associated with it. If @var{key} is not found,\n" | |
5352393c MG |
265 | "return @var{default} (or @code{#f} if no @var{default} argument\n" |
266 | "is supplied). Uses @code{eqv?} for equality testing.") | |
1bbd0b84 | 267 | #define FUNC_NAME s_scm_hashv_ref |
0f2d19dd | 268 | { |
54778cd3 | 269 | if (SCM_UNBNDP (dflt)) |
0f2d19dd | 270 | dflt = SCM_BOOL_F; |
1e6808ea | 271 | return scm_hash_fn_ref (table, key, dflt, scm_ihashv, scm_sloppy_assv, 0); |
0f2d19dd | 272 | } |
1bbd0b84 | 273 | #undef FUNC_NAME |
0f2d19dd JB |
274 | |
275 | ||
276 | ||
a1ec6916 | 277 | SCM_DEFINE (scm_hashv_set_x, "hashv-set!", 3, 0, 0, |
1e6808ea | 278 | (SCM table, SCM key, SCM val), |
5352393c MG |
279 | "Find the entry in @var{table} associated with @var{key}, and\n" |
280 | "store @var{value} there. Uses @code{eqv?} for equality testing.") | |
1bbd0b84 | 281 | #define FUNC_NAME s_scm_hashv_set_x |
0f2d19dd | 282 | { |
1e6808ea | 283 | return scm_hash_fn_set_x (table, key, val, scm_ihashv, scm_sloppy_assv, 0); |
0f2d19dd | 284 | } |
1bbd0b84 | 285 | #undef FUNC_NAME |
0f2d19dd JB |
286 | |
287 | ||
a1ec6916 | 288 | SCM_DEFINE (scm_hashv_remove_x, "hashv-remove!", 2, 0, 0, |
1e6808ea | 289 | (SCM table, SCM key), |
5352393c MG |
290 | "Remove @var{key} (and any value associated with it) from\n" |
291 | "@var{table}. Uses @code{eqv?} for equality tests.") | |
1bbd0b84 | 292 | #define FUNC_NAME s_scm_hashv_remove_x |
0f2d19dd | 293 | { |
1e6808ea MG |
294 | return scm_hash_fn_remove_x (table, key, scm_ihashv, scm_sloppy_assv, |
295 | scm_delv_x, 0); | |
0f2d19dd | 296 | } |
1bbd0b84 | 297 | #undef FUNC_NAME |
0f2d19dd JB |
298 | |
299 | \f | |
300 | ||
a1ec6916 | 301 | SCM_DEFINE (scm_hash_get_handle, "hash-get-handle", 2, 0, 0, |
22a52da1 DH |
302 | (SCM table, SCM key), |
303 | "This procedure returns the @code{(key . value)} pair from the\n" | |
304 | "hash table @var{table}. If @var{table} does not hold an\n" | |
305 | "associated value for @var{key}, @code{#f} is returned.\n" | |
306 | "Uses @code{equal?} for equality testing.") | |
1bbd0b84 | 307 | #define FUNC_NAME s_scm_hash_get_handle |
0f2d19dd | 308 | { |
22a52da1 | 309 | return scm_hash_fn_get_handle (table, key, scm_ihash, scm_sloppy_assoc, 0); |
0f2d19dd | 310 | } |
1bbd0b84 | 311 | #undef FUNC_NAME |
0f2d19dd JB |
312 | |
313 | ||
a1ec6916 | 314 | SCM_DEFINE (scm_hash_create_handle_x, "hash-create-handle!", 3, 0, 0, |
d550d22a GB |
315 | (SCM table, SCM key, SCM init), |
316 | "This function looks up @var{key} in @var{table} and returns its handle.\n" | |
317 | "If @var{key} is not already present, a new handle is created which\n" | |
318 | "associates @var{key} with @var{init}.") | |
1bbd0b84 | 319 | #define FUNC_NAME s_scm_hash_create_handle_x |
0f2d19dd | 320 | { |
d550d22a | 321 | return scm_hash_fn_create_handle_x (table, key, init, scm_ihash, scm_sloppy_assoc, 0); |
0f2d19dd | 322 | } |
1bbd0b84 | 323 | #undef FUNC_NAME |
0f2d19dd JB |
324 | |
325 | ||
a1ec6916 | 326 | SCM_DEFINE (scm_hash_ref, "hash-ref", 2, 1, 0, |
1e6808ea | 327 | (SCM table, SCM key, SCM dflt), |
d550d22a GB |
328 | "Look up @var{key} in the hash table @var{table}, and return the\n" |
329 | "value (if any) associated with it. If @var{key} is not found,\n" | |
5352393c MG |
330 | "return @var{default} (or @code{#f} if no @var{default} argument\n" |
331 | "is supplied). Uses @code{equal?} for equality testing.") | |
1bbd0b84 | 332 | #define FUNC_NAME s_scm_hash_ref |
0f2d19dd | 333 | { |
54778cd3 | 334 | if (SCM_UNBNDP (dflt)) |
0f2d19dd | 335 | dflt = SCM_BOOL_F; |
1e6808ea | 336 | return scm_hash_fn_ref (table, key, dflt, scm_ihash, scm_sloppy_assoc, 0); |
0f2d19dd | 337 | } |
1bbd0b84 | 338 | #undef FUNC_NAME |
0f2d19dd JB |
339 | |
340 | ||
341 | ||
a1ec6916 | 342 | SCM_DEFINE (scm_hash_set_x, "hash-set!", 3, 0, 0, |
1e6808ea | 343 | (SCM table, SCM key, SCM val), |
5352393c MG |
344 | "Find the entry in @var{table} associated with @var{key}, and\n" |
345 | "store @var{value} there. Uses @code{equal?} for equality\n" | |
346 | "testing.") | |
1bbd0b84 | 347 | #define FUNC_NAME s_scm_hash_set_x |
0f2d19dd | 348 | { |
1e6808ea | 349 | return scm_hash_fn_set_x (table, key, val, scm_ihash, scm_sloppy_assoc, 0); |
0f2d19dd | 350 | } |
1bbd0b84 | 351 | #undef FUNC_NAME |
0f2d19dd JB |
352 | |
353 | ||
354 | ||
a1ec6916 | 355 | SCM_DEFINE (scm_hash_remove_x, "hash-remove!", 2, 0, 0, |
1e6808ea | 356 | (SCM table, SCM key), |
5352393c MG |
357 | "Remove @var{key} (and any value associated with it) from\n" |
358 | "@var{table}. Uses @code{equal?} for equality tests.") | |
1bbd0b84 | 359 | #define FUNC_NAME s_scm_hash_remove_x |
0f2d19dd | 360 | { |
1e6808ea MG |
361 | return scm_hash_fn_remove_x (table, key, scm_ihash, scm_sloppy_assoc, |
362 | scm_delete_x, 0); | |
0f2d19dd | 363 | } |
1bbd0b84 | 364 | #undef FUNC_NAME |
0f2d19dd JB |
365 | |
366 | \f | |
367 | ||
368 | ||
92c2555f | 369 | typedef struct scm_t_ihashx_closure |
0f2d19dd JB |
370 | { |
371 | SCM hash; | |
372 | SCM assoc; | |
373 | SCM delete; | |
92c2555f | 374 | } scm_t_ihashx_closure; |
0f2d19dd JB |
375 | |
376 | ||
1cc91f1b | 377 | |
c014a02e | 378 | static unsigned long |
92c2555f | 379 | scm_ihashx (SCM obj, unsigned long n, scm_t_ihashx_closure *closure) |
0f2d19dd JB |
380 | { |
381 | SCM answer; | |
44d3cb0d | 382 | SCM_DEFER_INTS; |
fdc28395 | 383 | answer = scm_call_2 (closure->hash, obj, scm_ulong2num ((unsigned long) n)); |
44d3cb0d | 384 | SCM_ALLOW_INTS; |
0f2d19dd JB |
385 | return SCM_INUM (answer); |
386 | } | |
387 | ||
388 | ||
1cc91f1b | 389 | |
0f2d19dd | 390 | static SCM |
92c2555f | 391 | scm_sloppy_assx (SCM obj, SCM alist, scm_t_ihashx_closure *closure) |
0f2d19dd JB |
392 | { |
393 | SCM answer; | |
44d3cb0d | 394 | SCM_DEFER_INTS; |
fdc28395 | 395 | answer = scm_call_2 (closure->assoc, obj, alist); |
44d3cb0d | 396 | SCM_ALLOW_INTS; |
0f2d19dd JB |
397 | return answer; |
398 | } | |
399 | ||
400 | ||
401 | ||
1cc91f1b | 402 | |
0f2d19dd | 403 | static SCM |
92c2555f | 404 | scm_delx_x (SCM obj, SCM alist, scm_t_ihashx_closure *closure) |
0f2d19dd JB |
405 | { |
406 | SCM answer; | |
44d3cb0d | 407 | SCM_DEFER_INTS; |
fdc28395 | 408 | answer = scm_call_2 (closure->delete, obj, alist); |
44d3cb0d | 409 | SCM_ALLOW_INTS; |
0f2d19dd JB |
410 | return answer; |
411 | } | |
412 | ||
413 | ||
414 | ||
a1ec6916 | 415 | SCM_DEFINE (scm_hashx_get_handle, "hashx-get-handle", 4, 0, 0, |
1e6808ea MG |
416 | (SCM hash, SCM assoc, SCM table, SCM key), |
417 | "This behaves the same way as the corresponding\n" | |
418 | "@code{-get-handle} function, but uses @var{hash} as a hash\n" | |
419 | "function and @var{assoc} to compare keys. @code{hash} must be\n" | |
420 | "a function that takes two arguments, a key to be hashed and a\n" | |
d550d22a GB |
421 | "table size. @code{assoc} must be an associator function, like\n" |
422 | "@code{assoc}, @code{assq} or @code{assv}.") | |
1bbd0b84 | 423 | #define FUNC_NAME s_scm_hashx_get_handle |
0f2d19dd | 424 | { |
92c2555f | 425 | scm_t_ihashx_closure closure; |
0f2d19dd JB |
426 | closure.hash = hash; |
427 | closure.assoc = assoc; | |
1e6808ea MG |
428 | return scm_hash_fn_get_handle (table, key, scm_ihashx, scm_sloppy_assx, |
429 | (void *)&closure); | |
0f2d19dd | 430 | } |
1bbd0b84 | 431 | #undef FUNC_NAME |
0f2d19dd JB |
432 | |
433 | ||
a1ec6916 | 434 | SCM_DEFINE (scm_hashx_create_handle_x, "hashx-create-handle!", 5, 0, 0, |
1e6808ea MG |
435 | (SCM hash, SCM assoc, SCM table, SCM key, SCM init), |
436 | "This behaves the same way as the corresponding\n" | |
437 | "@code{-create-handle} function, but uses @var{hash} as a hash\n" | |
438 | "function and @var{assoc} to compare keys. @code{hash} must be\n" | |
439 | "a function that takes two arguments, a key to be hashed and a\n" | |
d550d22a GB |
440 | "table size. @code{assoc} must be an associator function, like\n" |
441 | "@code{assoc}, @code{assq} or @code{assv}.") | |
1bbd0b84 | 442 | #define FUNC_NAME s_scm_hashx_create_handle_x |
0f2d19dd | 443 | { |
92c2555f | 444 | scm_t_ihashx_closure closure; |
0f2d19dd JB |
445 | closure.hash = hash; |
446 | closure.assoc = assoc; | |
1e6808ea MG |
447 | return scm_hash_fn_create_handle_x (table, key, init, scm_ihashx, |
448 | scm_sloppy_assx, (void *)&closure); | |
0f2d19dd | 449 | } |
1bbd0b84 | 450 | #undef FUNC_NAME |
0f2d19dd JB |
451 | |
452 | ||
453 | ||
a1ec6916 | 454 | SCM_DEFINE (scm_hashx_ref, "hashx-ref", 4, 1, 0, |
1e6808ea | 455 | (SCM hash, SCM assoc, SCM table, SCM key, SCM dflt), |
d550d22a | 456 | "This behaves the same way as the corresponding @code{ref}\n" |
1e6808ea MG |
457 | "function, but uses @var{hash} as a hash function and\n" |
458 | "@var{assoc} to compare keys. @code{hash} must be a function\n" | |
459 | "that takes two arguments, a key to be hashed and a table size.\n" | |
460 | "@code{assoc} must be an associator function, like @code{assoc},\n" | |
461 | "@code{assq} or @code{assv}.\n" | |
462 | "\n" | |
463 | "By way of illustration, @code{hashq-ref table key} is\n" | |
464 | "equivalent to @code{hashx-ref hashq assq table key}.") | |
1bbd0b84 | 465 | #define FUNC_NAME s_scm_hashx_ref |
0f2d19dd | 466 | { |
92c2555f | 467 | scm_t_ihashx_closure closure; |
54778cd3 | 468 | if (SCM_UNBNDP (dflt)) |
0f2d19dd JB |
469 | dflt = SCM_BOOL_F; |
470 | closure.hash = hash; | |
471 | closure.assoc = assoc; | |
1e6808ea MG |
472 | return scm_hash_fn_ref (table, key, dflt, scm_ihashx, scm_sloppy_assx, |
473 | (void *)&closure); | |
0f2d19dd | 474 | } |
1bbd0b84 | 475 | #undef FUNC_NAME |
0f2d19dd JB |
476 | |
477 | ||
478 | ||
479 | ||
a1ec6916 | 480 | SCM_DEFINE (scm_hashx_set_x, "hashx-set!", 5, 0, 0, |
1e6808ea | 481 | (SCM hash, SCM assoc, SCM table, SCM key, SCM val), |
d550d22a | 482 | "This behaves the same way as the corresponding @code{set!}\n" |
1e6808ea MG |
483 | "function, but uses @var{hash} as a hash function and\n" |
484 | "@var{assoc} to compare keys. @code{hash} must be a function\n" | |
485 | "that takes two arguments, a key to be hashed and a table size.\n" | |
486 | "@code{assoc} must be an associator function, like @code{assoc},\n" | |
487 | "@code{assq} or @code{assv}.\n" | |
488 | "\n" | |
489 | " By way of illustration, @code{hashq-set! table key} is\n" | |
490 | "equivalent to @code{hashx-set! hashq assq table key}.") | |
1bbd0b84 | 491 | #define FUNC_NAME s_scm_hashx_set_x |
0f2d19dd | 492 | { |
92c2555f | 493 | scm_t_ihashx_closure closure; |
0f2d19dd JB |
494 | closure.hash = hash; |
495 | closure.assoc = assoc; | |
1e6808ea MG |
496 | return scm_hash_fn_set_x (table, key, val, scm_ihashx, scm_sloppy_assx, |
497 | (void *)&closure); | |
0f2d19dd | 498 | } |
1bbd0b84 | 499 | #undef FUNC_NAME |
0f2d19dd JB |
500 | |
501 | ||
1cc91f1b | 502 | |
0f2d19dd | 503 | SCM |
1be6b49c | 504 | scm_hashx_remove_x (SCM hash, SCM assoc, SCM delete, SCM table, SCM obj) |
0f2d19dd | 505 | { |
92c2555f | 506 | scm_t_ihashx_closure closure; |
0f2d19dd JB |
507 | closure.hash = hash; |
508 | closure.assoc = assoc; | |
509 | closure.delete = delete; | |
510 | return scm_hash_fn_remove_x (table, obj, scm_ihashx, scm_sloppy_assx, scm_delx_x, 0); | |
511 | } | |
512 | ||
b94903c2 MD |
513 | static SCM |
514 | fold_proc (void *proc, SCM key, SCM data, SCM value) | |
515 | { | |
fdc28395 | 516 | return scm_call_3 (SCM_PACK (proc), key, data, value); |
b94903c2 MD |
517 | } |
518 | ||
a1ec6916 | 519 | SCM_DEFINE (scm_hash_fold, "hash-fold", 3, 0, 0, |
1bbd0b84 | 520 | (SCM proc, SCM init, SCM table), |
d550d22a GB |
521 | "An iterator over hash-table elements.\n" |
522 | "Accumulates and returns a result by applying PROC successively.\n" | |
523 | "The arguments to PROC are \"(key value prior-result)\" where key\n" | |
524 | "and value are successive pairs from the hash table TABLE, and\n" | |
525 | "prior-result is either INIT (for the first application of PROC)\n" | |
526 | "or the return value of the previous application of PROC.\n" | |
72dd0a03 | 527 | "For example, @code{(hash-fold acons '() tab)} will convert a hash\n" |
9401323e | 528 | "table into an a-list of key-value pairs.") |
1bbd0b84 | 529 | #define FUNC_NAME s_scm_hash_fold |
b94903c2 | 530 | { |
34d19ef6 HWN |
531 | SCM_VALIDATE_PROC (1, proc); |
532 | SCM_VALIDATE_VECTOR (3, table); | |
54778cd3 | 533 | return scm_internal_hash_fold (fold_proc, (void *) SCM_UNPACK (proc), init, table); |
b94903c2 | 534 | } |
1bbd0b84 | 535 | #undef FUNC_NAME |
c7df61cd MD |
536 | |
537 | SCM | |
8cd5191b | 538 | scm_internal_hash_fold (SCM (*fn) (), void *closure, SCM init, SCM table) |
c7df61cd | 539 | { |
c014a02e | 540 | long i, n = SCM_VECTOR_LENGTH (table); |
c7df61cd MD |
541 | SCM result = init; |
542 | for (i = 0; i < n; ++i) | |
543 | { | |
544 | SCM ls = SCM_VELTS (table)[i], handle; | |
22a52da1 | 545 | while (!SCM_NULLP (ls)) |
c7df61cd | 546 | { |
0c95b57d | 547 | SCM_ASSERT (SCM_CONSP (ls), |
dd85ce47 | 548 | table, SCM_ARG3, s_scm_hash_fold); |
c7df61cd | 549 | handle = SCM_CAR (ls); |
0c95b57d | 550 | SCM_ASSERT (SCM_CONSP (handle), |
dd85ce47 | 551 | table, SCM_ARG3, s_scm_hash_fold); |
c7df61cd MD |
552 | result = fn (closure, SCM_CAR (handle), SCM_CDR (handle), result); |
553 | ls = SCM_CDR (ls); | |
554 | } | |
555 | } | |
556 | return result; | |
557 | } | |
558 | ||
0f2d19dd JB |
559 | \f |
560 | ||
1cc91f1b | 561 | |
0f2d19dd JB |
562 | void |
563 | scm_init_hashtab () | |
0f2d19dd | 564 | { |
a0599745 | 565 | #include "libguile/hashtab.x" |
0f2d19dd | 566 | } |
89e00824 ML |
567 | |
568 | /* | |
569 | Local Variables: | |
570 | c-file-style: "gnu" | |
571 | End: | |
572 | */ |