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
used.
hashassociates an exact non-negative integer with object and returns that integer. Ifhashwas 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 ofeq?) 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 typecondition-type:bad-range-argumentis signalled. In other words, ifhashpreviously returned k for some object, and that object has not been reclaimed, it is the value of the call tounhash.
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.
Subsequently calling 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.
object-hashis likehash, except that it accepts an additional optional argument, insert?. If insert? is supplied and is#f,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#f,object-hashalways returns an integer, creating an association in the table if necessary.
object-hashadditionally treats#fdifferently than doeshash. Callingobject-hashwith#fas its argument will return an integer that, when passed tounhash, will signal an error rather than returning#f. Likewise,valid-hash-number?will return#ffor this integer.
object-unhashis likeunhash, except that when k is not associated with any object or was previously associated with an object that has been reclaimed,object-unhashreturns#f. This means that there is an ambiguity in the value returned byobject-unhash: if#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: