* modules: New directory.
[bpt/guile.git] / libguile / hashtab.c
1 /* Copyright (C) 1995, 1996, 1998, 1999, 2000 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 SCM
64 scm_hash_fn_get_handle (SCM table,SCM obj,unsigned int (*hash_fn)(),SCM (*assoc_fn)(),void * closure)
65 {
66 unsigned int k;
67 SCM h;
68
69 SCM_ASSERT (SCM_VECTORP (table), table, SCM_ARG1, "hash_fn_get_handle");
70 if (SCM_VECTOR_LENGTH (table) == 0)
71 return SCM_EOL;
72 k = hash_fn (obj, SCM_VECTOR_LENGTH (table), closure);
73 if (k >= SCM_VECTOR_LENGTH (table))
74 scm_out_of_range ("hash_fn_get_handle", scm_ulong2num (k));
75 h = assoc_fn (obj, SCM_VELTS (table)[k], closure);
76 return h;
77 }
78
79
80 SCM
81 scm_hash_fn_create_handle_x (SCM table,SCM obj,SCM init,unsigned int (*hash_fn)(),
82 SCM (*assoc_fn)(),void * closure)
83 #define FUNC_NAME "scm_hash_fn_create_handle_x"
84 {
85 unsigned int k;
86 SCM it;
87
88 SCM_ASSERT (SCM_VECTORP (table), table, SCM_ARG1, "hash_fn_create_handle_x");
89 if (SCM_VECTOR_LENGTH (table) == 0)
90 SCM_MISC_ERROR ("void hashtable", SCM_EOL);
91
92 k = hash_fn (obj, SCM_VECTOR_LENGTH (table), closure);
93 if (k >= SCM_VECTOR_LENGTH (table))
94 scm_out_of_range ("hash_fn_create_handle_x", scm_ulong2num (k));
95 SCM_REDEFER_INTS;
96 it = assoc_fn (obj, SCM_VELTS (table)[k], closure);
97 if (SCM_NIMP (it))
98 {
99 SCM_REALLOW_INTS;
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 }
112 #undef FUNC_NAME
113
114
115 SCM
116 scm_hash_fn_ref (SCM table,SCM obj,SCM dflt,unsigned int (*hash_fn)(),
117 SCM (*assoc_fn)(),void * closure)
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
130
131 SCM
132 scm_hash_fn_set_x (SCM table,SCM obj,SCM val,unsigned int (*hash_fn)(),
133 SCM (*assoc_fn)(),void * closure)
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
145
146 SCM
147 scm_hash_fn_remove_x (SCM table,SCM obj,unsigned int (*hash_fn)(),SCM (*assoc_fn)(),
148 SCM (*delete_fn)(),void * closure)
149 {
150 unsigned int k;
151 SCM h;
152
153 SCM_ASSERT (SCM_VECTORP (table), table, SCM_ARG1, "hash_fn_remove_x");
154 if (SCM_VECTOR_LENGTH (table) == 0)
155 return SCM_EOL;
156 k = hash_fn (obj, SCM_VECTOR_LENGTH (table), closure);
157 if (k >= SCM_VECTOR_LENGTH (table))
158 scm_out_of_range ("hash_fn_remove_x", scm_ulong2num (k));
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
167 SCM_DEFINE (scm_hashq_get_handle, "hashq-get-handle", 2, 0, 0,
168 (SCM table, SCM obj),
169 "This procedure is similar to its @code{-ref} cousin, but returns a\n"
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)}.")
175 #define FUNC_NAME s_scm_hashq_get_handle
176 {
177 return scm_hash_fn_get_handle (table, obj, scm_ihashq, scm_sloppy_assq, 0);
178 }
179 #undef FUNC_NAME
180
181
182 SCM_DEFINE (scm_hashq_create_handle_x, "hashq-create-handle!", 3, 0, 0,
183 (SCM table, SCM key, SCM init),
184 "This function looks up @var{key} in @var{table} and returns its handle.\n"
185 "If @var{key} is not already present, a new handle is created which\n"
186 "associates @var{key} with @var{init}.")
187 #define FUNC_NAME s_scm_hashq_create_handle_x
188 {
189 return scm_hash_fn_create_handle_x (table, key, init, scm_ihashq, scm_sloppy_assq, 0);
190 }
191 #undef FUNC_NAME
192
193
194 SCM_DEFINE (scm_hashq_ref, "hashq-ref", 2, 1, 0,
195 (SCM table, SCM obj, SCM dflt),
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"
199 "supplied). Uses `eq?' for equality testing.")
200 #define FUNC_NAME s_scm_hashq_ref
201 {
202 if (SCM_UNBNDP (dflt))
203 dflt = SCM_BOOL_F;
204 return scm_hash_fn_ref (table, obj, dflt, scm_ihashq, scm_sloppy_assq, 0);
205 }
206 #undef FUNC_NAME
207
208
209
210 SCM_DEFINE (scm_hashq_set_x, "hashq-set!", 3, 0, 0,
211 (SCM table, SCM obj, SCM val),
212 "Find the entry in @var{table} associated with @var{key}, and store\n"
213 "@var{value} there. Uses `eq?' for equality testing.")
214 #define FUNC_NAME s_scm_hashq_set_x
215 {
216 return scm_hash_fn_set_x (table, obj, val, scm_ihashq, scm_sloppy_assq, 0);
217 }
218 #undef FUNC_NAME
219
220
221
222 SCM_DEFINE (scm_hashq_remove_x, "hashq-remove!", 2, 0, 0,
223 (SCM table, SCM obj),
224 "Remove @var{key} (and any value associated with it) from @var{table}.\n"
225 "Uses `eq?' for equality tests.")
226 #define FUNC_NAME s_scm_hashq_remove_x
227 {
228 return scm_hash_fn_remove_x (table, obj, scm_ihashq, scm_sloppy_assq, 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 obj),
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)}.")
243 #define FUNC_NAME s_scm_hashv_get_handle
244 {
245 return scm_hash_fn_get_handle (table, obj, scm_ihashv, scm_sloppy_assv, 0);
246 }
247 #undef FUNC_NAME
248
249
250 SCM_DEFINE (scm_hashv_create_handle_x, "hashv-create-handle!", 3, 0, 0,
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}.")
255 #define FUNC_NAME s_scm_hashv_create_handle_x
256 {
257 return scm_hash_fn_create_handle_x (table, key, init, scm_ihashv, scm_sloppy_assv, 0);
258 }
259 #undef FUNC_NAME
260
261
262 SCM_DEFINE (scm_hashv_ref, "hashv-ref", 2, 1, 0,
263 (SCM table, SCM obj, SCM dflt),
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.")
268 #define FUNC_NAME s_scm_hashv_ref
269 {
270 if (SCM_UNBNDP (dflt))
271 dflt = SCM_BOOL_F;
272 return scm_hash_fn_ref (table, obj, dflt, scm_ihashv, scm_sloppy_assv, 0);
273 }
274 #undef FUNC_NAME
275
276
277
278 SCM_DEFINE (scm_hashv_set_x, "hashv-set!", 3, 0, 0,
279 (SCM table, SCM obj, SCM val),
280 "Find the entry in @var{table} associated with @var{key}, and store\n"
281 "@var{value} there. Uses `eqv?' for equality testing.")
282 #define FUNC_NAME s_scm_hashv_set_x
283 {
284 return scm_hash_fn_set_x (table, obj, val, scm_ihashv, scm_sloppy_assv, 0);
285 }
286 #undef FUNC_NAME
287
288
289 SCM_DEFINE (scm_hashv_remove_x, "hashv-remove!", 2, 0, 0,
290 (SCM table, SCM obj),
291 "Remove @var{key} (and any value associated with it) from @var{table}.\n"
292 "Uses `eqv?' for equality tests.")
293 #define FUNC_NAME s_scm_hashv_remove_x
294 {
295 return scm_hash_fn_remove_x (table, obj, scm_ihashv, scm_sloppy_assv, 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 obj),
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)}.")
309 #define FUNC_NAME s_scm_hash_get_handle
310 {
311 return scm_hash_fn_get_handle (table, obj, scm_ihash, scm_sloppy_assoc, 0);
312 }
313 #undef FUNC_NAME
314
315
316 SCM_DEFINE (scm_hash_create_handle_x, "hash-create-handle!", 3, 0, 0,
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}.")
321 #define FUNC_NAME s_scm_hash_create_handle_x
322 {
323 return scm_hash_fn_create_handle_x (table, key, init, scm_ihash, scm_sloppy_assoc, 0);
324 }
325 #undef FUNC_NAME
326
327
328 SCM_DEFINE (scm_hash_ref, "hash-ref", 2, 1, 0,
329 (SCM table, SCM obj, SCM dflt),
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.")
334 #define FUNC_NAME s_scm_hash_ref
335 {
336 if (SCM_UNBNDP (dflt))
337 dflt = SCM_BOOL_F;
338 return scm_hash_fn_ref (table, obj, dflt, scm_ihash, scm_sloppy_assoc, 0);
339 }
340 #undef FUNC_NAME
341
342
343
344 SCM_DEFINE (scm_hash_set_x, "hash-set!", 3, 0, 0,
345 (SCM table, SCM obj, SCM val),
346 "Find the entry in @var{table} associated with @var{key}, and store\n"
347 "@var{value} there. Uses `equal?' for equality testing.")
348 #define FUNC_NAME s_scm_hash_set_x
349 {
350 return scm_hash_fn_set_x (table, obj, val, scm_ihash, scm_sloppy_assoc, 0);
351 }
352 #undef FUNC_NAME
353
354
355
356 SCM_DEFINE (scm_hash_remove_x, "hash-remove!", 2, 0, 0,
357 (SCM table, SCM obj),
358 "Remove @var{key} (and any value associated with it) from @var{table}.\n"
359 "Uses `equal?' for equality tests.")
360 #define FUNC_NAME s_scm_hash_remove_x
361 {
362 return scm_hash_fn_remove_x (table, obj, scm_ihash, scm_sloppy_assoc, scm_delete_x, 0);
363 }
364 #undef FUNC_NAME
365
366 \f
367
368
369 struct scm_ihashx_closure
370 {
371 SCM hash;
372 SCM assoc;
373 SCM delete;
374 };
375
376
377
378 static unsigned int
379 scm_ihashx (SCM obj,unsigned int n,struct scm_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,struct scm_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,struct scm_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 obj),
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}.")
429 #define FUNC_NAME s_scm_hashx_get_handle
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 }
436 #undef FUNC_NAME
437
438
439 SCM_DEFINE (scm_hashx_create_handle_x, "hashx-create-handle!", 5, 0, 0,
440 (SCM hash, SCM assoc, SCM table, SCM obj, SCM init),
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}.")
447 #define FUNC_NAME s_scm_hashx_create_handle_x
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 }
454 #undef FUNC_NAME
455
456
457
458 SCM_DEFINE (scm_hashx_ref, "hashx-ref", 4, 1, 0,
459 (SCM hash, SCM assoc, SCM table, SCM obj, SCM dflt),
460 "This behaves the same way as the corresponding @code{ref}\n"
461 "function, but uses @var{hasher} as a\n"
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}.")
468 #define FUNC_NAME s_scm_hashx_ref
469 {
470 struct scm_ihashx_closure closure;
471 if (SCM_UNBNDP (dflt))
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 }
477 #undef FUNC_NAME
478
479
480
481
482 SCM_DEFINE (scm_hashx_set_x, "hashx-set!", 5, 0, 0,
483 (SCM hash, SCM assoc, SCM table, SCM obj, SCM val),
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}.")
492 #define FUNC_NAME s_scm_hashx_set_x
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 }
499 #undef FUNC_NAME
500
501
502
503 SCM
504 scm_hashx_remove_x (SCM hash,SCM assoc,SCM delete,SCM table,SCM obj)
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
513 static SCM
514 fold_proc (void *proc, SCM key, SCM data, SCM value)
515 {
516 return scm_apply (SCM_PACK (proc), SCM_LIST3 (key, data, value), SCM_EOL);
517 }
518
519 SCM_DEFINE (scm_hash_fold, "hash-fold", 3, 0, 0,
520 (SCM proc, SCM init, SCM table),
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")
529 #define FUNC_NAME s_scm_hash_fold
530 {
531 SCM_VALIDATE_PROC (1,proc);
532 SCM_VALIDATE_VECTOR (3,table);
533 return scm_internal_hash_fold (fold_proc, (void *) SCM_UNPACK (proc), init, table);
534 }
535 #undef FUNC_NAME
536
537 SCM
538 scm_internal_hash_fold (SCM (*fn) (), void *closure, SCM init, SCM table)
539 {
540 int i, n = SCM_VECTOR_LENGTH (table);
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 {
547 SCM_ASSERT (SCM_CONSP (ls),
548 table, SCM_ARG1, s_scm_hash_fold);
549 handle = SCM_CAR (ls);
550 SCM_ASSERT (SCM_CONSP (handle),
551 table, SCM_ARG1, s_scm_hash_fold);
552 result = fn (closure, SCM_CAR (handle), SCM_CDR (handle), result);
553 ls = SCM_CDR (ls);
554 }
555 }
556 return result;
557 }
558
559 \f
560
561
562 void
563 scm_init_hashtab ()
564 {
565 #ifndef SCM_MAGIC_SNARFER
566 #include "libguile/hashtab.x"
567 #endif
568 }
569
570 /*
571 Local Variables:
572 c-file-style: "gnu"
573 End:
574 */