Star Sapphire Common LISP Home

Download Star Saphire
Index

16. HASH TABLES

This chapter contains the following sections:

16.0 Introduction to Hash Tables

16.1 Hash Table Functions

 

16.0 Introduction to Hash Tables

A hash table is a LISP object that can efficiently map an arbitrary LISP object to another LISP object. Each hash table has a set of entries, each of which associates a particular key with a particular value. The basic functions that deal with hash tables can create entries,and find the value associated with a given key. Finding the value s very fast, even if there are many entries; this is the key advantage of hash tables over other similar data structures such as property or association lists.

The mapping between values and keys is strictly one-to-one. Entering a second value for a given key will replace the previous value Adding a value to a hash table is a destructive operation; it modifies the hash table. Association lists can be augmented non-destructively.

Hash tables work by using the internal representation of an object to compute a number (the hash number) which is used as an index into an array which actually stores the entries. If more than one subsequent entry maps to the same hash number, a collision arises . In this case a chain of entries is constructed which is searched linearly. If the table is large enough, less collisions will occur and the access will be faster. Although the collision search can be optimized, it is usually better to try to optimize the hash function or increase the size of the hash table so that fewer collisions occur. All these details are handled at the implementation level; there is no need for the LISP programmer to worry about them.

Star Sapphire hash tables can use an arbitrary LISP predicate to compare objects being entered. The default is eql. Typically the key will be a symbol (although it can be any object); values are typically any LISP object.

The hash table functions use the same routines which are used internally to maintain packages; however, these routines are completely reentrant so user defined hash tables will have no influence on the package hash tables.

Hash tables are created with the function make-hash-table, which takes various options. To look up a key and get the associated value, use get-hash. New entries are added to a hash table using setf with gethash; the setf method is puthash (and can be used directly if portability is not important).

Here is a simple example:

(setq ht (make-hash-table))

(setf (gethash 'x ht) 15)

(setf (gethash 'y ht) 2)

(gethash 'x ht) => 15

(gethash 'y ht) => 2

In this example, x and y are keys and the numbers 15 and 2 are the respective values.

When a hash table is first allocated, it has a size which is the maximum number of entries that can be entered. Common LISP hash tables are rehashed automatically when an overflow condition is detected. A new, larger hash table is allocated and entries are reentered in the larger hash table so that the association between key and values is maintained.

This is completely transparent to the user; the parameters of this rehashing can be adjusted using options to make-hash. However, there is usually no pressing reason to use the make-hash arguments. Common LISP hash tables can be viewed as essentially infinite capacity, rapid access associative memory stores.

 

16.1 Hash Table Functions

The following are the hash-table functions supported in Star Sapphire.

make-hash-table

hash-table-p

gethash

puthash

clrhash

hash-table-count

hash-table-rehash-size

hash-table-rehash-threshold

hash-table-size

hash-table-test

The last four of these are specified in ANSI Common LISP but not in the first edition of Steele.