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