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