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