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