Next: , Previous: Queues, Up: System features


4.1.9 Hash tables

Scheme48 provides a simple hash table facility in the structure tables.

— procedure: make-table [hasher] –> table
— procedure: make-string-table –> string-table
— procedure: make-symbol-table –> symbol-table
— procedure: make-integer-table –> integer-table

Hash table constructors. Make-table creates a table that hashes keys either with hasher, if it is passed to make-table, or default-hash-function, and it compares keys for equality with eq?, unless they are numbers, in which case it compares with eqv?. Make-string-table makes a table whose hash function is string-hash and that compares the equality of keys with string=?. Make-symbol-table constructs a table that hashes symbol keys by converting them to strings and hashing them with string-hash; it compares keys' equality by eq?. Tables made by make-integer-table hash keys by taking their absolute value, and test for key equality with the = procedure.

— procedure: make-table-maker comparator hasher –> table-maker

Customized table constructor constructor: this returns a nullary procedure that creates a new table that uses comparator to compare keys for equality and hasher to hash keys.

— procedure: table? object –> boolean

Hash table disjoint type predicate.

— procedure: table-ref table key –> value or #f
— procedure: table-set! table key value –> unspecified

Table-ref returns the value associated with key in table, or #f if there is no such association. If value is #f, table-set! ensures that there is no longer an association with key in table; if value is any other value, table-set! creates a new association or assigns an existing one in table whose key is key and whose associated value is value.

— procedure: table-walk proc table –> unspecified

Table-walk applies proc to the key & value, in that order of arguments, of every association in table.

— procedure: make-table-immutable! table –> table

This makes the structure of table immutable, though not its contents. Table-set! may not be used with tables that have been made immutable.

— procedure: default-hash-function value –> integer-hash-code
— procedure: string-hash string –> integer-hash-code

Two built-in hashing functions. Default-hash-function can hash any Scheme value that could usefully be used in a case clause. String-hash is likely to be fast, as it is implemented as a VM primitive. String-hash is the same as what the features structure exports under the same name.