Next: Red-Black Trees, Previous: Hash Tables, Up: Associations
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.
hash
associates an exact non-negative integer with object and returns that integer. Ifhash
was previously called with object as its argument, the integer returned is the same as was returned by the previous call.hash
guarantees that distinct objects (in the sense ofeq?
) are associated with distinct integers.
unhash
takes 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-argument
is signalled. In other words, ifhash
previously 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-hash
is likehash
, except that it accepts an additional optional argument, insert?. If insert? is supplied and is#f
,object-hash
will 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-hash
always returns an integer, creating an association in the table if necessary.
object-hash
additionally treats#f
differently than doeshash
. Callingobject-hash
with#f
as its argument will return an integer that, when passed tounhash
, will signal an error rather than returning#f
. Likewise,valid-hash-number?
will return#f
for this integer.
object-unhash
is likeunhash
, except that when k is not associated with any object or was previously associated with an object that has been reclaimed,object-unhash
returns#f
. This means that there is an ambiguity in the value returned byobject-unhash
: if#f
is returned, there is no way to tell if k is associated with#f
or is not associated with any object at all.
Finally, this procedure makes new object-hash tables: