58 lines
16 KiB
Plaintext
58 lines
16 KiB
Plaintext
Copyright (c) 1986 Xerox Corporation. All rights reserved.
|
||
|
||
2
|
||
|
||
6.2 User-Specified Hashing Functions
|
||
1
|
||
|
||
In general terms, when a key is looked up in a hash array, it is converted to an integer, which is used to index into a linear array. If the key is not the same as the one found at that index, other indices are tried until it the desired key is found. The value stored with that key is then returned (from GETHASH) or replaced (from PUTHASH).
|
||
The important features of this algorithm, for purposes of customizing hash arrays, are (1) the "hashing function" used to convert a key to an integer; and (2) the comparison function used to compare the key found in the array with the key being looked up. In order for hash arrays to work correctly, any two objects which are equal according to the comparison function must "hash" to equal integers.
|
||
By default, the Interlisp hash array functions use a hashing function that computes an integer from the internal address of a key, and use EQ for comparing keys. This means that if non-atoms are used as hash keys, the exact same object (not a copy) must be used to retrieve the hash value.
|
||
There are some applications for which the EQ constraint is too restrictive. For example, it may be useful to use strings as hash keys, without the restriction that EQUAL but not EQ strings are considered to be different hash keys.
|
||
The user can override this default behavior for any hash array by specifying the functions used to compare keys and to "hash" a key to a number. This can be done by giving the HASHBITSFN and EQUIVFN arguments to HASHARRAY (("HASHARRAY" . Function)).
|
||
The EQUIVFN argument is a function of two arguments that returns non-NIL when its arguments are considered equal. The HASHBITSFN argument is a function of one argument that produces a positive small integer (in the range [0..2^16-1]) with the property that objects that are considered equal by the EQUIVFN produce the same hash bits.
|
||
For an existing hash array, the function HARRAYPROP (("HARRAYPROP" . Function)) can be used to examine the hashing and equivalence functions as the HASHBITSFN and EQUIVFN hash array properties. These properties are read-only for non-empty hash arrays, as it makes no sense to change the equivalence relationship once some keys have been hashed.
|
||
The following function is useful for creating hash arrays that take strings as hash keys:
|
||
(STRINGHASHBITS STRING) [Function]
|
||
1
|
||
|
||
Hashes the string STRING into an integer that can be used as a HASHBITSFN for a hash array. Strings which are STREQUAL hash to the same integer.
|
||
1
|
||
|
||
Example:
|
||
(HASHARRAY MINKEYS OVERFLOW 'STRINGHASHBITS 'STREQUAL)
|
||
creates a hash array where you can use strings as hash keys.
|
||
|