18.2. Macro `EXT:DEFINE-HASH-TABLE-TEST`
Prev | Chapter 18. Hash Tables | Next

## 18.2. Macro `EXT:DEFINE-HASH-TABLE-TEST`

You can define a new hash table test using the macro `EXT:DEFINE-HASH-TABLE-TEST`: `(`EXT:DEFINE-HASH-TABLE-TEST` test-name `test-function` `hash-function`)`, after which `name` can be passed as the `:TEST` argument to `MAKE-HASH-TABLE`. E.g.:

    (EXT:DEFINE-HASH-TABLE-TEST string STRING= SXHASH)
    ⇒ STRING
    (MAKE-HASH-TABLE :test 'string)
    ⇒ #S(HASH-TABLE :TEST (#<SYSTEM-FUNCTION STRING=> . #<SYSTEM-FUNCTION SXHASH>))

(which is not too useful because it is equivalent to an `EQUAL` `HASH-TABLE` but less efficient).

The fundamental requirement is that the `test-function` and `hash-function` are consistent:

    (FUNCALL test-function x y) ⇒
    (= (FUNCALL hash-function x) (FUNCALL hash-function y))

This means that the following definition:

    (EXT:DEFINE-HASH-TABLE-TEST number = SXHASH)	; broken!

is not correct because

    (= 1 1d0)
    ⇒ T	; same object!
    (= (SXHASH 1) (SXHASH 1d0))
    ⇒ NIL	; different buckets!

The correct way is, e.g.:

    (EXT:DEFINE-HASH-TABLE-TEST number = (LAMBDA (x) (SXHASH (COERCE x 'SHORT-FLOAT))))

### Note

Note that `COERCE`ing to a `SHORT-FLOAT` does not cons up fresh objects while `COERCE`ing to a `DOUBLE-FLOAT` does.

| These notes document CLISP version 2.45 | Last modified: 2008-05-14

Enter your search termsSubmit search form
Webclisp.cons.orgclisp.podval.orgwww.lisp.org

Prev | Up | Next
18.1. Function `MAKE-HASH-TABLE` | Home | 18.3. Function `HASH-TABLE-TEST`