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