1 /* Copyright (C) 1995,1996,1998,1999,2000,2001 Free Software Foundation, Inc.
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)
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.
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
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
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"
53 #include "libguile/validate.h"
54 #include "libguile/hashtab.h"
58 scm_c_make_hash_table (unsigned long k
)
60 return scm_c_make_vector (k
, SCM_EOL
);
65 scm_hash_fn_get_handle (SCM table
,SCM obj
,unsigned int (*hash_fn
)(),SCM (*assoc_fn
)(),void * closure
)
66 #define FUNC_NAME "scm_hash_fn_get_handle"
71 SCM_VALIDATE_VECTOR (1, table
);
72 if (SCM_VECTOR_LENGTH (table
) == 0)
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
);
84 scm_hash_fn_create_handle_x (SCM table
,SCM obj
,SCM init
,unsigned int (*hash_fn
)(),
85 SCM (*assoc_fn
)(),void * closure
)
86 #define FUNC_NAME "scm_hash_fn_create_handle_x"
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
);
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
));
99 it
= assoc_fn (obj
, SCM_VELTS (table
)[k
], closure
);
108 old_bucket
= SCM_VELTS (table
)[k
];
109 new_bucket
= scm_acons (obj
, init
, old_bucket
);
110 SCM_VELTS(table
)[k
] = new_bucket
;
112 return SCM_CAR (new_bucket
);
119 scm_hash_fn_ref (SCM table
,SCM obj
,SCM dflt
,unsigned int (*hash_fn
)(),
120 SCM (*assoc_fn
)(),void * closure
)
122 SCM it
= scm_hash_fn_get_handle (table
, obj
, hash_fn
, assoc_fn
, closure
);
133 scm_hash_fn_set_x (SCM table
,SCM obj
,SCM val
,unsigned int (*hash_fn
)(),
134 SCM (*assoc_fn
)(),void * closure
)
138 it
= scm_hash_fn_create_handle_x (table
, obj
, SCM_BOOL_F
, hash_fn
, assoc_fn
, closure
);
139 SCM_SETCDR (it
, val
);
148 scm_hash_fn_remove_x (SCM table
,SCM obj
,unsigned int (*hash_fn
)(),SCM (*assoc_fn
)(),
149 SCM (*delete_fn
)(),void * closure
)
154 SCM_ASSERT (SCM_VECTORP (table
), table
, SCM_ARG1
, "hash_fn_remove_x");
155 if (SCM_VECTOR_LENGTH (table
) == 0)
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
]);
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
176 return scm_hash_fn_get_handle (table
, key
, scm_ihashq
, scm_sloppy_assq
, 0);
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
188 return scm_hash_fn_create_handle_x (table
, key
, init
, scm_ihashq
, scm_sloppy_assq
, 0);
193 SCM_DEFINE (scm_hashq_ref
, "hashq-ref", 2, 1, 0,
194 (SCM table
, SCM obj
, 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
201 if (SCM_UNBNDP (dflt
))
203 return scm_hash_fn_ref (table
, obj
, dflt
, scm_ihashq
, scm_sloppy_assq
, 0);
209 SCM_DEFINE (scm_hashq_set_x
, "hashq-set!", 3, 0, 0,
210 (SCM table
, SCM obj
, 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
215 return scm_hash_fn_set_x (table
, obj
, val
, scm_ihashq
, scm_sloppy_assq
, 0);
221 SCM_DEFINE (scm_hashq_remove_x
, "hashq-remove!", 2, 0, 0,
222 (SCM table
, SCM obj
),
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
227 return scm_hash_fn_remove_x (table
, obj
, scm_ihashq
, scm_sloppy_assq
, scm_delq_x
, 0);
234 SCM_DEFINE (scm_hashv_get_handle
, "hashv-get-handle", 2, 0, 0,
235 (SCM table
, SCM key
),
236 "This procedure returns the @code{(key . value)} pair from the\n"
237 "hash table @var{table}. If @var{table} does not hold an\n"
238 "associated value for @var{key}, @code{#f} is returned.\n"
239 "Uses @code{eqv?} for equality testing.")
240 #define FUNC_NAME s_scm_hashv_get_handle
242 return scm_hash_fn_get_handle (table
, key
, scm_ihashv
, scm_sloppy_assv
, 0);
247 SCM_DEFINE (scm_hashv_create_handle_x
, "hashv-create-handle!", 3, 0, 0,
248 (SCM table
, SCM key
, SCM init
),
249 "This function looks up @var{key} in @var{table} and returns its handle.\n"
250 "If @var{key} is not already present, a new handle is created which\n"
251 "associates @var{key} with @var{init}.")
252 #define FUNC_NAME s_scm_hashv_create_handle_x
254 return scm_hash_fn_create_handle_x (table
, key
, init
, scm_ihashv
, scm_sloppy_assv
, 0);
259 SCM_DEFINE (scm_hashv_ref
, "hashv-ref", 2, 1, 0,
260 (SCM table
, SCM obj
, SCM dflt
),
261 "Look up @var{key} in the hash table @var{table}, and return the\n"
262 "value (if any) associated with it. If @var{key} is not found,\n"
263 "return @var{default} (or @code{#f} if no @var{default} argument\n"
264 "is supplied). Uses @code{eqv?} for equality testing.")
265 #define FUNC_NAME s_scm_hashv_ref
267 if (SCM_UNBNDP (dflt
))
269 return scm_hash_fn_ref (table
, obj
, dflt
, scm_ihashv
, scm_sloppy_assv
, 0);
275 SCM_DEFINE (scm_hashv_set_x
, "hashv-set!", 3, 0, 0,
276 (SCM table
, SCM obj
, SCM val
),
277 "Find the entry in @var{table} associated with @var{key}, and\n"
278 "store @var{value} there. Uses @code{eqv?} for equality testing.")
279 #define FUNC_NAME s_scm_hashv_set_x
281 return scm_hash_fn_set_x (table
, obj
, val
, scm_ihashv
, scm_sloppy_assv
, 0);
286 SCM_DEFINE (scm_hashv_remove_x
, "hashv-remove!", 2, 0, 0,
287 (SCM table
, SCM obj
),
288 "Remove @var{key} (and any value associated with it) from\n"
289 "@var{table}. Uses @code{eqv?} for equality tests.")
290 #define FUNC_NAME s_scm_hashv_remove_x
292 return scm_hash_fn_remove_x (table
, obj
, scm_ihashv
, scm_sloppy_assv
, scm_delv_x
, 0);
298 SCM_DEFINE (scm_hash_get_handle
, "hash-get-handle", 2, 0, 0,
299 (SCM table
, SCM key
),
300 "This procedure returns the @code{(key . value)} pair from the\n"
301 "hash table @var{table}. If @var{table} does not hold an\n"
302 "associated value for @var{key}, @code{#f} is returned.\n"
303 "Uses @code{equal?} for equality testing.")
304 #define FUNC_NAME s_scm_hash_get_handle
306 return scm_hash_fn_get_handle (table
, key
, scm_ihash
, scm_sloppy_assoc
, 0);
311 SCM_DEFINE (scm_hash_create_handle_x
, "hash-create-handle!", 3, 0, 0,
312 (SCM table
, SCM key
, SCM init
),
313 "This function looks up @var{key} in @var{table} and returns its handle.\n"
314 "If @var{key} is not already present, a new handle is created which\n"
315 "associates @var{key} with @var{init}.")
316 #define FUNC_NAME s_scm_hash_create_handle_x
318 return scm_hash_fn_create_handle_x (table
, key
, init
, scm_ihash
, scm_sloppy_assoc
, 0);
323 SCM_DEFINE (scm_hash_ref
, "hash-ref", 2, 1, 0,
324 (SCM table
, SCM obj
, SCM dflt
),
325 "Look up @var{key} in the hash table @var{table}, and return the\n"
326 "value (if any) associated with it. If @var{key} is not found,\n"
327 "return @var{default} (or @code{#f} if no @var{default} argument\n"
328 "is supplied). Uses @code{equal?} for equality testing.")
329 #define FUNC_NAME s_scm_hash_ref
331 if (SCM_UNBNDP (dflt
))
333 return scm_hash_fn_ref (table
, obj
, dflt
, scm_ihash
, scm_sloppy_assoc
, 0);
339 SCM_DEFINE (scm_hash_set_x
, "hash-set!", 3, 0, 0,
340 (SCM table
, SCM obj
, SCM val
),
341 "Find the entry in @var{table} associated with @var{key}, and\n"
342 "store @var{value} there. Uses @code{equal?} for equality\n"
344 #define FUNC_NAME s_scm_hash_set_x
346 return scm_hash_fn_set_x (table
, obj
, val
, scm_ihash
, scm_sloppy_assoc
, 0);
352 SCM_DEFINE (scm_hash_remove_x
, "hash-remove!", 2, 0, 0,
353 (SCM table
, SCM obj
),
354 "Remove @var{key} (and any value associated with it) from\n"
355 "@var{table}. Uses @code{equal?} for equality tests.")
356 #define FUNC_NAME s_scm_hash_remove_x
358 return scm_hash_fn_remove_x (table
, obj
, scm_ihash
, scm_sloppy_assoc
, scm_delete_x
, 0);
365 struct scm_ihashx_closure
375 scm_ihashx (SCM obj
,unsigned int n
,struct scm_ihashx_closure
* closure
)
379 answer
= scm_apply (closure
->hash
,
380 SCM_LIST2 (obj
, scm_ulong2num ((unsigned long)n
)),
383 return SCM_INUM (answer
);
389 scm_sloppy_assx (SCM obj
,SCM alist
,struct scm_ihashx_closure
* closure
)
393 answer
= scm_apply (closure
->assoc
,
394 SCM_LIST2 (obj
, alist
),
404 scm_delx_x (SCM obj
,SCM alist
,struct scm_ihashx_closure
* closure
)
408 answer
= scm_apply (closure
->delete,
409 SCM_LIST2 (obj
, alist
),
417 SCM_DEFINE (scm_hashx_get_handle
, "hashx-get-handle", 4, 0, 0,
418 (SCM hash
, SCM assoc
, SCM table
, SCM obj
),
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}.")
425 #define FUNC_NAME s_scm_hashx_get_handle
427 struct scm_ihashx_closure closure
;
429 closure
.assoc
= assoc
;
430 return scm_hash_fn_get_handle (table
, obj
, scm_ihashx
, scm_sloppy_assx
, (void *)&closure
);
435 SCM_DEFINE (scm_hashx_create_handle_x
, "hashx-create-handle!", 5, 0, 0,
436 (SCM hash
, SCM assoc
, SCM table
, SCM obj
, SCM init
),
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}.")
443 #define FUNC_NAME s_scm_hashx_create_handle_x
445 struct scm_ihashx_closure closure
;
447 closure
.assoc
= assoc
;
448 return scm_hash_fn_create_handle_x (table
, obj
, init
, scm_ihashx
, scm_sloppy_assx
, (void *)&closure
);
454 SCM_DEFINE (scm_hashx_ref
, "hashx-ref", 4, 1, 0,
455 (SCM hash
, SCM assoc
, SCM table
, SCM obj
, SCM dflt
),
456 "This behaves the same way as the corresponding @code{ref}\n"
457 "function, but uses @var{hasher} as a\n"
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}.")
464 #define FUNC_NAME s_scm_hashx_ref
466 struct scm_ihashx_closure closure
;
467 if (SCM_UNBNDP (dflt
))
470 closure
.assoc
= assoc
;
471 return scm_hash_fn_ref (table
, obj
, dflt
, scm_ihashx
, scm_sloppy_assx
, (void *)&closure
);
478 SCM_DEFINE (scm_hashx_set_x
, "hashx-set!", 5, 0, 0,
479 (SCM hash
, SCM assoc
, SCM table
, SCM obj
, SCM val
),
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}.")
488 #define FUNC_NAME s_scm_hashx_set_x
490 struct scm_ihashx_closure closure
;
492 closure
.assoc
= assoc
;
493 return scm_hash_fn_set_x (table
, obj
, val
, scm_ihashx
, scm_sloppy_assx
, (void *)&closure
);
500 scm_hashx_remove_x (SCM hash
,SCM assoc
,SCM
delete,SCM table
,SCM obj
)
502 struct scm_ihashx_closure closure
;
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);
510 fold_proc (void *proc
, SCM key
, SCM data
, SCM value
)
512 return scm_apply (SCM_PACK (proc
), SCM_LIST3 (key
, data
, value
), SCM_EOL
);
515 SCM_DEFINE (scm_hash_fold
, "hash-fold", 3, 0, 0,
516 (SCM proc
, SCM init
, SCM table
),
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")
525 #define FUNC_NAME s_scm_hash_fold
527 SCM_VALIDATE_PROC (1,proc
);
528 SCM_VALIDATE_VECTOR (3,table
);
529 return scm_internal_hash_fold (fold_proc
, (void *) SCM_UNPACK (proc
), init
, table
);
534 scm_internal_hash_fold (SCM (*fn
) (), void *closure
, SCM init
, SCM table
)
536 int i
, n
= SCM_VECTOR_LENGTH (table
);
538 for (i
= 0; i
< n
; ++i
)
540 SCM ls
= SCM_VELTS (table
)[i
], handle
;
541 while (!SCM_NULLP (ls
))
543 SCM_ASSERT (SCM_CONSP (ls
),
544 table
, SCM_ARG1
, s_scm_hash_fold
);
545 handle
= SCM_CAR (ls
);
546 SCM_ASSERT (SCM_CONSP (handle
),
547 table
, SCM_ARG1
, s_scm_hash_fold
);
548 result
= fn (closure
, SCM_CAR (handle
), SCM_CDR (handle
), result
);
561 #ifndef SCM_MAGIC_SNARFER
562 #include "libguile/hashtab.x"