The MIT/GNU Scheme object-hashing facility provides a mechanism for generating a unique hash number for an arbitrary object. This hash number, unlike an object's address, is unchanged by garbage collection. The object-hashing facility is useful in conjunction with hash tables, but it may be used for other things as well. In particular, it is used in the generation of the written representation for many objects (see Custom Output).
All of these procedures accept an optional argument called table;
this table contains the object-integer associations. If given, this
argument must be an object-hash table as constructed by
hash-table/make (see below). If not given, a default table is
hashassociates an exact non-negative integer with object and returns that integer. If
hashwas previously called with object as its argument, the integer returned is the same as was returned by the previous call.
hashguarantees that distinct objects (in the sense of
eq?) are associated with distinct integers.
unhashtakes an exact non-negative integer k and returns the object associated with that integer. If there is no object associated with k, or if the object previously associated with k has been reclaimed by the garbage collector, an error of type
condition-type:bad-range-argumentis signalled. In other words, if
hashpreviously returned k for some object, and that object has not been reclaimed, it is the value of the call to
An object that is passed to
hash as an argument is not protected
from being reclaimed by the garbage collector. If all other references
to that object are eliminated, the object will be reclaimed.
unhash with the hash number of the (now
reclaimed) object will signal an error.
(define x (cons 0 0)) ⇒ unspecified (hash x) ⇒ 77 (eqv? (hash x) (hash x)) ⇒ #t (define x 0) ⇒ unspecified (gc-flip) ;force a garbage collection (unhash 77) error-->
This predicate is true if object has an associated hash number. Otherwise it is false.
This predicate is true if k is the hash number associated with some object. Otherwise it is false.
The following two procedures provide a lower-level interface to the object-hashing mechanism.
hash, except that it accepts an additional optional argument, insert?. If insert? is supplied and is
object-hashwill return an integer for object only if there is already an association in the table; otherwise, it will return
#f. If insert? is not supplied, or is not
object-hashalways returns an integer, creating an association in the table if necessary.
#fdifferently than does
#fas its argument will return an integer that, when passed to
unhash, will signal an error rather than returning
#ffor this integer.
unhash, except that when k is not associated with any object or was previously associated with an object that has been reclaimed,
#f. This means that there is an ambiguity in the value returned by
#fis returned, there is no way to tell if k is associated with
#for is not associated with any object at all.
Finally, this procedure makes new object-hash tables: