1 /* Copyright (C) 1995, 1996, 1998, 1999 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 */
53 #include "scm_validate.h"
59 scm_hash_fn_get_handle (SCM table
,SCM obj
,unsigned int (*hash_fn
)(),SCM (*assoc_fn
)(),void * closure
)
64 SCM_ASSERT (SCM_NIMP (table
) && SCM_VECTORP (table
), table
, SCM_ARG1
, "hash_fn_get_handle");
65 if (SCM_LENGTH (table
) == 0)
67 k
= hash_fn (obj
, SCM_LENGTH (table
), closure
);
68 SCM_ASSERT ((0 <= k
) && (k
< SCM_LENGTH (table
)),
71 "hash_fn_get_handle");
72 h
= assoc_fn (obj
, SCM_VELTS (table
)[k
], closure
);
79 scm_hash_fn_create_handle_x (SCM table
,SCM obj
,SCM init
,unsigned int (*hash_fn
)(),
80 SCM (*assoc_fn
)(),void * closure
)
85 SCM_ASSERT (SCM_NIMP (table
) && SCM_VECTORP (table
), table
, SCM_ARG1
, "hash_fn_create_handle_x");
86 if (SCM_LENGTH (table
) == 0)
88 k
= hash_fn (obj
, SCM_LENGTH (table
), closure
);
89 SCM_ASSERT ((0 <= k
) && (k
< SCM_LENGTH (table
)),
92 "hash_fn_create_handle_x");
94 it
= assoc_fn (obj
, SCM_VELTS (table
)[k
], closure
);
102 old_bucket
= SCM_VELTS (table
)[k
];
103 new_bucket
= scm_acons (obj
, init
, old_bucket
);
104 SCM_VELTS(table
)[k
] = new_bucket
;
106 return SCM_CAR (new_bucket
);
114 scm_hash_fn_ref (SCM table
,SCM obj
,SCM dflt
,unsigned int (*hash_fn
)(),
115 SCM (*assoc_fn
)(),void * closure
)
119 it
= scm_hash_fn_get_handle (table
, obj
, hash_fn
, assoc_fn
, closure
);
130 scm_hash_fn_set_x (SCM table
,SCM obj
,SCM val
,unsigned int (*hash_fn
)(),
131 SCM (*assoc_fn
)(),void * closure
)
135 it
= scm_hash_fn_create_handle_x (table
, obj
, SCM_BOOL_F
, hash_fn
, assoc_fn
, closure
);
136 SCM_SETCDR (it
, val
);
145 scm_hash_fn_remove_x (SCM table
,SCM obj
,unsigned int (*hash_fn
)(),SCM (*assoc_fn
)(),
146 SCM (*delete_fn
)(),void * closure
)
151 SCM_ASSERT (SCM_NIMP (table
) && SCM_VECTORP (table
), table
, SCM_ARG1
, "hash_fn_remove_x");
152 if (SCM_LENGTH (table
) == 0)
154 k
= hash_fn (obj
, SCM_LENGTH (table
), closure
);
155 SCM_ASSERT ((0 <= k
) && (k
< SCM_LENGTH (table
)),
159 h
= assoc_fn (obj
, SCM_VELTS (table
)[k
], closure
);
160 SCM_VELTS(table
)[k
] = delete_fn (h
, SCM_VELTS(table
)[k
]);
167 GUILE_PROC (scm_hashq_get_handle
, "hashq-get-handle", 2, 0, 0,
168 (SCM table
, SCM obj
),
169 "@deffnx primitive hashv-get-handle table key
170 @deffnx primitive hash-get-handle table key
171 @deffnx primitive hashx-get-handle hasher assoc table key
172 These procedures are similar to their @code{-ref} cousins, but return a
173 @dfn{handle} from the hash table rather than the value associated with
174 @var{key}. By convention, a handle in a hash table is the pair which
175 associates a key with a value. Where @code{hashq-ref table key} returns
176 only a @code{value}, @code{hashq-get-handle table key} returns the pair
177 @code{(key . value)}.")
178 #define FUNC_NAME s_scm_hashq_get_handle
180 return scm_hash_fn_get_handle (table
, obj
, scm_ihashq
, scm_sloppy_assq
, 0);
185 GUILE_PROC (scm_hashq_create_handle_x
, "hashq-create-handle!", 3, 0, 0,
186 (SCM table
, SCM obj
, SCM init
),
187 "@deffnx primitive hashv-create-handle! table key init
188 @deffnx primitive hash-create-handle! table key init
189 @deffnx primitive hashx-create-handle! hasher assoc table key init
190 These functions look up @var{key} in @var{table} and return its handle,
191 If @var{key} is not already present, a new handle is created which
192 associates @var{key} with @var{init}.")
193 #define FUNC_NAME s_scm_hashq_create_handle_x
195 return scm_hash_fn_create_handle_x (table
, obj
, init
, scm_ihashq
, scm_sloppy_assq
, 0);
200 GUILE_PROC (scm_hashq_ref
, "hashq-ref", 2, 1, 0,
201 (SCM table
, SCM obj
, SCM dflt
),
202 "@deffnx primitive hashv-ref table key [default]
203 @deffnx primitive hash-ref table key [default]
204 Look up @var{key} in the hash table @var{table}, and return the
205 value (if any) associated with it. If @var{key} is not found,
206 return @var{default} (or @code{#f} if no @var{default} argument is
208 #define FUNC_NAME s_scm_hashq_ref
210 if (dflt
== SCM_UNDEFINED
)
212 return scm_hash_fn_ref (table
, obj
, dflt
, scm_ihashq
, scm_sloppy_assq
, 0);
218 GUILE_PROC (scm_hashq_set_x
, "hashq-set!", 3, 0, 0,
219 (SCM table
, SCM obj
, SCM val
),
220 "@deffnx primitive hashv-set! table key value
221 @deffnx primitive hash-set! table key value
222 Find the entry in @var{table} associated with @var{key}, and store
224 #define FUNC_NAME s_scm_hashq_set_x
226 return scm_hash_fn_set_x (table
, obj
, val
, scm_ihashq
, scm_sloppy_assq
, 0);
232 GUILE_PROC (scm_hashq_remove_x
, "hashq-remove!", 2, 0, 0,
233 (SCM table
, SCM obj
),
234 "@deffnx primitive hashv-remove! table key
235 @deffnx primitive hash-remove! table key
236 Remove @var{key} (and any value associated with it) from @var{table}.")
237 #define FUNC_NAME s_scm_hashq_remove_x
239 return scm_hash_fn_remove_x (table
, obj
, scm_ihashq
, scm_sloppy_assq
, scm_delq_x
, 0);
246 GUILE_PROC (scm_hashv_get_handle
, "hashv-get-handle", 2, 0, 0,
247 (SCM table
, SCM obj
),
249 #define FUNC_NAME s_scm_hashv_get_handle
251 return scm_hash_fn_get_handle (table
, obj
, scm_ihashv
, scm_sloppy_assv
, 0);
256 GUILE_PROC (scm_hashv_create_handle_x
, "hashv-create-handle!", 3, 0, 0,
257 (SCM table
, SCM obj
, SCM init
),
259 #define FUNC_NAME s_scm_hashv_create_handle_x
261 return scm_hash_fn_create_handle_x (table
, obj
, init
, scm_ihashv
, scm_sloppy_assv
, 0);
266 GUILE_PROC (scm_hashv_ref
, "hashv-ref", 2, 1, 0,
267 (SCM table
, SCM obj
, SCM dflt
),
269 #define FUNC_NAME s_scm_hashv_ref
271 if (dflt
== SCM_UNDEFINED
)
273 return scm_hash_fn_ref (table
, obj
, dflt
, scm_ihashv
, scm_sloppy_assv
, 0);
279 GUILE_PROC (scm_hashv_set_x
, "hashv-set!", 3, 0, 0,
280 (SCM table
, SCM obj
, SCM val
),
282 #define FUNC_NAME s_scm_hashv_set_x
284 return scm_hash_fn_set_x (table
, obj
, val
, scm_ihashv
, scm_sloppy_assv
, 0);
289 GUILE_PROC (scm_hashv_remove_x
, "hashv-remove!", 2, 0, 0,
290 (SCM table
, SCM obj
),
292 #define FUNC_NAME s_scm_hashv_remove_x
294 return scm_hash_fn_remove_x (table
, obj
, scm_ihashv
, scm_sloppy_assv
, scm_delv_x
, 0);
300 GUILE_PROC (scm_hash_get_handle
, "hash-get-handle", 2, 0, 0,
301 (SCM table
, SCM obj
),
303 #define FUNC_NAME s_scm_hash_get_handle
305 return scm_hash_fn_get_handle (table
, obj
, scm_ihash
, scm_sloppy_assoc
, 0);
310 GUILE_PROC (scm_hash_create_handle_x
, "hash-create-handle!", 3, 0, 0,
311 (SCM table
, SCM obj
, SCM init
),
313 #define FUNC_NAME s_scm_hash_create_handle_x
315 return scm_hash_fn_create_handle_x (table
, obj
, init
, scm_ihash
, scm_sloppy_assoc
, 0);
320 GUILE_PROC (scm_hash_ref
, "hash-ref", 2, 1, 0,
321 (SCM table
, SCM obj
, SCM dflt
),
323 #define FUNC_NAME s_scm_hash_ref
325 if (dflt
== SCM_UNDEFINED
)
327 return scm_hash_fn_ref (table
, obj
, dflt
, scm_ihash
, scm_sloppy_assoc
, 0);
333 GUILE_PROC (scm_hash_set_x
, "hash-set!", 3, 0, 0,
334 (SCM table
, SCM obj
, SCM val
),
336 #define FUNC_NAME s_scm_hash_set_x
338 return scm_hash_fn_set_x (table
, obj
, val
, scm_ihash
, scm_sloppy_assoc
, 0);
344 GUILE_PROC (scm_hash_remove_x
, "hash-remove!", 2, 0, 0,
345 (SCM table
, SCM obj
),
347 #define FUNC_NAME s_scm_hash_remove_x
349 return scm_hash_fn_remove_x (table
, obj
, scm_ihash
, scm_sloppy_assoc
, scm_delete_x
, 0);
356 struct scm_ihashx_closure
366 scm_ihashx (SCM obj
,unsigned int n
,struct scm_ihashx_closure
* closure
)
370 answer
= scm_apply (closure
->hash
,
371 scm_listify (obj
, scm_ulong2num ((unsigned long)n
), SCM_UNDEFINED
),
374 return SCM_INUM (answer
);
380 scm_sloppy_assx (SCM obj
,SCM alist
,struct scm_ihashx_closure
* closure
)
384 answer
= scm_apply (closure
->assoc
,
385 scm_listify (obj
, alist
, SCM_UNDEFINED
),
395 scm_delx_x (SCM obj
,SCM alist
,struct scm_ihashx_closure
* closure
)
399 answer
= scm_apply (closure
->delete,
400 scm_listify (obj
, alist
, SCM_UNDEFINED
),
408 GUILE_PROC (scm_hashx_get_handle
, "hashx-get-handle", 4, 0, 0,
409 (SCM hash
, SCM assoc
, SCM table
, SCM obj
),
411 #define FUNC_NAME s_scm_hashx_get_handle
413 struct scm_ihashx_closure closure
;
415 closure
.assoc
= assoc
;
416 return scm_hash_fn_get_handle (table
, obj
, scm_ihashx
, scm_sloppy_assx
, (void *)&closure
);
421 GUILE_PROC (scm_hashx_create_handle_x
, "hashx-create-handle!", 5, 0, 0,
422 (SCM hash
,SCM assoc
,SCM table
,SCM obj
,SCM init
),
424 #define FUNC_NAME s_scm_hashx_create_handle_x
426 struct scm_ihashx_closure closure
;
428 closure
.assoc
= assoc
;
429 return scm_hash_fn_create_handle_x (table
, obj
, init
, scm_ihashx
, scm_sloppy_assx
, (void *)&closure
);
435 GUILE_PROC (scm_hashx_ref
, "hashx-ref", 4, 1, 0,
436 (SCM hash
,SCM assoc
,SCM table
,SCM obj
,SCM dflt
),
437 "@deffnx primitive hashx-set! hasher assoc table key value
438 @deffnx primitive hashx-remove! hasher assoc table key
439 These behave the same way as the corresponding @code{ref} and
440 @code{set!} functions described above, but use @var{hasher} as a
441 hash function and @var{assoc} to compare keys. @code{hasher} must
442 be a function that takes two arguments, a key to be hashed and a
443 table size. @code{assoc} must be an associator function, like
444 @code{assoc}, @code{assq} or @code{assv}.
446 By way of illustration, @code{hashq-ref table key} is equivalent
447 to @code{hashx-ref hashq assq table key}.")
448 #define FUNC_NAME s_scm_hashx_ref
450 struct scm_ihashx_closure closure
;
451 if (dflt
== SCM_UNDEFINED
)
454 closure
.assoc
= assoc
;
455 return scm_hash_fn_ref (table
, obj
, dflt
, scm_ihashx
, scm_sloppy_assx
, (void *)&closure
);
462 GUILE_PROC (scm_hashx_set_x
, "hashx-set!", 5, 0, 0,
463 (SCM hash
, SCM assoc
, SCM table
, SCM obj
, SCM val
),
465 #define FUNC_NAME s_scm_hashx_set_x
467 struct scm_ihashx_closure closure
;
469 closure
.assoc
= assoc
;
470 return scm_hash_fn_set_x (table
, obj
, val
, scm_ihashx
, scm_sloppy_assx
, (void *)&closure
);
477 scm_hashx_remove_x (SCM hash
,SCM assoc
,SCM
delete,SCM table
,SCM obj
)
479 struct scm_ihashx_closure closure
;
481 closure
.assoc
= assoc
;
482 closure
.delete = delete;
483 return scm_hash_fn_remove_x (table
, obj
, scm_ihashx
, scm_sloppy_assx
, scm_delx_x
, 0);
487 fold_proc (void *proc
, SCM key
, SCM data
, SCM value
)
489 return scm_apply ((SCM
) proc
, SCM_LIST3 (key
, data
, value
), SCM_EOL
);
492 GUILE_PROC (scm_hash_fold
, "hash-fold", 3, 0, 0,
493 (SCM proc
, SCM init
, SCM table
),
495 #define FUNC_NAME s_scm_hash_fold
497 SCM_VALIDATE_PROC(1,proc
);
498 SCM_VALIDATE_VECTOR(3,table
);
499 return scm_internal_hash_fold (fold_proc
, (void *) proc
, init
, table
);
504 scm_internal_hash_fold (SCM (*fn
) (), void *closure
, SCM init
, SCM table
)
506 int i
, n
= SCM_LENGTH (table
);
508 for (i
= 0; i
< n
; ++i
)
510 SCM ls
= SCM_VELTS (table
)[i
], handle
;
511 while (SCM_NNULLP (ls
))
513 SCM_ASSERT (SCM_NIMP (ls
) && SCM_CONSP (ls
),
514 table
, SCM_ARG1
, s_scm_hash_fold
);
515 handle
= SCM_CAR (ls
);
516 SCM_ASSERT (SCM_NIMP (handle
) && SCM_CONSP (handle
),
517 table
, SCM_ARG1
, s_scm_hash_fold
);
518 result
= fn (closure
, SCM_CAR (handle
), SCM_CDR (handle
), result
);